Merge branch 'MDL-49143-28' of git://github.com/cameron1729/moodle into MOODLE_28_STABLE
[moodle.git] / webservice / tests / helpers.php
blob4d0287111e17cc126d47e74908bd58ab0440d754
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.
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/>.
17 /**
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();
27 /**
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 {
36 /**
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
42 * @param int $roleid
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) {
46 global $USER;
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);
54 if (empty($roleid)) {
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();
64 return $roleid;
67 /**
68 * Unassign a capability to $USER.
70 * @param string $capability capability name.
71 * @param int $contextid set the context id if you used assignUserCapability.
72 * @param int $roleid set the role id if you used assignUserCapability.
73 * @param int $courseid set the course id if you used getDataGenerator->enrol_users.
74 * @param string $enrol set the enrol plugin name if you used getDataGenerator->enrol_users with a different plugin than 'manual'.
76 public static function unassignUserCapability($capability, $contextid = null, $roleid = null, $courseid = null, $enrol = 'manual') {
77 global $DB;
79 if (!empty($courseid)) {
80 // Retrieve the role id.
81 $instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
82 if (count($instances) != 1) {
83 throw new coding_exception('No found enrol instance for courseid: ' . $courseid . ' and enrol: ' . $enrol);
85 $instance = reset($instances);
87 if (is_null($roleid) and $instance->roleid) {
88 $roleid = $instance->roleid;
90 } else {
91 if (empty($contextid) or empty($roleid)) {
92 throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid');
96 unassign_capability($capability, $roleid, $contextid);
98 accesslib_clear_all_caches_for_unit_testing();