[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Unassign a capability to $USER. 69 * 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'. 75 */ 76 public static function unassignUserCapability($capability, $contextid = null, $roleid = null, $courseid = null, $enrol = 'manual') { 77 global $DB; 78 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); 84 } 85 $instance = reset($instances); 86 87 if (is_null($roleid) and $instance->roleid) { 88 $roleid = $instance->roleid; 89 } 90 } else { 91 if (empty($contextid) or empty($roleid)) { 92 throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid'); 93 } 94 } 95 96 unassign_capability($capability, $roleid, $contextid); 97 98 accesslib_clear_all_caches_for_unit_testing(); 99 } 100 } 101
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |