|
21 | 21 | // Global $CFG not used here intentionally to make sure it is not required inside the lib.
|
22 | 22 | require_once(__DIR__ . '/../configonlylib.php');
|
23 | 23 |
|
24 |
| - |
25 | 24 | /**
|
26 | 25 | * Unit tests for config only library functions.
|
27 | 26 | *
|
@@ -144,4 +143,76 @@ public function test_min_get_slash_argument() {
|
144 | 143 | // Windows dir separators are removed, multiple ... gets collapsed to one .
|
145 | 144 | $this->assertSame('/standardcore/./5/u/f1', min_get_slash_argument());
|
146 | 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 = 1693612800; // Equivalent to 20230902 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 | + } |
147 | 218 | }
|
0 commit comments