forked from mapasculturais/mapasculturais
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventTest.php
More file actions
249 lines (179 loc) · 8.5 KB
/
Copy pathEventTest.php
File metadata and controls
249 lines (179 loc) · 8.5 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
239
240
241
242
243
244
245
246
247
248
249
<?php
require_once __DIR__.'/bootstrap.php';
use MapasCulturais\Entities\Event;
use MapasCulturais\Entities\EventAttendance;
use MapasCulturais\Entities\EventOccurrence;
use MapasCulturais\Entities\EventOccurrenceRecurrence;
use MapasCulturais\Entities\Space;
// Calendário de 1950 para ajudar os testes
// Janeiro
// Se Te Qu Qu Se Sá Do
// 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
// Fevereiro
// Se Te Qu Qu Se Sá Do
// 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
// Março
// Se Te Qu Qu Se Sá Do
// 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
// Abril
// Se Te Qu Qu Se Sá Do
// 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
class EventTest extends MapasCulturais_TestCase{
function __construct($name = null, $data = [], $dataName = '') {
parent::__construct($name, $data, $dataName);
$this->factory->autoPersist = true;
}
function testEvent(){
$this->user = 1;
$event = $this->factory->createEvent();
$event2 = $this->app->repo('Event')->findOneBy(array('name' => $event->name));
$this->assertEquals($event, $event2);
$events = $this->app->repo('Event')->findBy(array('name' => $event->name));
$this->assertEquals(1,count($events));
}
function createQuery($date1, $date2) {
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addEntityResult('MapasCulturais\Entities\EventOccurrence','e');
$rsm->addFieldResult('e', 'id', 'id');
$strNativeQuery = "SELECT * FROM recurring_event_occurrence_for('$date1', '$date2', 'Etc/UTC', NULL)";
return $this->app->em->createNativeQuery($strNativeQuery, $rsm);
}
function testEventOccurrence_single(){
$this->resetTransactions();
$this->user = 1;
$event = $this->factory->createEvent();
$space = $this->factory->createSpace();
$occurrence = $this->factory->createSingleEventOccurrence('1950-09-30 12:00', 120, $space, $event);
$occurrence->save(true);
$query = $this->createQuery('1950-09-30', '1950-10-01');
$occurrence2 = $query->getOneOrNullResult();
$this->assertEquals('1950-09-30', $occurrence2->startsOn->format('Y-m-d'));
$query = $this->createQuery('1950-10-02', '1950-10-05');
$occurrence3 = $query->getOneOrNullResult();
$this->assertEmpty($occurrence3);
}
function testEventOccurrence_daily(){
$this->resetTransactions();
$this->user = 1;
$event = $this->factory->createEvent();
$space = $this->factory->createSpace();
$occurrence = $this->factory->createDailyEventOccurrence('1950-01-02 12:00', 120, '1950-01-09', $space, $event);
$this->assertEmpty($occurrence->validationErrors, print_r($occurrence->validationErrors, true));
$occurrence->save(true);
for ($i=2; $i <= 9 ; $i++) {
$date = '1950-01-0' . $i;
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(1, count($occurrences));
}
$date = '1950-01-10';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(0, count($occurrences));
$date = '1950-01-01';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(0, count($occurrences));
}
function testEventOccurrence_weekly(){
$this->resetTransactions();
$this->user = 1;
$event = $this->factory->createEvent();
$space = $this->factory->createSpace();
$occurrence = $this->factory->createWeeklyEventOccurrence('1950-01-02 12:00', 180, '1950-11-01', [1,3], $space, $event);
$this->assertEmpty($occurrence->validationErrors, print_r($occurrence->validationErrors, true));
$date = '1950-01-02';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(1, count($occurrences));
$date = '1950-01-09';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(1, count($occurrences));
$date = '1950-01-16';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(1, count($occurrences));
$date = '1950-01-03';
$query = $this->createQuery($date, $date);
$occurrences = $query->getArrayResult();
$this->assertEquals(0, count($occurrences));
$occurrences = $this->app->repo('EventOccurrence')->findOneBy(array('frequency' => 'weekly'));
$this->assertNotNull($occurrences);
}
function testEventRules(){
$this->resetTransactions();
$this->user = 1;
$event = $this->factory->createEvent();
$space = $this->factory->createSpace();
$occurrence = new EventOccurrence;
$occurrence->event = $event;
$occurrence->space = $space;
$occurrence->rule = $this->getEventRule();
$this->assertEmpty($occurrence->validationErrors, print_r($occurrence->validationErrors, true));
$occurrence->save(true);
$this->assertEquals('weekly', $occurrence->frequency);
$this->assertEquals(2, count($occurrence->recurrences));
}
function getEventRule(){
return (object) [
"spaceId" => "31",
"startsAt" => "12:31",
"duration" => "00h01",
"frequency" => "weekly",
"startsOn" => "2014-02-10",
"until" => "2014-02-19",
"day" => (object) [ "1" => "on", "3" => "on" ],
"monthly" => "week",
"description" => "test description"
];
}
function testEventAttendance(){
$this->resetTransactions();
$this->user = $this->getUser('normal', 0);
$space = $this->factory->createSpace();
$event = $this->factory->createEvent();
$event_occurrence_1 = $this->factory->createDailyEventOccurrence('1950-01-01 12:00', 120, '1950-02-22', $space, $event);
$event_occurrence_2 = $this->factory->createWeeklyEventOccurrence('1950-01-10 18:00', 30, '1950-01-31', [1,3,5], $space, $event);
$this->user = $this->getUser('normal', 1);
$event_controller = $this->app->controller('event');
$occurrences = $event_controller->apiFindOccurrences (['@from' => '1950-01-05', '@to' => '1950-01-31', '@limit'=>3]);
$oc1 = (object) $occurrences[1];
$attendance = new EventAttendance;
$this->assertCount(2, $attendance->validationErrors, 'assert that empty event attendance returns validation errors');
$attendance->setReccurrenceString('invalid');
$this->assertArrayHasKey('reccurrenceString', $attendance->validationErrors, 'assert that invalid recurrenceString returns validation error');
$attendance->setReccurrenceString($oc1->_reccurrence_string);
$this->assertArrayNotHasKey('reccurrenceString', $attendance->validationErrors, 'assert that a valid recurrenceString don\'t return validation error');
$attendance->type = 'invalid';
$this->assertArrayHasKey('type', $attendance->validationErrors, 'assert that invalid type return validation error');
$attendance->type = EventAttendance::TYPE_CONFIRMATION;
$this->assertArrayNotHasKey('type', $attendance->validationErrors, 'assert that a valid type don\'t return validation error');
$this->assertCount(0, $attendance->validationErrors, 'assert that a valid event attendance dont return validation errors');
$attendance->save(true);
$controller = $this->app->controller('eventAttendance');
$attendances = $controller->apiQuery([
'@select' => '*',
'reccurrenceString' => "EQ({$oc1->_reccurrence_string})"
]);
$this->assertCount(1, $attendances);
$this->assertEquals($oc1->_reccurrence_string, $attendances[0]['reccurrenceString']);
}
}