MDL-27565 implement moodle_notes_create_note
[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_notes_create_notes' => false
93         );
95         //performance testing: number of time the web service are run
96         $this->iteration = 1;
98         //DO NOT CHANGE
99         //reset the timers
100         $this->timerrest = 0;
101         $this->timerxmlrpc = 0;
102         $this->timersoap = 0;
103     }
105     function testRun() {
106         global $CFG;
108         if (!$this->testrest and !$this->testxmlrpc and !$this->testsoap) {
109             print_r("Web service unit tests are not run as not setup.
110                 (see /webservice/simpletest/testwebservice.php)");
111         }
113         if (!empty($this->testtoken)) {
115             //Does not work till XML => PHP is implemented (MDL-22965)
116             if ($this->testrest) {
118                 $this->timerrest = time();
120                 require_once($CFG->dirroot . "/webservice/rest/lib.php");
121                 $restclient = new webservice_rest_client($CFG->wwwroot
122                                 . '/webservice/rest/server.php', $this->testtoken);
124                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
125                     foreach ($this->readonlytests as $functioname => $run) {
126                         if ($run) {
127                             //$this->{$functioname}($restclient);
128                         }
129                     }
130                     foreach ($this->writetests as $functioname => $run) {
131                         if ($run) {
132                             //$this->{$functioname}($restclient);
133                         }
134                     }
135                 }
137                 $this->timerrest = time() - $this->timerrest;
138                 //here you could call a log function to display the timer
139                 //example:
140                 //error_log('REST time: ');
141                 //error_log(print_r($this->timerrest));
142             }
144             if ($this->testxmlrpc) {
146                 $this->timerxmlrpc = time();
148                 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
149                 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
150                                 . '/webservice/xmlrpc/server.php', $this->testtoken);
152                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
153                     foreach ($this->readonlytests as $functioname => $run) {
154                         if ($run) {
155                             $this->{$functioname}($xmlrpcclient);
156                         }
157                     }
158                     foreach ($this->writetests as $functioname => $run) {
159                         if ($run) {
160                             $this->{$functioname}($xmlrpcclient);
161                         }
162                     }
163                 }
165                 $this->timerxmlrpc = time() - $this->timerxmlrpc;
166                 //here you could call a log function to display the timer
167                 //example:
168                 //error_log('XML-RPC time: ');
169                 //error_log(print_r($this->timerxmlrpc));
170             }
172             if ($this->testsoap) {
174                 $this->timersoap = time();
176                 require_once($CFG->dirroot . "/webservice/soap/lib.php");
177                 $soapclient = new webservice_soap_client($CFG->wwwroot
178                                 . '/webservice/soap/server.php', $this->testtoken,
179                         array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //force SOAP synchronous mode
180                                                                      //when function return null
181                 $soapclient->setWsdlCache(false);
183                 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
184                     foreach ($this->readonlytests as $functioname => $run) {
185                         if ($run) {
186                             $this->{$functioname}($soapclient);
187                         }
188                     }
189                     foreach ($this->writetests as $functioname => $run) {
190                         if ($run) {
191                             $this->{$functioname}($soapclient);
192                         }
193                     }
194                 }
196                 $this->timersoap = time() - $this->timersoap;
197                 //here you could call a log function to display the timer
198                 //example:
199                 //error_log('SOAP time: ');
200                 //error_log(print_r($this->timersoap));
201             }
202         }
203     }
205     ///// WEB SERVICE TEST FUNCTIONS
207     function moodle_group_get_groups($client) {
208         global $DB;
209         $dbgroups = $DB->get_records('groups');
210         $groupids = array();
211         foreach ($dbgroups as $dbgroup) {
212             $groupids[] = $dbgroup->id;
213         }
214         $function = 'moodle_group_get_groups';
216         $params = array('groupids' => $groupids);
217         $groups = $client->call($function, $params);
218         $this->assertEqual(count($groups), count($groupids));
219     }
221     function moodle_user_get_users_by_id($client) {
222         global $DB;
223         $dbusers = $DB->get_records('user', array('deleted' => 0));
224         $userids = array();
225         foreach ($dbusers as $dbuser) {
226             $userids[] = $dbuser->id;
227         }
228         $function = 'moodle_user_get_users_by_id';
230         $params = array('userids' => $userids);
231         $users = $client->call($function, $params);
233         $this->assertEqual(count($users), count($userids));
234     }
236     /**
237      * This test will:
238      * 1- create a user (core call)
239      * 2- enrol this user in the courses supporting enrolment
240      * 3- unenrol this user (core call)
241      */
242     function moodle_enrol_manual_enrol_users($client) {
243         global $DB, $CFG;
245         require_once($CFG->dirroot . "/user/lib.php");
246         require_once($CFG->dirroot . "/user/profile/lib.php");
247         require_once($CFG->dirroot . "/lib/enrollib.php");
249         //Delete some previous test data
250         if ($user = $DB->get_record('user', array('username' => 'veryimprobabletestusername2'))) {
251             $DB->delete_records('user', array('id' => $user->id));
252         }
253         if ($role = $DB->get_record('role', array('shortname' => 'role1thatshouldnotexist'))) {
254             set_role_contextlevels($role->id, array(CONTEXT_COURSE));
255             delete_role($role->id);
256         }
258         //create a user
259         $user = new stdClass();
260         $user->username = 'veryimprobabletestusername2';
261         $user->password = 'testpassword2';
262         $user->firstname = 'testfirstname2';
263         $user->lastname = 'testlastname2';
264         $user->email = 'testemail1@moodle.com';
265         $user->id = user_create_user($user);
267         $roleid = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
268         set_role_contextlevels($roleid, array(CONTEXT_COURSE));
270         $enrolments = array();
271         $courses = $DB->get_records('course');
273         foreach ($courses as $course) {
274             if ($course->id > 1) {
275                 $enrolments[] = array('roleid' => $roleid,
276                     'userid' => $user->id, 'courseid' => $course->id);
277                 $enrolledcourses[] = $course;
278             }
279         }
281         //web service call
282         $function = 'moodle_enrol_manual_enrol_users';
283         $wsparams = array('enrolments' => $enrolments);
284         $enrolmentsresult = $client->call($function, $wsparams);
286         //get instance that can unenrol
287         $enrols = enrol_get_plugins(true);
288         $enrolinstances = enrol_get_instances($course->id, true);
289         $unenrolled = false;
290         foreach ($enrolinstances as $instance) {
291             if (!$unenrolled and $enrols[$instance->enrol]->allow_unenrol($instance)) {
292                 $unenrolinstance = $instance;
293                 $unenrolled = true;
294             }
295         }
297         //test and unenrol the user
298         $enrolledusercourses = enrol_get_users_courses($user->id);
299         foreach ($enrolledcourses as $course) {
300             //test
301             $this->assertEqual(true, isset($enrolledusercourses[$course->id]));
303             //unenrol the user
304             $enrols[$unenrolinstance->enrol]->unenrol_user($unenrolinstance, $user->id, $roleid);
305         }
307         //delete user
308         $DB->delete_records('user', array('id' => $user->id));
310         //delete the context level
311         set_role_contextlevels($roleid, array(CONTEXT_COURSE));
313         //delete role
314         delete_role($roleid);
315     }
318     function moodle_enrol_get_enrolled_users($client) {
319         global $DB;
321         //function settings
322         $withcapability = '';
323         $groupid = null;
324         $onlyactive = false;
326         $dbcourses = $DB->get_records('course');
327         $function = 'moodle_enrol_get_enrolled_users';
329         foreach ($dbcourses as $dbcourse) {
331             $params = array();
333             $coursecontext = get_context_instance(CONTEXT_COURSE, $dbcourse->id);
335             list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
336             $sql = "SELECT DISTINCT ue.userid, e.courseid
337                       FROM {user_enrolments} ue
338                       JOIN {enrol} e ON (e.id = ue.enrolid)
339                      WHERE e.courseid = :courseid AND ue.userid IN ($sql)";
341             $params['courseid'] = $dbcourse->id;
343             $enrolledusers = $DB->get_records_sql($sql, $params);
345             $wsparams = array('courseid' => $dbcourse->id, 'withcapability' => $withcapability,
346                 'groupid' => $groupid, 'onlyactive' => $onlyactive);
347             $resultusers = $client->call($function, $wsparams);
349             $this->assertEqual(count($resultusers), count($enrolledusers));
350         }
351     }
353     function moodle_course_get_courses($client) {
354         global $DB;
356         $function = 'moodle_course_get_courses';
358         //retrieve all courses from db
359         $dbcourses = $DB->get_records('course');
360         $courseids = array();
361         foreach ($dbcourses as $dbcourse) {
362             $courseids[] = $dbcourse->id;
363         }
365         //retrieve all courses by id
366         $params = array('options' => array('ids' => $courseids));
367         $courses = $client->call($function, $params);
369         //check it is the same course count
370         $this->assertEqual(count($courses), count($courseids));
372         //check all course values are identic
373         foreach ($courses as $course) {
374             $this->assertEqual($course['fullname'],
375                     $dbcourses[$course['id']]->fullname);
376             $this->assertEqual($course['shortname'],
377                     $dbcourses[$course['id']]->shortname);
378             $this->assertEqual($course['categoryid'],
379                     $dbcourses[$course['id']]->category);
380             $this->assertEqual($course['categorysortorder'],
381                     $dbcourses[$course['id']]->sortorder);
382             $this->assertEqual($course['idnumber'],
383                     $dbcourses[$course['id']]->idnumber);
384             $this->assertEqual($course['summary'],
385                     $dbcourses[$course['id']]->summary);
386             $this->assertEqual($course['summaryformat'],
387                     $dbcourses[$course['id']]->summaryformat);
388             $this->assertEqual($course['format'],
389                     $dbcourses[$course['id']]->format);
390             $this->assertEqual($course['showgrades'],
391                     $dbcourses[$course['id']]->showgrades);
392             $this->assertEqual($course['newsitems'],
393                     $dbcourses[$course['id']]->newsitems);
394             $this->assertEqual($course['startdate'],
395                     $dbcourses[$course['id']]->startdate);
396             $this->assertEqual($course['numsections'],
397                     $dbcourses[$course['id']]->numsections);
398             $this->assertEqual($course['maxbytes'],
399                     $dbcourses[$course['id']]->maxbytes);
400             $this->assertEqual($course['visible'],
401                     $dbcourses[$course['id']]->visible);
402             $this->assertEqual($course['hiddensections'],
403                     $dbcourses[$course['id']]->hiddensections);
404             $this->assertEqual($course['groupmode'],
405                     $dbcourses[$course['id']]->groupmode);
406             $this->assertEqual($course['groupmodeforce'],
407                     $dbcourses[$course['id']]->groupmodeforce);
408             $this->assertEqual($course['defaultgroupingid'],
409                     $dbcourses[$course['id']]->defaultgroupingid);
410             $this->assertEqual($course['lang'],
411                     $dbcourses[$course['id']]->lang);
412             $this->assertEqual($course['timecreated'],
413                     $dbcourses[$course['id']]->timecreated);
414             $this->assertEqual($course['timemodified'],
415                     $dbcourses[$course['id']]->timemodified);
416             if (key_exists('enablecompletion', $course)) {
417                 $this->assertEqual($course['enablecompletion'],
418                         $dbcourses[$course['id']]->enablecompletion);
419             }
420             if (key_exists('completionstartonenrol', $course)) {
421                 $this->assertEqual($course['completionstartonenrol'],
422                         $dbcourses[$course['id']]->completionstartonenrol);
423             }
424             if (key_exists('completionnotify', $course)) {
425                 $this->assertEqual($course['completionnotify'],
426                         $dbcourses[$course['id']]->completionnotify);
427             }
428             $this->assertEqual($course['forcetheme'],
429                     $dbcourses[$course['id']]->theme);
430         }
431     }
433     function moodle_course_create_courses($client) {
434         global $DB, $CFG;
436         ///Test data
437         $courseconfig = get_config('moodlecourse');
439         $themeobjects = get_list_of_themes();
440         $theme = key($themeobjects);
441         $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
442         $categoryid = empty($categoryid) ? 0 : $categoryid;
444         $course1 = new stdClass();
445         $course1->fullname = 'Test Data create course 1';
446         $course1->shortname = 'testdatacourse1';
447         $course1->categoryid = $categoryid;
448         $course1->idnumber = '328327982372342343234';
449         $course1->summary = 'This is a summary';
450         $course1->summaryformat = FORMAT_HTML;
451         $course1->format = $courseconfig->format;
452         $course1->showgrades = $courseconfig->showgrades;
453         $course1->showreports = $courseconfig->showreports;
454         $course1->newsitems = $courseconfig->newsitems;
455         $course1->startdate = time();
456         $course1->numsections = $courseconfig->numsections;
457         $course1->maxbytes = $courseconfig->maxbytes;
458         $course1->visible = $courseconfig->visible;
459         $course1->hiddensections = $courseconfig->hiddensections;
460         $course1->groupmode = $courseconfig->groupmode;
461         $course1->groupmodeforce = $courseconfig->groupmodeforce;
462         $course1->defaultgroupingid = 0;
463         if (!empty($courseconfig->lang)) {
464             $course1->lang = $courseconfig->lang;
465         }
466         $course1->enablecompletion = $courseconfig->enablecompletion;
467         $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
468         $course1->completionnotify = 0;
469         $course1->forcetheme = $theme;
471         $course2 = new stdClass();
472         $course2->fullname = 'Test Data create course 2';
473         $course2->shortname = 'testdatacourse2';
474         $course2->categoryid = $categoryid;
476         $courses = array($course1, $course2);
478         //do not run the test if course1 or course2 already exists
479         $existingcourses = $DB->get_records_list('course', 'fullname',
480                         array($course1->fullname, $course2->fullname));
481         if (!empty($existingcourses)) {
482             throw new moodle_exception('testdatacoursesalreadyexist');
483         }
485         $function = 'moodle_course_create_courses';
486         $params = array('courses' => $courses);
487         $resultcourses = $client->call($function, $params);
488         $this->assertEqual(count($courses), count($resultcourses));
490         //retrieve user1 from the DB and check values
491         $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
492         $this->assertEqual($dbcourse1->fullname, $course1->fullname);
493         $this->assertEqual($dbcourse1->shortname, $course1->shortname);
494         $this->assertEqual($dbcourse1->category, $course1->categoryid);
495         $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
496         $this->assertEqual($dbcourse1->summary, $course1->summary);
497         $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
498         $this->assertEqual($dbcourse1->format, $course1->format);
499         $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
500         $this->assertEqual($dbcourse1->showreports, $course1->showreports);
501         $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
502         $this->assertEqual($dbcourse1->startdate, $course1->startdate);
503         $this->assertEqual($dbcourse1->numsections, $course1->numsections);
504         $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
505         $this->assertEqual($dbcourse1->visible, $course1->visible);
506         $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
507         $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
508         $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
509         $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
510         if (!empty($courseconfig->lang)) {
511             $this->assertEqual($dbcourse1->lang, $course1->lang);
512         }
513         if (completion_info::is_enabled_for_site()) {
514             $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
515             $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
516         }
517         $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
518         if (!empty($CFG->allowcoursethemes)) {
519             $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
520         }
522         //retrieve user2 from the DB and check values
523         $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
524         $this->assertEqual($dbcourse2->fullname, $course2->fullname);
525         $this->assertEqual($dbcourse2->shortname, $course2->shortname);
526         $this->assertEqual($dbcourse2->category, $course2->categoryid);
527         $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
528         $this->assertEqual($dbcourse2->format, $courseconfig->format);
529         $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
530         $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
531         $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
532         $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
533         $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
534         $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
535         $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
536         $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
537         $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
538         $this->assertEqual($dbcourse2->defaultgroupingid, 0);
540         //delete users from DB
541         $DB->delete_records_list('course', 'id',
542                 array($dbcourse1->id, $dbcourse2->id));
543     }
545     function moodle_user_create_users($client) {
546         global $DB, $CFG;
548         //Test data
549         //a full user: user1
550         $user1 = new stdClass();
551         $user1->username = 'testusername1';
552         $user1->password = 'testpassword1';
553         $user1->firstname = 'testfirstname1';
554         $user1->lastname = 'testlastname1';
555         $user1->email = 'testemail1@moodle.com';
556         $user1->auth = 'manual';
557         $user1->idnumber = 'testidnumber1';
558         $user1->lang = 'en';
559         $user1->theme = 'standard';
560         $user1->timezone = 99;
561         $user1->mailformat = 0;
562         $user1->description = 'Hello World!';
563         $user1->city = 'testcity1';
564         $user1->country = 'au';
565         $preferencename1 = 'preference1';
566         $preferencename2 = 'preference2';
567         $user1->preferences = array(
568             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
569             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
570         $customfieldname1 = 'testdatacustom1';
571         $customfieldname2 = 'testdatacustom2';
572         $user1->customfields = array(
573             array('type' => $customfieldname1, 'value' => 'customvalue'),
574             array('type' => $customfieldname2, 'value' => 'customvalue2'));
575         //a small user: user2
576         $user2 = new stdClass();
577         $user2->username = 'testusername2';
578         $user2->password = 'testpassword2';
579         $user2->firstname = 'testfirstname2';
580         $user2->lastname = 'testlastname2';
581         $user2->email = 'testemail1@moodle.com';
583         $users = array($user1, $user2);
585         //do not run the test if user1 or user2 already exists
586         $existingusers = $DB->get_records_list('user', 'username',
587                         array($user1->username, $user2->username));
588         if (!empty($existingusers)) {
589             throw new moodle_exception('testdatausersalreadyexist');
590         }
592         //do not run the test if data test custom fields already exists
593         $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
594                         array($customfieldname1, $customfieldname2));
595         if (!empty($existingcustomfields)) {
596             throw new moodle_exception('testdatacustomfieldsalreadyexist');
597         }
599         //create the custom fields
600         $customfield = new stdClass();
601         $customfield->shortname = $customfieldname1;
602         $customfield->name = $customfieldname1;
603         $customfield->datatype = 'text';
604         $DB->insert_record('user_info_field', $customfield);
605         $customfield = new stdClass();
606         $customfield->shortname = $customfieldname2;
607         $customfield->name = $customfieldname2;
608         $customfield->datatype = 'text';
609         $DB->insert_record('user_info_field', $customfield);
611         $function = 'moodle_user_create_users';
612         $params = array('users' => $users);
613         $resultusers = $client->call($function, $params);
614         $this->assertEqual(count($users), count($resultusers));
616         //retrieve user1 from the DB and check values
617         $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
618         $this->assertEqual($dbuser1->firstname, $user1->firstname);
619         $this->assertEqual($dbuser1->password,
620                 hash_internal_user_password($user1->password));
621         $this->assertEqual($dbuser1->lastname, $user1->lastname);
622         $this->assertEqual($dbuser1->email, $user1->email);
623         $this->assertEqual($dbuser1->auth, $user1->auth);
624         $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
625         $this->assertEqual($dbuser1->lang, $user1->lang);
626         $this->assertEqual($dbuser1->theme, $user1->theme);
627         $this->assertEqual($dbuser1->timezone, $user1->timezone);
628         $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
629         $this->assertEqual($dbuser1->description, $user1->description);
630         $this->assertEqual($dbuser1->city, $user1->city);
631         $this->assertEqual($dbuser1->country, $user1->country);
632         $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
633                         null, $dbuser1->id);
634         $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
635         $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
636                         null, $dbuser1->id);
637         $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
638         require_once($CFG->dirroot . "/user/profile/lib.php");
639         $customfields = profile_user_record($dbuser1->id);
641         $customfields = (array) $customfields;
642         $customfieldname1 = $user1->customfields[0]['type'];
643         $customfieldname2 = $user1->customfields[1]['type'];
644         $this->assertEqual($customfields[$customfieldname1],
645                 $user1->customfields[0]['value']);
646         $this->assertEqual($customfields[$customfieldname2],
647                 $user1->customfields[1]['value']);
650         //retrieve user2 from the DB and check values
651         $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
652         $this->assertEqual($dbuser2->firstname, $user2->firstname);
653         $this->assertEqual($dbuser2->password,
654                 hash_internal_user_password($user2->password));
655         $this->assertEqual($dbuser2->lastname, $user2->lastname);
656         $this->assertEqual($dbuser2->email, $user2->email);
658         //unset preferences
659         $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
661         //clear custom fields data
662         $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
664         //delete custom fields
665         $DB->delete_records_list('user_info_field', 'shortname',
666                 array($customfieldname1, $customfieldname2));
668         //delete users from DB
669         $DB->delete_records_list('user', 'id',
670                 array($dbuser1->id, $dbuser2->id));
671     }
673     function moodle_user_delete_users($client) {
674         global $DB, $CFG;
676         //Set test data
677         //a full user: user1
678         $user1 = new stdClass();
679         $user1->username = 'veryimprobabletestusername1';
680         $user1->password = 'testpassword1';
681         $user1->firstname = 'testfirstname1';
682         $user1->lastname = 'testlastname1';
683         $user1->email = 'testemail1@moodle.com';
684         $user1->auth = 'manual';
685         $user1->idnumber = 'testidnumber1';
686         $user1->lang = 'en';
687         $user1->theme = 'standard';
688         $user1->timezone = 99;
689         $user1->mailformat = 0;
690         $user1->description = 'Hello World!';
691         $user1->city = 'testcity1';
692         $user1->country = 'au';
693         $preferencename1 = 'preference1';
694         $preferencename2 = 'preference2';
695         $user1->preferences = array(
696             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
697             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
698         $customfieldname1 = 'testdatacustom1';
699         $customfieldname2 = 'testdatacustom2';
700         $user1->customfields = array(
701             array('type' => $customfieldname1, 'value' => 'customvalue'),
702             array('type' => $customfieldname2, 'value' => 'customvalue2'));
703         //a small user: user2
704         $user2 = new stdClass();
705         $user2->username = 'veryimprobabletestusername2';
706         $user2->password = 'testpassword2';
707         $user2->firstname = 'testfirstname2';
708         $user2->lastname = 'testlastname2';
709         $user2->email = 'testemail1@moodle.com';
710         $users = array($user1, $user2);
712         //can run this test only if test usernames don't exist
713         $searchusers = $DB->get_records_list('user', 'username',
714                 array($user1->username, $user1->username));
715         if (count($searchusers) == 0) {
716             //create two users
717             require_once($CFG->dirroot."/user/lib.php");
718             require_once($CFG->dirroot."/user/profile/lib.php");
719             $user1->id = user_create_user($user1);
720             // custom fields
721             if(!empty($user1->customfields)) {
722                 foreach($user1->customfields as $customfield) {
723                     $user1->{"profile_field_".$customfield['type']} = $customfield['value'];
724                 }
725                 profile_save_data((object) $user1);
726             }
727             //preferences
728             if (!empty($user1->preferences)) {
729                 foreach($user1->preferences as $preference) {
730                     set_user_preference($preference['type'], $preference['value'],$user1->id);
731                 }
732             }
733             $user2->id = user_create_user($user2);
735             //create the custom fields
736             $customfield = new stdClass();
737             $customfield->shortname = $customfieldname1;
738             $customfield->name = $customfieldname1;
739             $customfield->datatype = 'text';
740             $DB->insert_record('user_info_field', $customfield);
741             $customfield = new stdClass();
742             $customfield->shortname = $customfieldname2;
743             $customfield->name = $customfieldname2;
744             $customfield->datatype = 'text';
745             $DB->insert_record('user_info_field', $customfield);
747             //search for them => TEST they exists
748             $searchusers = $DB->get_records_list('user', 'username',
749                     array($user1->username, $user2->username));
750             $this->assertEqual(count($users), count($searchusers));
752             //delete the users by webservice
753             $function = 'moodle_user_delete_users';
754             $params = array('users' => array($user1->id, $user2->id));
755             $client->call($function, $params);
757             //search for them => TESTS they don't exists
758             $searchusers = $DB->get_records_list('user', 'username',
759                     array($user1->username, $user2->username));
760            
761             $this->assertTrue(empty($searchusers));
763             //unset preferences
764             $DB->delete_records('user_preferences', array('userid' => $user1->id));
766             //clear custom fields data
767             $DB->delete_records('user_info_data', array('userid' => $user1->id));
769             //delete custom fields
770             $DB->delete_records_list('user_info_field', 'shortname',
771                     array($customfieldname1, $customfieldname2));
773             //delete users from DB
774             $DB->delete_records_list('user', 'id',
775                     array($user1->id, $user2->id));
776         }
777     }
779     function moodle_user_update_users($client) {
780         global $DB, $CFG;
782         //Set test data
783         //a full user: user1
784         $user1 = new stdClass();
785         $user1->username = 'veryimprobabletestusername1';
786         $user1->password = 'testpassword1';
787         $user1->firstname = 'testfirstname1';
788         $user1->lastname = 'testlastname1';
789         $user1->email = 'testemail1@moodle.com';
790         $user1->auth = 'manual';
791         $user1->idnumber = 'testidnumber1';
792         $user1->lang = 'en';
793         $user1->theme = 'standard';
794         $user1->timezone = 99;
795         $user1->mailformat = 0;
796         $user1->description = 'Hello World!';
797         $user1->city = 'testcity1';
798         $user1->country = 'au';
799         $preferencename1 = 'preference1';
800         $preferencename2 = 'preference2';
801         $user1->preferences = array(
802             array('type' => $preferencename1, 'value' => 'preferencevalue1'),
803             array('type' => $preferencename2, 'value' => 'preferencevalue2'));
804         $customfieldname1 = 'testdatacustom1';
805         $customfieldname2 = 'testdatacustom2';
806         $user1->customfields = array(
807             array('type' => $customfieldname1, 'value' => 'customvalue'),
808             array('type' => $customfieldname2, 'value' => 'customvalue2'));
809         //a small user: user2
810         $user2 = new stdClass();
811         $user2->username = 'veryimprobabletestusername2';
812         $user2->password = 'testpassword2';
813         $user2->firstname = 'testfirstname2';
814         $user2->lastname = 'testlastname2';
815         $user2->email = 'testemail1@moodle.com';
816         $users = array($user1, $user2);
818         //can run this test only if test usernames don't exist
819         $searchusers = $DB->get_records_list('user', 'username',
820                 array($user1->username, $user1->username));
821         if (count($searchusers) == 0) {
822             //create two users
823             require_once($CFG->dirroot."/user/lib.php");
824             require_once($CFG->dirroot."/user/profile/lib.php");
825             $user1->id = user_create_user($user1);
826             //unset field created by user_create_user
827             unset($user1->timemodified);
828             unset($user1->timecreated);
830             // custom fields
831             if(!empty($user1->customfields)) {
832                 foreach($user1->customfields as $customfield) {
833                     $customuser1->id = $user1->id;
834                     $customuser1->{"profile_field_".$customfield['type']} = $customfield['value'];
835                 }
836                 profile_save_data((object) $customuser1);
837             }
838             //preferences
839             if (!empty($user1->preferences)) {
840                 foreach($user1->preferences as $preference) {
841                     set_user_preference($preference['type'], $preference['value'],$user1->id);
842                 }
843             }
844             $user2->id = user_create_user($user2);
845             unset($user2->timemodified);
846             unset($user2->timecreated);
848              //create the custom fields
849             $customfield = new stdClass();
850             $customfield->shortname = $customfieldname1;
851             $customfield->name = $customfieldname1;
852             $customfield->datatype = 'text';
853             $DB->insert_record('user_info_field', $customfield);
854             $customfield = new stdClass();
855             $customfield->shortname = $customfieldname2;
856             $customfield->name = $customfieldname2;
857             $customfield->datatype = 'text';
858             $DB->insert_record('user_info_field', $customfield);
859             
860             //search for them => TEST they exists
861             $searchusers = $DB->get_records_list('user', 'username',
862                     array($user1->username, $user2->username));
863             $this->assertEqual(count($users), count($searchusers));
865             //update the test data
866             $user1->username = 'veryimprobabletestusername1_updated';
867             $user1->password = 'testpassword1_updated';
868             $user1->firstname = 'testfirstname1_updated';
869             $user1->lastname = 'testlastname1_updated';
870             $user1->email = 'testemail1_updated@moodle.com';
871             $user1->auth = 'manual';
872             $user1->idnumber = 'testidnumber1_updated';
873             $user1->lang = 'en';
874             $user1->theme = 'standard';
875             $user1->timezone = 98;
876             $user1->mailformat = 1;
877             $user1->description = 'Hello World!_updated';
878             $user1->city = 'testcity1_updated';
879             $user1->country = 'au';
880             $preferencename1 = 'preference1';
881             $preferencename2 = 'preference2';
882             $user1->preferences = array(
883             array('type' => $preferencename1, 'value' => 'preferencevalue1_updated'),
884             array('type' => $preferencename2, 'value' => 'preferencevalue2_updated'));
885             $customfieldname1 = 'testdatacustom1';
886             $customfieldname2 = 'testdatacustom2';
887             $user1->customfields = array(
888             array('type' => $customfieldname1, 'value' => 'customvalue_updated'),
889             array('type' => $customfieldname2, 'value' => 'customvalue2_updated'));
890             $user2->username = 'veryimprobabletestusername2_updated';
891             $user2->password = 'testpassword2_updated';
892             $user2->firstname = 'testfirstname2_updated';
893             $user2->lastname = 'testlastname2_updated';
894             $user2->email = 'testemail1_updated@moodle.com';
895             $users = array($user1, $user2);
896             
897             //update the users by web service
898             $function = 'moodle_user_update_users';
899             $params = array('users' => $users);
900             $client->call($function, $params);
902             //compare DB user with the test data
903             $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
904             $this->assertEqual($dbuser1->firstname, $user1->firstname);
905             $this->assertEqual($dbuser1->password,
906                     hash_internal_user_password($user1->password));
907             $this->assertEqual($dbuser1->lastname, $user1->lastname);
908             $this->assertEqual($dbuser1->email, $user1->email);
909             $this->assertEqual($dbuser1->auth, $user1->auth);
910             $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
911             $this->assertEqual($dbuser1->lang, $user1->lang);
912             $this->assertEqual($dbuser1->theme, $user1->theme);
913             $this->assertEqual($dbuser1->timezone, $user1->timezone);
914             $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
915             $this->assertEqual($dbuser1->description, $user1->description);
916             $this->assertEqual($dbuser1->city, $user1->city);
917             $this->assertEqual($dbuser1->country, $user1->country);
918             $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
919                             null, $dbuser1->id);
920             $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
921             $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
922                             null, $dbuser1->id);
923             $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
924             require_once($CFG->dirroot . "/user/profile/lib.php");
925             $customfields = profile_user_record($dbuser1->id);
927             $customfields = (array) $customfields;
928             $customfieldname1 = $user1->customfields[0]['type'];
929             $customfieldname2 = $user1->customfields[1]['type'];
930             $this->assertEqual($customfields[$customfieldname1],
931                     $user1->customfields[0]['value']);
932             $this->assertEqual($customfields[$customfieldname2],
933                     $user1->customfields[1]['value']);
935             $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
936             $this->assertEqual($dbuser2->firstname, $user2->firstname);
937             $this->assertEqual($dbuser2->password,
938                     hash_internal_user_password($user2->password));
939             $this->assertEqual($dbuser2->lastname, $user2->lastname);
940             $this->assertEqual($dbuser2->email, $user2->email);
942             //unset preferences
943             $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
945             //clear custom fields data
946             $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
948             //delete custom fields
949             $DB->delete_records_list('user_info_field', 'shortname',
950                     array($customfieldname1, $customfieldname2));
952             //delete users from DB
953             $DB->delete_records_list('user', 'id',
954                     array($dbuser1->id, $dbuser2->id));
956         }
957     }
959     function moodle_role_assign($client) {
960         global $DB, $CFG;
962         $searchusers = $DB->get_records_list('user', 'username',
963                 array('veryimprobabletestusername2'));
964         $searchroles = $DB->get_records_list('role', 'shortname',
965                 array('role1thatshouldnotexist', 'role2thatshouldnotexist'));
967         if (empty($searchusers) and empty($searchroles)) {
969             //create a temp user
970             $user = new stdClass();
971             $user->username = 'veryimprobabletestusername2';
972             $user->password = 'testpassword2';
973             $user->firstname = 'testfirstname2';
974             $user->lastname = 'testlastname2';
975             $user->email = 'testemail1@moodle.com';
976             require_once($CFG->dirroot."/user/lib.php");
977             $user->id = user_create_user($user);
979             //create two roles
980             $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
981             $role2->id = create_role('role2thatshouldnotexist', 'role2thatshouldnotexist', '');
983             //assign user to role by webservice
984             $context = get_system_context();
985             $assignments = array(
986                 array('roleid' => $role1->id, 'userid' => $user->id, 'contextid' => $context->id),
987                 array('roleid' => $role2->id, 'userid' => $user->id, 'contextid' => $context->id)
988             );
990             $function = 'moodle_role_assign';
991             $params = array('assignments' => $assignments);
992             $client->call($function, $params);
994             //check that the assignment work
995             $roles = get_user_roles($context, $user->id, false);
996             foreach ($roles as $role) {
997                 $this->assertTrue(($role->roleid == $role1->id) or ($role->roleid == $role2->id) );
998             }
1000             //unassign roles from user
1001             role_unassign($role1->id, $user->id, $context->id, '', NULL);
1002             role_unassign($role2->id, $user->id, $context->id, '', NULL);
1004             //delete user from DB
1005             $DB->delete_records('user', array('id' => $user->id));
1007             //delete the two role from DB
1008             delete_role($role1->id);
1009             delete_role($role2->id);
1010         }
1011     }
1013     function moodle_role_unassign($client) {
1014         global $DB, $CFG;
1016         $searchusers = $DB->get_records_list('user', 'username',
1017                 array('veryimprobabletestusername2'));
1018         $searchroles = $DB->get_records_list('role', 'shortname',
1019                 array('role1thatshouldnotexist', 'role2thatshouldnotexist'));
1021         if (empty($searchusers) and empty($searchroles)) {
1023             //create a temp user
1024             $user = new stdClass();
1025             $user->username = 'veryimprobabletestusername2';
1026             $user->password = 'testpassword2';
1027             $user->firstname = 'testfirstname2';
1028             $user->lastname = 'testlastname2';
1029             $user->email = 'testemail1@moodle.com';
1030             require_once($CFG->dirroot."/user/lib.php");
1031             $user->id = user_create_user($user);
1033             //create two roles
1034             $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1035             $role2->id = create_role('role2thatshouldnotexist', 'role2thatshouldnotexist', '');
1036         
1037             //assign roles from user
1038             $context = get_system_context();
1039             role_assign($role1->id, $user->id, $context->id);
1040             role_assign($role2->id, $user->id, $context->id);
1042             //check that the local assignment work
1043             $roles = get_user_roles($context, $user->id, false);
1044             foreach ($roles as $role) {
1045                 $this->assertTrue(($role->roleid == $role1->id) or ($role->roleid == $role2->id) );
1046             }
1048             //unassign user to role by webservice          
1049             $assignments = array(
1050                 array('roleid' => $role1->id, 'userid' => $user->id, 'contextid' => $context->id),
1051                 array('roleid' => $role2->id, 'userid' => $user->id, 'contextid' => $context->id)
1052             );
1053             $function = 'moodle_role_unassign';
1054             $params = array('assignments' => $assignments);
1055             $client->call($function, $params);
1057             //check that the web service unassignment work
1058             $roles = get_user_roles($context, $user->id, false);
1059             $this->assertTrue(empty($roles));
1061             //delete user from DB
1062             $DB->delete_records('user', array('id' => $user->id));
1064             //delete the two role from DB
1065             delete_role($role1->id);
1066             delete_role($role2->id);
1067         }
1069     }
1071     /**
1072      * READ ONLY test
1073      * TODO: find a better solution that running web service for each course
1074      * in the system
1075      * For each courses, test the number of groups
1076      * @param object $client
1077      */
1078     function moodle_group_get_course_groups($client) {
1079         global $DB;
1081         $courses = $DB->get_records('course');
1082         foreach($courses as $course) {
1083             $coursegroups = groups_get_all_groups($course->id);
1084             $function = 'moodle_group_get_course_groups';
1085             $params = array('courseid' => $course->id);
1086             $groups = $client->call($function, $params);
1087             $this->assertEqual(count($groups), count($coursegroups));
1088         }
1089     }
1092     /**
1093      * READ ONLY test
1094      * Test that the same number of members are returned
1095      * for each existing group in the system
1096      * @param object $client
1097      */
1098     function moodle_group_get_groupmembers($client) {
1099         global $DB;
1101         $groups = $DB->get_records('groups');
1102         $groupids = array();
1103         foreach ($groups as $group) {
1104             $groupids[] = $group->id;
1105         }
1106         $function = 'moodle_group_get_groupmembers';
1107         $params = array('groupids' => $groupids);
1108         $groupsmembers = $client->call($function, $params);
1110         foreach($groupsmembers as $groupmembers) {
1111             $dbgroupmembers = groups_get_members($groupmembers['groupid']);
1112             unset($groups[$groupmembers['groupid']]);
1113             $this->assertEqual(count($dbgroupmembers), count($groupmembers['userids']));
1114         }
1116         //check that all existing groups have been returned by the web service function
1117         $this->assertTrue(empty($groups));
1118        
1119         
1120     }
1122     function moodle_group_add_groupmembers($client) {
1123         global $DB, $CFG;
1125         //create category
1126         $category = new stdClass();
1127         $category->name = 'tmpcategoryfortest123';
1128         $category->id = $DB->insert_record('course_categories', $category);
1130         //create a course
1131         $course = new stdClass();
1132         $course->fullname = 'tmpcoursefortest123';
1133         $course->shortname = 'tmpcoursefortest123';
1134         $course->idnumber = 'tmpcoursefortest123';
1135         $course->category = $category->id;
1136         $course->id = $DB->insert_record('course', $course);
1138         //create a role
1139         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1141         //create a user
1142         $user = new stdClass();
1143         $user->username = 'veryimprobabletestusername2';
1144         $user->password = 'testpassword2';
1145         $user->firstname = 'testfirstname2';
1146         $user->lastname = 'testlastname2';
1147         $user->email = 'testemail1@moodle.com';
1148         $user->mnethostid = $CFG->mnet_localhost_id;
1149         require_once($CFG->dirroot."/user/lib.php");
1150         $user->id = user_create_user($user);
1152         //create course context
1153         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1155         //enrol the user in the course with the created role
1156         role_assign($role1->id, $user->id, $context->id);
1157         $enrol = new stdClass();
1158         $enrol->courseid = $course->id;
1159         $enrol->roleid = $role1->id;
1160         $enrol->id = $DB->insert_record('enrol', $enrol);
1161         $enrolment = new stdClass();
1162         $enrolment->userid = $user->id;
1163         $enrolment->enrolid = $enrol->id;
1164         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1166         //create a group in the course
1167         $group = new stdClass();
1168         $group->courseid = $course->id;
1169         $group->name = 'tmpgroufortest123';
1170         $group->id = $DB->insert_record('groups', $group);
1172         //WEBSERVICE CALL
1173         $function = 'moodle_group_add_groupmembers';
1174         $params = array('members' => array(array('groupid' => $group->id, 'userid' => $user->id)));
1175         $groupsmembers = $client->call($function, $params);
1177         //CHECK TEST RESULT
1178         require_once($CFG->libdir . '/grouplib.php');
1179         $groupmembers = groups_get_members($group->id);
1180         $this->assertEqual(count($groupmembers), 1);
1181         $this->assertEqual($groupmembers[$user->id]->id, $user->id);
1183         //remove the members from the group
1184         require_once($CFG->dirroot . "/group/lib.php");
1185         groups_remove_member($group->id, $user->id);
1187         //delete the group
1188         $DB->delete_records('groups', array('id' => $group->id));
1190         //unenrol the user
1191         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1192         $DB->delete_records('enrol', array('id' => $enrol->id));
1193         role_unassign($role1->id, $user->id, $context->id);
1195         //delete course context
1196         delete_context(CONTEXT_COURSE, $course->id);
1198         //delete the user
1199         $DB->delete_records('user', array('id' => $user->id));
1201         //delete the role
1202         delete_role($role1->id);
1204         //delete the course
1205         $DB->delete_records('course', array('id' => $course->id));
1207         //delete the category
1208         $DB->delete_records('course_categories', array('id' => $category->id));
1209         
1210     }
1212     function moodle_group_delete_groupmembers($client) {
1213         global $DB, $CFG;
1215         //create category
1216         $category = new stdClass();
1217         $category->name = 'tmpcategoryfortest123';
1218         $category->id = $DB->insert_record('course_categories', $category);
1220         //create a course
1221         $course = new stdClass();
1222         $course->fullname = 'tmpcoursefortest123';
1223         $course->shortname = 'tmpcoursefortest123';
1224         $course->idnumber = 'tmpcoursefortest123';
1225         $course->category = $category->id;
1226         $course->id = $DB->insert_record('course', $course);
1228         //create a role
1229         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1231         //create a user
1232         $user = new stdClass();
1233         $user->username = 'veryimprobabletestusername2';
1234         $user->password = 'testpassword2';
1235         $user->firstname = 'testfirstname2';
1236         $user->lastname = 'testlastname2';
1237         $user->email = 'testemail1@moodle.com';
1238         $user->mnethostid = $CFG->mnet_localhost_id;
1239         require_once($CFG->dirroot."/user/lib.php");
1240         $user->id = user_create_user($user);
1242         //create course context
1243         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1245         //enrol the user in the course with the created role
1246         role_assign($role1->id, $user->id, $context->id);
1247         $enrol = new stdClass();
1248         $enrol->courseid = $course->id;
1249         $enrol->roleid = $role1->id;
1250         $enrol->id = $DB->insert_record('enrol', $enrol);
1251         $enrolment = new stdClass();
1252         $enrolment->userid = $user->id;
1253         $enrolment->enrolid = $enrol->id;
1254         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1256         //create a group in the course
1257         $group = new stdClass();
1258         $group->courseid = $course->id;
1259         $group->name = 'tmpgroufortest123';
1260         $group->id = $DB->insert_record('groups', $group);
1262         //add group member
1263         require_once($CFG->dirroot . "/group/lib.php");
1264         groups_add_member($group->id, $user->id);
1265         $groupmembers = groups_get_members($group->id);
1266         $this->assertEqual(count($groupmembers), 1);
1268         //WEB SERVICE CALL - remove the members from the group
1269         $function = 'moodle_group_delete_groupmembers';
1270         $params = array('members' => array(array('groupid' => $group->id, 'userid' => $user->id)));
1271         $client->call($function, $params);
1273         require_once($CFG->libdir . '/grouplib.php');
1274         $groupmembers = groups_get_members($group->id);
1275         $this->assertEqual(count($groupmembers), 0);
1277         //delete the group
1278         $DB->delete_records('groups', array('id' => $group->id));
1280         //unenrol the user
1281         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1282         $DB->delete_records('enrol', array('id' => $enrol->id));
1283         role_unassign($role1->id, $user->id, $context->id);
1285         //delete course context
1286         delete_context(CONTEXT_COURSE, $course->id);
1288         //delete the user
1289         $DB->delete_records('user', array('id' => $user->id));
1291         //delete the role
1292         delete_role($role1->id);
1294         //delete the course
1295         $DB->delete_records('course', array('id' => $course->id));
1297         //delete the category
1298         $DB->delete_records('course_categories', array('id' => $category->id));
1300     }
1302     function moodle_group_create_groups($client) {
1303         global $DB, $CFG;
1305         //create category
1306         $category = new stdClass();
1307         $category->name = 'tmpcategoryfortest123';
1308         $category->id = $DB->insert_record('course_categories', $category);
1310         //create a course
1311         $course = new stdClass();
1312         $course->fullname = 'tmpcoursefortest123';
1313         $course->shortname = 'tmpcoursefortest123';
1314         $course->idnumber = 'tmpcoursefortest123';
1315         $course->category = $category->id;
1316         $course->id = $DB->insert_record('course', $course);
1318         //create a role
1319         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1321         //create a user
1322         $user = new stdClass();
1323         $user->username = 'veryimprobabletestusername2';
1324         $user->password = 'testpassword2';
1325         $user->firstname = 'testfirstname2';
1326         $user->lastname = 'testlastname2';
1327         $user->email = 'testemail1@moodle.com';
1328         $user->mnethostid = $CFG->mnet_localhost_id;
1329         require_once($CFG->dirroot."/user/lib.php");
1330         $user->id = user_create_user($user);
1332         //create course context
1333         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1335         //enrol the user in the course with the created role
1336         role_assign($role1->id, $user->id, $context->id);
1337         $enrol = new stdClass();
1338         $enrol->courseid = $course->id;
1339         $enrol->roleid = $role1->id;
1340         $enrol->id = $DB->insert_record('enrol', $enrol);
1341         $enrolment = new stdClass();
1342         $enrolment->userid = $user->id;
1343         $enrolment->enrolid = $enrol->id;
1344         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1346         require_once($CFG->dirroot . "/group/lib.php");
1347         $groups = groups_get_all_groups($course->id);
1348         $this->assertEqual(count($groups), 0);
1350         //WEBSERVICE CALL - create a group in the course
1351         $group = new stdClass();
1352         $group->courseid = $course->id;
1353         $group->name = 'tmpgroufortest123';
1354         $group->enrolmentkey = '';
1355         $group->description = '';
1356         $group2 = new stdClass();
1357         $group2->courseid = $course->id;
1358         $group2->name = 'tmpgroufortest1233';
1359         $group2->enrolmentkey = '';
1360         $group2->description = '';
1361         $paramgroups = array($group, $group2);
1362         $function = 'moodle_group_create_groups';
1363         $params = array('groups' => $paramgroups);
1364         $createdgroups = $client->call($function, $params);
1366         $groups = groups_get_all_groups($course->id);
1367         $this->assertEqual(count($groups), count($paramgroups));
1369         //delete the group
1370         foreach ($groups as $dbgroup) {
1371             $DB->delete_records('groups', array('id' => $dbgroup->id));
1372         }
1374         //unenrol the user
1375         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1376         $DB->delete_records('enrol', array('id' => $enrol->id));
1377         role_unassign($role1->id, $user->id, $context->id);
1379         //delete course context
1380         delete_context(CONTEXT_COURSE, $course->id);
1382         //delete the user
1383         $DB->delete_records('user', array('id' => $user->id));
1385         //delete the role
1386         delete_role($role1->id);
1388         //delete the course
1389         $DB->delete_records('course', array('id' => $course->id));
1391         //delete the category
1392         $DB->delete_records('course_categories', array('id' => $category->id));
1394     }
1396     function moodle_group_delete_groups($client) {
1397         global $DB, $CFG;
1399         //create category
1400         $category = new stdClass();
1401         $category->name = 'tmpcategoryfortest123';
1402         $category->id = $DB->insert_record('course_categories', $category);
1404         //create a course
1405         $course = new stdClass();
1406         $course->fullname = 'tmpcoursefortest123';
1407         $course->shortname = 'tmpcoursefortest123';
1408         $course->idnumber = 'tmpcoursefortest123';
1409         $course->category = $category->id;
1410         $course->id = $DB->insert_record('course', $course);
1412         //create a role
1413         $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', '');
1415         //create a user
1416         $user = new stdClass();
1417         $user->username = 'veryimprobabletestusername2';
1418         $user->password = 'testpassword2';
1419         $user->firstname = 'testfirstname2';
1420         $user->lastname = 'testlastname2';
1421         $user->email = 'testemail1@moodle.com';
1422         $user->mnethostid = $CFG->mnet_localhost_id;
1423         require_once($CFG->dirroot."/user/lib.php");
1424         $user->id = user_create_user($user);
1426         //create course context
1427         $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
1429         //enrol the user in the course with the created role
1430         role_assign($role1->id, $user->id, $context->id);
1431         $enrol = new stdClass();
1432         $enrol->courseid = $course->id;
1433         $enrol->roleid = $role1->id;
1434         $enrol->id = $DB->insert_record('enrol', $enrol);
1435         $enrolment = new stdClass();
1436         $enrolment->userid = $user->id;
1437         $enrolment->enrolid = $enrol->id;
1438         $enrolment->id = $DB->insert_record('user_enrolments', $enrolment);
1440         //create a group in the course
1441         $group = new stdClass();
1442         $group->courseid = $course->id;
1443         $group->name = 'tmpgroufortest123';
1444         $group->enrolmentkey = '';
1445         $group->description = '';
1446         $group->id = $DB->insert_record('groups', $group);
1447         $group2 = new stdClass();
1448         $group2->courseid = $course->id;
1449         $group2->name = 'tmpgroufortest1233';
1450         $group2->enrolmentkey = '';
1451         $group2->description = '';
1452         $group2->id = $DB->insert_record('groups', $group2);
1453         $paramgroups = array($group, $group2);
1455         require_once($CFG->dirroot . "/group/lib.php");
1456         $groups = groups_get_all_groups($course->id);
1457         $this->assertEqual(2, count($groups));
1459         //WEBSERVICE CALL -  delete the group
1460         $function = 'moodle_group_delete_groups';
1461         $params = array('groupids' => array($group->id, $group2->id));
1462         $client->call($function, $params);
1464         $groups = groups_get_all_groups($course->id);
1465         $this->assertEqual(0, count($groups));
1467         //unenrol the user
1468         $DB->delete_records('user_enrolments', array('id' => $enrolment->id));
1469         $DB->delete_records('enrol', array('id' => $enrol->id));
1470         role_unassign($role1->id, $user->id, $context->id);
1472         //delete course context
1473         delete_context(CONTEXT_COURSE, $course->id);
1475         //delete the user
1476         $DB->delete_records('user', array('id' => $user->id));
1478         //delete the role
1479         delete_role($role1->id);
1481         //delete the course
1482         $DB->delete_records('course', array('id' => $course->id));
1484         //delete the category
1485         $DB->delete_records('course_categories', array('id' => $category->id));
1487     }
1489      function moodle_notes_create_notes($client) {
1490         global $DB, $CFG;
1492         $note1 = array();
1493         $note1['userid'] = 2; //about who is the note
1494         $note1['publishstate'] = 'personal'; //can be course, site, personal
1495         $note1['courseid'] = 2; //in Moodle a notes is always created into a course, even a site note.
1496         $note1['text'] = 'This is a personal note about the user';
1497         $note1['clientnoteid'] = 'note_1';
1499         $note2 = array();
1500         $note2['userid'] = 40000; //mostl likely going to fail
1501         $note2['publishstate'] = 'course';
1502         $note2['courseid'] = 2;
1503         $note2['text'] = 'This is a teacher note about the user';
1504         $note2['clientnoteid'] = 'note_2';
1506         $note3 = array();
1507         $note3['userid'] = 2;
1508         $note3['publishstate'] = 'site';
1509         $note3['courseid'] = 30000; //most likely going to fail
1510         $note3['text'] = 'This is a teacher site-wide note about the user';
1511         $note3['clientnoteid'] = 'note_3';
1513         $function = 'moodle_notes_create_notes';
1514         $params = array('notes' => array($note1, $note2, $note3));
1515         $notes = $client->call($function, $params);
1517         $this->assertEqual(3, count($notes)); //1 info is a success, 2 others should be failed
1518     }