Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

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