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