Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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_h5pactivity
  21   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_h5pactivity\event;
  26  
  27  use advanced_testcase;
  28  use moodle_url;
  29  use coding_exception;
  30  use context_module;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * H5P activity events test cases.
  36   *
  37   * @package    mod_h5pactivity
  38   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class report_viewed_test extends advanced_testcase {
  42  
  43      /**
  44       * Test report_viewed event.
  45       *
  46       * @dataProvider report_viewed_data
  47       * @param bool $usea if a (instanceid) will be used in the event
  48       * @param bool $useattemptid if attemptid will be used in the event
  49       * @param bool $useuserid if user id will be used in the event
  50       * @param bool $exception if exception is expected
  51       */
  52      public function test_report_viewed(bool $usea, bool $useattemptid, bool $useuserid, bool $exception) {
  53  
  54          $this->resetAfterTest();
  55  
  56          // Must be a non-guest user to create h5pactivities.
  57          $this->setAdminUser();
  58  
  59          $course = $this->getDataGenerator()->create_course();
  60          $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course->id]);
  61  
  62          $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
  63  
  64          // Create a user with 1 attempt.
  65          $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
  66          $params = ['cmid' => $activity->cmid, 'userid' => $user->id];
  67          $attempt = $generator->create_content($activity, $params);
  68  
  69          $other = [];
  70          $urlparams = [];
  71          if ($usea) {
  72              $other['instanceid'] = $activity->id;
  73              $urlparams['a'] = $activity->id;
  74          }
  75          if ($useuserid) {
  76              $other['userid'] = $user->id;
  77              $urlparams['userid'] = $user->id;
  78          }
  79          if ($useattemptid) {
  80              $other['attemptid'] = $attempt->id;
  81              $urlparams['attemptid'] = $attempt->id;
  82          }
  83          $params = [
  84              'context' => context_module::instance($activity->cmid),
  85              'objectid' => $activity->id,
  86              'other' => $other,
  87          ];
  88  
  89          if ($exception) {
  90              $this->expectException(coding_exception::class);
  91          }
  92  
  93          $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_h5pactivity\event\report_viewed', $event);
 104          $this->assertEquals(context_module::instance($activity->cmid), $event->get_context());
 105          $this->assertEquals($activity->id, $event->objectid);
 106  
 107          $eventurl = $event->get_url();
 108          $url = new moodle_url('/mod/h5pactivity/report.php', $urlparams);
 109          $this->assertTrue($eventurl->compare($url));
 110      }
 111  
 112      /**
 113       * Data provider for data request creation tests.
 114       *
 115       * @return array
 116       */
 117      public function report_viewed_data(): array {
 118          return [
 119              // Exception cases.
 120              'Event withour other data (exception)' => [
 121                  false, false, false, true
 122              ],
 123              'Event with only userid (exception)' => [
 124                  false, false, true, true
 125              ],
 126              'Event with only attemptid (exception)' => [
 127                  false, true, false, true
 128              ],
 129              'Event with attemptid and userid (exception)' => [
 130                  false, true, true, true
 131              ],
 132              // Correct cases.
 133              'Event with instance id' => [
 134                  true, false, false, false
 135              ],
 136              'Event with instance id and attempt id' => [
 137                  true, false, true, false
 138              ],
 139              'Event with instance id and userid' => [
 140                  true, true, false, false
 141              ],
 142              'Event with instance id, user id and attemptid' => [
 143                  true, true, true, false
 144              ],
 145          ];
 146      }
 147  }