-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCalendarFeedTest.php
More file actions
238 lines (197 loc) · 8.37 KB
/
CalendarFeedTest.php
File metadata and controls
238 lines (197 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace Tests\Feature\Http\Controllers;
use App\Enums\OrganizationStatus;
use App\Models\Event;
use App\Models\Org;
use Tests\DatabaseTestCase;
class CalendarFeedTest extends DatabaseTestCase
{
public function test_index_displays_only_active_organizations(): void
{
// Arrange
$first_org = Org::factory()->create([
'title' => 'First Active Org',
'status' => OrganizationStatus::Active,
]);
$second_org = Org::factory()->create([
'title' => 'Second Active Org',
'status' => OrganizationStatus::Active,
]);
$inactive_org = Org::factory()->create([
'title' => 'Inactive Org',
'status' => OrganizationStatus::InActive,
]);
$this->get(route('calendar-feed.index'))
->assertOk()
->assertViewIs('calendar-feed.index')
->assertViewHas('organizations', fn ($organizations) => $organizations->count() === 2
&& $organizations->contains('id', $first_org->id)
&& $organizations->contains('id', $second_org->id)
&& $organizations->contains('id', $inactive_org->id) === false
&& $organizations->firstWhere('id', $first_org->id)['checked']
&& $organizations->firstWhere('id', $second_org->id)['checked']);
}
public function test_show_generates_calendar_with_multiple_organizations_and_unique_uids(): void
{
$first_org = Org::factory()->create([
'title' => 'First Organization',
'status' => OrganizationStatus::Active,
]);
$second_org = Org::factory()->create([
'title' => 'Second Organization',
'status' => OrganizationStatus::Active,
]);
// Create multiple events for each organization
$first_org_events = Event::factory(3)->create([
'organization_id' => $first_org->id,
'cancelled_at' => null,
'active_at' => now()->addHour(),
'expire_at' => now()->addHours(3),
'event_name' => 'Event 1',
]);
$second_org_events = Event::factory(2)->create([
'organization_id' => $second_org->id,
'cancelled_at' => null,
'active_at' => now()->addDay(),
'expire_at' => now()->addDay()->addHours(2),
'event_name' => 'Event hello!',
]);
$response = $this->get(route('calendar-feed.show', [
'orgs' => "{$first_org->id}-{$second_org->id}",
]));
$response->assertOk();
$calendar_content = $response->getContent();
// Verify all events are present
$this->assertEquals(
5,
mb_substr_count($calendar_content, 'BEGIN:VEVENT'),
'Calendar should contain exactly 5 events'
);
// Extract all UIDs from the calendar content
preg_match_all('/UID:(.+)/', $calendar_content, $matches);
$uids = $matches[1];
// Verify we have the correct number of UIDs
$this->assertCount(5, $uids, 'Should have 5 UIDs');
// Verify all UIDs are unique
$unique_uids = array_unique($uids);
$this->assertCount(
5,
$unique_uids,
'All UIDs in the calendar should be unique'
);
// Verify each event from both organizations is present
foreach ($first_org_events as $event) {
$this->assertStringContainsString(
"SUMMARY:{$event->event_name}",
$calendar_content,
"Event '{$event->event_name}' from first organization should be in calendar"
);
}
foreach ($second_org_events as $event) {
$this->assertStringContainsString(
"SUMMARY:{$event->event_name}",
$calendar_content,
"Event '{$event->event_name}' from second organization should be in calendar"
);
}
// Verify calendar has multiple organization header
$this->assertStringContainsString(
'PRODID:HackGreenville.com Event Calendar',
$calendar_content,
'Calendar should have multiple organization header'
);
}
public function test_show_handles_cancelled_events_correctly(): void
{
// Arrange
$organization = Org::factory()->create(['status' => OrganizationStatus::Active]);
$active_event = Event::factory()->create([
'organization_id' => $organization->id,
'cancelled_at' => null,
'event_name' => 'Active Event',
'active_at' => now()->addHour(),
'expire_at' => now()->addHours(3),
]);
$cancelled_event = Event::factory()->create([
'organization_id' => $organization->id,
'cancelled_at' => now(),
'event_name' => 'Cancelled Event',
'active_at' => now()->addDay(),
'expire_at' => now()->addDay()->addHours(2),
]);
$response = $this->get(route('calendar-feed.show', ['orgs' => $organization->id]));
$calendar_content = $response->getContent();
$this->assertStringContainsString(
'STATUS:CONFIRMED',
$calendar_content,
'Active event should have CONFIRMED status'
);
// We want to ensure the status is confirmed
// as some calenders drop cancelled events
$this->assertStringContainsString(
'STATUS:CONFIRMED',
$calendar_content,
'Cancelled event should have CONFIRMED status'
);
// Verify both events are present but with different statuses
$this->assertStringContainsString(
"SUMMARY:{$active_event->event_name}",
$calendar_content
);
$this->assertStringContainsString(
"SUMMARY:[CANCELLED] {$cancelled_event->event_name}",
$calendar_content
);
}
public function test_show_generates_deterministic_uids(): void
{
$organization = Org::factory()->create(['status' => OrganizationStatus::Active]);
$event = Event::factory()->create([
'organization_id' => $organization->id,
'cancelled_at' => null,
'active_at' => now()->addDay(),
'expire_at' => now()->addDay(),
]);
// Get calendar content twice
$first_response = $this->get(route('calendar-feed.show', ['orgs' => $organization->id]));
$second_response = $this->get(route('calendar-feed.show', ['orgs' => $organization->id]));
// Extract UIDs from both responses
preg_match('/UID:(.+)/', $first_response->getContent(), $first_match);
preg_match('/UID:(.+)/', $second_response->getContent(), $second_match);
// Assert - Same event should have same UID in different requests
$this->assertEquals(
$first_match[1],
$second_match[1],
'UIDs should be deterministic for the same event'
);
}
public function test_index_filters_to_only_selected_organizations(): void
{
$selected_org = Org::factory()->create([
'title' => 'Selected Org',
'status' => OrganizationStatus::Active,
]);
$unselected_org = Org::factory()->create([
'title' => 'Unselected Org',
'status' => OrganizationStatus::Active,
]);
$this->get(route('calendar-feed.index', ['orgs' => (string) $selected_org->id]))
->assertOk()
->assertViewHas('organizations', fn ($organizations) => $organizations->count() === 1
&& $organizations->contains('id', $selected_org->id)
&& $organizations->contains('id', $unselected_org->id) === false);
}
public function test_older_events_never_show_up_on_calendar_feed(): void
{
$organization = Org::factory()->create(['status' => OrganizationStatus::Active]);
$event = Event::factory()->create([
'organization_id' => $organization->id,
'cancelled_at' => null,
'active_at' => now()->subWeek()->subMinute(),
'expire_at' => now()->subWeek(),
]);
$first_response = $this->get(route('calendar-feed.show', ['orgs' => $organization->id]));
preg_match('/UID:(.+)/', $first_response->getContent(), $first_match);
$this->assertFalse(isset($first_match[1]));
}
}