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 test.
  19   *
  20   * @package    mod_resource
  21   * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_resource\event;
  26  
  27  /**
  28   * Resource events test cases.
  29   *
  30   * @package    mod_resource
  31   * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class events_test extends \advanced_testcase {
  35  
  36      /**
  37       * Setup is called before calling test case.
  38       */
  39      public function setUp(): void {
  40          $this->resetAfterTest();
  41  
  42          // Must be a non-guest user to create resources.
  43          $this->setAdminUser();
  44      }
  45  
  46      /**
  47       * Test course_module_instance_list_viewed event.
  48       */
  49      public function test_course_module_instance_list_viewed() {
  50          // There is no proper API to call to trigger this event, so what we are
  51          // doing here is simply making sure that the events returns the right information.
  52  
  53          $course = $this->getDataGenerator()->create_course();
  54          $params = array(
  55              'context' => \context_course::instance($course->id)
  56          );
  57          $event = \mod_resource\event\course_module_instance_list_viewed::create($params);
  58  
  59          // Triggering and capturing the event.
  60          $sink = $this->redirectEvents();
  61          $event->trigger();
  62          $events = $sink->get_events();
  63          $this->assertCount(1, $events);
  64          $event = reset($events);
  65  
  66          // Checking that the event contains the expected values.
  67          $this->assertInstanceOf('\mod_resource\event\course_module_instance_list_viewed', $event);
  68          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
  69          $expected = array($course->id, 'resource', 'view all', 'index.php?id='.$course->id, '');
  70          $this->assertEventLegacyLogData($expected, $event);
  71          $this->assertEventContextNotUsed($event);
  72      }
  73  
  74      /**
  75       * Test course_module_viewed event.
  76       */
  77      public function test_course_module_viewed() {
  78          // There is no proper API to call to trigger this event, so what we are
  79          // doing here is simply making sure that the events returns the right information.
  80  
  81          $course = $this->getDataGenerator()->create_course();
  82          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
  83  
  84          $params = array(
  85              'context' => \context_module::instance($resource->cmid),
  86              'objectid' => $resource->id
  87          );
  88          $event = \mod_resource\event\course_module_viewed::create($params);
  89  
  90          // Triggering and capturing the event.
  91          $sink = $this->redirectEvents();
  92          $event->trigger();
  93          $events = $sink->get_events();
  94          $this->assertCount(1, $events);
  95          $event = reset($events);
  96  
  97          // Checking that the event contains the expected values.
  98          $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
  99          $this->assertEquals(\context_module::instance($resource->cmid), $event->get_context());
 100          $this->assertEquals($resource->id, $event->objectid);
 101          $expected = array($course->id, 'resource', 'view', 'view.php?id=' . $resource->cmid, $resource->id, $resource->cmid);
 102          $this->assertEventLegacyLogData($expected, $event);
 103          $this->assertEventContextNotUsed($event);
 104      }
 105  }