Proyectos de Subversion Moodle

Rev

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