2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains helper classes for testing the web service and external files.
20 * @package core_webservice
21 * @copyright 2012 Jerome Mouneyrac
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Helper base class for external tests. Helpfull to test capabilities.
30 * @package core_webservice
31 * @copyright 2012 Jerome Mouneyrac
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 abstract class externallib_advanced_testcase
extends advanced_testcase
{
37 * Assign a capability to $USER
38 * The function creates a student $USER if $USER->id is empty
40 * @param string $capability capability name
41 * @param int $contextid
43 * @return int the role id - mainly returned for creation, so calling function can reuse it
45 public static function assignUserCapability($capability, $contextid, $roleid = null) {
48 // Create a new student $USER if $USER doesn't exist
49 if (empty($USER->id
)) {
50 $user = self
::getDataGenerator()->create_user();
55 $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
58 assign_capability($capability, CAP_ALLOW
, $roleid, $contextid);
60 role_assign($roleid, $USER->id
, $contextid);
62 accesslib_clear_all_caches_for_unit_testing();
68 * Configure some filters for external tests.
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.
76 public static function configure_filters($filters) {
79 $filterstrings = false;
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']);
87 $filterstrings = $filterstrings ||
$filter['applytostrings'];
91 $wssettings = external_settings
::get_instance();
92 $wssettings->set_filter(true);
94 // Reset filter caches.
95 $filtermanager = filter_manager
::instance();
96 $filtermanager->reset_caches();
99 // Don't strip tags in strings.
100 $CFG->formatstringstriptags
= false;
105 * Unassign a capability to $USER.
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'.
113 public static function unassignUserCapability($capability, $contextid = null, $roleid = null, $courseid = null, $enrol = 'manual') {
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);
122 $instance = reset($instances);
124 if (is_null($roleid) and $instance->roleid
) {
125 $roleid = $instance->roleid
;
128 if (empty($contextid) or empty($roleid)) {
129 throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid');
133 unassign_capability($capability, $roleid, $contextid);
135 accesslib_clear_all_caches_for_unit_testing();