a0b396805692ff52ef0b0394bbd7d3165dcabb7a
[moodle.git] / calendar / tests / events_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains the class that handles testing of the calendar events.
19  *
20  * @package core_calendar
21  * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com>
22  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
27 require_once($CFG->dirroot . '/calendar/tests/externallib_test.php');
29 /**
30  * This file contains the class that handles testing of the calendar events.
31  *
32  * @package core_calendar
33  * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com>
34  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class core_calendar_events_testcase extends advanced_testcase {
38     /**
39      * The test user.
40      */
41     private $user;
43     /**
44      * The test course.
45      */
46     private $course;
48     /**
49      * Test set up.
50      */
51     protected function setUp() {
52         global $USER;
53         // The user we are going to test this on.
54         $this->setAdminUser();
55         $this->user = $USER;
56         $this->course = self::getDataGenerator()->create_course();
57     }
59     /**
60      * Tests for calendar_event_created event.
61      */
62     public function test_calendar_event_created() {
64         $this->resetAfterTest();
66         // Catch the events.
67         $sink = $this->redirectEvents();
69         // Create a calendar event.
70         $record = new stdClass();
71         $record->courseid = 0;
72         $time = time();
73         $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
74                 $record); // User event.
76         // Capture the event.
77         $events = $sink->get_events();
78         $sink->clear();
80         // Validate the event.
81         $event = $events[0];
82         $this->assertInstanceOf('\core\event\calendar_event_created', $event);
83         $this->assertEquals('event', $event->objecttable);
84         $this->assertEquals(0, $event->courseid);
85         $this->assertEquals($calevent->context, $event->get_context());
86         $expectedlog = array(0, 'calendar', 'add', 'event.php?action=edit&amp;id=' . $calevent->id , $calevent->name);
87         $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
88         $this->assertEquals($other, $event->other);
89         $this->assertEventLegacyLogData($expectedlog, $event);
90         $this->assertEventContextNotUsed($event);
92         // Now we create a repeated course event.
93         $record = new stdClass();
94         $record->courseid = $this->course->id;
95         $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, $time,
96                 $record);
97         $events = $sink->get_events();
98         $sink->close();
100         $this->assertEquals(10, count($events));
101         foreach ($events as $event) {
102             $this->assertInstanceOf('\core\event\calendar_event_created', $event);
103             $this->assertEquals('event', $event->objecttable);
104             $this->assertEquals($this->course->id, $event->courseid);
105             $this->assertEquals($calevent->context, $event->get_context());
106         }
107     }
109     /**
110      * Tests for event validations related to calendar_event_created event.
111      */
112     public function test_calendar_event_created_validations() {
113         $this->resetAfterTest();
114         $context = context_user::instance($this->user->id);
116         // Test not setting other['repeatid'].
117         try {
118             \core\event\calendar_event_created::create(array(
119                 'context'  => $context,
120                 'objectid' => 2,
121                 'other' => array(
122                     'timestart' => time(),
123                     'name' => 'event'
124                 )
125             ));
126             $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
127                     other['repeatid']");
128         } catch (coding_exception $e) {
129             $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
130         }
132         // Test not setting other['name'].
133         try {
134             \core\event\calendar_event_created::create(array(
135                 'context'  => $context,
136                 'objectid' => 2,
137                 'other' => array(
138                     'repeatid' => 0,
139                     'timestart' => time(),
140                 )
141             ));
142             $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
143                     other['name']");
144         } catch (coding_exception $e) {
145             $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
146         }
148         // Test not setting other['timestart'].
149         try {
150             \core\event\calendar_event_created::create(array(
151                 'context'  => $context,
152                 'objectid' => 2,
153                 'other' => array(
154                     'name' => 'event',
155                     'repeatid' => 0,
156                 )
157             ));
158             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
159                     other['timestart']");
160         } catch (coding_exception $e) {
161             $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
162         }
163     }
165     /**
166      * Tests for calendar_event_updated event.
167      */
168     public function test_calendar_event_updated() {
170         $this->resetAfterTest();
172         // Create a calendar event.
173         $record = new stdClass();
174         $record->courseid = 0;
175         $time = time();
176         $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
177                 $record); // User event.
179         // Catch the events.
180         $sink = $this->redirectEvents();
181         $prop = new stdClass();
182         $prop->name = 'new event';
183         $calevent->update($prop); // Update calender event.
184         // Capture the event.
185         $events = $sink->get_events();
187         // Validate the event.
188         $event = $events[0];
189         $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
190         $this->assertEquals('event', $event->objecttable);
191         $this->assertEquals(0, $event->courseid);
192         $this->assertEquals($calevent->context, $event->get_context());
193         $expectedlog = array(0, 'calendar', 'edit', 'event.php?action=edit&amp;id=' . $calevent->id , $calevent->name);
194         $this->assertEventLegacyLogData($expectedlog, $event);
195         $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'new event');
196         $this->assertEquals($other, $event->other);
197         $this->assertEventContextNotUsed($event);
199         // Now we create a repeated course event and update it.
200         $record = new stdClass();
201         $record->courseid = $this->course->id;
202         $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(),
203                 $record);
205         $sink->clear();
206         $prop = new stdClass();
207         $prop->name = 'new event';
208         $prop->repeateditall = true;
209         $calevent->update($prop); // Update calender event.
210         $events = $sink->get_events();
211         $sink->close();
213         $this->assertEquals(10, count($events));
214         foreach ($events as $event) {
215             $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
216             $this->assertEquals('event', $event->objecttable);
217             $this->assertEquals($this->course->id, $event->courseid);
218             $this->assertEquals($calevent->context, $event->get_context());
219         }
220     }
222     /**
223      * Tests for event validations related to calendar_event_created event.
224      */
225     public function test_calendar_event_updated_validations() {
226         $this->resetAfterTest();
227         $context = context_user::instance($this->user->id);
229         // Test not setting other['repeatid'].
230         try {
231             \core\event\calendar_event_updated::create(array(
232                 'context'  => $context,
233                 'objectid' => 2,
234                 'other' => array(
235                     'timestart' => time(),
236                     'name' => 'event'
237                 )
238             ));
239             $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
240                     other['repeatid']");
241         } catch (coding_exception $e) {
242             $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
243         }
245         // Test not setting other['name'].
246         try {
247             \core\event\calendar_event_updated::create(array(
248                 'context'  => $context,
249                 'objectid' => 2,
250                 'other' => array(
251                     'repeatid' => 0,
252                     'timestart' => time(),
253                 )
254             ));
255             $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
256                     other['name']");
257         } catch (coding_exception $e) {
258             $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
259         }
261         // Test not setting other['timestart'].
262         try {
263             \core\event\calendar_event_updated::create(array(
264                 'context'  => $context,
265                 'objectid' => 2,
266                 'other' => array(
267                     'name' => 'event',
268                     'repeatid' => 0,
269                 )
270             ));
271             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
272                     other['timestart']");
273         } catch (coding_exception $e) {
274             $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
275         }
276     }
278     /**
279      * Tests for calendar_event_deleted event.
280      */
281     public function test_calendar_event_deleted() {
282         global $DB;
284         $this->resetAfterTest();
286         // Create a calendar event.
287         $record = new stdClass();
288         $record->courseid = 0;
289         $record->repeatid = 0;
290         $time = time();
291         $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
292             $record); // User event.
293         $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
295         // Catch the events.
296         $sink = $this->redirectEvents();
297         $calevent->delete(false);
298         $events = $sink->get_events();
300         // Validate the event.
301         $event = $events[0];
302         $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
303         $this->assertEquals('event', $event->objecttable);
304         $this->assertEquals(0, $event->courseid);
305         $this->assertEquals($calevent->context, $event->get_context());
306         $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
307         $this->assertEquals($other, $event->other);
308         $this->assertEventContextNotUsed($event);
309         $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
311         // Now we create a repeated course event and delete it.
312         $record = new stdClass();
313         $record->courseid = $this->course->id;
314         $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(),
315             $record);
317         $sink->clear();
318         $prop = new stdClass();
319         $prop->name = 'new event';
320         $prop->repeateditall = true;
321         $calevent->delete(true);
322         $events = $sink->get_events();
323         $sink->close();
325         $this->assertEquals(10, count($events));
326         foreach ($events as $event) {
327             $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
328             $this->assertEquals('event', $event->objecttable);
329             $this->assertEquals($this->course->id, $event->courseid);
330             $this->assertEquals($calevent->context, $event->get_context());
331         }
332     }
334     /**
335      * Tests for event validations related to calendar_event_deleted event.
336      */
337     public function test_calendar_event_deleted_validations() {
338         $this->resetAfterTest();
339         $context = context_user::instance($this->user->id);
341         // Test not setting other['repeatid'].
342         try {
343             \core\event\calendar_event_deleted::create(array(
344                 'context'  => $context,
345                 'objectid' => 2,
346                 'other' => array(
347                     'timestart' => time(),
348                     'name' => 'event'
349                 )
350             ));
351             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
352                     other['repeatid']");
353         } catch (coding_exception $e) {
354             $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
355         }
357         // Test not setting other['name'].
358         try {
359             \core\event\calendar_event_deleted::create(array(
360                 'context'  => $context,
361                 'objectid' => 2,
362                 'other' => array(
363                     'repeatid' => 0,
364                     'timestart' => time(),
365                 )
366             ));
367             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
368                     other['name']");
369         } catch (coding_exception $e) {
370             $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
371         }
373         // Test not setting other['timestart'].
374         try {
375             \core\event\calendar_event_deleted::create(array(
376                 'context'  => $context,
377                 'objectid' => 2,
378                 'other' => array(
379                     'name' => 'event',
380                     'repeatid' => 0,
381                 )
382             ));
383             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
384                     other['timestart']");
385         } catch (coding_exception $e) {
386             $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
387         }
388     }