Differences Between: [Versions 310 and 311] [Versions 311 and 400] [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 defined('MOODLE_INTERNAL') || die(); 20 21 // Global $CFG not used here intentionally to make sure it is not required inside the lib. 22 require_once (__DIR__ . '/../configonlylib.php'); 23 24 /** 25 * Unit tests for config only library functions. 26 * 27 * @package core 28 * @category test 29 * @copyright 2012 Petr Skoda {@link http://skodak.org} 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class configonlylib_test extends \advanced_testcase { 33 34 /** 35 * Test cleaning of invalid utf-8 entities. 36 */ 37 public function test_min_fix_utf8() { 38 $this->assertSame('abc', min_fix_utf8('abc')); 39 $this->assertSame("žlutý koníček přeskočil potůček \n\t\r", min_fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0")); 40 $this->assertSame('aš', min_fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.'); 41 } 42 43 /** 44 * Test minimalistic parameter cleaning. 45 */ 46 public function test_min_clean_param() { 47 $this->assertSame('foo', min_clean_param('foo', 'RAW')); 48 $this->assertSame('aš', min_clean_param('a'.chr(130).'š', 'RAW')); 49 50 $this->assertSame(1, min_clean_param('1', 'INT')); 51 $this->assertSame(1, min_clean_param('1aa', 'INT')); 52 53 $this->assertSame('1abc-d_f', min_clean_param('/.1ačž"b?;c-d{}\\_f.', 'SAFEDIR')); 54 $this->assertSame(1, min_clean_param('1aa', 'INT')); 55 56 $this->assertSame('/a/b/./c5', min_clean_param('/a*?$//b/.../c5', 'SAFEPATH')); 57 $this->assertSame(1, min_clean_param('1aa', 'INT')); 58 } 59 60 /** 61 * Test minimalistic getting of page parameters. 62 */ 63 public function test_min_optional_param() { 64 $this->resetAfterTest(); 65 66 $_GET['foo'] = 'bar'; 67 $_GET['num'] = '1'; 68 $_GET['xnum'] = '1aa'; 69 70 $_POST['foo'] = 'rebar'; 71 $_POST['oof'] = 'rab'; 72 73 $this->assertSame('bar', min_optional_param('foo', null, 'RAW')); 74 $this->assertSame(null, min_optional_param('foo2', null, 'RAW')); 75 $this->assertSame('rab', min_optional_param('oof', null, 'RAW')); 76 77 $this->assertSame(1, min_optional_param('num', null, 'INT')); 78 $this->assertSame(1, min_optional_param('xnum', null, 'INT')); 79 } 80 81 /** 82 * Test fail-safe minimalistic slashargument processing. 83 */ 84 public function test_min_get_slash_argument() { 85 global $CFG; 86 87 $this->resetAfterTest(); 88 $this->assertEquals('https://www.example.com/moodle', $CFG->wwwroot); 89 90 $_SERVER = array(); 91 $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)'; 92 $_SERVER['QUERY_STRING'] = 'theme=standard&component=core&rev=5&image=u/f1'; 93 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php?theme=standard&component=core&rev=5&image=u/f1'; 94 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 95 $this->assertSame('', min_get_slash_argument()); 96 97 $_SERVER = array(); 98 $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)'; 99 $_SERVER['QUERY_STRING'] = ''; 100 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 101 $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1'; 102 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 103 $_GET = array(); 104 $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument()); 105 106 // IIS no url rewriting. 107 $_SERVER = array(); 108 $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0'; 109 $_SERVER['QUERY_STRING'] = ''; 110 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 111 $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1'; 112 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 113 $_GET = array(); 114 $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument()); 115 116 // IIS with url rewriting. 117 $_SERVER = array(); 118 $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0'; 119 $_SERVER['QUERY_STRING'] = 'file=/standard/core/5/u/f1'; 120 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 121 $_SERVER['PATH_INFO'] = '/'; 122 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 123 $_GET = array(); 124 $_GET['file'] = '/standard/core/5/u/f1'; 125 $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument()); 126 127 $_SERVER = array(); 128 $_SERVER['SERVER_SOFTWARE'] = 'Weird server'; 129 $_SERVER['QUERY_STRING'] = ''; 130 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 131 $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 132 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 133 $_GET = array(); 134 $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument()); 135 136 $_SERVER = array(); 137 $_SERVER['SERVER_SOFTWARE'] = 'Hacker server'; 138 $_SERVER['QUERY_STRING'] = ''; 139 $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1'; 140 $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard\\core/..\\../5/u/f1'; 141 $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php'; 142 $_GET = array(); 143 // Windows dir separators are removed, multiple ... gets collapsed to one . 144 $this->assertSame('/standardcore/./5/u/f1', min_get_slash_argument()); 145 } 146 147 /** 148 * Test the min_get_minimum_version function. 149 * 150 * @covers ::min_get_minimum_version 151 */ 152 public function test_min_get_minimum_version(): void { 153 // This is fairly hard to write a test for, but we can at least check that it returns a number 154 // greater than the version when the feature was first introduced. 155 $firstintroduced = 1621209600; // Equivalent to 20210517 00:00:00 GMT. 156 $this->assertGreaterThanOrEqual($firstintroduced, min_get_minimum_revision()); 157 } 158 159 /** 160 * Test the min_get_maximum_version function. 161 * 162 * @covers ::min_get_maximum_version 163 */ 164 public function test_min_get_maximum_version(): void { 165 // The maximum version should be set to a time in the near future. 166 // This is currently defined as "in the next minute". 167 // Note: We use a 65 second window to allow for slow test runners. 168 $this->assertGreaterThan(time(), min_get_maximum_revision()); 169 $this->assertLessThanOrEqual(time() + 65, min_get_maximum_revision()); 170 } 171 172 /** 173 * Test the min_is_revision_valid_and_current function. 174 * 175 * @covers ::min_is_revision_valid_and_current 176 * @dataProvider min_is_revision_valid_and_current_provider 177 */ 178 public function test_min_is_revision_valid_and_current(int $revision, bool $expected): void { 179 $this->assertEquals($expected, min_is_revision_valid_and_current($revision)); 180 } 181 182 /** 183 * Data provider for the min_is_revision_valid_and_current tests. 184 * 185 * @return array 186 */ 187 public function min_is_revision_valid_and_current_provider(): array { 188 return [ 189 'Negative value' => [-1, false], 190 'Empty value' => [0, false], 191 'A time before the minimum accepted value' => [min_get_minimum_revision() - 1, false], 192 'The minimum accepted value' => [min_get_minimum_revision(), true], 193 'The current time' => [time(), true], 194 // Note: We have to be careful using time values because the data provider is run at the start of the phpunit run, 195 // but the test may not be run for some time. 196 // On a slower machine and/or database, this could be several hours. 197 // For a more specific time we must have a specific test function. 198 'A time in the future' => [time() + DAYSECS, false] 199 ]; 200 } 201 202 203 /** 204 * Test the min_is_revision_valid_and_current function with close times. 205 * 206 * Note: These tests are incompatible with data providers. 207 * 208 * @covers ::min_is_revision_valid_and_current 209 */ 210 public function test_min_is_revision_valid_and_current_close_proximity(): void { 211 // A time in the near future. 212 $this->assertTrue(min_is_revision_valid_and_current(time() + 55)); 213 214 // A time in the too-far future. 215 $this->assertFalse(min_is_revision_valid_and_current(time() + 70)); 216 217 } 218 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body