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   * Unit tests for lib.php
  19   *
  20   * @package    mod_glossary
  21   * @category   test
  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  
  28  /**
  29   * Unit tests for glossary events.
  30   *
  31   * @package   mod_glossary
  32   * @category  test
  33   * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class glossary_event_testcase extends advanced_testcase {
  37  
  38      public function setUp() {
  39          $this->resetAfterTest();
  40      }
  41  
  42      /**
  43       * Test comment_created event.
  44       */
  45      public function test_comment_created() {
  46          global $CFG;
  47          require_once($CFG->dirroot . '/comment/lib.php');
  48  
  49          // Create a record for adding comment.
  50          $this->setAdminUser();
  51          $course = $this->getDataGenerator()->create_course();
  52          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
  53          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
  54  
  55          $entry = $glossarygenerator->create_content($glossary);
  56  
  57          $context = context_module::instance($glossary->cmid);
  58          $cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
  59          $cmt = new stdClass();
  60          $cmt->component = 'mod_glossary';
  61          $cmt->context = $context;
  62          $cmt->course = $course;
  63          $cmt->cm = $cm;
  64          $cmt->area = 'glossary_entry';
  65          $cmt->itemid = $entry->id;
  66          $cmt->showcount = true;
  67          $comment = new comment($cmt);
  68  
  69          // Triggering and capturing the event.
  70          $sink = $this->redirectEvents();
  71          $comment->add('New comment');
  72          $events = $sink->get_events();
  73          $this->assertCount(1, $events);
  74          $event = reset($events);
  75  
  76          // Checking that the event contains the expected values.
  77          $this->assertInstanceOf('\mod_glossary\event\comment_created', $event);
  78          $this->assertEquals($context, $event->get_context());
  79          $url = new moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid));
  80          $this->assertEquals($url, $event->get_url());
  81          $this->assertEventContextNotUsed($event);
  82      }
  83  
  84      /**
  85       * Test comment_deleted event.
  86       */
  87      public function test_comment_deleted() {
  88          global $CFG;
  89          require_once($CFG->dirroot . '/comment/lib.php');
  90  
  91          // Create a record for deleting comment.
  92          $this->setAdminUser();
  93          $course = $this->getDataGenerator()->create_course();
  94          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
  95          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
  96  
  97          $entry = $glossarygenerator->create_content($glossary);
  98  
  99          $context = context_module::instance($glossary->cmid);
 100          $cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
 101          $cmt = new stdClass();
 102          $cmt->component = 'mod_glossary';
 103          $cmt->context = $context;
 104          $cmt->course = $course;
 105          $cmt->cm = $cm;
 106          $cmt->area = 'glossary_entry';
 107          $cmt->itemid = $entry->id;
 108          $cmt->showcount = true;
 109          $comment = new comment($cmt);
 110          $newcomment = $comment->add('New comment 1');
 111  
 112          // Triggering and capturing the event.
 113          $sink = $this->redirectEvents();
 114          $comment->delete($newcomment->id);
 115          $events = $sink->get_events();
 116          $this->assertCount(1, $events);
 117          $event = reset($events);
 118  
 119          // Checking that the event contains the expected values.
 120          $this->assertInstanceOf('\mod_glossary\event\comment_deleted', $event);
 121          $this->assertEquals($context, $event->get_context());
 122          $url = new moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid));
 123          $this->assertEquals($url, $event->get_url());
 124          $this->assertEventContextNotUsed($event);
 125      }
 126  
 127      public function test_course_module_viewed() {
 128          global $DB;
 129          // There is no proper API to call to trigger this event, so what we are
 130          // doing here is simply making sure that the events returns the right information.
 131  
 132          $course = $this->getDataGenerator()->create_course();
 133          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
 134  
 135          $dbcourse = $DB->get_record('course', array('id' => $course->id));
 136          $dbglossary = $DB->get_record('glossary', array('id' => $glossary->id));
 137          $context = context_module::instance($glossary->cmid);
 138          $mode = 'letter';
 139  
 140          $event = \mod_glossary\event\course_module_viewed::create(array(
 141              'objectid' => $dbglossary->id,
 142              'context' => $context,
 143              'other' => array('mode' => $mode)
 144          ));
 145  
 146          $event->add_record_snapshot('course', $dbcourse);
 147          $event->add_record_snapshot('glossary', $dbglossary);
 148  
 149          // Triggering and capturing the event.
 150          $sink = $this->redirectEvents();
 151          $event->trigger();
 152          $events = $sink->get_events();
 153          $this->assertCount(1, $events);
 154          $event = reset($events);
 155  
 156          // Checking that the event contains the expected values.
 157          $this->assertInstanceOf('\mod_glossary\event\course_module_viewed', $event);
 158          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 159          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 160          $this->assertEquals($glossary->id, $event->objectid);
 161          $expected = array($course->id, 'glossary', 'view', 'view.php?id=' . $glossary->cmid . '&amp;tab=-1',
 162              $glossary->id, $glossary->cmid);
 163          $this->assertEventLegacyLogData($expected, $event);
 164          $this->assertEquals(new moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid, 'mode' => $mode)), $event->get_url());
 165          $this->assertEventContextNotUsed($event);
 166      }
 167  
 168      public function test_course_module_instance_list_viewed() {
 169          // There is no proper API to call to trigger this event, so what we are
 170          // doing here is simply making sure that the events returns the right information.
 171  
 172          $course = $this->getDataGenerator()->create_course();
 173  
 174          $event = \mod_glossary\event\course_module_instance_list_viewed::create(array(
 175              'context' => context_course::instance($course->id)
 176          ));
 177  
 178          // Triggering and capturing the event.
 179          $sink = $this->redirectEvents();
 180          $event->trigger();
 181          $events = $sink->get_events();
 182          $this->assertCount(1, $events);
 183          $event = reset($events);
 184  
 185          // Checking that the event contains the expected values.
 186          $this->assertInstanceOf('\mod_glossary\event\course_module_instance_list_viewed', $event);
 187          $this->assertEquals(CONTEXT_COURSE, $event->contextlevel);
 188          $this->assertEquals($course->id, $event->contextinstanceid);
 189          $expected = array($course->id, 'glossary', 'view all', 'index.php?id='.$course->id, '');
 190          $this->assertEventLegacyLogData($expected, $event);
 191          $this->assertEventContextNotUsed($event);
 192      }
 193  
 194      public function test_entry_created() {
 195          // There is no proper API to call to trigger this event, so what we are
 196          // doing here is simply making sure that the events returns the right information.
 197  
 198          $this->setAdminUser();
 199          $course = $this->getDataGenerator()->create_course();
 200          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
 201          $context = context_module::instance($glossary->cmid);
 202  
 203          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 204          $entry = $glossarygenerator->create_content($glossary);
 205  
 206          $eventparams = array(
 207              'context' => $context,
 208              'objectid' => $entry->id,
 209              'other' => array('concept' => $entry->concept)
 210          );
 211          $event = \mod_glossary\event\entry_created::create($eventparams);
 212          $event->add_record_snapshot('glossary_entries', $entry);
 213  
 214          $sink = $this->redirectEvents();
 215          $event->trigger();
 216          $events = $sink->get_events();
 217          $this->assertCount(1, $events);
 218          $event = reset($events);
 219  
 220          // Checking that the event contains the expected values.
 221          $this->assertInstanceOf('\mod_glossary\event\entry_created', $event);
 222          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 223          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 224          $expected = array($course->id, "glossary", "add entry",
 225              "view.php?id={$glossary->cmid}&amp;mode=entry&amp;hook={$entry->id}", $entry->id, $glossary->cmid);
 226          $this->assertEventLegacyLogData($expected, $event);
 227          $this->assertEventContextNotUsed($event);
 228      }
 229  
 230      public function test_entry_updated() {
 231          // There is no proper API to call to trigger this event, so what we are
 232          // doing here is simply making sure that the events returns the right information.
 233  
 234          $this->setAdminUser();
 235          $course = $this->getDataGenerator()->create_course();
 236          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
 237          $context = context_module::instance($glossary->cmid);
 238  
 239          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 240          $entry = $glossarygenerator->create_content($glossary);
 241  
 242          $eventparams = array(
 243              'context' => $context,
 244              'objectid' => $entry->id,
 245              'other' => array('concept' => $entry->concept)
 246          );
 247          $event = \mod_glossary\event\entry_updated::create($eventparams);
 248          $event->add_record_snapshot('glossary_entries', $entry);
 249  
 250          $sink = $this->redirectEvents();
 251          $event->trigger();
 252          $events = $sink->get_events();
 253          $this->assertCount(1, $events);
 254          $event = reset($events);
 255  
 256          // Checking that the event contains the expected values.
 257          $this->assertInstanceOf('\mod_glossary\event\entry_updated', $event);
 258          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 259          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 260          $expected = array($course->id, "glossary", "update entry",
 261              "view.php?id={$glossary->cmid}&amp;mode=entry&amp;hook={$entry->id}", $entry->id, $glossary->cmid);
 262          $this->assertEventLegacyLogData($expected, $event);
 263          $this->assertEventContextNotUsed($event);
 264      }
 265  
 266      public function test_entry_deleted() {
 267          global $DB;
 268          // There is no proper API to call to trigger this event, so what we are
 269          // doing here is simply making sure that the events returns the right information.
 270  
 271          $this->setAdminUser();
 272          $course = $this->getDataGenerator()->create_course();
 273          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
 274          $context = context_module::instance($glossary->cmid);
 275          $prevmode = 'view';
 276          $hook = 'ALL';
 277  
 278          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 279          $entry = $glossarygenerator->create_content($glossary);
 280  
 281          $DB->delete_records('glossary_entries', array('id' => $entry->id));
 282  
 283          $eventparams = array(
 284              'context' => $context,
 285              'objectid' => $entry->id,
 286              'other' => array(
 287                  'mode' => $prevmode,
 288                  'hook' => $hook,
 289                  'concept' => $entry->concept
 290              )
 291          );
 292          $event = \mod_glossary\event\entry_deleted::create($eventparams);
 293          $event->add_record_snapshot('glossary_entries', $entry);
 294  
 295          $sink = $this->redirectEvents();
 296          $event->trigger();
 297          $events = $sink->get_events();
 298          $this->assertCount(1, $events);
 299          $event = reset($events);
 300  
 301          // Checking that the event contains the expected values.
 302          $this->assertInstanceOf('\mod_glossary\event\entry_deleted', $event);
 303          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 304          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 305          $expected = array($course->id, "glossary", "delete entry",
 306              "view.php?id={$glossary->cmid}&amp;mode={$prevmode}&amp;hook={$hook}", $entry->id, $glossary->cmid);
 307          $this->assertEventLegacyLogData($expected, $event);
 308          $this->assertEventContextNotUsed($event);
 309      }
 310  
 311      public function test_category_created() {
 312          global $DB;
 313          // There is no proper API to call to trigger this event, so what we are
 314          // doing here is simply making sure that the events returns the right information.
 315  
 316          $this->setAdminUser();
 317          $course = $this->getDataGenerator()->create_course();
 318          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
 319          $context = context_module::instance($glossary->cmid);
 320  
 321          // Create category and trigger event.
 322          $category = new stdClass();
 323          $category->name = 'New category';
 324          $category->usedynalink = 0;
 325          $category->id = $DB->insert_record('glossary_categories', $category);
 326  
 327          $event = \mod_glossary\event\category_created::create(array(
 328              'context' => $context,
 329              'objectid' => $category->id
 330          ));
 331  
 332          $sink = $this->redirectEvents();
 333          $event->trigger();
 334          $events = $sink->get_events();
 335          $this->assertCount(1, $events);
 336          $event = reset($events);
 337  
 338          // Checking that the event contains the expected values.
 339          $this->assertInstanceOf('\mod_glossary\event\category_created', $event);
 340          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 341          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 342          //add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
 343          $expected = array($course->id, "glossary", "add category",
 344              "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
 345          $this->assertEventLegacyLogData($expected, $event);
 346          $this->assertEventContextNotUsed($event);
 347  
 348          // Update category and trigger event.
 349          $category->name = 'Updated category';
 350          $DB->update_record('glossary_categories', $category);
 351  
 352          $event = \mod_glossary\event\category_updated::create(array(
 353              'context' => $context,
 354              'objectid' => $category->id
 355          ));
 356  
 357          $sink = $this->redirectEvents();
 358          $event->trigger();
 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_glossary\event\category_updated', $event);
 365          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 366          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 367          //add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
 368          $expected = array($course->id, "glossary", "edit category",
 369              "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
 370          $this->assertEventLegacyLogData($expected, $event);
 371          $this->assertEventContextNotUsed($event);
 372  
 373  
 374          // Delete category and trigger event.
 375          $category = $DB->get_record('glossary_categories', array('id' => $category->id));
 376          $DB->delete_records('glossary_categories', array('id' => $category->id));
 377  
 378          $event = \mod_glossary\event\category_deleted::create(array(
 379              'context' => $context,
 380              'objectid' => $category->id
 381          ));
 382          $event->add_record_snapshot('glossary_categories', $category);
 383  
 384          $sink = $this->redirectEvents();
 385          $event->trigger();
 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_glossary\event\category_deleted', $event);
 392          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 393          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 394          //add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
 395          $expected = array($course->id, "glossary", "delete category",
 396              "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
 397          $this->assertEventLegacyLogData($expected, $event);
 398          $this->assertEventContextNotUsed($event);
 399      }
 400  
 401      public function test_entry_approved() {
 402          global $DB;
 403          // There is no proper API to call to trigger this event, so what we are
 404          // doing here is simply making sure that the events returns the right information.
 405  
 406          $this->setAdminUser();
 407          $course = $this->getDataGenerator()->create_course();
 408          $student = $this->getDataGenerator()->create_user();
 409          $rolestudent = $DB->get_record('role', array('shortname' => 'student'));
 410          $this->getDataGenerator()->enrol_user($student->id, $course->id, $rolestudent->id);
 411          $teacher = $this->getDataGenerator()->create_user();
 412          $roleteacher = $DB->get_record('role', array('shortname' => 'teacher'));
 413          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
 414  
 415          $this->setUser($teacher);
 416          $glossary = $this->getDataGenerator()->create_module('glossary',
 417                  array('course' => $course, 'defaultapproval' => 0));
 418          $context = context_module::instance($glossary->cmid);
 419  
 420          $this->setUser($student);
 421          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 422          $entry = $glossarygenerator->create_content($glossary);
 423          $this->assertEquals(0, $entry->approved);
 424  
 425          // Approve entry, trigger and validate event.
 426          $this->setUser($teacher);
 427          $newentry = new stdClass();
 428          $newentry->id           = $entry->id;
 429          $newentry->approved     = true;
 430          $newentry->timemodified = time();
 431          $DB->update_record("glossary_entries", $newentry);
 432          $params = array(
 433              'context' => $context,
 434              'objectid' => $entry->id
 435          );
 436          $event = \mod_glossary\event\entry_approved::create($params);
 437  
 438          $sink = $this->redirectEvents();
 439          $event->trigger();
 440          $events = $sink->get_events();
 441          $this->assertCount(1, $events);
 442          $event = reset($events);
 443  
 444          // Checking that the event contains the expected values.
 445          $this->assertInstanceOf('\mod_glossary\event\entry_approved', $event);
 446          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 447          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 448          $expected = array($course->id, "glossary", "approve entry",
 449              "showentry.php?id={$glossary->cmid}&amp;eid={$entry->id}", $entry->id, $glossary->cmid);
 450          $this->assertEventLegacyLogData($expected, $event);
 451          $this->assertEventContextNotUsed($event);
 452  
 453  
 454          // Disapprove entry, trigger and validate event.
 455          $this->setUser($teacher);
 456          $newentry = new stdClass();
 457          $newentry->id           = $entry->id;
 458          $newentry->approved     = false;
 459          $newentry->timemodified = time();
 460          $DB->update_record("glossary_entries", $newentry);
 461          $params = array(
 462              'context' => $context,
 463              'objectid' => $entry->id
 464          );
 465          $event = \mod_glossary\event\entry_disapproved::create($params);
 466  
 467          $sink = $this->redirectEvents();
 468          $event->trigger();
 469          $events = $sink->get_events();
 470          $this->assertCount(1, $events);
 471          $event = reset($events);
 472  
 473          // Checking that the event contains the expected values.
 474          $this->assertInstanceOf('\mod_glossary\event\entry_disapproved', $event);
 475          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 476          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 477          $expected = array($course->id, "glossary", "disapprove entry",
 478              "showentry.php?id={$glossary->cmid}&amp;eid={$entry->id}", $entry->id, $glossary->cmid);
 479          $this->assertEventLegacyLogData($expected, $event);
 480          $this->assertEventContextNotUsed($event);
 481      }
 482  
 483      public function test_entry_viewed() {
 484          // There is no proper API to call to trigger this event, so what we are
 485          // doing here is simply making sure that the events returns the right information.
 486  
 487          $this->setAdminUser();
 488          $course = $this->getDataGenerator()->create_course();
 489          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
 490          $context = context_module::instance($glossary->cmid);
 491  
 492          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 493          $entry = $glossarygenerator->create_content($glossary);
 494  
 495          $event = \mod_glossary\event\entry_viewed::create(array(
 496              'objectid' => $entry->id,
 497              'context' => $context
 498          ));
 499  
 500          $sink = $this->redirectEvents();
 501          $event->trigger();
 502          $events = $sink->get_events();
 503          $this->assertCount(1, $events);
 504          $event = reset($events);
 505  
 506          // Checking that the event contains the expected values.
 507          $this->assertInstanceOf('\mod_glossary\event\entry_viewed', $event);
 508          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 509          $this->assertEquals($glossary->cmid, $event->contextinstanceid);
 510          $expected = array($course->id, "glossary", "view entry",
 511              "showentry.php?eid={$entry->id}", $entry->id, $glossary->cmid);
 512          $this->assertEventLegacyLogData($expected, $event);
 513          $this->assertEventContextNotUsed($event);
 514      }
 515  }