Merge branch 'MDL-52763-30' of git://github.com/danpoltawski/moodle into MOODLE_30_STABLE
[moodle.git] / lib / tests / setuplib_test.php
blobb46cd6450923a5fbce36035c4681c2e9d35c9dd2
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 setuplib.php
20 * @package core
21 * @category phpunit
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();
29 /**
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 {
37 /**
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() {
41 global $CFG;
42 if (empty($CFG->docroot)) {
43 $docroot = 'http://docs.moodle.org/';
44 } else {
45 $docroot = $CFG->docroot;
47 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/\d{2}/' . current_language() . '/course/editing$~',
48 get_docs_url('course/editing'));
51 /**
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));
59 /**
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));
67 /**
68 * Test get_docs_url_standard in the special case of a link relative to wwwroot.
70 public function test_get_docs_url_wwwroot() {
71 global $CFG;
72 $this->assertSame($CFG->wwwroot . '/lib/tests/setuplib_test.php',
73 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php'));
76 /**
77 * Test if get_exception_info() removes file system paths.
79 public function test_exception_info_removes_serverpaths() {
80 global $CFG;
82 // This doesn't test them all possible ones, but these are set for unit tests.
83 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir', 'localcachedir');
85 $fixture = '';
86 $expected = '';
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() {
101 global $CFG;
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));
143 // Test purging.
144 $testfile = "$CFG->localcachedir/test/test.txt";
145 $this->assertTrue(touch($testfile));
147 $now = $this->setCurrentTimeStart();
148 set_config('localcachedirpurged', $now - 2);
149 purge_all_caches();
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));
161 clearstatcache();
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() {
174 global $CFG;
176 // Start with a file instead of a directory.
177 $base = $CFG->tempdir . DIRECTORY_SEPARATOR . md5(microtime() + rand());
178 touch($base);
180 // First the false test.
181 $this->assertFalse(make_unique_writable_directory($base, false));
183 // Now check for exception.
184 $this->setExpectedException('invalid_dataroot_permissions',
185 $base . ' is not writable. Unable to create a unique directory within it.'
187 make_unique_writable_directory($base);
189 unlink($base);
192 public function test_make_unique_directory() {
193 global $CFG;
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));
200 $seconddir = make_unique_writable_directory($CFG->tempdir);
201 $this->assertTrue(is_dir($seconddir));
202 $this->assertTrue(is_writable($seconddir));
204 // Directories should be different each iteration.
205 $this->assertNotEquals($firstdir, $seconddir);
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);
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));
220 $thirddir = get_request_storage_directory();
221 $this->assertTrue(is_dir($thirddir));
222 $this->assertNotEquals($firstdir, $thirddir);
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));
232 $fourthdir = get_request_storage_directory();
233 $this->assertTrue(is_dir($fourthdir));
234 $this->assertNotEquals($thirddir, $fourthdir);
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();
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);
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));
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));
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));
272 // And it should be within the new request directory.
273 $newrequestdir = get_request_storage_directory();
274 $this->assertEquals(0, strpos($fifthdir, $newrequestdir));
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',
287 'items' => array(
288 'a' => 'aa',
289 'b' => 'bb',
291 'mix' => array(
292 0 => '2',
294 'numerical' => array(
295 '2' => array('a' => 'b'),
296 '1' => '2',
300 $chunk = array(
301 'numerical' => array(
302 '0' => 'z',
303 '2' => array('d' => 'e'),
305 'action' => 'create',
306 'next' => '2',
307 'grade' => array(
308 0 => 'e',
309 1 => 'f',
310 2 => 'g',
312 'mix' => 'mix',
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',
327 'items' => array(
328 'a' => 'aa',
329 'b' => 'bb',
331 'mix' => 'mix',
332 'numerical' => array(
333 '2' => array('a' => 'b', 'd' => 'e'),
334 '1' => '2',
335 '0' => 'z',
337 'next' => '2',
340 $array = $original;
341 merge_query_params($array, $chunk);
343 $this->assertSame($expected, $array);
344 $this->assertNotSame($original, $array);
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);
351 // Prove that we cannot use array_merge_recursive() instead.
352 $this->assertNotSame($expected, array_merge_recursive($original, $chunk));
356 * Test the link processed by get_exception_info().
358 public function test_get_exception_info_link() {
359 global $CFG, $SESSION;
361 $initialloginhttps = $CFG->loginhttps;
362 $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
363 $CFG->loginhttps = false;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
446 $CFG->loginhttps = $initialloginhttps;
447 $SESSION->fromurl = '';
451 * Wrapper to call {@link get_exception_info()}.
453 * @param Exception $ex An exception.
454 * @return stdClass of information.
456 public function get_exception_info($ex) {
457 try {
458 throw $ex;
459 } catch (moodle_exception $e) {
460 return get_exception_info($e);