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 310 and 403] [Versions 311 and 403] [Versions 39 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 get_h5pactivity_access_information.
  19   *
  20   * @package    mod_h5pactivity
  21   * @category   external
  22   * @since      Moodle 3.9
  23   * @copyright  2020 Carlos Escobedo <carlos@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 dml_missing_record_exception;
  35  use core_external\external_api;
  36  use externallib_advanced_testcase;
  37  
  38  /**
  39   * External function test for get_h5pactivity_access_information.
  40   *
  41   * @package    mod_h5pactivity
  42   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class get_h5pactivity_access_information_test extends externallib_advanced_testcase {
  46  
  47      /**
  48       * Test the behaviour of get_h5pactivity_access_information().
  49       *
  50       * @dataProvider get_h5pactivity_access_information_data
  51       * @param string $role user role in course
  52       * @param int $enabletracking if tracking is enabled
  53       * @param array $enabledcaps capabilities enabled
  54       */
  55      public function test_get_h5pactivity_access_information(string $role, int $enabletracking, array $enabledcaps) {
  56          $this->resetAfterTest();
  57          $this->setAdminUser();
  58  
  59          $course = $this->getDataGenerator()->create_course();
  60          $activity = $this->getDataGenerator()->create_module('h5pactivity',
  61              [
  62                  'course' => $course,
  63                  'enabletracking' => $enabletracking
  64              ]
  65          );
  66  
  67          if ($role) {
  68              $user = $this->getDataGenerator()->create_and_enrol($course, $role);
  69              $this->setUser($user);
  70          }
  71  
  72          // Check the access information.
  73          $result = get_h5pactivity_access_information::execute($activity->id);
  74          $result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
  75          $this->assertCount(0, $result['warnings']);
  76          unset($result['warnings']);
  77  
  78          // Check the values for capabilities.
  79          foreach ($result as $capname => $capvalue) {
  80              if (in_array($capname, $enabledcaps)) {
  81                  $this->assertTrue($capvalue);
  82              } else {
  83                  $this->assertFalse($capvalue);
  84              }
  85          }
  86      }
  87  
  88      /**
  89       * Data provider for get_h5pactivity_access_information.
  90       *
  91       * @return array
  92       */
  93      public function get_h5pactivity_access_information_data(): array {
  94          return [
  95              'Admin, tracking enabled' => [
  96                  '', 1, ['canview', 'canreviewattempts', 'canaddinstance']
  97              ],
  98              'Admin, tracking disabled' => [
  99                  '', 0, ['canview', 'canreviewattempts', 'canaddinstance']
 100              ],
 101              'Student, tracking enabled' => [
 102                  'student', 1, ['canview', 'cansubmit']
 103              ],
 104              'Student, tracking disabled' => [
 105                  'student', 0, ['canview']
 106              ],
 107              'Teacher, tracking enabled' => [
 108                  'editingteacher', 1, [
 109                      'canview',
 110                      'canreviewattempts',
 111                      'canaddinstance'
 112                  ]
 113              ],
 114              'Teacher, tracking disabled' => [
 115                  'editingteacher', 0, [
 116                      'canview',
 117                      'canreviewattempts',
 118                      'canaddinstance'
 119                  ]
 120              ],
 121          ];
 122      }
 123  
 124      /**
 125       * Test dml_missing_record_exception in get_h5pactivity_access_information.
 126       */
 127      public function test_dml_missing_record_exception() {
 128          $this->resetAfterTest();
 129          $this->setAdminUser();
 130  
 131          $course = $this->getDataGenerator()->create_course();
 132  
 133          // Call the WS using an unexisting h5pactivityid.
 134          $this->expectException(dml_missing_record_exception::class);
 135          $result = get_h5pactivity_access_information::execute(1);
 136      }
 137  }