theme-standardold MDL-25431 Fixed up a block header display issue in standardold...
[moodle.git] / webservice / simpletest / testwebservice.php
CommitLineData
75681c41 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Test the different web service protocols.
20 *
21 * @author jerome@moodle.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
23 * @package web service
24 */
25if (!defined('MOODLE_INTERNAL')) {
6bb31e40 26 /// It must be included from a Moodle page
27 die('Direct access to this script is forbidden.');
75681c41 28}
29
30/**
31 * How to configure this unit tests:
32 * 0- Enable the web service you wish to test in the Moodle administration
33 * 1- Create a service with all functions in the Moodle administration
6bb31e40 34 * 2- Create a token associate this service and to an admin (or a user with all required capabilities)
75681c41 35 * 3- Configure setUp() function:
36 * a- write the token
37 * b- activate the protocols you wish to test
38 * c- activate the functions you wish to test (readonlytests and writetests arrays)
39 * d- set the number of time the web services are run
40 * Do not run WRITE test function on a production site as they impact the DB (even though every
41 * test should clean the modified data)
42 *
43 * How to write a new function:
44 * 1- Add the function name to the array readonlytests/writetests
45 * 2- Set it as false when you commit!
46 * 3- write the function - Do not prefix the function name by 'test'
47 */
48class webservice_test extends UnitTestCase {
49
50 public $testtoken;
51 public $testrest;
52 public $testxmlrpc;
53 public $testsoap;
54 public $timerrest;
55 public $timerxmlrpc;
56 public $timersoap;
57 public $readonlytests;
58 public $writetests;
59
60 function setUp() {
61 //token to test
6bb31e40 62 $this->testtoken = '72d338d58ff881cc293f8cd1d96d7a57';
75681c41 63
64 //protocols to test
65 $this->testrest = false; //Does not work till XML => PHP is implemented (MDL-22965)
66 $this->testxmlrpc = false;
67 $this->testsoap = false;
68
6bb31e40 69 ////// READ-ONLY DB tests ////
75681c41 70 $this->readonlytests = array(
6bb31e40 71 'moodle_group_get_groups' => false,
72 'moodle_course_get_courses' => false,
73 'moodle_user_get_users_by_id' => false,
b02b5fb7 74 'moodle_enrol_get_enrolled_users' => false
75681c41 75 );
76
6bb31e40 77 ////// WRITE DB tests ////
75681c41 78 $this->writetests = array(
6bb31e40 79 'moodle_user_create_users' => false,
80 'moodle_course_create_courses' => false,
75681c41 81 );
82
83 //performance testing: number of time the web service are run
84 $this->iteration = 1;
85
86 //DO NOT CHANGE
87 //reset the timers
88 $this->timerrest = 0;
89 $this->timerxmlrpc = 0;
90 $this->timersoap = 0;
91 }
92
93 function testRun() {
94 global $CFG;
95
96 if (!empty($this->testtoken)) {
97
98 //Does not work till XML => PHP is implemented (MDL-22965)
99 if ($this->testrest) {
100
101 $this->timerrest = time();
102
103 require_once($CFG->dirroot . "/webservice/rest/lib.php");
104 $restclient = new webservice_rest_client($CFG->wwwroot
105 . '/webservice/rest/server.php', $this->testtoken);
106
107 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
108 foreach ($this->readonlytests as $functioname => $run) {
109 if ($run) {
110 //$this->{$functioname}($restclient);
111 }
112 }
113 foreach ($this->writetests as $functioname => $run) {
114 if ($run) {
115 //$this->{$functioname}($restclient);
116 }
117 }
118 }
119
120 $this->timerrest = time() - $this->timerrest;
121 //here you could call a log function to display the timer
122 //example:
123 //error_log('REST time: ');
124 //error_log(print_r($this->timerrest));
125 }
126
127 if ($this->testxmlrpc) {
128
129 $this->timerxmlrpc = time();
130
131 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
132 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
133 . '/webservice/xmlrpc/server.php', $this->testtoken);
134
135 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
136 foreach ($this->readonlytests as $functioname => $run) {
137 if ($run) {
138 $this->{$functioname}($xmlrpcclient);
139 }
140 }
141 foreach ($this->writetests as $functioname => $run) {
142 if ($run) {
143 $this->{$functioname}($xmlrpcclient);
144 }
145 }
146 }
147
148 $this->timerxmlrpc = time() - $this->timerxmlrpc;
149 //here you could call a log function to display the timer
150 //example:
151 //error_log('XML-RPC time: ');
152 //error_log(print_r($this->timerxmlrpc));
153 }
154
155 if ($this->testsoap) {
156
157 $this->timersoap = time();
158
159 require_once($CFG->dirroot . "/webservice/soap/lib.php");
160 $soapclient = new webservice_soap_client($CFG->wwwroot
161 . '/webservice/soap/server.php', $this->testtoken);
162 $soapclient->setWsdlCache(false);
163
164 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
165 foreach ($this->readonlytests as $functioname => $run) {
166 if ($run) {
167 $this->{$functioname}($soapclient);
168 }
169 }
170 foreach ($this->writetests as $functioname => $run) {
171 if ($run) {
172 $this->{$functioname}($soapclient);
173 }
174 }
175 }
176
177 $this->timersoap = time() - $this->timersoap;
178 //here you could call a log function to display the timer
179 //example:
180 //error_log('SOAP time: ');
181 //error_log(print_r($this->timersoap));
182 }
183 }
184 }
185
186 ///// WEB SERVICE TEST FUNCTIONS
187
188 function moodle_group_get_groups($client) {
189 global $DB;
190 $dbgroups = $DB->get_records('groups');
191 $groupids = array();
192 foreach ($dbgroups as $dbgroup) {
193 $groupids[] = $dbgroup->id;
194 }
195 $function = 'moodle_group_get_groups';
196
197 $params = array('groupids' => $groupids);
198 $groups = $client->call($function, $params);
199 $this->assertEqual(count($groups), count($groupids));
200 }
201
6bb31e40 202 function moodle_user_get_users_by_id($client) {
203 global $DB;
204 $dbusers = $DB->get_records('user');
205 $userids = array();
206 foreach ($dbusers as $dbuser) {
207 $userids[] = $dbuser->id;
208 }
209 $function = 'moodle_user_get_users_by_id';
210
211 $params = array('userids' => $userids);
212 $users = $client->call($function, $params);
213
214 $this->assertEqual(count($users), count($userids));
215 }
216
b02b5fb7 217 function moodle_enrol_get_enrolled_users($client) {
218 global $DB;
219
220 //function settings
221 $withcapability = '';
222 $groupid = null;
223 $onlyactive = false;
224
225 $dbcourses = $DB->get_records('course');
226 $function = 'moodle_enrol_get_enrolled_users';
227
228 foreach ($dbcourses as $dbcourse) {
229
230 $params = array();
231
232 $coursecontext = get_context_instance(CONTEXT_COURSE, $dbcourse->id);
233
234 list($sql, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
235 $sql = "SELECT DISTINCT ue.userid, e.courseid
236 FROM {user_enrolments} ue
237 JOIN {enrol} e ON (e.id = ue.enrolid)
238 WHERE e.courseid = :courseid AND ue.userid IN ($sql)";
239
240 $params['courseid'] = $dbcourse->id;
241
242 $enrolledusers = $DB->get_records_sql($sql, $params);
243
244 $wsparams = array('courseid' => $dbcourse->id, 'withcapability' => $withcapability,
245 'groupid' => $groupid, 'onlyactive' => $onlyactive);
246 $resultusers = $client->call($function, $wsparams);
247
248 $this->assertEqual(count($resultusers), count($enrolledusers));
249 }
250 }
251
6bb31e40 252 function moodle_course_get_courses($client) {
75681c41 253 global $DB;
6bb31e40 254
255 $function = 'moodle_course_get_courses';
256
257 //retrieve all courses from db
258 $dbcourses = $DB->get_records('course');
259 $courseids = array();
260 foreach ($dbcourses as $dbcourse) {
261 $courseids[] = $dbcourse->id;
262 }
263
264 //retrieve all courses by id
265 $params = array('options' => array('ids' => $courseids));
266 $courses = $client->call($function, $params);
267
268 //check it is the same course count
269 $this->assertEqual(count($courses), count($courseids));
270
271 //check all course values are identic
272 foreach ($courses as $course) {
273 $this->assertEqual($course['fullname'],
274 $dbcourses[$course['id']]->fullname);
275 $this->assertEqual($course['shortname'],
276 $dbcourses[$course['id']]->shortname);
277 $this->assertEqual($course['categoryid'],
278 $dbcourses[$course['id']]->category);
279 $this->assertEqual($course['categorysortorder'],
280 $dbcourses[$course['id']]->sortorder);
281 $this->assertEqual($course['idnumber'],
282 $dbcourses[$course['id']]->idnumber);
283 $this->assertEqual($course['summary'],
284 $dbcourses[$course['id']]->summary);
285 $this->assertEqual($course['summaryformat'],
286 $dbcourses[$course['id']]->summaryformat);
287 $this->assertEqual($course['format'],
288 $dbcourses[$course['id']]->format);
289 $this->assertEqual($course['showgrades'],
290 $dbcourses[$course['id']]->showgrades);
291 $this->assertEqual($course['newsitems'],
292 $dbcourses[$course['id']]->newsitems);
293 $this->assertEqual($course['startdate'],
294 $dbcourses[$course['id']]->startdate);
295 $this->assertEqual($course['numsections'],
296 $dbcourses[$course['id']]->numsections);
297 $this->assertEqual($course['maxbytes'],
298 $dbcourses[$course['id']]->maxbytes);
299 $this->assertEqual($course['visible'],
300 $dbcourses[$course['id']]->visible);
301 $this->assertEqual($course['hiddensections'],
302 $dbcourses[$course['id']]->hiddensections);
303 $this->assertEqual($course['groupmode'],
304 $dbcourses[$course['id']]->groupmode);
305 $this->assertEqual($course['groupmodeforce'],
306 $dbcourses[$course['id']]->groupmodeforce);
307 $this->assertEqual($course['defaultgroupingid'],
308 $dbcourses[$course['id']]->defaultgroupingid);
309 $this->assertEqual($course['lang'],
310 $dbcourses[$course['id']]->lang);
311 $this->assertEqual($course['timecreated'],
312 $dbcourses[$course['id']]->timecreated);
313 $this->assertEqual($course['timemodified'],
314 $dbcourses[$course['id']]->timemodified);
315 if (key_exists('enablecompletion', $course)) {
316 $this->assertEqual($course['enablecompletion'],
317 $dbcourses[$course['id']]->enablecompletion);
318 }
319 if (key_exists('completionstartonenrol', $course)) {
320 $this->assertEqual($course['completionstartonenrol'],
321 $dbcourses[$course['id']]->completionstartonenrol);
322 }
323 if (key_exists('completionnotify', $course)) {
324 $this->assertEqual($course['completionnotify'],
325 $dbcourses[$course['id']]->completionnotify);
326 }
327 $this->assertEqual($course['forcetheme'],
328 $dbcourses[$course['id']]->theme);
329 }
330 }
331
332 function moodle_course_create_courses($client) {
333 global $DB, $CFG;
334
335 ///Test data
336 $courseconfig = get_config('moodlecourse');
337
338 $themeobjects = get_list_of_themes();
339 $theme = key($themeobjects);
340 $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
b02b5fb7 341 $categoryid = empty($categoryid) ? 0 : $categoryid;
6bb31e40 342
343 $course1 = new stdClass();
344 $course1->fullname = 'Test Data create course 1';
345 $course1->shortname = 'testdatacourse1';
346 $course1->categoryid = $categoryid;
347 $course1->idnumber = '328327982372342343234';
348 $course1->summary = 'This is a summary';
349 $course1->summaryformat = FORMAT_HTML;
350 $course1->format = $courseconfig->format;
351 $course1->showgrades = $courseconfig->showgrades;
352 $course1->showreports = $courseconfig->showreports;
353 $course1->newsitems = $courseconfig->newsitems;
354 $course1->startdate = time();
355 $course1->numsections = $courseconfig->numsections;
356 $course1->maxbytes = $courseconfig->maxbytes;
357 $course1->visible = $courseconfig->visible;
358 $course1->hiddensections = $courseconfig->hiddensections;
359 $course1->groupmode = $courseconfig->groupmode;
360 $course1->groupmodeforce = $courseconfig->groupmodeforce;
361 $course1->defaultgroupingid = 0;
362 if (!empty($courseconfig->lang)) {
363 $course1->lang = $courseconfig->lang;
364 }
365 $course1->enablecompletion = $courseconfig->enablecompletion;
366 $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
367 $course1->completionnotify = 0;
368 $course1->forcetheme = $theme;
369
370 $course2 = new stdClass();
371 $course2->fullname = 'Test Data create course 2';
372 $course2->shortname = 'testdatacourse2';
373 $course2->categoryid = $categoryid;
374
375 $courses = array($course1, $course2);
376
377 //do not run the test if course1 or course2 already exists
378 $existingcourses = $DB->get_records_list('course', 'fullname',
379 array($course1->fullname, $course2->fullname));
380 if (!empty($existingcourses)) {
381 throw new moodle_exception('testdatacoursesalreadyexist');
382 }
383
384 $function = 'moodle_course_create_courses';
385 $params = array('courses' => $courses);
386 $resultcourses = $client->call($function, $params);
387 $this->assertEqual(count($courses), count($resultcourses));
388
389 //retrieve user1 from the DB and check values
390 $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
391 $this->assertEqual($dbcourse1->fullname, $course1->fullname);
392 $this->assertEqual($dbcourse1->shortname, $course1->shortname);
393 $this->assertEqual($dbcourse1->category, $course1->categoryid);
394 $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
395 $this->assertEqual($dbcourse1->summary, $course1->summary);
396 $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
397 $this->assertEqual($dbcourse1->format, $course1->format);
398 $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
399 $this->assertEqual($dbcourse1->showreports, $course1->showreports);
400 $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
401 $this->assertEqual($dbcourse1->startdate, $course1->startdate);
402 $this->assertEqual($dbcourse1->numsections, $course1->numsections);
403 $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
404 $this->assertEqual($dbcourse1->visible, $course1->visible);
405 $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
406 $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
407 $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
408 $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
409 if (!empty($courseconfig->lang)) {
410 $this->assertEqual($dbcourse1->lang, $course1->lang);
411 }
412 if (completion_info::is_enabled_for_site()) {
413 $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
414 $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
75681c41 415 }
6bb31e40 416 $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
417 if (!empty($CFG->allowcoursethemes)) {
418 $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
419 }
420
421 //retrieve user2 from the DB and check values
422 $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
423 $this->assertEqual($dbcourse2->fullname, $course2->fullname);
424 $this->assertEqual($dbcourse2->shortname, $course2->shortname);
b02b5fb7 425 $this->assertEqual($dbcourse2->category, $course2->categoryid);
6bb31e40 426 $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
427 $this->assertEqual($dbcourse2->format, $courseconfig->format);
428 $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
429 $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
430 $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
431 $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
432 $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
433 $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
434 $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
435 $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
436 $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
437 $this->assertEqual($dbcourse2->defaultgroupingid, 0);
438
439 //delete users from DB
440 $DB->delete_records_list('course', 'id',
441 array($dbcourse1->id, $dbcourse2->id));
442 }
443
444 function moodle_user_create_users($client) {
445 global $DB, $CFG;
446
447 //Test data
448 //a full user: user1
449 $user1 = new stdClass();
450 $user1->username = 'testusername1';
451 $user1->password = 'testpassword1';
452 $user1->firstname = 'testfirstname1';
453 $user1->lastname = 'testlastname1';
454 $user1->email = 'testemail1@moodle.com';
455 $user1->auth = 'manual';
456 $user1->idnumber = 'testidnumber1';
6bb31e40 457 $user1->lang = 'en';
458 $user1->theme = 'standard';
459 $user1->timezone = 99;
460 $user1->mailformat = 0;
461 $user1->description = 'Hello World!';
462 $user1->city = 'testcity1';
463 $user1->country = 'au';
464 $preferencename1 = 'preference1';
465 $preferencename2 = 'preference2';
466 $user1->preferences = array(
467 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
468 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
469 $customfieldname1 = 'testdatacustom1';
470 $customfieldname2 = 'testdatacustom2';
471 $user1->customfields = array(
472 array('type' => $customfieldname1, 'value' => 'customvalue'),
473 array('type' => $customfieldname2, 'value' => 'customvalue2'));
474 //a small user: user2
475 $user2 = new stdClass();
476 $user2->username = 'testusername2';
477 $user2->password = 'testpassword2';
478 $user2->firstname = 'testfirstname2';
479 $user2->lastname = 'testlastname2';
480 $user2->email = 'testemail1@moodle.com';
481
482 $users = array($user1, $user2);
483
484 //do not run the test if user1 or user2 already exists
485 $existingusers = $DB->get_records_list('user', 'username',
486 array($user1->username, $user2->username));
487 if (!empty($existingusers)) {
488 throw new moodle_exception('testdatausersalreadyexist');
489 }
490
491 //do not run the test if data test custom fields already exists
492 $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
493 array($customfieldname1, $customfieldname2));
494 if (!empty($existingcustomfields)) {
495 throw new moodle_exception('testdatacustomfieldsalreadyexist');
496 }
497
498 //create the custom fields
499 $customfield = new stdClass();
500 $customfield->shortname = $customfieldname1;
501 $customfield->name = $customfieldname1;
502 $customfield->datatype = 'text';
503 $DB->insert_record('user_info_field', $customfield);
504 $customfield = new stdClass();
505 $customfield->shortname = $customfieldname2;
506 $customfield->name = $customfieldname2;
507 $customfield->datatype = 'text';
508 $DB->insert_record('user_info_field', $customfield);
509
510 $function = 'moodle_user_create_users';
511 $params = array('users' => $users);
512 $resultusers = $client->call($function, $params);
513 $this->assertEqual(count($users), count($resultusers));
514
515 //retrieve user1 from the DB and check values
516 $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
517 $this->assertEqual($dbuser1->firstname, $user1->firstname);
518 $this->assertEqual($dbuser1->password,
519 hash_internal_user_password($user1->password));
520 $this->assertEqual($dbuser1->lastname, $user1->lastname);
521 $this->assertEqual($dbuser1->email, $user1->email);
522 $this->assertEqual($dbuser1->auth, $user1->auth);
523 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
6bb31e40 524 $this->assertEqual($dbuser1->lang, $user1->lang);
525 $this->assertEqual($dbuser1->theme, $user1->theme);
526 $this->assertEqual($dbuser1->timezone, $user1->timezone);
527 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
528 $this->assertEqual($dbuser1->description, $user1->description);
529 $this->assertEqual($dbuser1->city, $user1->city);
530 $this->assertEqual($dbuser1->country, $user1->country);
531 $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
532 null, $dbuser1->id);
533 $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
534 $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
535 null, $dbuser1->id);
536 $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
537 require_once($CFG->dirroot . "/user/profile/lib.php");
538 $customfields = profile_user_record($dbuser1->id);
539
540 $customfields = (array) $customfields;
541 $customfieldname1 = $user1->customfields[0]['type'];
542 $customfieldname2 = $user1->customfields[1]['type'];
543 $this->assertEqual($customfields[$customfieldname1],
544 $user1->customfields[0]['value']);
545 $this->assertEqual($customfields[$customfieldname2],
546 $user1->customfields[1]['value']);
547
548
549 //retrieve user2 from the DB and check values
550 $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
551 $this->assertEqual($dbuser2->firstname, $user2->firstname);
552 $this->assertEqual($dbuser2->password,
553 hash_internal_user_password($user2->password));
554 $this->assertEqual($dbuser2->lastname, $user2->lastname);
555 $this->assertEqual($dbuser2->email, $user2->email);
556
557 //unset preferences
558 $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
559
560 //clear custom fields data
561 $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
562
563 //delete custom fields
564 $DB->delete_records_list('user_info_field', 'shortname',
565 array($customfieldname1, $customfieldname2));
566
567 //delete users from DB
568 $DB->delete_records_list('user', 'id',
569 array($dbuser1->id, $dbuser2->id));
75681c41 570 }
571
572}