2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_Config class
6 * @package PhpMyAdmin-test
13 require_once 'libraries/core.lib.php';
14 require_once 'libraries/Config.class.php';
15 require_once 'libraries/relation.lib.php';
16 require_once 'libraries/Theme.class.php';
17 require_once 'libraries/vendor_config.php';
18 require_once 'libraries/url_generating.lib.php';
19 require_once 'libraries/php-gettext/gettext.inc';
22 * Tests behaviour of PMA_Config class
24 * @package PhpMyAdmin-test
26 class PMA_ConfigTest
extends PHPUnit_Framework_TestCase
29 * Turn off backup globals
31 protected $backupGlobals = false;
39 * Sets up the fixture, for example, opens a network connection.
40 * This method is called before a test is executed.
44 protected function setUp()
46 $this->object = new PMA_Config
;
47 $GLOBALS['server'] = 0;
48 $_SESSION['is_git_revision'] = true;
49 $GLOBALS['PMA_Config'] = new PMA_Config(CONFIG_FILE
);
53 * Tears down the fixture, for example, closes a network connection.
54 * This method is called after a test is executed.
58 protected function tearDown()
63 * Test for CheckSystem
67 public function testCheckSystem()
69 $this->object->checkSystem();
71 $this->assertNotNull($this->object->get('PMA_VERSION'));
72 $this->assertNotEmpty($this->object->get('PMA_THEME_VERSION'));
73 $this->assertNotEmpty($this->object->get('PMA_THEME_GENERATION'));
77 * Test for GetFontsizeForm
81 public function testGetFontsizeForm()
83 $this->assertContains(
84 '<form name="form_fontsize_selection" id="form_fontsize_selection"',
85 PMA_Config
::getFontsizeForm()
88 $this->assertContains(
89 '<label for="select_fontsize">',
90 PMA_Config
::getFontsizeForm()
93 //test getFontsizeOptions for "em" unit
94 $fontsize = $GLOBALS['PMA_Config']->get('fontsize');
95 $GLOBALS['PMA_Config']->set('fontsize', '');
96 $_COOKIE['pma_fontsize'] = "10em";
97 $this->assertContains(
98 '<option value="7em"',
99 PMA_Config
::getFontsizeForm()
101 $this->assertContains(
102 '<option value="8em"',
103 PMA_Config
::getFontsizeForm()
106 //test getFontsizeOptions for "pt" unit
107 $_COOKIE['pma_fontsize'] = "10pt";
108 $this->assertContains(
109 '<option value="2pt"',
110 PMA_Config
::getFontsizeForm()
112 $this->assertContains(
113 '<option value="4pt"',
114 PMA_Config
::getFontsizeForm()
117 //test getFontsizeOptions for "px" unit
118 $_COOKIE['pma_fontsize'] = "10px";
119 $this->assertContains(
120 '<option value="5px"',
121 PMA_Config
::getFontsizeForm()
123 $this->assertContains(
124 '<option value="6px"',
125 PMA_Config
::getFontsizeForm()
128 //test getFontsizeOptions for unknown unit
129 $_COOKIE['pma_fontsize'] = "10abc";
130 $this->assertContains(
131 '<option value="7abc"',
132 PMA_Config
::getFontsizeForm()
134 $this->assertContains(
135 '<option value="8abc"',
136 PMA_Config
::getFontsizeForm()
138 unset($_COOKIE['pma_fontsize']);
139 //rollback the fontsize setting
140 $GLOBALS['PMA_Config']->set('fontsize', $fontsize);
144 * Test for checkOutputCompression
148 public function testCheckOutputCompression()
151 $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->assertFalse($this->object->get("OBGzip"));
158 $this->object->set('OBGzip', 'auto');
159 $this->object->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
160 $this->object->set('PMA_USR_BROWSER_VER', 5);
161 $this->object->checkOutputCompression();
162 $this->assertTrue($this->object->get("OBGzip"));
166 * Tests client parsing code.
168 * @param string $agent User agent string
169 * @param string $os Expected parsed OS (or null if none)
170 * @param string $browser Expected parsed browser (or null if none)
171 * @param string $version Expected browser version (or null if none)
175 * @dataProvider userAgentProvider
177 public function testCheckClient($agent, $os, $browser = null, $version = null)
179 $_SERVER['HTTP_USER_AGENT'] = $agent;
180 $this->object->checkClient();
181 $this->assertEquals($os, $this->object->get('PMA_USR_OS'));
185 $this->object->get('PMA_USR_BROWSER_AGENT')
188 if ($version != null) {
191 $this->object->get('PMA_USR_BROWSER_VER')
197 * user Agent Provider
201 public function userAgentProvider()
205 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00',
211 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/'
212 . '528.16 OmniWeb/622.8.0.112941',
218 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)',
224 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
230 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; '
237 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; '
238 . '.NET4.0C; rv:11.0) like Gecko',
244 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, '
245 . 'like Gecko) Chrome/25.0.1364.172 Safari/537.22',
251 'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) '
252 . 'AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2',
258 'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208',
264 'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; '
265 . 'amd64; en_US) KHTML/4.5.4 (like Gecko)',
270 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0',
276 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 '
283 * @todo Is this version really expected?
286 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.4+ (KHTML, like G'
287 . 'ecko) Version/5.0 Safari/535.4+ SUSE/12.1 (3.2.1) Epiphany/3.2.1',
302 public function testCheckGd2()
304 $prevIsGb2Val = $this->object->get('PMA_IS_GD2');
306 $this->object->set('GD2Available', 'yes');
307 $this->object->checkGd2();
308 $this->assertEquals(1, $this->object->get('PMA_IS_GD2'));
310 $this->object->set('GD2Available', 'no');
311 $this->object->checkGd2();
312 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'));
314 $this->object->set('GD2Available', $prevIsGb2Val);
316 if (!@function_exists
('imagecreatetruecolor')) {
317 $this->object->checkGd2();
320 $this->object->get('PMA_IS_GD2'),
321 'imagecreatetruecolor does not exist, PMA_IS_GD2 should be 0'
325 if (@function_exists
('gd_info')) {
326 $this->object->checkGd2();
328 if (strstr($gd_nfo["GD Version"], '2.')) {
331 $this->object->get('PMA_IS_GD2'),
332 'GD Version >= 2, PMA_IS_GD2 should be 1'
337 $this->object->get('PMA_IS_GD2'),
338 'GD Version < 2, PMA_IS_GD2 should be 0'
343 /* Get GD version string from phpinfo output */
345 phpinfo(INFO_MODULES
); /* Only modules */
346 $a = strip_tags(ob_get_contents());
349 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
350 if (strstr($v, '2.')) {
353 $this->object->get('PMA_IS_GD2'),
354 'PMA_IS_GD2 should be 1'
359 $this->object->get('PMA_IS_GD2'),
360 'PMA_IS_GD2 should be 0'
367 * Web server detection test
369 * @param string $server Server indentification
370 * @param boolean $iis Whether server should be detected as IIS
374 * @dataProvider serverNames
376 public function testCheckWebServer($server, $iis)
378 $_SERVER['SERVER_SOFTWARE'] = $server;
379 $this->object->checkWebServer();
380 $this->assertEquals($iis, $this->object->get('PMA_IS_IIS'));
381 unset($_SERVER['SERVER_SOFTWARE']);
386 * return server names
390 public function serverNames()
406 * test for CheckWebServerOs
410 public function testCheckWebServerOs()
412 $this->object->checkWebServerOs();
414 if (defined('PHP_OS')) {
415 if (stristr(PHP_OS
, 'darwin')) {
416 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
417 } elseif (stristr(PHP_OS
, 'win')) {
418 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
419 } elseif (stristr(PHP_OS
, 'OS/2')) {
420 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
422 } elseif (stristr(PHP_OS
, 'Linux')) {
423 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
425 $this->markTestIncomplete('Not known PHP_OS: ' . PHP_OS
);
428 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
430 define('PHP_OS', 'Windows');
431 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
436 * test for CheckPhpVersion
440 public function testCheckPhpVersion()
442 $this->object->checkPhpVersion();
445 $php_str_ver = phpversion();
449 '@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
453 if (isset($match) && ! empty($match[1])) {
454 if (! isset($match[2])) {
457 if (! isset($match[3])) {
460 $php_int_ver = (int) sprintf(
472 $this->object->get('PMA_PHP_STR_VERSION')
476 $this->object->get('PMA_PHP_INT_VERSION')
481 * Tests loading of default values
487 public function testLoadDefaults()
489 $prevDefaultSource = $this->object->default_source
;
491 $this->object->default_source
= 'unexisted.file.php';
492 $this->assertFalse($this->object->loadDefaults());
494 $this->object->default_source
= $prevDefaultSource;
496 include $this->object->default_source
;
501 $this->assertTrue($this->object->loadDefaults());
504 $this->object->default_source_mtime
,
505 filemtime($prevDefaultSource)
508 $loadedConf['Servers'][1],
509 $this->object->default_server
512 unset($loadedConf['Servers']);
514 $this->assertEquals($loadedConf, $this->object->default);
516 $expectedSettings = PMA_arrayMergeRecursive(
517 $this->object->settings
,
523 $this->object->settings
,
524 'Settings loaded wrong'
527 $this->assertFalse($this->object->error_config_default_file
);
531 * test for CheckConfigSource
535 public function testCheckConfigSource()
537 $this->object->setSource('unexisted.config.php');
538 $this->assertFalse($this->object->checkConfigSource());
539 $this->assertEquals(0, $this->object->source_mtime
);
541 $this->object->setSource('libraries/config.default.php');
543 $this->assertNotEmpty($this->object->getSource());
544 $this->assertTrue($this->object->checkConfigSource());
548 * Test getting and setting config values
552 * @covers PMA_Config::get
553 * @covers PMA_Config::set
555 public function testGetAndSet()
557 $this->assertNull($this->object->get("unresisting_setting"));
559 $this->object->set('test_setting', 'test_value');
561 $this->assertEquals('test_value', $this->object->get('test_setting'));
565 * Tests setting configuration source
569 * @covers PMA_Config::getSource
570 * @covers PMA_Config::setSource
572 public function testGetSetSource()
574 echo $this->object->getSource();
576 $this->assertEmpty($this->object->getSource(), "Source is null by default");
578 $this->object->setSource("config.sample.inc.php");
581 "config.sample.inc.php",
582 $this->object->getSource(),
583 "Cant set new source"
589 * test for CheckPmaAbsoluteUriEmpty
593 public function testCheckPmaAbsoluteUriEmpty()
595 $this->object->set('PmaAbsoluteUri', '');
597 $this->object->checkPmaAbsoluteUri(),
598 'PmaAbsoluteUri is not set and should be error'
601 $this->object->error_pma_uri
,
602 'PmaAbsoluteUri is not set and should be error'
607 * Checks correcting of absolute URI
609 * @param string $real Real URI received
610 * @param string $expected Expected corrected URI
614 * @depends testCheckPmaAbsoluteUriEmpty
615 * @dataProvider absoluteUris
617 public function testCheckPmaAbsoluteUri($real, $expected)
619 $this->object->set('PmaAbsoluteUri', $real);
620 $this->object->checkPmaAbsoluteUri();
621 $this->assertEquals($expected, $this->object->get('PmaAbsoluteUri'));
625 * return absolute Uris
629 public function absoluteUris()
633 'http://localhost/phpmyadmin/',
634 'http://localhost/phpmyadmin/',
637 'http://localhost/phpmyadmin',
638 'http://localhost/phpmyadmin/',
641 'localhost/phpmyadmin/',
642 'http://localhost/phpmyadmin/',
645 'http://user:pwd@localhost/phpmyadmin/index.php',
646 "http://user:pwd@localhost/phpmyadmin/index.php/",
649 'https://user:pwd@localhost/phpmyadmin/index.php',
650 "https://user:pwd@localhost/phpmyadmin/index.php/",
656 * Test for absolute URI composition
660 * @depends testCheckPmaAbsoluteUri
662 public function testCheckPmaAbsoluteUriScheme()
664 $_SERVER['HTTP_HOST'] = 'localhost';
665 $_SERVER['HTTP_SCHEME'] = 'http';
666 $_SERVER['HTTPS'] = 'off';
667 $GLOBALS['PMA_PHP_SELF'] = 'index.php';
669 $this->object->set('PmaAbsoluteUri', '');
671 $this->object->checkPmaAbsoluteUri();
674 $this->object->get('PmaAbsoluteUri')
679 * test for CheckCollationConnection
683 public function testCheckCollationConnection()
685 $_REQUEST['collation_connection'] = 'utf-8';
686 $this->object->checkCollationConnection();
689 $_REQUEST['collation_connection'],
690 $this->object->get('collation_connection')
699 public function testIsHttps()
701 $this->object->set('is_https', null);
702 $this->object->set('PmaAbsoluteUri', 'http://some_host.com/phpMyAdmin');
703 $this->assertFalse($this->object->isHttps());
705 $this->object->set('is_https', null);
706 $this->object->set('PmaAbsoluteUri', 'https://some_host.com/phpMyAdmin');
707 $this->assertTrue($this->object->isHttps());
711 * test for DetectHttps
715 public function testDetectHttps()
717 unset($_SERVER['REQUEST_URI']);
718 unset($_SERVER['HTTP_SCHEME']);
719 unset($_SERVER['HTTPS']);
721 $this->assertFalse($this->object->detectHttps());
723 $_SERVER['REQUEST_URI'] = '/url:\this_is_not_url';
724 $this->assertFalse($this->object->detectHttps());
726 $_SERVER['REQUEST_URI'] = 'file://localhost/phpmyadmin/index.php';
727 $this->assertFalse($this->object->detectHttps());
729 $_ENV['REQUEST_URI'] = 'http://localhost/phpmyadmin/index.php';
730 $this->assertFalse($this->object->detectHttps());
732 $_SERVER['REQUEST_URI'] = 'https://localhost/phpmyadmin/index.php';
733 $this->assertTrue($this->object->detectHttps());
735 $_SERVER['REQUEST_URI'] = 'localhost/phpmyadmin/index.php';
736 $_SERVER['HTTP_SCHEME'] = 'https';
737 $_SERVER['HTTPS'] = 'on';
738 $this->assertTrue($this->object->detectHttps());
742 * Test for checking cookie path
746 * @depends testDetectHttps
748 public function testCheckCookiePath()
750 $this->object->checkCookiePath();
751 echo $this->object->get('cookie_path');
752 $this->assertEquals('', $this->object->get('cookie_path'));
756 * Test for backward compatibility globals
760 * @depends testCheckSystem
761 * @depends testCheckWebServer
762 * @depends testLoadDefaults
766 public function testEnableBc()
768 $this->object->enableBc();
773 'PMA_THEME_GENERATION',
774 'PMA_PHP_STR_VERSION',
775 'PMA_PHP_INT_VERSION',
780 'PMA_USR_BROWSER_VER',
781 'PMA_USR_BROWSER_AGENT'
784 foreach ($defines as $define) {
785 $this->assertTrue(defined($define));
786 $this->assertEquals(constant($define), $this->object->get($define));
791 * Test for getting cookie path
793 * @param string $absolute The absolute URL used for phpMyAdmin
794 * @param string $expected Expected cookie path
798 * @dataProvider cookieUris
800 public function testGetCookiePath($absolute, $expected)
802 $this->object->set('PmaAbsoluteUri', $absolute);
803 $this->assertEquals($expected, $this->object->getCookiePath());
807 * Data provider for testGetCookiePath
809 * @return array data for testGetCookiePath
811 public function cookieUris()
815 'http://example.net/phpmyadmin/',
819 'http://example.net/',
826 * Tests loading of config file
828 * @param string $source File name of config to load
829 * @param boolean $result Expected result of loading
833 * @dataProvider configPaths
835 public function testLoad($source, $result)
838 $this->assertTrue($this->object->load($source));
840 $this->assertFalse($this->object->load($source));
845 * return of config Paths
849 public function configPaths()
853 './test/test_data/config.inc.php',
857 './test/test_data/config-nonexisting.inc.php',
861 './libraries/config.default.php',
868 * Test for loading user preferences
871 * @todo Test actualy preferences loading
873 public function testLoadUserPreferences()
875 $this->assertNull($this->object->loadUserPreferences());
879 * Test for setting user config value
883 public function testSetUserValue()
885 $this->object->setUserValue(null, 'lang', 'cs', 'en');
886 $this->object->setUserValue("TEST_COOKIE_USER_VAL", '', 'cfg_val_1');
888 $this->object->getUserValue("TEST_COOKIE_USER_VAL", 'fail'),
894 * Test for getting user config value
898 public function testGetUserValue()
900 $this->assertEquals($this->object->getUserValue('test_val', 'val'), 'val');
904 * Should test getting unique value for theme
907 * @todo Implement testGetThemeUniqueValue().
909 public function testGetThemeUniqueValue()
912 $_SESSION['PMA_Theme'] = PMA_Theme
::load('./themes/pmahomme');
915 PHPUnit_Framework_Assert
::readAttribute($this->object, 'source_mtime') +
916 PHPUnit_Framework_Assert
::readAttribute(
918 'default_source_mtime'
920 $this->object->get('user_preferences_mtime') +
921 $_SESSION['PMA_Theme']->mtime_info +
922 $_SESSION['PMA_Theme']->filesize_info
925 $this->object->set('fontsize', 10);
926 $this->assertEquals(10 +
$partial_sum, $this->object->getThemeUniqueValue());
927 $this->object->set('fontsize', null);
929 $_COOKIE['pma_fontsize'] = 20;
930 $this->assertEquals(20 +
$partial_sum, $this->object->getThemeUniqueValue());
931 unset($_COOKIE['pma_fontsize']);
933 $this->assertEquals($partial_sum, $this->object->getThemeUniqueValue());
938 * Should test checking of config permissions
941 * @todo Implement testCheckPermissions().
943 public function testCheckPermissions()
945 // Remove the following lines when you implement this test.
946 $this->markTestIncomplete(
947 'This test has not been implemented yet.'
953 * Test for setting cookies
957 public function testSetCookie()
960 $this->object->setCookie(
968 $this->object->setCookie(
969 'TEST_CONFIG_COOKIE',
977 $this->object->setCookie(
978 'TEST_CONFIG_COOKIE',
984 $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
986 $this->object->setCookie(
987 'TEST_MANUAL_COOKIE',
996 * Test for isGitRevision
1000 public function testIsGitRevision()
1003 $this->object->isGitRevision()
1008 * Test for Check HTTP
1014 public function testCheckHTTP()
1017 $this->object->checkHTTP("http://www.phpmyadmin.net/test/data")
1019 $this->assertContains(
1021 $this->object->checkHTTP("http://www.phpmyadmin.net/test/data", true)
1024 $this->object->checkHTTP("http://www.phpmyadmin.net/test/nothing")
1029 * Tests for rewriting URL to SSL variant
1031 * @param string $original Original URL
1032 * @param string $expected Expected URL rewritten to SSL
1036 * @dataProvider sslUris
1038 public function testSSLUri($original, $expected)
1040 $this->object->set('PmaAbsoluteUri', $original);
1041 $this->assertEquals($expected, $this->object->getSSLUri());
1046 * return of ssl Uris
1050 public function sslUris()
1054 'http://server.foo/path/',
1055 'https://server.foo:443/path/'
1058 'http://server.foo:80/path/',
1059 'https://server.foo:443/path/'
1062 'http://server.foo.bar:123/path/',
1063 'https://server.foo.bar:443/path/'
1066 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/',
1067 'https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:443/'