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  use core_external\external_settings;
  18  
  19  /**
  20   * Helper base class for external tests. Helpfull to test capabilities.
  21   *
  22   * @package    core_webservice
  23   * @copyright  2012 Jerome Mouneyrac
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  abstract class externallib_advanced_testcase extends advanced_testcase {
  27  
  28      /**
  29       * Assign a capability to $USER
  30       * The function creates a student $USER if $USER->id is empty
  31       *
  32       * @param string $capability capability name
  33       * @param int|context $contextid
  34       * @param int $roleid
  35       * @return int the role id - mainly returned for creation, so calling function can reuse it
  36       */
  37      public static function assignUserCapability($capability, $contextid, $roleid = null) {
  38          global $USER;
  39  
  40          // Create a new student $USER if $USER doesn't exist
  41          if (empty($USER->id)) {
  42              $user  = self::getDataGenerator()->create_user();
  43              self::setUser($user);
  44          }
  45  
  46          if (empty($roleid)) {
  47              $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  48          }
  49  
  50          assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
  51  
  52          role_assign($roleid, $USER->id, $contextid);
  53  
  54          accesslib_clear_all_caches_for_unit_testing();
  55  
  56          return $roleid;
  57      }
  58  
  59      /**
  60       * Configure some filters for external tests.
  61       *
  62       * @param array $filters Filters to enable. Each filter should contain:
  63       *                           - name: name of the filter.
  64       *                           - state: the state of the filter.
  65       *                           - move: -1 means up, 0 means the same, 1 means down.
  66       *                           - applytostrings: true to apply the filter to content and headings, false for just content.
  67       */
  68      public static function configure_filters($filters) {
  69          global $CFG;
  70  
  71          $filterstrings = false;
  72  
  73          // Enable the filters.
  74          foreach ($filters as $filter) {
  75              $filter = (array) $filter;
  76              filter_set_global_state($filter['name'], $filter['state'], $filter['move']);
  77              filter_set_applies_to_strings($filter['name'], $filter['applytostrings']);
  78  
  79              $filterstrings = $filterstrings || $filter['applytostrings'];
  80          }
  81  
  82          // Set WS filtering.
  83          $wssettings = external_settings::get_instance();
  84          $wssettings->set_filter(true);
  85  
  86          // Reset filter caches.
  87          $filtermanager = filter_manager::instance();
  88          $filtermanager->reset_caches();
  89  
  90          if ($filterstrings) {
  91              // Don't strip tags in strings.
  92              $CFG->formatstringstriptags = false;
  93          }
  94      }
  95  
  96      /**
  97       * Unassign a capability to $USER.
  98       *
  99       * @param string $capability capability name.
 100       * @param int $contextid set the context id if you used assignUserCapability.
 101       * @param int $roleid set the role id if you used assignUserCapability.
 102       * @param int $courseid set the course id if you used getDataGenerator->enrol_users.
 103       * @param string $enrol set the enrol plugin name if you used getDataGenerator->enrol_users with a different plugin than 'manual'.
 104       */
 105      public static function unassignUserCapability($capability, $contextid = null, $roleid = null, $courseid = null, $enrol = 'manual') {
 106          global $DB;
 107  
 108          if (!empty($courseid)) {
 109              // Retrieve the role id.
 110              $instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
 111              if (count($instances) != 1) {
 112                   throw new coding_exception('No found enrol instance for courseid: ' . $courseid . ' and enrol: ' . $enrol);
 113              }
 114              $instance = reset($instances);
 115  
 116              if (is_null($roleid) and $instance->roleid) {
 117                  $roleid = $instance->roleid;
 118              }
 119          } else {
 120              if (empty($contextid) or empty($roleid)) {
 121                  throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid');
 122              }
 123          }
 124  
 125          unassign_capability($capability, $roleid, $contextid);
 126  
 127          accesslib_clear_all_caches_for_unit_testing();
 128      }
 129  }