Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
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
 */
24
 
25
namespace core_calendar\event;
26
 
27
use core_calendar_externallib_testcase;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
global $CFG;
31
require_once($CFG->dirroot . '/calendar/tests/externallib_test.php');
32
 
33
/**
34
 * This file contains the class that handles testing of the calendar events.
35
 *
36
 * @package core_calendar
37
 * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com>
38
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class events_test extends \advanced_testcase {
41
 
42
    /**
43
     * The test user.
44
     */
45
    private $user;
46
 
47
    /**
48
     * The test course.
49
     */
50
    private $course;
51
 
52
    /**
53
     * Test set up.
54
     */
55
    protected function setUp(): void {
56
        global $USER;
57
        // The user we are going to test this on.
58
        $this->setAdminUser();
59
        $this->user = $USER;
60
        $this->course = self::getDataGenerator()->create_course();
61
    }
62
 
63
    /**
64
     * Tests for calendar_event_created event.
65
     */
11 efrain 66
    public function test_calendar_event_created(): void {
1 efrain 67
 
68
        $this->resetAfterTest();
69
 
70
        // Catch the events.
71
        $sink = $this->redirectEvents();
72
 
73
        // Create a calendar event.
74
        $record = new \stdClass();
75
        $record->courseid = 0;
76
        $time = time();
77
        $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time,
78
                $record); // User event.
79
 
80
        // Capture the event.
81
        $events = $sink->get_events();
82
        $sink->clear();
83
 
84
        // Validate the event.
85
        $event = $events[0];
86
        $this->assertInstanceOf('\core\event\calendar_event_created', $event);
87
        $this->assertEquals('event', $event->objecttable);
88
        $this->assertEquals(0, $event->courseid);
89
        $this->assertEquals($calevent->context, $event->get_context());
90
        $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
91
        $this->assertEquals($other, $event->other);
92
        $this->assertEventContextNotUsed($event);
93
 
94
        // Now we create a repeated course event.
95
        $record = new \stdClass();
96
        $record->courseid = $this->course->id;
97
        $calevent = \core_calendar\externallib_test::create_calendar_event('course', $this->user->id, 'course', 10, $time,
98
                $record);
99
        $events = $sink->get_events();
100
        $sink->close();
101
 
102
        $this->assertEquals(10, count($events));
103
        foreach ($events as $event) {
104
            $this->assertInstanceOf('\core\event\calendar_event_created', $event);
105
            $this->assertEquals('event', $event->objecttable);
106
            $this->assertEquals($this->course->id, $event->courseid);
107
            $this->assertEquals($calevent->context, $event->get_context());
108
        }
109
    }
110
 
111
    /**
112
     * Tests for event validations related to calendar_event_created event.
113
     */
11 efrain 114
    public function test_calendar_event_created_validations(): void {
1 efrain 115
        $this->resetAfterTest();
116
        $context = \context_user::instance($this->user->id);
117
 
118
        // Test not setting other['repeatid'].
119
        try {
120
            \core\event\calendar_event_created::create(array(
121
                'context'  => $context,
122
                'objectid' => 2,
123
                'other' => array(
124
                    'timestart' => time(),
125
                    'name' => 'event'
126
                )
127
            ));
128
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
129
                    other['repeatid']");
130
        } catch (\coding_exception $e) {
131
            $this->assertStringContainsString('The \'repeatid\' value must be set in other.', $e->getMessage());
132
        }
133
 
134
        // Test not setting other['name'].
135
        try {
136
            \core\event\calendar_event_created::create(array(
137
                'context'  => $context,
138
                'objectid' => 2,
139
                'other' => array(
140
                    'repeatid' => 0,
141
                    'timestart' => time(),
142
                )
143
            ));
144
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
145
                    other['name']");
146
        } catch (\coding_exception $e) {
147
            $this->assertStringContainsString('The \'name\' value must be set in other.', $e->getMessage());
148
        }
149
 
150
        // Test not setting other['timestart'].
151
        try {
152
            \core\event\calendar_event_created::create(array(
153
                'context'  => $context,
154
                'objectid' => 2,
155
                'other' => array(
156
                    'name' => 'event',
157
                    'repeatid' => 0,
158
                )
159
            ));
160
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
161
                    other['timestart']");
162
        } catch (\coding_exception $e) {
163
            $this->assertStringContainsString('The \'timestart\' value must be set in other.', $e->getMessage());
164
        }
165
    }
166
 
167
    /**
168
     * Tests for calendar_event_updated event.
169
     */
11 efrain 170
    public function test_calendar_event_updated(): void {
1 efrain 171
 
172
        $this->resetAfterTest();
173
 
174
        // Create a calendar event.
175
        $record = new \stdClass();
176
        $record->courseid = 0;
177
        $time = time();
178
        $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time,
179
                $record); // User event.
180
 
181
        // Catch the events.
182
        $sink = $this->redirectEvents();
183
        $prop = new \stdClass();
184
        $prop->name = 'new event';
185
        $calevent->update($prop); // Update calender event.
186
        // Capture the event.
187
        $events = $sink->get_events();
188
 
189
        // Validate the event.
190
        $event = $events[0];
191
        $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
192
        $this->assertEquals('event', $event->objecttable);
193
        $this->assertEquals(0, $event->courseid);
194
        $this->assertEquals($calevent->context, $event->get_context());
195
        $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'new event');
196
        $this->assertEquals($other, $event->other);
197
        $this->assertEventContextNotUsed($event);
198
 
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_test::create_calendar_event('course', $this->user->id, 'course', 10, time(),
203
                $record);
204
 
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();
212
 
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
    }
221
 
222
    /**
223
     * Tests for calendar_event_updated event.
224
     */
11 efrain 225
    public function test_calendar_event_updated_toggle_visibility(): void {
1 efrain 226
        global $DB;
227
        $siteid = 0;
228
 
229
        $this->resetAfterTest();
230
 
231
        // Create a calendar event.
232
        $time = time();
233
        $calevent = \core_calendar\externallib_test::create_calendar_event('Some wickedly awesome event yo!',
234
            $this->user->id, 'user', 0, $time);
235
 
236
        // Updated the visibility of the calendar event.
237
        $sink = $this->redirectEvents();
238
        $calevent->toggle_visibility();
239
        $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
240
        $events = $sink->get_events();
241
 
242
        // Validate the calendar_event_updated event.
243
        $event = $events[0];
244
        $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
245
        $this->assertEquals('event', $event->objecttable);
246
        $this->assertEquals($siteid, $event->courseid);
247
        $this->assertEquals($calevent->context, $event->get_context());
248
        $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'Some wickedly awesome event yo!');
249
        $this->assertEquals($other, $event->other);
250
        $this->assertEventContextNotUsed($event);
251
        $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
252
 
253
    }
254
 
255
    /**
256
     * Tests for event validations related to calendar_event_created event.
257
     */
11 efrain 258
    public function test_calendar_event_updated_validations(): void {
1 efrain 259
        $this->resetAfterTest();
260
        $context = \context_user::instance($this->user->id);
261
 
262
        // Test not setting other['repeatid'].
263
        try {
264
            \core\event\calendar_event_updated::create(array(
265
                'context'  => $context,
266
                'objectid' => 2,
267
                'other' => array(
268
                    'timestart' => time(),
269
                    'name' => 'event'
270
                )
271
            ));
272
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
273
                    other['repeatid']");
274
        } catch (\coding_exception $e) {
275
            $this->assertStringContainsString('The \'repeatid\' value must be set in other.', $e->getMessage());
276
        }
277
 
278
        // Test not setting other['name'].
279
        try {
280
            \core\event\calendar_event_updated::create(array(
281
                'context'  => $context,
282
                'objectid' => 2,
283
                'other' => array(
284
                    'repeatid' => 0,
285
                    'timestart' => time(),
286
                )
287
            ));
288
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
289
                    other['name']");
290
        } catch (\coding_exception $e) {
291
            $this->assertStringContainsString('The \'name\' value must be set in other.', $e->getMessage());
292
        }
293
 
294
        // Test not setting other['timestart'].
295
        try {
296
            \core\event\calendar_event_updated::create(array(
297
                'context'  => $context,
298
                'objectid' => 2,
299
                'other' => array(
300
                    'name' => 'event',
301
                    'repeatid' => 0,
302
                )
303
            ));
304
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
305
                    other['timestart']");
306
        } catch (\coding_exception $e) {
307
            $this->assertStringContainsString('The \'timestart\' value must be set in other.', $e->getMessage());
308
        }
309
    }
310
 
311
    /**
312
     * Tests for calendar_event_deleted event.
313
     */
11 efrain 314
    public function test_calendar_event_deleted(): void {
1 efrain 315
        global $DB;
316
 
317
        $this->resetAfterTest();
318
 
319
        // Create a calendar event.
320
        $record = new \stdClass();
321
        $record->courseid = 0;
322
        $record->repeatid = 0;
323
        $time = time();
324
        $calevent = \core_calendar\externallib_test::create_calendar_event('event', $this->user->id, 'user', 0, $time,
325
            $record); // User event.
326
        $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
327
 
328
        // Catch the events.
329
        $sink = $this->redirectEvents();
330
        $calevent->delete(false);
331
        $events = $sink->get_events();
332
 
333
        // Validate the event.
334
        $event = $events[0];
335
        $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
336
        $this->assertEquals('event', $event->objecttable);
337
        $this->assertEquals(0, $event->courseid);
338
        $this->assertEquals($calevent->context, $event->get_context());
339
        $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
340
        $this->assertEquals($other, $event->other);
341
        $this->assertEventContextNotUsed($event);
342
        $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
343
 
344
        // Now we create a repeated course event and delete it.
345
        $record = new \stdClass();
346
        $record->courseid = $this->course->id;
347
        $calevent = \core_calendar\externallib_test::create_calendar_event('course', $this->user->id, 'course', 10, time(),
348
            $record);
349
 
350
        $sink->clear();
351
        $prop = new \stdClass();
352
        $prop->name = 'new event';
353
        $prop->repeateditall = true;
354
        $calevent->delete(true);
355
        $events = $sink->get_events();
356
        $sink->close();
357
 
358
        $this->assertEquals(10, count($events));
359
        foreach ($events as $event) {
360
            $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
361
            $this->assertEquals('event', $event->objecttable);
362
            $this->assertEquals($this->course->id, $event->courseid);
363
            $this->assertEquals($calevent->context, $event->get_context());
364
        }
365
    }
366
 
367
    /**
368
     * Tests for event validations related to calendar_event_deleted event.
369
     */
11 efrain 370
    public function test_calendar_event_deleted_validations(): void {
1 efrain 371
        $this->resetAfterTest();
372
        $context = \context_user::instance($this->user->id);
373
 
374
        // Test not setting other['repeatid'].
375
        try {
376
            \core\event\calendar_event_deleted::create(array(
377
                'context'  => $context,
378
                'objectid' => 2,
379
                'other' => array(
380
                    'timestart' => time(),
381
                    'name' => 'event'
382
                )
383
            ));
384
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
385
                    other['repeatid']");
386
        } catch (\coding_exception $e) {
387
            $this->assertStringContainsString('The \'repeatid\' value must be set in other.', $e->getMessage());
388
        }
389
 
390
        // Test not setting other['name'].
391
        try {
392
            \core\event\calendar_event_deleted::create(array(
393
                'context'  => $context,
394
                'objectid' => 2,
395
                'other' => array(
396
                    'repeatid' => 0,
397
                    'timestart' => time(),
398
                )
399
            ));
400
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
401
                    other['name']");
402
        } catch (\coding_exception $e) {
403
            $this->assertStringContainsString('The \'name\' value must be set in other.', $e->getMessage());
404
        }
405
 
406
        // Test not setting other['timestart'].
407
        try {
408
            \core\event\calendar_event_deleted::create(array(
409
                'context'  => $context,
410
                'objectid' => 2,
411
                'other' => array(
412
                    'name' => 'event',
413
                    'repeatid' => 0,
414
                )
415
            ));
416
            $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
417
                    other['timestart']");
418
        } catch (\coding_exception $e) {
419
            $this->assertStringContainsString('The \'timestart\' value must be set in other.', $e->getMessage());
420
        }
421
    }
422
 
423
    /**
424
     * Tests for calendar_subscription_added event for a site subscription.
425
     */
11 efrain 426
    public function test_calendar_subscription_created_site(): void {
1 efrain 427
        global $CFG;
428
        require_once($CFG->dirroot . '/calendar/lib.php');
429
        $this->resetAfterTest(true);
430
 
431
        // Create a mock subscription.
432
        $subscription = new \stdClass();
433
        $subscription->eventtype = 'site';
434
        $subscription->name = 'test';
435
        $subscription->courseid = $this->course->id;
436
 
437
        // Trigger and capture the event.
438
        $sink = $this->redirectEvents();
439
        $id = calendar_add_subscription($subscription);
440
 
441
        $events = $sink->get_events();
442
        $event = reset($events);
443
        // Check that the event data is valid.
444
        $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
445
        $this->assertEquals($id, $event->objectid);
446
        $this->assertEquals($subscription->courseid, $event->other['courseid']);
447
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
448
        $this->assertArrayNotHasKey('categoryid', $event->other);
449
        $this->assertArrayNotHasKey('groupid', $event->other);
450
        $this->assertDebuggingNotCalled();
451
        $sink->close();
452
    }
453
 
454
    /**
455
     * Tests for calendar_subscription_added event for a category subscription.
456
     */
11 efrain 457
    public function test_calendar_subscription_created_category(): void {
1 efrain 458
        global $CFG;
459
        require_once($CFG->dirroot . '/calendar/lib.php');
460
        $this->resetAfterTest(true);
461
 
462
        $categoryid = $this->course->category;
463
 
464
        // Create a mock subscription.
465
        $subscription = new \stdClass();
466
        $subscription->eventtype = 'category';
467
        $subscription->name = 'test';
468
        $subscription->categoryid = $categoryid;
469
 
470
        // Trigger and capture the event.
471
        $sink = $this->redirectEvents();
472
        $id = calendar_add_subscription($subscription);
473
 
474
        $events = $sink->get_events();
475
        $event = reset($events);
476
        // Check that the event data is valid.
477
        $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
478
        $this->assertEquals($id, $event->objectid);
479
        $this->assertEquals($categoryid, $event->other['categoryid']);
480
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
481
        $this->assertArrayNotHasKey('courseid', $event->other);
482
        $this->assertArrayNotHasKey('groupid', $event->other);
483
        $this->assertDebuggingNotCalled();
484
        $sink->close();
485
    }
486
 
487
    /**
488
     * Tests for calendar_subscription_added event for a course subscription.
489
     */
11 efrain 490
    public function test_calendar_subscription_created_course(): void {
1 efrain 491
        global $CFG;
492
        require_once($CFG->dirroot . '/calendar/lib.php');
493
        $this->resetAfterTest(true);
494
 
495
        // Create a mock subscription.
496
        $subscription = new \stdClass();
497
        $subscription->eventtype = 'course';
498
        $subscription->name = 'test';
499
        $subscription->courseid = $this->course->id;
500
 
501
        // Trigger and capture the event.
502
        $sink = $this->redirectEvents();
503
        $id = calendar_add_subscription($subscription);
504
 
505
        $events = $sink->get_events();
506
        $event = reset($events);
507
        // Check that the event data is valid.
508
        $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
509
        $this->assertEquals($id, $event->objectid);
510
        $this->assertEquals($subscription->courseid, $event->other['courseid']);
511
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
512
        $this->assertArrayNotHasKey('categoryid', $event->other);
513
        $this->assertArrayNotHasKey('groupid', $event->other);
514
        $this->assertDebuggingNotCalled();
515
        $sink->close();
516
 
517
    }
518
 
519
    /**
520
     * Tests for calendar_subscription_added event for a group subscription.
521
     */
11 efrain 522
    public function test_calendar_subscription_created_group(): void {
1 efrain 523
        global $CFG;
524
        require_once($CFG->dirroot . '/calendar/lib.php');
525
        $this->resetAfterTest(true);
526
 
527
        $courseid = $this->course->id;
528
        $groupid = 42;
529
 
530
        // Create a mock subscription.
531
        $subscription = new \stdClass();
532
        $subscription->eventtype = 'group';
533
        $subscription->name = 'test';
534
        $subscription->courseid = $courseid;
535
        $subscription->groupid = $groupid;
536
 
537
        // Trigger and capture the event.
538
        $sink = $this->redirectEvents();
539
        $id = calendar_add_subscription($subscription);
540
 
541
        $events = $sink->get_events();
542
        $event = reset($events);
543
        // Check that the event data is valid.
544
        $this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
545
        $this->assertEquals($id, $event->objectid);
546
        $this->assertEquals($courseid, $event->other['courseid']);
547
        $this->assertEquals($groupid, $event->other['groupid']);
548
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
549
        $this->assertArrayNotHasKey('categoryid', $event->other);
550
        $this->assertDebuggingNotCalled();
551
        $sink->close();
552
    }
553
 
554
    /**
555
     * Tests for calendar_subscription_updated event for a site subscription.
556
     */
11 efrain 557
    public function test_calendar_subscription_updated_site(): void {
1 efrain 558
        global $CFG;
559
        require_once($CFG->dirroot . '/calendar/lib.php');
560
        $this->resetAfterTest(true);
561
 
562
        // Create a mock subscription.
563
        $subscription = new \stdClass();
564
        $subscription->eventtype = 'site';
565
        $subscription->name = 'test';
566
        $subscription->courseid = $this->course->id;
567
        $subscription->id = calendar_add_subscription($subscription);
568
        // Now edit it.
569
        $subscription->name = 'awesome';
570
 
571
        // Trigger and capture the event.
572
        $sink = $this->redirectEvents();
573
        calendar_update_subscription($subscription);
574
        $events = $sink->get_events();
575
        $event = reset($events);
576
        // Check that the event data is valid.
577
        $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
578
        $this->assertEquals($subscription->id, $event->objectid);
579
        $this->assertEquals($subscription->courseid, $event->other['courseid']);
580
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
581
        $this->assertArrayNotHasKey('categoryid', $event->other);
582
        $this->assertArrayNotHasKey('groupid', $event->other);
583
        $this->assertDebuggingNotCalled();
584
        $sink->close();
585
    }
586
 
587
    /**
588
     * Tests for calendar_subscription_updated event for a category subscription.
589
     */
11 efrain 590
    public function test_calendar_subscription_updated_category(): void {
1 efrain 591
        global $CFG;
592
        require_once($CFG->dirroot . '/calendar/lib.php');
593
        $this->resetAfterTest(true);
594
 
595
        $categoryid = $this->course->category;
596
 
597
        // Create a mock subscription.
598
        $subscription = new \stdClass();
599
        $subscription->eventtype = 'category';
600
        $subscription->name = 'test';
601
        $subscription->categoryid = $categoryid;
602
        $subscription->id = calendar_add_subscription($subscription);
603
        // Now edit it.
604
        $subscription->name = 'awesome';
605
 
606
        // Trigger and capture the event.
607
        $sink = $this->redirectEvents();
608
        calendar_update_subscription($subscription);
609
        $events = $sink->get_events();
610
        $event = reset($events);
611
        // Check that the event data is valid.
612
        $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
613
        $this->assertEquals($subscription->id, $event->objectid);
614
        $this->assertEquals($categoryid, $event->other['categoryid']);
615
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
616
        $this->assertArrayNotHasKey('courseid', $event->other);
617
        $this->assertArrayNotHasKey('groupid', $event->other);
618
        $this->assertDebuggingNotCalled();
619
        $sink->close();
620
    }
621
 
622
    /**
623
     * Tests for calendar_subscription_updated event for a group subscription.
624
     */
11 efrain 625
    public function test_calendar_subscription_updated_course(): void {
1 efrain 626
        global $CFG;
627
        require_once($CFG->dirroot . '/calendar/lib.php');
628
        $this->resetAfterTest(true);
629
 
630
        // Create a mock subscription.
631
        $subscription = new \stdClass();
632
        $subscription->eventtype = 'course';
633
        $subscription->name = 'test';
634
        $subscription->courseid = $this->course->id;
635
        $subscription->id = calendar_add_subscription($subscription);
636
        // Now edit it.
637
        $subscription->name = 'awesome';
638
 
639
        // Trigger and capture the event.
640
        $sink = $this->redirectEvents();
641
        calendar_update_subscription($subscription);
642
        $events = $sink->get_events();
643
        $event = reset($events);
644
        // Check that the event data is valid.
645
        $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
646
        $this->assertEquals($subscription->id, $event->objectid);
647
        $this->assertEquals($this->course->id, $event->other['courseid']);
648
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
649
        $this->assertArrayNotHasKey('categoryid', $event->other);
650
        $this->assertArrayNotHasKey('groupid', $event->other);
651
        $this->assertDebuggingNotCalled();
652
        $sink->close();
653
    }
654
 
655
    /**
656
     * Tests for calendar_subscription_updated event for a course subscription.
657
     */
11 efrain 658
    public function test_calendar_subscription_updated_group(): void {
1 efrain 659
        global $CFG;
660
        require_once($CFG->dirroot . '/calendar/lib.php');
661
        $this->resetAfterTest(true);
662
 
663
        $courseid = $this->course->id;
664
        $groupid = 42;
665
 
666
        // Create a mock subscription.
667
        $subscription = new \stdClass();
668
        $subscription->eventtype = 'group';
669
        $subscription->name = 'test';
670
        $subscription->courseid = $courseid;
671
        $subscription->groupid = $groupid;
672
 
673
        $subscription->id = calendar_add_subscription($subscription);
674
        // Now edit it.
675
        $subscription->name = 'awesome';
676
 
677
        // Trigger and capture the event.
678
        $sink = $this->redirectEvents();
679
        calendar_update_subscription($subscription);
680
        $events = $sink->get_events();
681
        $event = reset($events);
682
        // Check that the event data is valid.
683
        $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
684
        $this->assertEquals($subscription->id, $event->objectid);
685
        $this->assertEquals($this->course->id, $event->other['courseid']);
686
        $this->assertEquals($groupid, $event->other['groupid']);
687
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
688
        $this->assertArrayNotHasKey('categoryid', $event->other);
689
        $this->assertDebuggingNotCalled();
690
        $sink->close();
691
    }
692
 
693
    /**
694
     * Tests for calendar_subscription_deleted event for a site subscription.
695
     */
11 efrain 696
    public function test_calendar_subscription_deleted_site(): void {
1 efrain 697
        global $CFG;
698
        require_once($CFG->dirroot . '/calendar/lib.php');
699
        $this->resetAfterTest(true);
700
 
701
        // Create a mock subscription.
702
        $subscription = new \stdClass();
703
        $subscription->eventtype = 'site';
704
        $subscription->name = 'test';
705
        $subscription->courseid = $this->course->id;
706
        $subscription->id = calendar_add_subscription($subscription);
707
 
708
        // Trigger and capture the event.
709
        $sink = $this->redirectEvents();
710
        calendar_delete_subscription($subscription);
711
        $events = $sink->get_events();
712
        $event = reset($events);
713
        // Check that the event data is valid.
714
        $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
715
        $this->assertEquals($subscription->id, $event->objectid);
716
        $this->assertEquals($subscription->courseid, $event->other['courseid']);
717
        $this->assertDebuggingNotCalled();
718
        $sink->close();
719
 
720
    }
721
 
722
    /**
723
     * Tests for calendar_subscription_deleted event for a category subscription.
724
     */
11 efrain 725
    public function test_calendar_subscription_deleted_category(): void {
1 efrain 726
        global $CFG;
727
        require_once($CFG->dirroot . '/calendar/lib.php');
728
        $this->resetAfterTest(true);
729
 
730
        $categoryid = $this->course->category;
731
 
732
        // Create a mock subscription.
733
        $subscription = new \stdClass();
734
        $subscription->eventtype = 'category';
735
        $subscription->name = 'test';
736
        $subscription->categoryid = $categoryid;
737
        $subscription->id = calendar_add_subscription($subscription);
738
 
739
        // Trigger and capture the event.
740
        $sink = $this->redirectEvents();
741
        calendar_delete_subscription($subscription);
742
        $events = $sink->get_events();
743
        $event = reset($events);
744
        // Check that the event data is valid.
745
        $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
746
        $this->assertEquals($subscription->id, $event->objectid);
747
        $this->assertEquals($categoryid, $event->other['categoryid']);
748
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
749
        $this->assertArrayNotHasKey('courseid', $event->other);
750
        $this->assertArrayNotHasKey('groupid', $event->other);
751
        $this->assertDebuggingNotCalled();
752
        $sink->close();
753
    }
754
 
755
    /**
756
     * Tests for calendar_subscription_deleted event for a course.
757
     */
11 efrain 758
    public function test_calendar_subscription_deleted_course(): void {
1 efrain 759
        global $CFG;
760
        require_once($CFG->dirroot . '/calendar/lib.php');
761
        $this->resetAfterTest(true);
762
 
763
        // Create a mock subscription.
764
        $subscription = new \stdClass();
765
        $subscription->eventtype = 'course';
766
        $subscription->name = 'test';
767
        $subscription->courseid = $this->course->id;
768
        $subscription->id = calendar_add_subscription($subscription);
769
 
770
        // Trigger and capture the event.
771
        $sink = $this->redirectEvents();
772
        calendar_delete_subscription($subscription);
773
        $events = $sink->get_events();
774
        $event = reset($events);
775
        // Check that the event data is valid.
776
        $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
777
        $this->assertEquals($subscription->id, $event->objectid);
778
        $this->assertEquals($this->course->id, $event->other['courseid']);
779
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
780
        $this->assertArrayNotHasKey('categoryid', $event->other);
781
        $this->assertArrayNotHasKey('groupid', $event->other);
782
        $this->assertDebuggingNotCalled();
783
        $sink->close();
784
    }
785
 
786
    /**
787
     * Tests for calendar_subscription_deleted event for a group.
788
     */
11 efrain 789
    public function test_calendar_subscription_deleted_group(): void {
1 efrain 790
        global $CFG;
791
        require_once($CFG->dirroot . '/calendar/lib.php');
792
        $this->resetAfterTest(true);
793
 
794
        $courseid = $this->course->id;
795
        $groupid = 42;
796
 
797
        // Create a mock subscription.
798
        $subscription = new \stdClass();
799
        $subscription->eventtype = 'group';
800
        $subscription->name = 'test';
801
        $subscription->groupid = $groupid;
802
        $subscription->courseid = $courseid;
803
        $subscription->id = calendar_add_subscription($subscription);
804
 
805
        // Trigger and capture the event.
806
        $sink = $this->redirectEvents();
807
        calendar_delete_subscription($subscription);
808
        $events = $sink->get_events();
809
        $event = reset($events);
810
        // Check that the event data is valid.
811
        $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
812
        $this->assertEquals($subscription->id, $event->objectid);
813
        $this->assertEquals($this->course->id, $event->other['courseid']);
814
        $this->assertEquals($groupid, $event->other['groupid']);
815
        $this->assertEquals($subscription->eventtype, $event->other['eventtype']);
816
        $this->assertArrayNotHasKey('categoryid', $event->other);
817
        $this->assertDebuggingNotCalled();
818
        $sink->close();
819
    }
820
}