Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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 namespace tool_lp; 18 19 use core_competency\api; 20 use core_external\external_api; 21 use externallib_advanced_testcase; 22 23 defined('MOODLE_INTERNAL') || die(); 24 25 global $CFG; 26 27 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 28 29 /** 30 * External learning plans webservice API tests. 31 * 32 * @package tool_lp 33 * @copyright 2015 Damyon Wiese 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class externallib_test extends externallib_advanced_testcase { 37 38 /** @var \stdClass $creator User with enough permissions to create insystem context. */ 39 protected $creator = null; 40 41 /** @var \stdClass $catcreator User with enough permissions to create incategory context. */ 42 protected $catcreator = null; 43 44 /** @var \stdClass $category Category */ 45 protected $category = null; 46 47 /** @var \stdClass $category Category */ 48 protected $othercategory = null; 49 50 /** @var \stdClass $user User with enough permissions to view insystem context */ 51 protected $user = null; 52 53 /** @var \stdClass $catuser User with enough permissions to view incategory context */ 54 protected $catuser = null; 55 56 /** @var int Creator role id */ 57 protected $creatorrole = null; 58 59 /** @var int User role id */ 60 protected $userrole = null; 61 62 /** 63 * Setup function- we will create a course and add an assign instance to it. 64 */ 65 protected function setUp(): void { 66 global $DB, $CFG; 67 68 $this->resetAfterTest(true); 69 70 // Create some users. 71 $creator = $this->getDataGenerator()->create_user(); 72 $user = $this->getDataGenerator()->create_user(); 73 $catuser = $this->getDataGenerator()->create_user(); 74 $catcreator = $this->getDataGenerator()->create_user(); 75 $category = $this->getDataGenerator()->create_category(); 76 $othercategory = $this->getDataGenerator()->create_category(); 77 $syscontext = \context_system::instance(); 78 $catcontext = \context_coursecat::instance($category->id); 79 80 // Fetching default authenticated user role. 81 $authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid)); 82 83 // Reset all default authenticated users permissions. 84 unassign_capability('moodle/competency:competencygrade', $authrole->id); 85 unassign_capability('moodle/competency:competencymanage', $authrole->id); 86 unassign_capability('moodle/competency:competencyview', $authrole->id); 87 unassign_capability('moodle/competency:planmanage', $authrole->id); 88 unassign_capability('moodle/competency:planmanagedraft', $authrole->id); 89 unassign_capability('moodle/competency:planmanageown', $authrole->id); 90 unassign_capability('moodle/competency:planview', $authrole->id); 91 unassign_capability('moodle/competency:planviewdraft', $authrole->id); 92 unassign_capability('moodle/competency:planviewown', $authrole->id); 93 unassign_capability('moodle/competency:planviewowndraft', $authrole->id); 94 unassign_capability('moodle/competency:templatemanage', $authrole->id); 95 unassign_capability('moodle/competency:templateview', $authrole->id); 96 unassign_capability('moodle/cohort:manage', $authrole->id); 97 unassign_capability('moodle/competency:coursecompetencyconfigure', $authrole->id); 98 99 // Creating specific roles. 100 $this->creatorrole = create_role('Creator role', 'lpcreatorrole', 'learning plan creator role description'); 101 $this->userrole = create_role('User role', 'lpuserrole', 'learning plan user role description'); 102 103 assign_capability('moodle/competency:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); 104 assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $this->creatorrole, $syscontext->id); 105 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); 106 assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $this->creatorrole, $syscontext->id); 107 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id); 108 assign_capability('moodle/competency:planview', CAP_ALLOW, $this->creatorrole, $syscontext->id); 109 assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $this->creatorrole, $syscontext->id); 110 assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id); 111 assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $this->creatorrole, $syscontext->id); 112 assign_capability('moodle/cohort:manage', CAP_ALLOW, $this->creatorrole, $syscontext->id); 113 114 assign_capability('moodle/competency:competencyview', CAP_ALLOW, $this->userrole, $syscontext->id); 115 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontext->id); 116 assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id); 117 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id); 118 119 role_assign($this->creatorrole, $creator->id, $syscontext->id); 120 role_assign($this->creatorrole, $catcreator->id, $catcontext->id); 121 role_assign($this->userrole, $user->id, $syscontext->id); 122 role_assign($this->userrole, $catuser->id, $catcontext->id); 123 124 $this->creator = $creator; 125 $this->catcreator = $catcreator; 126 $this->user = $user; 127 $this->catuser = $catuser; 128 $this->category = $category; 129 $this->othercategory = $othercategory; 130 131 accesslib_clear_all_caches_for_unit_testing(); 132 } 133 134 public function test_search_users_by_capability() { 135 global $CFG; 136 $this->resetAfterTest(true); 137 138 $dg = $this->getDataGenerator(); 139 $ux = $dg->create_user(); 140 $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan', 141 'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing', 142 'institution' => 'HQ')); 143 144 // First we search with no capability assigned. 145 $this->setUser($ux); 146 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 147 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 148 $this->assertCount(0, $result['users']); 149 $this->assertEquals(0, $result['count']); 150 151 // Now we assign a different capability. 152 $usercontext = \context_user::instance($u1->id); 153 $systemcontext = \context_system::instance(); 154 $customrole = $this->assignUserCapability('moodle/competency:planview', $usercontext->id); 155 156 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 157 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 158 $this->assertCount(0, $result['users']); 159 $this->assertEquals(0, $result['count']); 160 161 // Now we assign a matching capability in the same role. 162 $usercontext = \context_user::instance($u1->id); 163 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $customrole); 164 165 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 166 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 167 $this->assertCount(1, $result['users']); 168 $this->assertEquals(1, $result['count']); 169 170 // Now assign another role with the same capability (test duplicates). 171 role_assign($this->creatorrole, $ux->id, $usercontext->id); 172 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 173 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 174 $this->assertCount(1, $result['users']); 175 $this->assertEquals(1, $result['count']); 176 177 // Now lets try a different user with only the role at system level. 178 $ux2 = $dg->create_user(); 179 role_assign($this->creatorrole, $ux2->id, $systemcontext->id); 180 $this->setUser($ux2); 181 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 182 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 183 $this->assertCount(1, $result['users']); 184 $this->assertEquals(1, $result['count']); 185 186 // Now lets try a different user with only the role at user level. 187 $ux3 = $dg->create_user(); 188 role_assign($this->creatorrole, $ux3->id, $usercontext->id); 189 $this->setUser($ux3); 190 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 191 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 192 $this->assertCount(1, $result['users']); 193 $this->assertEquals(1, $result['count']); 194 195 // Switch back. 196 $this->setUser($ux); 197 198 // Now add a prevent override (will change nothing because we still have an ALLOW). 199 assign_capability('moodle/competency:planmanage', CAP_PREVENT, $customrole, $usercontext->id); 200 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 201 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 202 $this->assertCount(1, $result['users']); 203 $this->assertEquals(1, $result['count']); 204 205 // Now change to a prohibit override (should prevent access). 206 assign_capability('moodle/competency:planmanage', CAP_PROHIBIT, $customrole, $usercontext->id); 207 $result = external::search_users('yyylan', 'moodle/competency:planmanage'); 208 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 209 $this->assertCount(1, $result['users']); 210 $this->assertEquals(1, $result['count']); 211 212 } 213 214 /** 215 * Ensures that overrides, as well as system permissions, are respected. 216 */ 217 public function test_search_users_by_capability_the_comeback() { 218 $this->resetAfterTest(); 219 $dg = $this->getDataGenerator(); 220 221 $master = $dg->create_user(); 222 $manager = $dg->create_user(); 223 $slave1 = $dg->create_user(array('lastname' => 'MOODLER')); 224 $slave2 = $dg->create_user(array('lastname' => 'MOODLER')); 225 $slave3 = $dg->create_user(array('lastname' => 'MOODLER')); 226 227 $syscontext = \context_system::instance(); 228 $slave1context = \context_user::instance($slave1->id); 229 $slave2context = \context_user::instance($slave2->id); 230 $slave3context = \context_user::instance($slave3->id); 231 232 // Creating a role giving the site config. 233 $roleid = $dg->create_role(); 234 assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext->id, true); 235 236 // Create a role override for slave 2. 237 assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $slave2context->id, true); 238 239 // Assigning the role. 240 // Master -> System context. 241 // Manager -> User context. 242 role_assign($roleid, $master->id, $syscontext); 243 role_assign($roleid, $manager->id, $slave1context); 244 245 // Flush accesslib. 246 accesslib_clear_all_caches_for_unit_testing(); 247 248 // Confirm. 249 // Master has system permissions. 250 $this->setUser($master); 251 $this->assertTrue(has_capability('moodle/site:config', $syscontext)); 252 $this->assertTrue(has_capability('moodle/site:config', $slave1context)); 253 $this->assertFalse(has_capability('moodle/site:config', $slave2context)); 254 $this->assertTrue(has_capability('moodle/site:config', $slave3context)); 255 256 // Manager only has permissions in slave 1. 257 $this->setUser($manager); 258 $this->assertFalse(has_capability('moodle/site:config', $syscontext)); 259 $this->assertTrue(has_capability('moodle/site:config', $slave1context)); 260 $this->assertFalse(has_capability('moodle/site:config', $slave2context)); 261 $this->assertFalse(has_capability('moodle/site:config', $slave3context)); 262 263 // Now do the test. 264 $this->setUser($master); 265 $result = external::search_users('MOODLER', 'moodle/site:config'); 266 $this->assertCount(2, $result['users']); 267 $this->assertEquals(2, $result['count']); 268 $this->assertArrayHasKey($slave1->id, $result['users']); 269 $this->assertArrayHasKey($slave3->id, $result['users']); 270 271 $this->setUser($manager); 272 $result = external::search_users('MOODLER', 'moodle/site:config'); 273 $this->assertCount(1, $result['users']); 274 $this->assertEquals(1, $result['count']); 275 $this->assertArrayHasKey($slave1->id, $result['users']); 276 } 277 278 public function test_search_users() { 279 global $CFG; 280 $this->resetAfterTest(true); 281 282 $dg = $this->getDataGenerator(); 283 $ux = $dg->create_user(); 284 $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan', 285 'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing', 286 'institution' => 'HQ')); 287 $u2 = $dg->create_user(array('idnumber' => 'Dogs', 'firstname' => 'Alice', 'lastname' => 'Dyyylan', 288 'email' => 'alyyyson@dyyylan.com', 'phone1' => '33333', 'phone2' => '77777', 'department' => 'Development', 289 'institution' => 'O2')); 290 $u3 = $dg->create_user(array('idnumber' => 'Fish', 'firstname' => 'Thomas', 'lastname' => 'Xow', 291 'email' => 'fishyyy@moodle.com', 'phone1' => '77777', 'phone2' => '33333', 'department' => 'Research', 292 'institution' => 'Bob')); 293 294 // We need to give the user the capability we are searching for on each of the test users. 295 $this->setAdminUser(); 296 $usercontext = \context_user::instance($u1->id); 297 $dummyrole = $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id); 298 $usercontext = \context_user::instance($u2->id); 299 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); 300 $usercontext = \context_user::instance($u3->id); 301 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); 302 303 $this->setUser($ux); 304 $usercontext = \context_user::instance($u1->id); 305 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); 306 $usercontext = \context_user::instance($u2->id); 307 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); 308 $usercontext = \context_user::instance($u3->id); 309 $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole); 310 311 $this->setAdminUser(); 312 313 // No identity fields. 314 $CFG->showuseridentity = ''; 315 $result = external::search_users('cats', 'moodle/competency:planmanage'); 316 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 317 $this->assertCount(0, $result['users']); 318 $this->assertEquals(0, $result['count']); 319 320 // Filter by name. 321 $CFG->showuseridentity = ''; 322 $result = external::search_users('dyyylan', 'moodle/competency:planmanage'); 323 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 324 $this->assertCount(2, $result['users']); 325 $this->assertEquals(2, $result['count']); 326 $this->assertEquals($u2->id, $result['users'][0]['id']); 327 $this->assertEquals($u1->id, $result['users'][1]['id']); 328 329 // Filter by institution and name. 330 $CFG->showuseridentity = 'institution'; 331 $result = external::search_users('bob', 'moodle/competency:planmanage'); 332 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 333 $this->assertCount(2, $result['users']); 334 $this->assertEquals(2, $result['count']); 335 $this->assertEquals($u1->id, $result['users'][0]['id']); 336 $this->assertEquals($u3->id, $result['users'][1]['id']); 337 338 // Filter by id number. 339 $CFG->showuseridentity = 'idnumber'; 340 $result = external::search_users('cats', 'moodle/competency:planmanage'); 341 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 342 $this->assertCount(1, $result['users']); 343 $this->assertEquals(1, $result['count']); 344 $this->assertEquals($u1->id, $result['users'][0]['id']); 345 $this->assertEquals($u1->idnumber, $result['users'][0]['idnumber']); 346 $this->assertEmpty($result['users'][0]['email']); 347 $this->assertEmpty($result['users'][0]['phone1']); 348 $this->assertEmpty($result['users'][0]['phone2']); 349 $this->assertEmpty($result['users'][0]['department']); 350 $this->assertEmpty($result['users'][0]['institution']); 351 352 // Filter by email. 353 $CFG->showuseridentity = 'email'; 354 $result = external::search_users('yyy', 'moodle/competency:planmanage'); 355 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 356 $this->assertCount(3, $result['users']); 357 $this->assertEquals(3, $result['count']); 358 $this->assertEquals($u2->id, $result['users'][0]['id']); 359 $this->assertEquals($u2->email, $result['users'][0]['email']); 360 $this->assertEquals($u1->id, $result['users'][1]['id']); 361 $this->assertEquals($u1->email, $result['users'][1]['email']); 362 $this->assertEquals($u3->id, $result['users'][2]['id']); 363 $this->assertEquals($u3->email, $result['users'][2]['email']); 364 365 // Filter by any. 366 $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution'; 367 $result = external::search_users('yyy', 'moodle/competency:planmanage'); 368 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 369 $this->assertCount(3, $result['users']); 370 $this->assertEquals(3, $result['count']); 371 $this->assertArrayHasKey('idnumber', $result['users'][0]); 372 $this->assertArrayHasKey('email', $result['users'][0]); 373 $this->assertArrayHasKey('phone1', $result['users'][0]); 374 $this->assertArrayHasKey('phone2', $result['users'][0]); 375 $this->assertArrayHasKey('department', $result['users'][0]); 376 $this->assertArrayHasKey('institution', $result['users'][0]); 377 378 // Switch to a user that cannot view identity fields. 379 $this->setUser($ux); 380 $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution'; 381 382 // Only names are included. 383 $result = external::search_users('fish'); 384 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 385 $this->assertCount(0, $result['users']); 386 $this->assertEquals(0, $result['count']); 387 388 $result = external::search_users('bob', 'moodle/competency:planmanage'); 389 $result = external_api::clean_returnvalue(external::search_users_returns(), $result); 390 $this->assertCount(1, $result['users']); 391 $this->assertEquals(1, $result['count']); 392 $this->assertEquals($u1->id, $result['users'][0]['id']); 393 $this->assertEmpty($result['users'][0]['idnumber']); 394 $this->assertEmpty($result['users'][0]['email']); 395 $this->assertEmpty($result['users'][0]['phone1']); 396 $this->assertEmpty($result['users'][0]['phone2']); 397 $this->assertEmpty($result['users'][0]['department']); 398 $this->assertEmpty($result['users'][0]['institution']); 399 } 400 401 public function test_data_for_user_competency_summary_in_plan() { 402 global $CFG; 403 404 $this->setUser($this->creator); 405 406 $dg = $this->getDataGenerator(); 407 $lpg = $dg->get_plugin_generator('core_competency'); 408 409 $f1 = $lpg->create_framework(); 410 411 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); 412 413 $tpl = $lpg->create_template(); 414 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1->get('id'))); 415 416 $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id'), 'name' => 'Evil')); 417 418 $uc = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1->get('id'))); 419 420 $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1, true); 421 $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 2, true); 422 423 $summary = external::data_for_user_competency_summary_in_plan($c1->get('id'), $plan->get('id')); 424 $this->assertTrue($summary->usercompetencysummary->cangrade); 425 $this->assertEquals('Evil', $summary->plan->name); 426 $this->assertEquals('B', $summary->usercompetencysummary->usercompetency->gradename); 427 $this->assertEquals('B', $summary->usercompetencysummary->evidence[0]->gradename); 428 $this->assertEquals('A', $summary->usercompetencysummary->evidence[1]->gradename); 429 } 430 431 public function test_data_for_user_competency_summary() { 432 $this->setUser($this->creator); 433 434 $dg = $this->getDataGenerator(); 435 $lpg = $dg->get_plugin_generator('core_competency'); 436 $f1 = $lpg->create_framework(); 437 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); 438 439 $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true); 440 $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true); 441 442 $summary = external::data_for_user_competency_summary($this->user->id, $c1->get('id')); 443 $this->assertTrue($summary->cangrade); 444 $this->assertEquals('B', $summary->usercompetency->gradename); 445 $this->assertEquals('B', $summary->evidence[0]->gradename); 446 $this->assertEquals('A', $summary->evidence[1]->gradename); 447 } 448 449 public function test_data_for_course_competency_page() { 450 $this->setAdminUser(); 451 452 $dg = $this->getDataGenerator(); 453 $lpg = $dg->get_plugin_generator('core_competency'); 454 $f1 = $lpg->create_framework(); 455 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); 456 $course1 = $dg->create_course(array('category' => $this->category->id)); 457 $cc = api::add_competency_to_course($course1->id, $c1->get('id')); 458 459 $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true); 460 $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true); 461 462 $pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page'); 463 $page = $pagegenerator->create_instance(array('course' => $course1->id)); 464 $page2 = $pagegenerator->create_instance(array('course' => $course1->id)); 465 466 $cm = get_coursemodule_from_instance('page', $page->id); 467 $cm2 = get_coursemodule_from_instance('page', $page2->id); 468 // Add the competency to the course module. 469 $ccm = api::add_competency_to_course_module($cm, $c1->get('id')); 470 $summary = external::data_for_course_competencies_page($course1->id, 0); 471 $summary2 = external::data_for_course_competencies_page($course1->id, $cm->id); 472 $summary3 = external::data_for_course_competencies_page($course1->id, $cm2->id); 473 474 $this->assertEquals(count($summary->competencies), 1); 475 $this->assertEquals(count($summary->competencies), count($summary2->competencies)); 476 $this->assertEquals(count($summary3->competencies), 0); 477 } 478 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body