MDL-39524 tests: fix incorrect phpdoc
[moodle.git] / lib / tests / setuplib_test.php
CommitLineData
32c842e2
TH
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.
13//
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/>.
16
17/**
18 * Unit tests for setuplib.php
19 *
20e56131
PS
20 * @package core
21 * @category phpunit
32c842e2
TH
22 * @copyright 2012 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Unit tests for setuplib.php
31 *
32 * @copyright 2012 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
85b38061 35class core_setuplib_testcase extends advanced_testcase {
32c842e2
TH
36
37 /**
38 * Test get_docs_url_standard in the normal case when we should link to Moodle docs.
39 */
40 public function test_get_docs_url_standard() {
41 global $CFG;
42 if (empty($CFG->docroot)) {
43 $docroot = 'http://docs.moodle.org/';
44 } else {
45 $docroot = $CFG->docroot;
46 }
2261bc15 47 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/\d{2}/' . current_language() . '/course/editing$~',
32c842e2
TH
48 get_docs_url('course/editing'));
49 }
50
51 /**
52 * Test get_docs_url_standard in the special case of an absolute HTTP URL.
53 */
54 public function test_get_docs_url_http() {
55 $url = 'http://moodle.org/';
56 $this->assertEquals($url, get_docs_url($url));
57 }
58
59 /**
60 * Test get_docs_url_standard in the special case of an absolute HTTPS URL.
61 */
62 public function test_get_docs_url_https() {
63 $url = 'https://moodle.org/';
64 $this->assertEquals($url, get_docs_url($url));
65 }
66
67 /**
68 * Test get_docs_url_standard in the special case of a link relative to wwwroot.
69 */
70 public function test_get_docs_url_wwwroot() {
71 global $CFG;
20e56131 72 $this->assertSame($CFG->wwwroot . '/lib/tests/setuplib_test.php',
32c842e2 73 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php'));
f28af1dd 74 }
8d220cb5
MN
75
76 /**
20e56131 77 * Test if get_exception_info() removes file system paths.
8d220cb5
MN
78 */
79 public function test_exception_info_removes_serverpaths() {
80 global $CFG;
81
82 // This doesn't test them all possible ones, but these are set for unit tests.
85b38061 83 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir', 'localcachedir');
8d220cb5
MN
84
85 $fixture = '';
86 $expected = '';
87 foreach ($cfgnames as $cfgname) {
88 if (!empty($CFG->$cfgname)) {
89 $fixture .= $CFG->$cfgname.' ';
90 $expected .= "[$cfgname] ";
91 }
92 }
93 $exception = new moodle_exception('generalexceptionmessage', 'error', '', $fixture, $fixture);
94 $exceptioninfo = get_exception_info($exception);
95
96 $this->assertContains($expected, $exceptioninfo->message, 'Exception message does not contain system paths');
97 $this->assertContains($expected, $exceptioninfo->debuginfo, 'Exception debug info does not contain system paths');
98 }
85b38061
PS
99
100 public function test_localcachedir() {
101 global $CFG;
102
103 $this->resetAfterTest(true);
104
105 // Test default location - can not be modified in phpunit tests because we override everything in config.php.
106 $this->assertSame("$CFG->dataroot/localcache", $CFG->localcachedir);
107
2a67e105 108 $this->setCurrentTimeStart();
85b38061
PS
109 $timestampfile = "$CFG->localcachedir/.lastpurged";
110
54957bdb
RT
111 // Delete existing localcache directory, as this is testing first call
112 // to make_localcache_directory.
113 remove_dir($CFG->localcachedir, true);
85b38061
PS
114 $dir = make_localcache_directory('', false);
115 $this->assertSame($CFG->localcachedir, $dir);
116 $this->assertFileNotExists("$CFG->localcachedir/.htaccess");
117 $this->assertFileExists($timestampfile);
2a67e105 118 $this->assertTimeCurrent(filemtime($timestampfile));
85b38061
PS
119
120 $dir = make_localcache_directory('test/test', false);
121 $this->assertSame("$CFG->localcachedir/test/test", $dir);
122
123 // Test custom location.
124 $CFG->localcachedir = "$CFG->dataroot/testlocalcache";
2a67e105 125 $this->setCurrentTimeStart();
85b38061
PS
126 $timestampfile = "$CFG->localcachedir/.lastpurged";
127 $this->assertFileNotExists($timestampfile);
128
129 $dir = make_localcache_directory('', false);
130 $this->assertSame($CFG->localcachedir, $dir);
131 $this->assertFileExists("$CFG->localcachedir/.htaccess");
132 $this->assertFileExists($timestampfile);
2a67e105 133 $this->assertTimeCurrent(filemtime($timestampfile));
85b38061
PS
134
135 $dir = make_localcache_directory('test', false);
136 $this->assertSame("$CFG->localcachedir/test", $dir);
137
138 $prevtime = filemtime($timestampfile);
139 $dir = make_localcache_directory('pokus', false);
140 $this->assertSame("$CFG->localcachedir/pokus", $dir);
141 $this->assertSame($prevtime, filemtime($timestampfile));
142
85b38061
PS
143 // Test purging.
144 $testfile = "$CFG->localcachedir/test/test.txt";
145 $this->assertTrue(touch($testfile));
146
2a67e105 147 $now = $this->setCurrentTimeStart();
85b38061
PS
148 set_config('localcachedirpurged', $now - 2);
149 purge_all_caches();
150 $this->assertFileNotExists($testfile);
151 $this->assertFileNotExists(dirname($testfile));
152 $this->assertFileExists($timestampfile);
2a67e105
PS
153 $this->assertTimeCurrent(filemtime($timestampfile));
154 $this->assertTimeCurrent($CFG->localcachedirpurged);
85b38061
PS
155
156 // Simulates purge_all_caches() on another server node.
157 make_localcache_directory('test', false);
158 $this->assertTrue(touch($testfile));
159 set_config('localcachedirpurged', $now - 1);
160 $this->assertTrue(touch($timestampfile, $now - 2));
161 clearstatcache();
162 $this->assertSame($now - 2, filemtime($timestampfile));
163
2a67e105 164 $this->setCurrentTimeStart();
85b38061
PS
165 $dir = make_localcache_directory('', false);
166 $this->assertSame("$CFG->localcachedir", $dir);
167 $this->assertFileNotExists($testfile);
168 $this->assertFileNotExists(dirname($testfile));
169 $this->assertFileExists($timestampfile);
2a67e105 170 $this->assertTimeCurrent(filemtime($timestampfile));
85b38061 171 }
a3777547 172
70be2642
AN
173 public function test_make_unique_directory_basedir_is_file() {
174 global $CFG;
175
176 // Start with a file instead of a directory.
177 $base = $CFG->tempdir . DIRECTORY_SEPARATOR . md5(microtime() + rand());
178 touch($base);
179
180 // First the false test.
181 $this->assertFalse(make_unique_writable_directory($base, false));
182
183 // Now check for exception.
184 $this->setExpectedException('invalid_dataroot_permissions',
185 $base . ' is not writable. Unable to create a unique directory within it.'
186 );
187 make_unique_writable_directory($base);
188
189 unlink($base);
190 }
191
192 public function test_make_unique_directory() {
193 global $CFG;
194
195 // Create directories should be both directories, and writable.
196 $firstdir = make_unique_writable_directory($CFG->tempdir);
197 $this->assertTrue(is_dir($firstdir));
198 $this->assertTrue(is_writable($firstdir));
199
200 $seconddir = make_unique_writable_directory($CFG->tempdir);
201 $this->assertTrue(is_dir($seconddir));
202 $this->assertTrue(is_writable($seconddir));
203
204 // Directories should be different each iteration.
205 $this->assertNotEquals($firstdir, $seconddir);
206 }
207
208 public function test_get_request_storage_directory() {
209 // Making a call to get_request_storage_directory should always give the same result.
210 $firstdir = get_request_storage_directory();
211 $seconddir = get_request_storage_directory();
212 $this->assertTrue(is_dir($firstdir));
213 $this->assertEquals($firstdir, $seconddir);
214
215 // Removing the directory and calling get_request_storage_directory() again should cause a new directory to be created.
216 remove_dir($firstdir);
217 $this->assertFalse(file_exists($firstdir));
218 $this->assertFalse(is_dir($firstdir));
219
220 $thirddir = get_request_storage_directory();
221 $this->assertTrue(is_dir($thirddir));
222 $this->assertNotEquals($firstdir, $thirddir);
223
224 // Removing it and replacing it with a file should cause it to be regenerated again.
225 remove_dir($thirddir);
226 $this->assertFalse(file_exists($thirddir));
227 $this->assertFalse(is_dir($thirddir));
228 touch($thirddir);
229 $this->assertTrue(file_exists($thirddir));
230 $this->assertFalse(is_dir($thirddir));
231
232 $fourthdir = get_request_storage_directory();
233 $this->assertTrue(is_dir($fourthdir));
234 $this->assertNotEquals($thirddir, $fourthdir);
235 }
236
237
238 public function test_make_request_directory() {
239 // Every request directory should be unique.
240 $firstdir = make_request_directory();
241 $seconddir = make_request_directory();
242 $thirddir = make_request_directory();
243 $fourthdir = make_request_directory();
244
245 $this->assertNotEquals($firstdir, $seconddir);
246 $this->assertNotEquals($firstdir, $thirddir);
247 $this->assertNotEquals($firstdir, $fourthdir);
248 $this->assertNotEquals($seconddir, $thirddir);
249 $this->assertNotEquals($seconddir, $fourthdir);
250 $this->assertNotEquals($thirddir, $fourthdir);
251
252 // They should also all be within the request storage directory.
253 $requestdir = get_request_storage_directory();
254 $this->assertEquals(0, strpos($firstdir, $requestdir));
255 $this->assertEquals(0, strpos($seconddir, $requestdir));
256 $this->assertEquals(0, strpos($thirddir, $requestdir));
257 $this->assertEquals(0, strpos($fourthdir, $requestdir));
258
259 // Removing the requestdir should mean that new request directories are still created successfully.
260 remove_dir($requestdir);
261 $this->assertFalse(file_exists($requestdir));
262 $this->assertFalse(is_dir($requestdir));
263
264 $fifthdir = make_request_directory();
265 $this->assertNotEquals($firstdir, $fifthdir);
266 $this->assertNotEquals($seconddir, $fifthdir);
267 $this->assertNotEquals($thirddir, $fifthdir);
268 $this->assertNotEquals($fourthdir, $fifthdir);
269 $this->assertTrue(is_dir($fifthdir));
270 $this->assertFalse(strpos($fifthdir, $requestdir));
271
272 // And it should be within the new request directory.
273 $newrequestdir = get_request_storage_directory();
274 $this->assertEquals(0, strpos($fifthdir, $newrequestdir));
275 }
276
a3777547
PS
277 public function test_merge_query_params() {
278 $original = array(
279 'id' => '1',
280 'course' => '2',
281 'action' => 'delete',
282 'grade' => array(
283 0 => 'a',
284 1 => 'b',
285 2 => 'c',
286 ),
287 'items' => array(
288 'a' => 'aa',
289 'b' => 'bb',
290 ),
291 'mix' => array(
292 0 => '2',
293 ),
294 'numerical' => array(
295 '2' => array('a' => 'b'),
296 '1' => '2',
297 ),
298 );
299
300 $chunk = array(
301 'numerical' => array(
302 '0' => 'z',
303 '2' => array('d' => 'e'),
304 ),
305 'action' => 'create',
306 'next' => '2',
307 'grade' => array(
308 0 => 'e',
309 1 => 'f',
310 2 => 'g',
311 ),
312 'mix' => 'mix',
313 );
314
315 $expected = array(
316 'id' => '1',
317 'course' => '2',
318 'action' => 'create',
319 'grade' => array(
320 0 => 'a',
321 1 => 'b',
322 2 => 'c',
323 3 => 'e',
324 4 => 'f',
325 5 => 'g',
326 ),
327 'items' => array(
328 'a' => 'aa',
329 'b' => 'bb',
330 ),
331 'mix' => 'mix',
332 'numerical' => array(
333 '2' => array('a' => 'b', 'd' => 'e'),
334 '1' => '2',
335 '0' => 'z',
336 ),
337 'next' => '2',
338 );
339
340 $array = $original;
341 merge_query_params($array, $chunk);
342
343 $this->assertSame($expected, $array);
344 $this->assertNotSame($original, $array);
345
346 $query = "id=1&course=2&action=create&grade%5B%5D=a&grade%5B%5D=b&grade%5B%5D=c&grade%5B%5D=e&grade%5B%5D=f&grade%5B%5D=g&items%5Ba%5D=aa&items%5Bb%5D=bb&mix=mix&numerical%5B2%5D%5Ba%5D=b&numerical%5B2%5D%5Bd%5D=e&numerical%5B1%5D=2&numerical%5B0%5D=z&next=2";
347 $decoded = array();
348 parse_str($query, $decoded);
349 $this->assertSame($expected, $decoded);
350
351 // Prove that we cannot use array_merge_recursive() instead.
352 $this->assertNotSame($expected, array_merge_recursive($original, $chunk));
353 }
db200a8e
FM
354
355 /**
356 * Test the link processed by get_exception_info().
357 */
358 public function test_get_exception_info_link() {
359 global $CFG, $SESSION;
360
361 $initialloginhttps = $CFG->loginhttps;
362 $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
363 $CFG->loginhttps = false;
364
365 // Simple local URL.
366 $url = $CFG->wwwroot . '/something/here?really=yes';
367 $exception = new moodle_exception('none', 'error', $url);
368 $infos = $this->get_exception_info($exception);
369 $this->assertSame($url, $infos->link);
370
371 // Relative local URL.
372 $url = '/something/here?really=yes';
373 $exception = new moodle_exception('none', 'error', $url);
374 $infos = $this->get_exception_info($exception);
375 $this->assertSame($CFG->wwwroot . '/', $infos->link);
376
377 // HTTPS URL when login HTTPS is not enabled.
378 $url = $httpswwwroot . '/something/here?really=yes';
379 $exception = new moodle_exception('none', 'error', $url);
380 $infos = $this->get_exception_info($exception);
381 $this->assertSame($CFG->wwwroot . '/', $infos->link);
382
383 // HTTPS URL with login HTTPS.
384 $CFG->loginhttps = true;
385 $url = $httpswwwroot . '/something/here?really=yes';
386 $exception = new moodle_exception('none', 'error', $url);
387 $infos = $this->get_exception_info($exception);
388 $this->assertSame($url, $infos->link);
389
390 // External HTTP URL.
391 $url = 'http://moodle.org/something/here?really=yes';
392 $exception = new moodle_exception('none', 'error', $url);
393 $infos = $this->get_exception_info($exception);
394 $this->assertSame($CFG->wwwroot . '/', $infos->link);
395
396 // External HTTPS URL.
397 $url = 'https://moodle.org/something/here?really=yes';
398 $exception = new moodle_exception('none', 'error', $url);
399 $infos = $this->get_exception_info($exception);
400 $this->assertSame($CFG->wwwroot . '/', $infos->link);
401
402 // External URL containing local URL.
403 $url = 'http://moodle.org/something/here?' . $CFG->wwwroot;
404 $exception = new moodle_exception('none', 'error', $url);
405 $infos = $this->get_exception_info($exception);
406 $this->assertSame($CFG->wwwroot . '/', $infos->link);
407
408 // Internal link from fromurl.
409 $SESSION->fromurl = $url = $CFG->wwwroot . '/something/here?really=yes';
410 $exception = new moodle_exception('none');
411 $infos = $this->get_exception_info($exception);
412 $this->assertSame($url, $infos->link);
413
414 // Internal HTTPS link from fromurl.
415 $SESSION->fromurl = $url = $httpswwwroot . '/something/here?really=yes';
416 $exception = new moodle_exception('none');
417 $infos = $this->get_exception_info($exception);
418 $this->assertSame($url, $infos->link);
419
420 // Internal HTTPS link from fromurl without login HTTPS.
421 $CFG->loginhttps = false;
422 $SESSION->fromurl = $httpswwwroot . '/something/here?really=yes';
423 $exception = new moodle_exception('none');
424 $infos = $this->get_exception_info($exception);
425 $this->assertSame($CFG->wwwroot . '/', $infos->link);
426
427 // External link from fromurl.
428 $SESSION->fromurl = 'http://moodle.org/something/here?really=yes';
429 $exception = new moodle_exception('none');
430 $infos = $this->get_exception_info($exception);
431 $this->assertSame($CFG->wwwroot . '/', $infos->link);
432
433 // External HTTPS link from fromurl.
434 $SESSION->fromurl = 'https://moodle.org/something/here?really=yes';
435 $exception = new moodle_exception('none');
436 $infos = $this->get_exception_info($exception);
437 $this->assertSame($CFG->wwwroot . '/', $infos->link);
438
439 // External HTTPS link from fromurl with login HTTPS.
440 $CFG->loginhttps = true;
441 $SESSION->fromurl = 'https://moodle.org/something/here?really=yes';
442 $exception = new moodle_exception('none');
443 $infos = $this->get_exception_info($exception);
444 $this->assertSame($CFG->wwwroot . '/', $infos->link);
445
446 $CFG->loginhttps = $initialloginhttps;
447 $SESSION->fromurl = '';
448 }
449
450 /**
451 * Wrapper to call {@link get_exception_info()}.
452 *
453 * @param Exception $ex An exception.
454 * @return stdClass of information.
455 */
456 public function get_exception_info($ex) {
457 try {
458 throw $ex;
459 } catch (moodle_exception $e) {
460 return get_exception_info($e);
461 }
462 }
9708ff95
DP
463
464 public function test_object() {
465 $obj = new object();
466 $this->assertDebuggingCalled("'object' class has been deprecated, please use stdClass instead.");
467 $this->assertInstanceOf('stdClass', $obj);
468 }
afda0506
DP
469
470 /**
471 * Data provider for test_get_real_size().
ce32fa55
DP
472 *
473 * @return array An array of arrays contain test data
afda0506
DP
474 */
475 public function data_for_test_get_real_size() {
476 return array(
477 array('8KB', 8192),
478 array('8Kb', 8192),
479 array('8K', 8192),
480 array('8k', 8192),
481 array('50MB', 52428800),
482 array('50Mb', 52428800),
483 array('50M', 52428800),
484 array('50m', 52428800),
485 array('8Gb', 8589934592),
486 array('8GB', 8589934592),
487 array('8G', 8589934592),
488 );
489 }
490
491 /**
492 * Test the get_real_size() function.
493 *
494 * @dataProvider data_for_test_get_real_size
495 *
496 * @param string $input the input for get_real_size()
497 * @param int $expectedbytes the expected bytes
afda0506
DP
498 */
499 public function test_get_real_size($input, $expectedbytes) {
500 $this->assertEquals($expectedbytes, get_real_size($input));
501 }
32c842e2 502}