Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Tests for forum report summary events.
  19   *
  20   * @package    forumreport_summary
  21   * @category   test
  22   * @copyright  2019 Michael Hawkins <michaelh@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace forumreport_summary\event;
  27  
  28  /**
  29   * Tests for forum report summary events.
  30   *
  31   * @package    forumreport_summary
  32   * @category   test
  33   * @copyright  2019 Michael Hawkins <michaelh@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class events_test extends \advanced_testcase {
  37      /**
  38       * Test report_downloaded event.
  39       */
  40      public function test_report_downloaded() {
  41          global $DB;
  42  
  43          $this->resetAfterTest();
  44  
  45          // Create course and teacher user.
  46          $course = $this->getDataGenerator()->create_course();
  47          $teacher = $this->getDataGenerator()->create_user();
  48          $roleteacher = $DB->get_record('role', ['shortname' => 'teacher']);
  49          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
  50  
  51          // Create forum.
  52          $this->setUser($teacher);
  53          $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
  54          $context = \context_module::instance($forum->cmid);
  55  
  56          // Trigger and capture event.
  57          $eventparams = [
  58              'context' => $context,
  59              'other' => [
  60                  'forumid' => $forum->id,
  61                  'hasviewall' => true,
  62              ],
  63          ];
  64          $event = \forumreport_summary\event\report_downloaded::create($eventparams);
  65          $sink = $this->redirectEvents();
  66          $event->trigger();
  67          $events = $sink->get_events();
  68          $this->assertCount(1, $events);
  69          $event = reset($events);
  70          $sink->close();
  71  
  72          // Check the event contains the expected data.
  73          $this->assertInstanceOf('\forumreport_summary\event\report_downloaded', $event);
  74          $this->assertEquals($context, $event->get_context());
  75          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
  76          $this->assertEquals($forum->cmid, $event->contextinstanceid);
  77          $this->assertEquals($teacher->id, $event->userid);
  78          $url = new \moodle_url('/mod/forum/report/summary/index.php',
  79                  ['courseid' => $course->id, 'forumid' => $forum->id]);
  80          $this->assertEquals($url, $event->get_url());
  81          $this->assertEventContextNotUsed($event);
  82          $this->assertNotEmpty($event->get_name());
  83          $this->assertNotEmpty($event->get_description());
  84      }
  85  
  86      /**
  87       * Test report_viewed event.
  88       */
  89      public function test_report_viewed() {
  90          global $DB;
  91  
  92          $this->resetAfterTest();
  93  
  94          // Create course and teacher user.
  95          $course = $this->getDataGenerator()->create_course();
  96          $teacher = $this->getDataGenerator()->create_user();
  97          $roleteacher = $DB->get_record('role', ['shortname' => 'teacher']);
  98          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
  99  
 100          // Create forum.
 101          $this->setUser($teacher);
 102          $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
 103          $context = \context_module::instance($forum->cmid);
 104  
 105          // Trigger and capture event.
 106          $eventparams = [
 107              'context' => $context,
 108              'other' => [
 109                  'forumid' => $forum->id,
 110                  'hasviewall' => true,
 111              ],
 112          ];
 113          $event = \forumreport_summary\event\report_viewed::create($eventparams);
 114          $sink = $this->redirectEvents();
 115          $event->trigger();
 116          $events = $sink->get_events();
 117          $this->assertCount(1, $events);
 118          $event = reset($events);
 119          $sink->close();
 120  
 121          // Check the event contains the expected data.
 122          $this->assertInstanceOf('\forumreport_summary\event\report_viewed', $event);
 123          $this->assertEquals($context, $event->get_context());
 124          $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
 125          $this->assertEquals($forum->cmid, $event->contextinstanceid);
 126          $this->assertEquals($teacher->id, $event->userid);
 127          $url = new \moodle_url('/mod/forum/report/summary/index.php',
 128                  ['courseid' => $course->id, 'forumid' => $forum->id]);
 129          $this->assertEquals($url, $event->get_url());
 130          $this->assertEventContextNotUsed($event);
 131          $this->assertNotEmpty($event->get_name());
 132          $this->assertNotEmpty($event->get_description());
 133      }
 134  }