Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 the capability overview helper functions.
  19   *
  20   * @package   tool_capability
  21   * @copyright 2021 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  23   */
  24  namespace tool_capability;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  global $CFG;
  28  require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/capability/locallib.php');
  29  
  30  
  31  /**
  32   * Tests for the capability overview helper functions.
  33   */
  34  class locallib_test extends \advanced_testcase {
  35  
  36      /**
  37       * Test the function that gets the data - simple case.
  38       */
  39      public function test_tool_capability_calculate_role_data() {
  40          global $DB;
  41  
  42          $data = tool_capability_calculate_role_data('mod/quiz:attempt', get_all_roles());
  43  
  44          $systcontext = \context_system::instance();
  45          $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
  46  
  47          $this->assertArrayHasKey($systcontext->id, $data);
  48          $this->assertCount(1, $data);
  49          foreach ($data[$systcontext->id]->rolecapabilities as $roleid => $permission) {
  50              if ($roleid == $studentroleid) {
  51                  $this->assertEquals(CAP_ALLOW, $permission);
  52              } else {
  53                  $this->assertEquals(CAP_INHERIT, $permission);
  54              }
  55          }
  56      }
  57  
  58      /**
  59       * Test the function that gets the data - simple case.
  60       */
  61      public function test_tool_capability_calculate_role_data_orphan_contexts() {
  62          global $DB;
  63          $this->resetAfterTest();
  64  
  65          // This simulates a situation that seems to happen sometimes, where
  66          // we end up with contexts with path = NULL in the database.
  67          $systcontext = \context_system::instance();
  68          $generator = $this->getDataGenerator();
  69          $course = $generator->create_course();
  70          $coursecontext = \context_course::instance($course->id);
  71          $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
  72          role_change_permission($studentroleid, $coursecontext, 'mod/quiz:attempt', CAP_PREVENT);
  73          // This is where we simulate the breakage.
  74          $DB->set_field('context', 'path', null, ['id' => $coursecontext->id]);
  75  
  76          // Now call the function. We mainly just want to know there is no exception.
  77          $data = tool_capability_calculate_role_data('mod/quiz:attempt', get_all_roles());
  78  
  79          $this->assertArrayHasKey($systcontext->id, $data);
  80          $this->assertCount(1, $data);
  81          foreach ($data[$systcontext->id]->rolecapabilities as $roleid => $permission) {
  82              if ($roleid == $studentroleid) {
  83                  $this->assertEquals(CAP_ALLOW, $permission);
  84              } else {
  85                  $this->assertEquals(CAP_INHERIT, $permission);
  86              }
  87          }
  88      }
  89  }