Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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