Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.7.x will end 11 May 2020 (12 months).
  • Bug fixes for security issues in 3.7.x will end 9 November 2020 (18 months) - Support has ended.
  • minimum PHP 7.1.0 Note: minimum PHP version has increased since Moodle 3.6. PHP 7.2.x and 7.3.x are supported too. PHP 7.x could have some engine limitations.
  • Differences Between: [Versions 37 and 310] [Versions 37 and 311]

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