Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / lib / tests / moodlelib_test.php
blobcedb716c5bdf0bc3de0ff2591cf84690d44f60b6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for (some of) ../moodlelib.php.
20 * @package core
21 * @category phpunit
22 * @copyright &copy; 2006 The Open University
23 * @author T.J.Hunt@open.ac.uk
24 * @author nicolas@moodle.com
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/moodlelib.php');
33 class moodlelib_testcase extends advanced_testcase {
35 public static $includecoverage = array('lib/moodlelib.php');
37 var $user_agents = array(
38 'MSIE' => array(
39 '5.0' => array('Windows 98' => 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'),
40 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
41 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
42 '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'),
43 '8.0' => array('Windows Vista' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)'),
44 '9.0' => array('Windows 7' => 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'),
45 '9.0i' => array('Windows 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)'),
46 '10.0' => array('Windows 8' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'),
47 '10.0i' => array('Windows 8' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0; Touch; .NET4.0E; .NET4.0C; Tablet PC 2.0)'),
49 'Firefox' => array(
50 '1.0.6' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'),
51 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
52 '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'),
53 '2.0' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
54 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'),
55 '3.0.6' => array('SUSE' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-1.4 Firefox/3.0.6'),
56 '3.6' => array('Linux' => 'Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/3.6'),
57 '11.0' => array('Windows' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko Firefox/11.0'),
58 '15.0a2' => array('Windows' => 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2'),
59 '18.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:18.0) Gecko/18.0 Firefox/18.0'),
61 'SeaMonkey' => array(
62 '2.0' => array('Windows' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1b3pre) Gecko/20081208 SeaMonkey/2.0'),
63 '2.1' => array('Linux' => 'Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20110609 Firefox/4.0.1 SeaMonkey/2.1'),
64 '2.3' => array('FreeBSD' => 'Mozilla/5.0 (X11; FreeBSD amd64; rv:6.0) Gecko/20110818 Firefox/6.0 SeaMonkey/2.3'),
66 'Safari' => array(
67 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
68 '412' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
70 'Safari iOS' => array(
71 '528' => array('iPhone' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; cs-cz) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16'),
72 '533' => array('iPad' => 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5'),
74 'WebKit Android' => array(
75 '525' => array('G1 Phone' => 'Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone'),
76 '530' => array('Nexus' => 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus'),
78 'Chrome' => array(
79 '8' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10'),
81 'Opera' => array(
82 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
83 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
84 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
88 /**
89 * Define a local decimal separator.
91 * It is not possible to directly change the result of get_string in
92 * a unit test. Instead, we create a language pack for language 'xx' in
93 * dataroot and make langconfig.php with the string we need to change.
94 * The example separator used here is 'X'; on PHP 5.3 and before this
95 * must be a single byte character due to PHP bug/limitation in
96 * number_format, so you can't use UTF-8 characters.
98 * @global type $SESSION
99 * @global type $CFG
101 protected function define_local_decimal_separator() {
102 global $SESSION, $CFG;
104 $SESSION->lang = 'xx';
105 $langconfig = "<?php\n\$string['decsep'] = 'X';";
106 $langfolder = $CFG->dataroot . '/lang/xx';
107 check_dir_exists($langfolder);
108 file_put_contents($langfolder . '/langconfig.php', $langconfig);
111 function test_cleanremoteaddr() {
112 //IPv4
113 $this->assertEquals(cleanremoteaddr('1023.121.234.1'), null);
114 $this->assertEquals(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
116 //IPv6
117 $this->assertEquals(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
118 $this->assertEquals(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
119 $this->assertEquals(cleanremoteaddr('0:0:0:::0:0:1'), null);
120 $this->assertEquals(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
121 $this->assertEquals(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
122 $this->assertEquals(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
123 $this->assertEquals(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
124 $this->assertEquals(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
125 $this->assertEquals(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
126 $this->assertEquals(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
127 $this->assertEquals(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
130 function test_address_in_subnet() {
131 /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn (number of bits in net mask)
132 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
133 $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
134 $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0'));
135 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
136 $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
137 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
138 $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
139 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
140 $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
141 $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
142 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
143 $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
144 $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
145 $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
146 $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
147 $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
149 // fixed input
150 $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24'));
151 $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
153 // incorrect input
154 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
155 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
156 $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
157 $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
158 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
159 $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
160 $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
161 $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
162 $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
163 $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
166 /// 2: xxx.xxx.xxx.xxx-yyy or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::xxxx-yyyy (a range of IP addresses in the last group)
167 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
168 $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
169 $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
170 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
171 $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
172 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
173 $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
174 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
175 $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
176 $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
177 $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
178 $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
179 $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
180 $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
182 // fixed input
183 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
184 $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe '));
186 // incorrect input
187 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
188 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
189 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
192 /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-)
193 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
194 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
195 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.'));
196 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234'));
197 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
198 $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
199 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
200 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
201 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
202 $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
203 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
204 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
205 $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
208 /// multiple subnets
209 $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
210 $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
211 $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30'));
212 $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
215 /// other incorrect input
216 $this->assertFalse(address_in_subnet('123.123.123.123', ''));
220 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
221 * works as expected.
223 function test_check_browser_version()
225 global $CFG;
227 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
228 $this->assertTrue(check_browser_version('Safari'));
229 $this->assertTrue(check_browser_version('WebKit'));
230 $this->assertTrue(check_browser_version('Safari', '312'));
231 $this->assertFalse(check_browser_version('Safari', '500'));
232 $this->assertFalse(check_browser_version('Chrome'));
233 $this->assertFalse(check_browser_version('Safari iOS'));
235 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
236 $this->assertTrue(check_browser_version('Safari iOS'));
237 $this->assertTrue(check_browser_version('WebKit'));
238 $this->assertTrue(check_browser_version('Safari iOS', '527'));
239 $this->assertFalse(check_browser_version('Safari iOS', 590));
240 $this->assertFalse(check_browser_version('Safari', '312'));
241 $this->assertFalse(check_browser_version('Safari', '500'));
242 $this->assertFalse(check_browser_version('Chrome'));
244 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
245 $this->assertTrue(check_browser_version('WebKit'));
246 $this->assertTrue(check_browser_version('WebKit Android', '527'));
247 $this->assertFalse(check_browser_version('WebKit Android', 590));
248 $this->assertFalse(check_browser_version('Safari'));
249 $this->assertFalse(check_browser_version('Chrome'));
251 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
252 $this->assertTrue(check_browser_version('Chrome'));
253 $this->assertTrue(check_browser_version('WebKit'));
254 $this->assertTrue(check_browser_version('Chrome', 8));
255 $this->assertFalse(check_browser_version('Chrome', 10));
256 $this->assertFalse(check_browser_version('Safari', '1'));
258 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
259 $this->assertTrue(check_browser_version('Opera'));
260 $this->assertTrue(check_browser_version('Opera', '8.0'));
261 $this->assertFalse(check_browser_version('Opera', '10.0'));
263 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
264 $this->assertTrue(check_browser_version('MSIE'));
265 $this->assertTrue(check_browser_version('MSIE', '5.0'));
266 $this->assertFalse(check_browser_version('MSIE', '7.0'));
268 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98'];
269 $this->assertFalse(check_browser_version('MSIE'));
270 $this->assertTrue(check_browser_version('MSIE', 0));
271 $this->assertTrue(check_browser_version('MSIE', '5.0'));
272 $this->assertFalse(check_browser_version('MSIE', '7.0'));
274 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
275 $this->assertTrue(check_browser_version('MSIE'));
276 $this->assertTrue(check_browser_version('MSIE', 0));
277 $this->assertTrue(check_browser_version('MSIE', '5.0'));
278 $this->assertTrue(check_browser_version('MSIE', '9.0'));
279 $this->assertFalse(check_browser_version('MSIE', '10'));
281 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0i']['Windows 7'];
282 $this->assertTrue(check_browser_version('MSIE'));
283 $this->assertTrue(check_browser_version('MSIE', 0));
284 $this->assertTrue(check_browser_version('MSIE', '5.0'));
285 $this->assertTrue(check_browser_version('MSIE', '9.0'));
286 $this->assertFalse(check_browser_version('MSIE', '10'));
288 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0']['Windows 8'];
289 $this->assertTrue(check_browser_version('MSIE'));
290 $this->assertTrue(check_browser_version('MSIE', 0));
291 $this->assertTrue(check_browser_version('MSIE', '5.0'));
292 $this->assertTrue(check_browser_version('MSIE', '9.0'));
293 $this->assertTrue(check_browser_version('MSIE', '10'));
294 $this->assertFalse(check_browser_version('MSIE', '11'));
296 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0i']['Windows 8'];
297 $this->assertTrue(check_browser_version('MSIE'));
298 $this->assertTrue(check_browser_version('MSIE', 0));
299 $this->assertTrue(check_browser_version('MSIE', '5.0'));
300 $this->assertTrue(check_browser_version('MSIE', '9.0'));
301 $this->assertTrue(check_browser_version('MSIE', '10'));
302 $this->assertFalse(check_browser_version('MSIE', '11'));
304 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
305 $this->assertTrue(check_browser_version('Firefox'));
306 $this->assertTrue(check_browser_version('Firefox', '1.5'));
307 $this->assertFalse(check_browser_version('Firefox', '3.0'));
308 $this->assertTrue(check_browser_version('Gecko', '2'));
309 $this->assertTrue(check_browser_version('Gecko', 20030516));
310 $this->assertTrue(check_browser_version('Gecko', 20051106));
311 $this->assertTrue(check_browser_version('Gecko', 2006010100));
313 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP'];
314 $this->assertTrue(check_browser_version('Firefox'));
315 $this->assertTrue(check_browser_version('Gecko', '1'));
316 $this->assertFalse(check_browser_version('Gecko', 20030516));
317 $this->assertFalse(check_browser_version('Gecko', 20051106));
318 $this->assertFalse(check_browser_version('Gecko', 2006010100));
319 $this->assertFalse(check_browser_version('Firefox', '1.5'));
320 $this->assertFalse(check_browser_version('Firefox', '3.0'));
321 $this->assertFalse(check_browser_version('Gecko', '2'));
323 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
324 $this->assertTrue(check_browser_version('Firefox'));
325 $this->assertTrue(check_browser_version('Firefox', '1.5'));
326 $this->assertTrue(check_browser_version('Gecko', '1'));
327 $this->assertTrue(check_browser_version('Gecko', '2'));
328 $this->assertTrue(check_browser_version('Gecko', 20030516));
329 $this->assertTrue(check_browser_version('Gecko', 20051106));
330 $this->assertTrue(check_browser_version('Gecko', 2006010100));
331 $this->assertFalse(check_browser_version('Firefox', '3.0'));
333 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.6']['Linux'];
334 $this->assertTrue(check_browser_version('Firefox'));
335 $this->assertTrue(check_browser_version('Firefox', '1.5'));
336 $this->assertTrue(check_browser_version('Firefox', '3.0'));
337 $this->assertTrue(check_browser_version('Gecko', '2'));
338 $this->assertTrue(check_browser_version('Gecko', '3.6'));
339 $this->assertTrue(check_browser_version('Gecko', 20030516));
340 $this->assertTrue(check_browser_version('Gecko', 20051106));
341 $this->assertTrue(check_browser_version('Gecko', 2006010100));
342 $this->assertFalse(check_browser_version('Firefox', '4'));
343 $this->assertFalse(check_browser_version('Firefox', '10'));
345 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.6']['Linux'];
346 $this->assertTrue(check_browser_version('Firefox'));
347 $this->assertTrue(check_browser_version('Firefox', '1.5'));
348 $this->assertTrue(check_browser_version('Firefox', '3.0'));
349 $this->assertTrue(check_browser_version('Gecko', '2'));
350 $this->assertTrue(check_browser_version('Gecko', '3.6'));
351 $this->assertTrue(check_browser_version('Gecko', 20030516));
352 $this->assertTrue(check_browser_version('Gecko', 20051106));
353 $this->assertTrue(check_browser_version('Gecko', 2006010100));
354 $this->assertFalse(check_browser_version('Firefox', '4'));
355 $this->assertFalse(check_browser_version('Firefox', '10'));
356 $this->assertFalse(check_browser_version('Firefox', '18'));
357 $this->assertFalse(check_browser_version('Gecko', '4'));
359 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['15.0a2']['Windows'];
360 $this->assertTrue(check_browser_version('Firefox'));
361 $this->assertTrue(check_browser_version('Firefox', '1.5'));
362 $this->assertTrue(check_browser_version('Firefox', '3.0'));
363 $this->assertTrue(check_browser_version('Gecko', '2'));
364 $this->assertTrue(check_browser_version('Gecko', '3.6'));
365 $this->assertTrue(check_browser_version('Gecko', '15.0'));
366 $this->assertTrue(check_browser_version('Gecko', 20030516));
367 $this->assertTrue(check_browser_version('Gecko', 20051106));
368 $this->assertTrue(check_browser_version('Gecko', 2006010100));
369 $this->assertTrue(check_browser_version('Firefox', '4'));
370 $this->assertTrue(check_browser_version('Firefox', '10'));
371 $this->assertTrue(check_browser_version('Firefox', '15'));
372 $this->assertFalse(check_browser_version('Firefox', '18'));
373 $this->assertFalse(check_browser_version('Gecko', '18'));
375 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['18.0']['Mac OS X'];
376 $this->assertTrue(check_browser_version('Firefox'));
377 $this->assertTrue(check_browser_version('Firefox', '1.5'));
378 $this->assertTrue(check_browser_version('Firefox', '3.0'));
379 $this->assertTrue(check_browser_version('Gecko', '2'));
380 $this->assertTrue(check_browser_version('Gecko', '3.6'));
381 $this->assertTrue(check_browser_version('Gecko', '15.0'));
382 $this->assertTrue(check_browser_version('Gecko', '18.0'));
383 $this->assertTrue(check_browser_version('Gecko', 20030516));
384 $this->assertTrue(check_browser_version('Gecko', 20051106));
385 $this->assertTrue(check_browser_version('Gecko', 2006010100));
386 $this->assertTrue(check_browser_version('Firefox', '4'));
387 $this->assertTrue(check_browser_version('Firefox', '10'));
388 $this->assertTrue(check_browser_version('Firefox', '15'));
389 $this->assertTrue(check_browser_version('Firefox', '18'));
390 $this->assertFalse(check_browser_version('Firefox', '19'));
391 $this->assertFalse(check_browser_version('Gecko', '19'));
393 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['SeaMonkey']['2.0']['Windows'];
395 $this->assertTrue(check_browser_version('Gecko', '2'));
396 $this->assertTrue(check_browser_version('Gecko', 20030516));
397 $this->assertTrue(check_browser_version('Gecko', 20051106));
398 $this->assertTrue(check_browser_version('Gecko', 2006010100));
399 $this->assertFalse(check_browser_version('Gecko', '3.6'));
400 $this->assertFalse(check_browser_version('Gecko', '4.0'));
401 $this->assertFalse(check_browser_version('Firefox'));
403 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['SeaMonkey']['2.1']['Linux'];
404 $this->assertTrue(check_browser_version('Gecko', '2'));
405 $this->assertTrue(check_browser_version('Gecko', '3.6'));
406 $this->assertTrue(check_browser_version('Gecko', '4.0'));
407 $this->assertTrue(check_browser_version('Gecko', 20030516));
408 $this->assertTrue(check_browser_version('Gecko', 20051106));
409 $this->assertTrue(check_browser_version('Gecko', 2006010100));
410 $this->assertTrue(check_browser_version('Firefox'));
411 $this->assertTrue(check_browser_version('Firefox', 4.0));
412 $this->assertFalse(check_browser_version('Firefox', 5));
413 $this->assertFalse(check_browser_version('Gecko', '18.0'));
417 function test_get_browser_version_classes() {
418 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
419 $this->assertEquals(array('safari'), get_browser_version_classes());
421 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
422 $this->assertEquals(array('safari'), get_browser_version_classes());
424 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
425 $this->assertEquals(array('safari', 'ios'), get_browser_version_classes());
427 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
428 $this->assertEquals(array('safari', 'android'), get_browser_version_classes());
430 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
431 $this->assertEquals(array('safari'), get_browser_version_classes());
433 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
434 $this->assertEquals(array('opera'), get_browser_version_classes());
436 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
437 $this->assertEquals(array('ie', 'ie6'), get_browser_version_classes());
439 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
440 $this->assertEquals(array('ie', 'ie7'), get_browser_version_classes());
442 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
443 $this->assertEquals(array('ie', 'ie8'), get_browser_version_classes());
445 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
446 $this->assertEquals(array('ie', 'ie9'), get_browser_version_classes());
448 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0i']['Windows 7'];
449 $this->assertEquals(array('ie', 'ie9'), get_browser_version_classes());
451 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0']['Windows 8'];
452 $this->assertEquals(array('ie', 'ie10'), get_browser_version_classes());
454 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0i']['Windows 8'];
455 $this->assertEquals(array('ie', 'ie10'), get_browser_version_classes());
457 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
458 $this->assertEquals(array('gecko', 'gecko18'), get_browser_version_classes());
460 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
461 $this->assertEquals(array('gecko', 'gecko19'), get_browser_version_classes());
464 function test_get_device_type() {
465 // IE8 (common pattern ~1.5% of IE7/8 users have embedded IE6 agent))
466 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BT Openworld BB; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; Hotbar 10.2.197.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)';
467 $this->assertEquals('default', get_device_type());
468 // Genuine IE6
469 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; AOL 9.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.0.3705; Media Center PC 2.8)';
470 $this->assertEquals('legacy', get_device_type());
473 function test_fix_utf8() {
474 // make sure valid data including other types is not changed
475 $this->assertSame(null, fix_utf8(null));
476 $this->assertSame(1, fix_utf8(1));
477 $this->assertSame(1.1, fix_utf8(1.1));
478 $this->assertSame(true, fix_utf8(true));
479 $this->assertSame('', fix_utf8(''));
480 $this->assertSame('abc', fix_utf8('abc'));
481 $array = array('do', 're', 'mi');
482 $this->assertSame($array, fix_utf8($array));
483 $object = new stdClass();
484 $object->a = 'aa';
485 $object->b = 'bb';
486 $this->assertEquals($object, fix_utf8($object));
488 // valid utf8 string
489 $this->assertSame("žlutý koníček přeskočil potůček \n\t\r\0", fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
491 // invalid utf8 string
492 $this->assertSame('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
495 function test_optional_param() {
496 global $CFG;
498 $_POST['username'] = 'post_user';
499 $_GET['username'] = 'get_user';
500 $this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
502 unset($_POST['username']);
503 $this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_GET['username']);
505 unset($_GET['username']);
506 $this->assertSame(optional_param('username', 'default_user', PARAM_RAW), 'default_user');
508 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
509 $_POST['username'] = 'post_user';
510 try {
511 optional_param('username', 'default_user', null);
512 $this->fail('coding_exception expected');
513 } catch (coding_exception $ex) {
514 $this->assertTrue(true);
516 try {
517 @optional_param('username', 'default_user');
518 $this->fail('coding_exception expected');
519 } catch (coding_exception $ex) {
520 $this->assertTrue(true);
522 try {
523 @optional_param('username');
524 $this->fail('coding_exception expected');
525 } catch (coding_exception $ex) {
526 $this->assertTrue(true);
528 try {
529 optional_param('', 'default_user', PARAM_RAW);
530 $this->fail('coding_exception expected');
531 } catch (coding_exception $ex) {
532 $this->assertTrue(true);
535 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
536 $_POST['username'] = array('a'=>'a');
537 $this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
538 $this->assertDebuggingCalled();
541 function test_optional_param_array() {
542 global $CFG;
544 $_POST['username'] = array('a'=>'post_user');
545 $_GET['username'] = array('a'=>'get_user');
546 $this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_POST['username']);
548 unset($_POST['username']);
549 $this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_GET['username']);
551 unset($_GET['username']);
552 $this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
554 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
555 $_POST['username'] = array('a'=>'post_user');
556 try {
557 optional_param_array('username', array('a'=>'default_user'), null);
558 $this->fail('coding_exception expected');
559 } catch (coding_exception $ex) {
560 $this->assertTrue(true);
562 try {
563 @optional_param_array('username', array('a'=>'default_user'));
564 $this->fail('coding_exception expected');
565 } catch (coding_exception $ex) {
566 $this->assertTrue(true);
568 try {
569 @optional_param_array('username');
570 $this->fail('coding_exception expected');
571 } catch (coding_exception $ex) {
572 $this->assertTrue(true);
574 try {
575 optional_param_array('', array('a'=>'default_user'), PARAM_RAW);
576 $this->fail('coding_exception expected');
577 } catch (coding_exception $ex) {
578 $this->assertTrue(true);
581 // do not allow nested arrays
582 try {
583 $_POST['username'] = array('a'=>array('b'=>'post_user'));
584 optional_param_array('username', array('a'=>'default_user'), PARAM_RAW);
585 $this->fail('coding_exception expected');
586 } catch (coding_exception $ex) {
587 $this->assertTrue(true);
590 // do not allow non-arrays
591 $_POST['username'] = 'post_user';
592 $this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
593 $this->assertDebuggingCalled();
595 // make sure array keys are sanitised
596 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
597 $this->assertSame(optional_param_array('username', array(), PARAM_RAW), array('a1_-'=>'post_user'));
598 $this->assertDebuggingCalled();
601 function test_required_param() {
602 global $CFG;
604 $_POST['username'] = 'post_user';
605 $_GET['username'] = 'get_user';
606 $this->assertSame(required_param('username', PARAM_RAW), 'post_user');
608 unset($_POST['username']);
609 $this->assertSame(required_param('username', PARAM_RAW), 'get_user');
611 unset($_GET['username']);
612 try {
613 $this->assertSame(required_param('username', PARAM_RAW), 'default_user');
614 $this->fail('moodle_exception expected');
615 } catch (moodle_exception $ex) {
616 $this->assertTrue(true);
619 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
620 $_POST['username'] = 'post_user';
621 try {
622 @required_param('username');
623 $this->fail('coding_exception expected');
624 } catch (coding_exception $ex) {
625 $this->assertTrue(true);
627 try {
628 required_param('username', '');
629 $this->fail('coding_exception expected');
630 } catch (coding_exception $ex) {
631 $this->assertTrue(true);
633 try {
634 required_param('', PARAM_RAW);
635 $this->fail('coding_exception expected');
636 } catch (coding_exception $ex) {
637 $this->assertTrue(true);
640 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
641 $_POST['username'] = array('a'=>'a');
642 $this->assertSame(required_param('username', PARAM_RAW), $_POST['username']);
643 $this->assertDebuggingCalled();
646 function test_required_param_array() {
647 global $CFG;
649 $_POST['username'] = array('a'=>'post_user');
650 $_GET['username'] = array('a'=>'get_user');
651 $this->assertSame(required_param_array('username', PARAM_RAW), $_POST['username']);
653 unset($_POST['username']);
654 $this->assertSame(required_param_array('username', PARAM_RAW), $_GET['username']);
656 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
657 $_POST['username'] = array('a'=>'post_user');
658 try {
659 required_param_array('username', null);
660 $this->fail('coding_exception expected');
661 } catch (coding_exception $ex) {
662 $this->assertTrue(true);
664 try {
665 @required_param_array('username');
666 $this->fail('coding_exception expected');
667 } catch (coding_exception $ex) {
668 $this->assertTrue(true);
670 try {
671 required_param_array('', PARAM_RAW);
672 $this->fail('coding_exception expected');
673 } catch (coding_exception $ex) {
674 $this->assertTrue(true);
677 // do not allow nested arrays
678 try {
679 $_POST['username'] = array('a'=>array('b'=>'post_user'));
680 required_param_array('username', PARAM_RAW);
681 $this->fail('coding_exception expected');
682 } catch (coding_exception $ex) {
683 $this->assertTrue(true);
686 // do not allow non-arrays
687 try {
688 $_POST['username'] = 'post_user';
689 required_param_array('username', PARAM_RAW);
690 $this->fail('moodle_exception expected');
691 } catch (moodle_exception $ex) {
692 $this->assertTrue(true);
695 // make sure array keys are sanitised
696 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
697 $this->assertSame(required_param_array('username', PARAM_RAW), array('a1_-'=>'post_user'));
698 $this->assertDebuggingCalled();
701 function test_clean_param() {
702 // forbid objects and arrays
703 try {
704 clean_param(array('x', 'y'), PARAM_RAW);
705 $this->fail('coding_exception expected');
706 } catch (coding_exception $ex) {
707 $this->assertTrue(true);
709 try {
710 $param = new stdClass();
711 $param->id = 1;
712 clean_param($param, PARAM_RAW);
713 $this->fail('coding_exception expected');
714 } catch (coding_exception $ex) {
715 $this->assertTrue(true);
718 // require correct type
719 try {
720 clean_param('x', 'xxxxxx');
721 $this->fail('moodle_exception expected');
722 } catch (moodle_exception $ex) {
723 $this->assertTrue(true);
725 try {
726 @clean_param('x');
727 $this->fail('moodle_exception expected');
728 } catch (moodle_exception $ex) {
729 $this->assertTrue(true);
734 function test_clean_param_array() {
735 $this->assertSame(clean_param_array(null, PARAM_RAW), array());
736 $this->assertSame(clean_param_array(array('a', 'b'), PARAM_RAW), array('a', 'b'));
737 $this->assertSame(clean_param_array(array('a', array('b')), PARAM_RAW, true), array('a', array('b')));
739 // require correct type
740 try {
741 clean_param_array(array('x'), 'xxxxxx');
742 $this->fail('moodle_exception expected');
743 } catch (moodle_exception $ex) {
744 $this->assertTrue(true);
746 try {
747 @clean_param_array(array('x'));
748 $this->fail('moodle_exception expected');
749 } catch (moodle_exception $ex) {
750 $this->assertTrue(true);
753 try {
754 clean_param_array(array('x', array('y')), PARAM_RAW);
755 $this->fail('coding_exception expected');
756 } catch (coding_exception $ex) {
757 $this->assertTrue(true);
760 // test recursive
763 function test_clean_param_raw() {
764 $this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
765 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
768 function test_clean_param_trim() {
769 $this->assertEquals(clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED), 'Frog toad');
772 function test_clean_param_clean() {
773 // PARAM_CLEAN is an ugly hack, do not use in new code (skodak)
774 // instead use more specific type, or submit sothing that can be verified properly
775 $this->assertEquals(clean_param('xx<script>', PARAM_CLEAN), 'xx');
778 function test_clean_param_alpha() {
779 $this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA),
780 'DSFMOSDJ');
783 function test_clean_param_alphanum() {
784 $this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM),
785 '978942897DSFMOSDJ');
788 function test_clean_param_alphaext() {
789 $this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT),
790 'DSFMOSDJ');
793 function test_clean_param_sequence() {
794 $this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE),
795 ',9789,42897');
798 function test_clean_param_component() {
799 // please note the cleaning of component names is very strict, no guessing here
800 $this->assertSame(clean_param('mod_forum', PARAM_COMPONENT), 'mod_forum');
801 $this->assertSame(clean_param('block_online_users', PARAM_COMPONENT), 'block_online_users');
802 $this->assertSame(clean_param('block_blond_online_users', PARAM_COMPONENT), 'block_blond_online_users');
803 $this->assertSame(clean_param('mod_something2', PARAM_COMPONENT), 'mod_something2');
804 $this->assertSame(clean_param('forum', PARAM_COMPONENT), 'forum');
805 $this->assertSame(clean_param('user', PARAM_COMPONENT), 'user');
806 $this->assertSame(clean_param('rating', PARAM_COMPONENT), 'rating');
807 $this->assertSame(clean_param('mod_2something', PARAM_COMPONENT), '');
808 $this->assertSame(clean_param('2mod_something', PARAM_COMPONENT), '');
809 $this->assertSame(clean_param('mod_something_xx', PARAM_COMPONENT), '');
810 $this->assertSame(clean_param('auth_something__xx', PARAM_COMPONENT), '');
811 $this->assertSame(clean_param('mod_Something', PARAM_COMPONENT), '');
812 $this->assertSame(clean_param('mod_somethíng', PARAM_COMPONENT), '');
813 $this->assertSame(clean_param('auth_xx-yy', PARAM_COMPONENT), '');
814 $this->assertSame(clean_param('_auth_xx', PARAM_COMPONENT), '');
815 $this->assertSame(clean_param('a2uth_xx', PARAM_COMPONENT), '');
816 $this->assertSame(clean_param('auth_xx_', PARAM_COMPONENT), '');
817 $this->assertSame(clean_param('auth_xx.old', PARAM_COMPONENT), '');
818 $this->assertSame(clean_param('_user', PARAM_COMPONENT), '');
819 $this->assertSame(clean_param('2rating', PARAM_COMPONENT), '');
820 $this->assertSame(clean_param('user_', PARAM_COMPONENT), '');
823 function test_is_valid_plugin_name() {
824 $this->assertTrue(is_valid_plugin_name('forum'));
825 $this->assertTrue(is_valid_plugin_name('forum2'));
826 $this->assertTrue(is_valid_plugin_name('online_users'));
827 $this->assertTrue(is_valid_plugin_name('blond_online_users'));
828 $this->assertFalse(is_valid_plugin_name('online__users'));
829 $this->assertFalse(is_valid_plugin_name('forum '));
830 $this->assertFalse(is_valid_plugin_name('forum.old'));
831 $this->assertFalse(is_valid_plugin_name('xx-yy'));
832 $this->assertFalse(is_valid_plugin_name('2xx'));
833 $this->assertFalse(is_valid_plugin_name('Xx'));
834 $this->assertFalse(is_valid_plugin_name('_xx'));
835 $this->assertFalse(is_valid_plugin_name('xx_'));
838 function test_clean_param_plugin() {
839 // please note the cleaning of plugin names is very strict, no guessing here
840 $this->assertSame(clean_param('forum', PARAM_PLUGIN), 'forum');
841 $this->assertSame(clean_param('forum2', PARAM_PLUGIN), 'forum2');
842 $this->assertSame(clean_param('online_users', PARAM_PLUGIN), 'online_users');
843 $this->assertSame(clean_param('blond_online_users', PARAM_PLUGIN), 'blond_online_users');
844 $this->assertSame(clean_param('online__users', PARAM_PLUGIN), '');
845 $this->assertSame(clean_param('forum ', PARAM_PLUGIN), '');
846 $this->assertSame(clean_param('forum.old', PARAM_PLUGIN), '');
847 $this->assertSame(clean_param('xx-yy', PARAM_PLUGIN), '');
848 $this->assertSame(clean_param('2xx', PARAM_PLUGIN), '');
849 $this->assertSame(clean_param('Xx', PARAM_PLUGIN), '');
850 $this->assertSame(clean_param('_xx', PARAM_PLUGIN), '');
851 $this->assertSame(clean_param('xx_', PARAM_PLUGIN), '');
854 function test_clean_param_area() {
855 // please note the cleaning of area names is very strict, no guessing here
856 $this->assertSame(clean_param('something', PARAM_AREA), 'something');
857 $this->assertSame(clean_param('something2', PARAM_AREA), 'something2');
858 $this->assertSame(clean_param('some_thing', PARAM_AREA), 'some_thing');
859 $this->assertSame(clean_param('some_thing_xx', PARAM_AREA), 'some_thing_xx');
860 $this->assertSame(clean_param('_something', PARAM_AREA), '');
861 $this->assertSame(clean_param('something_', PARAM_AREA), '');
862 $this->assertSame(clean_param('2something', PARAM_AREA), '');
863 $this->assertSame(clean_param('Something', PARAM_AREA), '');
864 $this->assertSame(clean_param('some-thing', PARAM_AREA), '');
865 $this->assertSame(clean_param('somethííng', PARAM_AREA), '');
866 $this->assertSame(clean_param('something.x', PARAM_AREA), '');
869 function test_clean_param_text() {
870 $this->assertEquals(PARAM_TEXT, PARAM_MULTILANG);
871 //standard
872 $this->assertEquals(clean_param('xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>');
873 $this->assertEquals(clean_param('<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>');
874 $this->assertEquals(clean_param('xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>');
875 //malformed
876 $this->assertEquals(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span>');
877 $this->assertEquals(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT), 'aa');
878 $this->assertEquals(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT), 'aa');
879 $this->assertEquals(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT), 'aa');
880 $this->assertEquals(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT), 'aa');
881 $this->assertEquals(clean_param('a<em>b</em>c', PARAM_TEXT), 'abc');
882 $this->assertEquals(clean_param('a><xx >c>', PARAM_TEXT), 'a>c>'); // standard strip_tags() behaviour
883 $this->assertEquals(clean_param('a<b', PARAM_TEXT), 'a');
884 $this->assertEquals(clean_param('a>b', PARAM_TEXT), 'a>b');
885 $this->assertEquals(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour
886 $this->assertEquals(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT), 'a');
887 $this->assertEquals(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT), '<lang lang="en">aa</lang>');
890 function test_clean_param_url() {
891 // Test PARAM_URL and PARAM_LOCALURL a bit
892 $this->assertEquals(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
893 $this->assertEquals(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL), 'http://some.very.long.and.silly.domain/with/a/path/');
894 $this->assertEquals(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
895 $this->assertEquals(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
896 $this->assertEquals(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
897 $this->assertEquals(clean_param('funny:thing', PARAM_URL), '');
900 function test_clean_param_localurl() {
901 global $CFG;
902 $this->assertEquals(clean_param('http://google.com/', PARAM_LOCALURL), '');
903 $this->assertEquals(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
904 $this->assertEquals(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
905 $this->assertEquals(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
906 $this->assertEquals(clean_param('funny:thing', PARAM_LOCALURL), '');
907 $this->assertEquals(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
910 function test_clean_param_file() {
911 $this->assertEquals(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt');
912 $this->assertEquals(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt');
913 $this->assertEquals(clean_param('../parentdirfile.txt', PARAM_FILE), '..parentdirfile.txt');
914 $this->assertEquals(clean_param('../../grandparentdirfile.txt', PARAM_FILE), '....grandparentdirfile.txt');
915 $this->assertEquals(clean_param('..\winparentdirfile.txt', PARAM_FILE), '..winparentdirfile.txt');
916 $this->assertEquals(clean_param('..\..\wingrandparentdir.txt', PARAM_FILE), '....wingrandparentdir.txt');
917 $this->assertEquals(clean_param('myfile.a.b.txt', PARAM_FILE), 'myfile.a.b.txt');
918 $this->assertEquals(clean_param('myfile..a..b.txt', PARAM_FILE), 'myfile..a..b.txt');
919 $this->assertEquals(clean_param('myfile.a..b...txt', PARAM_FILE), 'myfile.a..b...txt');
920 $this->assertEquals(clean_param('myfile.a.txt', PARAM_FILE), 'myfile.a.txt');
921 $this->assertEquals(clean_param('myfile...txt', PARAM_FILE), 'myfile...txt');
922 $this->assertEquals(clean_param('...jpg', PARAM_FILE), '...jpg');
923 $this->assertEquals(clean_param('.a.b.', PARAM_FILE), '.a.b.');
924 $this->assertEquals(clean_param('.', PARAM_FILE), '');
925 $this->assertEquals(clean_param('..', PARAM_FILE), '');
926 $this->assertEquals(clean_param('...', PARAM_FILE), '...');
927 $this->assertEquals(clean_param('. . . .', PARAM_FILE), '. . . .');
928 $this->assertEquals(clean_param('dontrtrim.me. .. .. . ', PARAM_FILE), 'dontrtrim.me. .. .. . ');
929 $this->assertEquals(clean_param(' . .dontltrim.me', PARAM_FILE), ' . .dontltrim.me');
930 $this->assertEquals(clean_param("here is a tab\t.txt", PARAM_FILE), 'here is a tab.txt');
931 $this->assertEquals(clean_param("here is a line\r\nbreak.txt", PARAM_FILE), 'here is a linebreak.txt');
933 //The following behaviours have been maintained although they seem a little odd
934 $this->assertEquals(clean_param('funny:thing', PARAM_FILE), 'funnything');
935 $this->assertEquals(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt');
936 $this->assertEquals(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt');
937 $this->assertEquals(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt');
938 $this->assertEquals(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt');
941 function test_clean_param_path() {
942 $this->assertEquals(clean_param('correctfile.txt', PARAM_PATH), 'correctfile.txt');
943 $this->assertEquals(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_PATH), 'bad/file.txt');
944 $this->assertEquals(clean_param('../parentdirfile.txt', PARAM_PATH), '/parentdirfile.txt');
945 $this->assertEquals(clean_param('../../grandparentdirfile.txt', PARAM_PATH), '/grandparentdirfile.txt');
946 $this->assertEquals(clean_param('..\winparentdirfile.txt', PARAM_PATH), '/winparentdirfile.txt');
947 $this->assertEquals(clean_param('..\..\wingrandparentdir.txt', PARAM_PATH), '/wingrandparentdir.txt');
948 $this->assertEquals(clean_param('funny:thing', PARAM_PATH), 'funnything');
949 $this->assertEquals(clean_param('./././here', PARAM_PATH), './here');
950 $this->assertEquals(clean_param('./currentdirfile.txt', PARAM_PATH), './currentdirfile.txt');
951 $this->assertEquals(clean_param('c:\temp\windowsfile.txt', PARAM_PATH), 'c/temp/windowsfile.txt');
952 $this->assertEquals(clean_param('/home/user/linuxfile.txt', PARAM_PATH), '/home/user/linuxfile.txt');
953 $this->assertEquals(clean_param('/home../user ./.linuxfile.txt', PARAM_PATH), '/home../user ./.linuxfile.txt');
954 $this->assertEquals(clean_param('~/myfile.txt', PARAM_PATH), '~/myfile.txt');
955 $this->assertEquals(clean_param('~/../myfile.txt', PARAM_PATH), '~/myfile.txt');
956 $this->assertEquals(clean_param('/..b../.../myfile.txt', PARAM_PATH), '/..b../.../myfile.txt');
957 $this->assertEquals(clean_param('..b../.../myfile.txt', PARAM_PATH), '..b../.../myfile.txt');
958 $this->assertEquals(clean_param('/super//slashes///', PARAM_PATH), '/super/slashes/');
961 function test_clean_param_username() {
962 global $CFG;
963 $currentstatus = $CFG->extendedusernamechars;
965 // Run tests with extended character == FALSE;
966 $CFG->extendedusernamechars = FALSE;
967 $this->assertEquals(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
968 $this->assertEquals(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
969 $this->assertEquals(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
970 $this->assertEquals(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
971 $this->assertEquals(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
972 $this->assertEquals(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
973 $this->assertEquals(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
974 $this->assertEquals(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
975 $this->assertEquals(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
976 $this->assertEquals(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
977 $this->assertEquals(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýáž?žý??šdoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
980 // Test success condition, if extendedusernamechars == ENABLE;
981 $CFG->extendedusernamechars = TRUE;
982 $this->assertEquals(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
983 $this->assertEquals(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
984 $this->assertEquals(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
985 $this->assertEquals(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
986 $this->assertEquals(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
987 $this->assertEquals(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
988 $this->assertEquals(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
990 $CFG->extendedusernamechars = $currentstatus;
993 function test_clean_param_stringid() {
994 // Test string identifiers validation
995 // valid strings:
996 $this->assertEquals(clean_param('validstring', PARAM_STRINGID), 'validstring');
997 $this->assertEquals(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability');
998 $this->assertEquals(clean_param('CZ', PARAM_STRINGID), 'CZ');
999 $this->assertEquals(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint');
1000 $this->assertEquals(clean_param('grade2', PARAM_STRINGID), 'grade2');
1001 // invalid strings:
1002 $this->assertEquals(clean_param('trailing ', PARAM_STRINGID), '');
1003 $this->assertEquals(clean_param('space bar', PARAM_STRINGID), '');
1004 $this->assertEquals(clean_param('0numeric', PARAM_STRINGID), '');
1005 $this->assertEquals(clean_param('*', PARAM_STRINGID), '');
1006 $this->assertEquals(clean_param(' ', PARAM_STRINGID), '');
1009 function test_clean_param_timezone() {
1010 // Test timezone validation
1011 $testvalues = array (
1012 'America/Jamaica' => 'America/Jamaica',
1013 'America/Argentina/Cordoba' => 'America/Argentina/Cordoba',
1014 'America/Port-au-Prince' => 'America/Port-au-Prince',
1015 'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires',
1016 'PST8PDT' => 'PST8PDT',
1017 'Wrong.Value' => '',
1018 'Wrong/.Value' => '',
1019 'Wrong(Value)' => '',
1020 '0' => '0',
1021 '0.0' => '0.0',
1022 '0.5' => '0.5',
1023 '9.0' => '9.0',
1024 '-9.0' => '-9.0',
1025 '+9.0' => '+9.0',
1026 '9.5' => '9.5',
1027 '-9.5' => '-9.5',
1028 '+9.5' => '+9.5',
1029 '12.0' => '12.0',
1030 '-12.0' => '-12.0',
1031 '+12.0' => '+12.0',
1032 '12.5' => '12.5',
1033 '-12.5' => '-12.5',
1034 '+12.5' => '+12.5',
1035 '13.0' => '13.0',
1036 '-13.0' => '-13.0',
1037 '+13.0' => '+13.0',
1038 '13.5' => '',
1039 '+13.5' => '',
1040 '-13.5' => '',
1041 '0.2' => '');
1043 foreach ($testvalues as $testvalue => $expectedvalue) {
1044 $actualvalue = clean_param($testvalue, PARAM_TIMEZONE);
1045 $this->assertEquals($actualvalue, $expectedvalue);
1049 function test_validate_param() {
1050 try {
1051 $param = validate_param('11a', PARAM_INT);
1052 $this->fail('invalid_parameter_exception expected');
1053 } catch (invalid_parameter_exception $ex) {
1054 $this->assertTrue(true);
1056 try {
1057 $param = validate_param('11', PARAM_INT);
1058 $this->assertEquals($param, 11);
1059 } catch (invalid_parameter_exception $ex) {
1060 $this->fail('invalid_parameter_exception not expected');
1062 try {
1063 $param = validate_param(null, PARAM_INT, false);
1064 $this->fail('invalid_parameter_exception expected');
1065 } catch (invalid_parameter_exception $ex) {
1066 $this->assertTrue(true);
1068 try {
1069 $param = validate_param(null, PARAM_INT, true);
1070 $this->assertTrue($param===null);
1071 } catch (invalid_parameter_exception $ex) {
1072 $this->fail('invalid_parameter_exception expected');
1074 try {
1075 $param = validate_param(array(), PARAM_INT);
1076 $this->fail('invalid_parameter_exception expected');
1077 } catch (invalid_parameter_exception $ex) {
1078 $this->assertTrue(true);
1080 try {
1081 $param = validate_param(new stdClass, PARAM_INT);
1082 $this->fail('invalid_parameter_exception expected');
1083 } catch (invalid_parameter_exception $ex) {
1084 $this->assertTrue(true);
1086 try {
1087 $param = validate_param('1.0', PARAM_FLOAT);
1088 $this->assertSame(1.0, $param);
1090 // Make sure valid floats do not cause exception.
1091 validate_param(1.0, PARAM_FLOAT);
1092 validate_param(10, PARAM_FLOAT);
1093 validate_param('0', PARAM_FLOAT);
1094 validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
1095 validate_param('011.1', PARAM_FLOAT);
1096 validate_param('11', PARAM_FLOAT);
1097 validate_param('+.1', PARAM_FLOAT);
1098 validate_param('-.1', PARAM_FLOAT);
1099 validate_param('1e10', PARAM_FLOAT);
1100 validate_param('.1e+10', PARAM_FLOAT);
1101 validate_param('1E-1', PARAM_FLOAT);
1102 $this->assertTrue(true);
1103 } catch (invalid_parameter_exception $ex) {
1104 $this->fail('Valid float notation not accepted');
1106 try {
1107 $param = validate_param('1,2', PARAM_FLOAT);
1108 $this->fail('invalid_parameter_exception expected');
1109 } catch (invalid_parameter_exception $ex) {
1110 $this->assertTrue(true);
1112 try {
1113 $param = validate_param('', PARAM_FLOAT);
1114 $this->fail('invalid_parameter_exception expected');
1115 } catch (invalid_parameter_exception $ex) {
1116 $this->assertTrue(true);
1118 try {
1119 $param = validate_param('.', PARAM_FLOAT);
1120 $this->fail('invalid_parameter_exception expected');
1121 } catch (invalid_parameter_exception $ex) {
1122 $this->assertTrue(true);
1124 try {
1125 $param = validate_param('e10', PARAM_FLOAT);
1126 $this->fail('invalid_parameter_exception expected');
1127 } catch (invalid_parameter_exception $ex) {
1128 $this->assertTrue(true);
1130 try {
1131 $param = validate_param('abc', PARAM_FLOAT);
1132 $this->fail('invalid_parameter_exception expected');
1133 } catch (invalid_parameter_exception $ex) {
1134 $this->assertTrue(true);
1138 function test_shorten_text_no_tags_already_short_enough() {
1139 // ......12345678901234567890123456.
1140 $text = "short text already no tags";
1141 $this->assertEquals($text, shorten_text($text));
1144 function test_shorten_text_with_tags_already_short_enough() {
1145 // .........123456...7890....12345678.......901234567.
1146 $text = "<p>short <b>text</b> already</p><p>with tags</p>";
1147 $this->assertEquals($text, shorten_text($text));
1150 function test_shorten_text_no_tags_needs_shortening() {
1151 // Default truncation is after 30 chars, but allowing 3 for the final '...'.
1152 // ......12345678901234567890123456789023456789012345678901234.
1153 $text = "long text without any tags blah de blah blah blah what";
1154 $this->assertEquals('long text without any tags ...', shorten_text($text));
1157 function test_shorten_text_with_tags_needs_shortening() {
1158 // .......................................123456789012345678901234567890...
1159 $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
1160 "be chopped off but <b>should be added back again</b></blockquote></p></div>";
1161 $this->assertEquals("<div class='frog'><p><blockquote>Long text with " .
1162 "tags that ...</blockquote></p></div>", shorten_text($text));
1165 function test_shorten_text_with_entities() {
1166 // Remember to allow 3 chars for the final '...'.
1167 // ......123456789012345678901234567_____890...
1168 $text = "some text which shouldn't &nbsp; break there";
1169 $this->assertEquals("some text which shouldn't &nbsp; ...",
1170 shorten_text($text, 31));
1171 $this->assertEquals("some text which shouldn't &nbsp;...",
1172 shorten_text($text, 30));
1173 $this->assertEquals("some text which shouldn't ...",
1174 shorten_text($text, 29));
1177 function test_shorten_text_known_tricky_case() {
1178 // This case caused a bug up to 1.9.5
1179 // ..........123456789012345678901234567890123456789.....0_____1___2___...
1180 $text = "<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;&lt;&lt;There are several";
1181 $this->assertEquals("<h3>standard 'break-out' sub groups in ...</h3>",
1182 shorten_text($text, 41));
1183 $this->assertEquals("<h3>standard 'break-out' sub groups in TGs?...</h3>",
1184 shorten_text($text, 42));
1185 $this->assertEquals("<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;...",
1186 shorten_text($text, 43));
1189 function test_shorten_text_no_spaces() {
1190 // ..........123456789.
1191 $text = "<h1>123456789</h1>"; // A string with no convenient breaks.
1192 $this->assertEquals("<h1>12345...</h1>",
1193 shorten_text($text, 8));
1196 function test_shorten_text_utf8_european() {
1197 // Text without tags.
1198 // ......123456789012345678901234567.
1199 $text = "Žluťoučký koníček přeskočil";
1200 $this->assertEquals($text, shorten_text($text)); // 30 chars by default.
1201 $this->assertEquals("Žluťoučký koníče...", shorten_text($text, 19, true));
1202 $this->assertEquals("Žluťoučký ...", shorten_text($text, 19, false));
1203 // And try it with 2-less (that are, in bytes, the middle of a sequence).
1204 $this->assertEquals("Žluťoučký koní...", shorten_text($text, 17, true));
1205 $this->assertEquals("Žluťoučký ...", shorten_text($text, 17, false));
1207 // .........123456789012345678...901234567....89012345.
1208 $text = "<p>Žluťoučký koníček <b>přeskočil</b> potůček</p>";
1209 $this->assertEquals($text, shorten_text($text, 60));
1210 $this->assertEquals("<p>Žluťoučký koníček ...</p>", shorten_text($text, 21));
1211 $this->assertEquals("<p>Žluťoučký koníče...</p>", shorten_text($text, 19, true));
1212 $this->assertEquals("<p>Žluťoučký ...</p>", shorten_text($text, 19, false));
1213 // And try it with 2 fewer (that are, in bytes, the middle of a sequence).
1214 $this->assertEquals("<p>Žluťoučký koní...</p>", shorten_text($text, 17, true));
1215 $this->assertEquals("<p>Žluťoučký ...</p>", shorten_text($text, 17, false));
1216 // And try over one tag (start/end), it does proper text len.
1217 $this->assertEquals("<p>Žluťoučký koníček <b>př...</b></p>", shorten_text($text, 23, true));
1218 $this->assertEquals("<p>Žluťoučký koníček <b>přeskočil</b> pot...</p>", shorten_text($text, 34, true));
1219 // And in the middle of one tag.
1220 $this->assertEquals("<p>Žluťoučký koníček <b>přeskočil...</b></p>", shorten_text($text, 30, true));
1223 function test_shorten_text_utf8_oriental() {
1224 // Japanese
1225 // text without tags
1226 // ......123456789012345678901234.
1227 $text = '言語設定言語設定abcdefghijkl';
1228 $this->assertEquals($text, shorten_text($text)); // 30 chars by default
1229 $this->assertEquals("言語設定言語...", shorten_text($text, 9, true));
1230 $this->assertEquals("言語設定言語...", shorten_text($text, 9, false));
1231 $this->assertEquals("言語設定言語設定ab...", shorten_text($text, 13, true));
1232 $this->assertEquals("言語設定言語設定...", shorten_text($text, 13, false));
1234 // Chinese
1235 // text without tags
1236 // ......123456789012345678901234.
1237 $text = '简体中文简体中文abcdefghijkl';
1238 $this->assertEquals($text, shorten_text($text)); // 30 chars by default
1239 $this->assertEquals("简体中文简体...", shorten_text($text, 9, true));
1240 $this->assertEquals("简体中文简体...", shorten_text($text, 9, false));
1241 $this->assertEquals("简体中文简体中文ab...", shorten_text($text, 13, true));
1242 $this->assertEquals("简体中文简体中文...", shorten_text($text, 13, false));
1245 function test_shorten_text_multilang() {
1246 // This is not necessaryily specific to multilang. The issue is really
1247 // tags with attributes, where before we were generating invalid HTML
1248 // output like shorten_text('<span id="x" class="y">A</span> B', 1);
1249 // returning '<span id="x" ...</span>'. It is just that multilang
1250 // requires the sort of HTML that is quite likely to trigger this.
1251 // ........................................1...
1252 $text = '<span lang="en" class="multilang">A</span>' .
1253 '<span lang="fr" class="multilang">B</span>';
1254 $this->assertEquals('<span lang="en" class="multilang">...</span>',
1255 shorten_text($text, 1));
1258 function test_usergetdate() {
1259 global $USER, $CFG, $DB;
1261 //Check if forcetimezone is set then save it and set it to use user timezone
1262 $cfgforcetimezone = null;
1263 if (isset($CFG->forcetimezone)) {
1264 $cfgforcetimezone = $CFG->forcetimezone;
1265 $CFG->forcetimezone = 99; //get user default timezone.
1268 $olduser = $USER;
1269 $USER = $DB->get_record('user', array('id'=>2)); //admin
1271 $userstimezone = $USER->timezone;
1272 $USER->timezone = 2;//set the timezone to a known state
1274 // The string version of date comes from server locale setting and does
1275 // not respect user language, so it is necessary to reset that.
1276 $oldlocale = setlocale(LC_TIME, '0');
1277 setlocale(LC_TIME, 'en_AU.UTF-8');
1279 $ts = 1261540267; //the time this function was created
1281 $arr = usergetdate($ts,1);//specify the timezone as an argument
1282 $arr = array_values($arr);
1284 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1285 $this->assertSame($seconds, 7);
1286 $this->assertSame($minutes, 51);
1287 $this->assertSame($hours, 4);
1288 $this->assertSame($mday, 23);
1289 $this->assertSame($wday, 3);
1290 $this->assertSame($mon, 12);
1291 $this->assertSame($year, 2009);
1292 $this->assertSame($yday, 356);
1293 $this->assertSame($weekday, 'Wednesday');
1294 $this->assertSame($month, 'December');
1295 $arr = usergetdate($ts);//gets the timezone from the $USER object
1296 $arr = array_values($arr);
1298 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1299 $this->assertSame($seconds, 7);
1300 $this->assertSame($minutes, 51);
1301 $this->assertSame($hours, 5);
1302 $this->assertSame($mday, 23);
1303 $this->assertSame($wday, 3);
1304 $this->assertSame($mon, 12);
1305 $this->assertSame($year, 2009);
1306 $this->assertSame($yday, 356);
1307 $this->assertSame($weekday, 'Wednesday');
1308 $this->assertSame($month, 'December');
1309 //set the timezone back to what it was
1310 $USER->timezone = $userstimezone;
1312 //restore forcetimezone if changed.
1313 if (!is_null($cfgforcetimezone)) {
1314 $CFG->forcetimezone = $cfgforcetimezone;
1317 setlocale(LC_TIME, $oldlocale);
1319 $USER = $olduser;
1322 public function test_normalize_component() {
1324 // moodle core
1325 $this->assertEquals(normalize_component('moodle'), array('core', null));
1326 $this->assertEquals(normalize_component('core'), array('core', null));
1328 // moodle core subsystems
1329 $this->assertEquals(normalize_component('admin'), array('core', 'admin'));
1330 $this->assertEquals(normalize_component('core_admin'), array('core', 'admin'));
1332 // activity modules and their subplugins
1333 $this->assertEquals(normalize_component('workshop'), array('mod', 'workshop'));
1334 $this->assertEquals(normalize_component('mod_workshop'), array('mod', 'workshop'));
1335 $this->assertEquals(normalize_component('workshopform_accumulative'), array('workshopform', 'accumulative'));
1336 $this->assertEquals(normalize_component('quiz'), array('mod', 'quiz'));
1337 $this->assertEquals(normalize_component('quiz_grading'), array('quiz', 'grading'));
1338 $this->assertEquals(normalize_component('data'), array('mod', 'data'));
1339 $this->assertEquals(normalize_component('datafield_checkbox'), array('datafield', 'checkbox'));
1341 // other plugin types
1342 $this->assertEquals(normalize_component('auth_mnet'), array('auth', 'mnet'));
1343 $this->assertEquals(normalize_component('enrol_self'), array('enrol', 'self'));
1344 $this->assertEquals(normalize_component('block_html'), array('block', 'html'));
1345 $this->assertEquals(normalize_component('block_mnet_hosts'), array('block', 'mnet_hosts'));
1346 $this->assertEquals(normalize_component('local_amos'), array('local', 'amos'));
1348 // unknown components are supposed to be activity modules
1349 $this->assertEquals(normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'),
1350 array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'));
1351 $this->assertEquals(normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'),
1352 array('mod', 'whothefuck_wouldcomewithsuchastupidnameofcomponent'));
1353 $this->assertEquals(normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'),
1354 array('mod', 'whothefuck_would_come_withsuchastupidnameofcomponent'));
1357 protected function get_fake_preference_test_userid() {
1358 global $DB;
1360 // we need some nonexistent user id
1361 $id = 2147483647 - 666;
1362 if ($DB->get_records('user', array('id'=>$id))) {
1363 //weird!
1364 return false;
1366 return $id;
1369 public function test_mark_user_preferences_changed() {
1370 $this->resetAfterTest(true);
1371 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1372 $this->fail('Can not find unused user id for the preferences test');
1373 return;
1376 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1377 mark_user_preferences_changed($otheruserid);
1379 $this->assertEquals(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1);
1380 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1383 public function test_check_user_preferences_loaded() {
1384 global $DB;
1385 $this->resetAfterTest(true);
1387 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1388 $this->fail('Can not find unused user id for the preferences test');
1389 return;
1392 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1393 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1395 $user = new stdClass();
1396 $user->id = $otheruserid;
1398 // load
1399 check_user_preferences_loaded($user);
1400 $this->assertTrue(isset($user->preference));
1401 $this->assertTrue(is_array($user->preference));
1402 $this->assertTrue(isset($user->preference['_lastloaded']));
1403 $this->assertEquals(count($user->preference), 1);
1405 // add preference via direct call
1406 $DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id));
1408 // no cache reload yet
1409 check_user_preferences_loaded($user);
1410 $this->assertEquals(count($user->preference), 1);
1412 // forced reloading of cache
1413 unset($user->preference);
1414 check_user_preferences_loaded($user);
1415 $this->assertEquals(count($user->preference), 2);
1416 $this->assertEquals($user->preference['xxx'], 'yyy');
1418 // add preference via direct call
1419 $DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id));
1421 // test timeouts and modifications from different session
1422 set_cache_flag('userpreferenceschanged', $user->id, 1, time() + 1000);
1423 $user->preference['_lastloaded'] = $user->preference['_lastloaded'] - 20;
1424 check_user_preferences_loaded($user);
1425 $this->assertEquals(count($user->preference), 2);
1426 check_user_preferences_loaded($user, 10);
1427 $this->assertEquals(count($user->preference), 3);
1428 $this->assertEquals($user->preference['aaa'], 'bbb');
1429 set_cache_flag('userpreferenceschanged', $user->id, null);
1432 public function test_set_user_preference() {
1433 global $DB, $USER;
1434 $this->resetAfterTest(true);
1436 $olduser = $USER;
1437 $USER = $DB->get_record('user', array('id'=>2)); //admin
1439 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1440 $this->fail('Can not find unused user id for the preferences test');
1441 return;
1444 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1445 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1447 $user = new stdClass();
1448 $user->id = $otheruserid;
1450 set_user_preference('aaa', 'bbb', $otheruserid);
1451 $this->assertEquals('bbb', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'aaa')));
1452 $this->assertEquals('bbb', get_user_preferences('aaa', null, $otheruserid));
1454 set_user_preference('xxx', 'yyy', $user);
1455 $this->assertEquals('yyy', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1456 $this->assertEquals('yyy', get_user_preferences('xxx', null, $otheruserid));
1457 $this->assertTrue(is_array($user->preference));
1458 $this->assertEquals($user->preference['aaa'], 'bbb');
1459 $this->assertEquals($user->preference['xxx'], 'yyy');
1461 set_user_preference('xxx', NULL, $user);
1462 $this->assertSame(false, $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1463 $this->assertSame(null, get_user_preferences('xxx', null, $otheruserid));
1465 set_user_preference('ooo', true, $user);
1466 $prefs = get_user_preferences(null, null, $otheruserid);
1467 $this->assertSame($prefs['aaa'], $user->preference['aaa']);
1468 $this->assertSame($prefs['ooo'], $user->preference['ooo']);
1469 $this->assertSame($prefs['ooo'], '1');
1471 set_user_preference('null', 0, $user);
1472 $this->assertSame('0', get_user_preferences('null', null, $otheruserid));
1474 $this->assertSame('lala', get_user_preferences('undefined', 'lala', $otheruserid));
1476 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1477 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1479 // test $USER default
1480 set_user_preference('_test_user_preferences_pref', 'ok');
1481 $this->assertSame('ok', $USER->preference['_test_user_preferences_pref']);
1482 unset_user_preference('_test_user_preferences_pref');
1483 $this->assertTrue(!isset($USER->preference['_test_user_preferences_pref']));
1485 // Test 1333 char values (no need for unicode, there are already tests for that in DB tests)
1486 $longvalue = str_repeat('a', 1333);
1487 set_user_preference('_test_long_user_preference', $longvalue);
1488 $this->assertEquals($longvalue, get_user_preferences('_test_long_user_preference'));
1489 $this->assertEquals($longvalue,
1490 $DB->get_field('user_preferences', 'value', array('userid' => $USER->id, 'name' => '_test_long_user_preference')));
1492 // Test > 1333 char values, coding_exception expected
1493 $longvalue = str_repeat('a', 1334);
1494 try {
1495 set_user_preference('_test_long_user_preference', $longvalue);
1496 $this->fail('Exception expected - longer than 1333 chars not allowed as preference value');
1497 } catch (coding_exception $ex) {
1498 $this->assertTrue(true);
1501 //test invalid params
1502 try {
1503 set_user_preference('_test_user_preferences_pref', array());
1504 $this->fail('Exception expected - array not valid preference value');
1505 } catch (coding_exception $ex) {
1506 $this->assertTrue(true);
1508 try {
1509 set_user_preference('_test_user_preferences_pref', new stdClass);
1510 $this->fail('Exception expected - class not valid preference value');
1511 } catch (coding_exception $ex) {
1512 $this->assertTrue(true);
1514 try {
1515 set_user_preference('_test_user_preferences_pref', 1, array('xx' => 1));
1516 $this->fail('Exception expected - user instance expected');
1517 } catch (coding_exception $ex) {
1518 $this->assertTrue(true);
1520 try {
1521 set_user_preference('_test_user_preferences_pref', 1, 'abc');
1522 $this->fail('Exception expected - user instance expected');
1523 } catch (coding_exception $ex) {
1524 $this->assertTrue(true);
1526 try {
1527 set_user_preference('', 1);
1528 $this->fail('Exception expected - invalid name accepted');
1529 } catch (coding_exception $ex) {
1530 $this->assertTrue(true);
1532 try {
1533 set_user_preference('1', 1);
1534 $this->fail('Exception expected - invalid name accepted');
1535 } catch (coding_exception $ex) {
1536 $this->assertTrue(true);
1539 $USER = $olduser;
1542 public function test_get_extra_user_fields() {
1543 global $CFG, $USER, $DB;
1544 $oldshowuseridentity = $CFG->showuseridentity;
1546 $olduser = $USER;
1547 $USER = $DB->get_record('user', array('id'=>2)); //admin
1549 // It would be really nice if there were a way to 'mock' has_capability
1550 // checks (either to return true or false) but as there is not, this
1551 // test doesn't test the capability check. Presumably, anyone running
1552 // unit tests will have the capability.
1553 $context = context_system::instance();
1555 // No fields
1556 $CFG->showuseridentity = '';
1557 $this->assertEquals(array(), get_extra_user_fields($context));
1559 // One field
1560 $CFG->showuseridentity = 'frog';
1561 $this->assertEquals(array('frog'), get_extra_user_fields($context));
1563 // Two fields
1564 $CFG->showuseridentity = 'frog,zombie';
1565 $this->assertEquals(array('frog', 'zombie'), get_extra_user_fields($context));
1567 // No fields, except
1568 $CFG->showuseridentity = '';
1569 $this->assertEquals(array(), get_extra_user_fields($context, array('frog')));
1571 // One field
1572 $CFG->showuseridentity = 'frog';
1573 $this->assertEquals(array(), get_extra_user_fields($context, array('frog')));
1575 // Two fields
1576 $CFG->showuseridentity = 'frog,zombie';
1577 $this->assertEquals(array('zombie'), get_extra_user_fields($context, array('frog')));
1579 // As long as this test passes, the value will be set back. This is only
1580 // in-memory anyhow
1581 $CFG->showuseridentity = $oldshowuseridentity;
1583 $USER = $olduser;
1586 public function test_get_extra_user_fields_sql() {
1587 global $CFG, $USER, $DB;
1589 $olduser = $USER;
1590 $USER = $DB->get_record('user', array('id'=>2)); //admin
1592 $oldshowuseridentity = $CFG->showuseridentity;
1593 $context = context_system::instance();
1595 // No fields
1596 $CFG->showuseridentity = '';
1597 $this->assertEquals('', get_extra_user_fields_sql($context));
1599 // One field
1600 $CFG->showuseridentity = 'frog';
1601 $this->assertEquals(', frog', get_extra_user_fields_sql($context));
1603 // Two fields with table prefix
1604 $CFG->showuseridentity = 'frog,zombie';
1605 $this->assertEquals(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1'));
1607 // Two fields with field prefix
1608 $CFG->showuseridentity = 'frog,zombie';
1609 $this->assertEquals(', frog AS u_frog, zombie AS u_zombie',
1610 get_extra_user_fields_sql($context, '', 'u_'));
1612 // One field excluded
1613 $CFG->showuseridentity = 'frog';
1614 $this->assertEquals('', get_extra_user_fields_sql($context, '', '', array('frog')));
1616 // Two fields, one excluded, table+field prefix
1617 $CFG->showuseridentity = 'frog,zombie';
1618 $this->assertEquals(', u1.zombie AS u_zombie',
1619 get_extra_user_fields_sql($context, 'u1', 'u_', array('frog')));
1621 // As long as this test passes, the value will be set back. This is only
1622 // in-memory anyhow
1623 $CFG->showuseridentity = $oldshowuseridentity;
1624 $USER = $olduser;
1628 * Test some critical TZ/DST.
1630 * This method tests some special TZ/DST combinations that were fixed
1631 * by MDL-38999. The tests are done by comparing the results of the
1632 * output using Moodle TZ/DST support and PHP native one.
1634 * Note: If you don't trust PHP TZ/DST support, can verify the
1635 * harcoded expectations below with:
1636 * http://www.tools4noobs.com/online_tools/unix_timestamp_to_datetime/
1638 public function test_some_moodle_special_dst() {
1639 $stamp = 1365386400; // 2013/04/08 02:00:00 GMT/UTC.
1641 // In Europe/Tallinn it was 2013/04/08 05:00:00.
1642 $expectation = '2013/04/08 05:00:00';
1643 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1644 $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
1645 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1646 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
1647 $this->assertSame($expectation, $phpres);
1648 $this->assertSame($expectation, $moodleres);
1650 // In St. Johns it was 2013/04/07 23:30:00.
1651 $expectation = '2013/04/07 23:30:00';
1652 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1653 $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
1654 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1655 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
1656 $this->assertSame($expectation, $phpres);
1657 $this->assertSame($expectation, $moodleres);
1659 $stamp = 1383876000; // 2013/11/08 02:00:00 GMT/UTC.
1661 // In Europe/Tallinn it was 2013/11/08 04:00:00.
1662 $expectation = '2013/11/08 04:00:00';
1663 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1664 $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
1665 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1666 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
1667 $this->assertSame($expectation, $phpres);
1668 $this->assertSame($expectation, $moodleres);
1670 // In St. Johns it was 2013/11/07 22:30:00.
1671 $expectation = '2013/11/07 22:30:00';
1672 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1673 $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
1674 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1675 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
1676 $this->assertSame($expectation, $phpres);
1677 $this->assertSame($expectation, $moodleres);
1680 public function test_userdate() {
1681 global $USER, $CFG, $DB;
1683 $olduser = $USER;
1684 $USER = $DB->get_record('user', array('id'=>2)); //admin
1686 $testvalues = array(
1687 array(
1688 'time' => '1309514400',
1689 'usertimezone' => 'America/Moncton',
1690 'timezone' => '0.0', //no dst offset
1691 'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM'
1693 array(
1694 'time' => '1309514400',
1695 'usertimezone' => 'America/Moncton',
1696 'timezone' => '99', //dst offset and timezone offset.
1697 'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
1699 array(
1700 'time' => '1309514400',
1701 'usertimezone' => 'America/Moncton',
1702 'timezone' => 'America/Moncton', //dst offset and timezone offset.
1703 'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
1705 array(
1706 'time' => '1293876000 ',
1707 'usertimezone' => 'America/Moncton',
1708 'timezone' => '0.0', //no dst offset
1709 'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM'
1711 array(
1712 'time' => '1293876000 ',
1713 'usertimezone' => 'America/Moncton',
1714 'timezone' => '99', //no dst offset in jan, so just timezone offset.
1715 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
1717 array(
1718 'time' => '1293876000 ',
1719 'usertimezone' => 'America/Moncton',
1720 'timezone' => 'America/Moncton', //no dst offset in jan
1721 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
1723 array(
1724 'time' => '1293876000 ',
1725 'usertimezone' => '2',
1726 'timezone' => '99', //take user timezone
1727 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1729 array(
1730 'time' => '1293876000 ',
1731 'usertimezone' => '-2',
1732 'timezone' => '99', //take user timezone
1733 'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
1735 array(
1736 'time' => '1293876000 ',
1737 'usertimezone' => '-10',
1738 'timezone' => '2', //take this timezone
1739 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1741 array(
1742 'time' => '1293876000 ',
1743 'usertimezone' => '-10',
1744 'timezone' => '-2', //take this timezone
1745 'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
1747 array(
1748 'time' => '1293876000 ',
1749 'usertimezone' => '-10',
1750 'timezone' => 'random/time', //this should show server time
1751 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
1753 array(
1754 'time' => '1293876000 ',
1755 'usertimezone' => '14', //server time zone
1756 'timezone' => '99', //this should show user time
1757 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
1761 //Check if forcetimezone is set then save it and set it to use user timezone
1762 $cfgforcetimezone = null;
1763 if (isset($CFG->forcetimezone)) {
1764 $cfgforcetimezone = $CFG->forcetimezone;
1765 $CFG->forcetimezone = 99; //get user default timezone.
1767 //store user default timezone to restore later
1768 $userstimezone = $USER->timezone;
1770 // The string version of date comes from server locale setting and does
1771 // not respect user language, so it is necessary to reset that.
1772 $oldlocale = setlocale(LC_TIME, '0');
1773 setlocale(LC_TIME, 'en_AU.UTF-8');
1775 //set default timezone to Australia/Perth, else time calculated
1776 //will not match expected values. Before that save system defaults.
1777 $systemdefaulttimezone = date_default_timezone_get();
1778 date_default_timezone_set('Australia/Perth');
1780 foreach ($testvalues as $vals) {
1781 $USER->timezone = $vals['usertimezone'];
1782 $actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
1784 //On different systems case of AM PM changes so compare case insensitive
1785 $vals['expectedoutput'] = textlib::strtolower($vals['expectedoutput']);
1786 $actualoutput = textlib::strtolower($actualoutput);
1788 $this->assertEquals($vals['expectedoutput'], $actualoutput,
1789 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1790 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1793 //restore user timezone back to what it was
1794 $USER->timezone = $userstimezone;
1796 //restore forcetimezone
1797 if (!is_null($cfgforcetimezone)) {
1798 $CFG->forcetimezone = $cfgforcetimezone;
1801 //restore system default values.
1802 date_default_timezone_set($systemdefaulttimezone);
1803 setlocale(LC_TIME, $oldlocale);
1805 $USER = $olduser;
1808 public function test_make_timestamp() {
1809 global $USER, $CFG, $DB;
1811 $olduser = $USER;
1812 $USER = $DB->get_record('user', array('id'=>2)); //admin
1814 $testvalues = array(
1815 array(
1816 'usertimezone' => 'America/Moncton',
1817 'year' => '2011',
1818 'month' => '7',
1819 'day' => '1',
1820 'hour' => '10',
1821 'minutes' => '00',
1822 'seconds' => '00',
1823 'timezone' => '0.0',
1824 'applydst' => false, //no dst offset
1825 'expectedoutput' => '1309514400' // 6pm at UTC+0
1827 array(
1828 'usertimezone' => 'America/Moncton',
1829 'year' => '2011',
1830 'month' => '7',
1831 'day' => '1',
1832 'hour' => '10',
1833 'minutes' => '00',
1834 'seconds' => '00',
1835 'timezone' => '99', //user default timezone
1836 'applydst' => false, //don't apply dst
1837 'expectedoutput' => '1309528800'
1839 array(
1840 'usertimezone' => 'America/Moncton',
1841 'year' => '2011',
1842 'month' => '7',
1843 'day' => '1',
1844 'hour' => '10',
1845 'minutes' => '00',
1846 'seconds' => '00',
1847 'timezone' => '99', //user default timezone
1848 'applydst' => true, //apply dst
1849 'expectedoutput' => '1309525200'
1851 array(
1852 'usertimezone' => 'America/Moncton',
1853 'year' => '2011',
1854 'month' => '7',
1855 'day' => '1',
1856 'hour' => '10',
1857 'minutes' => '00',
1858 'seconds' => '00',
1859 'timezone' => 'America/Moncton', //string timezone
1860 'applydst' => true, //apply dst
1861 'expectedoutput' => '1309525200'
1863 array(
1864 'usertimezone' => '2',//no dst applyed
1865 'year' => '2011',
1866 'month' => '7',
1867 'day' => '1',
1868 'hour' => '10',
1869 'minutes' => '00',
1870 'seconds' => '00',
1871 'timezone' => '99', //take user timezone
1872 'applydst' => true, //apply dst
1873 'expectedoutput' => '1309507200'
1875 array(
1876 'usertimezone' => '-2',//no dst applyed
1877 'year' => '2011',
1878 'month' => '7',
1879 'day' => '1',
1880 'hour' => '10',
1881 'minutes' => '00',
1882 'seconds' => '00',
1883 'timezone' => '99', //take usertimezone
1884 'applydst' => true, //apply dst
1885 'expectedoutput' => '1309521600'
1887 array(
1888 'usertimezone' => '-10',//no dst applyed
1889 'year' => '2011',
1890 'month' => '7',
1891 'day' => '1',
1892 'hour' => '10',
1893 'minutes' => '00',
1894 'seconds' => '00',
1895 'timezone' => '2', //take this timezone
1896 'applydst' => true, //apply dst
1897 'expectedoutput' => '1309507200'
1899 array(
1900 'usertimezone' => '-10',//no dst applyed
1901 'year' => '2011',
1902 'month' => '7',
1903 'day' => '1',
1904 'hour' => '10',
1905 'minutes' => '00',
1906 'seconds' => '00',
1907 'timezone' => '-2', //take this timezone
1908 'applydst' => true, //apply dst,
1909 'expectedoutput' => '1309521600'
1911 array(
1912 'usertimezone' => '-10',//no dst applyed
1913 'year' => '2011',
1914 'month' => '7',
1915 'day' => '1',
1916 'hour' => '10',
1917 'minutes' => '00',
1918 'seconds' => '00',
1919 'timezone' => 'random/time', //This should show server time
1920 'applydst' => true, //apply dst,
1921 'expectedoutput' => '1309485600'
1923 array(
1924 'usertimezone' => '14',//server time
1925 'year' => '2011',
1926 'month' => '7',
1927 'day' => '1',
1928 'hour' => '10',
1929 'minutes' => '00',
1930 'seconds' => '00',
1931 'timezone' => '99', //get user time
1932 'applydst' => true, //apply dst,
1933 'expectedoutput' => '1309485600'
1937 //Check if forcetimezone is set then save it and set it to use user timezone
1938 $cfgforcetimezone = null;
1939 if (isset($CFG->forcetimezone)) {
1940 $cfgforcetimezone = $CFG->forcetimezone;
1941 $CFG->forcetimezone = 99; //get user default timezone.
1944 //store user default timezone to restore later
1945 $userstimezone = $USER->timezone;
1947 // The string version of date comes from server locale setting and does
1948 // not respect user language, so it is necessary to reset that.
1949 $oldlocale = setlocale(LC_TIME, '0');
1950 setlocale(LC_TIME, 'en_AU.UTF-8');
1952 //set default timezone to Australia/Perth, else time calulated
1953 //will not match expected values. Before that save system defaults.
1954 $systemdefaulttimezone = date_default_timezone_get();
1955 date_default_timezone_set('Australia/Perth');
1957 //Test make_timestamp with all testvals and assert if anything wrong.
1958 foreach ($testvalues as $vals) {
1959 $USER->timezone = $vals['usertimezone'];
1960 $actualoutput = make_timestamp(
1961 $vals['year'],
1962 $vals['month'],
1963 $vals['day'],
1964 $vals['hour'],
1965 $vals['minutes'],
1966 $vals['seconds'],
1967 $vals['timezone'],
1968 $vals['applydst']
1971 //On different systems case of AM PM changes so compare case insenitive
1972 $vals['expectedoutput'] = textlib::strtolower($vals['expectedoutput']);
1973 $actualoutput = textlib::strtolower($actualoutput);
1975 $this->assertEquals($vals['expectedoutput'], $actualoutput,
1976 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1977 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1980 //restore user timezone back to what it was
1981 $USER->timezone = $userstimezone;
1983 //restore forcetimezone
1984 if (!is_null($cfgforcetimezone)) {
1985 $CFG->forcetimezone = $cfgforcetimezone;
1988 //restore system default values.
1989 date_default_timezone_set($systemdefaulttimezone);
1990 setlocale(LC_TIME, $oldlocale);
1992 $USER = $olduser;
1996 * Test get_string and most importantly the implementation of the lang_string
1997 * object.
1999 public function test_get_string() {
2000 global $COURSE;
2002 // Make sure we are using English
2003 $originallang = $COURSE->lang;
2004 $COURSE->lang = 'en';
2006 $yes = get_string('yes');
2007 $yesexpected = 'Yes';
2008 $this->assertEquals(getType($yes), 'string');
2009 $this->assertEquals($yes, $yesexpected);
2011 $yes = get_string('yes', 'moodle');
2012 $this->assertEquals(getType($yes), 'string');
2013 $this->assertEquals($yes, $yesexpected);
2015 $yes = get_string('yes', 'core');
2016 $this->assertEquals(getType($yes), 'string');
2017 $this->assertEquals($yes, $yesexpected);
2019 $yes = get_string('yes', '');
2020 $this->assertEquals(getType($yes), 'string');
2021 $this->assertEquals($yes, $yesexpected);
2023 $yes = get_string('yes', null);
2024 $this->assertEquals(getType($yes), 'string');
2025 $this->assertEquals($yes, $yesexpected);
2027 $yes = get_string('yes', null, 1);
2028 $this->assertEquals(getType($yes), 'string');
2029 $this->assertEquals($yes, $yesexpected);
2031 $days = 1;
2032 $numdays = get_string('numdays', 'core', '1');
2033 $numdaysexpected = $days.' days';
2034 $this->assertEquals(getType($numdays), 'string');
2035 $this->assertEquals($numdays, $numdaysexpected);
2037 $yes = get_string('yes', null, null, true);
2038 $this->assertEquals(get_class($yes), 'lang_string');
2039 $this->assertEquals((string)$yes, $yesexpected);
2041 // Test using a lang_string object as the $a argument for a normal
2042 // get_string call (returning string)
2043 $test = new lang_string('yes', null, null, true);
2044 $testexpected = get_string('numdays', 'core', get_string('yes'));
2045 $testresult = get_string('numdays', null, $test);
2046 $this->assertEquals(getType($testresult), 'string');
2047 $this->assertEquals($testresult, $testexpected);
2049 // Test using a lang_string object as the $a argument for an object
2050 // get_string call (returning lang_string)
2051 $test = new lang_string('yes', null, null, true);
2052 $testexpected = get_string('numdays', 'core', get_string('yes'));
2053 $testresult = get_string('numdays', null, $test, true);
2054 $this->assertEquals(get_class($testresult), 'lang_string');
2055 $this->assertEquals("$testresult", $testexpected);
2057 // Make sure that object properties that can't be converted don't cause
2058 // errors
2059 // Level one: This is as deep as current language processing goes
2060 $test = new stdClass;
2061 $test->one = 'here';
2062 $string = get_string('yes', null, $test, true);
2063 $this->assertEquals($string, $yesexpected);
2065 // Make sure that object properties that can't be converted don't cause
2066 // errors.
2067 // Level two: Language processing doesn't currently reach this deep.
2068 // only immediate scalar properties are worked with.
2069 $test = new stdClass;
2070 $test->one = new stdClass;
2071 $test->one->two = 'here';
2072 $string = get_string('yes', null, $test, true);
2073 $this->assertEquals($string, $yesexpected);
2075 // Make sure that object properties that can't be converted don't cause
2076 // errors.
2077 // Level three: It should never ever go this deep, but we're making sure
2078 // it doesn't cause any probs anyway.
2079 $test = new stdClass;
2080 $test->one = new stdClass;
2081 $test->one->two = new stdClass;
2082 $test->one->two->three = 'here';
2083 $string = get_string('yes', null, $test, true);
2084 $this->assertEquals($string, $yesexpected);
2086 // Make sure that object properties that can't be converted don't cause
2087 // errors and check lang_string properties.
2088 // Level one: This is as deep as current language processing goes
2089 $test = new stdClass;
2090 $test->one = new lang_string('yes');
2091 $string = get_string('yes', null, $test, true);
2092 $this->assertEquals($string, $yesexpected);
2094 // Make sure that object properties that can't be converted don't cause
2095 // errors and check lang_string properties.
2096 // Level two: Language processing doesn't currently reach this deep.
2097 // only immediate scalar properties are worked with.
2098 $test = new stdClass;
2099 $test->one = new stdClass;
2100 $test->one->two = new lang_string('yes');
2101 $string = get_string('yes', null, $test, true);
2102 $this->assertEquals($string, $yesexpected);
2104 // Make sure that object properties that can't be converted don't cause
2105 // errors and check lang_string properties.
2106 // Level three: It should never ever go this deep, but we're making sure
2107 // it doesn't cause any probs anyway.
2108 $test = new stdClass;
2109 $test->one = new stdClass;
2110 $test->one->two = new stdClass;
2111 $test->one->two->three = new lang_string('yes');
2112 $string = get_string('yes', null, $test, true);
2113 $this->assertEquals($string, $yesexpected);
2115 // Make sure that array properties that can't be converted don't cause
2116 // errors
2117 $test = array();
2118 $test['one'] = new stdClass;
2119 $test['one']->two = 'here';
2120 $string = get_string('yes', null, $test, true);
2121 $this->assertEquals($string, $yesexpected);
2123 // Same thing but as above except using an object... this is allowed :P
2124 $string = get_string('yes', null, null, true);
2125 $object = new stdClass;
2126 $object->$string = 'Yes';
2127 $this->assertEquals($string, $yesexpected);
2128 $this->assertEquals($object->$string, $yesexpected);
2130 // Reset the language
2131 $COURSE->lang = $originallang;
2135 * @expectedException PHPUnit_Framework_Error_Warning
2136 * @return void
2138 public function test_get_string_limitation() {
2139 // This is one of the limitations to the lang_string class. It can't be
2140 // used as a key
2141 $array = array(get_string('yes', null, null, true) => 'yes');
2145 * Test localised float formatting.
2147 public function test_format_float() {
2149 // Special case for null
2150 $this->assertEquals('', format_float(null));
2152 // Default 1 decimal place
2153 $this->assertEquals('5.4', format_float(5.43));
2154 $this->assertEquals('5.0', format_float(5.001));
2156 // Custom number of decimal places
2157 $this->assertEquals('5.43000', format_float(5.43, 5));
2159 // Option to strip ending zeros after rounding
2160 $this->assertEquals('5.43', format_float(5.43, 5, true, true));
2161 $this->assertEquals('5', format_float(5.0001, 3, true, true));
2163 // Tests with a localised decimal separator.
2164 $this->define_local_decimal_separator();
2166 // Localisation on (default)
2167 $this->assertEquals('5X43000', format_float(5.43, 5));
2168 $this->assertEquals('5X43', format_float(5.43, 5, true, true));
2170 // Localisation off
2171 $this->assertEquals('5.43000', format_float(5.43, 5, false));
2172 $this->assertEquals('5.43', format_float(5.43, 5, false, true));
2176 * Test localised float unformatting.
2178 public function test_unformat_float() {
2180 // Tests without the localised decimal separator.
2182 // Special case for null, empty or white spaces only strings.
2183 $this->assertEquals(null, unformat_float(null));
2184 $this->assertEquals(null, unformat_float(''));
2185 $this->assertEquals(null, unformat_float(' '));
2187 // Regular use.
2188 $this->assertEquals(5.4, unformat_float('5.4'));
2189 $this->assertEquals(5.4, unformat_float('5.4', true));
2191 // No decimal.
2192 $this->assertEquals(5.0, unformat_float('5'));
2194 // Custom number of decimal.
2195 $this->assertEquals(5.43267, unformat_float('5.43267'));
2197 // Empty decimal.
2198 $this->assertEquals(100.0, unformat_float('100.00'));
2200 // With the thousand separator.
2201 $this->assertEquals(1000.0, unformat_float('1 000'));
2202 $this->assertEquals(1000.32, unformat_float('1 000.32'));
2204 // Negative number.
2205 $this->assertEquals(-100.0, unformat_float('-100'));
2207 // Wrong value.
2208 $this->assertEquals(0.0, unformat_float('Wrong value'));
2209 // Wrong value in strict mode.
2210 $this->assertFalse(unformat_float('Wrong value', true));
2212 // Combining options.
2213 $this->assertEquals(-1023.862567, unformat_float(' -1 023.862567 '));
2215 // Bad decimal separator (should crop the decimal).
2216 $this->assertEquals(50.0, unformat_float('50,57'));
2217 // Bad decimal separator in strict mode (should return false).
2218 $this->assertFalse(unformat_float('50,57', true));
2220 // Tests with a localised decimal separator.
2221 $this->define_local_decimal_separator();
2223 // We repeat the tests above but with the current decimal separator.
2225 // Regular use without and with the localised separator.
2226 $this->assertEquals (5.4, unformat_float('5.4'));
2227 $this->assertEquals (5.4, unformat_float('5X4'));
2229 // Custom number of decimal.
2230 $this->assertEquals (5.43267, unformat_float('5X43267'));
2232 // Empty decimal.
2233 $this->assertEquals (100.0, unformat_float('100X00'));
2235 // With the thousand separator.
2236 $this->assertEquals (1000.32, unformat_float('1 000X32'));
2238 // Bad different separator (should crop the decimal).
2239 $this->assertEquals (50.0, unformat_float('50Y57'));
2240 // Bad different separator in strict mode (should return false).
2241 $this->assertFalse (unformat_float('50Y57', true));
2243 // Combining options.
2244 $this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 '));
2245 // Combining options in strict mode.
2246 $this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 ', true));
2250 * Test deleting of users.
2252 public function test_delete_user() {
2253 global $DB, $CFG;
2255 $this->resetAfterTest();
2257 $guest = $DB->get_record('user', array('id'=>$CFG->siteguest), '*', MUST_EXIST);
2258 $admin = $DB->get_record('user', array('id'=>$CFG->siteadmins), '*', MUST_EXIST);
2259 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
2261 $user = $this->getDataGenerator()->create_user(array('idnumber'=>'abc'));
2262 $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'xyz'));
2264 $result = delete_user($user);
2265 $this->assertTrue($result);
2266 $deluser = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
2267 $this->assertEquals(1, $deluser->deleted);
2268 $this->assertEquals(0, $deluser->picture);
2269 $this->assertSame('', $deluser->idnumber);
2270 $this->assertSame(md5($user->username), $deluser->email);
2271 $this->assertRegExp('/^'.preg_quote($user->email, '/').'\.\d*$/', $deluser->username);
2273 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
2275 // Try invalid params.
2277 $record = new stdClass();
2278 $record->grrr = 1;
2279 try {
2280 delete_user($record);
2281 $this->fail('Expecting exception for invalid delete_user() $user parameter');
2282 } catch (coding_exception $e) {
2283 $this->assertTrue(true);
2285 $record->id = 1;
2286 try {
2287 delete_user($record);
2288 $this->fail('Expecting exception for invalid delete_user() $user parameter');
2289 } catch (coding_exception $e) {
2290 $this->assertTrue(true);
2293 $record = new stdClass();
2294 $record->id = 666;
2295 $record->username = 'xx';
2296 $this->assertFalse($DB->record_exists('user', array('id'=>666))); // Any non-existent id is ok.
2297 $result = delete_user($record);
2298 $this->assertFalse($result);
2300 $result = delete_user($guest);
2301 $this->assertFalse($result);
2303 $result = delete_user($admin);
2304 $this->assertFalse($result);
2306 $this->resetDebugging();
2310 * Test function convert_to_array()
2312 public function test_convert_to_array() {
2313 // check that normal classes are converted to arrays the same way as (array) would do
2314 $obj = new stdClass();
2315 $obj->prop1 = 'hello';
2316 $obj->prop2 = array('first', 'second', 13);
2317 $obj->prop3 = 15;
2318 $this->assertEquals(convert_to_array($obj), (array)$obj);
2320 // check that context object (with iterator) is converted to array properly
2321 $obj = get_system_context();
2322 $ar = array(
2323 'id' => $obj->id,
2324 'contextlevel' => $obj->contextlevel,
2325 'instanceid' => $obj->instanceid,
2326 'path' => $obj->path,
2327 'depth' => $obj->depth
2329 $this->assertEquals(convert_to_array($obj), $ar);
2333 * Test the function date_format_string().
2335 function test_date_format_string() {
2336 global $CFG;
2338 // Forcing locale and timezone.
2339 $oldlocale = setlocale(LC_TIME, '0');
2340 if ($CFG->ostype == 'WINDOWS') {
2341 setlocale(LC_TIME, 'English_Australia.1252');
2342 } else {
2343 setlocale(LC_TIME, 'en_AU.UTF-8');
2345 $systemdefaulttimezone = date_default_timezone_get();
2346 date_default_timezone_set('Australia/Perth');
2348 $tests = array(
2349 array(
2350 'tz' => 99,
2351 'str' => '%A, %d %B %Y, %I:%M %p',
2352 'expected' => 'Saturday, 01 January 2011, 06:00 PM'
2354 array(
2355 'tz' => 0,
2356 'str' => '%A, %d %B %Y, %I:%M %p',
2357 'expected' => 'Saturday, 01 January 2011, 10:00 AM'
2359 array(
2360 'tz' => -12,
2361 'str' => '%A, %d %B %Y, %I:%M %p',
2362 'expected' => 'Saturday, 01 January 2011, 10:00 AM'
2364 // Following tests pass on Windows only because en lang pack does
2365 // not contain localewincharset, in real life lang pack maintainers
2366 // may use only characters that are present in localewincharset
2367 // in format strings!
2368 array(
2369 'tz' => 99,
2370 'str' => 'Žluťoučký koníček %A',
2371 'expected' => 'Žluťoučký koníček Saturday'
2373 array(
2374 'tz' => 99,
2375 'str' => '言語設定言語 %A',
2376 'expected' => '言語設定言語 Saturday'
2378 array(
2379 'tz' => 99,
2380 'str' => '简体中文简体 %A',
2381 'expected' => '简体中文简体 Saturday'
2385 // Note: date_format_string() uses the timezone only to differenciate
2386 // the server time from the UTC time. It does not modify the timestamp.
2387 // Hence similar results for timezones <= 13.
2388 // On different systems case of AM PM changes so compare case insensitive.
2389 foreach ($tests as $test) {
2390 $str = date_format_string(1293876000, $test['str'], $test['tz']);
2391 $this->assertEquals(textlib::strtolower($test['expected']), textlib::strtolower($str));
2394 // Restore system default values.
2395 date_default_timezone_set($systemdefaulttimezone);
2396 setlocale(LC_TIME, $oldlocale);
2399 public function test_get_config() {
2400 global $CFG;
2402 $this->resetAfterTest();
2404 // Preparation.
2405 set_config('phpunit_test_get_config_1', 'test 1');
2406 set_config('phpunit_test_get_config_2', 'test 2', 'mod_forum');
2407 if (!is_array($CFG->config_php_settings)) {
2408 $CFG->config_php_settings = array();
2410 $CFG->config_php_settings['phpunit_test_get_config_3'] = 'test 3';
2412 if (!is_array($CFG->forced_plugin_settings)) {
2413 $CFG->forced_plugin_settings = array();
2415 if (!array_key_exists('mod_forum', $CFG->forced_plugin_settings)) {
2416 $CFG->forced_plugin_settings['mod_forum'] = array();
2418 $CFG->forced_plugin_settings['mod_forum']['phpunit_test_get_config_4'] = 'test 4';
2419 $CFG->phpunit_test_get_config_5 = 'test 5';
2421 // Testing.
2422 $this->assertEquals('test 1', get_config('core', 'phpunit_test_get_config_1'));
2423 $this->assertEquals('test 2', get_config('mod_forum', 'phpunit_test_get_config_2'));
2424 $this->assertEquals('test 3', get_config('core', 'phpunit_test_get_config_3'));
2425 $this->assertEquals('test 4', get_config('mod_forum', 'phpunit_test_get_config_4'));
2426 $this->assertFalse(get_config('core', 'phpunit_test_get_config_5'));
2427 $this->assertFalse(get_config('core', 'phpunit_test_get_config_x'));
2428 $this->assertFalse(get_config('mod_forum', 'phpunit_test_get_config_x'));
2430 // Test config we know to exist.
2431 $this->assertEquals($CFG->dataroot, get_config('core', 'dataroot'));
2432 $this->assertEquals($CFG->phpunit_dataroot, get_config('core', 'phpunit_dataroot'));
2433 $this->assertEquals($CFG->dataroot, get_config('core', 'phpunit_dataroot'));
2434 $this->assertEquals(get_config('core', 'dataroot'), get_config('core', 'phpunit_dataroot'));
2436 // Test setting a config var that already exists.
2437 set_config('phpunit_test_get_config_1', 'test a');
2438 $this->assertEquals('test a', $CFG->phpunit_test_get_config_1);
2439 $this->assertEquals('test a', get_config('core', 'phpunit_test_get_config_1'));
2441 // Test cache invalidation.
2442 $cache = cache::make('core', 'config');
2443 $this->assertInternalType('array', $cache->get('core'));
2444 $this->assertInternalType('array', $cache->get('mod_forum'));
2445 set_config('phpunit_test_get_config_1', 'test b');
2446 $this->assertFalse($cache->get('core'));
2447 set_config('phpunit_test_get_config_4', 'test c', 'mod_forum');
2448 $this->assertFalse($cache->get('mod_forum'));
2451 function test_get_max_upload_sizes() {
2452 // Test with very low limits so we are not affected by php upload limits.
2453 // Test activity limit smallest.
2454 $sitebytes = 102400;
2455 $coursebytes = 51200;
2456 $modulebytes = 10240;
2457 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2459 $this->assertEquals('Activity upload limit (10KB)', $result['0']);
2460 $this->assertEquals(2, count($result));
2462 // Test course limit smallest.
2463 $sitebytes = 102400;
2464 $coursebytes = 10240;
2465 $modulebytes = 51200;
2466 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2468 $this->assertEquals('Course upload limit (10KB)', $result['0']);
2469 $this->assertEquals(2, count($result));
2471 // Test site limit smallest.
2472 $sitebytes = 10240;
2473 $coursebytes = 102400;
2474 $modulebytes = 51200;
2475 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2477 $this->assertEquals('Site upload limit (10KB)', $result['0']);
2478 $this->assertEquals(2, count($result));
2480 // Test site limit not set.
2481 $sitebytes = 0;
2482 $coursebytes = 102400;
2483 $modulebytes = 51200;
2484 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2486 $this->assertEquals('Activity upload limit (50KB)', $result['0']);
2487 $this->assertEquals(3, count($result));
2489 $sitebytes = 0;
2490 $coursebytes = 51200;
2491 $modulebytes = 102400;
2492 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2494 $this->assertEquals('Course upload limit (50KB)', $result['0']);
2495 $this->assertEquals(3, count($result));
2497 // Test custom bytes in range.
2498 $sitebytes = 102400;
2499 $coursebytes = 51200;
2500 $modulebytes = 51200;
2501 $custombytes = 10240;
2502 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2504 $this->assertEquals(3, count($result));
2506 // Test custom bytes in range but non-standard.
2507 $sitebytes = 102400;
2508 $coursebytes = 51200;
2509 $modulebytes = 51200;
2510 $custombytes = 25600;
2511 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2513 $this->assertEquals(4, count($result));
2515 // Test custom bytes out of range.
2516 $sitebytes = 102400;
2517 $coursebytes = 51200;
2518 $modulebytes = 51200;
2519 $custombytes = 102400;
2520 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2522 $this->assertEquals(3, count($result));
2524 // Test custom bytes out of range and non-standard.
2525 $sitebytes = 102400;
2526 $coursebytes = 51200;
2527 $modulebytes = 51200;
2528 $custombytes = 256000;
2529 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2531 $this->assertEquals(3, count($result));
2533 // Test site limit only.
2534 $sitebytes = 51200;
2535 $result = get_max_upload_sizes($sitebytes);
2537 $this->assertEquals('Site upload limit (50KB)', $result['0']);
2538 $this->assertEquals('50KB', $result['51200']);
2539 $this->assertEquals('10KB', $result['10240']);
2540 $this->assertCount(3, $result);
2542 // Test no limit.
2543 $result = get_max_upload_sizes();
2544 $this->assertArrayHasKey('0', $result);
2545 $this->assertArrayHasKey(get_max_upload_file_size(), $result);
2549 * Test function password_is_legacy_hash().
2551 public function test_password_is_legacy_hash() {
2552 // Well formed md5s should be matched.
2553 foreach (array('some', 'strings', 'to_check!') as $string) {
2554 $md5 = md5($string);
2555 $this->assertTrue(password_is_legacy_hash($md5));
2557 // Strings that are not md5s should not be matched.
2558 foreach (array('', AUTH_PASSWORD_NOT_CACHED, 'IPW8WTcsWNgAWcUS1FBVHegzJnw5M2jOmYkmfc8z.xdBOyC4Caeum') as $notmd5) {
2559 $this->assertFalse(password_is_legacy_hash($notmd5));
2564 * Test function validate_internal_user_password().
2566 public function test_validate_internal_user_password() {
2567 if (password_compat_not_supported()) {
2568 // If bcrypt is not properly supported test legacy md5 hashes instead.
2569 // Can't hardcode these as we don't know the site's password salt.
2570 $validhashes = array(
2571 'pw' => hash_internal_user_password('pw'),
2572 'abc' => hash_internal_user_password('abc'),
2573 'C0mP1eX_&}<?@*&%` |\"' => hash_internal_user_password('C0mP1eX_&}<?@*&%` |\"'),
2574 'ĩńťėŕňăţĩōŋāĹ' => hash_internal_user_password('ĩńťėŕňăţĩōŋāĹ')
2576 } else {
2577 // Otherwise test bcrypt hashes.
2578 $validhashes = array(
2579 'pw' => '$2y$10$LOSDi5eaQJhutSRun.OVJ.ZSxQZabCMay7TO1KmzMkDMPvU40zGXK',
2580 'abc' => '$2y$10$VWTOhVdsBbWwtdWNDRHSpewjd3aXBQlBQf5rBY/hVhw8hciarFhXa',
2581 'C0mP1eX_&}<?@*&%` |\"' => '$2y$10$3PJf.q.9ywNJlsInPbqc8.IFeSsvXrGvQLKRFBIhVu1h1I3vpIry6',
2582 'ĩńťėŕňăţĩōŋāĹ' => '$2y$10$3A2Y8WpfRAnP3czJiSv6N.6Xp0T8hW3QZz2hUCYhzyWr1kGP1yUve'
2586 foreach ($validhashes as $password => $hash) {
2587 $user = new stdClass();
2588 $user->auth = 'manual';
2589 $user->password = $hash;
2590 // The correct password should be validated.
2591 $this->assertTrue(validate_internal_user_password($user, $password));
2592 // An incorrect password should not be validated.
2593 $this->assertFalse(validate_internal_user_password($user, 'badpw'));
2598 * Test function hash_internal_user_password().
2600 public function test_hash_internal_user_password() {
2601 $passwords = array('pw', 'abc123', 'C0mP1eX_&}<?@*&%` |\"', 'ĩńťėŕňăţĩōŋāĹ');
2603 // Check that some passwords that we convert to hashes can
2604 // be validated.
2605 foreach ($passwords as $password) {
2606 $hash = hash_internal_user_password($password);
2607 $fasthash = hash_internal_user_password($password, true);
2608 $user = new stdClass();
2609 $user->auth = 'manual';
2610 $user->password = $hash;
2611 $this->assertTrue(validate_internal_user_password($user, $password));
2613 if (password_compat_not_supported()) {
2614 // If bcrypt is not properly supported make sure the passwords are in md5 format.
2615 $this->assertTrue(password_is_legacy_hash($hash));
2616 } else {
2617 // Otherwise they should not be in md5 format.
2618 $this->assertFalse(password_is_legacy_hash($hash));
2620 // Check that cost factor in hash is correctly set.
2621 $this->assertRegExp('/\$10\$/', $hash);
2622 $this->assertRegExp('/\$04\$/', $fasthash);
2628 * Test function update_internal_user_password().
2630 public function test_update_internal_user_password() {
2631 global $DB;
2632 $this->resetAfterTest();
2633 $passwords = array('password', '1234', 'changeme', '****');
2634 foreach ($passwords as $password) {
2635 $user = $this->getDataGenerator()->create_user(array('auth'=>'manual'));
2636 update_internal_user_password($user, $password);
2637 // The user object should have been updated.
2638 $this->assertTrue(validate_internal_user_password($user, $password));
2639 // The database field for the user should also have been updated to the
2640 // same value.
2641 $this->assertEquals($user->password, $DB->get_field('user', 'password', array('id' => $user->id)));
2644 $user = $this->getDataGenerator()->create_user(array('auth'=>'manual'));
2645 // Manually set the user's password to the md5 of the string 'password'.
2646 $DB->set_field('user', 'password', '5f4dcc3b5aa765d61d8327deb882cf99', array('id' => $user->id));
2648 // Update the password.
2649 update_internal_user_password($user, 'password');
2651 if (password_compat_not_supported()) {
2652 // If bcrypt not properly supported the password should remain as an md5 hash.
2653 $expected_hash = hash_internal_user_password('password', true);
2654 $this->assertEquals($user->password, $expected_hash);
2655 $this->assertTrue(password_is_legacy_hash($user->password));
2656 } else {
2657 // Otherwise password should have been updated to a bcrypt hash.
2658 $this->assertFalse(password_is_legacy_hash($user->password));