Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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   * Events tests.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2014 Mark Nelson <markn@moodle.com>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\event;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once (__DIR__.'/../fixtures/event_fixtures.php');
  31  
  32  class events_test extends \advanced_testcase {
  33  
  34      /**
  35       * Test set up.
  36       *
  37       * This is executed before running any test in this file.
  38       */
  39      public function setUp(): void {
  40          $this->resetAfterTest();
  41      }
  42  
  43      /**
  44       * Test the course category created event.
  45       */
  46      public function test_course_category_created() {
  47          // Trigger and capture the event.
  48          $sink = $this->redirectEvents();
  49          $category = $this->getDataGenerator()->create_category();
  50          $events = $sink->get_events();
  51          $event = reset($events);
  52  
  53          // Check that the event data is valid.
  54          $this->assertInstanceOf('\core\event\course_category_created', $event);
  55          $this->assertEquals(\context_coursecat::instance($category->id), $event->get_context());
  56          $url = new \moodle_url('/course/management.php', array('categoryid' => $event->objectid));
  57          $this->assertEquals($url, $event->get_url());
  58          $expected = array(SITEID, 'category', 'add', 'editcategory.php?id=' . $category->id, $category->id);
  59          $this->assertEventLegacyLogData($expected, $event);
  60          $this->assertEventContextNotUsed($event);
  61      }
  62  
  63      /**
  64       * Test the course category updated event.
  65       */
  66      public function test_course_category_updated() {
  67          // Create a category.
  68          $category = $this->getDataGenerator()->create_category();
  69  
  70          // Create some data we are going to use to update this category.
  71          $data = new \stdClass();
  72          $data->name = 'Category name change';
  73  
  74          // Trigger and capture the event for updating a category.
  75          $sink = $this->redirectEvents();
  76          $category->update($data);
  77          $events = $sink->get_events();
  78          $event = reset($events);
  79  
  80          // Check that the event data is valid.
  81          $this->assertInstanceOf('\core\event\course_category_updated', $event);
  82          $this->assertEquals(\context_coursecat::instance($category->id), $event->get_context());
  83          $url = new \moodle_url('/course/editcategory.php', array('id' => $event->objectid));
  84          $this->assertEquals($url, $event->get_url());
  85          $expected = array(SITEID, 'category', 'update', 'editcategory.php?id=' . $category->id, $category->id);
  86          $this->assertEventLegacyLogData($expected, $event);
  87  
  88          // Create another category and a child category.
  89          $category2 = $this->getDataGenerator()->create_category();
  90          $childcat = $this->getDataGenerator()->create_category(array('parent' => $category2->id));
  91  
  92          // Trigger and capture the event for changing the parent of a category.
  93          $sink = $this->redirectEvents();
  94          $childcat->change_parent($category);
  95          $events = $sink->get_events();
  96          $event = reset($events);
  97  
  98          // Check that the event data is valid.
  99          $this->assertInstanceOf('\core\event\course_category_updated', $event);
 100          $this->assertEquals(\context_coursecat::instance($childcat->id), $event->get_context());
 101          $expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
 102          $this->assertEventLegacyLogData($expected, $event);
 103  
 104          // Trigger and capture the event for changing the sortorder of a category.
 105          $sink = $this->redirectEvents();
 106          $category2->change_sortorder_by_one(true);
 107          $events = $sink->get_events();
 108          $event = reset($events);
 109  
 110          // Check that the event data is valid.
 111          $this->assertInstanceOf('\core\event\course_category_updated', $event);
 112          $this->assertEquals(\context_coursecat::instance($category2->id), $event->get_context());
 113          $expected = array(SITEID, 'category', 'move', 'management.php?categoryid=' . $category2->id, $category2->id);
 114          $this->assertEventLegacyLogData($expected, $event);
 115  
 116          // Trigger and capture the event for deleting a category and moving it's children to another.
 117          $sink = $this->redirectEvents();
 118          $category->delete_move($category->id);
 119          $events = $sink->get_events();
 120          $event = reset($events);
 121  
 122          // Check that the event data is valid.
 123          $this->assertInstanceOf('\core\event\course_category_updated', $event);
 124          $this->assertEquals(\context_coursecat::instance($childcat->id), $event->get_context());
 125          $expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
 126          $this->assertEventLegacyLogData($expected, $event);
 127  
 128          // Trigger and capture the event for hiding a category.
 129          $sink = $this->redirectEvents();
 130          $category2->hide();
 131          $events = $sink->get_events();
 132          $event = reset($events);
 133  
 134          // Check that the event data is valid.
 135          $this->assertInstanceOf('\core\event\course_category_updated', $event);
 136          $this->assertEquals(\context_coursecat::instance($category2->id), $event->get_context());
 137          $expected = array(SITEID, 'category', 'hide', 'editcategory.php?id=' . $category2->id, $category2->id);
 138          $this->assertEventLegacyLogData($expected, $event);
 139  
 140          // Trigger and capture the event for unhiding a category.
 141          $sink = $this->redirectEvents();
 142          $category2->show();
 143          $events = $sink->get_events();
 144          $event = reset($events);
 145  
 146          // Check that the event data is valid.
 147          $this->assertInstanceOf('\core\event\course_category_updated', $event);
 148          $this->assertEquals(\context_coursecat::instance($category2->id), $event->get_context());
 149          $expected = array(SITEID, 'category', 'show', 'editcategory.php?id=' . $category2->id, $category2->id);
 150          $this->assertEventLegacyLogData($expected, $event);
 151          $this->assertEventContextNotUsed($event);
 152      }
 153  
 154      /**
 155       * Test the email failed event.
 156       *
 157       * It's not possible to use the moodle API to simulate the failure of sending
 158       * an email, so here we simply create the event and trigger it.
 159       */
 160      public function test_email_failed() {
 161          // Trigger event for failing to send email.
 162          $event = \core\event\email_failed::create(array(
 163              'context' => \context_system::instance(),
 164              'userid' => 1,
 165              'relateduserid' => 2,
 166              'other' => array(
 167                  'subject' => 'This is a subject',
 168                  'message' => 'This is a message',
 169                  'errorinfo' => 'The email failed to send!'
 170              )
 171          ));
 172  
 173          // Trigger and capture the event.
 174          $sink = $this->redirectEvents();
 175          $event->trigger();
 176          $events = $sink->get_events();
 177          $event = reset($events);
 178  
 179          $this->assertInstanceOf('\core\event\email_failed', $event);
 180          $this->assertEquals(\context_system::instance(), $event->get_context());
 181          $expected = array(SITEID, 'library', 'mailer', qualified_me(), 'ERROR: The email failed to send!');
 182          $this->assertEventLegacyLogData($expected, $event);
 183          $this->assertEventContextNotUsed($event);
 184      }
 185  
 186      /**
 187       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 188       */
 189      public function test_course_user_report_viewed() {
 190  
 191          $user = $this->getDataGenerator()->create_user();
 192          $course = $this->getDataGenerator()->create_course();
 193          $context = \context_course::instance($course->id);
 194  
 195          $eventparams = array();
 196          $eventparams['context'] = $context;
 197          $eventparams['relateduserid'] = $user->id;
 198          $eventparams['other'] = array();
 199          $eventparams['other']['mode'] = 'grade';
 200          $event = \core\event\course_user_report_viewed::create($eventparams);
 201  
 202          // Trigger and capture the event.
 203          $sink = $this->redirectEvents();
 204          $event->trigger();
 205          $events = $sink->get_events();
 206          $event = reset($events);
 207  
 208          $this->assertInstanceOf('\core\event\course_user_report_viewed', $event);
 209          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 210          $expected = array($course->id, 'course', 'user report', 'user.php?id=' . $course->id . '&amp;user='
 211                  . $user->id . '&amp;mode=grade', $user->id);
 212          $this->assertEventLegacyLogData($expected, $event);
 213          $this->assertEventContextNotUsed($event);
 214      }
 215  
 216      /**
 217       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 218       */
 219      public function test_course_viewed() {
 220  
 221          $user = $this->getDataGenerator()->create_user();
 222          $course = $this->getDataGenerator()->create_course();
 223          $context = \context_course::instance($course->id);
 224  
 225          // First try with no optional parameters.
 226          $eventparams = array();
 227          $eventparams['context'] = $context;
 228          $event = \core\event\course_viewed::create($eventparams);
 229  
 230          // Trigger and capture the event.
 231          $sink = $this->redirectEvents();
 232          $event->trigger();
 233          $events = $sink->get_events();
 234          $event = reset($events);
 235  
 236          $this->assertInstanceOf('\core\event\course_viewed', $event);
 237          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 238          $expected = array($course->id, 'course', 'view', 'view.php?id=' . $course->id, $course->id);
 239          $this->assertEventLegacyLogData($expected, $event);
 240          $this->assertEventContextNotUsed($event);
 241  
 242          // Now try with optional parameters.
 243          $sectionnumber = 7;
 244          $eventparams = array();
 245          $eventparams['context'] = $context;
 246          $eventparams['other'] = array('coursesectionnumber' => $sectionnumber);
 247          $event = \core\event\course_viewed::create($eventparams);
 248  
 249          // Trigger and capture the event.
 250          $sink = $this->redirectEvents();
 251          $event->trigger();
 252          $loggeddata = $event->get_data();
 253          $events = $sink->get_events();
 254          $event = reset($events);
 255  
 256  
 257          $this->assertInstanceOf('\core\event\course_viewed', $event);
 258          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 259          $expected = array($course->id, 'course', 'view section', 'view.php?id=' . $course->id . '&amp;section='
 260                  . $sectionnumber, $sectionnumber);
 261          $this->assertEventLegacyLogData($expected, $event);
 262          $this->assertEventContextNotUsed($event);
 263  
 264          delete_course($course->id, false);
 265          $restored = \core\event\base::restore($loggeddata, array('origin' => 'web', 'ip' => '127.0.0.1'));
 266          $this->assertInstanceOf('\core\event\course_viewed', $restored);
 267          $this->assertNull($restored->get_url());
 268      }
 269  
 270      public function test_recent_capability_viewed() {
 271          $this->resetAfterTest();
 272  
 273          $this->setAdminUser();
 274          $course = $this->getDataGenerator()->create_course();
 275          $context = \context_course::instance($course->id);
 276  
 277          $event = \core\event\recent_activity_viewed::create(array('context' => $context));
 278  
 279          // Trigger and capture the event.
 280          $sink = $this->redirectEvents();
 281          $event->trigger();
 282          $events = $sink->get_events();
 283          $event = reset($events);
 284  
 285          $this->assertInstanceOf('\core\event\recent_activity_viewed', $event);
 286          $this->assertEquals($context, $event->get_context());
 287          $expected = array($course->id, "course", "recent", "recent.php?id=$course->id", $course->id);
 288          $this->assertEventLegacyLogData($expected, $event);
 289          $this->assertEventContextNotUsed($event);
 290          $url = new \moodle_url('/course/recent.php', array('id' => $course->id));
 291          $this->assertEquals($url, $event->get_url());
 292          $event->get_name();
 293      }
 294  
 295      public function test_user_profile_viewed() {
 296          $this->resetAfterTest();
 297          $this->setAdminUser();
 298  
 299          $user = $this->getDataGenerator()->create_user();
 300          $course = $this->getDataGenerator()->create_course();
 301          $coursecontext = \context_course::instance($course->id);
 302  
 303          // User profile viewed in course context.
 304          $eventparams = array(
 305              'objectid' => $user->id,
 306              'relateduserid' => $user->id,
 307              'courseid' => $course->id,
 308              'context' => $coursecontext,
 309              'other' => array(
 310                  'courseid' => $course->id,
 311                  'courseshortname' => $course->shortname,
 312                  'coursefullname' => $course->fullname
 313              )
 314          );
 315          $event = \core\event\user_profile_viewed::create($eventparams);
 316  
 317          // Trigger and capture the event.
 318          $sink = $this->redirectEvents();
 319          $event->trigger();
 320          $events = $sink->get_events();
 321          $event = reset($events);
 322  
 323          $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
 324          $log = array($course->id, 'user', 'view', 'view.php?id=' . $user->id . '&course=' . $course->id, $user->id);
 325          $this->assertEventLegacyLogData($log, $event);
 326          $this->assertEventContextNotUsed($event);
 327  
 328          // User profile viewed in user context.
 329          $usercontext = \context_user::instance($user->id);
 330          $eventparams['context'] = $usercontext;
 331          unset($eventparams['courseid'], $eventparams['other']);
 332          $event = \core\event\user_profile_viewed::create($eventparams);
 333  
 334          // Trigger and capture the event.
 335          $sink = $this->redirectEvents();
 336          $event->trigger();
 337          $events = $sink->get_events();
 338          $event = reset($events);
 339  
 340          $this->assertInstanceOf('\core\event\user_profile_viewed', $event);
 341          $expected = null;
 342          $this->assertEventLegacyLogData($expected, $event);
 343          $this->assertEventContextNotUsed($event);
 344      }
 345  
 346      /**
 347       * There is no API associated with this event, so we will just test standard features.
 348       */
 349      public function test_grade_viewed() {
 350          $this->resetAfterTest();
 351          $this->setAdminUser();
 352  
 353          $user = $this->getDataGenerator()->create_user();
 354          $course = $this->getDataGenerator()->create_course();
 355          $coursecontext = \context_course::instance($course->id);
 356  
 357          $event = \core_tests\event\grade_report_viewed::create(
 358              array(
 359                  'context' => $coursecontext,
 360                  'courseid' => $course->id,
 361                  'userid' => $user->id,
 362              )
 363          );
 364  
 365          // Trigger and capture the event.
 366          $sink = $this->redirectEvents();
 367          $event->trigger();
 368          $events = $sink->get_events();
 369          $event = reset($events);
 370  
 371          $this->assertInstanceOf('\core\event\grade_report_viewed', $event);
 372          $this->assertEquals($event->courseid, $course->id);
 373          $this->assertEquals($event->userid, $user->id);
 374          $this->assertEventContextNotUsed($event);
 375      }
 376  
 377      /**
 378       * Test the database text field content replaced event.
 379       */
 380      public function test_database_text_field_content_replaced() {
 381          global $CFG;
 382  
 383          require_once($CFG->dirroot . '/lib/adminlib.php');
 384  
 385          // Trigger and capture the event for finding and replacing strings in the database.
 386          $sink = $this->redirectEvents();
 387          ob_start();
 388          db_replace('searchstring', 'replacestring');
 389          ob_end_clean();
 390          $events = $sink->get_events();
 391          $event = reset($events);
 392  
 393          // Check that the event data is valid.
 394          $this->assertInstanceOf('\core\event\database_text_field_content_replaced', $event);
 395          $this->assertEquals(\context_system::instance(), $event->get_context());
 396          $this->assertEquals('searchstring', $event->other['search']);
 397          $this->assertEquals('replacestring', $event->other['replace']);
 398      }
 399  }