2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for setuplib.php
22 * @copyright 2012 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 * Unit tests for setuplib.php
32 * @copyright 2012 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_setuplib_testcase
extends advanced_testcase
{
38 * Test get_docs_url_standard in the normal case when we should link to Moodle docs.
40 public function test_get_docs_url_standard() {
42 if (empty($CFG->docroot
)) {
43 $docroot = 'http://docs.moodle.org/';
45 $docroot = $CFG->docroot
;
47 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/\d{2}/' . current_language() . '/course/editing$~',
48 get_docs_url('course/editing'));
52 * Test get_docs_url_standard in the special case of an absolute HTTP URL.
54 public function test_get_docs_url_http() {
55 $url = 'http://moodle.org/';
56 $this->assertEquals($url, get_docs_url($url));
60 * Test get_docs_url_standard in the special case of an absolute HTTPS URL.
62 public function test_get_docs_url_https() {
63 $url = 'https://moodle.org/';
64 $this->assertEquals($url, get_docs_url($url));
68 * Test get_docs_url_standard in the special case of a link relative to wwwroot.
70 public function test_get_docs_url_wwwroot() {
72 $this->assertSame($CFG->wwwroot
. '/lib/tests/setuplib_test.php',
73 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php'));
77 * Test if get_exception_info() removes file system paths.
79 public function test_exception_info_removes_serverpaths() {
82 // This doesn't test them all possible ones, but these are set for unit tests.
83 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir', 'localcachedir');
87 foreach ($cfgnames as $cfgname) {
88 if (!empty($CFG->$cfgname)) {
89 $fixture .= $CFG->$cfgname.' ';
90 $expected .= "[$cfgname] ";
93 $exception = new moodle_exception('generalexceptionmessage', 'error', '', $fixture, $fixture);
94 $exceptioninfo = get_exception_info($exception);
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');
100 public function test_localcachedir() {
103 $this->resetAfterTest(true);
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
);
108 $this->setCurrentTimeStart();
109 $timestampfile = "$CFG->localcachedir/.lastpurged";
111 // Delete existing localcache directory, as this is testing first call
112 // to make_localcache_directory.
113 remove_dir($CFG->localcachedir
, true);
114 $dir = make_localcache_directory('', false);
115 $this->assertSame($CFG->localcachedir
, $dir);
116 $this->assertFileNotExists("$CFG->localcachedir/.htaccess");
117 $this->assertFileExists($timestampfile);
118 $this->assertTimeCurrent(filemtime($timestampfile));
120 $dir = make_localcache_directory('test/test', false);
121 $this->assertSame("$CFG->localcachedir/test/test", $dir);
123 // Test custom location.
124 $CFG->localcachedir
= "$CFG->dataroot/testlocalcache";
125 $this->setCurrentTimeStart();
126 $timestampfile = "$CFG->localcachedir/.lastpurged";
127 $this->assertFileNotExists($timestampfile);
129 $dir = make_localcache_directory('', false);
130 $this->assertSame($CFG->localcachedir
, $dir);
131 $this->assertFileExists("$CFG->localcachedir/.htaccess");
132 $this->assertFileExists($timestampfile);
133 $this->assertTimeCurrent(filemtime($timestampfile));
135 $dir = make_localcache_directory('test', false);
136 $this->assertSame("$CFG->localcachedir/test", $dir);
138 $prevtime = filemtime($timestampfile);
139 $dir = make_localcache_directory('pokus', false);
140 $this->assertSame("$CFG->localcachedir/pokus", $dir);
141 $this->assertSame($prevtime, filemtime($timestampfile));
144 $testfile = "$CFG->localcachedir/test/test.txt";
145 $this->assertTrue(touch($testfile));
147 $now = $this->setCurrentTimeStart();
148 set_config('localcachedirpurged', $now - 2);
150 $this->assertFileNotExists($testfile);
151 $this->assertFileNotExists(dirname($testfile));
152 $this->assertFileExists($timestampfile);
153 $this->assertTimeCurrent(filemtime($timestampfile));
154 $this->assertTimeCurrent($CFG->localcachedirpurged
);
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));
162 $this->assertSame($now - 2, filemtime($timestampfile));
164 $this->setCurrentTimeStart();
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);
170 $this->assertTimeCurrent(filemtime($timestampfile));
173 public function test_make_unique_directory_basedir_is_file() {
176 // Start with a file instead of a directory.
177 $base = $CFG->tempdir
. DIRECTORY_SEPARATOR
. md5(microtime(true) +
rand());
180 // First the false test.
181 $this->assertFalse(make_unique_writable_directory($base, false));
183 // Now check for exception.
184 $this->expectException('invalid_dataroot_permissions');
185 $this->expectExceptionMessage($base . ' is not writable. Unable to create a unique directory within it.');
186 make_unique_writable_directory($base);
191 public function test_make_unique_directory() {
194 // Create directories should be both directories, and writable.
195 $firstdir = make_unique_writable_directory($CFG->tempdir
);
196 $this->assertTrue(is_dir($firstdir));
197 $this->assertTrue(is_writable($firstdir));
199 $seconddir = make_unique_writable_directory($CFG->tempdir
);
200 $this->assertTrue(is_dir($seconddir));
201 $this->assertTrue(is_writable($seconddir));
203 // Directories should be different each iteration.
204 $this->assertNotEquals($firstdir, $seconddir);
207 public function test_get_request_storage_directory() {
208 // Making a call to get_request_storage_directory should always give the same result.
209 $firstdir = get_request_storage_directory();
210 $seconddir = get_request_storage_directory();
211 $this->assertTrue(is_dir($firstdir));
212 $this->assertEquals($firstdir, $seconddir);
214 // Removing the directory and calling get_request_storage_directory() again should cause a new directory to be created.
215 remove_dir($firstdir);
216 $this->assertFalse(file_exists($firstdir));
217 $this->assertFalse(is_dir($firstdir));
219 $thirddir = get_request_storage_directory();
220 $this->assertTrue(is_dir($thirddir));
221 $this->assertNotEquals($firstdir, $thirddir);
223 // Removing it and replacing it with a file should cause it to be regenerated again.
224 remove_dir($thirddir);
225 $this->assertFalse(file_exists($thirddir));
226 $this->assertFalse(is_dir($thirddir));
228 $this->assertTrue(file_exists($thirddir));
229 $this->assertFalse(is_dir($thirddir));
231 $fourthdir = get_request_storage_directory();
232 $this->assertTrue(is_dir($fourthdir));
233 $this->assertNotEquals($thirddir, $fourthdir);
237 public function test_make_request_directory() {
238 // Every request directory should be unique.
239 $firstdir = make_request_directory();
240 $seconddir = make_request_directory();
241 $thirddir = make_request_directory();
242 $fourthdir = make_request_directory();
244 $this->assertNotEquals($firstdir, $seconddir);
245 $this->assertNotEquals($firstdir, $thirddir);
246 $this->assertNotEquals($firstdir, $fourthdir);
247 $this->assertNotEquals($seconddir, $thirddir);
248 $this->assertNotEquals($seconddir, $fourthdir);
249 $this->assertNotEquals($thirddir, $fourthdir);
251 // They should also all be within the request storage directory.
252 $requestdir = get_request_storage_directory();
253 $this->assertEquals(0, strpos($firstdir, $requestdir));
254 $this->assertEquals(0, strpos($seconddir, $requestdir));
255 $this->assertEquals(0, strpos($thirddir, $requestdir));
256 $this->assertEquals(0, strpos($fourthdir, $requestdir));
258 // Removing the requestdir should mean that new request directories are still created successfully.
259 remove_dir($requestdir);
260 $this->assertFalse(file_exists($requestdir));
261 $this->assertFalse(is_dir($requestdir));
263 $fifthdir = make_request_directory();
264 $this->assertNotEquals($firstdir, $fifthdir);
265 $this->assertNotEquals($seconddir, $fifthdir);
266 $this->assertNotEquals($thirddir, $fifthdir);
267 $this->assertNotEquals($fourthdir, $fifthdir);
268 $this->assertTrue(is_dir($fifthdir));
269 $this->assertFalse(strpos($fifthdir, $requestdir));
271 // And it should be within the new request directory.
272 $newrequestdir = get_request_storage_directory();
273 $this->assertEquals(0, strpos($fifthdir, $newrequestdir));
276 public function test_merge_query_params() {
280 'action' => 'delete',
293 'numerical' => array(
294 '2' => array('a' => 'b'),
300 'numerical' => array(
302 '2' => array('d' => 'e'),
304 'action' => 'create',
317 'action' => 'create',
331 'numerical' => array(
332 '2' => array('a' => 'b', 'd' => 'e'),
340 merge_query_params($array, $chunk);
342 $this->assertSame($expected, $array);
343 $this->assertNotSame($original, $array);
345 $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 parse_str($query, $decoded);
348 $this->assertSame($expected, $decoded);
350 // Prove that we cannot use array_merge_recursive() instead.
351 $this->assertNotSame($expected, array_merge_recursive($original, $chunk));
355 * Test the link processed by get_exception_info().
357 public function test_get_exception_info_link() {
358 global $CFG, $SESSION;
360 $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot
);
363 $url = $CFG->wwwroot
. '/something/here?really=yes';
364 $exception = new moodle_exception('none', 'error', $url);
365 $infos = $this->get_exception_info($exception);
366 $this->assertSame($url, $infos->link
);
368 // Relative local URL.
369 $url = '/something/here?really=yes';
370 $exception = new moodle_exception('none', 'error', $url);
371 $infos = $this->get_exception_info($exception);
372 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
374 // HTTPS URL when login HTTPS is not enabled (default) and site is HTTP.
375 $CFG->wwwroot
= str_replace('https:', 'http:', $CFG->wwwroot
);
376 $url = $httpswwwroot . '/something/here?really=yes';
377 $exception = new moodle_exception('none', 'error', $url);
378 $infos = $this->get_exception_info($exception);
379 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
381 // HTTPS URL when login HTTPS is not enabled and site is HTTPS.
382 $CFG->wwwroot
= str_replace('http:', 'https:', $CFG->wwwroot
);
383 $url = $httpswwwroot . '/something/here?really=yes';
384 $exception = new moodle_exception('none', 'error', $url);
385 $infos = $this->get_exception_info($exception);
386 $this->assertSame($url, $infos->link
);
388 // External HTTP URL.
389 $url = 'http://moodle.org/something/here?really=yes';
390 $exception = new moodle_exception('none', 'error', $url);
391 $infos = $this->get_exception_info($exception);
392 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
394 // External HTTPS URL.
395 $url = 'https://moodle.org/something/here?really=yes';
396 $exception = new moodle_exception('none', 'error', $url);
397 $infos = $this->get_exception_info($exception);
398 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
400 // External URL containing local URL.
401 $url = 'http://moodle.org/something/here?' . $CFG->wwwroot
;
402 $exception = new moodle_exception('none', 'error', $url);
403 $infos = $this->get_exception_info($exception);
404 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
406 // Internal link from fromurl.
407 $SESSION->fromurl
= $url = $CFG->wwwroot
. '/something/here?really=yes';
408 $exception = new moodle_exception('none');
409 $infos = $this->get_exception_info($exception);
410 $this->assertSame($url, $infos->link
);
412 // Internal HTTPS link from fromurl.
413 $SESSION->fromurl
= $url = $httpswwwroot . '/something/here?really=yes';
414 $exception = new moodle_exception('none');
415 $infos = $this->get_exception_info($exception);
416 $this->assertSame($url, $infos->link
);
418 // External link from fromurl.
419 $SESSION->fromurl
= 'http://moodle.org/something/here?really=yes';
420 $exception = new moodle_exception('none');
421 $infos = $this->get_exception_info($exception);
422 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
424 // External HTTPS link from fromurl.
425 $SESSION->fromurl
= 'https://moodle.org/something/here?really=yes';
426 $exception = new moodle_exception('none');
427 $infos = $this->get_exception_info($exception);
428 $this->assertSame($CFG->wwwroot
. '/', $infos->link
);
430 $SESSION->fromurl
= '';
434 * Wrapper to call {@link get_exception_info()}.
436 * @param Exception $ex An exception.
437 * @return stdClass of information.
439 public function get_exception_info($ex) {
442 } catch (moodle_exception
$e) {
443 return get_exception_info($e);
448 * Data provider for test_get_real_size().
450 * @return array An array of arrays contain test data
452 public function data_for_test_get_real_size() {
458 array('50MB', 52428800),
459 array('50Mb', 52428800),
460 array('50M', 52428800),
461 array('50m', 52428800),
462 array('8Gb', 8589934592),
463 array('8GB', 8589934592),
464 array('8G', 8589934592),
469 * Test the get_real_size() function.
471 * @dataProvider data_for_test_get_real_size
473 * @param string $input the input for get_real_size()
474 * @param int $expectedbytes the expected bytes
476 public function test_get_real_size($input, $expectedbytes) {
477 $this->assertEquals($expectedbytes, get_real_size($input));