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    mod_wiki
  21   * @category   phpunit
  22   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_wiki\event;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  global $CFG;
  30  require_once($CFG->dirroot.'/mod/wiki/locallib.php');
  31  /**
  32   * Events tests class.
  33   *
  34   * @package    mod_wiki
  35   * @category   phpunit
  36   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class events_test extends \advanced_testcase {
  40      private $course;
  41      private $wiki;
  42      private $wikigenerator;
  43      private $student;
  44      private $teacher;
  45  
  46      /**
  47       * Setup test data.
  48       */
  49      public function setUp(): void {
  50          global $DB;
  51  
  52          $this->resetAfterTest();
  53          // Create course and wiki.
  54          $this->course = $this->getDataGenerator()->create_course();
  55          $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
  56          $this->wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
  57  
  58          // Create student and teacher in course.
  59          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  60          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
  61          $this->student = $this->getDataGenerator()->create_user();
  62          $this->teacher = $this->getDataGenerator()->create_user();
  63          $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $studentrole->id);
  64          $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $teacherrole->id);
  65          $this->setAdminUser();
  66      }
  67  
  68      /**
  69       * Test comment_created event.
  70       */
  71      public function test_comment_created() {
  72          $this->setUp();
  73  
  74          $page = $this->wikigenerator->create_first_page($this->wiki);
  75          $context = \context_module::instance($this->wiki->cmid);
  76  
  77          // Triggering and capturing the event.
  78          $sink = $this->redirectEvents();
  79          wiki_add_comment($context, $page->id, 'Test comment', $this->wiki->defaultformat);
  80          $events = $sink->get_events();
  81          $this->assertCount(1, $events);
  82          $event = reset($events);
  83  
  84          // Checking that the event contains the expected values.
  85          $this->assertInstanceOf('\mod_wiki\event\comment_created', $event);
  86          $this->assertEquals($context, $event->get_context());
  87          $this->assertEquals($page->id, $event->other['itemid']);
  88          $this->assertEventContextNotUsed($event);
  89      }
  90  
  91      /**
  92       * Test comment_deleted event.
  93       */
  94      public function test_comment_deleted() {
  95          $this->setUp();
  96  
  97          $page = $this->wikigenerator->create_first_page($this->wiki);
  98          $context = \context_module::instance($this->wiki->cmid);
  99  
 100          // Add comment so we can delete it later.
 101          wiki_add_comment($context, $page->id, 'Test comment', 'html');
 102          $comment = wiki_get_comments($context->id, $page->id);
 103          $this->assertCount(1, $comment);
 104          $comment = array_shift($comment);
 105  
 106          // Triggering and capturing the event.
 107          $sink = $this->redirectEvents();
 108          wiki_delete_comment($comment->id, $context, $page->id);
 109          $events = $sink->get_events();
 110          $this->assertCount(1, $events);
 111          $event = reset($events);
 112  
 113          // Checking that the event contains the expected values.
 114          $this->assertInstanceOf('\mod_wiki\event\comment_deleted', $event);
 115          $this->assertEquals($context, $event->get_context());
 116          $this->assertEquals($page->id, $event->other['itemid']);
 117          $this->assertEventContextNotUsed($event);
 118      }
 119  
 120      /**
 121       * Test comment_viewed event.
 122       */
 123      public function test_comment_viewed() {
 124          // There is no proper API to call or trigger this event, so simulating event
 125          // to check if event returns the right information.
 126  
 127          $this->setUp();
 128          $page = $this->wikigenerator->create_first_page($this->wiki);
 129          $context = \context_module::instance($this->wiki->cmid);
 130  
 131          $params = array(
 132                  'context' => $context,
 133                  'objectid' => $page->id
 134                  );
 135          $event = \mod_wiki\event\comments_viewed::create($params);
 136  
 137          // Triggering and capturing the event.
 138          $sink = $this->redirectEvents();
 139          $event->trigger();
 140          $events = $sink->get_events();
 141          $this->assertCount(1, $events);
 142          $event = reset($events);
 143  
 144          // Checking that the event contains the expected values.
 145          $this->assertInstanceOf('\mod_wiki\event\comments_viewed', $event);
 146          $this->assertEquals($context, $event->get_context());
 147          $expected = array($this->course->id, 'wiki', 'comments', 'comments.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 148          $this->assertEventLegacyLogData($expected, $event);
 149          $this->assertEventContextNotUsed($event);
 150      }
 151  
 152      /**
 153       * Test instances_list_viewed event.
 154       */
 155      public function test_course_module_instance_list_viewed() {
 156          // There is no proper API to call or trigger this event, so simulating event
 157          // to check if event returns the right information.
 158  
 159          $this->setUp();
 160          $context = \context_course::instance($this->course->id);
 161  
 162          $params = array('context' => $context);
 163          $event = \mod_wiki\event\course_module_instance_list_viewed::create($params);
 164  
 165          // Triggering and capturing the event.
 166          $sink = $this->redirectEvents();
 167          $event->trigger();
 168          $events = $sink->get_events();
 169          $this->assertCount(1, $events);
 170          $event = reset($events);
 171  
 172          // Checking that the event contains the expected values.
 173          $this->assertInstanceOf('\mod_wiki\event\course_module_instance_list_viewed', $event);
 174          $this->assertEquals($context, $event->get_context());
 175          $expected = array($this->course->id, 'wiki', 'view all', 'index.php?id=' . $this->course->id, '');
 176          $this->assertEventLegacyLogData($expected, $event);
 177          $this->assertEventContextNotUsed($event);
 178      }
 179  
 180      /**
 181       * Test course_module_viewed event.
 182       */
 183      public function test_course_module_viewed() {
 184          // There is no proper API to call or trigger this event, so simulating event
 185          // to check if event returns the right information.
 186  
 187          $this->setUp();
 188          $context = \context_module::instance($this->wiki->cmid);
 189  
 190          $params = array(
 191                  'context' => $context,
 192                  'objectid' => $this->wiki->id
 193                  );
 194          $event = \mod_wiki\event\course_module_viewed::create($params);
 195  
 196          // Triggering and capturing the event.
 197          $sink = $this->redirectEvents();
 198          $event->trigger();
 199          $events = $sink->get_events();
 200          $this->assertCount(1, $events);
 201          $event = reset($events);
 202  
 203          // Checking that the event contains the expected values.
 204          $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
 205          $this->assertEquals($context, $event->get_context());
 206          $this->assertEquals($this->wiki->id, $event->objectid);
 207          $expected = array($this->course->id, 'wiki', 'view', 'view.php?id=' . $this->wiki->cmid,
 208              $this->wiki->id, $this->wiki->cmid);
 209          $this->assertEventLegacyLogData($expected, $event);
 210          $this->assertEventContextNotUsed($event);
 211      }
 212  
 213      /**
 214       * Test page_viewed event.
 215       */
 216      public function test_page_viewed() {
 217          // There is no proper API to call or trigger this event, so simulating event
 218          // to check if event returns the right information.
 219  
 220          $this->setUp();
 221  
 222          $page = $this->wikigenerator->create_first_page($this->wiki);
 223          $context = \context_module::instance($this->wiki->cmid);
 224  
 225          $params = array(
 226                  'context' => $context,
 227                  'objectid' => $page->id
 228                  );
 229          $event = \mod_wiki\event\page_viewed::create($params);
 230  
 231          // Triggering and capturing the event.
 232          $sink = $this->redirectEvents();
 233          $event->trigger();
 234          $events = $sink->get_events();
 235          $this->assertCount(1, $events);
 236          $event = reset($events);
 237  
 238          // Checking that the event contains the expected values.
 239          $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
 240          $this->assertEquals($context, $event->get_context());
 241          $this->assertEquals($page->id, $event->objectid);
 242          $expected = array($this->course->id, 'wiki', 'view', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 243          $this->assertEventLegacyLogData($expected, $event);
 244          $this->assertEventContextNotUsed($event);
 245      }
 246  
 247      /**
 248       * Test page_viewed event for prettypage view.
 249       */
 250      public function test_pretty_page_viewed() {
 251          // There is no proper API to call or trigger this event, so simulating event
 252          // to check if event returns the right information.
 253  
 254          $this->setUp();
 255  
 256          $page = $this->wikigenerator->create_first_page($this->wiki);
 257          $context = \context_module::instance($this->wiki->cmid);
 258  
 259          $params = array(
 260                  'context' => $context,
 261                  'objectid' => $page->id,
 262                  'other' => array('prettyview' => true)
 263                  );
 264          $event = \mod_wiki\event\page_viewed::create($params);
 265  
 266          // Triggering and capturing the event.
 267          $sink = $this->redirectEvents();
 268          $event->trigger();
 269          $events = $sink->get_events();
 270          $this->assertCount(1, $events);
 271          $event = reset($events);
 272  
 273          // Checking that the event contains the expected values.
 274          $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
 275          $this->assertEquals($context, $event->get_context());
 276          $this->assertEquals($page->id, $event->objectid);
 277          $expected = array($this->course->id, 'wiki', 'view', 'prettyview.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 278          $this->assertEventLegacyLogData($expected, $event);
 279          $this->assertEventContextNotUsed($event);
 280      }
 281  
 282      /**
 283       * Test page_created event.
 284       */
 285      public function test_page_created() {
 286          global $USER;
 287  
 288          $this->setUp();
 289  
 290          $context = \context_module::instance($this->wiki->cmid);
 291  
 292          // Triggering and capturing the event.
 293          $sink = $this->redirectEvents();
 294          $page = $this->wikigenerator->create_first_page($this->wiki);
 295          $events = $sink->get_events();
 296          $this->assertCount(2, $events);
 297          $event = reset($events);
 298  
 299          // Checking that the event contains the expected values.
 300          $this->assertInstanceOf('\mod_wiki\event\page_created', $event);
 301          $this->assertEquals($context, $event->get_context());
 302          $this->assertEquals($page->id, $event->objectid);
 303          $expected = array($this->course->id, 'wiki', 'add page',
 304              'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 305          $this->assertEventLegacyLogData($expected, $event);
 306          $this->assertEventContextNotUsed($event);
 307      }
 308  
 309      /**
 310       * Test page_deleted and page_version_deleted and page_locks_deleted event.
 311       */
 312      public function test_page_deleted() {
 313          global $DB;
 314  
 315          $this->setUp();
 316  
 317          $page = $this->wikigenerator->create_first_page($this->wiki);
 318          $context = \context_module::instance($this->wiki->cmid);
 319          $oldversions = $DB->get_records('wiki_versions', array('pageid' => $page->id));
 320          $oldversion = array_shift($oldversions);
 321  
 322          // Triggering and capturing the event.
 323          $sink = $this->redirectEvents();
 324          wiki_delete_pages($context, array($page->id));
 325          $events = $sink->get_events();
 326          $this->assertCount(4, $events);
 327          $event = array_shift($events);
 328  
 329          // Checking that the event contains the page_version_deleted event.
 330          $this->assertInstanceOf('\mod_wiki\event\page_version_deleted', $event);
 331          $this->assertEquals($context, $event->get_context());
 332          $this->assertEquals($page->id, $event->other['pageid']);
 333          $this->assertEquals($oldversion->id, $event->objectid);
 334          $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' .  $page->id,  $page->id, $this->wiki->cmid);
 335          $this->assertEventLegacyLogData($expected, $event);
 336  
 337          // Checking that the event contains the page_deleted event.
 338          $event = array_pop($events);
 339          $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
 340          $this->assertEquals($context, $event->get_context());
 341          $this->assertEquals($page->id, $event->objectid);
 342          $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 343          $this->assertEventLegacyLogData($expected, $event);
 344  
 345          // Checking that the event contains the expected values.
 346          $event = array_pop($events);
 347          $this->assertInstanceOf('\mod_wiki\event\page_locks_deleted', $event);
 348          $this->assertEquals($context, $event->get_context());
 349          $this->assertEquals($page->id, $event->objectid);
 350          $expected = array($this->course->id, 'wiki', 'overridelocks', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 351          $this->assertEventLegacyLogData($expected, $event);
 352  
 353          // Delete all pages.
 354          $page1 = $this->wikigenerator->create_first_page($this->wiki);
 355          $page2 = $this->wikigenerator->create_content($this->wiki);
 356          $page3 = $this->wikigenerator->create_content($this->wiki, array('title' => 'Custom title'));
 357  
 358          // Triggering and capturing the event.
 359          $sink = $this->redirectEvents();
 360          wiki_delete_pages($context, array($page1->id, $page2->id));
 361          $events = $sink->get_events();
 362          $this->assertCount(8, $events);
 363          $event = array_pop($events);
 364  
 365          // Checking that the event contains the expected values.
 366          $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
 367          $this->assertEquals($context, $event->get_context());
 368          $this->assertEquals($page2->id, $event->objectid);
 369          $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' . $page2->id, $page2->id, $this->wiki->cmid);
 370          $this->assertEventLegacyLogData($expected, $event);
 371          $this->assertEventContextNotUsed($event);
 372      }
 373  
 374      /**
 375       * Test page_updated event.
 376       */
 377      public function test_page_updated() {
 378          global $USER;
 379  
 380          $this->setUp();
 381  
 382          $page = $this->wikigenerator->create_first_page($this->wiki);
 383          $context = \context_module::instance($this->wiki->cmid);
 384  
 385          // Triggering and capturing the event.
 386          $sink = $this->redirectEvents();
 387          wiki_save_page($page, 'New content', $USER->id);
 388          $events = $sink->get_events();
 389          $this->assertCount(1, $events);
 390          $event = reset($events);
 391  
 392          // Checking that the event contains the expected values.
 393          $this->assertInstanceOf('\mod_wiki\event\page_updated', $event);
 394          $this->assertEquals($context, $event->get_context());
 395          $this->assertEquals($page->id, $event->objectid);
 396          $expected = array($this->course->id, 'wiki', 'edit',
 397              'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 398          $this->assertEventLegacyLogData($expected, $event);
 399          $this->assertEventContextNotUsed($event);
 400      }
 401  
 402      /**
 403       * Test page_diff_viewed event.
 404       */
 405      public function test_page_diff_viewed() {
 406          // There is no proper API to call or trigger this event, so simulating event
 407          // to check if event returns the right information.
 408  
 409          $this->setUp();
 410  
 411          $page = $this->wikigenerator->create_first_page($this->wiki);
 412          $context = \context_module::instance($this->wiki->cmid);
 413  
 414          $params = array(
 415                  'context' => $context,
 416                  'objectid' => $page->id,
 417                  'other' => array(
 418                      'comparewith' => 1,
 419                      'compare' => 2
 420                      )
 421                  );
 422          $event = \mod_wiki\event\page_diff_viewed::create($params);
 423  
 424          // Triggering and capturing the event.
 425          $sink = $this->redirectEvents();
 426          $event->trigger();
 427          $events = $sink->get_events();
 428          $this->assertCount(1, $events);
 429          $event = reset($events);
 430  
 431          // Checking that the event contains the expected values.
 432          $this->assertInstanceOf('\mod_wiki\event\page_diff_viewed', $event);
 433          $this->assertEquals($context, $event->get_context());
 434          $this->assertEquals($page->id, $event->objectid);
 435          $expected = array($this->course->id, 'wiki', 'diff', 'diff.php?pageid=' . $page->id . '&comparewith=' .
 436              1 . '&compare=' .  2, $page->id, $this->wiki->cmid);
 437          $this->assertEventLegacyLogData($expected, $event);
 438          $this->assertEventContextNotUsed($event);
 439      }
 440  
 441      /**
 442       * Test page_history_viewed event.
 443       */
 444      public function test_page_history_viewed() {
 445          // There is no proper API to call or trigger this event, so simulating event
 446          // to check if event returns the right information.
 447  
 448          $this->setUp();
 449  
 450          $page = $this->wikigenerator->create_first_page($this->wiki);
 451          $context = \context_module::instance($this->wiki->cmid);
 452  
 453          $params = array(
 454                  'context' => $context,
 455                  'objectid' => $page->id
 456                  );
 457          $event = \mod_wiki\event\page_history_viewed::create($params);
 458  
 459          // Triggering and capturing the event.
 460          $sink = $this->redirectEvents();
 461          $event->trigger();
 462          $events = $sink->get_events();
 463          $this->assertCount(1, $events);
 464          $event = reset($events);
 465  
 466          // Checking that the event contains the expected values.
 467          $this->assertInstanceOf('\mod_wiki\event\page_history_viewed', $event);
 468          $this->assertEquals($context, $event->get_context());
 469          $this->assertEquals($page->id, $event->objectid);
 470          $expected = array($this->course->id, 'wiki', 'history', 'history.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 471          $this->assertEventLegacyLogData($expected, $event);
 472          $this->assertEventContextNotUsed($event);
 473      }
 474  
 475      /**
 476       * Test page_map_viewed event.
 477       */
 478      public function test_page_map_viewed() {
 479          // There is no proper API to call or trigger this event, so simulating event
 480          // to check if event returns the right information.
 481  
 482          $this->setUp();
 483  
 484          $page = $this->wikigenerator->create_first_page($this->wiki);
 485          $context = \context_module::instance($this->wiki->cmid);
 486  
 487          $params = array(
 488                  'context' => $context,
 489                  'objectid' => $page->id,
 490                  'other' => array(
 491                      'option' => 0
 492                      )
 493                  );
 494          $event = \mod_wiki\event\page_map_viewed::create($params);
 495  
 496          // Triggering and capturing the event.
 497          $sink = $this->redirectEvents();
 498          $event->trigger();
 499          $events = $sink->get_events();
 500          $this->assertCount(1, $events);
 501          $event = reset($events);
 502  
 503          // Checking that the event contains the expected values.
 504          $this->assertInstanceOf('\mod_wiki\event\page_map_viewed', $event);
 505          $this->assertEquals($context, $event->get_context());
 506          $this->assertEquals($page->id, $event->objectid);
 507          $this->assertEquals(0, $event->other['option']);
 508          $expected = array($this->course->id, 'wiki', 'map', 'map.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 509          $this->assertEventLegacyLogData($expected, $event);
 510          $this->assertEventContextNotUsed($event);
 511      }
 512  
 513      /**
 514       * Test page_version_viewed event.
 515       */
 516      public function test_page_version_viewed() {
 517          // There is no proper API to call or trigger this event, so simulating event
 518          // to check if event returns the right information.
 519  
 520          $this->setUp();
 521  
 522          $page = $this->wikigenerator->create_first_page($this->wiki);
 523          $context = \context_module::instance($this->wiki->cmid);
 524  
 525          $params = array(
 526                  'context' => $context,
 527                  'objectid' => $page->id,
 528                  'other' => array(
 529                      'versionid' => 1
 530                      )
 531                  );
 532          $event = \mod_wiki\event\page_version_viewed::create($params);
 533  
 534          // Triggering and capturing the event.
 535          $sink = $this->redirectEvents();
 536          $event->trigger();
 537          $events = $sink->get_events();
 538          $this->assertCount(1, $events);
 539          $event = reset($events);
 540  
 541          // Checking that the event contains the expected values.
 542          $this->assertInstanceOf('\mod_wiki\event\page_version_viewed', $event);
 543          $this->assertEquals($context, $event->get_context());
 544          $this->assertEquals($page->id, $event->objectid);
 545          $this->assertEquals(1, $event->other['versionid']);
 546          $expected = array($this->course->id, 'wiki', 'history', 'viewversion.php?pageid=' . $page->id . '&versionid=1',
 547              $page->id, $this->wiki->cmid);
 548          $this->assertEventLegacyLogData($expected, $event);
 549          $this->assertEventContextNotUsed($event);
 550      }
 551  
 552      /**
 553       * Test page_version_restored event.
 554       */
 555      public function test_page_version_restored() {
 556          $this->setUp();
 557  
 558          $page = $this->wikigenerator->create_first_page($this->wiki);
 559          $context = \context_module::instance($this->wiki->cmid);
 560          $version = wiki_get_current_version($page->id);
 561  
 562          // Triggering and capturing the event.
 563          $sink = $this->redirectEvents();
 564          wiki_restore_page($page, $version, $context);
 565          $events = $sink->get_events();
 566          $this->assertCount(2, $events);
 567          $event = array_pop($events);
 568  
 569          // Checking that the event contains the expected values.
 570          $this->assertInstanceOf('\mod_wiki\event\page_version_restored', $event);
 571          $this->assertEquals($context, $event->get_context());
 572          $this->assertEquals($version->id, $event->objectid);
 573          $this->assertEquals($page->id, $event->other['pageid']);
 574          $expected = array($this->course->id, 'wiki', 'restore', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
 575          $this->assertEventLegacyLogData($expected, $event);
 576          $this->assertEventContextNotUsed($event);
 577      }
 578  }