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