Translated using Weblate (Chinese (Simplified))
[phpmyadmin.git] / test / classes / VersionInformationTest.php
blob7430cf6eaa28b8ad1cc4863ce645d888feb15105
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Tests for methods in PhpMyAdmin\VersionInformation class
6 * @package PhpMyAdmin-test
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin\Tests;
12 use PhpMyAdmin\Tests\PmaTestCase;
13 use PhpMyAdmin\VersionInformation;
14 use stdClass;
16 /**
17 * Tests for methods in PhpMyAdmin\VersionInformation class
19 * @package PhpMyAdmin-test
21 class VersionInformationTest extends PmaTestCase
23 private $_releases;
25 /**
26 * Sets up the fixture, for example, opens a network connection.
27 * This method is called before a test is executed.
29 * @return void
31 protected function setUp(): void
33 $this->_releases = [];
35 $release = new stdClass();
36 $release->date = "2015-09-08";
37 $release->php_versions = ">=5.3,<7.1";
38 $release->version = "4.4.14.1";
39 $release->mysql_versions = ">=5.5";
40 $this->_releases[] = $release;
42 $release = new stdClass();
43 $release->date = "2015-09-09";
44 $release->php_versions = ">=5.3,<7.0";
45 $release->version = "4.4.13.3";
46 $release->mysql_versions = ">=5.5";
47 $this->_releases[] = $release;
49 $release = new stdClass();
50 $release->date = "2015-05-13";
51 $release->php_versions = ">=5.2,<5.3";
52 $release->version = "4.0.10.10";
53 $release->mysql_versions = ">=5.0";
54 $this->_releases[] = $release;
57 /**
58 * Test version checking
60 * @return void
62 * @group large
63 * @group network
65 public function testGetLatestVersion()
67 $GLOBALS['cfg']['ProxyUrl'] = PROXY_URL;
68 $GLOBALS['cfg']['ProxyUser'] = PROXY_USER;
69 $GLOBALS['cfg']['ProxyPass'] = PROXY_PASS;
70 $GLOBALS['cfg']['VersionCheck'] = true;
71 $versionInformation = new VersionInformation();
72 $version = $versionInformation->getLatestVersion();
73 $this->assertNotEmpty($version->version);
74 $this->assertNotEmpty($version->date);
77 /**
78 * Test version to int conversion.
80 * @param string $version Version string
81 * @param int $numeric Integer matching version
83 * @return void
85 * @dataProvider dataVersions
87 public function testVersionToInt($version, $numeric): void
89 $versionInformation = new VersionInformation();
90 $this->assertEquals(
91 $numeric,
92 $versionInformation->versionToInt($version)
97 /**
98 * Data provider for version parsing
100 * @return array with test data
102 public function dataVersions()
104 return [
106 '1.0.0',
107 1000050,
110 '2.0.0.2-dev',
111 2000002,
114 '3.4.2.1',
115 3040251,
118 '3.4.2-dev3',
119 3040203,
122 '3.4.2-dev',
123 3040200,
126 '3.4.2-pl',
127 3040260,
130 '3.4.2-pl3',
131 3040263,
134 '4.4.2-rc22',
135 4040252,
138 '4.4.2-rc',
139 4040230,
142 '4.4.22-beta22',
143 4042242,
146 '4.4.22-beta',
147 4042220,
150 '4.4.21-alpha22',
151 4042132,
154 '4.4.20-alpha',
155 4042010,
158 '4.40.20-alpha-dev',
159 4402010,
162 '4.4a',
163 4000050,
166 '4.4.4-test',
167 4040400,
170 '4.1.0',
171 4010050,
174 '4.0.1.3',
175 4000153,
178 '4.1-dev',
179 4010000,
185 * Tests getLatestCompatibleVersion() when there is only one server confgiured
187 * @return void
189 public function testGetLatestCompatibleVersionWithSingleServer()
191 $GLOBALS['cfg']['Servers'] = [
195 $mockVersionInfo = $this->getMockBuilder('PhpMyAdmin\VersionInformation')
196 ->setMethods(['evaluateVersionCondition'])
197 ->getMock();
199 $mockVersionInfo->expects($this->at(0))
200 ->method('evaluateVersionCondition')
201 ->with('PHP', '>=5.3')
202 ->will($this->returnValue(true));
204 $mockVersionInfo->expects($this->at(1))
205 ->method('evaluateVersionCondition')
206 ->with('PHP', '<7.1')
207 ->will($this->returnValue(true));
209 $mockVersionInfo->expects($this->at(2))
210 ->method('evaluateVersionCondition')
211 ->with('MySQL', '>=5.5')
212 ->will($this->returnValue(true));
214 $compatible = $mockVersionInfo
215 ->getLatestCompatibleVersion($this->_releases);
216 $this->assertEquals('4.4.14.1', $compatible['version']);
220 * Tests getLatestCompatibleVersion() when there are multiple servers configured
222 * @return void
224 public function testGetLatestCompatibleVersionWithMultipleServers()
226 $GLOBALS['cfg']['Servers'] = [
231 $mockVersionInfo = $this->getMockBuilder('PhpMyAdmin\VersionInformation')
232 ->setMethods(['evaluateVersionCondition'])
233 ->getMock();
235 $mockVersionInfo->expects($this->at(0))
236 ->method('evaluateVersionCondition')
237 ->with('PHP', '>=5.3')
238 ->will($this->returnValue(true));
240 $mockVersionInfo->expects($this->at(1))
241 ->method('evaluateVersionCondition')
242 ->with('PHP', '<7.1')
243 ->will($this->returnValue(true));
245 $compatible = $mockVersionInfo
246 ->getLatestCompatibleVersion($this->_releases);
247 $this->assertEquals('4.4.14.1', $compatible['version']);
251 * Tests getLatestCompatibleVersion() with an old PHP version
253 * @return void
255 public function testGetLatestCompatibleVersionWithOldPHPVersion()
257 $GLOBALS['cfg']['Servers'] = [
262 $mockVersionInfo = $this->getMockBuilder('PhpMyAdmin\VersionInformation')
263 ->setMethods(['evaluateVersionCondition'])
264 ->getMock();
266 $mockVersionInfo->expects($this->at(0))
267 ->method('evaluateVersionCondition')
268 ->with('PHP', '>=5.3')
269 ->will($this->returnValue(false));
271 $mockVersionInfo->expects($this->at(1))
272 ->method('evaluateVersionCondition')
273 ->with('PHP', '>=5.3')
274 ->will($this->returnValue(false));
276 $mockVersionInfo->expects($this->at(2))
277 ->method('evaluateVersionCondition')
278 ->with('PHP', '>=5.2')
279 ->will($this->returnValue(true));
281 $mockVersionInfo->expects($this->at(3))
282 ->method('evaluateVersionCondition')
283 ->with('PHP', '<5.3')
284 ->will($this->returnValue(true));
286 $compatible = $mockVersionInfo
287 ->getLatestCompatibleVersion($this->_releases);
288 $this->assertEquals('4.0.10.10', $compatible['version']);
293 * Tests getLatestCompatibleVersion() with an new PHP version
295 * @dataProvider dataProviderVersionConditions
296 * @param array[] $versions The versions to use
297 * @param array[] $conditions The conditions that will be executed
298 * @param string|null $matchedLastVersion The version that will be matched
299 * @return void
301 public function testGetLatestCompatibleVersionWithNewPHPVersion(array $versions, array $conditions, ?string $matchedLastVersion): void
303 $GLOBALS['cfg']['Servers'] = [];
305 $mockVersionInfo = $this->getMockBuilder(VersionInformation::class)
306 ->setMethods(['evaluateVersionCondition'])
307 ->getMock();
309 $i = 0;
310 foreach ($conditions as $conditionArray) {
312 $condition,
313 $returnValue,
314 ] = $conditionArray;
315 $mockVersionInfo->expects($this->at($i))
316 ->method('evaluateVersionCondition')
317 ->with('PHP', $condition)
318 ->will($this->returnValue($returnValue));
319 $i++;
321 /** @var VersionInformation $mockVersionInfo */
322 $compatible = $mockVersionInfo->getLatestCompatibleVersion($versions);
323 $this->assertEquals($matchedLastVersion, $compatible['version'] ?? null);
327 * Provider for testGetLatestCompatibleVersionWithNewPHPVersion
328 * Returns the conditions to be used for mocks
329 * @return array[]
331 public function dataProviderVersionConditions(): array
333 return [
336 ((object) [
337 'date' => '2019-12-26',
338 'php_versions' => '>=5.5,<8.0',
339 'version' => '4.9.3',
340 'mysql_versions' => '>=5.5',
342 ((object) [
343 'date' => '2019-12-26',
344 'php_versions' => '>=7.1,<8.0',
345 'version' => '5.0.0',
346 'mysql_versions' => '>=5.5',
351 '>=5.5',
352 true,
355 '<8.0',
356 true,
359 '>=7.1',
360 true,
363 '<8.0',
364 false,
367 '4.9.3',
371 ((object) [
372 'date' => '2019-12-26',
373 'php_versions' => '>=5.5,<7.0',
374 'version' => '6.0.0',
375 'mysql_versions' => '>=5.5',
377 ((object) [
378 'date' => '2019-12-26',
379 'php_versions' => '>=7.1,<8.0',
380 'version' => '5.0.0',
381 'mysql_versions' => '>=5.5',
386 '>=5.5',
387 true,
390 '<7.0',
391 true,
394 '>=7.1',
395 false,
398 '6.0.0',
402 ((object) [
403 'date' => '2019-12-26',
404 'php_versions' => '>=5.5,<7.0',
405 'version' => '6.0.0-rc1',
406 'mysql_versions' => '>=5.5',
408 ((object) [
409 'date' => '2019-12-26',
410 'php_versions' => '>=7.1,<8.0',
411 'version' => '6.0.0-rc2',
412 'mysql_versions' => '>=5.5',
417 '>=5.5',
418 true,
421 '<7.0',
422 true,
425 '>=7.1',
426 false,
429 '6.0.0-rc1',
433 ((object) [
434 'date' => '2019-12-26',
435 'php_versions' => '>=5.5,<7.0',
436 'version' => '6.0.0',
437 'mysql_versions' => '>=5.5',
439 ((object) [
440 'date' => '2019-12-26',
441 'php_versions' => '>=7.1,<8.0',
442 'version' => '5.0.0',
443 'mysql_versions' => '>=5.5',
448 '>=5.5',
449 false,
452 '>=7.1',
453 true,
456 '<8.0',
457 false,
460 null,
464 ((object) [
465 'date' => '2019-12-26',
466 'php_versions' => '>=5.5,<7.0',
467 'version' => '6.0.0',
468 'mysql_versions' => '>=5.5',
470 ((object) [
471 'date' => '2019-12-26',
472 'php_versions' => '>=7.1,<8.0',
473 'version' => '5.0.0',
474 'mysql_versions' => '>=5.5',
479 '>=5.5',
480 false,
483 '>=7.1',
484 true,
487 '<8.0',
488 true,
491 '5.0.0',
495 ((object) [
496 'date' => '2019-12-26',
497 'php_versions' => '>=5.5,<8.0',
498 'version' => '4.9.3',
499 'mysql_versions' => '>=5.5',
501 ((object) [
502 'date' => '2019-12-26',
503 'php_versions' => '>=7.1,<8.0',
504 'version' => '5.0.0',
505 'mysql_versions' => '>=5.5',
510 '>=5.5',
511 true,
514 '<8.0',
515 true,
518 '>=7.1',
519 true,
522 '<8.0',
523 true,
526 '5.0.0',
530 ((object) [
531 'date' => '2019-12-26',
532 'php_versions' => '>=7.1,<8.0',
533 'version' => '5.0.0',
534 'mysql_versions' => '>=5.5',
536 ((object) [
537 'date' => '2019-12-26',
538 'php_versions' => '>=5.5,<8.0',
539 'version' => '4.9.3',
540 'mysql_versions' => '>=5.5',
545 '>=7.1',
546 true,
549 '<8.0',
550 true,
553 '>=5.5',
554 true,
557 '<8.0',
558 true,
561 '5.0.0',
567 * Tests evaluateVersionCondition() method
569 * @return void
571 public function testEvaluateVersionCondition()
573 $mockVersionInfo = $this->getMockBuilder('PhpMyAdmin\VersionInformation')
574 ->setMethods(['getPHPVersion'])
575 ->getMock();
577 $mockVersionInfo->expects($this->any())
578 ->method('getPHPVersion')
579 ->will($this->returnValue('5.2.4'));
581 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.3'));
582 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '<5.3'));
583 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>=5.2'));
584 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '>5.2'));
585 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.3'));
587 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<=5.2'));
588 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '<5.2'));
589 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>=7.0'));
590 $this->assertFalse($mockVersionInfo->evaluateVersionCondition('PHP', '>7.0'));
591 $this->assertTrue($mockVersionInfo->evaluateVersionCondition('PHP', '!=5.2'));