Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 defined('MOODLE_INTERNAL') || die(); 18 19 global $CFG; 20 21 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 22 require_once($CFG->dirroot . '/enrol/externallib.php'); 23 24 /** 25 * Role external PHPunit tests 26 * 27 * @package core_enrol 28 * @category external 29 * @copyright 2012 Jerome Mouneyrac 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 * @since Moodle 2.4 32 */ 33 class core_enrol_role_external_testcase extends externallib_advanced_testcase { 34 35 /** 36 * Tests set up 37 */ 38 protected function setUp(): void { 39 global $CFG; 40 require_once($CFG->dirroot . '/enrol/externallib.php'); 41 } 42 43 /** 44 * Test assign_roles 45 */ 46 public function test_assign_roles() { 47 global $USER; 48 49 $this->resetAfterTest(true); 50 51 $course = self::getDataGenerator()->create_course(); 52 53 // Set the required capabilities by the external function. 54 $context = context_course::instance($course->id); 55 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id); 56 $this->assignUserCapability('moodle/course:view', $context->id, $roleid); 57 58 // Add manager role to $USER. 59 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'. 60 role_assign(1, $USER->id, context_system::instance()->id); 61 62 // Check the teacher role has not been assigned to $USER. 63 $users = get_role_users(3, $context); 64 $this->assertEquals(count($users), 0); 65 66 // Call the external function. Assign teacher role to $USER with contextid. 67 core_role_external::assign_roles(array( 68 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id))); 69 70 // Check the role has been assigned. 71 $users = get_role_users(3, $context); 72 $this->assertEquals(count($users), 1); 73 74 // Unassign role. 75 role_unassign(3, $USER->id, $context->id); 76 $users = get_role_users(3, $context); 77 $this->assertEquals(count($users), 0); 78 79 // Call the external function. Assign teacher role to $USER. 80 core_role_external::assign_roles(array( 81 array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id))); 82 $users = get_role_users(3, $context); 83 $this->assertEquals(count($users), 1); 84 85 // Call without required capability. 86 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid); 87 $this->expectException('moodle_exception'); 88 $categories = core_role_external::assign_roles( 89 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)); 90 } 91 92 /** 93 * Test unassign_roles 94 */ 95 public function test_unassign_roles() { 96 global $USER; 97 98 $this->resetAfterTest(true); 99 100 $course = self::getDataGenerator()->create_course(); 101 102 // Set the required capabilities by the external function. 103 $context = context_course::instance($course->id); 104 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id); 105 $this->assignUserCapability('moodle/course:view', $context->id, $roleid); 106 107 // Add manager role to $USER. 108 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'. 109 role_assign(1, $USER->id, context_system::instance()->id); 110 111 // Add teacher role to $USER on course context. 112 role_assign(3, $USER->id, $context->id); 113 114 // Check the teacher role has been assigned to $USER on course context. 115 $users = get_role_users(3, $context); 116 $this->assertEquals(count($users), 1); 117 118 // Call the external function. Unassign teacher role using contextid. 119 core_role_external::unassign_roles(array( 120 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id))); 121 122 // Check the role has been unassigned on course context. 123 $users = get_role_users(3, $context); 124 $this->assertEquals(count($users), 0); 125 126 // Add teacher role to $USER on course context. 127 role_assign(3, $USER->id, $context->id); 128 $users = get_role_users(3, $context); 129 $this->assertEquals(count($users), 1); 130 131 // Call the external function. Unassign teacher role using context level and instanceid. 132 core_role_external::unassign_roles(array( 133 array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id))); 134 135 // Check the role has been unassigned on course context. 136 $users = get_role_users(3, $context); 137 $this->assertEquals(count($users), 0); 138 139 // Call without required capability. 140 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid); 141 $this->expectException('moodle_exception'); 142 $categories = core_role_external::unassign_roles( 143 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)); 144 } 145 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body