Merge branch 'MDL-60667_master' of git://github.com/dmonllao/moodle
[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 calendar_event_updated event.
224      */
225     public function test_calendar_event_updated_toggle_visibility() {
226         global $DB, $SITE;
228         $this->resetAfterTest();
230         // Create a calendar event.
231         $time = time();
232         $calevent = core_calendar_externallib_testcase::create_calendar_event('Some wickedly awesome event yo!',
233             $this->user->id, 'user', 0, $time);
235         // Updated the visibility of the calendar event.
236         $sink = $this->redirectEvents();
237         $calevent->toggle_visibility();
238         $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
239         $events = $sink->get_events();
241         // Validate the calendar_event_updated event.
242         $event = $events[0];
243         $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
244         $this->assertEquals('event', $event->objecttable);
245         $this->assertEquals($SITE->id, $event->courseid);
246         $this->assertEquals($calevent->context, $event->get_context());
247         $expectedlog = array($SITE->id, 'calendar', 'edit', 'event.php?action=edit&amp;id=' . $calevent->id ,
248             $calevent->name);
249         $this->assertEventLegacyLogData($expectedlog, $event);
250         $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'Some wickedly awesome event yo!');
251         $this->assertEquals($other, $event->other);
252         $this->assertEventContextNotUsed($event);
253         $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
255     }
257     /**
258      * Tests for event validations related to calendar_event_created event.
259      */
260     public function test_calendar_event_updated_validations() {
261         $this->resetAfterTest();
262         $context = context_user::instance($this->user->id);
264         // Test not setting other['repeatid'].
265         try {
266             \core\event\calendar_event_updated::create(array(
267                 'context'  => $context,
268                 'objectid' => 2,
269                 'other' => array(
270                     'timestart' => time(),
271                     'name' => 'event'
272                 )
273             ));
274             $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
275                     other['repeatid']");
276         } catch (coding_exception $e) {
277             $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
278         }
280         // Test not setting other['name'].
281         try {
282             \core\event\calendar_event_updated::create(array(
283                 'context'  => $context,
284                 'objectid' => 2,
285                 'other' => array(
286                     'repeatid' => 0,
287                     'timestart' => time(),
288                 )
289             ));
290             $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
291                     other['name']");
292         } catch (coding_exception $e) {
293             $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
294         }
296         // Test not setting other['timestart'].
297         try {
298             \core\event\calendar_event_updated::create(array(
299                 'context'  => $context,
300                 'objectid' => 2,
301                 'other' => array(
302                     'name' => 'event',
303                     'repeatid' => 0,
304                 )
305             ));
306             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
307                     other['timestart']");
308         } catch (coding_exception $e) {
309             $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
310         }
311     }
313     /**
314      * Tests for calendar_event_deleted event.
315      */
316     public function test_calendar_event_deleted() {
317         global $DB;
319         $this->resetAfterTest();
321         // Create a calendar event.
322         $record = new stdClass();
323         $record->courseid = 0;
324         $record->repeatid = 0;
325         $time = time();
326         $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
327             $record); // User event.
328         $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
330         // Catch the events.
331         $sink = $this->redirectEvents();
332         $calevent->delete(false);
333         $events = $sink->get_events();
335         // Validate the event.
336         $event = $events[0];
337         $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
338         $this->assertEquals('event', $event->objecttable);
339         $this->assertEquals(0, $event->courseid);
340         $this->assertEquals($calevent->context, $event->get_context());
341         $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
342         $this->assertEquals($other, $event->other);
343         $this->assertEventContextNotUsed($event);
344         $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
346         // Now we create a repeated course event and delete it.
347         $record = new stdClass();
348         $record->courseid = $this->course->id;
349         $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(),
350             $record);
352         $sink->clear();
353         $prop = new stdClass();
354         $prop->name = 'new event';
355         $prop->repeateditall = true;
356         $calevent->delete(true);
357         $events = $sink->get_events();
358         $sink->close();
360         $this->assertEquals(10, count($events));
361         foreach ($events as $event) {
362             $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
363             $this->assertEquals('event', $event->objecttable);
364             $this->assertEquals($this->course->id, $event->courseid);
365             $this->assertEquals($calevent->context, $event->get_context());
366         }
367     }
369     /**
370      * Tests for event validations related to calendar_event_deleted event.
371      */
372     public function test_calendar_event_deleted_validations() {
373         $this->resetAfterTest();
374         $context = context_user::instance($this->user->id);
376         // Test not setting other['repeatid'].
377         try {
378             \core\event\calendar_event_deleted::create(array(
379                 'context'  => $context,
380                 'objectid' => 2,
381                 'other' => array(
382                     'timestart' => time(),
383                     'name' => 'event'
384                 )
385             ));
386             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
387                     other['repeatid']");
388         } catch (coding_exception $e) {
389             $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
390         }
392         // Test not setting other['name'].
393         try {
394             \core\event\calendar_event_deleted::create(array(
395                 'context'  => $context,
396                 'objectid' => 2,
397                 'other' => array(
398                     'repeatid' => 0,
399                     'timestart' => time(),
400                 )
401             ));
402             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
403                     other['name']");
404         } catch (coding_exception $e) {
405             $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
406         }
408         // Test not setting other['timestart'].
409         try {
410             \core\event\calendar_event_deleted::create(array(
411                 'context'  => $context,
412                 'objectid' => 2,
413                 'other' => array(
414                     'name' => 'event',
415                     'repeatid' => 0,
416                 )
417             ));
418             $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
419                     other['timestart']");
420         } catch (coding_exception $e) {
421             $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
422         }
423     }
425     /**
426      * Tests for calendar_subscription_added event for a site subscription.
427      */
428     public function test_calendar_subscription_created_site() {
429         global $CFG;
430         require_once($CFG->dirroot . '/calendar/lib.php');
431         $this->resetAfterTest(true);
433         // Create a mock subscription.
434         $subscription = new stdClass();
435         $subscription->eventtype = 'site';
436         $subscription->name = 'test';
437         $subscription->courseid = $this->course->id;
439         // Trigger and capture the event.
440         $sink = $this->redirectEvents();
441         $id = calendar_add_subscription($subscription);
443         $events = $sink->get_events();
444         $event = reset($events);
445         // Check that the event data is valid.
446         $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
447         $this->assertEquals($id, $event->objectid);
448         $this->assertEquals($subscription->courseid, $event->other['courseid']);
449         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
450         $this->assertArrayNotHasKey('categoryid', $event->other);
451         $this->assertArrayNotHasKey('groupid', $event->other);
452         $this->assertDebuggingNotCalled();
453         $sink->close();
454     }
456     /**
457      * Tests for calendar_subscription_added event for a category subscription.
458      */
459     public function test_calendar_subscription_created_category() {
460         global $CFG;
461         require_once($CFG->dirroot . '/calendar/lib.php');
462         $this->resetAfterTest(true);
464         $categoryid = $this->course->category;
466         // Create a mock subscription.
467         $subscription = new stdClass();
468         $subscription->eventtype = 'category';
469         $subscription->name = 'test';
470         $subscription->categoryid = $categoryid;
472         // Trigger and capture the event.
473         $sink = $this->redirectEvents();
474         $id = calendar_add_subscription($subscription);
476         $events = $sink->get_events();
477         $event = reset($events);
478         // Check that the event data is valid.
479         $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
480         $this->assertEquals($id, $event->objectid);
481         $this->assertEquals($categoryid, $event->other['categoryid']);
482         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
483         $this->assertArrayNotHasKey('courseid', $event->other);
484         $this->assertArrayNotHasKey('groupid', $event->other);
485         $this->assertDebuggingNotCalled();
486         $sink->close();
487     }
489     /**
490      * Tests for calendar_subscription_added event for a course subscription.
491      */
492     public function test_calendar_subscription_created_course() {
493         global $CFG;
494         require_once($CFG->dirroot . '/calendar/lib.php');
495         $this->resetAfterTest(true);
497         // Create a mock subscription.
498         $subscription = new stdClass();
499         $subscription->eventtype = 'course';
500         $subscription->name = 'test';
501         $subscription->courseid = $this->course->id;
503         // Trigger and capture the event.
504         $sink = $this->redirectEvents();
505         $id = calendar_add_subscription($subscription);
507         $events = $sink->get_events();
508         $event = reset($events);
509         // Check that the event data is valid.
510         $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
511         $this->assertEquals($id, $event->objectid);
512         $this->assertEquals($subscription->courseid, $event->other['courseid']);
513         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
514         $this->assertArrayNotHasKey('categoryid', $event->other);
515         $this->assertArrayNotHasKey('groupid', $event->other);
516         $this->assertDebuggingNotCalled();
517         $sink->close();
519     }
521     /**
522      * Tests for calendar_subscription_added event for a group subscription.
523      */
524     public function test_calendar_subscription_created_group() {
525         global $CFG;
526         require_once($CFG->dirroot . '/calendar/lib.php');
527         $this->resetAfterTest(true);
529         $courseid = $this->course->id;
530         $groupid = 42;
532         // Create a mock subscription.
533         $subscription = new stdClass();
534         $subscription->eventtype = 'group';
535         $subscription->name = 'test';
536         $subscription->groupid = "{$courseid}-{$groupid}";
538         // Trigger and capture the event.
539         $sink = $this->redirectEvents();
540         $id = calendar_add_subscription($subscription);
542         $events = $sink->get_events();
543         $event = reset($events);
544         // Check that the event data is valid.
545         $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
546         $this->assertEquals($id, $event->objectid);
547         $this->assertEquals($courseid, $event->other['courseid']);
548         $this->assertEquals($groupid, $event->other['groupid']);
549         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
550         $this->assertArrayNotHasKey('categoryid', $event->other);
551         $this->assertDebuggingNotCalled();
552         $sink->close();
553     }
555     /**
556      * Tests for calendar_subscription_updated event for a site subscription.
557      */
558     public function test_calendar_subscription_updated_site() {
559         global $CFG;
560         require_once($CFG->dirroot . '/calendar/lib.php');
561         $this->resetAfterTest(true);
563         // Create a mock subscription.
564         $subscription = new stdClass();
565         $subscription->eventtype = 'site';
566         $subscription->name = 'test';
567         $subscription->courseid = $this->course->id;
568         $subscription->id = calendar_add_subscription($subscription);
569         // Now edit it.
570         $subscription->name = 'awesome';
572         // Trigger and capture the event.
573         $sink = $this->redirectEvents();
574         calendar_update_subscription($subscription);
575         $events = $sink->get_events();
576         $event = reset($events);
577         // Check that the event data is valid.
578         $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
579         $this->assertEquals($subscription->id, $event->objectid);
580         $this->assertEquals($subscription->courseid, $event->other['courseid']);
581         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
582         $this->assertArrayNotHasKey('categoryid', $event->other);
583         $this->assertArrayNotHasKey('groupid', $event->other);
584         $this->assertDebuggingNotCalled();
585         $sink->close();
586     }
588     /**
589      * Tests for calendar_subscription_updated event for a category subscription.
590      */
591     public function test_calendar_subscription_updated_category() {
592         global $CFG;
593         require_once($CFG->dirroot . '/calendar/lib.php');
594         $this->resetAfterTest(true);
596         $categoryid = $this->course->category;
598         // Create a mock subscription.
599         $subscription = new stdClass();
600         $subscription->eventtype = 'category';
601         $subscription->name = 'test';
602         $subscription->categoryid = $categoryid;
603         $subscription->id = calendar_add_subscription($subscription);
604         // Now edit it.
605         $subscription->name = 'awesome';
607         // Trigger and capture the event.
608         $sink = $this->redirectEvents();
609         calendar_update_subscription($subscription);
610         $events = $sink->get_events();
611         $event = reset($events);
612         // Check that the event data is valid.
613         $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
614         $this->assertEquals($subscription->id, $event->objectid);
615         $this->assertEquals($categoryid, $event->other['categoryid']);
616         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
617         $this->assertArrayNotHasKey('courseid', $event->other);
618         $this->assertArrayNotHasKey('groupid', $event->other);
619         $this->assertDebuggingNotCalled();
620         $sink->close();
621     }
623     /**
624      * Tests for calendar_subscription_updated event for a group subscription.
625      */
626     public function test_calendar_subscription_updated_course() {
627         global $CFG;
628         require_once($CFG->dirroot . '/calendar/lib.php');
629         $this->resetAfterTest(true);
631         // Create a mock subscription.
632         $subscription = new stdClass();
633         $subscription->eventtype = 'course';
634         $subscription->name = 'test';
635         $subscription->courseid = $this->course->id;
636         $subscription->id = calendar_add_subscription($subscription);
637         // Now edit it.
638         $subscription->name = 'awesome';
640         // Trigger and capture the event.
641         $sink = $this->redirectEvents();
642         calendar_update_subscription($subscription);
643         $events = $sink->get_events();
644         $event = reset($events);
645         // Check that the event data is valid.
646         $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
647         $this->assertEquals($subscription->id, $event->objectid);
648         $this->assertEquals($this->course->id, $event->other['courseid']);
649         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
650         $this->assertArrayNotHasKey('categoryid', $event->other);
651         $this->assertArrayNotHasKey('groupid', $event->other);
652         $this->assertDebuggingNotCalled();
653         $sink->close();
654     }
656     /**
657      * Tests for calendar_subscription_updated event for a course subscription.
658      */
659     public function test_calendar_subscription_updated_group() {
660         global $CFG;
661         require_once($CFG->dirroot . '/calendar/lib.php');
662         $this->resetAfterTest(true);
664         $courseid = $this->course->id;
665         $groupid = 42;
667         // Create a mock subscription.
668         $subscription = new stdClass();
669         $subscription->eventtype = 'group';
670         $subscription->name = 'test';
671         $subscription->groupid = "{$courseid}-{$groupid}";
672         $subscription->id = calendar_add_subscription($subscription);
673         // Now edit it.
674         $subscription->name = 'awesome';
676         // Trigger and capture the event.
677         $sink = $this->redirectEvents();
678         calendar_update_subscription($subscription);
679         $events = $sink->get_events();
680         $event = reset($events);
681         // Check that the event data is valid.
682         $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
683         $this->assertEquals($subscription->id, $event->objectid);
684         $this->assertEquals($this->course->id, $event->other['courseid']);
685         $this->assertEquals($groupid, $event->other['groupid']);
686         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
687         $this->assertArrayNotHasKey('categoryid', $event->other);
688         $this->assertDebuggingNotCalled();
689         $sink->close();
690     }
692     /**
693      * Tests for calendar_subscription_deleted event for a site subscription.
694      */
695     public function test_calendar_subscription_deleted_site() {
696         global $CFG;
697         require_once($CFG->dirroot . '/calendar/lib.php');
698         $this->resetAfterTest(true);
700         // Create a mock subscription.
701         $subscription = new stdClass();
702         $subscription->eventtype = 'site';
703         $subscription->name = 'test';
704         $subscription->courseid = $this->course->id;
705         $subscription->id = calendar_add_subscription($subscription);
707         // Trigger and capture the event.
708         $sink = $this->redirectEvents();
709         calendar_delete_subscription($subscription);
710         $events = $sink->get_events();
711         $event = reset($events);
712         // Check that the event data is valid.
713         $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
714         $this->assertEquals($subscription->id, $event->objectid);
715         $this->assertEquals($subscription->courseid, $event->other['courseid']);
716         $this->assertDebuggingNotCalled();
717         $sink->close();
719     }
721     /**
722      * Tests for calendar_subscription_deleted event for a category subscription.
723      */
724     public function test_calendar_subscription_deleted_category() {
725         global $CFG;
726         require_once($CFG->dirroot . '/calendar/lib.php');
727         $this->resetAfterTest(true);
729         $categoryid = $this->course->category;
731         // Create a mock subscription.
732         $subscription = new stdClass();
733         $subscription->eventtype = 'category';
734         $subscription->name = 'test';
735         $subscription->categoryid = $categoryid;
736         $subscription->id = calendar_add_subscription($subscription);
738         // Trigger and capture the event.
739         $sink = $this->redirectEvents();
740         calendar_delete_subscription($subscription);
741         $events = $sink->get_events();
742         $event = reset($events);
743         // Check that the event data is valid.
744         $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
745         $this->assertEquals($subscription->id, $event->objectid);
746         $this->assertEquals($categoryid, $event->other['categoryid']);
747         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
748         $this->assertArrayNotHasKey('courseid', $event->other);
749         $this->assertArrayNotHasKey('groupid', $event->other);
750         $this->assertDebuggingNotCalled();
751         $sink->close();
752     }
754     /**
755      * Tests for calendar_subscription_deleted event for a course.
756      */
757     public function test_calendar_subscription_deleted_course() {
758         global $CFG;
759         require_once($CFG->dirroot . '/calendar/lib.php');
760         $this->resetAfterTest(true);
762         // Create a mock subscription.
763         $subscription = new stdClass();
764         $subscription->eventtype = 'course';
765         $subscription->name = 'test';
766         $subscription->courseid = $this->course->id;
767         $subscription->id = calendar_add_subscription($subscription);
769         // Trigger and capture the event.
770         $sink = $this->redirectEvents();
771         calendar_delete_subscription($subscription);
772         $events = $sink->get_events();
773         $event = reset($events);
774         // Check that the event data is valid.
775         $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
776         $this->assertEquals($subscription->id, $event->objectid);
777         $this->assertEquals($this->course->id, $event->other['courseid']);
778         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
779         $this->assertArrayNotHasKey('categoryid', $event->other);
780         $this->assertArrayNotHasKey('groupid', $event->other);
781         $this->assertDebuggingNotCalled();
782         $sink->close();
783     }
785     /**
786      * Tests for calendar_subscription_deleted event for a group.
787      */
788     public function test_calendar_subscription_deleted_group() {
789         global $CFG;
790         require_once($CFG->dirroot . '/calendar/lib.php');
791         $this->resetAfterTest(true);
793         $courseid = $this->course->id;
794         $groupid = 42;
796         // Create a mock subscription.
797         $subscription = new stdClass();
798         $subscription->eventtype = 'group';
799         $subscription->name = 'test';
800         $subscription->groupid = "{$courseid}-{$groupid}";
801         $subscription->id = calendar_add_subscription($subscription);
803         // Trigger and capture the event.
804         $sink = $this->redirectEvents();
805         calendar_delete_subscription($subscription);
806         $events = $sink->get_events();
807         $event = reset($events);
808         // Check that the event data is valid.
809         $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
810         $this->assertEquals($subscription->id, $event->objectid);
811         $this->assertEquals($this->course->id, $event->other['courseid']);
812         $this->assertEquals($groupid, $event->other['groupid']);
813         $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
814         $this->assertArrayNotHasKey('categoryid', $event->other);
815         $this->assertDebuggingNotCalled();
816         $sink->close();
817     }