Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

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