webservice MDL-21658 add moodle_user_delete_users() unit test function + fix moodle_u...
[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,
d1d0788d 81 'moodle_user_delete_users' => false
75681c41 82 );
83
84 //performance testing: number of time the web service are run
85 $this->iteration = 1;
86
87 //DO NOT CHANGE
88 //reset the timers
89 $this->timerrest = 0;
90 $this->timerxmlrpc = 0;
91 $this->timersoap = 0;
92 }
93
94 function testRun() {
95 global $CFG;
96
97 if (!empty($this->testtoken)) {
98
99 //Does not work till XML => PHP is implemented (MDL-22965)
100 if ($this->testrest) {
101
102 $this->timerrest = time();
103
104 require_once($CFG->dirroot . "/webservice/rest/lib.php");
105 $restclient = new webservice_rest_client($CFG->wwwroot
106 . '/webservice/rest/server.php', $this->testtoken);
107
108 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
109 foreach ($this->readonlytests as $functioname => $run) {
110 if ($run) {
111 //$this->{$functioname}($restclient);
112 }
113 }
114 foreach ($this->writetests as $functioname => $run) {
115 if ($run) {
116 //$this->{$functioname}($restclient);
117 }
118 }
119 }
120
121 $this->timerrest = time() - $this->timerrest;
122 //here you could call a log function to display the timer
123 //example:
124 //error_log('REST time: ');
125 //error_log(print_r($this->timerrest));
126 }
127
128 if ($this->testxmlrpc) {
129
130 $this->timerxmlrpc = time();
131
132 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
133 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
134 . '/webservice/xmlrpc/server.php', $this->testtoken);
135
136 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
137 foreach ($this->readonlytests as $functioname => $run) {
138 if ($run) {
139 $this->{$functioname}($xmlrpcclient);
140 }
141 }
142 foreach ($this->writetests as $functioname => $run) {
143 if ($run) {
144 $this->{$functioname}($xmlrpcclient);
145 }
146 }
147 }
148
149 $this->timerxmlrpc = time() - $this->timerxmlrpc;
150 //here you could call a log function to display the timer
151 //example:
152 //error_log('XML-RPC time: ');
153 //error_log(print_r($this->timerxmlrpc));
154 }
155
156 if ($this->testsoap) {
157
158 $this->timersoap = time();
159
160 require_once($CFG->dirroot . "/webservice/soap/lib.php");
161 $soapclient = new webservice_soap_client($CFG->wwwroot
d1d0788d 162 . '/webservice/soap/server.php', $this->testtoken,
163 array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //force SOAP synchronous mode
164 //when function return null
75681c41 165 $soapclient->setWsdlCache(false);
166
167 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
168 foreach ($this->readonlytests as $functioname => $run) {
169 if ($run) {
170 $this->{$functioname}($soapclient);
171 }
172 }
173 foreach ($this->writetests as $functioname => $run) {
174 if ($run) {
175 $this->{$functioname}($soapclient);
176 }
177 }
178 }
179
180 $this->timersoap = time() - $this->timersoap;
181 //here you could call a log function to display the timer
182 //example:
183 //error_log('SOAP time: ');
184 //error_log(print_r($this->timersoap));
185 }
186 }
187 }
188
189 ///// WEB SERVICE TEST FUNCTIONS
190
191 function moodle_group_get_groups($client) {
192 global $DB;
193 $dbgroups = $DB->get_records('groups');
194 $groupids = array();
195 foreach ($dbgroups as $dbgroup) {
196 $groupids[] = $dbgroup->id;
197 }
198 $function = 'moodle_group_get_groups';
199
200 $params = array('groupids' => $groupids);
201 $groups = $client->call($function, $params);
202 $this->assertEqual(count($groups), count($groupids));
203 }
204
6bb31e40 205 function moodle_user_get_users_by_id($client) {
206 global $DB;
d1d0788d 207 $dbusers = $DB->get_records('user', array('deleted' => 0));
6bb31e40 208 $userids = array();
209 foreach ($dbusers as $dbuser) {
210 $userids[] = $dbuser->id;
211 }
212 $function = 'moodle_user_get_users_by_id';
213
214 $params = array('userids' => $userids);
215 $users = $client->call($function, $params);
216
217 $this->assertEqual(count($users), count($userids));
218 }
219
b02b5fb7 220 function moodle_enrol_get_enrolled_users($client) {
221 global $DB;
222
223 //function settings
224 $withcapability = '';
225 $groupid = null;
226 $onlyactive = false;
227
228 $dbcourses = $DB->get_records('course');
229 $function = 'moodle_enrol_get_enrolled_users';
230
231 foreach ($dbcourses as $dbcourse) {
232
233 $params = array();
234
235 $coursecontext = get_context_instance(CONTEXT_COURSE, $dbcourse->id);
236
237 list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
238 $sql = "SELECT DISTINCT ue.userid, e.courseid
239 FROM {user_enrolments} ue
240 JOIN {enrol} e ON (e.id = ue.enrolid)
241 WHERE e.courseid = :courseid AND ue.userid IN ($sql)";
242
243 $params['courseid'] = $dbcourse->id;
244
245 $enrolledusers = $DB->get_records_sql($sql, $params);
246
247 $wsparams = array('courseid' => $dbcourse->id, 'withcapability' => $withcapability,
248 'groupid' => $groupid, 'onlyactive' => $onlyactive);
249 $resultusers = $client->call($function, $wsparams);
250
251 $this->assertEqual(count($resultusers), count($enrolledusers));
252 }
253 }
254
6bb31e40 255 function moodle_course_get_courses($client) {
75681c41 256 global $DB;
6bb31e40 257
258 $function = 'moodle_course_get_courses';
259
260 //retrieve all courses from db
261 $dbcourses = $DB->get_records('course');
262 $courseids = array();
263 foreach ($dbcourses as $dbcourse) {
264 $courseids[] = $dbcourse->id;
265 }
266
267 //retrieve all courses by id
268 $params = array('options' => array('ids' => $courseids));
269 $courses = $client->call($function, $params);
270
271 //check it is the same course count
272 $this->assertEqual(count($courses), count($courseids));
273
274 //check all course values are identic
275 foreach ($courses as $course) {
276 $this->assertEqual($course['fullname'],
277 $dbcourses[$course['id']]->fullname);
278 $this->assertEqual($course['shortname'],
279 $dbcourses[$course['id']]->shortname);
280 $this->assertEqual($course['categoryid'],
281 $dbcourses[$course['id']]->category);
282 $this->assertEqual($course['categorysortorder'],
283 $dbcourses[$course['id']]->sortorder);
284 $this->assertEqual($course['idnumber'],
285 $dbcourses[$course['id']]->idnumber);
286 $this->assertEqual($course['summary'],
287 $dbcourses[$course['id']]->summary);
288 $this->assertEqual($course['summaryformat'],
289 $dbcourses[$course['id']]->summaryformat);
290 $this->assertEqual($course['format'],
291 $dbcourses[$course['id']]->format);
292 $this->assertEqual($course['showgrades'],
293 $dbcourses[$course['id']]->showgrades);
294 $this->assertEqual($course['newsitems'],
295 $dbcourses[$course['id']]->newsitems);
296 $this->assertEqual($course['startdate'],
297 $dbcourses[$course['id']]->startdate);
298 $this->assertEqual($course['numsections'],
299 $dbcourses[$course['id']]->numsections);
300 $this->assertEqual($course['maxbytes'],
301 $dbcourses[$course['id']]->maxbytes);
302 $this->assertEqual($course['visible'],
303 $dbcourses[$course['id']]->visible);
304 $this->assertEqual($course['hiddensections'],
305 $dbcourses[$course['id']]->hiddensections);
306 $this->assertEqual($course['groupmode'],
307 $dbcourses[$course['id']]->groupmode);
308 $this->assertEqual($course['groupmodeforce'],
309 $dbcourses[$course['id']]->groupmodeforce);
310 $this->assertEqual($course['defaultgroupingid'],
311 $dbcourses[$course['id']]->defaultgroupingid);
312 $this->assertEqual($course['lang'],
313 $dbcourses[$course['id']]->lang);
314 $this->assertEqual($course['timecreated'],
315 $dbcourses[$course['id']]->timecreated);
316 $this->assertEqual($course['timemodified'],
317 $dbcourses[$course['id']]->timemodified);
318 if (key_exists('enablecompletion', $course)) {
319 $this->assertEqual($course['enablecompletion'],
320 $dbcourses[$course['id']]->enablecompletion);
321 }
322 if (key_exists('completionstartonenrol', $course)) {
323 $this->assertEqual($course['completionstartonenrol'],
324 $dbcourses[$course['id']]->completionstartonenrol);
325 }
326 if (key_exists('completionnotify', $course)) {
327 $this->assertEqual($course['completionnotify'],
328 $dbcourses[$course['id']]->completionnotify);
329 }
330 $this->assertEqual($course['forcetheme'],
331 $dbcourses[$course['id']]->theme);
332 }
333 }
334
335 function moodle_course_create_courses($client) {
336 global $DB, $CFG;
337
338 ///Test data
339 $courseconfig = get_config('moodlecourse');
340
341 $themeobjects = get_list_of_themes();
342 $theme = key($themeobjects);
343 $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
b02b5fb7 344 $categoryid = empty($categoryid) ? 0 : $categoryid;
6bb31e40 345
346 $course1 = new stdClass();
347 $course1->fullname = 'Test Data create course 1';
348 $course1->shortname = 'testdatacourse1';
349 $course1->categoryid = $categoryid;
350 $course1->idnumber = '328327982372342343234';
351 $course1->summary = 'This is a summary';
352 $course1->summaryformat = FORMAT_HTML;
353 $course1->format = $courseconfig->format;
354 $course1->showgrades = $courseconfig->showgrades;
355 $course1->showreports = $courseconfig->showreports;
356 $course1->newsitems = $courseconfig->newsitems;
357 $course1->startdate = time();
358 $course1->numsections = $courseconfig->numsections;
359 $course1->maxbytes = $courseconfig->maxbytes;
360 $course1->visible = $courseconfig->visible;
361 $course1->hiddensections = $courseconfig->hiddensections;
362 $course1->groupmode = $courseconfig->groupmode;
363 $course1->groupmodeforce = $courseconfig->groupmodeforce;
364 $course1->defaultgroupingid = 0;
365 if (!empty($courseconfig->lang)) {
366 $course1->lang = $courseconfig->lang;
367 }
368 $course1->enablecompletion = $courseconfig->enablecompletion;
369 $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
370 $course1->completionnotify = 0;
371 $course1->forcetheme = $theme;
372
373 $course2 = new stdClass();
374 $course2->fullname = 'Test Data create course 2';
375 $course2->shortname = 'testdatacourse2';
376 $course2->categoryid = $categoryid;
377
378 $courses = array($course1, $course2);
379
380 //do not run the test if course1 or course2 already exists
381 $existingcourses = $DB->get_records_list('course', 'fullname',
382 array($course1->fullname, $course2->fullname));
383 if (!empty($existingcourses)) {
384 throw new moodle_exception('testdatacoursesalreadyexist');
385 }
386
387 $function = 'moodle_course_create_courses';
388 $params = array('courses' => $courses);
389 $resultcourses = $client->call($function, $params);
390 $this->assertEqual(count($courses), count($resultcourses));
391
392 //retrieve user1 from the DB and check values
393 $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
394 $this->assertEqual($dbcourse1->fullname, $course1->fullname);
395 $this->assertEqual($dbcourse1->shortname, $course1->shortname);
396 $this->assertEqual($dbcourse1->category, $course1->categoryid);
397 $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
398 $this->assertEqual($dbcourse1->summary, $course1->summary);
399 $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
400 $this->assertEqual($dbcourse1->format, $course1->format);
401 $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
402 $this->assertEqual($dbcourse1->showreports, $course1->showreports);
403 $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
404 $this->assertEqual($dbcourse1->startdate, $course1->startdate);
405 $this->assertEqual($dbcourse1->numsections, $course1->numsections);
406 $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
407 $this->assertEqual($dbcourse1->visible, $course1->visible);
408 $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
409 $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
410 $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
411 $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
412 if (!empty($courseconfig->lang)) {
413 $this->assertEqual($dbcourse1->lang, $course1->lang);
414 }
415 if (completion_info::is_enabled_for_site()) {
416 $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
417 $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
75681c41 418 }
6bb31e40 419 $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
420 if (!empty($CFG->allowcoursethemes)) {
421 $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
422 }
423
424 //retrieve user2 from the DB and check values
425 $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
426 $this->assertEqual($dbcourse2->fullname, $course2->fullname);
427 $this->assertEqual($dbcourse2->shortname, $course2->shortname);
b02b5fb7 428 $this->assertEqual($dbcourse2->category, $course2->categoryid);
6bb31e40 429 $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
430 $this->assertEqual($dbcourse2->format, $courseconfig->format);
431 $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
432 $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
433 $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
434 $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
435 $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
436 $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
437 $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
438 $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
439 $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
440 $this->assertEqual($dbcourse2->defaultgroupingid, 0);
441
442 //delete users from DB
443 $DB->delete_records_list('course', 'id',
444 array($dbcourse1->id, $dbcourse2->id));
445 }
446
447 function moodle_user_create_users($client) {
448 global $DB, $CFG;
449
450 //Test data
451 //a full user: user1
452 $user1 = new stdClass();
453 $user1->username = 'testusername1';
454 $user1->password = 'testpassword1';
455 $user1->firstname = 'testfirstname1';
456 $user1->lastname = 'testlastname1';
457 $user1->email = 'testemail1@moodle.com';
458 $user1->auth = 'manual';
459 $user1->idnumber = 'testidnumber1';
6bb31e40 460 $user1->lang = 'en';
461 $user1->theme = 'standard';
462 $user1->timezone = 99;
463 $user1->mailformat = 0;
464 $user1->description = 'Hello World!';
465 $user1->city = 'testcity1';
466 $user1->country = 'au';
467 $preferencename1 = 'preference1';
468 $preferencename2 = 'preference2';
469 $user1->preferences = array(
470 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
471 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
472 $customfieldname1 = 'testdatacustom1';
473 $customfieldname2 = 'testdatacustom2';
474 $user1->customfields = array(
475 array('type' => $customfieldname1, 'value' => 'customvalue'),
476 array('type' => $customfieldname2, 'value' => 'customvalue2'));
477 //a small user: user2
478 $user2 = new stdClass();
479 $user2->username = 'testusername2';
480 $user2->password = 'testpassword2';
481 $user2->firstname = 'testfirstname2';
482 $user2->lastname = 'testlastname2';
483 $user2->email = 'testemail1@moodle.com';
484
485 $users = array($user1, $user2);
486
487 //do not run the test if user1 or user2 already exists
488 $existingusers = $DB->get_records_list('user', 'username',
489 array($user1->username, $user2->username));
490 if (!empty($existingusers)) {
491 throw new moodle_exception('testdatausersalreadyexist');
492 }
493
494 //do not run the test if data test custom fields already exists
495 $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
496 array($customfieldname1, $customfieldname2));
497 if (!empty($existingcustomfields)) {
498 throw new moodle_exception('testdatacustomfieldsalreadyexist');
499 }
500
501 //create the custom fields
502 $customfield = new stdClass();
503 $customfield->shortname = $customfieldname1;
504 $customfield->name = $customfieldname1;
505 $customfield->datatype = 'text';
506 $DB->insert_record('user_info_field', $customfield);
507 $customfield = new stdClass();
508 $customfield->shortname = $customfieldname2;
509 $customfield->name = $customfieldname2;
510 $customfield->datatype = 'text';
511 $DB->insert_record('user_info_field', $customfield);
512
513 $function = 'moodle_user_create_users';
514 $params = array('users' => $users);
515 $resultusers = $client->call($function, $params);
516 $this->assertEqual(count($users), count($resultusers));
517
518 //retrieve user1 from the DB and check values
519 $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
520 $this->assertEqual($dbuser1->firstname, $user1->firstname);
521 $this->assertEqual($dbuser1->password,
522 hash_internal_user_password($user1->password));
523 $this->assertEqual($dbuser1->lastname, $user1->lastname);
524 $this->assertEqual($dbuser1->email, $user1->email);
525 $this->assertEqual($dbuser1->auth, $user1->auth);
526 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
6bb31e40 527 $this->assertEqual($dbuser1->lang, $user1->lang);
528 $this->assertEqual($dbuser1->theme, $user1->theme);
529 $this->assertEqual($dbuser1->timezone, $user1->timezone);
530 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
531 $this->assertEqual($dbuser1->description, $user1->description);
532 $this->assertEqual($dbuser1->city, $user1->city);
533 $this->assertEqual($dbuser1->country, $user1->country);
534 $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
535 null, $dbuser1->id);
536 $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
537 $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
538 null, $dbuser1->id);
539 $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
540 require_once($CFG->dirroot . "/user/profile/lib.php");
541 $customfields = profile_user_record($dbuser1->id);
542
543 $customfields = (array) $customfields;
544 $customfieldname1 = $user1->customfields[0]['type'];
545 $customfieldname2 = $user1->customfields[1]['type'];
546 $this->assertEqual($customfields[$customfieldname1],
547 $user1->customfields[0]['value']);
548 $this->assertEqual($customfields[$customfieldname2],
549 $user1->customfields[1]['value']);
550
551
552 //retrieve user2 from the DB and check values
553 $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
554 $this->assertEqual($dbuser2->firstname, $user2->firstname);
555 $this->assertEqual($dbuser2->password,
556 hash_internal_user_password($user2->password));
557 $this->assertEqual($dbuser2->lastname, $user2->lastname);
558 $this->assertEqual($dbuser2->email, $user2->email);
559
560 //unset preferences
561 $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
562
563 //clear custom fields data
564 $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
565
566 //delete custom fields
567 $DB->delete_records_list('user_info_field', 'shortname',
568 array($customfieldname1, $customfieldname2));
569
570 //delete users from DB
571 $DB->delete_records_list('user', 'id',
572 array($dbuser1->id, $dbuser2->id));
75681c41 573 }
574
d1d0788d 575 function moodle_user_delete_users($client) {
576 global $DB, $CFG;
577
578 //Set test data
579 //a full user: user1
580 $user1 = new stdClass();
581 $user1->username = 'veryimprobabletestusername1';
582 $user1->password = 'testpassword1';
583 $user1->firstname = 'testfirstname1';
584 $user1->lastname = 'testlastname1';
585 $user1->email = 'testemail1@moodle.com';
586 $user1->auth = 'manual';
587 $user1->idnumber = 'testidnumber1';
588 $user1->lang = 'en';
589 $user1->theme = 'standard';
590 $user1->timezone = 99;
591 $user1->mailformat = 0;
592 $user1->description = 'Hello World!';
593 $user1->city = 'testcity1';
594 $user1->country = 'au';
595 $preferencename1 = 'preference1';
596 $preferencename2 = 'preference2';
597 $user1->preferences = array(
598 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
599 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
600 $customfieldname1 = 'testdatacustom1';
601 $customfieldname2 = 'testdatacustom2';
602 $user1->customfields = array(
603 array('type' => $customfieldname1, 'value' => 'customvalue'),
604 array('type' => $customfieldname2, 'value' => 'customvalue2'));
605 //a small user: user2
606 $user2 = new stdClass();
607 $user2->username = 'veryimprobabletestusername2';
608 $user2->password = 'testpassword2';
609 $user2->firstname = 'testfirstname2';
610 $user2->lastname = 'testlastname2';
611 $user2->email = 'testemail1@moodle.com';
612 $users = array($user1, $user2);
613
614 //can run this test only if test usernames don't exist
615 $searchusers = $DB->get_records_list('user', 'username',
616 array($user1->username, $user1->username));
617 if (count($searchusers) == 0) {
618 //create two users
619 require_once($CFG->dirroot."/user/lib.php");
620 require_once($CFG->dirroot."/user/profile/lib.php");
621 $user1->id = user_create_user($user1);
622 // custom fields
623 if(!empty($user1->customfields)) {
624 foreach($user1->customfields as $customfield) {
625 $user1->{"profile_field_".$customfield['type']} = $customfield['value'];
626 }
627 profile_save_data((object) $user1);
628 }
629 //preferences
630 if (!empty($user1->preferences)) {
631 foreach($user1->preferences as $preference) {
632 set_user_preference($preference['type'], $preference['value'],$user1->id);
633 }
634 }
635 $user2->id = user_create_user($user2);
636
637 //search for them => TEST they exists
638 $searchusers = $DB->get_records_list('user', 'username',
639 array($user1->username, $user2->username));
640 $this->assertEqual(count($users), count($searchusers));
641
642 //delete the users by webservice
643 $function = 'moodle_user_delete_users';
644 $params = array('users' => array($user1->id, $user2->id));
645 $client->call($function, $params);
646
647 //search for them => TESTS they don't exists
648 $searchusers = $DB->get_records_list('user', 'username',
649 array($user1->username, $user2->username));
650
651 $this->assertTrue(empty($searchusers));
652 }
653 }
654
75681c41 655}