2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA\libraries\Config class
6 * @package PhpMyAdmin-test
13 use PMA\libraries\Theme
;
15 require_once 'libraries/relation.lib.php';
16 require_once 'test/PMATestCase.php';
19 * Tests behaviour of PMA\libraries\Config class
21 * @package PhpMyAdmin-test
23 class ConfigTest
extends PMATestCase
26 * Turn off backup globals
28 protected $backupGlobals = false;
31 * @var PMA\libraries\Config
36 * @var object to test file permission
38 protected $permTestObj;
41 * Sets up the fixture, for example, opens a network connection.
42 * This method is called before a test is executed.
46 protected function setUp()
48 $this->object = new PMA\libraries\Config
;
49 $GLOBALS['server'] = 0;
50 $_SESSION['is_git_revision'] = true;
51 $GLOBALS['PMA_Config'] = new PMA\libraries\
Config(CONFIG_FILE
);
52 $GLOBALS['cfg']['ProxyUrl'] = '';
54 //for testing file permissions
55 $this->permTestObj
= new PMA\libraries\
Config("./config.sample.inc.php");
59 * Tears down the fixture, for example, closes a network connection.
60 * This method is called after a test is executed.
64 protected function tearDown()
68 unset($this->permTestObj
);
72 * Test for CheckSystem
77 public function testCheckSystem()
79 $this->object->checkSystem();
81 $this->assertNotNull($this->object->get('PMA_VERSION'));
82 $this->assertNotEmpty($this->object->get('PMA_THEME_VERSION'));
83 $this->assertNotEmpty($this->object->get('PMA_THEME_GENERATION'));
87 * Test for GetFontsizeForm
91 public function testGetFontsizeForm()
93 $this->assertContains(
94 '<form name="form_fontsize_selection" id="form_fontsize_selection"',
95 PMA\libraries\Config
::getFontsizeForm()
98 $this->assertContains(
99 '<label for="select_fontsize">',
100 PMA\libraries\Config
::getFontsizeForm()
103 //test getFontsizeOptions for "em" unit
104 $fontsize = $GLOBALS['PMA_Config']->get('fontsize');
105 $GLOBALS['PMA_Config']->set('fontsize', '');
106 $_COOKIE['pma_fontsize'] = "10em";
107 $this->assertContains(
108 '<option value="7em"',
109 PMA\libraries\Config
::getFontsizeForm()
111 $this->assertContains(
112 '<option value="8em"',
113 PMA\libraries\Config
::getFontsizeForm()
116 //test getFontsizeOptions for "pt" unit
117 $_COOKIE['pma_fontsize'] = "10pt";
118 $this->assertContains(
119 '<option value="2pt"',
120 PMA\libraries\Config
::getFontsizeForm()
122 $this->assertContains(
123 '<option value="4pt"',
124 PMA\libraries\Config
::getFontsizeForm()
127 //test getFontsizeOptions for "px" unit
128 $_COOKIE['pma_fontsize'] = "10px";
129 $this->assertContains(
130 '<option value="5px"',
131 PMA\libraries\Config
::getFontsizeForm()
133 $this->assertContains(
134 '<option value="6px"',
135 PMA\libraries\Config
::getFontsizeForm()
138 //test getFontsizeOptions for unknown unit
139 $_COOKIE['pma_fontsize'] = "10abc";
140 $this->assertContains(
141 '<option value="7abc"',
142 PMA\libraries\Config
::getFontsizeForm()
144 $this->assertContains(
145 '<option value="8abc"',
146 PMA\libraries\Config
::getFontsizeForm()
148 unset($_COOKIE['pma_fontsize']);
149 //rollback the fontsize setting
150 $GLOBALS['PMA_Config']->set('fontsize', $fontsize);
154 * Test for checkOutputCompression
158 public function testCheckOutputCompression()
161 $this->object->set('OBGzip', 'auto');
163 $this->object->set('PMA_USR_BROWSER_AGENT', 'IE');
164 $this->object->set('PMA_USR_BROWSER_VER', 6);
165 $this->object->checkOutputCompression();
166 $this->assertTrue($this->object->get("OBGzip"));
168 $this->object->set('OBGzip', 'auto');
169 $this->object->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
170 $this->object->set('PMA_USR_BROWSER_VER', 5);
171 $this->object->checkOutputCompression();
172 $this->assertTrue($this->object->get("OBGzip"));
176 * Tests client parsing code.
178 * @param string $agent User agent string
179 * @param string $os Expected parsed OS (or null if none)
180 * @param string $browser Expected parsed browser (or null if none)
181 * @param string $version Expected browser version (or null if none)
185 * @dataProvider userAgentProvider
187 public function testCheckClient($agent, $os, $browser = null, $version = null)
189 $_SERVER['HTTP_USER_AGENT'] = $agent;
190 $this->object->checkClient();
191 $this->assertEquals($os, $this->object->get('PMA_USR_OS'));
195 $this->object->get('PMA_USR_BROWSER_AGENT')
198 if ($version != null) {
201 $this->object->get('PMA_USR_BROWSER_VER')
207 * user Agent Provider
211 public function userAgentProvider()
215 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00',
221 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/'
222 . '528.16 OmniWeb/622.8.0.112941',
228 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)',
234 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
240 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; '
247 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; '
248 . '.NET4.0C; rv:11.0) like Gecko',
254 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; '
255 . '.NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; '
256 . '.NET CLR 3.0.30729; InfoPath.3; rv:11.0) like Gecko',
262 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, '
263 . 'like Gecko) Chrome/25.0.1364.172 Safari/537.22',
269 'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) '
270 . 'AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2',
276 'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208',
282 'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; '
283 . 'amd64; en_US) KHTML/4.5.4 (like Gecko)',
288 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0',
294 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 '
301 * @todo Is this version really expected?
304 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.4+ (KHTML, like G'
305 . 'ecko) Version/5.0 Safari/535.4+ SUSE/12.1 (3.2.1) Epiphany/3.2.1',
320 public function testCheckGd2()
322 $prevIsGb2Val = $this->object->get('PMA_IS_GD2');
324 $this->object->set('GD2Available', 'yes');
325 $this->object->checkGd2();
326 $this->assertEquals(1, $this->object->get('PMA_IS_GD2'));
328 $this->object->set('GD2Available', 'no');
329 $this->object->checkGd2();
330 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'));
332 $this->object->set('GD2Available', 'auto');
334 if (!@function_exists
('imagecreatetruecolor')) {
335 $this->object->checkGd2();
338 $this->object->get('PMA_IS_GD2'),
339 'imagecreatetruecolor does not exist, PMA_IS_GD2 should be 0'
343 if (@function_exists
('gd_info')) {
344 $this->object->checkGd2();
346 if (mb_strstr($gd_nfo["GD Version"], '2.')) {
349 $this->object->get('PMA_IS_GD2'),
350 'GD Version >= 2, PMA_IS_GD2 should be 1'
355 $this->object->get('PMA_IS_GD2'),
356 'GD Version < 2, PMA_IS_GD2 should be 0'
361 /* Get GD version string from phpinfo output */
363 phpinfo(INFO_MODULES
); /* Only modules */
364 $a = strip_tags(ob_get_contents());
367 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
368 if (mb_strstr($v, '2.')) {
371 $this->object->get('PMA_IS_GD2'),
372 'PMA_IS_GD2 should be 1'
377 $this->object->get('PMA_IS_GD2'),
378 'PMA_IS_GD2 should be 0'
385 * Web server detection test
387 * @param string $server Server identification
388 * @param boolean $iis Whether server should be detected as IIS
392 * @dataProvider serverNames
394 public function testCheckWebServer($server, $iis)
396 $_SERVER['SERVER_SOFTWARE'] = $server;
397 $this->object->checkWebServer();
398 $this->assertEquals($iis, $this->object->get('PMA_IS_IIS'));
399 unset($_SERVER['SERVER_SOFTWARE']);
404 * return server names
408 public function serverNames()
424 * test for CheckWebServerOs
428 public function testCheckWebServerOs()
430 $this->object->checkWebServerOs();
432 if (defined('PHP_OS')) {
433 if (stristr(PHP_OS
, 'darwin')) {
434 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
435 } elseif (stristr(PHP_OS
, 'win')) {
436 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
437 } elseif (stristr(PHP_OS
, 'OS/2')) {
438 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
439 } elseif (stristr(PHP_OS
, 'Linux')) {
440 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
442 $this->markTestIncomplete('Not known PHP_OS: ' . PHP_OS
);
445 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
447 define('PHP_OS', 'Windows');
448 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'));
453 * Tests loading of default values
459 public function testLoadDefaults()
461 $prevDefaultSource = $this->object->default_source
;
463 $this->object->default_source
= 'unexisted.file.php';
464 $this->assertFalse($this->object->loadDefaults());
466 $this->object->default_source
= $prevDefaultSource;
468 include $this->object->default_source
;
473 $this->assertTrue($this->object->loadDefaults());
476 $this->object->default_source_mtime
,
477 filemtime($prevDefaultSource)
480 $loadedConf['Servers'][1],
481 $this->object->default_server
484 unset($loadedConf['Servers']);
486 $this->assertEquals($loadedConf, $this->object->default);
488 $expectedSettings = array_replace_recursive(
489 $this->object->settings
,
495 $this->object->settings
,
496 'Settings loaded wrong'
499 $this->assertFalse($this->object->error_config_default_file
);
503 * test for CheckConfigSource
507 public function testCheckConfigSource()
509 $this->object->setSource('unexisted.config.php');
510 $this->assertFalse($this->object->checkConfigSource());
511 $this->assertEquals(0, $this->object->source_mtime
);
513 $this->object->setSource('libraries/config.default.php');
515 $this->assertNotEmpty($this->object->getSource());
516 $this->assertTrue($this->object->checkConfigSource());
520 * Test getting and setting config values
524 public function testGetAndSet()
526 $this->assertNull($this->object->get("unresisting_setting"));
528 $this->object->set('test_setting', 'test_value');
530 $this->assertEquals('test_value', $this->object->get('test_setting'));
534 * Tests setting configuration source
538 public function testGetSetSource()
540 echo $this->object->getSource();
542 $this->assertEmpty($this->object->getSource(), "Source is null by default");
544 $this->object->setSource("config.sample.inc.php");
547 "config.sample.inc.php",
548 $this->object->getSource(),
549 "Cant set new source"
554 * test for CheckCollationConnection
558 public function testCheckCollationConnection()
560 $_REQUEST['collation_connection'] = 'utf-8';
561 $this->object->checkCollationConnection();
564 $_REQUEST['collation_connection'],
565 $this->object->get('collation_connection')
574 * @dataProvider httpsParams
576 public function testIsHttps($scheme, $https, $uri, $lb, $front, $proto, $port, $expected)
578 $_SERVER['HTTP_SCHEME'] = $scheme;
579 $_SERVER['HTTPS'] = $https;
580 $_SERVER['REQUEST_URI'] = $uri;
581 $_SERVER['HTTP_HTTPS_FROM_LB'] = $lb;
582 $_SERVER['HTTP_FRONT_END_HTTPS'] = $front;
583 $_SERVER['HTTP_X_FORWARDED_PROTO'] = $proto;
584 $_SERVER['SERVER_PORT'] = $port;
586 $this->object->set('is_https', null);
587 $this->assertEquals($expected, $this->object->isHttps());
591 * Data provider for https detection
595 public function httpsParams()
598 array('http', '', '', '', '', 'http', 80, false),
599 array('http', '', 'http://', '', '', 'http', 80, false),
600 array('http', '', '', '', '', 'http', 443, true),
601 array('http', '', '', '', '', 'https', 80, true),
602 array('http', '', '', '', 'on', 'http', 80, true),
603 array('http', '', '', 'on', '', 'http', 80, true),
604 array('http', '', 'https://', '', '', 'http', 80, true),
605 array('http', 'on', '', '', '', 'http', 80, true),
606 array('https', '', '', '', '', 'http', 80, true),
611 * Test for backward compatibility globals
615 * @depends testCheckSystem
616 * @depends testCheckWebServer
617 * @depends testLoadDefaults
621 public function testEnableBc()
623 $this->object->enableBc();
628 'PMA_THEME_GENERATION',
632 'PMA_USR_BROWSER_VER',
633 'PMA_USR_BROWSER_AGENT'
636 foreach ($defines as $define) {
637 $this->assertTrue(defined($define));
638 $this->assertEquals(constant($define), $this->object->get($define));
643 * Test for getting root path
645 * @param string $request The request URL used for phpMyAdmin
646 * @param string $absolute The absolute URL used for phpMyAdmin
647 * @param string $expected Expected root path
651 * @dataProvider rootUris
653 public function testGetRootPath($request, $absolute, $expected)
655 $GLOBALS['PMA_PHP_SELF'] = $request;
656 $this->object->set('PmaAbsoluteUri', $absolute);
657 $this->assertEquals($expected, $this->object->getRootPath());
661 * Data provider for testGetRootPath
663 * @return array data for testGetRootPath
665 public function rootUris()
694 '\\path\\to\\index.php',
699 '/foo/bar/phpmyadmin/index.php',
701 '/foo/bar/phpmyadmin/',
704 '/foo/bar/phpmyadmin/',
706 '/foo/bar/phpmyadmin/',
709 'https://example.net/baz/phpmyadmin/',
714 'http://example.net/baz/phpmyadmin/',
719 'http://example.net/phpmyadmin/',
724 'http://example.net/',
729 'http://example.net/',
730 'http://example.net/phpmyadmin/',
734 'http://example.net/',
735 'http://example.net/phpmyadmin',
739 'http://example.net/',
744 'http://example.net/',
752 * Tests loading of config file
754 * @param string $source File name of config to load
755 * @param boolean $result Expected result of loading
759 * @dataProvider configPaths
761 public function testLoad($source, $result)
764 $this->assertTrue($this->object->load($source));
766 $this->assertFalse($this->object->load($source));
771 * return of config Paths
775 public function configPaths()
779 './test/test_data/config.inc.php',
783 './test/test_data/config-nonexisting.inc.php',
787 './libraries/config.default.php',
794 * Test for loading user preferences
797 * @todo Test actually preferences loading
799 public function testLoadUserPreferences()
801 $this->assertNull($this->object->loadUserPreferences());
805 * Test for setting user config value
809 public function testSetUserValue()
811 $this->object->setUserValue(null, 'lang', 'cs', 'en');
812 $this->object->setUserValue("TEST_COOKIE_USER_VAL", '', 'cfg_val_1');
814 $this->object->getUserValue("TEST_COOKIE_USER_VAL", 'fail'),
820 * Test for getting user config value
824 public function testGetUserValue()
826 $this->assertEquals($this->object->getUserValue('test_val', 'val'), 'val');
830 * Should test getting unique value for theme
834 public function testGetThemeUniqueValue()
839 PHPUnit_Framework_Assert
::readAttribute($this->object, 'source_mtime') +
840 PHPUnit_Framework_Assert
::readAttribute(
842 'default_source_mtime'
844 $this->object->get('user_preferences_mtime') +
845 $_SESSION['PMA_Theme']->mtime_info +
846 $_SESSION['PMA_Theme']->filesize_info
849 $this->object->set('fontsize', 10);
850 $this->assertEquals(10 +
$partial_sum, $this->object->getThemeUniqueValue());
851 $this->object->set('fontsize', null);
853 $_COOKIE['pma_fontsize'] = 20;
854 $this->assertEquals(20 +
$partial_sum, $this->object->getThemeUniqueValue());
855 unset($_COOKIE['pma_fontsize']);
857 $this->assertEquals($partial_sum, $this->object->getThemeUniqueValue());
862 * Should test checking of config permissions
866 public function testCheckPermissions()
868 //load file permissions for the current permissions file
869 $perms = @fileperms
($this->object->getSource());
870 //testing for permissions for no configuration file
871 $this->assertFalse(!($perms === false) && ($perms & 2));
873 //load file permissions for the current permissions file
874 $perms = @fileperms
($this->permTestObj
->getSource());
875 //testing for permissions
876 $this->assertFalse(!($perms === false) && ($perms & 2));
878 //if the above assertion is false then applying further assertions
879 if (!($perms === false) && ($perms & 2)) {
880 $this->assertFalse($this->permTestObj
->get('PMA_IS_WINDOWS') == 0);
886 * Test for setting cookies
890 public function testSetCookie()
893 $this->object->setCookie(
901 $this->object->setCookie(
902 'TEST_CONFIG_COOKIE',
910 $this->object->setCookie(
911 'TEST_CONFIG_COOKIE',
917 $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
919 $this->object->setCookie(
920 'TEST_MANUAL_COOKIE',
929 * Test for isGitRevision
933 public function testIsGitRevision()
936 $this->object->isGitRevision()