Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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          $this->assertEventContextNotUsed($event);
  70      }
  71  
  72      /**
  73       * Test course_module_viewed event.
  74       */
  75      public function test_course_module_viewed() {
  76          // There is no proper API to call to trigger this event, so what we are
  77          // doing here is simply making sure that the events returns the right information.
  78  
  79          $course = $this->getDataGenerator()->create_course();
  80          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
  81  
  82          $params = array(
  83              'context' => \context_module::instance($resource->cmid),
  84              'objectid' => $resource->id
  85          );
  86          $event = \mod_resource\event\course_module_viewed::create($params);
  87  
  88          // Triggering and capturing the event.
  89          $sink = $this->redirectEvents();
  90          $event->trigger();
  91          $events = $sink->get_events();
  92          $this->assertCount(1, $events);
  93          $event = reset($events);
  94  
  95          // Checking that the event contains the expected values.
  96          $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
  97          $this->assertEquals(\context_module::instance($resource->cmid), $event->get_context());
  98          $this->assertEquals($resource->id, $event->objectid);
  99          $this->assertEventContextNotUsed($event);
 100      }
 101  }