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   * Tests for h5p viewed event.
  19   *
  20   * @package    core_h5p
  21   * @category   test
  22   * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.8
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_h5p\local\library\autoloader;
  30  
  31  /**
  32   * Tests for h5p viewed event.
  33   *
  34   * @package    core_h5p
  35   * @category   test
  36   * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @since      Moodle 3.8
  39   */
  40  class core_h5p_event_h5p_viewed_testcase extends advanced_testcase {
  41  
  42      /**
  43       * Setup test.
  44       */
  45      protected function setUp(): void {
  46          parent::setUp();
  47          autoloader::register();
  48      }
  49  
  50      /**
  51       * test_event_h5p_viewed description
  52       * @runInSeparateProcess
  53       */
  54      public function test_event_h5p_viewed() {
  55          $this->resetAfterTest(true);
  56  
  57          $user = $this->getDataGenerator()->create_user();
  58          $course = $this->getDataGenerator()->create_course();
  59          $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  60          $pagecontext = \context_module::instance($page->cmid);
  61          // Url dummy just for testing proposal.
  62          $url  = \moodle_url::make_pluginfile_url(
  63              $pagecontext->id,
  64              \core_h5p\file_storage::COMPONENT,
  65              'unittest',
  66              0,
  67              '/',
  68              'h5pfiletest.h5p'
  69          );
  70          // Dummy H5P id for testing proposal. We don't need a real h5p.
  71          $dummyh5pid = 111;
  72          $now = time();
  73          // Event parameters for testing.
  74          $params = [
  75              'objectid' => $dummyh5pid,
  76              'userid' => $user->id,
  77              'context' => $pagecontext,
  78              'other' => [
  79                  'url' => $url->out(),
  80                  'time' => $now
  81              ]
  82          ];
  83          // Prepare redirect Events.
  84          $sink = $this->redirectEvents();
  85          // Test the event H5P viewed.
  86          $event = \core_h5p\event\h5p_viewed::create($params);
  87          $event->trigger();
  88          $result = $sink->get_events();
  89          $event = reset($result);
  90          $sink->close();
  91          // Check the event info.
  92          $this->assertEquals($dummyh5pid, $event->objectid);
  93          $this->assertEquals($user->id, $event->userid);
  94          $this->assertEquals($pagecontext->id, $event->contextid);
  95          $this->assertEquals($url->out(), $event->other['url']);
  96          $this->assertEquals($now, $event->other['time']);
  97      }
  98  }