| 1 | efrain | 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 |  */
 | 
        
           | 1441 | ariadna | 36 | final class externallib_test extends externallib_advanced_testcase {
 | 
        
           | 1 | efrain | 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;
 | 
        
           | 1441 | ariadna | 67 |         parent::setUp();
 | 
        
           | 1 | efrain | 68 |   | 
        
           |  |  | 69 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         // Create some users.
 | 
        
           |  |  | 72 |         $creator = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 73 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 74 |         $catuser = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 75 |         $catcreator = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 76 |         $category = $this->getDataGenerator()->create_category();
 | 
        
           |  |  | 77 |         $othercategory = $this->getDataGenerator()->create_category();
 | 
        
           |  |  | 78 |         $syscontext = \context_system::instance();
 | 
        
           |  |  | 79 |         $catcontext = \context_coursecat::instance($category->id);
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |         // Fetching default authenticated user role.
 | 
        
           |  |  | 82 |         $authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid));
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         // Reset all default authenticated users permissions.
 | 
        
           |  |  | 85 |         unassign_capability('moodle/competency:competencygrade', $authrole->id);
 | 
        
           |  |  | 86 |         unassign_capability('moodle/competency:competencymanage', $authrole->id);
 | 
        
           |  |  | 87 |         unassign_capability('moodle/competency:competencyview', $authrole->id);
 | 
        
           |  |  | 88 |         unassign_capability('moodle/competency:planmanage', $authrole->id);
 | 
        
           |  |  | 89 |         unassign_capability('moodle/competency:planmanagedraft', $authrole->id);
 | 
        
           |  |  | 90 |         unassign_capability('moodle/competency:planmanageown', $authrole->id);
 | 
        
           |  |  | 91 |         unassign_capability('moodle/competency:planview', $authrole->id);
 | 
        
           |  |  | 92 |         unassign_capability('moodle/competency:planviewdraft', $authrole->id);
 | 
        
           |  |  | 93 |         unassign_capability('moodle/competency:planviewown', $authrole->id);
 | 
        
           |  |  | 94 |         unassign_capability('moodle/competency:planviewowndraft', $authrole->id);
 | 
        
           |  |  | 95 |         unassign_capability('moodle/competency:templatemanage', $authrole->id);
 | 
        
           |  |  | 96 |         unassign_capability('moodle/competency:templateview', $authrole->id);
 | 
        
           |  |  | 97 |         unassign_capability('moodle/cohort:manage', $authrole->id);
 | 
        
           |  |  | 98 |         unassign_capability('moodle/competency:coursecompetencyconfigure', $authrole->id);
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         // Creating specific roles.
 | 
        
           |  |  | 101 |         $this->creatorrole = create_role('Creator role', 'lpcreatorrole', 'learning plan creator role description');
 | 
        
           |  |  | 102 |         $this->userrole = create_role('User role', 'lpuserrole', 'learning plan user role description');
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |         assign_capability('moodle/competency:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 105 |         assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 106 |         assign_capability('moodle/competency:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 107 |         assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 108 |         assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 109 |         assign_capability('moodle/competency:planview', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 110 |         assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 111 |         assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 112 |         assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 113 |         assign_capability('moodle/cohort:manage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |         assign_capability('moodle/competency:competencyview', CAP_ALLOW, $this->userrole, $syscontext->id);
 | 
        
           |  |  | 116 |         assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontext->id);
 | 
        
           |  |  | 117 |         assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id);
 | 
        
           |  |  | 118 |         assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |         role_assign($this->creatorrole, $creator->id, $syscontext->id);
 | 
        
           |  |  | 121 |         role_assign($this->creatorrole, $catcreator->id, $catcontext->id);
 | 
        
           |  |  | 122 |         role_assign($this->userrole, $user->id, $syscontext->id);
 | 
        
           |  |  | 123 |         role_assign($this->userrole, $catuser->id, $catcontext->id);
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |         $this->creator = $creator;
 | 
        
           |  |  | 126 |         $this->catcreator = $catcreator;
 | 
        
           |  |  | 127 |         $this->user = $user;
 | 
        
           |  |  | 128 |         $this->catuser = $catuser;
 | 
        
           |  |  | 129 |         $this->category = $category;
 | 
        
           |  |  | 130 |         $this->othercategory = $othercategory;
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |         accesslib_clear_all_caches_for_unit_testing();
 | 
        
           |  |  | 133 |     }
 | 
        
           |  |  | 134 |   | 
        
           | 11 | efrain | 135 |     public function test_search_users_by_capability(): void {
 | 
        
           | 1 | efrain | 136 |         global $CFG;
 | 
        
           |  |  | 137 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |         $dg = $this->getDataGenerator();
 | 
        
           |  |  | 140 |         $ux = $dg->create_user();
 | 
        
           |  |  | 141 |         $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan',
 | 
        
           |  |  | 142 |             'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing',
 | 
        
           |  |  | 143 |             'institution' => 'HQ'));
 | 
        
           |  |  | 144 |   | 
        
           | 1441 | ariadna | 145 |         // Assign capability required to perform the search.
 | 
        
           | 1 | efrain | 146 |         $this->setUser($ux);
 | 
        
           | 1441 | ariadna | 147 |         $systemcontext = \context_system::instance();
 | 
        
           |  |  | 148 |         $customrole = $this->assignUserCapability('moodle/competency:templatemanage', $systemcontext->id);
 | 
        
           | 1 | efrain | 149 |   | 
        
           |  |  | 150 |         // Now we assign a different capability.
 | 
        
           |  |  | 151 |         $usercontext = \context_user::instance($u1->id);
 | 
        
           | 1441 | ariadna | 152 |         $this->assignUserCapability('moodle/competency:templatemanage', $usercontext->id, $customrole);
 | 
        
           | 1 | efrain | 153 |   | 
        
           |  |  | 154 |         $result = external::search_users('yyylan', 'moodle/competency:planmanage');
 | 
        
           |  |  | 155 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 156 |         $this->assertCount(0, $result['users']);
 | 
        
           |  |  | 157 |         $this->assertEquals(0, $result['count']);
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |         // Now we assign a matching capability in the same role.
 | 
        
           |  |  | 160 |         $usercontext = \context_user::instance($u1->id);
 | 
        
           |  |  | 161 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $customrole);
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         $result = external::search_users('yyylan', 'moodle/competency:planmanage');
 | 
        
           |  |  | 164 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 165 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 166 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 |         // Now assign another role with the same capability (test duplicates).
 | 
        
           |  |  | 169 |         role_assign($this->creatorrole, $ux->id, $usercontext->id);
 | 
        
           |  |  | 170 |         $result = external::search_users('yyylan', 'moodle/competency:planmanage');
 | 
        
           |  |  | 171 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 172 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 173 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 174 |   | 
        
           |  |  | 175 |         // Now lets try a different user with only the role at system level.
 | 
        
           |  |  | 176 |         $ux2 = $dg->create_user();
 | 
        
           |  |  | 177 |         role_assign($this->creatorrole, $ux2->id, $systemcontext->id);
 | 
        
           |  |  | 178 |         $this->setUser($ux2);
 | 
        
           |  |  | 179 |         $result = external::search_users('yyylan', 'moodle/competency:planmanage');
 | 
        
           |  |  | 180 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 181 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 182 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 |         // Now lets try a different user with only the role at user level.
 | 
        
           |  |  | 185 |         $ux3 = $dg->create_user();
 | 
        
           |  |  | 186 |         role_assign($this->creatorrole, $ux3->id, $usercontext->id);
 | 
        
           |  |  | 187 |         $this->setUser($ux3);
 | 
        
           | 1441 | ariadna | 188 |         $systemcontext = \context_system::instance();
 | 
        
           |  |  | 189 |         $customrole = $this->assignUserCapability('moodle/competency:templatemanage', $systemcontext->id, $customrole);
 | 
        
           | 1 | efrain | 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 |      */
 | 
        
           | 11 | efrain | 217 |     public function test_search_users_by_capability_the_comeback(): void {
 | 
        
           | 1 | efrain | 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);
 | 
        
           | 1441 | ariadna | 265 |         $dummyrole = $this->assignUserCapability('moodle/competency:templatemanage', $syscontext->id);
 | 
        
           | 1 | efrain | 266 |         $result = external::search_users('MOODLER', 'moodle/site:config');
 | 
        
           |  |  | 267 |         $this->assertCount(2, $result['users']);
 | 
        
           |  |  | 268 |         $this->assertEquals(2, $result['count']);
 | 
        
           |  |  | 269 |         $this->assertArrayHasKey($slave1->id, $result['users']);
 | 
        
           |  |  | 270 |         $this->assertArrayHasKey($slave3->id, $result['users']);
 | 
        
           |  |  | 271 |   | 
        
           |  |  | 272 |         $this->setUser($manager);
 | 
        
           | 1441 | ariadna | 273 |         $this->assignUserCapability('moodle/competency:templatemanage', $syscontext->id, $dummyrole);
 | 
        
           | 1 | efrain | 274 |         $result = external::search_users('MOODLER', 'moodle/site:config');
 | 
        
           |  |  | 275 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 276 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 277 |         $this->assertArrayHasKey($slave1->id, $result['users']);
 | 
        
           |  |  | 278 |     }
 | 
        
           |  |  | 279 |   | 
        
           | 11 | efrain | 280 |     public function test_search_users(): void {
 | 
        
           | 1 | efrain | 281 |         global $CFG;
 | 
        
           |  |  | 282 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 283 |   | 
        
           |  |  | 284 |         $dg = $this->getDataGenerator();
 | 
        
           |  |  | 285 |         $ux = $dg->create_user();
 | 
        
           |  |  | 286 |         $u1 = $dg->create_user(array('idnumber' => 'Cats', 'firstname' => 'Bob', 'lastname' => 'Dyyylan',
 | 
        
           |  |  | 287 |             'email' => 'bobbyyy@dyyylan.com', 'phone1' => '123456', 'phone2' => '78910', 'department' => 'Marketing',
 | 
        
           |  |  | 288 |             'institution' => 'HQ'));
 | 
        
           |  |  | 289 |         $u2 = $dg->create_user(array('idnumber' => 'Dogs', 'firstname' => 'Alice', 'lastname' => 'Dyyylan',
 | 
        
           |  |  | 290 |             'email' => 'alyyyson@dyyylan.com', 'phone1' => '33333', 'phone2' => '77777', 'department' => 'Development',
 | 
        
           |  |  | 291 |             'institution' => 'O2'));
 | 
        
           |  |  | 292 |         $u3 = $dg->create_user(array('idnumber' => 'Fish', 'firstname' => 'Thomas', 'lastname' => 'Xow',
 | 
        
           |  |  | 293 |             'email' => 'fishyyy@moodle.com', 'phone1' => '77777', 'phone2' => '33333', 'department' => 'Research',
 | 
        
           |  |  | 294 |             'institution' => 'Bob'));
 | 
        
           |  |  | 295 |   | 
        
           |  |  | 296 |         // We need to give the user the capability we are searching for on each of the test users.
 | 
        
           |  |  | 297 |         $this->setAdminUser();
 | 
        
           |  |  | 298 |         $usercontext = \context_user::instance($u1->id);
 | 
        
           |  |  | 299 |         $dummyrole = $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id);
 | 
        
           |  |  | 300 |         $usercontext = \context_user::instance($u2->id);
 | 
        
           |  |  | 301 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
 | 
        
           |  |  | 302 |         $usercontext = \context_user::instance($u3->id);
 | 
        
           |  |  | 303 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
 | 
        
           |  |  | 304 |   | 
        
           |  |  | 305 |         $this->setUser($ux);
 | 
        
           |  |  | 306 |         $usercontext = \context_user::instance($u1->id);
 | 
        
           |  |  | 307 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
 | 
        
           |  |  | 308 |         $usercontext = \context_user::instance($u2->id);
 | 
        
           |  |  | 309 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
 | 
        
           |  |  | 310 |         $usercontext = \context_user::instance($u3->id);
 | 
        
           |  |  | 311 |         $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
 | 
        
           |  |  | 312 |   | 
        
           |  |  | 313 |         $this->setAdminUser();
 | 
        
           |  |  | 314 |   | 
        
           |  |  | 315 |         // No identity fields.
 | 
        
           |  |  | 316 |         $CFG->showuseridentity = '';
 | 
        
           |  |  | 317 |         $result = external::search_users('cats', 'moodle/competency:planmanage');
 | 
        
           |  |  | 318 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 319 |         $this->assertCount(0, $result['users']);
 | 
        
           |  |  | 320 |         $this->assertEquals(0, $result['count']);
 | 
        
           |  |  | 321 |   | 
        
           |  |  | 322 |         // Filter by name.
 | 
        
           |  |  | 323 |         $CFG->showuseridentity = '';
 | 
        
           |  |  | 324 |         $result = external::search_users('dyyylan', 'moodle/competency:planmanage');
 | 
        
           |  |  | 325 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 326 |         $this->assertCount(2, $result['users']);
 | 
        
           |  |  | 327 |         $this->assertEquals(2, $result['count']);
 | 
        
           |  |  | 328 |         $this->assertEquals($u2->id, $result['users'][0]['id']);
 | 
        
           |  |  | 329 |         $this->assertEquals($u1->id, $result['users'][1]['id']);
 | 
        
           |  |  | 330 |   | 
        
           |  |  | 331 |         // Filter by institution and name.
 | 
        
           |  |  | 332 |         $CFG->showuseridentity = 'institution';
 | 
        
           |  |  | 333 |         $result = external::search_users('bob', 'moodle/competency:planmanage');
 | 
        
           |  |  | 334 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 335 |         $this->assertCount(2, $result['users']);
 | 
        
           |  |  | 336 |         $this->assertEquals(2, $result['count']);
 | 
        
           |  |  | 337 |         $this->assertEquals($u1->id, $result['users'][0]['id']);
 | 
        
           |  |  | 338 |         $this->assertEquals($u3->id, $result['users'][1]['id']);
 | 
        
           |  |  | 339 |   | 
        
           |  |  | 340 |         // Filter by id number.
 | 
        
           |  |  | 341 |         $CFG->showuseridentity = 'idnumber';
 | 
        
           |  |  | 342 |         $result = external::search_users('cats', 'moodle/competency:planmanage');
 | 
        
           |  |  | 343 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 344 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 345 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 346 |         $this->assertEquals($u1->id, $result['users'][0]['id']);
 | 
        
           |  |  | 347 |         $this->assertEquals($u1->idnumber, $result['users'][0]['idnumber']);
 | 
        
           |  |  | 348 |         $this->assertEmpty($result['users'][0]['email']);
 | 
        
           |  |  | 349 |         $this->assertEmpty($result['users'][0]['phone1']);
 | 
        
           |  |  | 350 |         $this->assertEmpty($result['users'][0]['phone2']);
 | 
        
           |  |  | 351 |         $this->assertEmpty($result['users'][0]['department']);
 | 
        
           |  |  | 352 |         $this->assertEmpty($result['users'][0]['institution']);
 | 
        
           |  |  | 353 |   | 
        
           |  |  | 354 |         // Filter by email.
 | 
        
           |  |  | 355 |         $CFG->showuseridentity = 'email';
 | 
        
           |  |  | 356 |         $result = external::search_users('yyy', 'moodle/competency:planmanage');
 | 
        
           |  |  | 357 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 358 |         $this->assertCount(3, $result['users']);
 | 
        
           |  |  | 359 |         $this->assertEquals(3, $result['count']);
 | 
        
           |  |  | 360 |         $this->assertEquals($u2->id, $result['users'][0]['id']);
 | 
        
           |  |  | 361 |         $this->assertEquals($u2->email, $result['users'][0]['email']);
 | 
        
           |  |  | 362 |         $this->assertEquals($u1->id, $result['users'][1]['id']);
 | 
        
           |  |  | 363 |         $this->assertEquals($u1->email, $result['users'][1]['email']);
 | 
        
           |  |  | 364 |         $this->assertEquals($u3->id, $result['users'][2]['id']);
 | 
        
           |  |  | 365 |         $this->assertEquals($u3->email, $result['users'][2]['email']);
 | 
        
           |  |  | 366 |   | 
        
           |  |  | 367 |         // Filter by any.
 | 
        
           |  |  | 368 |         $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution';
 | 
        
           |  |  | 369 |         $result = external::search_users('yyy', 'moodle/competency:planmanage');
 | 
        
           |  |  | 370 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 371 |         $this->assertCount(3, $result['users']);
 | 
        
           |  |  | 372 |         $this->assertEquals(3, $result['count']);
 | 
        
           |  |  | 373 |         $this->assertArrayHasKey('idnumber', $result['users'][0]);
 | 
        
           |  |  | 374 |         $this->assertArrayHasKey('email', $result['users'][0]);
 | 
        
           |  |  | 375 |         $this->assertArrayHasKey('phone1', $result['users'][0]);
 | 
        
           |  |  | 376 |         $this->assertArrayHasKey('phone2', $result['users'][0]);
 | 
        
           |  |  | 377 |         $this->assertArrayHasKey('department', $result['users'][0]);
 | 
        
           |  |  | 378 |         $this->assertArrayHasKey('institution', $result['users'][0]);
 | 
        
           |  |  | 379 |   | 
        
           |  |  | 380 |         // Switch to a user that cannot view identity fields.
 | 
        
           |  |  | 381 |         $this->setUser($ux);
 | 
        
           | 1441 | ariadna | 382 |         $systemcontext = \context_system::instance();
 | 
        
           |  |  | 383 |         $this->assignUserCapability('moodle/competency:templatemanage', $systemcontext->id, $dummyrole);
 | 
        
           | 1 | efrain | 384 |         $CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution';
 | 
        
           |  |  | 385 |   | 
        
           |  |  | 386 |         // Only names are included.
 | 
        
           |  |  | 387 |         $result = external::search_users('fish');
 | 
        
           |  |  | 388 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 389 |         $this->assertCount(0, $result['users']);
 | 
        
           |  |  | 390 |         $this->assertEquals(0, $result['count']);
 | 
        
           |  |  | 391 |   | 
        
           |  |  | 392 |         $result = external::search_users('bob', 'moodle/competency:planmanage');
 | 
        
           |  |  | 393 |         $result = external_api::clean_returnvalue(external::search_users_returns(), $result);
 | 
        
           |  |  | 394 |         $this->assertCount(1, $result['users']);
 | 
        
           |  |  | 395 |         $this->assertEquals(1, $result['count']);
 | 
        
           |  |  | 396 |         $this->assertEquals($u1->id, $result['users'][0]['id']);
 | 
        
           |  |  | 397 |         $this->assertEmpty($result['users'][0]['idnumber']);
 | 
        
           |  |  | 398 |         $this->assertEmpty($result['users'][0]['email']);
 | 
        
           |  |  | 399 |         $this->assertEmpty($result['users'][0]['phone1']);
 | 
        
           |  |  | 400 |         $this->assertEmpty($result['users'][0]['phone2']);
 | 
        
           |  |  | 401 |         $this->assertEmpty($result['users'][0]['department']);
 | 
        
           |  |  | 402 |         $this->assertEmpty($result['users'][0]['institution']);
 | 
        
           |  |  | 403 |     }
 | 
        
           |  |  | 404 |   | 
        
           | 11 | efrain | 405 |     public function test_data_for_user_competency_summary_in_plan(): void {
 | 
        
           | 1 | efrain | 406 |         global $CFG;
 | 
        
           |  |  | 407 |   | 
        
           |  |  | 408 |         $this->setUser($this->creator);
 | 
        
           |  |  | 409 |   | 
        
           |  |  | 410 |         $dg = $this->getDataGenerator();
 | 
        
           |  |  | 411 |         $lpg = $dg->get_plugin_generator('core_competency');
 | 
        
           |  |  | 412 |   | 
        
           |  |  | 413 |         $f1 = $lpg->create_framework();
 | 
        
           |  |  | 414 |   | 
        
           |  |  | 415 |         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 | 
        
           |  |  | 416 |   | 
        
           |  |  | 417 |         $tpl = $lpg->create_template();
 | 
        
           |  |  | 418 |         $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1->get('id')));
 | 
        
           |  |  | 419 |   | 
        
           |  |  | 420 |         $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id'), 'name' => 'Evil'));
 | 
        
           |  |  | 421 |   | 
        
           |  |  | 422 |         $uc = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1->get('id')));
 | 
        
           |  |  | 423 |   | 
        
           |  |  | 424 |         $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1, true);
 | 
        
           |  |  | 425 |         $evidence = \core_competency\external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 2, true);
 | 
        
           |  |  | 426 |   | 
        
           |  |  | 427 |         $summary = external::data_for_user_competency_summary_in_plan($c1->get('id'), $plan->get('id'));
 | 
        
           |  |  | 428 |         $this->assertTrue($summary->usercompetencysummary->cangrade);
 | 
        
           |  |  | 429 |         $this->assertEquals('Evil', $summary->plan->name);
 | 
        
           |  |  | 430 |         $this->assertEquals('B', $summary->usercompetencysummary->usercompetency->gradename);
 | 
        
           |  |  | 431 |         $this->assertEquals('B', $summary->usercompetencysummary->evidence[0]->gradename);
 | 
        
           |  |  | 432 |         $this->assertEquals('A', $summary->usercompetencysummary->evidence[1]->gradename);
 | 
        
           |  |  | 433 |     }
 | 
        
           |  |  | 434 |   | 
        
           | 11 | efrain | 435 |     public function test_data_for_user_competency_summary(): void {
 | 
        
           | 1 | efrain | 436 |         $this->setUser($this->creator);
 | 
        
           |  |  | 437 |   | 
        
           |  |  | 438 |         $dg = $this->getDataGenerator();
 | 
        
           |  |  | 439 |         $lpg = $dg->get_plugin_generator('core_competency');
 | 
        
           |  |  | 440 |         $f1 = $lpg->create_framework();
 | 
        
           |  |  | 441 |         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 | 
        
           |  |  | 442 |   | 
        
           |  |  | 443 |         $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true);
 | 
        
           |  |  | 444 |         $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true);
 | 
        
           |  |  | 445 |   | 
        
           |  |  | 446 |         $summary = external::data_for_user_competency_summary($this->user->id, $c1->get('id'));
 | 
        
           |  |  | 447 |         $this->assertTrue($summary->cangrade);
 | 
        
           |  |  | 448 |         $this->assertEquals('B', $summary->usercompetency->gradename);
 | 
        
           |  |  | 449 |         $this->assertEquals('B', $summary->evidence[0]->gradename);
 | 
        
           |  |  | 450 |         $this->assertEquals('A', $summary->evidence[1]->gradename);
 | 
        
           |  |  | 451 |     }
 | 
        
           |  |  | 452 |   | 
        
           | 11 | efrain | 453 |     public function test_data_for_course_competency_page(): void {
 | 
        
           | 1 | efrain | 454 |         $this->setAdminUser();
 | 
        
           |  |  | 455 |   | 
        
           |  |  | 456 |         $dg = $this->getDataGenerator();
 | 
        
           |  |  | 457 |         $lpg = $dg->get_plugin_generator('core_competency');
 | 
        
           |  |  | 458 |         $f1 = $lpg->create_framework();
 | 
        
           |  |  | 459 |         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 | 
        
           |  |  | 460 |         $course1 = $dg->create_course(array('category' => $this->category->id));
 | 
        
           |  |  | 461 |         $cc = api::add_competency_to_course($course1->id, $c1->get('id'));
 | 
        
           |  |  | 462 |   | 
        
           |  |  | 463 |         $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 1, true);
 | 
        
           |  |  | 464 |         $evidence = \core_competency\external::grade_competency($this->user->id, $c1->get('id'), 2, true);
 | 
        
           |  |  | 465 |   | 
        
           |  |  | 466 |         $pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page');
 | 
        
           |  |  | 467 |         $page = $pagegenerator->create_instance(array('course' => $course1->id));
 | 
        
           |  |  | 468 |         $page2 = $pagegenerator->create_instance(array('course' => $course1->id));
 | 
        
           |  |  | 469 |   | 
        
           |  |  | 470 |         $cm = get_coursemodule_from_instance('page', $page->id);
 | 
        
           |  |  | 471 |         $cm2 = get_coursemodule_from_instance('page', $page2->id);
 | 
        
           |  |  | 472 |         // Add the competency to the course module.
 | 
        
           |  |  | 473 |         $ccm = api::add_competency_to_course_module($cm, $c1->get('id'));
 | 
        
           |  |  | 474 |         $summary = external::data_for_course_competencies_page($course1->id, 0);
 | 
        
           |  |  | 475 |         $summary2 = external::data_for_course_competencies_page($course1->id, $cm->id);
 | 
        
           |  |  | 476 |         $summary3 = external::data_for_course_competencies_page($course1->id, $cm2->id);
 | 
        
           |  |  | 477 |   | 
        
           |  |  | 478 |         $this->assertEquals(count($summary->competencies), 1);
 | 
        
           |  |  | 479 |         $this->assertEquals(count($summary->competencies), count($summary2->competencies));
 | 
        
           |  |  | 480 |         $this->assertEquals(count($summary3->competencies), 0);
 | 
        
           |  |  | 481 |     }
 | 
        
           |  |  | 482 | }
 |