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 tests.
  19   *
  20   * @package    mod_survey
  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_survey\event;
  26  
  27  /**
  28   * Events tests class.
  29   *
  30   * @package    mod_survey
  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.
  38       */
  39      public function setUp(): void {
  40          $this->resetAfterTest();
  41      }
  42  
  43      /**
  44       * Test report downloaded event.
  45       */
  46      public function test_report_downloaded() {
  47          // There is no proper API to call to generate chapters for a book, so what we are
  48          // doing here is simply making sure that the events returns the right information.
  49  
  50          $course = $this->getDataGenerator()->create_course();
  51          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
  52  
  53          $params = array(
  54              'objectid' => $survey->id,
  55              'context' => \context_module::instance($survey->cmid),
  56              'courseid' => $course->id,
  57              'other' => array('type' => 'xls')
  58          );
  59          $event = \mod_survey\event\report_downloaded::create($params);
  60  
  61          // Triggering and capturing the event.
  62          $sink = $this->redirectEvents();
  63          $event->trigger();
  64          $events = $sink->get_events();
  65          $this->assertCount(1, $events);
  66          $event = reset($events);
  67  
  68          // Checking that the event contains the expected values.
  69          $this->assertInstanceOf('\mod_survey\event\report_downloaded', $event);
  70          $this->assertEquals(\context_module::instance($survey->cmid), $event->get_context());
  71          $this->assertEquals($survey->id, $event->objectid);
  72          $url = new \moodle_url('/mod/survey/download.php', array('id' => $survey->cmid, 'type' => 'xls'));
  73          $expected = array($course->id, "survey", "download", $url->out(), $survey->id, $survey->cmid);
  74          $this->assertEventLegacyLogData($expected, $event);
  75          $this->assertEventContextNotUsed($event);
  76      }
  77  
  78      /**
  79       * Test report viewed event.
  80       */
  81      public function test_report_viewed() {
  82          // There is no proper API to call to generate chapters for a book, so what we are
  83          // doing here is simply making sure that the events returns the right information.
  84  
  85          $course = $this->getDataGenerator()->create_course();
  86          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
  87  
  88          $params = array(
  89              'objectid' => $survey->id,
  90              'context' => \context_module::instance($survey->cmid),
  91              'courseid' => $course->id
  92          );
  93          $event = \mod_survey\event\report_viewed::create($params);
  94  
  95          // Triggering and capturing the event.
  96          $sink = $this->redirectEvents();
  97          $event->trigger();
  98          $events = $sink->get_events();
  99          $this->assertCount(1, $events);
 100          $event = reset($events);
 101  
 102          // Checking that the event contains the expected values.
 103          $this->assertInstanceOf('\mod_survey\event\report_viewed', $event);
 104          $this->assertEquals(\context_module::instance($survey->cmid), $event->get_context());
 105          $this->assertEquals($survey->id, $event->objectid);
 106          $expected = array($course->id, "survey", "view report", 'report.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
 107          $this->assertEventLegacyLogData($expected, $event);
 108          $this->assertEventContextNotUsed($event);
 109      }
 110  
 111      /**
 112       * Test response submitted event.
 113       */
 114      public function test_response_submitted() {
 115          // There is no proper API to call to generate chapters for a book, so what we are
 116          // doing here is simply making sure that the events returns the right information.
 117  
 118          $course = $this->getDataGenerator()->create_course();
 119          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
 120  
 121          $params = array(
 122              'context' => \context_module::instance($survey->cmid),
 123              'courseid' => $course->id,
 124              'other' => array('surveyid' => $survey->id)
 125          );
 126          $event = \mod_survey\event\response_submitted::create($params);
 127  
 128          // Triggering and capturing the event.
 129          $sink = $this->redirectEvents();
 130          $event->trigger();
 131          $events = $sink->get_events();
 132          $this->assertCount(1, $events);
 133          $event = reset($events);
 134  
 135          // Checking that the event contains the expected values.
 136          $this->assertInstanceOf('\mod_survey\event\response_submitted', $event);
 137          $this->assertEquals(\context_module::instance($survey->cmid), $event->get_context());
 138          $this->assertEquals($survey->id, $event->other['surveyid']);
 139          $expected = array($course->id, "survey", "submit", 'view.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
 140          $this->assertEventLegacyLogData($expected, $event);
 141          $this->assertEventContextNotUsed($event);
 142      }
 143  }