[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * 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 24 */ 25 26 defined('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 */ 35 class core_setuplib_testcase extends advanced_testcase { 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 } 47 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/\d{2}/' . current_language() . '/course/editing$~', 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; 72 $this->assertSame($CFG->wwwroot . '/lib/tests/setuplib_test.php', 73 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php')); 74 } 75 76 /** 77 * Test if get_exception_info() removes file system paths. 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. 83 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir', 'localcachedir'); 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 } 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 108 $this->setCurrentTimeStart(); 109 $timestampfile = "$CFG->localcachedir/.lastpurged"; 110 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)); 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"; 125 $this->setCurrentTimeStart(); 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); 133 $this->assertTimeCurrent(filemtime($timestampfile)); 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 143 // Test purging. 144 $testfile = "$CFG->localcachedir/test/test.txt"; 145 $this->assertTrue(touch($testfile)); 146 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); 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 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)); 171 } 172 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->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); 187 188 unlink($base); 189 } 190 191 public function test_make_unique_directory() { 192 global $CFG; 193 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)); 198 199 $seconddir = make_unique_writable_directory($CFG->tempdir); 200 $this->assertTrue(is_dir($seconddir)); 201 $this->assertTrue(is_writable($seconddir)); 202 203 // Directories should be different each iteration. 204 $this->assertNotEquals($firstdir, $seconddir); 205 } 206 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); 213 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)); 218 219 $thirddir = get_request_storage_directory(); 220 $this->assertTrue(is_dir($thirddir)); 221 $this->assertNotEquals($firstdir, $thirddir); 222 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)); 227 touch($thirddir); 228 $this->assertTrue(file_exists($thirddir)); 229 $this->assertFalse(is_dir($thirddir)); 230 231 $fourthdir = get_request_storage_directory(); 232 $this->assertTrue(is_dir($fourthdir)); 233 $this->assertNotEquals($thirddir, $fourthdir); 234 } 235 236 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(); 243 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); 250 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)); 257 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)); 262 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)); 270 271 // And it should be within the new request directory. 272 $newrequestdir = get_request_storage_directory(); 273 $this->assertEquals(0, strpos($fifthdir, $newrequestdir)); 274 } 275 276 public function test_merge_query_params() { 277 $original = array( 278 'id' => '1', 279 'course' => '2', 280 'action' => 'delete', 281 'grade' => array( 282 0 => 'a', 283 1 => 'b', 284 2 => 'c', 285 ), 286 'items' => array( 287 'a' => 'aa', 288 'b' => 'bb', 289 ), 290 'mix' => array( 291 0 => '2', 292 ), 293 'numerical' => array( 294 '2' => array('a' => 'b'), 295 '1' => '2', 296 ), 297 ); 298 299 $chunk = array( 300 'numerical' => array( 301 '0' => 'z', 302 '2' => array('d' => 'e'), 303 ), 304 'action' => 'create', 305 'next' => '2', 306 'grade' => array( 307 0 => 'e', 308 1 => 'f', 309 2 => 'g', 310 ), 311 'mix' => 'mix', 312 ); 313 314 $expected = array( 315 'id' => '1', 316 'course' => '2', 317 'action' => 'create', 318 'grade' => array( 319 0 => 'a', 320 1 => 'b', 321 2 => 'c', 322 3 => 'e', 323 4 => 'f', 324 5 => 'g', 325 ), 326 'items' => array( 327 'a' => 'aa', 328 'b' => 'bb', 329 ), 330 'mix' => 'mix', 331 'numerical' => array( 332 '2' => array('a' => 'b', 'd' => 'e'), 333 '1' => '2', 334 '0' => 'z', 335 ), 336 'next' => '2', 337 ); 338 339 $array = $original; 340 merge_query_params($array, $chunk); 341 342 $this->assertSame($expected, $array); 343 $this->assertNotSame($original, $array); 344 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"; 346 $decoded = array(); 347 parse_str($query, $decoded); 348 $this->assertSame($expected, $decoded); 349 350 // Prove that we cannot use array_merge_recursive() instead. 351 $this->assertNotSame($expected, array_merge_recursive($original, $chunk)); 352 } 353 354 /** 355 * Test the link processed by get_exception_info(). 356 */ 357 public function test_get_exception_info_link() { 358 global $CFG, $SESSION; 359 360 $initialloginhttps = $CFG->loginhttps; 361 $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot); 362 $CFG->loginhttps = false; 363 364 // Simple local URL. 365 $url = $CFG->wwwroot . '/something/here?really=yes'; 366 $exception = new moodle_exception('none', 'error', $url); 367 $infos = $this->get_exception_info($exception); 368 $this->assertSame($url, $infos->link); 369 370 // Relative local URL. 371 $url = '/something/here?really=yes'; 372 $exception = new moodle_exception('none', 'error', $url); 373 $infos = $this->get_exception_info($exception); 374 $this->assertSame($CFG->wwwroot . '/', $infos->link); 375 376 // HTTPS URL when login HTTPS is not enabled. 377 $url = $httpswwwroot . '/something/here?really=yes'; 378 $exception = new moodle_exception('none', 'error', $url); 379 $infos = $this->get_exception_info($exception); 380 $this->assertSame($CFG->wwwroot . '/', $infos->link); 381 382 // HTTPS URL with login HTTPS. 383 $CFG->loginhttps = true; 384 $url = $httpswwwroot . '/something/here?really=yes'; 385 $exception = new moodle_exception('none', 'error', $url); 386 $infos = $this->get_exception_info($exception); 387 $this->assertSame($url, $infos->link); 388 389 // External HTTP URL. 390 $url = 'http://moodle.org/something/here?really=yes'; 391 $exception = new moodle_exception('none', 'error', $url); 392 $infos = $this->get_exception_info($exception); 393 $this->assertSame($CFG->wwwroot . '/', $infos->link); 394 395 // External HTTPS URL. 396 $url = 'https://moodle.org/something/here?really=yes'; 397 $exception = new moodle_exception('none', 'error', $url); 398 $infos = $this->get_exception_info($exception); 399 $this->assertSame($CFG->wwwroot . '/', $infos->link); 400 401 // External URL containing local URL. 402 $url = 'http://moodle.org/something/here?' . $CFG->wwwroot; 403 $exception = new moodle_exception('none', 'error', $url); 404 $infos = $this->get_exception_info($exception); 405 $this->assertSame($CFG->wwwroot . '/', $infos->link); 406 407 // Internal link from fromurl. 408 $SESSION->fromurl = $url = $CFG->wwwroot . '/something/here?really=yes'; 409 $exception = new moodle_exception('none'); 410 $infos = $this->get_exception_info($exception); 411 $this->assertSame($url, $infos->link); 412 413 // Internal HTTPS link from fromurl. 414 $SESSION->fromurl = $url = $httpswwwroot . '/something/here?really=yes'; 415 $exception = new moodle_exception('none'); 416 $infos = $this->get_exception_info($exception); 417 $this->assertSame($url, $infos->link); 418 419 // Internal HTTPS link from fromurl without login HTTPS. 420 $CFG->loginhttps = false; 421 $SESSION->fromurl = $httpswwwroot . '/something/here?really=yes'; 422 $exception = new moodle_exception('none'); 423 $infos = $this->get_exception_info($exception); 424 $this->assertSame($CFG->wwwroot . '/', $infos->link); 425 426 // External link from fromurl. 427 $SESSION->fromurl = 'http://moodle.org/something/here?really=yes'; 428 $exception = new moodle_exception('none'); 429 $infos = $this->get_exception_info($exception); 430 $this->assertSame($CFG->wwwroot . '/', $infos->link); 431 432 // External HTTPS link from fromurl. 433 $SESSION->fromurl = 'https://moodle.org/something/here?really=yes'; 434 $exception = new moodle_exception('none'); 435 $infos = $this->get_exception_info($exception); 436 $this->assertSame($CFG->wwwroot . '/', $infos->link); 437 438 // External HTTPS link from fromurl with login HTTPS. 439 $CFG->loginhttps = true; 440 $SESSION->fromurl = 'https://moodle.org/something/here?really=yes'; 441 $exception = new moodle_exception('none'); 442 $infos = $this->get_exception_info($exception); 443 $this->assertSame($CFG->wwwroot . '/', $infos->link); 444 445 $CFG->loginhttps = $initialloginhttps; 446 $SESSION->fromurl = ''; 447 } 448 449 /** 450 * Wrapper to call {@link get_exception_info()}. 451 * 452 * @param Exception $ex An exception. 453 * @return stdClass of information. 454 */ 455 public function get_exception_info($ex) { 456 try { 457 throw $ex; 458 } catch (moodle_exception $e) { 459 return get_exception_info($e); 460 } 461 } 462 463 public function test_object() { 464 $obj = new object(); 465 $this->assertDebuggingCalled("'object' class has been deprecated, please use stdClass instead."); 466 $this->assertInstanceOf('stdClass', $obj); 467 } 468 469 /** 470 * Data provider for test_get_real_size(). 471 * 472 * @return array An array of arrays contain test data 473 */ 474 public function data_for_test_get_real_size() { 475 return array( 476 array('8KB', 8192), 477 array('8Kb', 8192), 478 array('8K', 8192), 479 array('8k', 8192), 480 array('50MB', 52428800), 481 array('50Mb', 52428800), 482 array('50M', 52428800), 483 array('50m', 52428800), 484 array('8Gb', 8589934592), 485 array('8GB', 8589934592), 486 array('8G', 8589934592), 487 ); 488 } 489 490 /** 491 * Test the get_real_size() function. 492 * 493 * @dataProvider data_for_test_get_real_size 494 * 495 * @param string $input the input for get_real_size() 496 * @param int $expectedbytes the expected bytes 497 */ 498 public function test_get_real_size($input, $expectedbytes) { 499 $this->assertEquals($expectedbytes, get_real_size($input)); 500 } 501 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |