Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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   * This file contains the class that handles testing of course events.
  19   *
  20   * @package core
  21   * @copyright  2016 Stephen Bourget
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * This file contains the class that handles testing of course events.
  29   *
  30   * @package core_course
  31   * @copyright  2016 Stephen Bourget
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_course_events_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Tests set up
  38       */
  39      protected function setUp(): void {
  40          global $CFG;
  41          require_once($CFG->dirroot . '/course/lib.php');
  42          $this->resetAfterTest();
  43      }
  44  
  45      /**
  46       * Test the course category viewed.
  47       *
  48       * There is no external API for viewing a category, so the unit test will simply
  49       * create and trigger the event and ensure data is returned as expected.
  50       */
  51      public function test_course_category_viewed_event() {
  52  
  53          // Create a category.
  54          $category = $this->getDataGenerator()->create_category();
  55  
  56          // Trigger an event: course category viewed.
  57          $eventparams = array(
  58              'objectid' => $category->id,
  59              'context' => context_system::instance(),
  60          );
  61  
  62          $event = \core\event\course_category_viewed::create($eventparams);
  63          // Trigger and capture the event.
  64          $sink = $this->redirectEvents();
  65          $event->trigger();
  66          $events = $sink->get_events();
  67          $event = reset($events);
  68  
  69          // Check that the event data is valid.
  70          $this->assertInstanceOf('\core\event\course_category_viewed', $event);
  71          $this->assertEquals($category->id, $event->objectid);
  72          $this->assertDebuggingNotCalled();
  73          $sink->close();
  74  
  75      }
  76  
  77      /**
  78       * Test the course information viewed.
  79       *
  80       * There is no external API for viewing course information so the unit test will simply
  81       * create and trigger the event and ensure data is returned as expected.
  82       */
  83      public function test_course_information_viewed_event() {
  84  
  85          // Create a course.
  86          $data = new stdClass();
  87          $course = $this->getDataGenerator()->create_course($data);
  88  
  89          // Trigger an event: course category viewed.
  90          $eventparams = array(
  91              'objectid' => $course->id,
  92              'context' => context_course::instance($course->id),
  93          );
  94  
  95          $event = \core\event\course_information_viewed::create($eventparams);
  96          // Trigger and capture the event.
  97          $sink = $this->redirectEvents();
  98          $event->trigger();
  99          $events = $sink->get_events();
 100          $event = reset($events);
 101  
 102          // Check that the event data is valid.
 103          $this->assertInstanceOf('\core\event\course_information_viewed', $event);
 104          $this->assertEquals($course->id, $event->objectid);
 105          $this->assertDebuggingNotCalled();
 106          $sink->close();
 107  
 108      }
 109  
 110      /**
 111       * Test the courses searched.
 112       *
 113       * There is no external API for viewing course information so the unit test will simply
 114       * create and trigger the event and ensure data is returned as expected.
 115       */
 116      public function test_courses_searched_event() {
 117  
 118          // Trigger an event: courses searched.
 119          $search = 'mysearch';
 120          $eventparams = array(
 121              'context' => context_system::instance(),
 122              'other' => array('query' => $search)
 123          );
 124  
 125          $event = \core\event\courses_searched::create($eventparams);
 126          // Trigger and capture the event.
 127          $sink = $this->redirectEvents();
 128          $event->trigger();
 129          $events = $sink->get_events();
 130          $event = reset($events);
 131  
 132          // Check that the event data is valid.
 133          $this->assertInstanceOf('\core\event\courses_searched', $event);
 134          $this->assertEquals($search, $event->other['query']);
 135          $this->assertDebuggingNotCalled();
 136          $sink->close();
 137  
 138      }
 139  }