blocks MDL-24002 Split block renderering into several smaller methods, one for each...
[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,
75681c41 74 );
75
6bb31e40 76 ////// WRITE DB tests ////
75681c41 77 $this->writetests = array(
6bb31e40 78 'moodle_user_create_users' => false,
79 'moodle_course_create_courses' => false,
75681c41 80 );
81
82 //performance testing: number of time the web service are run
83 $this->iteration = 1;
84
85 //DO NOT CHANGE
86 //reset the timers
87 $this->timerrest = 0;
88 $this->timerxmlrpc = 0;
89 $this->timersoap = 0;
90 }
91
92 function testRun() {
93 global $CFG;
94
95 if (!empty($this->testtoken)) {
96
97 //Does not work till XML => PHP is implemented (MDL-22965)
98 if ($this->testrest) {
99
100 $this->timerrest = time();
101
102 require_once($CFG->dirroot . "/webservice/rest/lib.php");
103 $restclient = new webservice_rest_client($CFG->wwwroot
104 . '/webservice/rest/server.php', $this->testtoken);
105
106 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
107 foreach ($this->readonlytests as $functioname => $run) {
108 if ($run) {
109 //$this->{$functioname}($restclient);
110 }
111 }
112 foreach ($this->writetests as $functioname => $run) {
113 if ($run) {
114 //$this->{$functioname}($restclient);
115 }
116 }
117 }
118
119 $this->timerrest = time() - $this->timerrest;
120 //here you could call a log function to display the timer
121 //example:
122 //error_log('REST time: ');
123 //error_log(print_r($this->timerrest));
124 }
125
126 if ($this->testxmlrpc) {
127
128 $this->timerxmlrpc = time();
129
130 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
131 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
132 . '/webservice/xmlrpc/server.php', $this->testtoken);
133
134 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
135 foreach ($this->readonlytests as $functioname => $run) {
136 if ($run) {
137 $this->{$functioname}($xmlrpcclient);
138 }
139 }
140 foreach ($this->writetests as $functioname => $run) {
141 if ($run) {
142 $this->{$functioname}($xmlrpcclient);
143 }
144 }
145 }
146
147 $this->timerxmlrpc = time() - $this->timerxmlrpc;
148 //here you could call a log function to display the timer
149 //example:
150 //error_log('XML-RPC time: ');
151 //error_log(print_r($this->timerxmlrpc));
152 }
153
154 if ($this->testsoap) {
155
156 $this->timersoap = time();
157
158 require_once($CFG->dirroot . "/webservice/soap/lib.php");
159 $soapclient = new webservice_soap_client($CFG->wwwroot
160 . '/webservice/soap/server.php', $this->testtoken);
161 $soapclient->setWsdlCache(false);
162
163 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
164 foreach ($this->readonlytests as $functioname => $run) {
165 if ($run) {
166 $this->{$functioname}($soapclient);
167 }
168 }
169 foreach ($this->writetests as $functioname => $run) {
170 if ($run) {
171 $this->{$functioname}($soapclient);
172 }
173 }
174 }
175
176 $this->timersoap = time() - $this->timersoap;
177 //here you could call a log function to display the timer
178 //example:
179 //error_log('SOAP time: ');
180 //error_log(print_r($this->timersoap));
181 }
182 }
183 }
184
185 ///// WEB SERVICE TEST FUNCTIONS
186
187 function moodle_group_get_groups($client) {
188 global $DB;
189 $dbgroups = $DB->get_records('groups');
190 $groupids = array();
191 foreach ($dbgroups as $dbgroup) {
192 $groupids[] = $dbgroup->id;
193 }
194 $function = 'moodle_group_get_groups';
195
196 $params = array('groupids' => $groupids);
197 $groups = $client->call($function, $params);
198 $this->assertEqual(count($groups), count($groupids));
199 }
200
6bb31e40 201 function moodle_user_get_users_by_id($client) {
202 global $DB;
203 $dbusers = $DB->get_records('user');
204 $userids = array();
205 foreach ($dbusers as $dbuser) {
206 $userids[] = $dbuser->id;
207 }
208 $function = 'moodle_user_get_users_by_id';
209
210 $params = array('userids' => $userids);
211 $users = $client->call($function, $params);
212
213 $this->assertEqual(count($users), count($userids));
214 }
215
216 function moodle_course_get_courses($client) {
75681c41 217 global $DB;
6bb31e40 218
219 $function = 'moodle_course_get_courses';
220
221 //retrieve all courses from db
222 $dbcourses = $DB->get_records('course');
223 $courseids = array();
224 foreach ($dbcourses as $dbcourse) {
225 $courseids[] = $dbcourse->id;
226 }
227
228 //retrieve all courses by id
229 $params = array('options' => array('ids' => $courseids));
230 $courses = $client->call($function, $params);
231
232 //check it is the same course count
233 $this->assertEqual(count($courses), count($courseids));
234
235 //check all course values are identic
236 foreach ($courses as $course) {
237 $this->assertEqual($course['fullname'],
238 $dbcourses[$course['id']]->fullname);
239 $this->assertEqual($course['shortname'],
240 $dbcourses[$course['id']]->shortname);
241 $this->assertEqual($course['categoryid'],
242 $dbcourses[$course['id']]->category);
243 $this->assertEqual($course['categorysortorder'],
244 $dbcourses[$course['id']]->sortorder);
245 $this->assertEqual($course['idnumber'],
246 $dbcourses[$course['id']]->idnumber);
247 $this->assertEqual($course['summary'],
248 $dbcourses[$course['id']]->summary);
249 $this->assertEqual($course['summaryformat'],
250 $dbcourses[$course['id']]->summaryformat);
251 $this->assertEqual($course['format'],
252 $dbcourses[$course['id']]->format);
253 $this->assertEqual($course['showgrades'],
254 $dbcourses[$course['id']]->showgrades);
255 $this->assertEqual($course['newsitems'],
256 $dbcourses[$course['id']]->newsitems);
257 $this->assertEqual($course['startdate'],
258 $dbcourses[$course['id']]->startdate);
259 $this->assertEqual($course['numsections'],
260 $dbcourses[$course['id']]->numsections);
261 $this->assertEqual($course['maxbytes'],
262 $dbcourses[$course['id']]->maxbytes);
263 $this->assertEqual($course['visible'],
264 $dbcourses[$course['id']]->visible);
265 $this->assertEqual($course['hiddensections'],
266 $dbcourses[$course['id']]->hiddensections);
267 $this->assertEqual($course['groupmode'],
268 $dbcourses[$course['id']]->groupmode);
269 $this->assertEqual($course['groupmodeforce'],
270 $dbcourses[$course['id']]->groupmodeforce);
271 $this->assertEqual($course['defaultgroupingid'],
272 $dbcourses[$course['id']]->defaultgroupingid);
273 $this->assertEqual($course['lang'],
274 $dbcourses[$course['id']]->lang);
275 $this->assertEqual($course['timecreated'],
276 $dbcourses[$course['id']]->timecreated);
277 $this->assertEqual($course['timemodified'],
278 $dbcourses[$course['id']]->timemodified);
279 if (key_exists('enablecompletion', $course)) {
280 $this->assertEqual($course['enablecompletion'],
281 $dbcourses[$course['id']]->enablecompletion);
282 }
283 if (key_exists('completionstartonenrol', $course)) {
284 $this->assertEqual($course['completionstartonenrol'],
285 $dbcourses[$course['id']]->completionstartonenrol);
286 }
287 if (key_exists('completionnotify', $course)) {
288 $this->assertEqual($course['completionnotify'],
289 $dbcourses[$course['id']]->completionnotify);
290 }
291 $this->assertEqual($course['forcetheme'],
292 $dbcourses[$course['id']]->theme);
293 }
294 }
295
296 function moodle_course_create_courses($client) {
297 global $DB, $CFG;
298
299 ///Test data
300 $courseconfig = get_config('moodlecourse');
301
302 $themeobjects = get_list_of_themes();
303 $theme = key($themeobjects);
304 $categoryid = $DB->get_record('course_categories', array(), '*', IGNORE_MULTIPLE)->id;
305 $categoryid = empty($categoryid)?0:$categoryid;
306
307 $course1 = new stdClass();
308 $course1->fullname = 'Test Data create course 1';
309 $course1->shortname = 'testdatacourse1';
310 $course1->categoryid = $categoryid;
311 $course1->idnumber = '328327982372342343234';
312 $course1->summary = 'This is a summary';
313 $course1->summaryformat = FORMAT_HTML;
314 $course1->format = $courseconfig->format;
315 $course1->showgrades = $courseconfig->showgrades;
316 $course1->showreports = $courseconfig->showreports;
317 $course1->newsitems = $courseconfig->newsitems;
318 $course1->startdate = time();
319 $course1->numsections = $courseconfig->numsections;
320 $course1->maxbytes = $courseconfig->maxbytes;
321 $course1->visible = $courseconfig->visible;
322 $course1->hiddensections = $courseconfig->hiddensections;
323 $course1->groupmode = $courseconfig->groupmode;
324 $course1->groupmodeforce = $courseconfig->groupmodeforce;
325 $course1->defaultgroupingid = 0;
326 if (!empty($courseconfig->lang)) {
327 $course1->lang = $courseconfig->lang;
328 }
329 $course1->enablecompletion = $courseconfig->enablecompletion;
330 $course1->completionstartonenrol = $courseconfig->completionstartonenrol;
331 $course1->completionnotify = 0;
332 $course1->forcetheme = $theme;
333
334 $course2 = new stdClass();
335 $course2->fullname = 'Test Data create course 2';
336 $course2->shortname = 'testdatacourse2';
337 $course2->categoryid = $categoryid;
338
339 $courses = array($course1, $course2);
340
341 //do not run the test if course1 or course2 already exists
342 $existingcourses = $DB->get_records_list('course', 'fullname',
343 array($course1->fullname, $course2->fullname));
344 if (!empty($existingcourses)) {
345 throw new moodle_exception('testdatacoursesalreadyexist');
346 }
347
348 $function = 'moodle_course_create_courses';
349 $params = array('courses' => $courses);
350 $resultcourses = $client->call($function, $params);
351 $this->assertEqual(count($courses), count($resultcourses));
352
353 //retrieve user1 from the DB and check values
354 $dbcourse1 = $DB->get_record('course', array('fullname' => $course1->fullname));
355 $this->assertEqual($dbcourse1->fullname, $course1->fullname);
356 $this->assertEqual($dbcourse1->shortname, $course1->shortname);
357 $this->assertEqual($dbcourse1->category, $course1->categoryid);
358 $this->assertEqual($dbcourse1->idnumber, $course1->idnumber);
359 $this->assertEqual($dbcourse1->summary, $course1->summary);
360 $this->assertEqual($dbcourse1->summaryformat, $course1->summaryformat);
361 $this->assertEqual($dbcourse1->format, $course1->format);
362 $this->assertEqual($dbcourse1->showgrades, $course1->showgrades);
363 $this->assertEqual($dbcourse1->showreports, $course1->showreports);
364 $this->assertEqual($dbcourse1->newsitems, $course1->newsitems);
365 $this->assertEqual($dbcourse1->startdate, $course1->startdate);
366 $this->assertEqual($dbcourse1->numsections, $course1->numsections);
367 $this->assertEqual($dbcourse1->maxbytes, $course1->maxbytes);
368 $this->assertEqual($dbcourse1->visible, $course1->visible);
369 $this->assertEqual($dbcourse1->hiddensections, $course1->hiddensections);
370 $this->assertEqual($dbcourse1->groupmode, $course1->groupmode);
371 $this->assertEqual($dbcourse1->groupmodeforce, $course1->groupmodeforce);
372 $this->assertEqual($dbcourse1->defaultgroupingid, $course1->defaultgroupingid);
373 if (!empty($courseconfig->lang)) {
374 $this->assertEqual($dbcourse1->lang, $course1->lang);
375 }
376 if (completion_info::is_enabled_for_site()) {
377 $this->assertEqual($dbcourse1->enablecompletion, $course1->enablecompletion);
378 $this->assertEqual($dbcourse1->completionstartonenrol, $course1->completionstartonenrol);
75681c41 379 }
6bb31e40 380 $this->assertEqual($dbcourse1->completionnotify, $course1->completionnotify);
381 if (!empty($CFG->allowcoursethemes)) {
382 $this->assertEqual($dbcourse1->theme, $course1->forcetheme);
383 }
384
385 //retrieve user2 from the DB and check values
386 $dbcourse2 = $DB->get_record('course', array('fullname' => $course2->fullname));
387 $this->assertEqual($dbcourse2->fullname, $course2->fullname);
388 $this->assertEqual($dbcourse2->shortname, $course2->shortname);
389 $this->assertEqual($dbcourse2->category, $course2->categoryid );
390 $this->assertEqual($dbcourse2->summaryformat, FORMAT_MOODLE);
391 $this->assertEqual($dbcourse2->format, $courseconfig->format);
392 $this->assertEqual($dbcourse2->showgrades, $courseconfig->showgrades);
393 $this->assertEqual($dbcourse2->showreports, $courseconfig->showreports);
394 $this->assertEqual($dbcourse2->newsitems, $courseconfig->newsitems);
395 $this->assertEqual($dbcourse2->numsections, $courseconfig->numsections);
396 $this->assertEqual($dbcourse2->maxbytes, $courseconfig->maxbytes);
397 $this->assertEqual($dbcourse2->visible, $courseconfig->visible);
398 $this->assertEqual($dbcourse2->hiddensections, $courseconfig->hiddensections);
399 $this->assertEqual($dbcourse2->groupmode, $courseconfig->groupmode);
400 $this->assertEqual($dbcourse2->groupmodeforce, $courseconfig->groupmodeforce);
401 $this->assertEqual($dbcourse2->defaultgroupingid, 0);
402
403 //delete users from DB
404 $DB->delete_records_list('course', 'id',
405 array($dbcourse1->id, $dbcourse2->id));
406 }
407
408 function moodle_user_create_users($client) {
409 global $DB, $CFG;
410
411 //Test data
412 //a full user: user1
413 $user1 = new stdClass();
414 $user1->username = 'testusername1';
415 $user1->password = 'testpassword1';
416 $user1->firstname = 'testfirstname1';
417 $user1->lastname = 'testlastname1';
418 $user1->email = 'testemail1@moodle.com';
419 $user1->auth = 'manual';
420 $user1->idnumber = 'testidnumber1';
421 $user1->emailstop = 1;
422 $user1->lang = 'en';
423 $user1->theme = 'standard';
424 $user1->timezone = 99;
425 $user1->mailformat = 0;
426 $user1->description = 'Hello World!';
427 $user1->city = 'testcity1';
428 $user1->country = 'au';
429 $preferencename1 = 'preference1';
430 $preferencename2 = 'preference2';
431 $user1->preferences = array(
432 array('type' => $preferencename1, 'value' => 'preferencevalue1'),
433 array('type' => $preferencename2, 'value' => 'preferencevalue2'));
434 $customfieldname1 = 'testdatacustom1';
435 $customfieldname2 = 'testdatacustom2';
436 $user1->customfields = array(
437 array('type' => $customfieldname1, 'value' => 'customvalue'),
438 array('type' => $customfieldname2, 'value' => 'customvalue2'));
439 //a small user: user2
440 $user2 = new stdClass();
441 $user2->username = 'testusername2';
442 $user2->password = 'testpassword2';
443 $user2->firstname = 'testfirstname2';
444 $user2->lastname = 'testlastname2';
445 $user2->email = 'testemail1@moodle.com';
446
447 $users = array($user1, $user2);
448
449 //do not run the test if user1 or user2 already exists
450 $existingusers = $DB->get_records_list('user', 'username',
451 array($user1->username, $user2->username));
452 if (!empty($existingusers)) {
453 throw new moodle_exception('testdatausersalreadyexist');
454 }
455
456 //do not run the test if data test custom fields already exists
457 $existingcustomfields = $DB->get_records_list('user_info_field', 'shortname',
458 array($customfieldname1, $customfieldname2));
459 if (!empty($existingcustomfields)) {
460 throw new moodle_exception('testdatacustomfieldsalreadyexist');
461 }
462
463 //create the custom fields
464 $customfield = new stdClass();
465 $customfield->shortname = $customfieldname1;
466 $customfield->name = $customfieldname1;
467 $customfield->datatype = 'text';
468 $DB->insert_record('user_info_field', $customfield);
469 $customfield = new stdClass();
470 $customfield->shortname = $customfieldname2;
471 $customfield->name = $customfieldname2;
472 $customfield->datatype = 'text';
473 $DB->insert_record('user_info_field', $customfield);
474
475 $function = 'moodle_user_create_users';
476 $params = array('users' => $users);
477 $resultusers = $client->call($function, $params);
478 $this->assertEqual(count($users), count($resultusers));
479
480 //retrieve user1 from the DB and check values
481 $dbuser1 = $DB->get_record('user', array('username' => $user1->username));
482 $this->assertEqual($dbuser1->firstname, $user1->firstname);
483 $this->assertEqual($dbuser1->password,
484 hash_internal_user_password($user1->password));
485 $this->assertEqual($dbuser1->lastname, $user1->lastname);
486 $this->assertEqual($dbuser1->email, $user1->email);
487 $this->assertEqual($dbuser1->auth, $user1->auth);
488 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
489 $this->assertEqual($dbuser1->emailstop, $user1->emailstop);
490 $this->assertEqual($dbuser1->lang, $user1->lang);
491 $this->assertEqual($dbuser1->theme, $user1->theme);
492 $this->assertEqual($dbuser1->timezone, $user1->timezone);
493 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
494 $this->assertEqual($dbuser1->description, $user1->description);
495 $this->assertEqual($dbuser1->city, $user1->city);
496 $this->assertEqual($dbuser1->country, $user1->country);
497 $user1preference1 = get_user_preferences($user1->preferences[0]['type'],
498 null, $dbuser1->id);
499 $this->assertEqual($user1->preferences[0]['value'], $user1preference1);
500 $user1preference2 = get_user_preferences($user1->preferences[1]['type'],
501 null, $dbuser1->id);
502 $this->assertEqual($user1->preferences[1]['value'], $user1preference2);
503 require_once($CFG->dirroot . "/user/profile/lib.php");
504 $customfields = profile_user_record($dbuser1->id);
505
506 $customfields = (array) $customfields;
507 $customfieldname1 = $user1->customfields[0]['type'];
508 $customfieldname2 = $user1->customfields[1]['type'];
509 $this->assertEqual($customfields[$customfieldname1],
510 $user1->customfields[0]['value']);
511 $this->assertEqual($customfields[$customfieldname2],
512 $user1->customfields[1]['value']);
513
514
515 //retrieve user2 from the DB and check values
516 $dbuser2 = $DB->get_record('user', array('username' => $user2->username));
517 $this->assertEqual($dbuser2->firstname, $user2->firstname);
518 $this->assertEqual($dbuser2->password,
519 hash_internal_user_password($user2->password));
520 $this->assertEqual($dbuser2->lastname, $user2->lastname);
521 $this->assertEqual($dbuser2->email, $user2->email);
522
523 //unset preferences
524 $DB->delete_records('user_preferences', array('userid' => $dbuser1->id));
525
526 //clear custom fields data
527 $DB->delete_records('user_info_data', array('userid' => $dbuser1->id));
528
529 //delete custom fields
530 $DB->delete_records_list('user_info_field', 'shortname',
531 array($customfieldname1, $customfieldname2));
532
533 //delete users from DB
534 $DB->delete_records_list('user', 'id',
535 array($dbuser1->id, $dbuser2->id));
75681c41 536 }
537
538}