[ 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 * User external PHPunit tests 19 * 20 * @package core_user 21 * @category external 22 * @copyright 2012 Jerome Mouneyrac 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 2.4 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 require_once($CFG->dirroot . '/user/externallib.php'); 33 require_once($CFG->dirroot . '/files/externallib.php'); 34 35 class core_user_externallib_testcase extends externallib_advanced_testcase { 36 37 /** 38 * Test get_users 39 */ 40 public function test_get_users() { 41 global $USER, $CFG; 42 43 $this->resetAfterTest(true); 44 45 $course = self::getDataGenerator()->create_course(); 46 47 $user1 = array( 48 'username' => 'usernametest1', 49 'idnumber' => 'idnumbertest1', 50 'firstname' => 'First Name User Test 1', 51 'lastname' => 'Last Name User Test 1', 52 'email' => 'usertest1@example.com', 53 'address' => '2 Test Street Perth 6000 WA', 54 'phone1' => '01010101010', 55 'phone2' => '02020203', 56 'icq' => 'testuser1', 57 'skype' => 'testuser1', 58 'yahoo' => 'testuser1', 59 'aim' => 'testuser1', 60 'msn' => 'testuser1', 61 'department' => 'Department of user 1', 62 'institution' => 'Institution of user 1', 63 'description' => 'This is a description for user 1', 64 'descriptionformat' => FORMAT_MOODLE, 65 'city' => 'Perth', 66 'url' => 'http://moodle.org', 67 'country' => 'AU' 68 ); 69 70 $user1 = self::getDataGenerator()->create_user($user1); 71 set_config('usetags', 1); 72 require_once($CFG->dirroot . '/user/editlib.php'); 73 $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); 74 useredit_update_interests($user1, $user1->interests); 75 76 $user2 = self::getDataGenerator()->create_user( 77 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2')); 78 79 $generatedusers = array(); 80 $generatedusers[$user1->id] = $user1; 81 $generatedusers[$user2->id] = $user2; 82 83 $context = context_course::instance($course->id); 84 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); 85 86 // Enrol the users in the course. 87 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid); 88 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid); 89 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid); 90 91 // call as admin and receive all possible fields. 92 $this->setAdminUser(); 93 94 $searchparams = array( 95 array('key' => 'invalidkey', 'value' => 'invalidkey'), 96 array('key' => 'email', 'value' => $user1->email), 97 array('key' => 'firstname', 'value' => $user1->firstname)); 98 99 // Call the external function. 100 $result = core_user_external::get_users($searchparams); 101 102 // We need to execute the return values cleaning process to simulate the web service server 103 $result = external_api::clean_returnvalue(core_user_external::get_users_returns(), $result); 104 105 // Check we retrieve the good total number of enrolled users + no error on capability. 106 $expectedreturnedusers = 1; 107 $returnedusers = $result['users']; 108 $this->assertEquals($expectedreturnedusers, count($returnedusers)); 109 110 foreach($returnedusers as $returneduser) { 111 $generateduser = ($returneduser['id'] == $USER->id) ? 112 $USER : $generatedusers[$returneduser['id']]; 113 $this->assertEquals($generateduser->username, $returneduser['username']); 114 if (!empty($generateduser->idnumber)) { 115 $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']); 116 } 117 $this->assertEquals($generateduser->firstname, $returneduser['firstname']); 118 $this->assertEquals($generateduser->lastname, $returneduser['lastname']); 119 if ($generateduser->email != $USER->email) { // Don't check the tmp modified $USER email. 120 $this->assertEquals($generateduser->email, $returneduser['email']); 121 } 122 if (!empty($generateduser->address)) { 123 $this->assertEquals($generateduser->address, $returneduser['address']); 124 } 125 if (!empty($generateduser->phone1)) { 126 $this->assertEquals($generateduser->phone1, $returneduser['phone1']); 127 } 128 if (!empty($generateduser->phone2)) { 129 $this->assertEquals($generateduser->phone2, $returneduser['phone2']); 130 } 131 if (!empty($generateduser->icq)) { 132 $this->assertEquals($generateduser->icq, $returneduser['icq']); 133 } 134 if (!empty($generateduser->skype)) { 135 $this->assertEquals($generateduser->skype, $returneduser['skype']); 136 } 137 if (!empty($generateduser->yahoo)) { 138 $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']); 139 } 140 if (!empty($generateduser->aim)) { 141 $this->assertEquals($generateduser->aim, $returneduser['aim']); 142 } 143 if (!empty($generateduser->msn)) { 144 $this->assertEquals($generateduser->msn, $returneduser['msn']); 145 } 146 if (!empty($generateduser->department)) { 147 $this->assertEquals($generateduser->department, $returneduser['department']); 148 } 149 if (!empty($generateduser->institution)) { 150 $this->assertEquals($generateduser->institution, $returneduser['institution']); 151 } 152 if (!empty($generateduser->description)) { 153 $this->assertEquals($generateduser->description, $returneduser['description']); 154 } 155 if (!empty($generateduser->descriptionformat)) { 156 $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']); 157 } 158 if (!empty($generateduser->city)) { 159 $this->assertEquals($generateduser->city, $returneduser['city']); 160 } 161 if (!empty($generateduser->country)) { 162 $this->assertEquals($generateduser->country, $returneduser['country']); 163 } 164 if (!empty($generateduser->url)) { 165 $this->assertEquals($generateduser->url, $returneduser['url']); 166 } 167 if (!empty($CFG->usetags) and !empty($generateduser->interests)) { 168 $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']); 169 } 170 } 171 172 // Test the invalid key warning. 173 $warnings = $result['warnings']; 174 $this->assertEquals(count($warnings), 1); 175 $warning = array_pop($warnings); 176 $this->assertEquals($warning['item'], 'invalidkey'); 177 $this->assertEquals($warning['warningcode'], 'invalidfieldparameter'); 178 179 // Test sending twice the same search field. 180 try { 181 $searchparams = array( 182 array('key' => 'firstname', 'value' => 'Canard'), 183 array('key' => 'email', 'value' => $user1->email), 184 array('key' => 'firstname', 'value' => $user1->firstname)); 185 186 // Call the external function. 187 $result = core_user_external::get_users($searchparams); 188 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); 189 } catch (moodle_exception $e) { 190 $this->assertEquals('keyalreadyset', $e->errorcode); 191 } catch (Exception $e) { 192 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.'); 193 } 194 } 195 196 /** 197 * Test get_users_by_field 198 */ 199 public function test_get_users_by_field() { 200 global $USER, $CFG; 201 202 $this->resetAfterTest(true); 203 204 $course = self::getDataGenerator()->create_course(); 205 $user1 = array( 206 'username' => 'usernametest1', 207 'idnumber' => 'idnumbertest1', 208 'firstname' => 'First Name User Test 1', 209 'lastname' => 'Last Name User Test 1', 210 'email' => 'usertest1@example.com', 211 'address' => '2 Test Street Perth 6000 WA', 212 'phone1' => '01010101010', 213 'phone2' => '02020203', 214 'icq' => 'testuser1', 215 'skype' => 'testuser1', 216 'yahoo' => 'testuser1', 217 'aim' => 'testuser1', 218 'msn' => 'testuser1', 219 'department' => 'Department of user 1', 220 'institution' => 'Institution of user 1', 221 'description' => 'This is a description for user 1', 222 'descriptionformat' => FORMAT_MOODLE, 223 'city' => 'Perth', 224 'url' => 'http://moodle.org', 225 'country' => 'AU' 226 ); 227 $user1 = self::getDataGenerator()->create_user($user1); 228 if (!empty($CFG->usetags)) { 229 require_once($CFG->dirroot . '/user/editlib.php'); 230 $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); 231 useredit_update_interests($user1, $user1->interests); 232 } 233 $user2 = self::getDataGenerator()->create_user( 234 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2')); 235 236 $generatedusers = array(); 237 $generatedusers[$user1->id] = $user1; 238 $generatedusers[$user2->id] = $user2; 239 240 $context = context_course::instance($course->id); 241 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id); 242 243 // Enrol the users in the course. 244 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual'); 245 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual'); 246 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual'); 247 248 // call as admin and receive all possible fields. 249 $this->setAdminUser(); 250 251 $fieldstosearch = array('id', 'idnumber', 'username', 'email'); 252 253 foreach ($fieldstosearch as $fieldtosearch) { 254 255 // Call the external function. 256 $returnedusers = core_user_external::get_users_by_field($fieldtosearch, 257 array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch})); 258 $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); 259 260 // Expected result differ following the searched field 261 // Admin user in the PHPunit framework doesn't have an idnumber. 262 if ($fieldtosearch == 'idnumber') { 263 $expectedreturnedusers = 2; 264 } else { 265 $expectedreturnedusers = 3; 266 } 267 268 // Check we retrieve the good total number of enrolled users + no error on capability. 269 $this->assertEquals($expectedreturnedusers, count($returnedusers)); 270 271 foreach($returnedusers as $returneduser) { 272 $generateduser = ($returneduser['id'] == $USER->id) ? 273 $USER : $generatedusers[$returneduser['id']]; 274 $this->assertEquals($generateduser->username, $returneduser['username']); 275 if (!empty($generateduser->idnumber)) { 276 $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']); 277 } 278 $this->assertEquals($generateduser->firstname, $returneduser['firstname']); 279 $this->assertEquals($generateduser->lastname, $returneduser['lastname']); 280 if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email 281 $this->assertEquals($generateduser->email, $returneduser['email']); 282 } 283 if (!empty($generateduser->address)) { 284 $this->assertEquals($generateduser->address, $returneduser['address']); 285 } 286 if (!empty($generateduser->phone1)) { 287 $this->assertEquals($generateduser->phone1, $returneduser['phone1']); 288 } 289 if (!empty($generateduser->phone2)) { 290 $this->assertEquals($generateduser->phone2, $returneduser['phone2']); 291 } 292 if (!empty($generateduser->icq)) { 293 $this->assertEquals($generateduser->icq, $returneduser['icq']); 294 } 295 if (!empty($generateduser->skype)) { 296 $this->assertEquals($generateduser->skype, $returneduser['skype']); 297 } 298 if (!empty($generateduser->yahoo)) { 299 $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']); 300 } 301 if (!empty($generateduser->aim)) { 302 $this->assertEquals($generateduser->aim, $returneduser['aim']); 303 } 304 if (!empty($generateduser->msn)) { 305 $this->assertEquals($generateduser->msn, $returneduser['msn']); 306 } 307 if (!empty($generateduser->department)) { 308 $this->assertEquals($generateduser->department, $returneduser['department']); 309 } 310 if (!empty($generateduser->institution)) { 311 $this->assertEquals($generateduser->institution, $returneduser['institution']); 312 } 313 if (!empty($generateduser->description)) { 314 $this->assertEquals($generateduser->description, $returneduser['description']); 315 } 316 if (!empty($generateduser->descriptionformat) and isset($returneduser['descriptionformat'])) { 317 $this->assertEquals($generateduser->descriptionformat, $returneduser['descriptionformat']); 318 } 319 if (!empty($generateduser->city)) { 320 $this->assertEquals($generateduser->city, $returneduser['city']); 321 } 322 if (!empty($generateduser->country)) { 323 $this->assertEquals($generateduser->country, $returneduser['country']); 324 } 325 if (!empty($generateduser->url)) { 326 $this->assertEquals($generateduser->url, $returneduser['url']); 327 } 328 if (!empty($CFG->usetags) and !empty($generateduser->interests)) { 329 $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']); 330 } 331 } 332 } 333 334 // Test that no result are returned for search by username if we are not admin 335 $this->setGuestUser(); 336 337 // Call the external function. 338 $returnedusers = core_user_external::get_users_by_field('username', 339 array($USER->username, $user1->username, $user2->username)); 340 $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); 341 342 // Only the own $USER username should be returned 343 $this->assertEquals(1, count($returnedusers)); 344 345 // And finally test as one of the enrolled users. 346 $this->setUser($user1); 347 348 // Call the external function. 349 $returnedusers = core_user_external::get_users_by_field('username', 350 array($USER->username, $user1->username, $user2->username)); 351 $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers); 352 353 // Only the own $USER username should be returned still. 354 $this->assertEquals(1, count($returnedusers)); 355 } 356 357 public function get_course_user_profiles_setup($capability) { 358 global $USER, $CFG; 359 360 $this->resetAfterTest(true); 361 362 $return = new stdClass(); 363 364 // Create the course and fetch its context. 365 $return->course = self::getDataGenerator()->create_course(); 366 $return->user1 = array( 367 'username' => 'usernametest1', 368 'idnumber' => 'idnumbertest1', 369 'firstname' => 'First Name User Test 1', 370 'lastname' => 'Last Name User Test 1', 371 'email' => 'usertest1@example.com', 372 'address' => '2 Test Street Perth 6000 WA', 373 'phone1' => '01010101010', 374 'phone2' => '02020203', 375 'icq' => 'testuser1', 376 'skype' => 'testuser1', 377 'yahoo' => 'testuser1', 378 'aim' => 'testuser1', 379 'msn' => 'testuser1', 380 'department' => 'Department of user 1', 381 'institution' => 'Institution of user 1', 382 'description' => 'This is a description for user 1', 383 'descriptionformat' => FORMAT_MOODLE, 384 'city' => 'Perth', 385 'url' => 'http://moodle.org', 386 'country' => 'AU' 387 ); 388 $return->user1 = self::getDataGenerator()->create_user($return->user1); 389 if (!empty($CFG->usetags)) { 390 require_once($CFG->dirroot . '/user/editlib.php'); 391 $return->user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); 392 useredit_update_interests($return->user1, $return->user1->interests); 393 } 394 $return->user2 = self::getDataGenerator()->create_user(); 395 396 $context = context_course::instance($return->course->id); 397 $return->roleid = $this->assignUserCapability($capability, $context->id); 398 399 // Enrol the users in the course. 400 $this->getDataGenerator()->enrol_user($return->user1->id, $return->course->id, $return->roleid, 'manual'); 401 $this->getDataGenerator()->enrol_user($return->user2->id, $return->course->id, $return->roleid, 'manual'); 402 $this->getDataGenerator()->enrol_user($USER->id, $return->course->id, $return->roleid, 'manual'); 403 404 return $return; 405 } 406 407 /** 408 * Test get_course_user_profiles 409 */ 410 public function test_get_course_user_profiles() { 411 global $USER, $CFG; 412 413 $this->resetAfterTest(true); 414 415 $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails'); 416 417 // Call the external function. 418 $enrolledusers = core_user_external::get_course_user_profiles(array( 419 array('userid' => $USER->id, 'courseid' => $data->course->id))); 420 421 // We need to execute the return values cleaning process to simulate the web service server. 422 $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); 423 424 // Check we retrieve the good total number of enrolled users + no error on capability. 425 $this->assertEquals(1, count($enrolledusers)); 426 } 427 428 public function test_get_user_course_profile_as_admin() { 429 global $USER, $CFG; 430 431 global $USER, $CFG; 432 433 $this->resetAfterTest(true); 434 435 $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails'); 436 437 // Do the same call as admin to receive all possible fields. 438 $this->setAdminUser(); 439 $USER->email = "admin@example.com"; 440 441 // Call the external function. 442 $enrolledusers = core_user_external::get_course_user_profiles(array( 443 array('userid' => $data->user1->id, 'courseid' => $data->course->id))); 444 445 // We need to execute the return values cleaning process to simulate the web service server. 446 $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers); 447 448 foreach($enrolledusers as $enrolleduser) { 449 if ($enrolleduser['username'] == $data->user1->username) { 450 $this->assertEquals($data->user1->idnumber, $enrolleduser['idnumber']); 451 $this->assertEquals($data->user1->firstname, $enrolleduser['firstname']); 452 $this->assertEquals($data->user1->lastname, $enrolleduser['lastname']); 453 $this->assertEquals($data->user1->email, $enrolleduser['email']); 454 $this->assertEquals($data->user1->address, $enrolleduser['address']); 455 $this->assertEquals($data->user1->phone1, $enrolleduser['phone1']); 456 $this->assertEquals($data->user1->phone2, $enrolleduser['phone2']); 457 $this->assertEquals($data->user1->icq, $enrolleduser['icq']); 458 $this->assertEquals($data->user1->skype, $enrolleduser['skype']); 459 $this->assertEquals($data->user1->yahoo, $enrolleduser['yahoo']); 460 $this->assertEquals($data->user1->aim, $enrolleduser['aim']); 461 $this->assertEquals($data->user1->msn, $enrolleduser['msn']); 462 $this->assertEquals($data->user1->department, $enrolleduser['department']); 463 $this->assertEquals($data->user1->institution, $enrolleduser['institution']); 464 $this->assertEquals($data->user1->description, $enrolleduser['description']); 465 $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']); 466 $this->assertEquals($data->user1->city, $enrolleduser['city']); 467 $this->assertEquals($data->user1->country, $enrolleduser['country']); 468 $this->assertEquals($data->user1->url, $enrolleduser['url']); 469 if (!empty($CFG->usetags)) { 470 $this->assertEquals(implode(', ', $data->user1->interests), $enrolleduser['interests']); 471 } 472 } 473 } 474 } 475 476 /** 477 * Test create_users 478 */ 479 public function test_create_users() { 480 global $USER, $CFG, $DB; 481 482 $this->resetAfterTest(true); 483 484 $user1 = array( 485 'username' => 'usernametest1', 486 'password' => 'Moodle2012!', 487 'idnumber' => 'idnumbertest1', 488 'firstname' => 'First Name User Test 1', 489 'lastname' => 'Last Name User Test 1', 490 'middlename' => 'Middle Name User Test 1', 491 'lastnamephonetic' => '最後のお名前のテスト一号', 492 'firstnamephonetic' => 'お名前のテスト一号', 493 'alternatename' => 'Alternate Name User Test 1', 494 'email' => 'usertest1@example.com', 495 'description' => 'This is a description for user 1', 496 'city' => 'Perth', 497 'country' => 'AU' 498 ); 499 500 $context = context_system::instance(); 501 $roleid = $this->assignUserCapability('moodle/user:create', $context->id); 502 503 // Call the external function. 504 $createdusers = core_user_external::create_users(array($user1)); 505 506 // We need to execute the return values cleaning process to simulate the web service server. 507 $createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers); 508 509 // Check we retrieve the good total number of created users + no error on capability. 510 $this->assertEquals(1, count($createdusers)); 511 512 foreach($createdusers as $createduser) { 513 $dbuser = $DB->get_record('user', array('id' => $createduser['id'])); 514 $this->assertEquals($dbuser->username, $user1['username']); 515 $this->assertEquals($dbuser->idnumber, $user1['idnumber']); 516 $this->assertEquals($dbuser->firstname, $user1['firstname']); 517 $this->assertEquals($dbuser->lastname, $user1['lastname']); 518 $this->assertEquals($dbuser->email, $user1['email']); 519 $this->assertEquals($dbuser->description, $user1['description']); 520 $this->assertEquals($dbuser->city, $user1['city']); 521 $this->assertEquals($dbuser->country, $user1['country']); 522 } 523 524 // Call without required capability 525 $this->unassignUserCapability('moodle/user:create', $context->id, $roleid); 526 $this->expectException('required_capability_exception'); 527 $createdusers = core_user_external::create_users(array($user1)); 528 } 529 530 /** 531 * Test delete_users 532 */ 533 public function test_delete_users() { 534 global $USER, $CFG, $DB; 535 536 $this->resetAfterTest(true); 537 538 $user1 = self::getDataGenerator()->create_user(); 539 $user2 = self::getDataGenerator()->create_user(); 540 541 // Check the users were correctly created. 542 $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)', 543 array('userid1' => $user1->id, 'userid2' => $user2->id))); 544 545 $context = context_system::instance(); 546 $roleid = $this->assignUserCapability('moodle/user:delete', $context->id); 547 548 // Call the external function. 549 core_user_external::delete_users(array($user1->id, $user2->id)); 550 551 // Check we retrieve no users + no error on capability. 552 $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)', 553 array('userid1' => $user1->id, 'userid2' => $user2->id))); 554 555 // Call without required capability. 556 $this->unassignUserCapability('moodle/user:delete', $context->id, $roleid); 557 $this->expectException('required_capability_exception'); 558 core_user_external::delete_users(array($user1->id, $user2->id)); 559 } 560 561 /** 562 * Test update_users 563 */ 564 public function test_update_users() { 565 global $USER, $CFG, $DB; 566 567 $this->resetAfterTest(true); 568 569 $wsuser = self::getDataGenerator()->create_user(); 570 self::setUser($wsuser); 571 572 $context = context_user::instance($USER->id); 573 $contextid = $context->id; 574 $filename = "reddot.png"; 575 $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38" 576 . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; 577 578 // Call the files api to create a file. 579 $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/', 580 $filename, $filecontent, null, null); 581 $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); 582 583 $draftid = $draftfile['itemid']; 584 585 $user1 = self::getDataGenerator()->create_user(); 586 587 $user1 = array( 588 'id' => $user1->id, 589 'username' => 'usernametest1', 590 'password' => 'Moodle2012!', 591 'idnumber' => 'idnumbertest1', 592 'firstname' => 'First Name User Test 1', 593 'lastname' => 'Last Name User Test 1', 594 'middlename' => 'Middle Name User Test 1', 595 'lastnamephonetic' => '最後のお名前のテスト一号', 596 'firstnamephonetic' => 'お名前のテスト一号', 597 'alternatename' => 'Alternate Name User Test 1', 598 'email' => 'usertest1@example.com', 599 'description' => 'This is a description for user 1', 600 'city' => 'Perth', 601 'userpicture' => $draftid, 602 'country' => 'AU' 603 ); 604 605 $context = context_system::instance(); 606 $roleid = $this->assignUserCapability('moodle/user:update', $context->id); 607 608 // Call the external function. 609 core_user_external::update_users(array($user1)); 610 611 $dbuser = $DB->get_record('user', array('id' => $user1['id'])); 612 $this->assertEquals($dbuser->username, $user1['username']); 613 $this->assertEquals($dbuser->idnumber, $user1['idnumber']); 614 $this->assertEquals($dbuser->firstname, $user1['firstname']); 615 $this->assertEquals($dbuser->lastname, $user1['lastname']); 616 $this->assertEquals($dbuser->email, $user1['email']); 617 $this->assertEquals($dbuser->description, $user1['description']); 618 $this->assertEquals($dbuser->city, $user1['city']); 619 $this->assertEquals($dbuser->country, $user1['country']); 620 $this->assertNotEquals(0, $dbuser->picture, 'Picture must be set to the new icon itemid for this user'); 621 622 // Confirm no picture change when parameter is not supplied. 623 unset($user1['userpicture']); 624 core_user_external::update_users(array($user1)); 625 $dbusernopic = $DB->get_record('user', array('id' => $user1['id'])); 626 $this->assertEquals($dbuser->picture, $dbusernopic->picture, 'Picture not change without the parameter.'); 627 628 // Confirm delete of picture deletes the picture from the user record. 629 $user1['userpicture'] = 0; 630 core_user_external::update_users(array($user1)); 631 $dbuserdelpic = $DB->get_record('user', array('id' => $user1['id'])); 632 $this->assertEquals(0, $dbuserdelpic->picture, 'Picture must be deleted when sent as 0.'); 633 634 635 // Call without required capability. 636 $this->unassignUserCapability('moodle/user:update', $context->id, $roleid); 637 $this->expectException('required_capability_exception'); 638 core_user_external::update_users(array($user1)); 639 } 640 641 /** 642 * Test add_user_private_files 643 */ 644 public function test_add_user_private_files() { 645 global $USER, $CFG, $DB; 646 647 $this->resetAfterTest(true); 648 649 $context = context_system::instance(); 650 $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id); 651 652 $context = context_user::instance($USER->id); 653 $contextid = $context->id; 654 $component = "user"; 655 $filearea = "draft"; 656 $itemid = 0; 657 $filepath = "/"; 658 $filename = "Simple.txt"; 659 $filecontent = base64_encode("Let us create a nice simple file"); 660 $contextlevel = null; 661 $instanceid = null; 662 $browser = get_file_browser(); 663 664 // Call the files api to create a file. 665 $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, 666 $filename, $filecontent, $contextlevel, $instanceid); 667 $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); 668 669 $draftid = $draftfile['itemid']; 670 // Make sure the file was created. 671 $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename); 672 $this->assertNotEmpty($file); 673 674 // Make sure the file does not exist in the user private files. 675 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename); 676 $this->assertEmpty($file); 677 678 // Call the external function. 679 core_user_external::add_user_private_files($draftid); 680 681 // Make sure the file was added to the user private files. 682 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename); 683 $this->assertNotEmpty($file); 684 } 685 686 /** 687 * Test add user device 688 */ 689 public function test_add_user_device() { 690 global $USER, $CFG, $DB; 691 692 $this->resetAfterTest(true); 693 694 $device = array( 695 'appid' => 'com.moodle.moodlemobile', 696 'name' => 'occam', 697 'model' => 'Nexus 4', 698 'platform' => 'Android', 699 'version' => '4.2.2', 700 'pushid' => 'apushdkasdfj4835', 701 'uuid' => 'asdnfl348qlksfaasef859' 702 ); 703 704 // Call the external function. 705 core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], 706 $device['version'], $device['pushid'], $device['uuid']); 707 708 $created = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); 709 $created = (array) $created; 710 711 $this->assertEquals($device, array_intersect_key((array)$created, $device)); 712 713 // Test reuse the same pushid value. 714 $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], 715 $device['version'], $device['pushid'], $device['uuid']); 716 // We need to execute the return values cleaning process to simulate the web service server. 717 $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); 718 $this->assertCount(1, $warnings); 719 720 // Test update an existing device. 721 $device['pushid'] = 'different than before'; 722 $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], 723 $device['version'], $device['pushid'], $device['uuid']); 724 $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); 725 726 $this->assertEquals(1, $DB->count_records('user_devices')); 727 $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); 728 $this->assertEquals($device, array_intersect_key((array)$updated, $device)); 729 730 // Test creating a new device just changing the uuid. 731 $device['uuid'] = 'newuidforthesameuser'; 732 $device['pushid'] = 'new different than before'; 733 $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], 734 $device['version'], $device['pushid'], $device['uuid']); 735 $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); 736 $this->assertEquals(2, $DB->count_records('user_devices')); 737 } 738 739 /** 740 * Test remove user device 741 */ 742 public function test_remove_user_device() { 743 global $USER, $CFG, $DB; 744 745 $this->resetAfterTest(true); 746 747 $device = array( 748 'appid' => 'com.moodle.moodlemobile', 749 'name' => 'occam', 750 'model' => 'Nexus 4', 751 'platform' => 'Android', 752 'version' => '4.2.2', 753 'pushid' => 'apushdkasdfj4835', 754 'uuid' => 'ABCDE3723ksdfhasfaasef859' 755 ); 756 757 // A device with the same properties except the appid and pushid. 758 $device2 = $device; 759 $device2['pushid'] = "0987654321"; 760 $device2['appid'] = "other.app.com"; 761 762 $this->setAdminUser(); 763 // Create a user device using the external API function. 764 core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], 765 $device['version'], $device['pushid'], $device['uuid']); 766 767 // Create the same device but for a different app. 768 core_user_external::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'], 769 $device2['version'], $device2['pushid'], $device2['uuid']); 770 771 // Try to remove a device that does not exist. 772 $result = core_user_external::remove_user_device('1234567890'); 773 $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); 774 $this->assertFalse($result['removed']); 775 $this->assertCount(1, $result['warnings']); 776 777 // Try to remove a device that does not exist for an existing app. 778 $result = core_user_external::remove_user_device('1234567890', $device['appid']); 779 $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); 780 $this->assertFalse($result['removed']); 781 $this->assertCount(1, $result['warnings']); 782 783 // Remove an existing device for an existing app. This will remove one of the two devices. 784 $result = core_user_external::remove_user_device($device['uuid'], $device['appid']); 785 $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); 786 $this->assertTrue($result['removed']); 787 788 // Remove all the devices. This must remove the remaining device. 789 $result = core_user_external::remove_user_device($device['uuid']); 790 $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); 791 $this->assertTrue($result['removed']); 792 } 793 794 /** 795 * Test get_user_preferences 796 */ 797 public function test_get_user_preferences() { 798 $this->resetAfterTest(true); 799 800 $user = self::getDataGenerator()->create_user(); 801 set_user_preference('calendar_maxevents', 1, $user); 802 set_user_preference('some_random_text', 'text', $user); 803 804 $this->setUser($user); 805 806 $result = core_user_external::get_user_preferences(); 807 $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); 808 $this->assertCount(0, $result['warnings']); 809 // Expect 3, _lastloaded is always returned. 810 $this->assertCount(3, $result['preferences']); 811 812 foreach ($result['preferences'] as $pref) { 813 if ($pref['name'] === '_lastloaded') { 814 continue; 815 } 816 // Check we receive the expected preferences. 817 $this->assertEquals(get_user_preferences($pref['name']), $pref['value']); 818 } 819 820 // Retrieve just one preference. 821 $result = core_user_external::get_user_preferences('some_random_text'); 822 $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); 823 $this->assertCount(0, $result['warnings']); 824 $this->assertCount(1, $result['preferences']); 825 $this->assertEquals('text', $result['preferences'][0]['value']); 826 827 // Retrieve non-existent preference. 828 $result = core_user_external::get_user_preferences('non_existent'); 829 $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); 830 $this->assertCount(0, $result['warnings']); 831 $this->assertCount(1, $result['preferences']); 832 $this->assertEquals(null, $result['preferences'][0]['value']); 833 834 // Check that as admin we can retrieve all the preferences for any user. 835 $this->setAdminUser(); 836 $result = core_user_external::get_user_preferences('', $user->id); 837 $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); 838 $this->assertCount(0, $result['warnings']); 839 $this->assertCount(3, $result['preferences']); 840 841 foreach ($result['preferences'] as $pref) { 842 if ($pref['name'] === '_lastloaded') { 843 continue; 844 } 845 // Check we receive the expected preferences. 846 $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']); 847 } 848 849 // Check that as a non admin user we cannot retrieve other users preferences. 850 $anotheruser = self::getDataGenerator()->create_user(); 851 $this->setUser($anotheruser); 852 853 $this->setExpectedException('required_capability_exception'); 854 $result = core_user_external::get_user_preferences('', $user->id); 855 } 856 857 }
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 |