quiz delays MDL-20956 don't let overdue attempts + delays block students.
[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')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
29/**
30 * How to configure this unit tests:
31 * 0- Enable the web service you wish to test in the Moodle administration
32 * 1- Create a service with all functions in the Moodle administration
33 * 2- Create a token associate this service and to an admin (so you get all capabilities)
34 * 3- Configure setUp() function:
35 * a- write the token
36 * b- activate the protocols you wish to test
37 * c- activate the functions you wish to test (readonlytests and writetests arrays)
38 * d- set the number of time the web services are run
39 * Do not run WRITE test function on a production site as they impact the DB (even though every
40 * test should clean the modified data)
41 *
42 * How to write a new function:
43 * 1- Add the function name to the array readonlytests/writetests
44 * 2- Set it as false when you commit!
45 * 3- write the function - Do not prefix the function name by 'test'
46 */
47class webservice_test extends UnitTestCase {
48
49 public $testtoken;
50 public $testrest;
51 public $testxmlrpc;
52 public $testsoap;
53 public $timerrest;
54 public $timerxmlrpc;
55 public $timersoap;
56 public $readonlytests;
57 public $writetests;
58
59 function setUp() {
60 //token to test
61 $this->testtoken = '793e26aeddea7f0a696795d14dfccb0f';
62
63 //protocols to test
64 $this->testrest = false; //Does not work till XML => PHP is implemented (MDL-22965)
65 $this->testxmlrpc = false;
66 $this->testsoap = false;
67
68 ////// DB READ-ONLY tests ////
69 $this->readonlytests = array(
70 'moodle_group_get_groups' => false
71 );
72
73 ////// DB WRITE tests ////
74 $this->writetests = array(
75 'moodle_user_create_users' => false
76 );
77
78 //performance testing: number of time the web service are run
79 $this->iteration = 1;
80
81 //DO NOT CHANGE
82 //reset the timers
83 $this->timerrest = 0;
84 $this->timerxmlrpc = 0;
85 $this->timersoap = 0;
86 }
87
88 function testRun() {
89 global $CFG;
90
91 if (!empty($this->testtoken)) {
92
93 //Does not work till XML => PHP is implemented (MDL-22965)
94 if ($this->testrest) {
95
96 $this->timerrest = time();
97
98 require_once($CFG->dirroot . "/webservice/rest/lib.php");
99 $restclient = new webservice_rest_client($CFG->wwwroot
100 . '/webservice/rest/server.php', $this->testtoken);
101
102 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
103 foreach ($this->readonlytests as $functioname => $run) {
104 if ($run) {
105 //$this->{$functioname}($restclient);
106 }
107 }
108 foreach ($this->writetests as $functioname => $run) {
109 if ($run) {
110 //$this->{$functioname}($restclient);
111 }
112 }
113 }
114
115 $this->timerrest = time() - $this->timerrest;
116 //here you could call a log function to display the timer
117 //example:
118 //error_log('REST time: ');
119 //error_log(print_r($this->timerrest));
120 }
121
122 if ($this->testxmlrpc) {
123
124 $this->timerxmlrpc = time();
125
126 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
127 $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
128 . '/webservice/xmlrpc/server.php', $this->testtoken);
129
130 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
131 foreach ($this->readonlytests as $functioname => $run) {
132 if ($run) {
133 $this->{$functioname}($xmlrpcclient);
134 }
135 }
136 foreach ($this->writetests as $functioname => $run) {
137 if ($run) {
138 $this->{$functioname}($xmlrpcclient);
139 }
140 }
141 }
142
143 $this->timerxmlrpc = time() - $this->timerxmlrpc;
144 //here you could call a log function to display the timer
145 //example:
146 //error_log('XML-RPC time: ');
147 //error_log(print_r($this->timerxmlrpc));
148 }
149
150 if ($this->testsoap) {
151
152 $this->timersoap = time();
153
154 require_once($CFG->dirroot . "/webservice/soap/lib.php");
155 $soapclient = new webservice_soap_client($CFG->wwwroot
156 . '/webservice/soap/server.php', $this->testtoken);
157 $soapclient->setWsdlCache(false);
158
159 for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
160 foreach ($this->readonlytests as $functioname => $run) {
161 if ($run) {
162 $this->{$functioname}($soapclient);
163 }
164 }
165 foreach ($this->writetests as $functioname => $run) {
166 if ($run) {
167 $this->{$functioname}($soapclient);
168 }
169 }
170 }
171
172 $this->timersoap = time() - $this->timersoap;
173 //here you could call a log function to display the timer
174 //example:
175 //error_log('SOAP time: ');
176 //error_log(print_r($this->timersoap));
177 }
178 }
179 }
180
181 ///// WEB SERVICE TEST FUNCTIONS
182
183 function moodle_group_get_groups($client) {
184 global $DB;
185 $dbgroups = $DB->get_records('groups');
186 $groupids = array();
187 foreach ($dbgroups as $dbgroup) {
188 $groupids[] = $dbgroup->id;
189 }
190 $function = 'moodle_group_get_groups';
191
192 $params = array('groupids' => $groupids);
193 $groups = $client->call($function, $params);
194 $this->assertEqual(count($groups), count($groupids));
195 }
196
197 function moodle_user_create_users($client) {
198 global $DB;
199 //do not run the test if users already exists
200 $users = $DB->get_records_list('user', 'username',
201 array('testusername1', 'testusername2'));
202 if (!empty($users)) {
203 throw new moodle_exception('testuseralreadyexist');
204 } else {
205 //a full user
206 $user1 = new stdClass();
207 $user1->username = 'testusername1';
208 $user1->password = 'testpassword1';
209 $user1->firstname = 'testfirstname1';
210 $user1->lastname = 'testlastname1';
211 $user1->email = 'testemail1@moodle.com';
212 $user1->auth = 'manual';
213 $user1->idnumber = 'testidnumber1';
214 $user1->emailstop = 1;
215 $user1->lang = 'en';
216 $user1->theme = 'standard';
217 $user1->timezone = 99;
218 $user1->mailformat = 0;
219 $user1->description = 'Hello World!';
220 $user1->city = 'testcity1';
221 $user1->country = 'au';
222 $user1->preferences = array(
223 array('type' => 'preference1', 'value' => 'value1'),
224 array('type' => 'preference2', 'value' => 'value2'));
225 $user1->customfields = array(
226 array('type' => 'type', 'value' => 'value'),
227 array('type' => 'type2', 'value' => 'value2'));
228
229 //a minimum user
230 $user2 = new stdClass();
231 $user2->username = 'testusername2';
232 $user2->password = 'testpassword2';
233 $user2->firstname = 'testfirstname2';
234 $user2->lastname = 'testlastname2';
235 $user2->email = 'testemail1@moodle.com';
236
237 $users = array($user1, $user2);
238
239 $function = 'moodle_user_create_users';
240 $params = array('users' => $users);
241 $resultusers = $client->call($function, $params);
242 $this->assertEqual(count($users), count($resultusers));
243
244 //retrieve users from the DB and check values
245 $dbuser1 = $DB->get_record('user', array('username' => 'testusername1'));
246 $this->assertEqual($dbuser1->firstname, $user1->firstname);
247 $this->assertEqual($dbuser1->password,
248 hash_internal_user_password($user1->password));
249 $this->assertEqual($dbuser1->lastname, $user1->lastname);
250 $this->assertEqual($dbuser1->email, $user1->email);
251 $this->assertEqual($dbuser1->auth, $user1->auth);
252 $this->assertEqual($dbuser1->idnumber, $user1->idnumber);
253 $this->assertEqual($dbuser1->emailstop, $user1->emailstop);
254 $this->assertEqual($dbuser1->lang, $user1->lang);
255 $this->assertEqual($dbuser1->theme, $user1->theme);
256 $this->assertEqual($dbuser1->timezone, $user1->timezone);
257 $this->assertEqual($dbuser1->mailformat, $user1->mailformat);
258 $this->assertEqual($dbuser1->description, $user1->description);
259 $this->assertEqual($dbuser1->city, $user1->city);
260 $this->assertEqual($dbuser1->country, $user1->country);
261 $user1preference1 = get_user_preferences('preference1', null, $dbuser1->id);
262 $this->assertEqual('value1', $user1preference1);
263 $user1preference2 = get_user_preferences('preference2', null, $dbuser1->id);
264 $this->assertEqual('value2', $user1preference2);
265
266 //retrieve users from the DB and check values
267 $dbuser2 = $DB->get_record('user', array('username' => 'testusername2'));
268 $this->assertEqual($dbuser2->firstname, $user2->firstname);
269 $this->assertEqual($dbuser2->password,
270 hash_internal_user_password($user2->password));
271 $this->assertEqual($dbuser2->lastname, $user2->lastname);
272 $this->assertEqual($dbuser2->email, $user2->email);
273
274 //delete users from DB
275 $DB->delete_records_list('user', 'id',
276 array($dbuser1->id, $dbuser2->id));
277 }
278 }
279
280}