PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/test/classes/ConfigTest.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 1351 lines | 1040 code | 93 blank | 218 comment | 23 complexity | b3ac55181ff5b2771c545d624a8858cb MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Tests;
  4. use PhpMyAdmin\Config;
  5. use PhpMyAdmin\Config\Settings;
  6. use PhpMyAdmin\DatabaseInterface;
  7. use function array_merge;
  8. use function array_replace_recursive;
  9. use function define;
  10. use function defined;
  11. use function file_exists;
  12. use function file_put_contents;
  13. use function fileperms;
  14. use function function_exists;
  15. use function gd_info;
  16. use function mb_strstr;
  17. use function ob_end_clean;
  18. use function ob_get_contents;
  19. use function ob_start;
  20. use function phpinfo;
  21. use function preg_match;
  22. use function realpath;
  23. use function strip_tags;
  24. use function stristr;
  25. use function sys_get_temp_dir;
  26. use function tempnam;
  27. use function unlink;
  28. use const DIRECTORY_SEPARATOR;
  29. use const INFO_MODULES;
  30. use const PHP_EOL;
  31. use const PHP_OS;
  32. /**
  33. * @covers \PhpMyAdmin\Config
  34. */
  35. class ConfigTest extends AbstractTestCase
  36. {
  37. /** @var Config */
  38. protected $object;
  39. /** @var Config to test file permission */
  40. protected $permTestObj;
  41. /**
  42. * Sets up the fixture, for example, opens a network connection.
  43. * This method is called before a test is executed.
  44. */
  45. protected function setUp(): void
  46. {
  47. parent::setUp();
  48. parent::setTheme();
  49. $_SERVER['HTTP_USER_AGENT'] = '';
  50. $this->object = new Config();
  51. $GLOBALS['server'] = 0;
  52. $_SESSION['git_location'] = '.git';
  53. $_SESSION['is_git_revision'] = true;
  54. $GLOBALS['config'] = new Config(CONFIG_FILE);
  55. $GLOBALS['cfg']['ProxyUrl'] = '';
  56. //for testing file permissions
  57. $this->permTestObj = new Config(ROOT_PATH . 'config.sample.inc.php');
  58. }
  59. /**
  60. * Tears down the fixture, for example, closes a network connection.
  61. * This method is called after a test is executed.
  62. */
  63. protected function tearDown(): void
  64. {
  65. parent::tearDown();
  66. unset($this->object);
  67. unset($this->permTestObj);
  68. }
  69. /**
  70. * Test for load
  71. */
  72. public function testLoadConfigs(): void
  73. {
  74. $defaultConfig = new Config();
  75. $tmpConfig = tempnam('./', 'config.test.inc.php');
  76. if ($tmpConfig === false) {
  77. $this->markTestSkipped('Creating a temporary file does not work');
  78. }
  79. $this->assertFileExists($tmpConfig);
  80. // end of setup
  81. // Test loading an empty file does not change the default config
  82. $config = new Config($tmpConfig);
  83. $this->assertSame($defaultConfig->settings, $config->settings);
  84. $contents = '<?php' . PHP_EOL
  85. . '$cfg[\'ProtectBinary\'] = true;';
  86. file_put_contents($tmpConfig, $contents);
  87. // Test loading a config changes the setup
  88. $config = new Config($tmpConfig);
  89. $defaultConfig->settings['ProtectBinary'] = true;
  90. $this->assertSame($defaultConfig->settings, $config->settings);
  91. $defaultConfig->settings['ProtectBinary'] = 'blob';
  92. // Teardown
  93. unlink($tmpConfig);
  94. $this->assertFalse(file_exists($tmpConfig));
  95. }
  96. /**
  97. * Test for load
  98. */
  99. public function testLoadInvalidConfigs(): void
  100. {
  101. $defaultConfig = new Config();
  102. $tmpConfig = tempnam('./', 'config.test.inc.php');
  103. if ($tmpConfig === false) {
  104. $this->markTestSkipped('Creating a temporary file does not work');
  105. }
  106. $this->assertFileExists($tmpConfig);
  107. // end of setup
  108. // Test loading an empty file does not change the default config
  109. $config = new Config($tmpConfig);
  110. $this->assertSame($defaultConfig->settings, $config->settings);
  111. $contents = '<?php' . PHP_EOL
  112. . '$cfg[\'fooBar\'] = true;';
  113. file_put_contents($tmpConfig, $contents);
  114. // Test loading a custom key config changes the setup
  115. $config = new Config($tmpConfig);
  116. $defaultConfig->settings['fooBar'] = true;
  117. // Equals because of the key sorting
  118. $this->assertEquals($defaultConfig->settings, $config->settings);
  119. unset($defaultConfig->settings['fooBar']);
  120. $contents = '<?php' . PHP_EOL
  121. . '$cfg[\'/InValidKey\'] = true;' . PHP_EOL
  122. . '$cfg[\'In/ValidKey\'] = true;' . PHP_EOL
  123. . '$cfg[\'/InValid/Key\'] = true;' . PHP_EOL
  124. . '$cfg[\'In/Valid/Key\'] = true;' . PHP_EOL
  125. . '$cfg[\'ValidKey\'] = true;';
  126. file_put_contents($tmpConfig, $contents);
  127. // Test loading a custom key config changes the setup
  128. $config = new Config($tmpConfig);
  129. $defaultConfig->settings['ValidKey'] = true;
  130. // Equals because of the key sorting
  131. $this->assertEquals($defaultConfig->settings, $config->settings);
  132. unset($defaultConfig->settings['ValidKey']);
  133. // Teardown
  134. unlink($tmpConfig);
  135. $this->assertFalse(file_exists($tmpConfig));
  136. }
  137. /**
  138. * Test for CheckSystem
  139. *
  140. * @group medium
  141. */
  142. public function testCheckSystem(): void
  143. {
  144. $this->object->checkSystem();
  145. $this->assertIsBool($this->object->get('PMA_IS_WINDOWS'));
  146. }
  147. /**
  148. * Test for checkOutputCompression
  149. */
  150. public function testCheckOutputCompression(): void
  151. {
  152. $this->object->set('OBGzip', 'auto');
  153. $this->object->set('PMA_USR_BROWSER_AGENT', 'IE');
  154. $this->object->set('PMA_USR_BROWSER_VER', 6);
  155. $this->object->checkOutputCompression();
  156. $this->assertTrue($this->object->get('OBGzip'));
  157. $this->object->set('OBGzip', 'auto');
  158. $this->object->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
  159. $this->object->set('PMA_USR_BROWSER_VER', 5);
  160. $this->object->checkOutputCompression();
  161. $this->assertTrue($this->object->get('OBGzip'));
  162. }
  163. /**
  164. * Tests client parsing code.
  165. *
  166. * @param string $agent User agent string
  167. * @param string $os Expected parsed OS (or null if none)
  168. * @param string $browser Expected parsed browser (or null if none)
  169. * @param string $version Expected browser version (or null if none)
  170. *
  171. * @dataProvider userAgentProvider
  172. */
  173. public function testCheckClient(string $agent, string $os, ?string $browser = null, ?string $version = null): void
  174. {
  175. $_SERVER['HTTP_USER_AGENT'] = $agent;
  176. $this->object->checkClient();
  177. $this->assertEquals($os, $this->object->get('PMA_USR_OS'));
  178. if ($os != null) {
  179. $this->assertEquals(
  180. $browser,
  181. $this->object->get('PMA_USR_BROWSER_AGENT')
  182. );
  183. }
  184. if ($version == null) {
  185. return;
  186. }
  187. $this->assertEquals(
  188. $version,
  189. $this->object->get('PMA_USR_BROWSER_VER')
  190. );
  191. }
  192. /**
  193. * user Agent Provider
  194. *
  195. * @return array
  196. */
  197. public function userAgentProvider(): array
  198. {
  199. return [
  200. [
  201. 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00',
  202. 'Linux',
  203. 'OPERA',
  204. '9.80',
  205. ],
  206. [
  207. 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/528.16 OmniWeb/622.8.0.112941',
  208. 'Mac',
  209. 'OMNIWEB',
  210. '622',
  211. ],
  212. [
  213. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)',
  214. 'Win',
  215. 'IE',
  216. '8.0',
  217. ],
  218. [
  219. 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
  220. 'Win',
  221. 'IE',
  222. '9.0',
  223. ],
  224. [
  225. 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)',
  226. 'Win',
  227. 'IE',
  228. '10.0',
  229. ],
  230. [
  231. 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
  232. 'Win',
  233. 'IE',
  234. '11.0',
  235. ],
  236. [
  237. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; '
  238. . '.NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; '
  239. . '.NET CLR 3.0.30729; InfoPath.3; rv:11.0) like Gecko',
  240. 'Win',
  241. 'IE',
  242. '11.0',
  243. ],
  244. [
  245. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, '
  246. . 'like Gecko) Chrome/25.0.1364.172 Safari/537.22',
  247. 'Win',
  248. 'CHROME',
  249. '25.0.1364.172',
  250. ],
  251. [
  252. 'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) '
  253. . 'AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2',
  254. 'Unix',
  255. 'SAFARI',
  256. '5.0.419',
  257. ],
  258. [
  259. 'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208',
  260. 'Win',
  261. 'GECKO',
  262. '1.9',
  263. ],
  264. [
  265. 'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; amd64; en_US) KHTML/4.5.4 (like Gecko)',
  266. 'Other',
  267. 'KONQUEROR',
  268. ],
  269. [
  270. 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0',
  271. 'Linux',
  272. 'FIREFOX',
  273. '5.0',
  274. ],
  275. [
  276. 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0',
  277. 'Linux',
  278. 'FIREFOX',
  279. '12.0',
  280. ],
  281. /**
  282. * @todo Is this version really expected?
  283. */
  284. [
  285. 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.4+ (KHTML, like G'
  286. . 'ecko) Version/5.0 Safari/535.4+ SUSE/12.1 (3.2.1) Epiphany/3.2.1',
  287. 'Linux',
  288. 'SAFARI',
  289. '5.0',
  290. ],
  291. ];
  292. }
  293. /**
  294. * test for CheckGd2
  295. */
  296. public function testCheckGd2(): void
  297. {
  298. $this->object->set('GD2Available', 'yes');
  299. $this->object->checkGd2();
  300. $this->assertEquals(1, $this->object->get('PMA_IS_GD2'));
  301. $this->object->set('GD2Available', 'no');
  302. $this->object->checkGd2();
  303. $this->assertEquals(0, $this->object->get('PMA_IS_GD2'));
  304. $this->object->set('GD2Available', 'auto');
  305. if (! function_exists('imagecreatetruecolor')) {
  306. $this->object->checkGd2();
  307. $this->assertEquals(
  308. 0,
  309. $this->object->get('PMA_IS_GD2'),
  310. 'imagecreatetruecolor does not exist, PMA_IS_GD2 should be 0'
  311. );
  312. }
  313. if (function_exists('gd_info')) {
  314. $this->object->checkGd2();
  315. $gd_nfo = gd_info();
  316. if (mb_strstr($gd_nfo['GD Version'], '2.')) {
  317. $this->assertEquals(
  318. 1,
  319. $this->object->get('PMA_IS_GD2'),
  320. 'GD Version >= 2, PMA_IS_GD2 should be 1'
  321. );
  322. } else {
  323. $this->assertEquals(
  324. 0,
  325. $this->object->get('PMA_IS_GD2'),
  326. 'GD Version < 2, PMA_IS_GD2 should be 0'
  327. );
  328. }
  329. }
  330. /* Get GD version string from phpinfo output */
  331. ob_start();
  332. phpinfo(INFO_MODULES); /* Only modules */
  333. $a = strip_tags((string) ob_get_contents());
  334. ob_end_clean();
  335. if (! preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
  336. return;
  337. }
  338. if (mb_strstr($v, '2.')) {
  339. $this->assertEquals(
  340. 1,
  341. $this->object->get('PMA_IS_GD2'),
  342. 'PMA_IS_GD2 should be 1'
  343. );
  344. } else {
  345. $this->assertEquals(
  346. 0,
  347. $this->object->get('PMA_IS_GD2'),
  348. 'PMA_IS_GD2 should be 0'
  349. );
  350. }
  351. }
  352. /**
  353. * Web server detection test
  354. *
  355. * @param string $server Server identification
  356. * @param int $iis Whether server should be detected as IIS
  357. *
  358. * @dataProvider serverNames
  359. */
  360. public function testCheckWebServer(string $server, int $iis): void
  361. {
  362. $_SERVER['SERVER_SOFTWARE'] = $server;
  363. $this->object->checkWebServer();
  364. $this->assertEquals($iis, $this->object->get('PMA_IS_IIS'));
  365. unset($_SERVER['SERVER_SOFTWARE']);
  366. }
  367. /**
  368. * return server names
  369. *
  370. * @return array
  371. */
  372. public function serverNames(): array
  373. {
  374. return [
  375. [
  376. 'Microsoft-IIS 7.0',
  377. 1,
  378. ],
  379. [
  380. 'Apache/2.2.17',
  381. 0,
  382. ],
  383. ];
  384. }
  385. /**
  386. * test for CheckWebServerOs
  387. */
  388. public function testCheckWebServerOs(): void
  389. {
  390. $this->object->checkWebServerOs();
  391. if (defined('PHP_OS')) {
  392. if (stristr(PHP_OS, 'darwin')) {
  393. $this->assertFalse($this->object->get('PMA_IS_WINDOWS'));
  394. } elseif (stristr(PHP_OS, 'win')) {
  395. $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
  396. } elseif (stristr(PHP_OS, 'OS/2')) {
  397. $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
  398. } elseif (stristr(PHP_OS, 'Linux')) {
  399. $this->assertFalse($this->object->get('PMA_IS_WINDOWS'));
  400. } else {
  401. $this->markTestIncomplete('Not known PHP_OS: ' . PHP_OS);
  402. }
  403. } else {
  404. $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
  405. define('PHP_OS', 'Windows');
  406. $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
  407. }
  408. }
  409. /**
  410. * Tests loading of default values
  411. *
  412. * @group large
  413. */
  414. public function testLoadDefaults(): void
  415. {
  416. $this->object->defaultServer = [];
  417. $this->object->default = [];
  418. $this->object->settings = ['is_setup' => false, 'AvailableCharsets' => ['test']];
  419. $this->object->loadDefaults();
  420. $settings = new Settings([]);
  421. $config = $settings->toArray();
  422. $this->assertIsArray($config['Servers']);
  423. $this->assertEquals($config['Servers'][1], $this->object->defaultServer);
  424. unset($config['Servers']);
  425. $this->assertEquals($config, $this->object->default);
  426. $this->assertEquals(
  427. array_replace_recursive(['is_setup' => false, 'AvailableCharsets' => ['test']], $config),
  428. $this->object->settings
  429. );
  430. }
  431. /**
  432. * test for CheckConfigSource
  433. */
  434. public function testCheckConfigSource(): void
  435. {
  436. $this->object->setSource('unexisted.config.php');
  437. $this->assertFalse($this->object->checkConfigSource());
  438. $this->assertEquals(0, $this->object->sourceMtime);
  439. $this->object->setSource(ROOT_PATH . 'test/test_data/config.inc.php');
  440. $this->assertNotEmpty($this->object->getSource());
  441. $this->assertTrue($this->object->checkConfigSource());
  442. }
  443. /**
  444. * Test getting and setting config values
  445. */
  446. public function testGetAndSet(): void
  447. {
  448. $this->assertNull($this->object->get('unresisting_setting'));
  449. $this->object->set('test_setting', 'test_value');
  450. $this->assertEquals('test_value', $this->object->get('test_setting'));
  451. }
  452. /**
  453. * Tests setting configuration source
  454. */
  455. public function testGetSetSource(): void
  456. {
  457. echo $this->object->getSource();
  458. $this->assertEmpty($this->object->getSource(), 'Source is null by default');
  459. $this->object->setSource(ROOT_PATH . 'config.sample.inc.php');
  460. $this->assertEquals(
  461. ROOT_PATH . 'config.sample.inc.php',
  462. $this->object->getSource(),
  463. 'Cant set new source'
  464. );
  465. }
  466. /**
  467. * test for IsHttp
  468. *
  469. * @param string $scheme http scheme
  470. * @param string $https https
  471. * @param string $forwarded forwarded header
  472. * @param string $uri request uri
  473. * @param string $lb http https from lb
  474. * @param string $front http front end https
  475. * @param string $proto http x forwarded proto
  476. * @param string $protoCloudFront http cloudfront forwarded proto
  477. * @param string $pmaAbsoluteUri phpMyAdmin absolute URI
  478. * @param int $port server port
  479. * @param bool $expected expected result
  480. *
  481. * @dataProvider httpsParams
  482. */
  483. public function testIsHttps(
  484. string $scheme,
  485. string $https,
  486. string $forwarded,
  487. string $uri,
  488. string $lb,
  489. string $front,
  490. string $proto,
  491. string $protoCloudFront,
  492. string $pmaAbsoluteUri,
  493. int $port,
  494. bool $expected
  495. ): void {
  496. $_SERVER['HTTP_SCHEME'] = $scheme;
  497. $_SERVER['HTTPS'] = $https;
  498. $_SERVER['HTTP_FORWARDED'] = $forwarded;
  499. $_SERVER['REQUEST_URI'] = $uri;
  500. $_SERVER['HTTP_HTTPS_FROM_LB'] = $lb;
  501. $_SERVER['HTTP_FRONT_END_HTTPS'] = $front;
  502. $_SERVER['HTTP_X_FORWARDED_PROTO'] = $proto;
  503. $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] = $protoCloudFront;
  504. $_SERVER['SERVER_PORT'] = $port;
  505. $this->object->set('is_https', null);
  506. $this->object->set('PmaAbsoluteUri', $pmaAbsoluteUri);
  507. $this->assertEquals($expected, $this->object->isHttps());
  508. }
  509. /**
  510. * Data provider for https detection
  511. *
  512. * @return array
  513. */
  514. public function httpsParams(): array
  515. {
  516. return [
  517. [
  518. 'http',
  519. '',
  520. '',
  521. '',
  522. '',
  523. '',
  524. 'http',
  525. '',
  526. '',
  527. 80,
  528. false,
  529. ],
  530. [
  531. 'http',
  532. '',
  533. '',
  534. 'http://',
  535. '',
  536. '',
  537. 'http',
  538. '',
  539. '',
  540. 80,
  541. false,
  542. ],
  543. [
  544. 'http',
  545. '',
  546. '',
  547. '',
  548. '',
  549. '',
  550. 'http',
  551. '',
  552. '',
  553. 443,
  554. true,
  555. ],
  556. [
  557. 'http',
  558. '',
  559. '',
  560. '',
  561. '',
  562. '',
  563. 'https',
  564. '',
  565. '',
  566. 80,
  567. true,
  568. ],
  569. [
  570. 'http',
  571. '',
  572. '',
  573. '',
  574. '',
  575. 'on',
  576. 'http',
  577. '',
  578. '',
  579. 80,
  580. true,
  581. ],
  582. [
  583. 'http',
  584. '',
  585. '',
  586. '',
  587. 'on',
  588. '',
  589. 'http',
  590. '',
  591. '',
  592. 80,
  593. true,
  594. ],
  595. [
  596. 'http',
  597. '',
  598. '',
  599. 'https://',
  600. '',
  601. '',
  602. 'http',
  603. '',
  604. '',
  605. 80,
  606. true,
  607. ],
  608. [
  609. 'http',
  610. 'on',
  611. '',
  612. '',
  613. '',
  614. '',
  615. 'http',
  616. '',
  617. '',
  618. 80,
  619. true,
  620. ],
  621. [
  622. 'https',
  623. '',
  624. '',
  625. '',
  626. '',
  627. '',
  628. 'http',
  629. '',
  630. '',
  631. 80,
  632. true,
  633. ],
  634. [
  635. 'http',
  636. '',
  637. '',
  638. '',
  639. '',
  640. '',
  641. '',
  642. 'https',
  643. '',
  644. 80,
  645. true,
  646. ],
  647. [
  648. 'http',
  649. '',
  650. '',
  651. '',
  652. '',
  653. '',
  654. 'https',
  655. 'http',
  656. '',
  657. 80,
  658. true,
  659. ],
  660. [
  661. 'https',
  662. '',
  663. '',
  664. '',
  665. '',
  666. '',
  667. '',
  668. '',
  669. '',
  670. 80,
  671. true,
  672. ],
  673. [
  674. 'http',
  675. '',
  676. '',
  677. '',
  678. '',
  679. '',
  680. '',
  681. '',
  682. '',
  683. 8080,
  684. false,
  685. ],
  686. [
  687. '',
  688. '',
  689. '',
  690. '',
  691. '',
  692. '',
  693. '',
  694. '',
  695. 'https://127.0.0.1',
  696. 80,
  697. true,
  698. ],
  699. [
  700. '',
  701. '',
  702. '',
  703. '',
  704. '',
  705. '',
  706. '',
  707. '',
  708. 'http://127.0.0.1',
  709. 80,
  710. false,
  711. ],
  712. [
  713. '',
  714. '',
  715. 'for=12.34.56.78;host=example.com;proto=https, for=23.45.67.89',
  716. '',
  717. '',
  718. '',
  719. '',
  720. '',
  721. 'http://127.0.0.1',
  722. 80,
  723. true,
  724. ],
  725. ];
  726. }
  727. /**
  728. * Test for getting root path
  729. *
  730. * @param string $request The request URL used for phpMyAdmin
  731. * @param string $absolute The absolute URL used for phpMyAdmin
  732. * @param string $expected Expected root path
  733. *
  734. * @dataProvider rootUris
  735. */
  736. public function testGetRootPath(string $request, string $absolute, string $expected): void
  737. {
  738. $GLOBALS['PMA_PHP_SELF'] = $request;
  739. $this->object->set('PmaAbsoluteUri', $absolute);
  740. $this->assertEquals($expected, $this->object->getRootPath());
  741. }
  742. /**
  743. * Data provider for testGetRootPath
  744. *
  745. * @return array data for testGetRootPath
  746. */
  747. public function rootUris(): array
  748. {
  749. return [
  750. [
  751. '',
  752. '',
  753. '/',
  754. ],
  755. [
  756. '/',
  757. '',
  758. '/',
  759. ],
  760. [
  761. '/index.php',
  762. '',
  763. '/',
  764. ],
  765. [
  766. '\\index.php',
  767. '',
  768. '/',
  769. ],
  770. [
  771. '\\',
  772. '',
  773. '/',
  774. ],
  775. [
  776. '\\path\\to\\index.php',
  777. '',
  778. '/path/to/',
  779. ],
  780. [
  781. '/foo/bar/phpmyadmin/index.php',
  782. '',
  783. '/foo/bar/phpmyadmin/',
  784. ],
  785. [
  786. '/foo/bar/phpmyadmin/',
  787. '',
  788. '/foo/bar/phpmyadmin/',
  789. ],
  790. [
  791. 'https://example.net/baz/phpmyadmin/',
  792. '',
  793. '/baz/phpmyadmin/',
  794. ],
  795. [
  796. 'http://example.net/baz/phpmyadmin/',
  797. '',
  798. '/baz/phpmyadmin/',
  799. ],
  800. [
  801. 'http://example.net/phpmyadmin/',
  802. '',
  803. '/phpmyadmin/',
  804. ],
  805. [
  806. 'http://example.net/',
  807. '',
  808. '/',
  809. ],
  810. [
  811. 'http://example.net/',
  812. 'http://example.net/phpmyadmin/',
  813. '/phpmyadmin/',
  814. ],
  815. [
  816. 'http://example.net/',
  817. 'http://example.net/phpmyadmin',
  818. '/phpmyadmin/',
  819. ],
  820. [
  821. 'http://example.net/',
  822. '/phpmyadmin2',
  823. '/phpmyadmin2/',
  824. ],
  825. [
  826. 'http://example.net/',
  827. '/phpmyadmin3/',
  828. '/phpmyadmin3/',
  829. ],
  830. ];
  831. }
  832. /**
  833. * Tests loading of config file
  834. *
  835. * @param string $source File name of config to load
  836. * @param bool $result Expected result of loading
  837. *
  838. * @dataProvider configPaths
  839. */
  840. public function testLoad(string $source, bool $result): void
  841. {
  842. if ($result) {
  843. $this->assertTrue($this->object->load($source));
  844. } else {
  845. $this->assertFalse($this->object->load($source));
  846. }
  847. }
  848. /**
  849. * return of config Paths
  850. *
  851. * @return array
  852. */
  853. public function configPaths(): array
  854. {
  855. return [
  856. [
  857. ROOT_PATH . 'test/test_data/config.inc.php',
  858. true,
  859. ],
  860. [
  861. ROOT_PATH . 'test/test_data/config-nonexisting.inc.php',
  862. false,
  863. ],
  864. ];
  865. }
  866. /**
  867. * Test for loading user preferences
  868. *
  869. * @todo Test actually preferences loading
  870. * @doesNotPerformAssertions
  871. */
  872. public function testLoadUserPreferences(): void
  873. {
  874. $this->object->loadUserPreferences();
  875. }
  876. /**
  877. * Test for setting user config value
  878. */
  879. public function testSetUserValue(): void
  880. {
  881. $this->object->setUserValue(null, 'lang', 'cs', 'en');
  882. $this->object->setUserValue('TEST_COOKIE_USER_VAL', '', 'cfg_val_1');
  883. $this->assertEquals(
  884. $this->object->getUserValue('TEST_COOKIE_USER_VAL', 'fail'),
  885. 'cfg_val_1'
  886. );
  887. }
  888. /**
  889. * Test for getting user config value
  890. */
  891. public function testGetUserValue(): void
  892. {
  893. $this->assertEquals($this->object->getUserValue('test_val', 'val'), 'val');
  894. }
  895. /**
  896. * Should test checking of config permissions
  897. */
  898. public function testCheckPermissions(): void
  899. {
  900. //load file permissions for the current permissions file
  901. $perms = @fileperms($this->object->getSource());
  902. //testing for permissions for no configuration file
  903. $this->assertFalse(! ($perms === false) && ($perms & 2));
  904. //load file permissions for the current permissions file
  905. $perms = @fileperms($this->permTestObj->getSource());
  906. if (! ($perms === false) && ($perms & 2)) {
  907. $this->assertTrue((bool) $this->permTestObj->get('PMA_IS_WINDOWS'));
  908. } else {
  909. $this->assertFalse((bool) $this->permTestObj->get('PMA_IS_WINDOWS'));
  910. }
  911. }
  912. /**
  913. * Test for setting cookies
  914. */
  915. public function testSetCookie(): void
  916. {
  917. $this->object->set('is_https', false);
  918. $this->assertFalse(
  919. $this->object->setCookie(
  920. 'TEST_DEF_COOKIE',
  921. 'test_def_123',
  922. 'test_def_123'
  923. )
  924. );
  925. $this->assertTrue(
  926. $this->object->setCookie(
  927. 'TEST_CONFIG_COOKIE',
  928. 'test_val_123',
  929. null,
  930. 3600
  931. )
  932. );
  933. $this->assertTrue(
  934. $this->object->setCookie(
  935. 'TEST_CONFIG_COOKIE',
  936. '',
  937. 'default_val'
  938. )
  939. );
  940. $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
  941. $this->assertTrue(
  942. $this->object->setCookie(
  943. 'TEST_MANUAL_COOKIE',
  944. 'other',
  945. 'other'
  946. )
  947. );
  948. }
  949. /**
  950. * Test for getTempDir
  951. *
  952. * @group file-system
  953. */
  954. public function testGetTempDir(): void
  955. {
  956. $this->object->set('TempDir', sys_get_temp_dir() . DIRECTORY_SEPARATOR);
  957. // Check no double slash is here
  958. $this->assertEquals(
  959. sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'upload',
  960. $this->object->getTempDir('upload')
  961. );
  962. }
  963. /**
  964. * Test for getUploadTempDir
  965. *
  966. * @group file-system
  967. */
  968. public function testGetUploadTempDir(): void
  969. {
  970. $this->object->set('TempDir', realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR);
  971. $this->assertEquals(
  972. $this->object->getTempDir('upload'),
  973. $this->object->getUploadTempDir()
  974. );
  975. }
  976. /**
  977. * Test for checkServers
  978. *
  979. * @param array $settings settings array
  980. * @param array $expected expected result
  981. *
  982. * @dataProvider serverSettingsProvider
  983. */
  984. public function testCheckServers(array $settings, array $expected): void
  985. {
  986. $this->object->settings['Servers'] = $settings;
  987. $this->object->checkServers();
  988. $expected = array_merge($this->object->defaultServer, $expected);
  989. $this->assertEquals($expected, $this->object->settings['Servers'][1]);
  990. }
  991. /**
  992. * Data provider for checkServers test
  993. *
  994. * @return array
  995. */
  996. public function serverSettingsProvider(): array
  997. {
  998. return [
  999. 'empty' => [
  1000. [],
  1001. [],
  1002. ],
  1003. 'only_host' => [
  1004. [1 => ['host' => '127.0.0.1']],
  1005. ['host' => '127.0.0.1'],
  1006. ],
  1007. 'empty_host' => [
  1008. [1 => ['host' => '']],
  1009. [
  1010. 'verbose' => 'Server 1',
  1011. 'host' => '',
  1012. ],
  1013. ],
  1014. ];
  1015. }
  1016. /**
  1017. * @group with-trigger-error
  1018. */
  1019. public function testCheckServersWithInvalidServer(): void
  1020. {
  1021. $this->expectError();
  1022. $this->expectErrorMessage('Invalid server index: invalid');
  1023. $this->object->settings['Servers'] = ['invalid' => ['host' => '127.0.0.1'], 1 => ['host' => '127.0.0.1']];
  1024. $this->object->checkServers();
  1025. $expected = array_merge($this->object->defaultServer, ['host' => '127.0.0.1']);
  1026. $this->assertEquals($expected, $this->object->settings['Servers'][1]);
  1027. }
  1028. /**
  1029. * Test for selectServer
  1030. *
  1031. * @param array $settings settings array
  1032. * @param string $request request
  1033. * @param int $expected expected result
  1034. *
  1035. * @dataProvider selectServerProvider
  1036. * @depends testCheckServers
  1037. */
  1038. public function testSelectServer(array $settings, string $request, int $expected): void
  1039. {
  1040. $this->object->settings['Servers'] = $settings;
  1041. $this->object->checkServers();
  1042. $_REQUEST['server'] = $request;
  1043. $this->assertEquals($expected, $this->object->selectServer());
  1044. }
  1045. /**
  1046. * Data provider for selectServer test
  1047. *
  1048. * @return array
  1049. */
  1050. public function selectServerProvider(): array
  1051. {
  1052. return [
  1053. 'zero' => [
  1054. [],
  1055. '0',
  1056. 1,
  1057. ],
  1058. 'number' => [
  1059. [1 => []],
  1060. '1',
  1061. 1,
  1062. ],
  1063. 'host' => [
  1064. [2 => ['host' => '127.0.0.1']],
  1065. '127.0.0.1',
  1066. 2,
  1067. ],
  1068. 'verbose' => [
  1069. [
  1070. 1 => [
  1071. 'verbose' => 'Server 1',
  1072. 'host' => '',
  1073. ],
  1074. ],
  1075. 'Server 1',
  1076. 1,
  1077. ],
  1078. 'md5' => [
  1079. [
  1080. 66 => [
  1081. 'verbose' => 'Server 1',
  1082. 'host' => '',
  1083. ],
  1084. ],
  1085. '753f173bd4ac8a45eae0fe9a4fbe0fc0',
  1086. 66,
  1087. ],
  1088. 'nonexisting_string' => [
  1089. [1 => []],
  1090. 'invalid',
  1091. 1,
  1092. ],
  1093. 'nonexisting' => [
  1094. [1 => []],
  1095. '100',
  1096. 1,
  1097. ],
  1098. ];
  1099. }
  1100. /**
  1101. * Test for getConnectionParams
  1102. *
  1103. * @param array $server_cfg Server configuration
  1104. * @param int $mode Mode to test
  1105. * @param array|null $server Server array to test
  1106. * @param array $expected Expected result
  1107. *
  1108. * @dataProvider connectionParams
  1109. */
  1110. public function testGetConnectionParams(array $server_cfg, int $mode, ?array $server, array $expected): void
  1111. {
  1112. $GLOBALS['cfg']['Server'] = $server_cfg;
  1113. $result = Config::getConnectionParams($mode, $server);
  1114. $this->assertEquals($expected, $result);
  1115. }
  1116. /**
  1117. * Data provider for getConnectionParams test
  1118. *
  1119. * @return array
  1120. */
  1121. public function connectionParams(): array
  1122. {
  1123. $cfg_basic = [
  1124. 'user' => 'u',
  1125. 'password' => 'pass',
  1126. 'host' => '',
  1127. 'controluser' => 'u2',
  1128. 'controlpass' => 'p2',
  1129. ];
  1130. $cfg_ssl = [
  1131. 'user' => 'u',
  1132. 'password' => 'pass',
  1133. 'host' => '',
  1134. 'ssl' => true,
  1135. 'controluser' => 'u2',
  1136. 'controlpass' => 'p2',
  1137. ];
  1138. $cfg_control_ssl = [
  1139. 'user' => 'u',
  1140. 'password' => 'pass',
  1141. 'host' => '',
  1142. 'control_ssl' => true,
  1143. 'controluser' => 'u2',
  1144. 'controlpass' => 'p2',
  1145. ];
  1146. return [
  1147. [
  1148. $cfg_basic,
  1149. DatabaseInterface::CONNECT_USER,
  1150. null,
  1151. [
  1152. 'u',
  1153. 'pass',
  1154. [
  1155. 'user' => 'u',
  1156. 'password' => 'pass',
  1157. 'host' => 'localhost',
  1158. 'socket' => null,
  1159. 'port' => 0,
  1160. 'ssl' => false,
  1161. 'compress' => false,
  1162. 'controluser' => 'u2',
  1163. 'controlpass' => 'p2',
  1164. ],
  1165. ],
  1166. ],
  1167. [
  1168. $cfg_basic,
  1169. DatabaseInterface::CONNECT_CONTROL,
  1170. null,
  1171. [
  1172. 'u2',
  1173. 'p2',
  1174. [
  1175. 'host' => 'localhost',
  1176. 'socket' => null,
  1177. 'port' => 0,
  1178. 'ssl' => false,
  1179. 'compress' => false,
  1180. ],
  1181. ],
  1182. ],
  1183. [
  1184. $cfg_ssl,
  1185. DatabaseInterface::CONNECT_USER,
  1186. null,
  1187. [
  1188. 'u',
  1189. 'pass',
  1190. [
  1191. 'user' => 'u',
  1192. 'password' => 'pass',
  1193. 'host' => 'localhost',
  1194. 'socket' => null,
  1195. 'port' => 0,
  1196. 'ssl' => true,
  1197. 'compress' => false,
  1198. 'controluser' => 'u2',
  1199. 'controlpass' => 'p2',
  1200. ],
  1201. ],
  1202. ],
  1203. [
  1204. $cfg_ssl,
  1205. DatabaseInterface::CONNECT_CONTROL,
  1206. null,
  1207. [
  1208. 'u2',
  1209. 'p2',
  1210. [
  1211. 'host' => 'localhost',
  1212. 'socket' => null,
  1213. 'port' => 0,
  1214. 'ssl' => true,
  1215. 'compress' => false,
  1216. ],
  1217. ],
  1218. ],
  1219. [
  1220. $cfg_control_ssl,
  1221. DatabaseInterface::CONNECT_USER,
  1222. null,
  1223. [
  1224. 'u',
  1225. 'pass',
  1226. [
  1227. 'user' => 'u',
  1228. 'password' => 'pass',
  1229. 'host' => 'localhost',
  1230. 'socket' => null,
  1231. 'port' => 0,
  1232. 'ssl' => false,
  1233. 'compress' => false,
  1234. 'controluser' => 'u2',
  1235. 'controlpass' => 'p2',
  1236. 'control_ssl' => true,
  1237. ],
  1238. ],
  1239. ],
  1240. [
  1241. $cfg_control_ssl,
  1242. DatabaseInterface::CONNECT_CONTROL,
  1243. null,
  1244. [
  1245. 'u2',
  1246. 'p2',
  1247. [
  1248. 'host' => 'localhost',
  1249. 'socket' => null,
  1250. 'port' => 0,
  1251. 'ssl' => true,
  1252. 'compress' => false,
  1253. ],
  1254. ],
  1255. ],
  1256. ];
  1257. }
  1258. }