See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 * API tests. 19 * 20 * @package tool_cohortroles 21 * @copyright 2015 Damyon Wiese 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use tool_cohortroles\api; 28 29 /** 30 * API tests. 31 * 32 * @package tool_cohortroles 33 * @copyright 2015 Damyon Wiese 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class tool_cohortroles_api_testcase extends advanced_testcase { 37 /** @var stdClass $cohort */ 38 protected $cohort = null; 39 40 /** @var stdClass $userassignto */ 41 protected $userassignto = null; 42 43 /** @var stdClass $userassignover */ 44 protected $userassignover = null; 45 46 /** @var stdClass $role */ 47 protected $role = null; 48 49 /** 50 * Setup function- we will create a course and add an assign instance to it. 51 */ 52 protected function setUp() { 53 $this->resetAfterTest(true); 54 55 // Create some users. 56 $this->cohort = $this->getDataGenerator()->create_cohort(); 57 $this->userassignto = $this->getDataGenerator()->create_user(); 58 $this->userassignover = $this->getDataGenerator()->create_user(); 59 $this->roleid = create_role('Sausage Roll', 'sausageroll', 'mmmm'); 60 cohort_add_member($this->cohort->id, $this->userassignover->id); 61 } 62 63 /** 64 * @expectedException required_capability_exception 65 */ 66 public function test_create_cohort_role_assignment_without_permission() { 67 $this->setUser($this->userassignto); 68 $params = (object) array( 69 'userid' => $this->userassignto->id, 70 'roleid' => $this->roleid, 71 'cohortid' => $this->cohort->id 72 ); 73 api::create_cohort_role_assignment($params); 74 } 75 76 /** 77 * @expectedException core_competency\invalid_persistent_exception 78 */ 79 public function test_create_cohort_role_assignment_with_invalid_data() { 80 $this->setAdminUser(); 81 $params = (object) array( 82 'userid' => $this->userassignto->id, 83 'roleid' => -8, 84 'cohortid' => $this->cohort->id 85 ); 86 api::create_cohort_role_assignment($params); 87 } 88 89 public function test_create_cohort_role_assignment() { 90 $this->setAdminUser(); 91 $params = (object) array( 92 'userid' => $this->userassignto->id, 93 'roleid' => $this->roleid, 94 'cohortid' => $this->cohort->id 95 ); 96 $result = api::create_cohort_role_assignment($params); 97 $this->assertNotEmpty($result->get('id')); 98 $this->assertEquals($result->get('userid'), $this->userassignto->id); 99 $this->assertEquals($result->get('roleid'), $this->roleid); 100 $this->assertEquals($result->get('cohortid'), $this->cohort->id); 101 } 102 103 /** 104 * @expectedException required_capability_exception 105 */ 106 public function test_delete_cohort_role_assignment_without_permission() { 107 $this->setAdminUser(); 108 $params = (object) array( 109 'userid' => $this->userassignto->id, 110 'roleid' => $this->roleid, 111 'cohortid' => $this->cohort->id 112 ); 113 $result = api::create_cohort_role_assignment($params); 114 $this->setUser($this->userassignto); 115 api::delete_cohort_role_assignment($result->get('id')); 116 } 117 118 /** 119 * @expectedException dml_missing_record_exception 120 */ 121 public function test_delete_cohort_role_assignment_with_invalid_data() { 122 $this->setAdminUser(); 123 $params = (object) array( 124 'userid' => $this->userassignto->id, 125 'roleid' => $this->roleid, 126 'cohortid' => $this->cohort->id 127 ); 128 $result = api::create_cohort_role_assignment($params); 129 api::delete_cohort_role_assignment($result->get('id') + 1); 130 } 131 132 public function test_delete_cohort_role_assignment() { 133 $this->setAdminUser(); 134 // Create a cohort role assigment. 135 $params = (object) [ 136 'userid' => $this->userassignto->id, 137 'roleid' => $this->roleid, 138 'cohortid' => $this->cohort->id 139 ]; 140 $cohortroleassignment = api::create_cohort_role_assignment($params); 141 $sync = api::sync_all_cohort_roles(); 142 $rolesadded = [ 143 [ 144 'useridassignedto' => $this->userassignto->id, 145 'useridassignedover' => $this->userassignover->id, 146 'roleid' => $this->roleid 147 ] 148 ]; 149 $expected = [ 150 'rolesadded' => $rolesadded, 151 'rolesremoved' => [] 152 ]; 153 $this->assertEquals($sync, $expected); 154 155 // Delete the cohort role assigment and confirm the roles are removed. 156 $result = api::delete_cohort_role_assignment($cohortroleassignment->get('id')); 157 $this->assertTrue($result); 158 $sync = api::sync_all_cohort_roles(); 159 $expected = [ 160 'rolesadded' => [], 161 'rolesremoved' => $rolesadded 162 ]; 163 $this->assertEquals($expected, $sync); 164 } 165 166 /** 167 * Test case verifying that syncing won't remove role assignments if they are valid for another cohort role assignment. 168 */ 169 public function test_delete_cohort_role_assignment_cohorts_having_same_members() { 170 $this->setAdminUser(); 171 172 // Create 2 cohorts, with a 1 user (user1) present in both, 173 // and user2 and user3 members of 1 cohort each. 174 $cohort1 = $this->getDataGenerator()->create_cohort(); 175 $cohort2 = $this->getDataGenerator()->create_cohort(); 176 $user1 = $this->getDataGenerator()->create_user(); 177 $user2 = $this->getDataGenerator()->create_user(); 178 $user3 = $this->getDataGenerator()->create_user(); 179 cohort_add_member($cohort1->id, $user1->id); 180 cohort_add_member($cohort1->id, $user2->id); 181 cohort_add_member($cohort2->id, $user1->id); 182 cohort_add_member($cohort2->id, $user3->id); 183 184 // And a role and a user to assign that role to. 185 $user4 = $this->getDataGenerator()->create_user(); // A cohort manager, for example. 186 $roleid = create_role('Role 1', 'myrole', 'test'); 187 188 // Assign the role for user4 in both cohorts. 189 $params = (object) [ 190 'userid' => $user4->id, 191 'roleid' => $roleid, 192 'cohortid' => $cohort1->id 193 ]; 194 $cohort1roleassignment = api::create_cohort_role_assignment($params); 195 $params->cohortid = $cohort2->id; 196 $cohort2roleassignment = api::create_cohort_role_assignment($params); 197 198 $sync = api::sync_all_cohort_roles(); 199 200 // There is no guarantee about the order of roles assigned. 201 // so confirm we have 3 role assignments, and they are for the users 1, 2 and 3. 202 $this->assertCount(3, $sync['rolesadded']); 203 $addedusers = array_column($sync['rolesadded'], 'useridassignedover'); 204 $this->assertContains($user1->id, $addedusers); 205 $this->assertContains($user2->id, $addedusers); 206 $this->assertContains($user3->id, $addedusers); 207 208 // Remove the role assignment for user4/cohort1. 209 // Verify only 1 role is unassigned as the others are still valid for the other cohort role assignment. 210 $result = api::delete_cohort_role_assignment($cohort1roleassignment->get('id')); 211 $this->assertTrue($result); 212 213 $sync = api::sync_all_cohort_roles(); 214 215 $this->assertCount(0, $sync['rolesadded']); 216 $this->assertCount(1, $sync['rolesremoved']); 217 $removedusers = array_column($sync['rolesremoved'], 'useridassignedover'); 218 $this->assertContains($user2->id, $removedusers); 219 } 220 221 public function test_list_cohort_role_assignments() { 222 $this->setAdminUser(); 223 $params = (object) array( 224 'userid' => $this->userassignto->id, 225 'roleid' => $this->roleid, 226 'cohortid' => $this->cohort->id 227 ); 228 $result = api::create_cohort_role_assignment($params); 229 230 $list = api::list_cohort_role_assignments(); 231 $list[0]->is_valid(); 232 $this->assertEquals($list[0], $result); 233 } 234 235 public function test_count_cohort_role_assignments() { 236 $this->setAdminUser(); 237 $params = (object) array( 238 'userid' => $this->userassignto->id, 239 'roleid' => $this->roleid, 240 'cohortid' => $this->cohort->id 241 ); 242 $result = api::create_cohort_role_assignment($params); 243 244 $count = api::count_cohort_role_assignments(); 245 $this->assertEquals($count, 1); 246 } 247 248 public function test_sync_all_cohort_roles() { 249 $this->setAdminUser(); 250 $params = (object) array( 251 'userid' => $this->userassignto->id, 252 'roleid' => $this->roleid, 253 'cohortid' => $this->cohort->id 254 ); 255 $result = api::create_cohort_role_assignment($params); 256 257 // Verify roles are assigned when users enter the cohort. 258 $sync = api::sync_all_cohort_roles(); 259 260 $rolesadded = array(array( 261 'useridassignedto' => $this->userassignto->id, 262 'useridassignedover' => $this->userassignover->id, 263 'roleid' => $this->roleid 264 )); 265 $rolesremoved = array(); 266 $expected = array('rolesadded' => $rolesadded, 267 'rolesremoved' => $rolesremoved); 268 $this->assertEquals($sync, $expected); 269 270 // Verify roles are removed when users leave the cohort. 271 cohort_remove_member($this->cohort->id, $this->userassignover->id); 272 $sync = api::sync_all_cohort_roles(); 273 274 $rolesadded = array(); 275 $rolesremoved = array(array( 276 'useridassignedto' => $this->userassignto->id, 277 'useridassignedover' => $this->userassignover->id, 278 'roleid' => $this->roleid 279 )); 280 $expected = array('rolesadded' => $rolesadded, 281 'rolesremoved' => $rolesremoved); 282 $this->assertEquals($sync, $expected); 283 284 // Verify roles assigned by any other component are not removed. 285 $usercontext = context_user::instance($this->userassignover->id); 286 role_assign($this->roleid, $this->userassignto->id, $usercontext->id); 287 $sync = api::sync_all_cohort_roles(); 288 289 $rolesadded = array(); 290 $rolesremoved = array(); 291 $expected = array('rolesadded' => $rolesadded, 292 'rolesremoved' => $rolesremoved); 293 $this->assertEquals($sync, $expected); 294 295 // Remove manual role assignment. 296 role_unassign($this->roleid, $this->userassignto->id, $usercontext->id); 297 // Add someone to the cohort again... 298 cohort_add_member($this->cohort->id, $this->userassignover->id); 299 $sync = api::sync_all_cohort_roles(); 300 $rolesadded = array(array( 301 'useridassignedto' => $this->userassignto->id, 302 'useridassignedover' => $this->userassignover->id, 303 'roleid' => $this->roleid 304 )); 305 $rolesremoved = array(); 306 $expected = array('rolesadded' => $rolesadded, 307 'rolesremoved' => $rolesremoved); 308 $this->assertEquals($sync, $expected); 309 310 // Verify no fatal errors when a cohort is deleted. 311 cohort_delete_cohort($this->cohort); 312 $sync = api::sync_all_cohort_roles(); 313 314 $rolesadded = array(); 315 $rolesremoved = array(array( 316 'useridassignedto' => $this->userassignto->id, 317 'useridassignedover' => $this->userassignover->id, 318 'roleid' => $this->roleid 319 )); 320 $expected = array('rolesadded' => $rolesadded, 321 'rolesremoved' => $rolesremoved); 322 $this->assertEquals($sync, $expected); 323 } 324 325 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body