Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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   * External function test for log_report_viewed.
  19   *
  20   * @package    mod_h5pactivity
  21   * @category   external
  22   * @since      Moodle 3.11
  23   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace mod_h5pactivity\external;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  33  
  34  use mod_h5pactivity\local\manager;
  35  use core_external\external_api;
  36  use externallib_advanced_testcase;
  37  
  38  /**
  39   * External function test for log_report_viewed.
  40   *
  41   * @package    mod_h5pactivity
  42   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class log_report_viewed_testcase extends externallib_advanced_testcase {
  46      /**
  47       * Test the behaviour of log_report_viewed.
  48       *
  49       * @dataProvider execute_data
  50       * @param int $enabletracking the activity tracking enable
  51       * @param int $reviewmode the activity review mode
  52       * @param string $loginuser the user which calls the webservice
  53       * @param string|null $participant the user to log the data
  54       */
  55      public function test_execute(int $enabletracking, int $reviewmode, string $loginuser, ?string $participant): void {
  56          $this->resetAfterTest();
  57          $this->setAdminUser();
  58  
  59          // Create a course.
  60          $course = $this->getDataGenerator()->create_course();
  61  
  62          // Enrol users: 1 teacher, 1 student.
  63          $users = [
  64              'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'),
  65              'student' => $this->getDataGenerator()->create_and_enrol($course, 'student'),
  66          ];
  67  
  68          // Add h5p activity.
  69          $activity = $this->getDataGenerator()->create_module('h5pactivity',
  70              ['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]);
  71  
  72          // Create attempt for h5p activity.
  73          $attempts = [];
  74          $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
  75          $user = $users['student'];
  76          $manager = manager::create_from_instance($activity);
  77          $cm = $manager->get_coursemodule();
  78          $params = ['cmid' => $cm->id, 'userid' => $user->id];
  79          $attempts['student'] = $generator->create_content($activity, $params);
  80  
  81          // Redirect events to the sink, so we can recover them later.
  82          $sink = $this->redirectEvents();
  83  
  84          // Execute external method.
  85          $this->setUser($users[$loginuser]);
  86          $attemptid = $attempts[$participant]->id ?? 0;
  87          $result = log_report_viewed::execute($activity->id, $user->id, $attemptid);
  88          $result = external_api::clean_returnvalue(
  89              log_report_viewed::execute_returns(),
  90              $result
  91          );
  92  
  93          // Validate general structure.
  94          $this->assertArrayHasKey('status', $result);
  95  
  96          $events = $sink->get_events();
  97          $event = end($events);
  98  
  99          // Check the event details are correct.
 100          $this->assertInstanceOf('mod_h5pactivity\event\report_viewed', $event);
 101          $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
 102  
 103          $this->assertEquals($cm->instance, $event->other['instanceid']);
 104          $this->assertEquals($user->id, $event->other['userid']);
 105          $this->assertEquals($attemptid, $event->other['attemptid']);
 106      }
 107  
 108      /**
 109       * Data provider for the test_execute tests.
 110       *
 111       * @return  array
 112       */
 113      public function execute_data(): array {
 114          return [
 115              'Student reviewing own attempt' => [
 116                  1, manager::REVIEWCOMPLETION, 'student', 'student'
 117              ],
 118              'Teacher reviewing student attempts' => [
 119                  1, manager::REVIEWCOMPLETION, 'editingteacher', 'student'
 120              ],
 121          ];
 122      }
 123  }