Translated using Weblate (Japanese)
[phpmyadmin.git] / test / classes / ConfigTest.php
blob311240c727d536cb2fdfa2bc3e0a488d5b069a8b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Test for PMA\libraries\Config class
6 * @package PhpMyAdmin-test
7 * @group current
8 */
11 * Include to test.
13 use PMA\libraries\Theme;
15 require_once 'libraries/relation.lib.php';
16 require_once 'test/PMATestCase.php';
18 /**
19 * Tests behaviour of PMA\libraries\Config class
21 * @package PhpMyAdmin-test
23 class ConfigTest extends PMATestCase
25 /**
26 * Turn off backup globals
28 protected $backupGlobals = false;
30 /**
31 * @var PMA\libraries\Config
33 protected $object;
35 /**
36 * @var object to test file permission
38 protected $permTestObj;
40 /**
41 * Sets up the fixture, for example, opens a network connection.
42 * This method is called before a test is executed.
44 * @return void
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");
58 /**
59 * Tears down the fixture, for example, closes a network connection.
60 * This method is called after a test is executed.
62 * @return void
64 protected function tearDown()
66 parent::tearDown();
67 unset($this->object);
68 unset($this->permTestObj);
71 /**
72 * Test for CheckSystem
74 * @return void
75 * @group medium
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'));
86 /**
87 * Test for GetFontsizeForm
89 * @return void
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
156 * @return void
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)
183 * @return void
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'));
192 if ($os != null) {
193 $this->assertEquals(
194 $browser,
195 $this->object->get('PMA_USR_BROWSER_AGENT')
198 if ($version != null) {
199 $this->assertEquals(
200 $version,
201 $this->object->get('PMA_USR_BROWSER_VER')
207 * user Agent Provider
209 * @return array
211 public function userAgentProvider()
213 return array(
214 array(
215 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00',
216 'Linux',
217 'OPERA',
218 '9.80',
220 array(
221 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/'
222 . '528.16 OmniWeb/622.8.0.112941',
223 'Mac',
224 'OMNIWEB',
225 '622',
227 array(
228 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)',
229 'Win',
230 'IE',
231 '8.0',
233 array(
234 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
235 'Win',
236 'IE',
237 '9.0',
239 array(
240 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; '
241 . 'Trident/6.0)',
242 'Win',
243 'IE',
244 '10.0',
246 array(
247 'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; '
248 . '.NET4.0C; rv:11.0) like Gecko',
249 'Win',
250 'IE',
251 '11.0',
253 array(
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',
257 'Win',
258 'IE',
259 '11.0',
261 array(
262 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, '
263 . 'like Gecko) Chrome/25.0.1364.172 Safari/537.22',
264 'Win',
265 'CHROME',
266 '25.0.1364.172',
268 array(
269 'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) '
270 . 'AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2',
271 'Unix',
272 'SAFARI',
273 '5.0.419',
275 array(
276 'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208',
277 'Win',
278 'GECKO',
279 '1.9',
281 array(
282 'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; '
283 . 'amd64; en_US) KHTML/4.5.4 (like Gecko)',
284 'Other',
285 'KONQUEROR',
287 array(
288 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0',
289 'Linux',
290 'FIREFOX',
291 '5.0',
293 array(
294 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 '
295 . 'Firefox/12.0',
296 'Linux',
297 'FIREFOX',
298 '12.0',
301 * @todo Is this version really expected?
303 array(
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',
306 'Linux',
307 'SAFARI',
308 '5.0',
316 * test for CheckGd2
318 * @return void
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();
336 $this->assertEquals(
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();
345 $gd_nfo = gd_info();
346 if (mb_strstr($gd_nfo["GD Version"], '2.')) {
347 $this->assertEquals(
349 $this->object->get('PMA_IS_GD2'),
350 'GD Version >= 2, PMA_IS_GD2 should be 1'
352 } else {
353 $this->assertEquals(
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 */
362 ob_start();
363 phpinfo(INFO_MODULES); /* Only modules */
364 $a = strip_tags(ob_get_contents());
365 ob_end_clean();
367 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
368 if (mb_strstr($v, '2.')) {
369 $this->assertEquals(
371 $this->object->get('PMA_IS_GD2'),
372 'PMA_IS_GD2 should be 1'
374 } else {
375 $this->assertEquals(
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
390 * @return void
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
406 * @return array
408 public function serverNames()
410 return array(
411 array(
412 "Microsoft-IIS 7.0",
415 array(
416 "Apache/2.2.17",
424 * test for CheckWebServerOs
426 * @return void
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'));
441 } else {
442 $this->markTestIncomplete('Not known PHP_OS: ' . PHP_OS);
444 } else {
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
455 * @return void
457 * @group large
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;
470 $loadedConf = $cfg;
471 unset($cfg);
473 $this->assertTrue($this->object->loadDefaults());
475 $this->assertEquals(
476 $this->object->default_source_mtime,
477 filemtime($prevDefaultSource)
479 $this->assertEquals(
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,
490 $loadedConf
493 $this->assertEquals(
494 $expectedSettings,
495 $this->object->settings,
496 'Settings loaded wrong'
499 $this->assertFalse($this->object->error_config_default_file);
503 * test for CheckConfigSource
505 * @return void
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
522 * @return void
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
536 * @return void
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");
546 $this->assertEquals(
547 "config.sample.inc.php",
548 $this->object->getSource(),
549 "Cant set new source"
554 * test for CheckCollationConnection
556 * @return void
558 public function testCheckCollationConnection()
560 $_REQUEST['collation_connection'] = 'utf-8';
561 $this->object->checkCollationConnection();
563 $this->assertEquals(
564 $_REQUEST['collation_connection'],
565 $this->object->get('collation_connection')
570 * test for IsHttp
572 * @return void
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
593 * @return array
595 public function httpsParams()
597 return array(
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
613 * @return void
615 * @depends testCheckSystem
616 * @depends testCheckWebServer
617 * @depends testLoadDefaults
619 * @group large
621 public function testEnableBc()
623 $this->object->enableBc();
625 $defines = array(
626 'PMA_VERSION',
627 'PMA_THEME_VERSION',
628 'PMA_THEME_GENERATION',
629 'PMA_IS_WINDOWS',
630 'PMA_IS_GD2',
631 'PMA_USR_OS',
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
649 * @return void
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()
667 return array(
668 array(
671 '/',
673 array(
674 '/',
676 '/',
678 array(
679 '/index.php',
681 '/',
683 array(
684 '\\index.php',
686 '/',
688 array(
689 '\\',
691 '/',
693 array(
694 '\\path\\to\\index.php',
696 '/path/to/',
698 array(
699 '/foo/bar/phpmyadmin/index.php',
701 '/foo/bar/phpmyadmin/',
703 array(
704 '/foo/bar/phpmyadmin/',
706 '/foo/bar/phpmyadmin/',
708 array(
709 'https://example.net/baz/phpmyadmin/',
711 '/baz/phpmyadmin/',
713 array(
714 'http://example.net/baz/phpmyadmin/',
716 '/baz/phpmyadmin/',
718 array(
719 'http://example.net/phpmyadmin/',
721 '/phpmyadmin/',
723 array(
724 'http://example.net/',
726 '/',
728 array(
729 'http://example.net/',
730 'http://example.net/phpmyadmin/',
731 '/phpmyadmin/',
733 array(
734 'http://example.net/',
735 'http://example.net/phpmyadmin',
736 '/phpmyadmin/',
738 array(
739 'http://example.net/',
740 '/phpmyadmin2',
741 '/phpmyadmin2/',
743 array(
744 'http://example.net/',
745 '/phpmyadmin3/',
746 '/phpmyadmin3/',
752 * Tests loading of config file
754 * @param string $source File name of config to load
755 * @param boolean $result Expected result of loading
757 * @return void
759 * @dataProvider configPaths
761 public function testLoad($source, $result)
763 if ($result) {
764 $this->assertTrue($this->object->load($source));
765 } else {
766 $this->assertFalse($this->object->load($source));
771 * return of config Paths
773 * @return array
775 public function configPaths()
777 return array(
778 array(
779 './test/test_data/config.inc.php',
780 true,
782 array(
783 './test/test_data/config-nonexisting.inc.php',
784 false,
786 array(
787 './libraries/config.default.php',
788 true,
794 * Test for loading user preferences
796 * @return void
797 * @todo Test actually preferences loading
799 public function testLoadUserPreferences()
801 $this->assertNull($this->object->loadUserPreferences());
805 * Test for setting user config value
807 * @return void
809 public function testSetUserValue()
811 $this->object->setUserValue(null, 'lang', 'cs', 'en');
812 $this->object->setUserValue("TEST_COOKIE_USER_VAL", '', 'cfg_val_1');
813 $this->assertEquals(
814 $this->object->getUserValue("TEST_COOKIE_USER_VAL", 'fail'),
815 'cfg_val_1'
820 * Test for getting user config value
822 * @return void
824 public function testGetUserValue()
826 $this->assertEquals($this->object->getUserValue('test_val', 'val'), 'val');
830 * Should test getting unique value for theme
832 * @return void
834 public function testGetThemeUniqueValue()
838 $partial_sum = (
839 PHPUnit_Framework_Assert::readAttribute($this->object, 'source_mtime') +
840 PHPUnit_Framework_Assert::readAttribute(
841 $this->object,
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
864 * @return void
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
888 * @return void
890 public function testSetCookie()
892 $this->assertFalse(
893 $this->object->setCookie(
894 'TEST_DEF_COOKIE',
895 'test_def_123',
896 'test_def_123'
900 $this->assertTrue(
901 $this->object->setCookie(
902 'TEST_CONFIG_COOKIE',
903 'test_val_123',
904 null,
905 3600
909 $this->assertTrue(
910 $this->object->setCookie(
911 'TEST_CONFIG_COOKIE',
913 'default_val'
917 $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
918 $this->assertTrue(
919 $this->object->setCookie(
920 'TEST_MANUAL_COOKIE',
921 'other',
922 'other'
929 * Test for isGitRevision
931 * @return void
933 public function testIsGitRevision()
935 $this->assertTrue(
936 $this->object->isGitRevision()