webservice MDL-21658 add moodle_user_update_users() unit test function
[moodle.git] / webservice / simpletest / testwebservice.php
CommitLineData
75681c41 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Test the different web service protocols.
20 *
21 * @author jerome@moodle.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
23 * @package web service
24 */
25if (!defined('MOODLE_INTERNAL')) {
6bb31e40 26 /// It must be included from a Moodle page
27 die('Direct access to this script is forbidden.');
75681c41 28}
29
30/**
31 * How to configure this unit tests:
32 * 0- Enable the web service you wish to test in the Moodle administration
33 * 1- Create a service with all functions in the Moodle administration
6bb31e40 34 * 2- Create a token associate this service and to an admin (or a user with all required capabilities)
75681c41 35 * 3- Configure setUp() function:
36 * a- write the token
37 * b- activate the protocols you wish to test
38 * c- activate the functions you wish to test (readonlytests and writetests arrays)
39 * d- set the number of time the web services are run
40 * Do not run WRITE test function on a production site as they impact the DB (even though every
41 * test should clean the modified data)
42 *
43 * How to write a new function:
44 * 1- Add the function name to the array readonlytests/writetests
45 * 2- Set it as false when you commit!
46 * 3- write the function - Do not prefix the function name by 'test'
47 */
48class webservice_test extends UnitTestCase {
49
50 public $testtoken;
51 public $testrest;
52 public $testxmlrpc;
53 public $testsoap;
54 public $timerrest;
55 public $timerxmlrpc;
56 public $timersoap;
57 public $readonlytests;
58 public $writetests;
59
60 function setUp() {
61 //token to test
6bb31e40 62 $this->testtoken = '72d338d58ff881cc293f8cd1d96d7a57';
75681c41 63
64 //protocols to test
65 $this->testrest = false; //Does not work till XML => PHP is implemented (MDL-22965)
66 $this->testxmlrpc = false;
67 $this->testsoap = false;
68
6bb31e40 69 ////// READ-ONLY DB tests ////
75681c41 70 $this->readonlytests = array(
6bb31e40 71 'moodle_group_get_groups' => false,
72 'moodle_course_get_courses' => false,
73 'moodle_user_get_users_by_id' => false,
b02b5fb7 74 'moodle_enrol_get_enrolled_users' => false
75681c41 75 );
76
6bb31e40 77 ////// WRITE DB tests ////
75681c41 78 $this->writetests = array(
6bb31e40 79 'moodle_user_create_users' => false,
80 'moodle_course_create_courses' => false,
a9f07c41 81 'moodle_user_delete_users' => false,
82 'moodle_user_update_users' => false
75681c41 83 );
84
85 //performance testing: number of time the web service are run
86 $this->iteration = 1;
87
88 //DO NOT CHANGE
89 //reset the timers
90 $this->timerrest = 0;
91 $this->timerxmlrpc = 0;
92 $this->timersoap = 0;
93 }
94
95 function testRun() {
96 global $CFG;
97
98 if (!empty($this->testtoken)) {
99
100 //Does not work till XML => PHP is implemented (MDL-22965)
101 if ($this->testrest) {
102
103 $this->timerrest = time();
104
105 require_once($CFG->dirroot . "/webservice/rest/lib.php");
106 $restclient = new webservice_rest_client($CFG->wwwroot
107 . '/webservice/rest/server.php', $this->testtoken);
108
109 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
110 foreach ($this->readonlytests as $functioname => $run) {
111 if ($run) {
112 //$this->{$functioname}($restclient);
113 }
114 }
115 foreach ($this->writetests as $functioname => $run) {
116 if ($run) {
117 //$this->{$functioname}($restclient);
118 }
119 }
120 }
121
122 $this->timerrest = time() - $this->timerrest;
123 //here you could call a log function to display the timer
124 //example:
125 //error_log('REST time: ');
126 //error_log(print_r($this->timerrest));
127 }
128
129 if ($this->testxmlrpc) {
130
131 $this->timerxmlrpc = time();
132
133 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
134 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
135 . '/webservice/xmlrpc/server.php', $this->testtoken);
136
137 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
138 foreach ($this->readonlytests as $functioname => $run) {
139 if ($run) {
140 $this->{$functioname}($xmlrpcclient);
141 }
142 }
143 foreach ($this->writetests as $functioname => $run) {
144 if ($run) {
145 $this->{$functioname}($xmlrpcclient);
146 }
147 }
148 }
149
150 $this->timerxmlrpc = time() - $this->timerxmlrpc;
151 //here you could call a log function to display the timer
152 //example:
153 //error_log('XML-RPC time: ');
154 //error_log(print_r($this->timerxmlrpc));
155 }
156
157 if ($this->testsoap) {
158
159 $this->timersoap = time();
160
161 require_once($CFG->dirroot . "/webservice/soap/lib.php");
162 $soapclient = new webservice_soap_client($CFG->wwwroot
d1d0788d 163 . '/webservice/soap/server.php', $this->testtoken,
164 array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //force SOAP synchronous mode
165 //when function return null
75681c41 166 $soapclient->setWsdlCache(false);
167
168 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
169 foreach ($this->readonlytests as $functioname => $run) {
170 if ($run) {
171 $this->{$functioname}($soapclient);
172 }
173 }
174 foreach ($this->writetests as $functioname => $run) {
175 if ($run) {
176 $this->{$functioname}($soapclient);
177 }
178 }
179 }
180
181 $this->timersoap = time() - $this->timersoap;
182 //here you could call a log function to display the timer
183 //example:
184 //error_log('SOAP time: ');
185 //error_log(print_r($this->timersoap));
186 }
187 }
188 }
189
190 ///// WEB SERVICE TEST FUNCTIONS
191
192 function moodle_group_get_groups($client) {
193 global $DB;
194 $dbgroups = $DB->get_records('groups');
195 $groupids = array();
196 foreach ($dbgroups as $dbgroup) {
197 $groupids[] = $dbgroup->id;
198 }
199 $function = 'moodle_group_get_groups';
200
201 $params = array('groupids' => $groupids);
202 $groups = $client->call($function, $params);
203 $this->assertEqual(count($groups), count($groupids));
204 }
205
6bb31e40 206 function moodle_user_get_users_by_id($client) {
207 global $DB;
d1d0788d 208 $dbusers = $DB->get_records('user', array('deleted' => 0));
6bb31e40 209 $userids = array();
210 foreach ($dbusers as $dbuser) {
211 $userids[] = $dbuser->id;
212 }
213 $function = 'moodle_user_get_users_by_id';
214
215 $params = array('userids' => $userids);
216 $users = $client->call($function, $params);
217
218 $this->assertEqual(count($users), count($userids));
219 }
220
b02b5fb7 221 function moodle_enrol_get_enrolled_users($client) {
222 global $DB;
223
224 //function settings
225 $withcapability = '';
226 $groupid = null;
227 $onlyactive = false;
228
229 $dbcourses = $DB->get_records('course');
230 $function = 'moodle_enrol_get_enrolled_users';
231
232 foreach ($dbcourses as $dbcourse) {
233
234 $params = array();
235
236 $coursecontext = get_context_instance(CONTEXT_COURSE, $dbcourse->id);
237
238 list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
239 $sql = "SELECT DISTINCT ue.userid, e.courseid
240 FROM {user_enrolments} ue
241 JOIN {enrol} e ON (e.id = ue.enrolid)
242 WHERE e.courseid = :courseid AND ue.userid IN ($sql)";
243
244 $params['courseid'] = $dbcourse->id;
245
246 $enrolledusers = $DB->get_records_sql($sql, $params);
247
248 $wsparams = array('courseid' => $dbcourse->id, 'withcapability' => $withcapability,
249 'groupid' => $groupid, 'onlyactive' => $onlyactive);
250 $resultusers = $client->call($function, $wsparams);
251
252 $this->assertEqual(count($resultusers), count($enrolledusers));
253 }
254 }
255
6bb31e40 256 function moodle_course_get_courses($client) {
75681c41 257 global $DB;
6bb31e40 258
259 $function = 'moodle_course_get_courses';
260
261 //retrieve all courses from db
262 $dbcourses = $DB->get_records('course');
263 $courseids = array();
264 foreach ($dbcourses as $dbcourse) {
265 $courseids[] = $dbcourse->id;
266 }
267
268 //retrieve all courses by id
269 $params = array('options' => array('ids' => $courseids));
270 $courses = $client->call($function, $params);
271
272 //check it is the same course count
273 $this->assertEqual(count($courses), count($courseids));
274
275 //check all course values are identic
276 foreach ($courses as $course) {
277 $this->assertEqual($course['fullname'],
278 $dbcourses[$course['id']]->fullname);
279 $this->assertEqual($course['shortname'],
280 $dbcourses[$course['id']]->shortname);
281 $this->assertEqual($course['categoryid'],
282 $dbcourses[$course['id']]->category);
283 $this->assertEqual($course['categorysortorder'],
284 $dbcourses[$course['id']]->sortorder);
285 $this->assertEqual($course['idnumber'],
286 $dbcourses[$course['id']]->idnumber);
287 $this->assertEqual($course['summary'],
288 $dbcourses[$course['id']]->summary);
289 $this->assertEqual($course['summaryformat'],
290 $dbcourses[$course['id']]->summaryformat);
291 $this->assertEqual($course['format'],
292 $dbcourses[$course['id']]->format);
293 $this->assertEqual($course['showgrades'],
294 $dbcourses[$course['id']]->showgrades);
295 $this->assertEqual($course['newsitems'],
296 $dbcourses[$course['id']]->newsitems);
297 $this->assertEqual($course['startdate'],
298 $dbcourses[$course['id']]->startdate);
299 $this->assertEqual($course['numsections'],
300 $dbcourses[$course['id']]->numsections);
301 $this->assertEqual($course['maxbytes'],
302 $dbcourses[$course['id']]->maxbytes);
303 $this->assertEqual($course['visible'],
304 $dbcourses[$course['id']]->visible);
305 $this->assertEqual($course['hiddensections'],
306 $dbcourses[$course['id']]->hiddensections);
307 $this->assertEqual($course['groupmode'],
308 $dbcourses[$course['id']]->groupmode);
309 $this->assertEqual($course['groupmodeforce'],
310 $dbcourses[$course['id']]->groupmodeforce);
311 $this->assertEqual($course['defaultgroupingid'],
312 $dbcourses[$course['id']]->defaultgroupingid);
313 $this->assertEqual($course['lang'],
314 $dbcourses[$course['id']]->lang);
315 $this->assertEqual($course['timecreated'],
316 $dbcourses[$course['id']]->timecreated);
317 $this->assertEqual($course['timemodified'],
318 $dbcourses[$course['id']]->timemodified);
319 if (key_exists('enablecompletion', $course)) {
320 $this->assertEqual($course['enablecompletion'],
321 $dbcourses[$course['id']]->enablecompletion);
322 }
323 if (key_exists('completionstartonenrol', $course)) {
324 $this->assertEqual($course['completionstartonenrol'],
325 $dbcourses[$course['id']]->completionstartonenrol);
326 }
327 if (key_exists('completionnotify', $course)) {
328 $this->assertEqual($course['completionnotify'],
329 $dbcourses[$course['id']]->completionnotify);
330 }
331 $this->assertEqual($course['forcetheme'],
332 $dbcourses[$course['id']]->theme);
333 }
334 }
335
336 function moodle_course_create_courses($client) {
337 global $DB, $CFG;
338
339 ///Test data
340 $courseconfig = get_config('moodlecourse');
341
342 $themeobjects = get_list_of_themes();
343 $theme = key($themeobjects);
344 $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
b02b5fb7 345 $categoryid = empty($categoryid) ? 0 : $categoryid;
6bb31e40 346
347 $course1 = new stdClass();
348 $course1->fullname = 'Test Data create course 1';
349 $course1->shortname = 'testdatacourse1';
350 $course1->categoryid = $categoryid;
351 $course1->idnumber = '328327982372342343234';
352 $course1->summary = 'This is a summary';
353 $course1->summaryformat = FORMAT_HTML;
354 $course1->format = $courseconfig->format;
355 $course1->showgrades = $courseconfig->showgrades;
356 $course1->showreports = $courseconfig->showreports;
357 $course1->newsitems = $courseconfig->newsitems;
358 $course1->startdate = time();
359 $course1->numsections = $courseconfig->numsections;
360 $course1->maxbytes = $courseconfig->maxbytes;
361 $course1->visible = $courseconfig->visible;
362 $course1->hiddensections = $courseconfig->hiddensections;
363 $course1->groupmode = $courseconfig->groupmode;
364 $course1->groupmodeforce = $courseconfig->groupmodeforce;
365 $course1->defaultgroupingid = 0;
366 if (!empty($courseconfig->lang)) {
367 $course1->lang = $courseconfig->lang;
368 }
369 $course1->enablecompletion = $courseconfig->enablecompletion;
370 $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
371 $course1->completionnotify = 0;
372 $course1->forcetheme = $theme;
373
374 $course2 = new stdClass();
375 $course2->fullname = 'Test Data create course 2';
376 $course2->shortname = 'testdatacourse2';
377 $course2->categoryid = $categoryid;
378
379 $courses = array($course1, $course2);
380
381 //do not run the test if course1 or course2 already exists
382 $existingcourses = $DB->get_records_list('course', 'fullname',
383 array($course1->fullname, $course2->fullname));
384 if (!empty($existingcourses)) {
385 throw new moodle_exception('testdatacoursesalreadyexist');
386 }
387
388 $function = 'moodle_course_create_courses';
389 $params = array('courses' => $courses);
390 $resultcourses = $client->call($function, $params);
391 $this->assertEqual(count($courses), count($resultcourses));
392
393 //retrieve user1 from the DB and check values
394 $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
395 $this->assertEqual($dbcourse1->fullname, $course1->fullname);
396 $this->assertEqual($dbcourse1->shortname, $course1->shortname);
397 $this->assertEqual($dbcourse1->category, $course1->categoryid);
398 $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
399 $this->assertEqual($dbcourse1->summary, $course1->summary);
400 $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
401 $this->assertEqual($dbcourse1->format, $course1->format);
402 $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
403 $this->assertEqual($dbcourse1->showreports, $course1->showreports);
404 $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
405 $this->assertEqual($dbcourse1->startdate, $course1->startdate);
406 $this->assertEqual($dbcourse1->numsections, $course1->numsections);
407 $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
408 $this->assertEqual($dbcourse1->visible, $course1->visible);
409 $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
410 $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
411 $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
412 $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
413 if (!empty($courseconfig->lang)) {
414 $this->assertEqual($dbcourse1->lang, $course1->lang);
415 }
416 if (completion_info::is_enabled_for_site()) {
417 $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
418 $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
75681c41 419 }
6bb31e40 420 $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
421 if (!empty($CFG->allowcoursethemes)) {
422 $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
423 }
424
425 //retrieve user2 from the DB and check values
426 $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
427 $this->assertEqual($dbcourse2->fullname, $course2->fullname);
428 $this->assertEqual($dbcourse2->shortname, $course2->shortname);
b02b5fb7 429 $this->assertEqual($dbcourse2->category, $course2->categoryid);
6bb31e40 430 $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
431 $this->assertEqual($dbcourse2->format, $courseconfig->format);
432 $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
433 $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
434 $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
435 $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
436 $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
437 $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
438 $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
439 $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
440 $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
441 $this->assertEqual($dbcourse2->defaultgroupingid, 0);
442
443 //delete users from DB
444 $DB->delete_records_list('course', 'id',
445 array($dbcourse1->id, $dbcourse2->id));
446 }
447
448 function moodle_user_create_users($client) {
449 global $DB, $CFG;
450
451 //Test data
452 //a full user: user1
453 $user1 = new stdClass();
454 $user1->username = 'testusername1';
455 $user1->password = 'testpassword1';
456 $user1->firstname = 'testfirstname1';
457 $user1->lastname = 'testlastname1';
458 $user1->email = 'testemail1@moodle.com';
459 $user1->auth = 'manual';
460 $user1->idnumber = 'testidnumber1';
6bb31e40 461 $user1->lang = 'en';
462 $user1->theme = 'standard';
463 $user1->timezone = 99;
464 $user1->mailformat = 0;
465 $user1->description = 'Hello World!';
466 $user1->city = 'testcity1';
467 $user1->country = 'au';
468 $preferencename1 = 'preference1';
469 $preferencename2 = 'preference2';
470 $user1->preferences = array(
471 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
472 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
473 $customfieldname1 = 'testdatacustom1';
474 $customfieldname2 = 'testdatacustom2';
475 $user1->customfields = array(
476 array('type' => $customfieldname1, 'value' => 'customvalue'),
477 array('type' => $customfieldname2, 'value' => 'customvalue2'));
478 //a small user: user2
479 $user2 = new stdClass();
480 $user2->username = 'testusername2';
481 $user2->password = 'testpassword2';
482 $user2->firstname = 'testfirstname2';
483 $user2->lastname = 'testlastname2';
484 $user2->email = 'testemail1@moodle.com';
485
486 $users = array($user1, $user2);
487
488 //do not run the test if user1 or user2 already exists
489 $existingusers = $DB->get_records_list('user', 'username',
490 array($user1->username, $user2->username));
491 if (!empty($existingusers)) {
492 throw new moodle_exception('testdatausersalreadyexist');
493 }
494
495 //do not run the test if data test custom fields already exists
496 $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
497 array($customfieldname1, $customfieldname2));
498 if (!empty($existingcustomfields)) {
499 throw new moodle_exception('testdatacustomfieldsalreadyexist');
500 }
501
502 //create the custom fields
503 $customfield = new stdClass();
504 $customfield->shortname = $customfieldname1;
505 $customfield->name = $customfieldname1;
506 $customfield->datatype = 'text';
507 $DB->insert_record('user_info_field', $customfield);
508 $customfield = new stdClass();
509 $customfield->shortname = $customfieldname2;
510 $customfield->name = $customfieldname2;
511 $customfield->datatype = 'text';
512 $DB->insert_record('user_info_field', $customfield);
513
514 $function = 'moodle_user_create_users';
515 $params = array('users' => $users);
516 $resultusers = $client->call($function, $params);
517 $this->assertEqual(count($users), count($resultusers));
518
519 //retrieve user1 from the DB and check values
520 $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
521 $this->assertEqual($dbuser1->firstname, $user1->firstname);
522 $this->assertEqual($dbuser1->password,
523 hash_internal_user_password($user1->password));
524 $this->assertEqual($dbuser1->lastname, $user1->lastname);
525 $this->assertEqual($dbuser1->email, $user1->email);
526 $this->assertEqual($dbuser1->auth, $user1->auth);
527 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
6bb31e40 528 $this->assertEqual($dbuser1->lang, $user1->lang);
529 $this->assertEqual($dbuser1->theme, $user1->theme);
530 $this->assertEqual($dbuser1->timezone, $user1->timezone);
531 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
532 $this->assertEqual($dbuser1->description, $user1->description);
533 $this->assertEqual($dbuser1->city, $user1->city);
534 $this->assertEqual($dbuser1->country, $user1->country);
535 $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
536 null, $dbuser1->id);
537 $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
538 $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
539 null, $dbuser1->id);
540 $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
541 require_once($CFG->dirroot . "/user/profile/lib.php");
542 $customfields = profile_user_record($dbuser1->id);
543
544 $customfields = (array) $customfields;
545 $customfieldname1 = $user1->customfields[0]['type'];
546 $customfieldname2 = $user1->customfields[1]['type'];
547 $this->assertEqual($customfields[$customfieldname1],
548 $user1->customfields[0]['value']);
549 $this->assertEqual($customfields[$customfieldname2],
550 $user1->customfields[1]['value']);
551
552
553 //retrieve user2 from the DB and check values
554 $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
555 $this->assertEqual($dbuser2->firstname, $user2->firstname);
556 $this->assertEqual($dbuser2->password,
557 hash_internal_user_password($user2->password));
558 $this->assertEqual($dbuser2->lastname, $user2->lastname);
559 $this->assertEqual($dbuser2->email, $user2->email);
560
561 //unset preferences
562 $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
563
564 //clear custom fields data
565 $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
566
567 //delete custom fields
568 $DB->delete_records_list('user_info_field', 'shortname',
569 array($customfieldname1, $customfieldname2));
570
571 //delete users from DB
572 $DB->delete_records_list('user', 'id',
573 array($dbuser1->id, $dbuser2->id));
75681c41 574 }
575
d1d0788d 576 function moodle_user_delete_users($client) {
577 global $DB, $CFG;
578
579 //Set test data
580 //a full user: user1
581 $user1 = new stdClass();
582 $user1->username = 'veryimprobabletestusername1';
583 $user1->password = 'testpassword1';
584 $user1->firstname = 'testfirstname1';
585 $user1->lastname = 'testlastname1';
586 $user1->email = 'testemail1@moodle.com';
587 $user1->auth = 'manual';
588 $user1->idnumber = 'testidnumber1';
589 $user1->lang = 'en';
590 $user1->theme = 'standard';
591 $user1->timezone = 99;
592 $user1->mailformat = 0;
593 $user1->description = 'Hello World!';
594 $user1->city = 'testcity1';
595 $user1->country = 'au';
596 $preferencename1 = 'preference1';
597 $preferencename2 = 'preference2';
598 $user1->preferences = array(
599 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
600 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
601 $customfieldname1 = 'testdatacustom1';
602 $customfieldname2 = 'testdatacustom2';
603 $user1->customfields = array(
604 array('type' => $customfieldname1, 'value' => 'customvalue'),
605 array('type' => $customfieldname2, 'value' => 'customvalue2'));
606 //a small user: user2
607 $user2 = new stdClass();
608 $user2->username = 'veryimprobabletestusername2';
609 $user2->password = 'testpassword2';
610 $user2->firstname = 'testfirstname2';
611 $user2->lastname = 'testlastname2';
a9f07c41 612 $user2->email = 'testemail1@moodle.com';
d1d0788d 613 $users = array($user1, $user2);
614
615 //can run this test only if test usernames don't exist
616 $searchusers = $DB->get_records_list('user', 'username',
617 array($user1->username, $user1->username));
618 if (count($searchusers) == 0) {
619 //create two users
620 require_once($CFG->dirroot."/user/lib.php");
621 require_once($CFG->dirroot."/user/profile/lib.php");
622 $user1->id = user_create_user($user1);
623 // custom fields
624 if(!empty($user1->customfields)) {
625 foreach($user1->customfields as $customfield) {
626 $user1->{"profile_field_".$customfield['type']} = $customfield['value'];
627 }
628 profile_save_data((object) $user1);
629 }
630 //preferences
631 if (!empty($user1->preferences)) {
632 foreach($user1->preferences as $preference) {
633 set_user_preference($preference['type'], $preference['value'],$user1->id);
634 }
635 }
636 $user2->id = user_create_user($user2);
637
a9f07c41 638 //create the custom fields
639 $customfield = new stdClass();
640 $customfield->shortname = $customfieldname1;
641 $customfield->name = $customfieldname1;
642 $customfield->datatype = 'text';
643 $DB->insert_record('user_info_field', $customfield);
644 $customfield = new stdClass();
645 $customfield->shortname = $customfieldname2;
646 $customfield->name = $customfieldname2;
647 $customfield->datatype = 'text';
648 $DB->insert_record('user_info_field', $customfield);
649
d1d0788d 650 //search for them => TEST they exists
651 $searchusers = $DB->get_records_list('user', 'username',
652 array($user1->username, $user2->username));
653 $this->assertEqual(count($users), count($searchusers));
654
655 //delete the users by webservice
656 $function = 'moodle_user_delete_users';
657 $params = array('users' => array($user1->id, $user2->id));
658 $client->call($function, $params);
659
660 //search for them => TESTS they don't exists
661 $searchusers = $DB->get_records_list('user', 'username',
662 array($user1->username, $user2->username));
663
664 $this->assertTrue(empty($searchusers));
a9f07c41 665
666 //unset preferences
667 $DB->delete_records('user_preferences', array('userid' => $user1->id));
668
669 //clear custom fields data
670 $DB->delete_records('user_info_data', array('userid' => $user1->id));
671
672 //delete custom fields
673 $DB->delete_records_list('user_info_field', 'shortname',
674 array($customfieldname1, $customfieldname2));
675
676 //delete users from DB
677 $DB->delete_records_list('user', 'id',
678 array($user1->id, $user2->id));
679 }
680 }
681
682 function moodle_user_update_users($client) {
683 global $DB, $CFG;
684
685 //Set test data
686 //a full user: user1
687 $user1 = new stdClass();
688 $user1->username = 'veryimprobabletestusername1';
689 $user1->password = 'testpassword1';
690 $user1->firstname = 'testfirstname1';
691 $user1->lastname = 'testlastname1';
692 $user1->email = 'testemail1@moodle.com';
693 $user1->auth = 'manual';
694 $user1->idnumber = 'testidnumber1';
695 $user1->lang = 'en';
696 $user1->theme = 'standard';
697 $user1->timezone = 99;
698 $user1->mailformat = 0;
699 $user1->description = 'Hello World!';
700 $user1->city = 'testcity1';
701 $user1->country = 'au';
702 $preferencename1 = 'preference1';
703 $preferencename2 = 'preference2';
704 $user1->preferences = array(
705 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
706 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
707 $customfieldname1 = 'testdatacustom1';
708 $customfieldname2 = 'testdatacustom2';
709 $user1->customfields = array(
710 array('type' => $customfieldname1, 'value' => 'customvalue'),
711 array('type' => $customfieldname2, 'value' => 'customvalue2'));
712 //a small user: user2
713 $user2 = new stdClass();
714 $user2->username = 'veryimprobabletestusername2';
715 $user2->password = 'testpassword2';
716 $user2->firstname = 'testfirstname2';
717 $user2->lastname = 'testlastname2';
718 $user2->email = 'testemail1@moodle.com';
719 $users = array($user1, $user2);
720
721 //can run this test only if test usernames don't exist
722 $searchusers = $DB->get_records_list('user', 'username',
723 array($user1->username, $user1->username));
724 if (count($searchusers) == 0) {
725 //create two users
726 require_once($CFG->dirroot."/user/lib.php");
727 require_once($CFG->dirroot."/user/profile/lib.php");
728 $user1->id = user_create_user($user1);
729 //unset field created by user_create_user
730 unset($user1->timemodified);
731 unset($user1->timecreated);
732
733 // custom fields
734 if(!empty($user1->customfields)) {
735 foreach($user1->customfields as $customfield) {
736 $customuser1->id = $user1->id;
737 $customuser1->{"profile_field_".$customfield['type']} = $customfield['value'];
738 }
739 profile_save_data((object) $customuser1);
740 }
741 //preferences
742 if (!empty($user1->preferences)) {
743 foreach($user1->preferences as $preference) {
744 set_user_preference($preference['type'], $preference['value'],$user1->id);
745 }
746 }
747 $user2->id = user_create_user($user2);
748 unset($user2->timemodified);
749 unset($user2->timecreated);
750
751 //create the custom fields
752 $customfield = new stdClass();
753 $customfield->shortname = $customfieldname1;
754 $customfield->name = $customfieldname1;
755 $customfield->datatype = 'text';
756 $DB->insert_record('user_info_field', $customfield);
757 $customfield = new stdClass();
758 $customfield->shortname = $customfieldname2;
759 $customfield->name = $customfieldname2;
760 $customfield->datatype = 'text';
761 $DB->insert_record('user_info_field', $customfield);
762
763 //search for them => TEST they exists
764 $searchusers = $DB->get_records_list('user', 'username',
765 array($user1->username, $user2->username));
766 $this->assertEqual(count($users), count($searchusers));
767
768 //update the test data
769 $user1->username = 'veryimprobabletestusername1_updated';
770 $user1->password = 'testpassword1_updated';
771 $user1->firstname = 'testfirstname1_updated';
772 $user1->lastname = 'testlastname1_updated';
773 $user1->email = 'testemail1_updated@moodle.com';
774 $user1->auth = 'manual';
775 $user1->idnumber = 'testidnumber1_updated';
776 $user1->lang = 'en';
777 $user1->theme = 'standard';
778 $user1->timezone = 98;
779 $user1->mailformat = 1;
780 $user1->description = 'Hello World!_updated';
781 $user1->city = 'testcity1_updated';
782 $user1->country = 'au';
783 $preferencename1 = 'preference1';
784 $preferencename2 = 'preference2';
785 $user1->preferences = array(
786 array('type' => $preferencename1, 'value' => 'preferencevalue1_updated'),
787 array('type' => $preferencename2, 'value' => 'preferencevalue2_updated'));
788 $customfieldname1 = 'testdatacustom1';
789 $customfieldname2 = 'testdatacustom2';
790 $user1->customfields = array(
791 array('type' => $customfieldname1, 'value' => 'customvalue_updated'),
792 array('type' => $customfieldname2, 'value' => 'customvalue2_updated'));
793 $user2->username = 'veryimprobabletestusername2_updated';
794 $user2->password = 'testpassword2_updated';
795 $user2->firstname = 'testfirstname2_updated';
796 $user2->lastname = 'testlastname2_updated';
797 $user2->email = 'testemail1_updated@moodle.com';
798 $users = array($user1, $user2);
799
800 //update the user
801 //delete the users by webservice
802 $function = 'moodle_user_update_users';
803 $params = array('users' => $users);
804 $client->call($function, $params);
805
806 //compare user
807 $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
808 $this->assertEqual($dbuser1->firstname, $user1->firstname);
809 $this->assertEqual($dbuser1->password,
810 hash_internal_user_password($user1->password));
811 $this->assertEqual($dbuser1->lastname, $user1->lastname);
812 $this->assertEqual($dbuser1->email, $user1->email);
813 $this->assertEqual($dbuser1->auth, $user1->auth);
814 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
815 $this->assertEqual($dbuser1->lang, $user1->lang);
816 $this->assertEqual($dbuser1->theme, $user1->theme);
817 $this->assertEqual($dbuser1->timezone, $user1->timezone);
818 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
819 $this->assertEqual($dbuser1->description, $user1->description);
820 $this->assertEqual($dbuser1->city, $user1->city);
821 $this->assertEqual($dbuser1->country, $user1->country);
822 $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
823 null, $dbuser1->id);
824 $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
825 $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
826 null, $dbuser1->id);
827 $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
828 require_once($CFG->dirroot . "/user/profile/lib.php");
829 $customfields = profile_user_record($dbuser1->id);
830
831 $customfields = (array) $customfields;
832 $customfieldname1 = $user1->customfields[0]['type'];
833 $customfieldname2 = $user1->customfields[1]['type'];
834 $this->assertEqual($customfields[$customfieldname1],
835 $user1->customfields[0]['value']);
836 $this->assertEqual($customfields[$customfieldname2],
837 $user1->customfields[1]['value']);
838
839 //retrieve user2 from the DB and check values
840 $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
841 $this->assertEqual($dbuser2->firstname, $user2->firstname);
842 $this->assertEqual($dbuser2->password,
843 hash_internal_user_password($user2->password));
844 $this->assertEqual($dbuser2->lastname, $user2->lastname);
845 $this->assertEqual($dbuser2->email, $user2->email);
846
847 //unset preferences
848 $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
849
850 //clear custom fields data
851 $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
852
853 //delete custom fields
854 $DB->delete_records_list('user_info_field', 'shortname',
855 array($customfieldname1, $customfieldname2));
856
857 //delete users from DB
858 $DB->delete_records_list('user', 'id',
859 array($dbuser1->id, $dbuser2->id));
860
d1d0788d 861 }
862 }
863
75681c41 864}