Merge branch 'MLD-27565-wip' of 'git://github.com/mouneyrac/moodle.git' with changes...
[moodle.git] / webservice / simpletest / testwebservice.php
1 <?php
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/>.
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  */
25 if (!defined('MOODLE_INTERNAL')) {
26     ///  It must be included from a Moodle page
27     die('Direct access to this script is forbidden.');
28 }
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
34  * 2- Create a token associate this service and to an admin (or a user with all required capabilities)
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  */
48 class webservice_test extends UnitTestCase {
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;
60     function setUp() {
61         //token to test
62         $this->testtoken = 'acabec9d20933913f14309785324f579';
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;
69         ////// READ-ONLY DB tests ////
70         $this->readonlytests = array(
71             'moodle_group_get_groups' => false,
72             'moodle_course_get_courses' => false,
73             'moodle_user_get_users_by_id' => false,
74             'moodle_enrol_get_enrolled_users' => false,
75             'moodle_group_get_course_groups' => false,
76             'moodle_group_get_groupmembers' => false
77         );
79         ////// WRITE DB tests ////
80         $this->writetests = array(
81             'moodle_user_create_users' => false,
82             'moodle_course_create_courses' => false,
83             'moodle_user_delete_users' => false,
84             'moodle_user_update_users' => false,
85             'moodle_role_assign' => false,
86             'moodle_role_unassign' => false,
87             'moodle_group_add_groupmembers' => false,
88             'moodle_group_delete_groupmembers' => false,
89             'moodle_group_create_groups' => false,
90             'moodle_group_delete_groups' => false,
91             'moodle_enrol_manual_enrol_users' => false,
92             'moodle_message_send_messages' => false,
93             'moodle_notes_create_notes' => false
94         );
96         //performance testing: number of time the web service are run
97         $this->iteration = 1;
99         //DO NOT CHANGE
100         //reset the timers
101         $this->timerrest = 0;
102         $this->timerxmlrpc = 0;
103         $this->timersoap = 0;
104     }
106     function testRun() {
107         global $CFG;
109         if (!$this->testrest and !$this->testxmlrpc and !$this->testsoap) {
110             print_r("Web service unit tests are not run as not setup.
111                 (see /webservice/simpletest/testwebservice.php)");
112         }
114         if (!empty($this->testtoken)) {
116             //Does not work till XML => PHP is implemented (MDL-22965)
117             if ($this->testrest) {
119                 $this->timerrest = time();
121                 require_once($CFG->dirroot . "/webservice/rest/lib.php");
122                 $restclient = new webservice_rest_client($CFG->wwwroot
123                                 . '/webservice/rest/server.php', $this->testtoken);
125                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
126                     foreach ($this->readonlytests as $functioname => $run) {
127                         if ($run) {
128                             //$this->{$functioname}($restclient);
129                         }
130                     }
131                     foreach ($this->writetests as $functioname => $run) {
132                         if ($run) {
133                             //$this->{$functioname}($restclient);
134                         }
135                     }
136                 }
138                 $this->timerrest = time() - $this->timerrest;
139                 //here you could call a log function to display the timer
140                 //example:
141                 //error_log('REST time: ');
142                 //error_log(print_r($this->timerrest));
143             }
145             if ($this->testxmlrpc) {
147                 $this->timerxmlrpc = time();
149                 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
150                 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
151                                 . '/webservice/xmlrpc/server.php', $this->testtoken);
153                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
154                     foreach ($this->readonlytests as $functioname => $run) {
155                         if ($run) {
156                             $this->{$functioname}($xmlrpcclient);
157                         }
158                     }
159                     foreach ($this->writetests as $functioname => $run) {
160                         if ($run) {
161                             $this->{$functioname}($xmlrpcclient);
162                         }
163                     }
164                 }
166                 $this->timerxmlrpc = time() - $this->timerxmlrpc;
167                 //here you could call a log function to display the timer
168                 //example:
169                 //error_log('XML-RPC time: ');
170                 //error_log(print_r($this->timerxmlrpc));
171             }
173             if ($this->testsoap) {
175                 $this->timersoap = time();
177                 require_once($CFG->dirroot . "/webservice/soap/lib.php");
178                 $soapclient = new webservice_soap_client($CFG->wwwroot
179                                 . '/webservice/soap/server.php', $this->testtoken,
180                         array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //force SOAP synchronous mode
181                                                                      //when function return null
182                 $soapclient->setWsdlCache(false);
184                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
185                     foreach ($this->readonlytests as $functioname => $run) {
186                         if ($run) {
187                             $this->{$functioname}($soapclient);
188                         }
189                     }
190                     foreach ($this->writetests as $functioname => $run) {
191                         if ($run) {
192                             $this->{$functioname}($soapclient);
193                         }
194                     }
195                 }
197                 $this->timersoap = time() - $this->timersoap;
198                 //here you could call a log function to display the timer
199                 //example:
200                 //error_log('SOAP time: ');
201                 //error_log(print_r($this->timersoap));
202             }
203         }
204     }
206     ///// WEB SERVICE TEST FUNCTIONS
208     function moodle_group_get_groups($client) {
209         global $DB;
210         $dbgroups = $DB->get_records('groups');
211         $groupids = array();
212         foreach ($dbgroups as $dbgroup) {
213             $groupids[] = $dbgroup->id;
214         }
215         $function = 'moodle_group_get_groups';
217         $params = array('groupids' => $groupids);
218         $groups = $client->call($function, $params);
219         $this->assertEqual(count($groups), count($groupids));
220     }
222     function moodle_user_get_users_by_id($client) {
223         global $DB;
224         $dbusers = $DB->get_records('user', array('deleted' => 0));
225         $userids = array();
226         foreach ($dbusers as $dbuser) {
227             $userids[] = $dbuser->id;
228         }
229         $function = 'moodle_user_get_users_by_id';
231         $params = array('userids' => $userids);
232         $users = $client->call($function, $params);
234         $this->assertEqual(count($users), count($userids));
235     }
237     /**
238      * This test will:
239      * 1- create a user (core call)
240      * 2- enrol this user in the courses supporting enrolment
241      * 3- unenrol this user (core call)
242      */
243     function moodle_enrol_manual_enrol_users($client) {
244         global $DB, $CFG;
246         require_once($CFG->dirroot . "/user/lib.php");
247         require_once($CFG->dirroot . "/user/profile/lib.php");
248         require_once($CFG->dirroot . "/lib/enrollib.php");
250         //Delete some previous test data
251         if ($user = $DB->get_record('user', array('username' => 'veryimprobabletestusername2'))) {
252             $DB->delete_records('user', array('id' => $user->id));
253         }
254         if ($role = $DB->get_record('role', array('shortname' => 'role1thatshouldnotexist'))) {
255             set_role_contextlevels($role->id, array(CONTEXT_COURSE));
256             delete_role($role->id);
257         }
259         //create a user
260         $user = new stdClass();
261         $user->username = 'veryimprobabletestusername2';
262         $user->password = 'testpassword2';
263         $user->firstname = 'testfirstname2';
264         $user->lastname = 'testlastname2';
265         $user->email = 'testemail1@moodle.com';
266         $user->id = user_create_user($user);
268         $roleid = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
269         set_role_contextlevels($roleid, array(CONTEXT_COURSE));
271         $enrolments = array();
272         $courses = $DB->get_records('course');
274         foreach ($courses as $course) {
275             if ($course->id > 1) {
276                 $enrolments[] = array('roleid' => $roleid,
277                     'userid' => $user->id, 'courseid' => $course->id);
278                 $enrolledcourses[] = $course;
279             }
280         }
282         //web service call
283         $function = 'moodle_enrol_manual_enrol_users';
284         $wsparams = array('enrolments' => $enrolments);
285         $enrolmentsresult = $client->call($function, $wsparams);
287         //get instance that can unenrol
288         $enrols = enrol_get_plugins(true);
289         $enrolinstances = enrol_get_instances($course->id, true);
290         $unenrolled = false;
291         foreach ($enrolinstances as $instance) {
292             if (!$unenrolled and $enrols[$instance->enrol]->allow_unenrol($instance)) {
293                 $unenrolinstance = $instance;
294                 $unenrolled = true;
295             }
296         }
298         //test and unenrol the user
299         $enrolledusercourses = enrol_get_users_courses($user->id);
300         foreach ($enrolledcourses as $course) {
301             //test
302             $this->assertEqual(true, isset($enrolledusercourses[$course->id]));
304             //unenrol the user
305             $enrols[$unenrolinstance->enrol]->unenrol_user($unenrolinstance, $user->id, $roleid);
306         }
308         //delete user
309         $DB->delete_records('user', array('id' => $user->id));
311         //delete the context level
312         set_role_contextlevels($roleid, array(CONTEXT_COURSE));
314         //delete role
315         delete_role($roleid);
316     }
319     function moodle_enrol_get_enrolled_users($client) {
320         global $DB;
322         //function settings
323         $withcapability = '';
324         $groupid = null;
325         $onlyactive = false;
327         $dbcourses = $DB->get_records('course');
328         $function = 'moodle_enrol_get_enrolled_users';
330         foreach ($dbcourses as $dbcourse) {
332             $params = array();
334             $coursecontext = get_context_instance(CONTEXT_COURSE, $dbcourse->id);
336             list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
337             $sql = "SELECT DISTINCT ue.userid, e.courseid
338                       FROM {user_enrolments} ue
339                       JOIN {enrol} e ON (e.id = ue.enrolid)
340                      WHERE e.courseid = :courseid AND ue.userid IN ($sql)";
342             $params['courseid'] = $dbcourse->id;
344             $enrolledusers = $DB->get_records_sql($sql, $params);
346             $wsparams = array('courseid' => $dbcourse->id, 'withcapability' => $withcapability,
347                 'groupid' => $groupid, 'onlyactive' => $onlyactive);
348             $resultusers = $client->call($function, $wsparams);
350             $this->assertEqual(count($resultusers), count($enrolledusers));
351         }
352     }
354     function moodle_course_get_courses($client) {
355         global $DB;
357         $function = 'moodle_course_get_courses';
359         //retrieve all courses from db
360         $dbcourses = $DB->get_records('course');
361         $courseids = array();
362         foreach ($dbcourses as $dbcourse) {
363             $courseids[] = $dbcourse->id;
364         }
366         //retrieve all courses by id
367         $params = array('options' => array('ids' => $courseids));
368         $courses = $client->call($function, $params);
370         //check it is the same course count
371         $this->assertEqual(count($courses), count($courseids));
373         //check all course values are identic
374         foreach ($courses as $course) {
375             $this->assertEqual($course['fullname'],
376                     $dbcourses[$course['id']]->fullname);
377             $this->assertEqual($course['shortname'],
378                     $dbcourses[$course['id']]->shortname);
379             $this->assertEqual($course['categoryid'],
380                     $dbcourses[$course['id']]->category);
381             $this->assertEqual($course['categorysortorder'],
382                     $dbcourses[$course['id']]->sortorder);
383             $this->assertEqual($course['idnumber'],
384                     $dbcourses[$course['id']]->idnumber);
385             $this->assertEqual($course['summary'],
386                     $dbcourses[$course['id']]->summary);
387             $this->assertEqual($course['summaryformat'],
388                     $dbcourses[$course['id']]->summaryformat);
389             $this->assertEqual($course['format'],
390                     $dbcourses[$course['id']]->format);
391             $this->assertEqual($course['showgrades'],
392                     $dbcourses[$course['id']]->showgrades);
393             $this->assertEqual($course['newsitems'],
394                     $dbcourses[$course['id']]->newsitems);
395             $this->assertEqual($course['startdate'],
396                     $dbcourses[$course['id']]->startdate);
397             $this->assertEqual($course['numsections'],
398                     $dbcourses[$course['id']]->numsections);
399             $this->assertEqual($course['maxbytes'],
400                     $dbcourses[$course['id']]->maxbytes);
401             $this->assertEqual($course['visible'],
402                     $dbcourses[$course['id']]->visible);
403             $this->assertEqual($course['hiddensections'],
404                     $dbcourses[$course['id']]->hiddensections);
405             $this->assertEqual($course['groupmode'],
406                     $dbcourses[$course['id']]->groupmode);
407             $this->assertEqual($course['groupmodeforce'],
408                     $dbcourses[$course['id']]->groupmodeforce);
409             $this->assertEqual($course['defaultgroupingid'],
410                     $dbcourses[$course['id']]->defaultgroupingid);
411             $this->assertEqual($course['lang'],
412                     $dbcourses[$course['id']]->lang);
413             $this->assertEqual($course['timecreated'],
414                     $dbcourses[$course['id']]->timecreated);
415             $this->assertEqual($course['timemodified'],
416                     $dbcourses[$course['id']]->timemodified);
417             if (key_exists('enablecompletion', $course)) {
418                 $this->assertEqual($course['enablecompletion'],
419                         $dbcourses[$course['id']]->enablecompletion);
420             }
421             if (key_exists('completionstartonenrol', $course)) {
422                 $this->assertEqual($course['completionstartonenrol'],
423                         $dbcourses[$course['id']]->completionstartonenrol);
424             }
425             if (key_exists('completionnotify', $course)) {
426                 $this->assertEqual($course['completionnotify'],
427                         $dbcourses[$course['id']]->completionnotify);
428             }
429             $this->assertEqual($course['forcetheme'],
430                     $dbcourses[$course['id']]->theme);
431         }
432     }
434     function moodle_course_create_courses($client) {
435         global $DB, $CFG;
437         ///Test data
438         $courseconfig = get_config('moodlecourse');
440         $themeobjects = get_list_of_themes();
441         $theme = key($themeobjects);
442         $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
443         $categoryid = empty($categoryid) ? 0 : $categoryid;
445         $course1 = new stdClass();
446         $course1->fullname = 'Test Data create course 1';
447         $course1->shortname = 'testdatacourse1';
448         $course1->categoryid = $categoryid;
449         $course1->idnumber = '328327982372342343234';
450         $course1->summary = 'This is a summary';
451         $course1->summaryformat = FORMAT_HTML;
452         $course1->format = $courseconfig->format;
453         $course1->showgrades = $courseconfig->showgrades;
454         $course1->showreports = $courseconfig->showreports;
455         $course1->newsitems = $courseconfig->newsitems;
456         $course1->startdate = time();
457         $course1->numsections = $courseconfig->numsections;
458         $course1->maxbytes = $courseconfig->maxbytes;
459         $course1->visible = $courseconfig->visible;
460         $course1->hiddensections = $courseconfig->hiddensections;
461         $course1->groupmode = $courseconfig->groupmode;
462         $course1->groupmodeforce = $courseconfig->groupmodeforce;
463         $course1->defaultgroupingid = 0;
464         if (!empty($courseconfig->lang)) {
465             $course1->lang = $courseconfig->lang;
466         }
467         $course1->enablecompletion = $courseconfig->enablecompletion;
468         $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
469         $course1->completionnotify = 0;
470         $course1->forcetheme = $theme;
472         $course2 = new stdClass();
473         $course2->fullname = 'Test Data create course 2';
474         $course2->shortname = 'testdatacourse2';
475         $course2->categoryid = $categoryid;
477         $courses = array($course1, $course2);
479         //do not run the test if course1 or course2 already exists
480         $existingcourses = $DB->get_records_list('course', 'fullname',
481                         array($course1->fullname, $course2->fullname));
482         if (!empty($existingcourses)) {
483             throw new moodle_exception('testdatacoursesalreadyexist');
484         }
486         $function = 'moodle_course_create_courses';
487         $params = array('courses' => $courses);
488         $resultcourses = $client->call($function, $params);
489         $this->assertEqual(count($courses), count($resultcourses));
491         //retrieve user1 from the DB and check values
492         $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
493         $this->assertEqual($dbcourse1->fullname, $course1->fullname);
494         $this->assertEqual($dbcourse1->shortname, $course1->shortname);
495         $this->assertEqual($dbcourse1->category, $course1->categoryid);
496         $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
497         $this->assertEqual($dbcourse1->summary, $course1->summary);
498         $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
499         $this->assertEqual($dbcourse1->format, $course1->format);
500         $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
501         $this->assertEqual($dbcourse1->showreports, $course1->showreports);
502         $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
503         $this->assertEqual($dbcourse1->startdate, $course1->startdate);
504         $this->assertEqual($dbcourse1->numsections, $course1->numsections);
505         $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
506         $this->assertEqual($dbcourse1->visible, $course1->visible);
507         $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
508         $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
509         $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
510         $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
511         if (!empty($courseconfig->lang)) {
512             $this->assertEqual($dbcourse1->lang, $course1->lang);
513         }
514         if (completion_info::is_enabled_for_site()) {
515             $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
516             $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
517         }
518         $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
519         if (!empty($CFG->allowcoursethemes)) {
520             $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
521         }
523         //retrieve user2 from the DB and check values
524         $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
525         $this->assertEqual($dbcourse2->fullname, $course2->fullname);
526         $this->assertEqual($dbcourse2->shortname, $course2->shortname);
527         $this->assertEqual($dbcourse2->category, $course2->categoryid);
528         $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
529         $this->assertEqual($dbcourse2->format, $courseconfig->format);
530         $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
531         $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
532         $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
533         $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
534         $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
535         $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
536         $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
537         $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
538         $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
539         $this->assertEqual($dbcourse2->defaultgroupingid, 0);
541         //delete users from DB
542         $DB->delete_records_list('course', 'id',
543                 array($dbcourse1->id, $dbcourse2->id));
544     }
546     function moodle_user_create_users($client) {
547         global $DB, $CFG;
549         //Test data
550         //a full user: user1
551         $user1 = new stdClass();
552         $user1->username = 'testusername1';
553         $user1->password = 'testpassword1';
554         $user1->firstname = 'testfirstname1';
555         $user1->lastname = 'testlastname1';
556         $user1->email = 'testemail1@moodle.com';
557         $user1->auth = 'manual';
558         $user1->idnumber = 'testidnumber1';
559         $user1->lang = 'en';
560         $user1->theme = 'standard';
561         $user1->timezone = 99;
562         $user1->mailformat = 0;
563         $user1->description = 'Hello World!';
564         $user1->city = 'testcity1';
565         $user1->country = 'au';
566         $preferencename1 = 'preference1';
567         $preferencename2 = 'preference2';
568         $user1->preferences = array(
569             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
570             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
571         $customfieldname1 = 'testdatacustom1';
572         $customfieldname2 = 'testdatacustom2';
573         $user1->customfields = array(
574             array('type' => $customfieldname1, 'value' => 'customvalue'),
575             array('type' => $customfieldname2, 'value' => 'customvalue2'));
576         //a small user: user2
577         $user2 = new stdClass();
578         $user2->username = 'testusername2';
579         $user2->password = 'testpassword2';
580         $user2->firstname = 'testfirstname2';
581         $user2->lastname = 'testlastname2';
582         $user2->email = 'testemail1@moodle.com';
584         $users = array($user1, $user2);
586         //do not run the test if user1 or user2 already exists
587         $existingusers = $DB->get_records_list('user', 'username',
588                         array($user1->username, $user2->username));
589         if (!empty($existingusers)) {
590             throw new moodle_exception('testdatausersalreadyexist');
591         }
593         //do not run the test if data test custom fields already exists
594         $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
595                         array($customfieldname1, $customfieldname2));
596         if (!empty($existingcustomfields)) {
597             throw new moodle_exception('testdatacustomfieldsalreadyexist');
598         }
600         //create the custom fields
601         $customfield = new stdClass();
602         $customfield->shortname = $customfieldname1;
603         $customfield->name = $customfieldname1;
604         $customfield->datatype = 'text';
605         $DB->insert_record('user_info_field', $customfield);
606         $customfield = new stdClass();
607         $customfield->shortname = $customfieldname2;
608         $customfield->name = $customfieldname2;
609         $customfield->datatype = 'text';
610         $DB->insert_record('user_info_field', $customfield);
612         $function = 'moodle_user_create_users';
613         $params = array('users' => $users);
614         $resultusers = $client->call($function, $params);
615         $this->assertEqual(count($users), count($resultusers));
617         //retrieve user1 from the DB and check values
618         $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
619         $this->assertEqual($dbuser1->firstname, $user1->firstname);
620         $this->assertEqual($dbuser1->password,
621                 hash_internal_user_password($user1->password));
622         $this->assertEqual($dbuser1->lastname, $user1->lastname);
623         $this->assertEqual($dbuser1->email, $user1->email);
624         $this->assertEqual($dbuser1->auth, $user1->auth);
625         $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
626         $this->assertEqual($dbuser1->lang, $user1->lang);
627         $this->assertEqual($dbuser1->theme, $user1->theme);
628         $this->assertEqual($dbuser1->timezone, $user1->timezone);
629         $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
630         $this->assertEqual($dbuser1->description, $user1->description);
631         $this->assertEqual($dbuser1->city, $user1->city);
632         $this->assertEqual($dbuser1->country, $user1->country);
633         $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
634                         null, $dbuser1->id);
635         $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
636         $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
637                         null, $dbuser1->id);
638         $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
639         require_once($CFG->dirroot . "/user/profile/lib.php");
640         $customfields = profile_user_record($dbuser1->id);
642         $customfields = (array) $customfields;
643         $customfieldname1 = $user1->customfields[0]['type'];
644         $customfieldname2 = $user1->customfields[1]['type'];
645         $this->assertEqual($customfields[$customfieldname1],
646                 $user1->customfields[0]['value']);
647         $this->assertEqual($customfields[$customfieldname2],
648                 $user1->customfields[1]['value']);
651         //retrieve user2 from the DB and check values
652         $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
653         $this->assertEqual($dbuser2->firstname, $user2->firstname);
654         $this->assertEqual($dbuser2->password,
655                 hash_internal_user_password($user2->password));
656         $this->assertEqual($dbuser2->lastname, $user2->lastname);
657         $this->assertEqual($dbuser2->email, $user2->email);
659         //unset preferences
660         $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
662         //clear custom fields data
663         $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
665         //delete custom fields
666         $DB->delete_records_list('user_info_field', 'shortname',
667                 array($customfieldname1, $customfieldname2));
669         //delete users from DB
670         $DB->delete_records_list('user', 'id',
671                 array($dbuser1->id, $dbuser2->id));
672     }
674     function moodle_user_delete_users($client) {
675         global $DB, $CFG;
677         //Set test data
678         //a full user: user1
679         $user1 = new stdClass();
680         $user1->username = 'veryimprobabletestusername1';
681         $user1->password = 'testpassword1';
682         $user1->firstname = 'testfirstname1';
683         $user1->lastname = 'testlastname1';
684         $user1->email = 'testemail1@moodle.com';
685         $user1->auth = 'manual';
686         $user1->idnumber = 'testidnumber1';
687         $user1->lang = 'en';
688         $user1->theme = 'standard';
689         $user1->timezone = 99;
690         $user1->mailformat = 0;
691         $user1->description = 'Hello World!';
692         $user1->city = 'testcity1';
693         $user1->country = 'au';
694         $preferencename1 = 'preference1';
695         $preferencename2 = 'preference2';
696         $user1->preferences = array(
697             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
698             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
699         $customfieldname1 = 'testdatacustom1';
700         $customfieldname2 = 'testdatacustom2';
701         $user1->customfields = array(
702             array('type' => $customfieldname1, 'value' => 'customvalue'),
703             array('type' => $customfieldname2, 'value' => 'customvalue2'));
704         //a small user: user2
705         $user2 = new stdClass();
706         $user2->username = 'veryimprobabletestusername2';
707         $user2->password = 'testpassword2';
708         $user2->firstname = 'testfirstname2';
709         $user2->lastname = 'testlastname2';
710         $user2->email = 'testemail1@moodle.com';
711         $users = array($user1, $user2);
713         //can run this test only if test usernames don't exist
714         $searchusers = $DB->get_records_list('user', 'username',
715                 array($user1->username, $user1->username));
716         if (count($searchusers) == 0) {
717             //create two users
718             require_once($CFG->dirroot."/user/lib.php");
719             require_once($CFG->dirroot."/user/profile/lib.php");
720             $user1->id = user_create_user($user1);
721             // custom fields
722             if(!empty($user1->customfields)) {
723                 foreach($user1->customfields as $customfield) {
724                     $user1->{"profile_field_".$customfield['type']} = $customfield['value'];
725                 }
726                 profile_save_data((object) $user1);
727             }
728             //preferences
729             if (!empty($user1->preferences)) {
730                 foreach($user1->preferences as $preference) {
731                     set_user_preference($preference['type'], $preference['value'],$user1->id);
732                 }
733             }
734             $user2->id = user_create_user($user2);
736             //create the custom fields
737             $customfield = new stdClass();
738             $customfield->shortname = $customfieldname1;
739             $customfield->name = $customfieldname1;
740             $customfield->datatype = 'text';
741             $DB->insert_record('user_info_field', $customfield);
742             $customfield = new stdClass();
743             $customfield->shortname = $customfieldname2;
744             $customfield->name = $customfieldname2;
745             $customfield->datatype = 'text';
746             $DB->insert_record('user_info_field', $customfield);
748             //search for them => TEST they exists
749             $searchusers = $DB->get_records_list('user', 'username',
750                     array($user1->username, $user2->username));
751             $this->assertEqual(count($users), count($searchusers));
753             //delete the users by webservice
754             $function = 'moodle_user_delete_users';
755             $params = array('users' => array($user1->id, $user2->id));
756             $client->call($function, $params);
758             //search for them => TESTS they don't exists
759             $searchusers = $DB->get_records_list('user', 'username',
760                     array($user1->username, $user2->username));
761            
762             $this->assertTrue(empty($searchusers));
764             //unset preferences
765             $DB->delete_records('user_preferences', array('userid' => $user1->id));
767             //clear custom fields data
768             $DB->delete_records('user_info_data', array('userid' => $user1->id));
770             //delete custom fields
771             $DB->delete_records_list('user_info_field', 'shortname',
772                     array($customfieldname1, $customfieldname2));
774             //delete users from DB
775             $DB->delete_records_list('user', 'id',
776                     array($user1->id, $user2->id));
777         }
778     }
780     function moodle_user_update_users($client) {
781         global $DB, $CFG;
783         //Set test data
784         //a full user: user1
785         $user1 = new stdClass();
786         $user1->username = 'veryimprobabletestusername1';
787         $user1->password = 'testpassword1';
788         $user1->firstname = 'testfirstname1';
789         $user1->lastname = 'testlastname1';
790         $user1->email = 'testemail1@moodle.com';
791         $user1->auth = 'manual';
792         $user1->idnumber = 'testidnumber1';
793         $user1->lang = 'en';
794         $user1->theme = 'standard';
795         $user1->timezone = 99;
796         $user1->mailformat = 0;
797         $user1->description = 'Hello World!';
798         $user1->city = 'testcity1';
799         $user1->country = 'au';
800         $preferencename1 = 'preference1';
801         $preferencename2 = 'preference2';
802         $user1->preferences = array(
803             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
804             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
805         $customfieldname1 = 'testdatacustom1';
806         $customfieldname2 = 'testdatacustom2';
807         $user1->customfields = array(
808             array('type' => $customfieldname1, 'value' => 'customvalue'),
809             array('type' => $customfieldname2, 'value' => 'customvalue2'));
810         //a small user: user2
811         $user2 = new stdClass();
812         $user2->username = 'veryimprobabletestusername2';
813         $user2->password = 'testpassword2';
814         $user2->firstname = 'testfirstname2';
815         $user2->lastname = 'testlastname2';
816         $user2->email = 'testemail1@moodle.com';
817         $users = array($user1, $user2);
819         //can run this test only if test usernames don't exist
820         $searchusers = $DB->get_records_list('user', 'username',
821                 array($user1->username, $user1->username));
822         if (count($searchusers) == 0) {
823             //create two users
824             require_once($CFG->dirroot."/user/lib.php");
825             require_once($CFG->dirroot."/user/profile/lib.php");
826             $user1->id = user_create_user($user1);
827             //unset field created by user_create_user
828             unset($user1->timemodified);
829             unset($user1->timecreated);
831             // custom fields
832             if(!empty($user1->customfields)) {
833                 foreach($user1->customfields as $customfield) {
834                     $customuser1->id = $user1->id;
835                     $customuser1->{"profile_field_".$customfield['type']} = $customfield['value'];
836                 }
837                 profile_save_data((object) $customuser1);
838             }
839             //preferences
840             if (!empty($user1->preferences)) {
841                 foreach($user1->preferences as $preference) {
842                     set_user_preference($preference['type'], $preference['value'],$user1->id);
843                 }
844             }
845             $user2->id = user_create_user($user2);
846             unset($user2->timemodified);
847             unset($user2->timecreated);
849              //create the custom fields
850             $customfield = new stdClass();
851             $customfield->shortname = $customfieldname1;
852             $customfield->name = $customfieldname1;
853             $customfield->datatype = 'text';
854             $DB->insert_record('user_info_field', $customfield);
855             $customfield = new stdClass();
856             $customfield->shortname = $customfieldname2;
857             $customfield->name = $customfieldname2;
858             $customfield->datatype = 'text';
859             $DB->insert_record('user_info_field', $customfield);
860             
861             //search for them => TEST they exists
862             $searchusers = $DB->get_records_list('user', 'username',
863                     array($user1->username, $user2->username));
864             $this->assertEqual(count($users), count($searchusers));
866             //update the test data
867             $user1->username = 'veryimprobabletestusername1_updated';
868             $user1->password = 'testpassword1_updated';
869             $user1->firstname = 'testfirstname1_updated';
870             $user1->lastname = 'testlastname1_updated';
871             $user1->email = 'testemail1_updated@moodle.com';
872             $user1->auth = 'manual';
873             $user1->idnumber = 'testidnumber1_updated';
874             $user1->lang = 'en';
875             $user1->theme = 'standard';
876             $user1->timezone = 98;
877             $user1->mailformat = 1;
878             $user1->description = 'Hello World!_updated';
879             $user1->city = 'testcity1_updated';
880             $user1->country = 'au';
881             $preferencename1 = 'preference1';
882             $preferencename2 = 'preference2';
883             $user1->preferences = array(
884             array('type' => $preferencename1, 'value' => 'preferencevalue1_updated'),
885             array('type' => $preferencename2, 'value' => 'preferencevalue2_updated'));
886             $customfieldname1 = 'testdatacustom1';
887             $customfieldname2 = 'testdatacustom2';
888             $user1->customfields = array(
889             array('type' => $customfieldname1, 'value' => 'customvalue_updated'),
890             array('type' => $customfieldname2, 'value' => 'customvalue2_updated'));
891             $user2->username = 'veryimprobabletestusername2_updated';
892             $user2->password = 'testpassword2_updated';
893             $user2->firstname = 'testfirstname2_updated';
894             $user2->lastname = 'testlastname2_updated';
895             $user2->email = 'testemail1_updated@moodle.com';
896             $users = array($user1, $user2);
897             
898             //update the users by web service
899             $function = 'moodle_user_update_users';
900             $params = array('users' => $users);
901             $client->call($function, $params);
903             //compare DB user with the test data
904             $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
905             $this->assertEqual($dbuser1->firstname, $user1->firstname);
906             $this->assertEqual($dbuser1->password,
907                     hash_internal_user_password($user1->password));
908             $this->assertEqual($dbuser1->lastname, $user1->lastname);
909             $this->assertEqual($dbuser1->email, $user1->email);
910             $this->assertEqual($dbuser1->auth, $user1->auth);
911             $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
912             $this->assertEqual($dbuser1->lang, $user1->lang);
913             $this->assertEqual($dbuser1->theme, $user1->theme);
914             $this->assertEqual($dbuser1->timezone, $user1->timezone);
915             $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
916             $this->assertEqual($dbuser1->description, $user1->description);
917             $this->assertEqual($dbuser1->city, $user1->city);
918             $this->assertEqual($dbuser1->country, $user1->country);
919             $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
920                             null, $dbuser1->id);
921             $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
922             $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
923                             null, $dbuser1->id);
924             $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
925             require_once($CFG->dirroot . "/user/profile/lib.php");
926             $customfields = profile_user_record($dbuser1->id);
928             $customfields = (array) $customfields;
929             $customfieldname1 = $user1->customfields[0]['type'];
930             $customfieldname2 = $user1->customfields[1]['type'];
931             $this->assertEqual($customfields[$customfieldname1],
932                     $user1->customfields[0]['value']);
933             $this->assertEqual($customfields[$customfieldname2],
934                     $user1->customfields[1]['value']);
936             $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
937             $this->assertEqual($dbuser2->firstname, $user2->firstname);
938             $this->assertEqual($dbuser2->password,
939                     hash_internal_user_password($user2->password));
940             $this->assertEqual($dbuser2->lastname, $user2->lastname);
941             $this->assertEqual($dbuser2->email, $user2->email);
943             //unset preferences
944             $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
946             //clear custom fields data
947             $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
949             //delete custom fields
950             $DB->delete_records_list('user_info_field', 'shortname',
951                     array($customfieldname1, $customfieldname2));
953             //delete users from DB
954             $DB->delete_records_list('user', 'id',
955                     array($dbuser1->id, $dbuser2->id));
957         }
958     }
960     function moodle_role_assign($client) {
961         global $DB, $CFG;
963         $searchusers = $DB->get_records_list('user', 'username',
964                 array('veryimprobabletestusername2'));
965         $searchroles = $DB->get_records_list('role', 'shortname',
966                 array('role1thatshouldnotexist', 'role2thatshouldnotexist'));
968         if (empty($searchusers) and empty($searchroles)) {
970             //create a temp user
971             $user = new stdClass();
972             $user->username = 'veryimprobabletestusername2';
973             $user->password = 'testpassword2';
974             $user->firstname = 'testfirstname2';
975             $user->lastname = 'testlastname2';
976             $user->email = 'testemail1@moodle.com';
977             require_once($CFG->dirroot."/user/lib.php");
978             $user->id = user_create_user($user);
980             //create two roles
981             $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
982             $role2->id = create_role('role2thatshouldnotexist', 'role2thatshouldnotexist', '');
984             //assign user to role by webservice
985             $context = get_system_context();
986             $assignments = array(
987                 array('roleid' => $role1->id, 'userid' => $user->id, 'contextid' => $context->id),
988                 array('roleid' => $role2->id, 'userid' => $user->id, 'contextid' => $context->id)
989             );
991             $function = 'moodle_role_assign';
992             $params = array('assignments' => $assignments);
993             $client->call($function, $params);
995             //check that the assignment work
996             $roles = get_user_roles($context, $user->id, false);
997             foreach ($roles as $role) {
998                 $this->assertTrue(($role->roleid == $role1->id) or ($role->roleid == $role2->id) );
999             }
1001             //unassign roles from user
1002             role_unassign($role1->id, $user->id, $context->id, '', NULL);
1003             role_unassign($role2->id, $user->id, $context->id, '', NULL);
1005             //delete user from DB
1006             $DB->delete_records('user', array('id' => $user->id));
1008             //delete the two role from DB
1009             delete_role($role1->id);
1010             delete_role($role2->id);
1011         }
1012     }
1014     function moodle_role_unassign($client) {
1015         global $DB, $CFG;
1017         $searchusers = $DB->get_records_list('user', 'username',
1018                 array('veryimprobabletestusername2'));
1019         $searchroles = $DB->get_records_list('role', 'shortname',
1020                 array('role1thatshouldnotexist', 'role2thatshouldnotexist'));
1022         if (empty($searchusers) and empty($searchroles)) {
1024             //create a temp user
1025             $user = new stdClass();
1026             $user->username = 'veryimprobabletestusername2';
1027             $user->password = 'testpassword2';
1028             $user->firstname = 'testfirstname2';
1029             $user->lastname = 'testlastname2';
1030             $user->email = 'testemail1@moodle.com';
1031             require_once($CFG->dirroot."/user/lib.php");
1032             $user->id = user_create_user($user);
1034             //create two roles
1035             $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1036             $role2->id = create_role('role2thatshouldnotexist', 'role2thatshouldnotexist', '');
1037         
1038             //assign roles from user
1039             $context = get_system_context();
1040             role_assign($role1->id, $user->id, $context->id);
1041             role_assign($role2->id, $user->id, $context->id);
1043             //check that the local assignment work
1044             $roles = get_user_roles($context, $user->id, false);
1045             foreach ($roles as $role) {
1046                 $this->assertTrue(($role->roleid == $role1->id) or ($role->roleid == $role2->id) );
1047             }
1049             //unassign user to role by webservice          
1050             $assignments = array(
1051                 array('roleid' => $role1->id, 'userid' => $user->id, 'contextid' => $context->id),
1052                 array('roleid' => $role2->id, 'userid' => $user->id, 'contextid' => $context->id)
1053             );
1054             $function = 'moodle_role_unassign';
1055             $params = array('assignments' => $assignments);
1056             $client->call($function, $params);
1058             //check that the web service unassignment work
1059             $roles = get_user_roles($context, $user->id, false);
1060             $this->assertTrue(empty($roles));
1062             //delete user from DB
1063             $DB->delete_records('user', array('id' => $user->id));
1065             //delete the two role from DB
1066             delete_role($role1->id);
1067             delete_role($role2->id);
1068         }
1070     }
1072     /**
1073      * READ ONLY test
1074      * TODO: find a better solution that running web service for each course
1075      * in the system
1076      * For each courses, test the number of groups
1077      * @param object $client
1078      */
1079     function moodle_group_get_course_groups($client) {
1080         global $DB;
1082         $courses = $DB->get_records('course');
1083         foreach($courses as $course) {
1084             $coursegroups = groups_get_all_groups($course->id);
1085             $function = 'moodle_group_get_course_groups';
1086             $params = array('courseid' => $course->id);
1087             $groups = $client->call($function, $params);
1088             $this->assertEqual(count($groups), count($coursegroups));
1089         }
1090     }
1093     /**
1094      * READ ONLY test
1095      * Test that the same number of members are returned
1096      * for each existing group in the system
1097      * @param object $client
1098      */
1099     function moodle_group_get_groupmembers($client) {
1100         global $DB;
1102         $groups = $DB->get_records('groups');
1103         $groupids = array();
1104         foreach ($groups as $group) {
1105             $groupids[] = $group->id;
1106         }
1107         $function = 'moodle_group_get_groupmembers';
1108         $params = array('groupids' => $groupids);
1109         $groupsmembers = $client->call($function, $params);
1111         foreach($groupsmembers as $groupmembers) {
1112             $dbgroupmembers = groups_get_members($groupmembers['groupid']);
1113             unset($groups[$groupmembers['groupid']]);
1114             $this->assertEqual(count($dbgroupmembers), count($groupmembers['userids']));
1115         }
1117         //check that all existing groups have been returned by the web service function
1118         $this->assertTrue(empty($groups));
1119        
1120         
1121     }
1123     function moodle_group_add_groupmembers($client) {
1124         global $DB, $CFG;
1126         //create category
1127         $category = new stdClass();
1128         $category->name = 'tmpcategoryfortest123';
1129         $category->id = $DB->insert_record('course_categories', $category);
1131         //create a course
1132         $course = new stdClass();
1133         $course->fullname = 'tmpcoursefortest123';
1134         $course->shortname = 'tmpcoursefortest123';
1135         $course->idnumber = 'tmpcoursefortest123';
1136         $course->category = $category->id;
1137         $course->id = $DB->insert_record('course', $course);
1139         //create a role
1140         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1142         //create a user
1143         $user = new stdClass();
1144         $user->username = 'veryimprobabletestusername2';
1145         $user->password = 'testpassword2';
1146         $user->firstname = 'testfirstname2';
1147         $user->lastname = 'testlastname2';
1148         $user->email = 'testemail1@moodle.com';
1149         $user->mnethostid = $CFG->mnet_localhost_id;
1150         require_once($CFG->dirroot."/user/lib.php");
1151         $user->id = user_create_user($user);
1153         //create course context
1154         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1156         //enrol the user in the course with the created role
1157         role_assign($role1->id, $user->id, $context->id);
1158         $enrol = new stdClass();
1159         $enrol->courseid = $course->id;
1160         $enrol->roleid = $role1->id;
1161         $enrol->id = $DB->insert_record('enrol', $enrol);
1162         $enrolment = new stdClass();
1163         $enrolment->userid = $user->id;
1164         $enrolment->enrolid = $enrol->id;
1165         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1167         //create a group in the course
1168         $group = new stdClass();
1169         $group->courseid = $course->id;
1170         $group->name = 'tmpgroufortest123';
1171         $group->id = $DB->insert_record('groups', $group);
1173         //WEBSERVICE CALL
1174         $function = 'moodle_group_add_groupmembers';
1175         $params = array('members' => array(array('groupid' => $group->id, 'userid' => $user->id)));
1176         $groupsmembers = $client->call($function, $params);
1178         //CHECK TEST RESULT
1179         require_once($CFG->libdir . '/grouplib.php');
1180         $groupmembers = groups_get_members($group->id);
1181         $this->assertEqual(count($groupmembers), 1);
1182         $this->assertEqual($groupmembers[$user->id]->id, $user->id);
1184         //remove the members from the group
1185         require_once($CFG->dirroot . "/group/lib.php");
1186         groups_remove_member($group->id, $user->id);
1188         //delete the group
1189         $DB->delete_records('groups', array('id' => $group->id));
1191         //unenrol the user
1192         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1193         $DB->delete_records('enrol', array('id' => $enrol->id));
1194         role_unassign($role1->id, $user->id, $context->id);
1196         //delete course context
1197         delete_context(CONTEXT_COURSE, $course->id);
1199         //delete the user
1200         $DB->delete_records('user', array('id' => $user->id));
1202         //delete the role
1203         delete_role($role1->id);
1205         //delete the course
1206         $DB->delete_records('course', array('id' => $course->id));
1208         //delete the category
1209         $DB->delete_records('course_categories', array('id' => $category->id));
1210         
1211     }
1213     function moodle_group_delete_groupmembers($client) {
1214         global $DB, $CFG;
1216         //create category
1217         $category = new stdClass();
1218         $category->name = 'tmpcategoryfortest123';
1219         $category->id = $DB->insert_record('course_categories', $category);
1221         //create a course
1222         $course = new stdClass();
1223         $course->fullname = 'tmpcoursefortest123';
1224         $course->shortname = 'tmpcoursefortest123';
1225         $course->idnumber = 'tmpcoursefortest123';
1226         $course->category = $category->id;
1227         $course->id = $DB->insert_record('course', $course);
1229         //create a role
1230         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1232         //create a user
1233         $user = new stdClass();
1234         $user->username = 'veryimprobabletestusername2';
1235         $user->password = 'testpassword2';
1236         $user->firstname = 'testfirstname2';
1237         $user->lastname = 'testlastname2';
1238         $user->email = 'testemail1@moodle.com';
1239         $user->mnethostid = $CFG->mnet_localhost_id;
1240         require_once($CFG->dirroot."/user/lib.php");
1241         $user->id = user_create_user($user);
1243         //create course context
1244         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1246         //enrol the user in the course with the created role
1247         role_assign($role1->id, $user->id, $context->id);
1248         $enrol = new stdClass();
1249         $enrol->courseid = $course->id;
1250         $enrol->roleid = $role1->id;
1251         $enrol->id = $DB->insert_record('enrol', $enrol);
1252         $enrolment = new stdClass();
1253         $enrolment->userid = $user->id;
1254         $enrolment->enrolid = $enrol->id;
1255         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1257         //create a group in the course
1258         $group = new stdClass();
1259         $group->courseid = $course->id;
1260         $group->name = 'tmpgroufortest123';
1261         $group->id = $DB->insert_record('groups', $group);
1263         //add group member
1264         require_once($CFG->dirroot . "/group/lib.php");
1265         groups_add_member($group->id, $user->id);
1266         $groupmembers = groups_get_members($group->id);
1267         $this->assertEqual(count($groupmembers), 1);
1269         //WEB SERVICE CALL - remove the members from the group
1270         $function = 'moodle_group_delete_groupmembers';
1271         $params = array('members' => array(array('groupid' => $group->id, 'userid' => $user->id)));
1272         $client->call($function, $params);
1274         require_once($CFG->libdir . '/grouplib.php');
1275         $groupmembers = groups_get_members($group->id);
1276         $this->assertEqual(count($groupmembers), 0);
1278         //delete the group
1279         $DB->delete_records('groups', array('id' => $group->id));
1281         //unenrol the user
1282         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1283         $DB->delete_records('enrol', array('id' => $enrol->id));
1284         role_unassign($role1->id, $user->id, $context->id);
1286         //delete course context
1287         delete_context(CONTEXT_COURSE, $course->id);
1289         //delete the user
1290         $DB->delete_records('user', array('id' => $user->id));
1292         //delete the role
1293         delete_role($role1->id);
1295         //delete the course
1296         $DB->delete_records('course', array('id' => $course->id));
1298         //delete the category
1299         $DB->delete_records('course_categories', array('id' => $category->id));
1301     }
1303     function moodle_group_create_groups($client) {
1304         global $DB, $CFG;
1306         //create category
1307         $category = new stdClass();
1308         $category->name = 'tmpcategoryfortest123';
1309         $category->id = $DB->insert_record('course_categories', $category);
1311         //create a course
1312         $course = new stdClass();
1313         $course->fullname = 'tmpcoursefortest123';
1314         $course->shortname = 'tmpcoursefortest123';
1315         $course->idnumber = 'tmpcoursefortest123';
1316         $course->category = $category->id;
1317         $course->id = $DB->insert_record('course', $course);
1319         //create a role
1320         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1322         //create a user
1323         $user = new stdClass();
1324         $user->username = 'veryimprobabletestusername2';
1325         $user->password = 'testpassword2';
1326         $user->firstname = 'testfirstname2';
1327         $user->lastname = 'testlastname2';
1328         $user->email = 'testemail1@moodle.com';
1329         $user->mnethostid = $CFG->mnet_localhost_id;
1330         require_once($CFG->dirroot."/user/lib.php");
1331         $user->id = user_create_user($user);
1333         //create course context
1334         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1336         //enrol the user in the course with the created role
1337         role_assign($role1->id, $user->id, $context->id);
1338         $enrol = new stdClass();
1339         $enrol->courseid = $course->id;
1340         $enrol->roleid = $role1->id;
1341         $enrol->id = $DB->insert_record('enrol', $enrol);
1342         $enrolment = new stdClass();
1343         $enrolment->userid = $user->id;
1344         $enrolment->enrolid = $enrol->id;
1345         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1347         require_once($CFG->dirroot . "/group/lib.php");
1348         $groups = groups_get_all_groups($course->id);
1349         $this->assertEqual(count($groups), 0);
1351         //WEBSERVICE CALL - create a group in the course
1352         $group = new stdClass();
1353         $group->courseid = $course->id;
1354         $group->name = 'tmpgroufortest123';
1355         $group->enrolmentkey = '';
1356         $group->description = '';
1357         $group2 = new stdClass();
1358         $group2->courseid = $course->id;
1359         $group2->name = 'tmpgroufortest1233';
1360         $group2->enrolmentkey = '';
1361         $group2->description = '';
1362         $paramgroups = array($group, $group2);
1363         $function = 'moodle_group_create_groups';
1364         $params = array('groups' => $paramgroups);
1365         $createdgroups = $client->call($function, $params);
1367         $groups = groups_get_all_groups($course->id);
1368         $this->assertEqual(count($groups), count($paramgroups));
1370         //delete the group
1371         foreach ($groups as $dbgroup) {
1372             $DB->delete_records('groups', array('id' => $dbgroup->id));
1373         }
1375         //unenrol the user
1376         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1377         $DB->delete_records('enrol', array('id' => $enrol->id));
1378         role_unassign($role1->id, $user->id, $context->id);
1380         //delete course context
1381         delete_context(CONTEXT_COURSE, $course->id);
1383         //delete the user
1384         $DB->delete_records('user', array('id' => $user->id));
1386         //delete the role
1387         delete_role($role1->id);
1389         //delete the course
1390         $DB->delete_records('course', array('id' => $course->id));
1392         //delete the category
1393         $DB->delete_records('course_categories', array('id' => $category->id));
1395     }
1397     function moodle_group_delete_groups($client) {
1398         global $DB, $CFG;
1400         //create category
1401         $category = new stdClass();
1402         $category->name = 'tmpcategoryfortest123';
1403         $category->id = $DB->insert_record('course_categories', $category);
1405         //create a course
1406         $course = new stdClass();
1407         $course->fullname = 'tmpcoursefortest123';
1408         $course->shortname = 'tmpcoursefortest123';
1409         $course->idnumber = 'tmpcoursefortest123';
1410         $course->category = $category->id;
1411         $course->id = $DB->insert_record('course', $course);
1413         //create a role
1414         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1416         //create a user
1417         $user = new stdClass();
1418         $user->username = 'veryimprobabletestusername2';
1419         $user->password = 'testpassword2';
1420         $user->firstname = 'testfirstname2';
1421         $user->lastname = 'testlastname2';
1422         $user->email = 'testemail1@moodle.com';
1423         $user->mnethostid = $CFG->mnet_localhost_id;
1424         require_once($CFG->dirroot."/user/lib.php");
1425         $user->id = user_create_user($user);
1427         //create course context
1428         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1430         //enrol the user in the course with the created role
1431         role_assign($role1->id, $user->id, $context->id);
1432         $enrol = new stdClass();
1433         $enrol->courseid = $course->id;
1434         $enrol->roleid = $role1->id;
1435         $enrol->id = $DB->insert_record('enrol', $enrol);
1436         $enrolment = new stdClass();
1437         $enrolment->userid = $user->id;
1438         $enrolment->enrolid = $enrol->id;
1439         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1441         //create a group in the course
1442         $group = new stdClass();
1443         $group->courseid = $course->id;
1444         $group->name = 'tmpgroufortest123';
1445         $group->enrolmentkey = '';
1446         $group->description = '';
1447         $group->id = $DB->insert_record('groups', $group);
1448         $group2 = new stdClass();
1449         $group2->courseid = $course->id;
1450         $group2->name = 'tmpgroufortest1233';
1451         $group2->enrolmentkey = '';
1452         $group2->description = '';
1453         $group2->id = $DB->insert_record('groups', $group2);
1454         $paramgroups = array($group, $group2);
1456         require_once($CFG->dirroot . "/group/lib.php");
1457         $groups = groups_get_all_groups($course->id);
1458         $this->assertEqual(2, count($groups));
1460         //WEBSERVICE CALL -  delete the group
1461         $function = 'moodle_group_delete_groups';
1462         $params = array('groupids' => array($group->id, $group2->id));
1463         $client->call($function, $params);
1465         $groups = groups_get_all_groups($course->id);
1466         $this->assertEqual(0, count($groups));
1468         //unenrol the user
1469         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1470         $DB->delete_records('enrol', array('id' => $enrol->id));
1471         role_unassign($role1->id, $user->id, $context->id);
1473         //delete course context
1474         delete_context(CONTEXT_COURSE, $course->id);
1476         //delete the user
1477         $DB->delete_records('user', array('id' => $user->id));
1479         //delete the role
1480         delete_role($role1->id);
1482         //delete the course
1483         $DB->delete_records('course', array('id' => $course->id));
1485         //delete the category
1486         $DB->delete_records('course_categories', array('id' => $category->id));
1487     }
1489     function moodle_message_send_messages($client) {
1490         global $DB;
1491         $function = 'moodle_message_send_messages';
1492         $message = array();
1493         $message['text'] = 'this is a message with a link http://www.google.com';
1494         $message['touserid'] = 2;  //replace by a existing user id
1495         $message['clientmsgid'] = 'message_1';
1496         $message2 = array();
1497         $message2['text'] = 'this is a message with an image
1498             http://moodle.org/pluginfile.php/51/mod_forum/post/713724/moodle2-logo.png';
1499         $message2['touserid'] = 2;  //replace by a existing user id
1500         $message2['clientmsgid'] = 'message_2';
1501         $params = array('messages' => array($message, $message2));
1502         $success = $client->call($function, $params);
1503         $this->assertEqual(count($success), 2);
1504     }
1506      function moodle_notes_create_notes($client) {
1507         global $DB, $CFG;
1509         $note1 = array();
1510         $note1['userid'] = 2; //about who is the note
1511         $note1['publishstate'] = 'personal'; //can be course, site, personal
1512         $note1['courseid'] = 2; //in Moodle a notes is always created into a course, even a site note.
1513         $note1['text'] = 'This is a personal note about the user';
1514         $note1['clientnoteid'] = 'note_1';
1516         $note2 = array();
1517         $note2['userid'] = 40000; //mostl likely going to fail
1518         $note2['publishstate'] = 'course';
1519         $note2['courseid'] = 2;
1520         $note2['text'] = 'This is a teacher note about the user';
1521         $note2['clientnoteid'] = 'note_2';
1523         $note3 = array();
1524         $note3['userid'] = 2;
1525         $note3['publishstate'] = 'site';
1526         $note3['courseid'] = 30000; //most likely going to fail
1527         $note3['text'] = 'This is a teacher site-wide note about the user';
1528         $note3['clientnoteid'] = 'note_3';
1530         $function = 'moodle_notes_create_notes';
1531         $params = array('notes' => array($note1, $note2, $note3));
1532         $notes = $client->call($function, $params);
1534         $this->assertEqual(3, count($notes)); //1 info is a success, 2 others should be failed
1535     }