/user/tests/externallib_test.php

https://bitbucket.org/synergylearning/campusconnect · PHP · 754 lines · 549 code · 106 blank · 99 comment · 54 complexity · c68724b251eb5c671cafaa214ad16782 MD5 · raw file

  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. * User external PHPunit tests
  18. *
  19. * @package core_user
  20. * @category external
  21. * @copyright 2012 Jerome Mouneyrac
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 2.4
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28. require_once($CFG->dirroot . '/user/externallib.php');
  29. require_once($CFG->dirroot . '/files/externallib.php');
  30. class core_user_externallib_testcase extends externallib_advanced_testcase {
  31. /**
  32. * Test get_users
  33. */
  34. public function test_get_users() {
  35. global $USER, $CFG;
  36. $this->resetAfterTest(true);
  37. $course = self::getDataGenerator()->create_course();
  38. $user1 = array(
  39. 'username' => 'usernametest1',
  40. 'idnumber' => 'idnumbertest1',
  41. 'firstname' => 'First Name User Test 1',
  42. 'lastname' => 'Last Name User Test 1',
  43. 'email' => 'usertest1@email.com',
  44. 'address' => '2 Test Street Perth 6000 WA',
  45. 'phone1' => '01010101010',
  46. 'phone2' => '02020203',
  47. 'icq' => 'testuser1',
  48. 'skype' => 'testuser1',
  49. 'yahoo' => 'testuser1',
  50. 'aim' => 'testuser1',
  51. 'msn' => 'testuser1',
  52. 'department' => 'Department of user 1',
  53. 'institution' => 'Institution of user 1',
  54. 'description' => 'This is a description for user 1',
  55. 'descriptionformat' => FORMAT_MOODLE,
  56. 'city' => 'Perth',
  57. 'url' => 'http://moodle.org',
  58. 'country' => 'au'
  59. );
  60. $user1 = self::getDataGenerator()->create_user($user1);
  61. set_config('usetags', 1);
  62. require_once($CFG->dirroot . '/user/editlib.php');
  63. require_once($CFG->dirroot . '/tag/lib.php');
  64. $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
  65. useredit_update_interests($user1, $user1->interests);
  66. $user2 = self::getDataGenerator()->create_user(
  67. array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
  68. $generatedusers = array();
  69. $generatedusers[$user1->id] = $user1;
  70. $generatedusers[$user2->id] = $user2;
  71. $context = context_course::instance($course->id);
  72. $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
  73. // Enrol the users in the course.
  74. $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid);
  75. $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid);
  76. $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid);
  77. // call as admin and receive all possible fields.
  78. $this->setAdminUser();
  79. $searchparams = array(
  80. array('key' => 'invalidkey', 'value' => 'invalidkey'),
  81. array('key' => 'email', 'value' => $user1->email),
  82. array('key' => 'firstname', 'value' => $user1->firstname));
  83. // Call the external function.
  84. $result = core_user_external::get_users($searchparams);
  85. // We need to execute the return values cleaning process to simulate the web service server
  86. $result = external_api::clean_returnvalue(core_user_external::get_users_returns(), $result);
  87. // Check we retrieve the good total number of enrolled users + no error on capability.
  88. $expectedreturnedusers = 1;
  89. $returnedusers = $result['users'];
  90. $this->assertEquals($expectedreturnedusers, count($returnedusers));
  91. foreach($returnedusers as $returneduser) {
  92. $generateduser = ($returneduser['id'] == $USER->id) ?
  93. $USER : $generatedusers[$returneduser['id']];
  94. $this->assertEquals($generateduser->username, $returneduser['username']);
  95. if (!empty($generateduser->idnumber)) {
  96. $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
  97. }
  98. $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
  99. $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
  100. if ($generateduser->email != $USER->email) { // Don't check the tmp modified $USER email.
  101. $this->assertEquals($generateduser->email, $returneduser['email']);
  102. }
  103. if (!empty($generateduser->address)) {
  104. $this->assertEquals($generateduser->address, $returneduser['address']);
  105. }
  106. if (!empty($generateduser->phone1)) {
  107. $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
  108. }
  109. if (!empty($generateduser->phone2)) {
  110. $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
  111. }
  112. if (!empty($generateduser->icq)) {
  113. $this->assertEquals($generateduser->icq, $returneduser['icq']);
  114. }
  115. if (!empty($generateduser->skype)) {
  116. $this->assertEquals($generateduser->skype, $returneduser['skype']);
  117. }
  118. if (!empty($generateduser->yahoo)) {
  119. $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
  120. }
  121. if (!empty($generateduser->aim)) {
  122. $this->assertEquals($generateduser->aim, $returneduser['aim']);
  123. }
  124. if (!empty($generateduser->msn)) {
  125. $this->assertEquals($generateduser->msn, $returneduser['msn']);
  126. }
  127. if (!empty($generateduser->department)) {
  128. $this->assertEquals($generateduser->department, $returneduser['department']);
  129. }
  130. if (!empty($generateduser->institution)) {
  131. $this->assertEquals($generateduser->institution, $returneduser['institution']);
  132. }
  133. if (!empty($generateduser->description)) {
  134. $this->assertEquals($generateduser->description, $returneduser['description']);
  135. }
  136. if (!empty($generateduser->descriptionformat)) {
  137. $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']);
  138. }
  139. if (!empty($generateduser->city)) {
  140. $this->assertEquals($generateduser->city, $returneduser['city']);
  141. }
  142. if (!empty($generateduser->country)) {
  143. $this->assertEquals($generateduser->country, $returneduser['country']);
  144. }
  145. if (!empty($generateduser->url)) {
  146. $this->assertEquals($generateduser->url, $returneduser['url']);
  147. }
  148. if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
  149. $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
  150. }
  151. }
  152. // Test the invalid key warning.
  153. $warnings = $result['warnings'];
  154. $this->assertEquals(count($warnings), 1);
  155. $warning = array_pop($warnings);
  156. $this->assertEquals($warning['item'], 'invalidkey');
  157. $this->assertEquals($warning['warningcode'], 'invalidfieldparameter');
  158. // Test sending twice the same search field.
  159. try {
  160. $searchparams = array(
  161. array('key' => 'firstname', 'value' => 'Canard'),
  162. array('key' => 'email', 'value' => $user1->email),
  163. array('key' => 'firstname', 'value' => $user1->firstname));
  164. // Call the external function.
  165. $result = core_user_external::get_users($searchparams);
  166. $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
  167. } catch (moodle_exception $e) {
  168. $this->assertEquals('keyalreadyset', $e->errorcode);
  169. } catch (Exception $e) {
  170. $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
  171. }
  172. }
  173. /**
  174. * Test get_users_by_field
  175. */
  176. public function test_get_users_by_field() {
  177. global $USER, $CFG;
  178. $this->resetAfterTest(true);
  179. $course = self::getDataGenerator()->create_course();
  180. $user1 = array(
  181. 'username' => 'usernametest1',
  182. 'idnumber' => 'idnumbertest1',
  183. 'firstname' => 'First Name User Test 1',
  184. 'lastname' => 'Last Name User Test 1',
  185. 'email' => 'usertest1@email.com',
  186. 'address' => '2 Test Street Perth 6000 WA',
  187. 'phone1' => '01010101010',
  188. 'phone2' => '02020203',
  189. 'icq' => 'testuser1',
  190. 'skype' => 'testuser1',
  191. 'yahoo' => 'testuser1',
  192. 'aim' => 'testuser1',
  193. 'msn' => 'testuser1',
  194. 'department' => 'Department of user 1',
  195. 'institution' => 'Institution of user 1',
  196. 'description' => 'This is a description for user 1',
  197. 'descriptionformat' => FORMAT_MOODLE,
  198. 'city' => 'Perth',
  199. 'url' => 'http://moodle.org',
  200. 'country' => 'au'
  201. );
  202. $user1 = self::getDataGenerator()->create_user($user1);
  203. if (!empty($CFG->usetags)) {
  204. require_once($CFG->dirroot . '/user/editlib.php');
  205. require_once($CFG->dirroot . '/tag/lib.php');
  206. $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
  207. useredit_update_interests($user1, $user1->interests);
  208. }
  209. $user2 = self::getDataGenerator()->create_user(
  210. array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
  211. $generatedusers = array();
  212. $generatedusers[$user1->id] = $user1;
  213. $generatedusers[$user2->id] = $user2;
  214. $context = context_course::instance($course->id);
  215. $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
  216. // Enrol the users in the course.
  217. $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
  218. $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
  219. $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
  220. // call as admin and receive all possible fields.
  221. $this->setAdminUser();
  222. $fieldstosearch = array('id', 'idnumber', 'username', 'email');
  223. foreach ($fieldstosearch as $fieldtosearch) {
  224. // Call the external function.
  225. $returnedusers = core_user_external::get_users_by_field($fieldtosearch,
  226. array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch}));
  227. // Expected result differ following the searched field
  228. // Admin user in the PHPunit framework doesn't have email or idnumber.
  229. if ($fieldtosearch == 'email' or $fieldtosearch == 'idnumber') {
  230. $expectedreturnedusers = 2;
  231. } else {
  232. $expectedreturnedusers = 3;
  233. }
  234. // Check we retrieve the good total number of enrolled users + no error on capability.
  235. $this->assertEquals($expectedreturnedusers, count($returnedusers));
  236. foreach($returnedusers as $returneduser) {
  237. $generateduser = ($returneduser['id'] == $USER->id) ?
  238. $USER : $generatedusers[$returneduser['id']];
  239. $this->assertEquals($generateduser->username, $returneduser['username']);
  240. if (!empty($generateduser->idnumber)) {
  241. $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
  242. }
  243. $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
  244. $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
  245. if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email
  246. $this->assertEquals($generateduser->email, $returneduser['email']);
  247. }
  248. if (!empty($generateduser->address)) {
  249. $this->assertEquals($generateduser->address, $returneduser['address']);
  250. }
  251. if (!empty($generateduser->phone1)) {
  252. $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
  253. }
  254. if (!empty($generateduser->phone2)) {
  255. $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
  256. }
  257. if (!empty($generateduser->icq)) {
  258. $this->assertEquals($generateduser->icq, $returneduser['icq']);
  259. }
  260. if (!empty($generateduser->skype)) {
  261. $this->assertEquals($generateduser->skype, $returneduser['skype']);
  262. }
  263. if (!empty($generateduser->yahoo)) {
  264. $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
  265. }
  266. if (!empty($generateduser->aim)) {
  267. $this->assertEquals($generateduser->aim, $returneduser['aim']);
  268. }
  269. if (!empty($generateduser->msn)) {
  270. $this->assertEquals($generateduser->msn, $returneduser['msn']);
  271. }
  272. if (!empty($generateduser->department)) {
  273. $this->assertEquals($generateduser->department, $returneduser['department']);
  274. }
  275. if (!empty($generateduser->institution)) {
  276. $this->assertEquals($generateduser->institution, $returneduser['institution']);
  277. }
  278. if (!empty($generateduser->description)) {
  279. $this->assertEquals($generateduser->description, $returneduser['description']);
  280. }
  281. if (!empty($generateduser->descriptionformat) and isset($returneduser['descriptionformat'])) {
  282. $this->assertEquals($generateduser->descriptionformat, $returneduser['descriptionformat']);
  283. }
  284. if (!empty($generateduser->city)) {
  285. $this->assertEquals($generateduser->city, $returneduser['city']);
  286. }
  287. if (!empty($generateduser->country)) {
  288. $this->assertEquals($generateduser->country, $returneduser['country']);
  289. }
  290. if (!empty($generateduser->url)) {
  291. $this->assertEquals($generateduser->url, $returneduser['url']);
  292. }
  293. if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
  294. $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
  295. }
  296. }
  297. }
  298. // Test that no result are returned for search by username if we are not admin
  299. $this->setGuestUser();
  300. // Call the external function.
  301. $returnedusers = core_user_external::get_users_by_field('username',
  302. array($USER->username, $user1->username, $user2->username));
  303. // Only the own $USER username should be returned
  304. $this->assertEquals(1, count($returnedusers));
  305. // And finally test as one of the enrolled users.
  306. $this->setUser($user1);
  307. // Call the external function.
  308. $returnedusers = core_user_external::get_users_by_field('username',
  309. array($USER->username, $user1->username, $user2->username));
  310. // Only the own $USER username should be returned still.
  311. $this->assertEquals(1, count($returnedusers));
  312. }
  313. /**
  314. * Test get_course_user_profiles
  315. */
  316. public function test_get_course_user_profiles() {
  317. global $USER, $CFG;
  318. $this->resetAfterTest(true);
  319. $course = self::getDataGenerator()->create_course();
  320. $user1 = array(
  321. 'username' => 'usernametest1',
  322. 'idnumber' => 'idnumbertest1',
  323. 'firstname' => 'First Name User Test 1',
  324. 'lastname' => 'Last Name User Test 1',
  325. 'email' => 'usertest1@email.com',
  326. 'address' => '2 Test Street Perth 6000 WA',
  327. 'phone1' => '01010101010',
  328. 'phone2' => '02020203',
  329. 'icq' => 'testuser1',
  330. 'skype' => 'testuser1',
  331. 'yahoo' => 'testuser1',
  332. 'aim' => 'testuser1',
  333. 'msn' => 'testuser1',
  334. 'department' => 'Department of user 1',
  335. 'institution' => 'Institution of user 1',
  336. 'description' => 'This is a description for user 1',
  337. 'descriptionformat' => FORMAT_MOODLE,
  338. 'city' => 'Perth',
  339. 'url' => 'http://moodle.org',
  340. 'country' => 'au'
  341. );
  342. $user1 = self::getDataGenerator()->create_user($user1);
  343. if (!empty($CFG->usetags)) {
  344. require_once($CFG->dirroot . '/user/editlib.php');
  345. require_once($CFG->dirroot . '/tag/lib.php');
  346. $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
  347. useredit_update_interests($user1, $user1->interests);
  348. }
  349. $user2 = self::getDataGenerator()->create_user();
  350. $context = context_course::instance($course->id);
  351. $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
  352. // Enrol the users in the course.
  353. $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
  354. $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
  355. $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
  356. // Call the external function.
  357. $enrolledusers = core_user_external::get_course_user_profiles(array(
  358. array('userid' => $USER->id, 'courseid' => $course->id),
  359. array('userid' => $user1->id, 'courseid' => $course->id),
  360. array('userid' => $user2->id, 'courseid' => $course->id)));
  361. // We need to execute the return values cleaning process to simulate the web service server.
  362. $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
  363. // Check we retrieve the good total number of enrolled users + no error on capability.
  364. $this->assertEquals(3, count($enrolledusers));
  365. // Do the same call as admin to receive all possible fields.
  366. $this->setAdminUser();
  367. $USER->email = "admin@fakeemail.com";
  368. // Call the external function.
  369. $enrolledusers = core_user_external::get_course_user_profiles(array(
  370. array('userid' => $USER->id, 'courseid' => $course->id),
  371. array('userid' => $user1->id, 'courseid' => $course->id),
  372. array('userid' => $user2->id, 'courseid' => $course->id)));
  373. // We need to execute the return values cleaning process to simulate the web service server.
  374. $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
  375. foreach($enrolledusers as $enrolleduser) {
  376. if ($enrolleduser['username'] == $user1->username) {
  377. $this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
  378. $this->assertEquals($user1->firstname, $enrolleduser['firstname']);
  379. $this->assertEquals($user1->lastname, $enrolleduser['lastname']);
  380. $this->assertEquals($user1->email, $enrolleduser['email']);
  381. $this->assertEquals($user1->address, $enrolleduser['address']);
  382. $this->assertEquals($user1->phone1, $enrolleduser['phone1']);
  383. $this->assertEquals($user1->phone2, $enrolleduser['phone2']);
  384. $this->assertEquals($user1->icq, $enrolleduser['icq']);
  385. $this->assertEquals($user1->skype, $enrolleduser['skype']);
  386. $this->assertEquals($user1->yahoo, $enrolleduser['yahoo']);
  387. $this->assertEquals($user1->aim, $enrolleduser['aim']);
  388. $this->assertEquals($user1->msn, $enrolleduser['msn']);
  389. $this->assertEquals($user1->department, $enrolleduser['department']);
  390. $this->assertEquals($user1->institution, $enrolleduser['institution']);
  391. $this->assertEquals($user1->description, $enrolleduser['description']);
  392. $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
  393. $this->assertEquals($user1->city, $enrolleduser['city']);
  394. $this->assertEquals($user1->country, $enrolleduser['country']);
  395. $this->assertEquals($user1->url, $enrolleduser['url']);
  396. if (!empty($CFG->usetags)) {
  397. $this->assertEquals(implode(', ', $user1->interests), $enrolleduser['interests']);
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * Test create_users
  404. */
  405. public function test_create_users() {
  406. global $USER, $CFG, $DB;
  407. $this->resetAfterTest(true);
  408. $user1 = array(
  409. 'username' => 'usernametest1',
  410. 'password' => 'Moodle2012!',
  411. 'idnumber' => 'idnumbertest1',
  412. 'firstname' => 'First Name User Test 1',
  413. 'lastname' => 'Last Name User Test 1',
  414. 'middlename' => 'Middle Name User Test 1',
  415. 'lastnamephonetic' => '最後のお名前のテスト一号',
  416. 'firstnamephonetic' => 'お名前のテスト一号',
  417. 'alternatename' => 'Alternate Name User Test 1',
  418. 'email' => 'usertest1@email.com',
  419. 'description' => 'This is a description for user 1',
  420. 'city' => 'Perth',
  421. 'country' => 'au'
  422. );
  423. $context = context_system::instance();
  424. $roleid = $this->assignUserCapability('moodle/user:create', $context->id);
  425. // Call the external function.
  426. $createdusers = core_user_external::create_users(array($user1));
  427. // We need to execute the return values cleaning process to simulate the web service server.
  428. $createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers);
  429. // Check we retrieve the good total number of created users + no error on capability.
  430. $this->assertEquals(1, count($createdusers));
  431. foreach($createdusers as $createduser) {
  432. $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
  433. $this->assertEquals($dbuser->username, $user1['username']);
  434. $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
  435. $this->assertEquals($dbuser->firstname, $user1['firstname']);
  436. $this->assertEquals($dbuser->lastname, $user1['lastname']);
  437. $this->assertEquals($dbuser->email, $user1['email']);
  438. $this->assertEquals($dbuser->description, $user1['description']);
  439. $this->assertEquals($dbuser->city, $user1['city']);
  440. $this->assertEquals($dbuser->country, $user1['country']);
  441. }
  442. // Call without required capability
  443. $this->unassignUserCapability('moodle/user:create', $context->id, $roleid);
  444. $this->setExpectedException('required_capability_exception');
  445. $createdusers = core_user_external::create_users(array($user1));
  446. }
  447. /**
  448. * Test delete_users
  449. */
  450. public function test_delete_users() {
  451. global $USER, $CFG, $DB;
  452. $this->resetAfterTest(true);
  453. $user1 = self::getDataGenerator()->create_user();
  454. $user2 = self::getDataGenerator()->create_user();
  455. // Check the users were correctly created.
  456. $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
  457. array('userid1' => $user1->id, 'userid2' => $user2->id)));
  458. $context = context_system::instance();
  459. $roleid = $this->assignUserCapability('moodle/user:delete', $context->id);
  460. // Call the external function.
  461. core_user_external::delete_users(array($user1->id, $user2->id));
  462. // Check we retrieve no users + no error on capability.
  463. $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
  464. array('userid1' => $user1->id, 'userid2' => $user2->id)));
  465. // Call without required capability.
  466. $this->unassignUserCapability('moodle/user:delete', $context->id, $roleid);
  467. $this->setExpectedException('required_capability_exception');
  468. core_user_external::delete_users(array($user1->id, $user2->id));
  469. }
  470. /**
  471. * Test get_users_by_id
  472. */
  473. public function test_get_users_by_id() {
  474. global $USER, $CFG;
  475. $this->resetAfterTest(true);
  476. $user1 = array(
  477. 'username' => 'usernametest1',
  478. 'idnumber' => 'idnumbertest1',
  479. 'firstname' => 'First Name User Test 1',
  480. 'lastname' => 'Last Name User Test 1',
  481. 'email' => 'usertest1@email.com',
  482. 'address' => '2 Test Street Perth 6000 WA',
  483. 'phone1' => '01010101010',
  484. 'phone2' => '02020203',
  485. 'icq' => 'testuser1',
  486. 'skype' => 'testuser1',
  487. 'yahoo' => 'testuser1',
  488. 'aim' => 'testuser1',
  489. 'msn' => 'testuser1',
  490. 'department' => 'Department of user 1',
  491. 'institution' => 'Institution of user 1',
  492. 'description' => 'This is a description for user 1',
  493. 'descriptionformat' => FORMAT_MOODLE,
  494. 'city' => 'Perth',
  495. 'url' => 'http://moodle.org',
  496. 'country' => 'au'
  497. );
  498. $user1 = self::getDataGenerator()->create_user($user1);
  499. if (!empty($CFG->usetags)) {
  500. require_once($CFG->dirroot . '/user/editlib.php');
  501. require_once($CFG->dirroot . '/tag/lib.php');
  502. $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
  503. useredit_update_interests($user1, $user1->interests);
  504. }
  505. $user2 = self::getDataGenerator()->create_user();
  506. $context = context_system::instance();
  507. $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
  508. // Call the external function.
  509. $returnedusers = core_user_external::get_users_by_id(array(
  510. $USER->id, $user1->id, $user2->id));
  511. // We need to execute the return values cleaning process to simulate the web service server.
  512. $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);
  513. // Check we retrieve the good total number of enrolled users + no error on capability.
  514. $this->assertEquals(3, count($returnedusers));
  515. // Do the same call as admin to receive all possible fields.
  516. $this->setAdminUser();
  517. $USER->email = "admin@fakeemail.com";
  518. // Call the external function.
  519. $returnedusers = core_user_external::get_users_by_id(array(
  520. $USER->id, $user1->id, $user2->id));
  521. // We need to execute the return values cleaning process to simulate the web service server.
  522. $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);
  523. foreach($returnedusers as $enrolleduser) {
  524. if ($enrolleduser['username'] == $user1->username) {
  525. $this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
  526. $this->assertEquals($user1->firstname, $enrolleduser['firstname']);
  527. $this->assertEquals($user1->lastname, $enrolleduser['lastname']);
  528. $this->assertEquals($user1->email, $enrolleduser['email']);
  529. $this->assertEquals($user1->address, $enrolleduser['address']);
  530. $this->assertEquals($user1->phone1, $enrolleduser['phone1']);
  531. $this->assertEquals($user1->phone2, $enrolleduser['phone2']);
  532. $this->assertEquals($user1->icq, $enrolleduser['icq']);
  533. $this->assertEquals($user1->skype, $enrolleduser['skype']);
  534. $this->assertEquals($user1->yahoo, $enrolleduser['yahoo']);
  535. $this->assertEquals($user1->aim, $enrolleduser['aim']);
  536. $this->assertEquals($user1->msn, $enrolleduser['msn']);
  537. $this->assertEquals($user1->department, $enrolleduser['department']);
  538. $this->assertEquals($user1->institution, $enrolleduser['institution']);
  539. $this->assertEquals($user1->description, $enrolleduser['description']);
  540. $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
  541. $this->assertEquals($user1->city, $enrolleduser['city']);
  542. $this->assertEquals($user1->country, $enrolleduser['country']);
  543. $this->assertEquals($user1->url, $enrolleduser['url']);
  544. if (!empty($CFG->usetags)) {
  545. $this->assertEquals(implode(', ', $user1->interests), $enrolleduser['interests']);
  546. }
  547. }
  548. }
  549. }
  550. /**
  551. * Test update_users
  552. */
  553. public function test_update_users() {
  554. global $USER, $CFG, $DB;
  555. $this->resetAfterTest(true);
  556. $user1 = self::getDataGenerator()->create_user();
  557. $user1 = array(
  558. 'id' => $user1->id,
  559. 'username' => 'usernametest1',
  560. 'password' => 'Moodle2012!',
  561. 'idnumber' => 'idnumbertest1',
  562. 'firstname' => 'First Name User Test 1',
  563. 'lastname' => 'Last Name User Test 1',
  564. 'middlename' => 'Middle Name User Test 1',
  565. 'lastnamephonetic' => '最後のお名前のテスト一号',
  566. 'firstnamephonetic' => 'お名前のテスト一号',
  567. 'alternatename' => 'Alternate Name User Test 1',
  568. 'email' => 'usertest1@email.com',
  569. 'description' => 'This is a description for user 1',
  570. 'city' => 'Perth',
  571. 'country' => 'au'
  572. );
  573. $context = context_system::instance();
  574. $roleid = $this->assignUserCapability('moodle/user:update', $context->id);
  575. // Call the external function.
  576. core_user_external::update_users(array($user1));
  577. $dbuser = $DB->get_record('user', array('id' => $user1['id']));
  578. $this->assertEquals($dbuser->username, $user1['username']);
  579. $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
  580. $this->assertEquals($dbuser->firstname, $user1['firstname']);
  581. $this->assertEquals($dbuser->lastname, $user1['lastname']);
  582. $this->assertEquals($dbuser->email, $user1['email']);
  583. $this->assertEquals($dbuser->description, $user1['description']);
  584. $this->assertEquals($dbuser->city, $user1['city']);
  585. $this->assertEquals($dbuser->country, $user1['country']);
  586. // Call without required capability.
  587. $this->unassignUserCapability('moodle/user:update', $context->id, $roleid);
  588. $this->setExpectedException('required_capability_exception');
  589. core_user_external::update_users(array($user1));
  590. }
  591. /**
  592. * Test add_user_private_files
  593. */
  594. public function test_add_user_private_files() {
  595. global $USER, $CFG, $DB;
  596. $this->resetAfterTest(true);
  597. $context = context_system::instance();
  598. $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id);
  599. $context = context_user::instance($USER->id);
  600. $contextid = $context->id;
  601. $component = "user";
  602. $filearea = "draft";
  603. $itemid = 0;
  604. $filepath = "/";
  605. $filename = "Simple.txt";
  606. $filecontent = base64_encode("Let us create a nice simple file");
  607. $contextlevel = null;
  608. $instanceid = null;
  609. $browser = get_file_browser();
  610. // Call the files api to create a file.
  611. $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
  612. $filename, $filecontent, $contextlevel, $instanceid);
  613. $draftid = $draftfile['itemid'];
  614. // Make sure the file was created.
  615. $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
  616. $this->assertNotEmpty($file);
  617. // Make sure the file does not exist in the user private files.
  618. $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
  619. $this->assertEmpty($file);
  620. // Call the external function.
  621. core_user_external::add_user_private_files($draftid);
  622. // Make sure the file was added to the user private files.
  623. $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
  624. $this->assertNotEmpty($file);
  625. }
  626. /**
  627. * Test add user device
  628. */
  629. public function test_add_user_device() {
  630. global $USER, $CFG, $DB;
  631. $this->resetAfterTest(true);
  632. $device = array(
  633. 'appid' => 'com.moodle.moodlemobile',
  634. 'name' => 'occam',
  635. 'model' => 'Nexus 4',
  636. 'platform' => 'Android',
  637. 'version' => '4.2.2',
  638. 'pushid' => 'apushdkasdfj4835',
  639. 'uuid' => 'asdnfl348qlksfaasef859'
  640. );
  641. // Call the external function.
  642. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
  643. $device['version'], $device['pushid'], $device['uuid']);
  644. $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
  645. $created = (array) $created;
  646. $this->assertEquals($device, array_intersect_key((array)$created, $device));
  647. }
  648. }