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