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