Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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      }
 148  
 149      /**
 150       * Test instances_list_viewed event.
 151       */
 152      public function test_course_module_instance_list_viewed() {
 153          // There is no proper API to call or trigger this event, so simulating event
 154          // to check if event returns the right information.
 155  
 156          $this->setUp();
 157          $context = \context_course::instance($this->course->id);
 158  
 159          $params = array('context' => $context);
 160          $event = \mod_wiki\event\course_module_instance_list_viewed::create($params);
 161  
 162          // Triggering and capturing the event.
 163          $sink = $this->redirectEvents();
 164          $event->trigger();
 165          $events = $sink->get_events();
 166          $this->assertCount(1, $events);
 167          $event = reset($events);
 168  
 169          // Checking that the event contains the expected values.
 170          $this->assertInstanceOf('\mod_wiki\event\course_module_instance_list_viewed', $event);
 171          $this->assertEquals($context, $event->get_context());
 172      }
 173  
 174      /**
 175       * Test course_module_viewed event.
 176       */
 177      public function test_course_module_viewed() {
 178          // There is no proper API to call or trigger this event, so simulating event
 179          // to check if event returns the right information.
 180  
 181          $this->setUp();
 182          $context = \context_module::instance($this->wiki->cmid);
 183  
 184          $params = array(
 185                  'context' => $context,
 186                  'objectid' => $this->wiki->id
 187                  );
 188          $event = \mod_wiki\event\course_module_viewed::create($params);
 189  
 190          // Triggering and capturing the event.
 191          $sink = $this->redirectEvents();
 192          $event->trigger();
 193          $events = $sink->get_events();
 194          $this->assertCount(1, $events);
 195          $event = reset($events);
 196  
 197          // Checking that the event contains the expected values.
 198          $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
 199          $this->assertEquals($context, $event->get_context());
 200          $this->assertEquals($this->wiki->id, $event->objectid);
 201      }
 202  
 203      /**
 204       * Test page_viewed event.
 205       */
 206      public function test_page_viewed() {
 207          // There is no proper API to call or trigger this event, so simulating event
 208          // to check if event returns the right information.
 209  
 210          $this->setUp();
 211  
 212          $page = $this->wikigenerator->create_first_page($this->wiki);
 213          $context = \context_module::instance($this->wiki->cmid);
 214  
 215          $params = array(
 216                  'context' => $context,
 217                  'objectid' => $page->id
 218                  );
 219          $event = \mod_wiki\event\page_viewed::create($params);
 220  
 221          // Triggering and capturing the event.
 222          $sink = $this->redirectEvents();
 223          $event->trigger();
 224          $events = $sink->get_events();
 225          $this->assertCount(1, $events);
 226          $event = reset($events);
 227  
 228          // Checking that the event contains the expected values.
 229          $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
 230          $this->assertEquals($context, $event->get_context());
 231          $this->assertEquals($page->id, $event->objectid);
 232      }
 233  
 234      /**
 235       * Test page_viewed event for prettypage view.
 236       */
 237      public function test_pretty_page_viewed() {
 238          // There is no proper API to call or trigger this event, so simulating event
 239          // to check if event returns the right information.
 240  
 241          $this->setUp();
 242  
 243          $page = $this->wikigenerator->create_first_page($this->wiki);
 244          $context = \context_module::instance($this->wiki->cmid);
 245  
 246          $params = array(
 247                  'context' => $context,
 248                  'objectid' => $page->id,
 249                  'other' => array('prettyview' => true)
 250                  );
 251          $event = \mod_wiki\event\page_viewed::create($params);
 252  
 253          // Triggering and capturing the event.
 254          $sink = $this->redirectEvents();
 255          $event->trigger();
 256          $events = $sink->get_events();
 257          $this->assertCount(1, $events);
 258          $event = reset($events);
 259  
 260          // Checking that the event contains the expected values.
 261          $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
 262          $this->assertEquals($context, $event->get_context());
 263          $this->assertEquals($page->id, $event->objectid);
 264      }
 265  
 266      /**
 267       * Test page_created event.
 268       */
 269      public function test_page_created() {
 270          global $USER;
 271  
 272          $this->setUp();
 273  
 274          $context = \context_module::instance($this->wiki->cmid);
 275  
 276          // Triggering and capturing the event.
 277          $sink = $this->redirectEvents();
 278          $page = $this->wikigenerator->create_first_page($this->wiki);
 279          $events = $sink->get_events();
 280          $this->assertCount(2, $events);
 281          $event = reset($events);
 282  
 283          // Checking that the event contains the expected values.
 284          $this->assertInstanceOf('\mod_wiki\event\page_created', $event);
 285          $this->assertEquals($context, $event->get_context());
 286          $this->assertEquals($page->id, $event->objectid);
 287      }
 288  
 289      /**
 290       * Test page_deleted and page_version_deleted and page_locks_deleted event.
 291       */
 292      public function test_page_deleted() {
 293          global $DB;
 294  
 295          $this->setUp();
 296  
 297          $page = $this->wikigenerator->create_first_page($this->wiki);
 298          $context = \context_module::instance($this->wiki->cmid);
 299          $oldversions = $DB->get_records('wiki_versions', array('pageid' => $page->id));
 300          $oldversion = array_shift($oldversions);
 301  
 302          // Triggering and capturing the event.
 303          $sink = $this->redirectEvents();
 304          wiki_delete_pages($context, array($page->id));
 305          $events = $sink->get_events();
 306          $this->assertCount(4, $events);
 307          $event = array_shift($events);
 308  
 309          // Checking that the event contains the page_version_deleted event.
 310          $this->assertInstanceOf('\mod_wiki\event\page_version_deleted', $event);
 311          $this->assertEquals($context, $event->get_context());
 312          $this->assertEquals($page->id, $event->other['pageid']);
 313          $this->assertEquals($oldversion->id, $event->objectid);
 314  
 315          // Checking that the event contains the page_deleted event.
 316          $event = array_pop($events);
 317          $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
 318          $this->assertEquals($context, $event->get_context());
 319          $this->assertEquals($page->id, $event->objectid);
 320  
 321          // Checking that the event contains the expected values.
 322          $event = array_pop($events);
 323          $this->assertInstanceOf('\mod_wiki\event\page_locks_deleted', $event);
 324          $this->assertEquals($context, $event->get_context());
 325          $this->assertEquals($page->id, $event->objectid);
 326  
 327          // Delete all pages.
 328          $page1 = $this->wikigenerator->create_first_page($this->wiki);
 329          $page2 = $this->wikigenerator->create_content($this->wiki);
 330          $page3 = $this->wikigenerator->create_content($this->wiki, array('title' => 'Custom title'));
 331  
 332          // Triggering and capturing the event.
 333          $sink = $this->redirectEvents();
 334          wiki_delete_pages($context, array($page1->id, $page2->id));
 335          $events = $sink->get_events();
 336          $this->assertCount(8, $events);
 337          $event = array_pop($events);
 338  
 339          // Checking that the event contains the expected values.
 340          $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
 341          $this->assertEquals($context, $event->get_context());
 342          $this->assertEquals($page2->id, $event->objectid);
 343      }
 344  
 345      /**
 346       * Test page_updated event.
 347       */
 348      public function test_page_updated() {
 349          global $USER;
 350  
 351          $this->setUp();
 352  
 353          $page = $this->wikigenerator->create_first_page($this->wiki);
 354          $context = \context_module::instance($this->wiki->cmid);
 355  
 356          // Triggering and capturing the event.
 357          $sink = $this->redirectEvents();
 358          wiki_save_page($page, 'New content', $USER->id);
 359          $events = $sink->get_events();
 360          $this->assertCount(1, $events);
 361          $event = reset($events);
 362  
 363          // Checking that the event contains the expected values.
 364          $this->assertInstanceOf('\mod_wiki\event\page_updated', $event);
 365          $this->assertEquals($context, $event->get_context());
 366          $this->assertEquals($page->id, $event->objectid);
 367      }
 368  
 369      /**
 370       * Test page_diff_viewed event.
 371       */
 372      public function test_page_diff_viewed() {
 373          // There is no proper API to call or trigger this event, so simulating event
 374          // to check if event returns the right information.
 375  
 376          $this->setUp();
 377  
 378          $page = $this->wikigenerator->create_first_page($this->wiki);
 379          $context = \context_module::instance($this->wiki->cmid);
 380  
 381          $params = array(
 382                  'context' => $context,
 383                  'objectid' => $page->id,
 384                  'other' => array(
 385                      'comparewith' => 1,
 386                      'compare' => 2
 387                      )
 388                  );
 389          $event = \mod_wiki\event\page_diff_viewed::create($params);
 390  
 391          // Triggering and capturing the event.
 392          $sink = $this->redirectEvents();
 393          $event->trigger();
 394          $events = $sink->get_events();
 395          $this->assertCount(1, $events);
 396          $event = reset($events);
 397  
 398          // Checking that the event contains the expected values.
 399          $this->assertInstanceOf('\mod_wiki\event\page_diff_viewed', $event);
 400          $this->assertEquals($context, $event->get_context());
 401          $this->assertEquals($page->id, $event->objectid);
 402      }
 403  
 404      /**
 405       * Test page_history_viewed event.
 406       */
 407      public function test_page_history_viewed() {
 408          // There is no proper API to call or trigger this event, so simulating event
 409          // to check if event returns the right information.
 410  
 411          $this->setUp();
 412  
 413          $page = $this->wikigenerator->create_first_page($this->wiki);
 414          $context = \context_module::instance($this->wiki->cmid);
 415  
 416          $params = array(
 417                  'context' => $context,
 418                  'objectid' => $page->id
 419                  );
 420          $event = \mod_wiki\event\page_history_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_history_viewed', $event);
 431          $this->assertEquals($context, $event->get_context());
 432          $this->assertEquals($page->id, $event->objectid);
 433      }
 434  
 435      /**
 436       * Test page_map_viewed event.
 437       */
 438      public function test_page_map_viewed() {
 439          // There is no proper API to call or trigger this event, so simulating event
 440          // to check if event returns the right information.
 441  
 442          $this->setUp();
 443  
 444          $page = $this->wikigenerator->create_first_page($this->wiki);
 445          $context = \context_module::instance($this->wiki->cmid);
 446  
 447          $params = array(
 448                  'context' => $context,
 449                  'objectid' => $page->id,
 450                  'other' => array(
 451                      'option' => 0
 452                      )
 453                  );
 454          $event = \mod_wiki\event\page_map_viewed::create($params);
 455  
 456          // Triggering and capturing the event.
 457          $sink = $this->redirectEvents();
 458          $event->trigger();
 459          $events = $sink->get_events();
 460          $this->assertCount(1, $events);
 461          $event = reset($events);
 462  
 463          // Checking that the event contains the expected values.
 464          $this->assertInstanceOf('\mod_wiki\event\page_map_viewed', $event);
 465          $this->assertEquals($context, $event->get_context());
 466          $this->assertEquals($page->id, $event->objectid);
 467          $this->assertEquals(0, $event->other['option']);
 468      }
 469  
 470      /**
 471       * Test page_version_viewed event.
 472       */
 473      public function test_page_version_viewed() {
 474          // There is no proper API to call or trigger this event, so simulating event
 475          // to check if event returns the right information.
 476  
 477          $this->setUp();
 478  
 479          $page = $this->wikigenerator->create_first_page($this->wiki);
 480          $context = \context_module::instance($this->wiki->cmid);
 481  
 482          $params = array(
 483                  'context' => $context,
 484                  'objectid' => $page->id,
 485                  'other' => array(
 486                      'versionid' => 1
 487                      )
 488                  );
 489          $event = \mod_wiki\event\page_version_viewed::create($params);
 490  
 491          // Triggering and capturing the event.
 492          $sink = $this->redirectEvents();
 493          $event->trigger();
 494          $events = $sink->get_events();
 495          $this->assertCount(1, $events);
 496          $event = reset($events);
 497  
 498          // Checking that the event contains the expected values.
 499          $this->assertInstanceOf('\mod_wiki\event\page_version_viewed', $event);
 500          $this->assertEquals($context, $event->get_context());
 501          $this->assertEquals($page->id, $event->objectid);
 502          $this->assertEquals(1, $event->other['versionid']);
 503      }
 504  
 505      /**
 506       * Test page_version_restored event.
 507       */
 508      public function test_page_version_restored() {
 509          $this->setUp();
 510  
 511          $page = $this->wikigenerator->create_first_page($this->wiki);
 512          $context = \context_module::instance($this->wiki->cmid);
 513          $version = wiki_get_current_version($page->id);
 514  
 515          // Triggering and capturing the event.
 516          $sink = $this->redirectEvents();
 517          wiki_restore_page($page, $version, $context);
 518          $events = $sink->get_events();
 519          $this->assertCount(2, $events);
 520          $event = array_pop($events);
 521  
 522          // Checking that the event contains the expected values.
 523          $this->assertInstanceOf('\mod_wiki\event\page_version_restored', $event);
 524          $this->assertEquals($context, $event->get_context());
 525          $this->assertEquals($version->id, $event->objectid);
 526          $this->assertEquals($page->id, $event->other['pageid']);
 527      }
 528  }