fix coding style : control structures
[phpmyadmin.git] / test / classes / PMA_Config_test.php
blob9a5bf7845b866042d584b254dbffd60b75aaf1e6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Test for PMA_Config class
7 * @package phpMyAdmin-test
8 * @group current
9 */
12 * Include to test.
14 require_once 'libraries/Config.class.php';
15 require_once 'libraries/relation.lib.php';
17 class PMA_ConfigTest extends PHPUnit_Framework_TestCase
19 /**
20 * Turn off backup globals
22 protected $backupGlobals = FALSE;
24 /**
25 * @var PMA_Config
27 protected $object;
29 /**
30 * Sets up the fixture, for example, opens a network connection.
31 * This method is called before a test is executed.
33 protected function setUp()
35 $this->object = new PMA_Config;
38 /**
39 * Tears down the fixture, for example, closes a network connection.
40 * This method is called after a test is executed.
42 protected function tearDown()
46 public function testCheckSystem()
48 $this->object->checkSystem();
50 $this->assertNotNull($this->object->get('PMA_VERSION'));
51 $this->assertNotEmpty($this->object->get('PMA_THEME_VERSION'));
52 $this->assertNotEmpty($this->object->get('PMA_THEME_GENERATION'));
55 public function testCheckOutputCompression()
58 $this->object->set('OBGzip', 'auto');
60 $this->object->set('PMA_USR_BROWSER_AGENT', 'IE');
61 $this->object->set('PMA_USR_BROWSER_VER', 6);
62 $this->object->checkOutputCompression();
63 $this->assertFalse($this->object->get("OBGzip"));
65 $this->object->set('OBGzip', 'auto');
66 $this->object->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
67 $this->object->set('PMA_USR_BROWSER_VER', 5);
68 $this->object->checkOutputCompression();
69 $this->assertEquals('auto',$this->object->get("OBGzip"));
71 ini_set('zlib.output_compression', 'Off');
72 $this->object->checkOutputCompression();
73 $this->assertFalse($this->object->get("OBGzip"));
75 ini_set('zlib.output_compression', 'On');
78 public function testCheckClient()
80 $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00';
81 $this->object->checkClient();
82 $this->assertEquals("Linux", $this->object->get('PMA_USR_OS'), "User OS expected to be Linux");
83 $this->assertEquals("OPERA", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be Opera");
84 $this->assertEquals("9.80", $this->object->get('PMA_USR_BROWSER_VER'), "Browser ver expected to be 9.80");
86 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/528.16 OmniWeb/622.8.0.112941';
87 $this->object->checkClient();
88 $this->assertEquals("Mac", $this->object->get('PMA_USR_OS'), "User OS expected to be Mac");
89 $this->assertEquals("OMNIWEB", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be OmniWeb");
91 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)';
92 $this->object->checkClient();
93 $this->assertEquals("Win", $this->object->get('PMA_USR_OS'), "User OS expected to be Windows");
94 $this->assertEquals("IE", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be IE");
96 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2';
97 $this->object->checkClient();
98 $this->assertEquals("Unix", $this->object->get('PMA_USR_OS'), "User OS expected to be Unix");
100 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; OS/2 Webexplorer)';
101 $this->object->checkClient();
102 $this->assertEquals("OS/2", $this->object->get('PMA_USR_OS'), "User OS expected to be OS/2");
104 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208';
105 $this->object->checkClient();
106 $this->assertEquals("GECKO", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be Gecko");
108 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; amd64; en_US) KHTML/4.5.4 (like Gecko)';
109 $this->object->checkClient();
110 $this->assertEquals("KONQUEROR", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be Konqueror");
112 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0';
113 $this->object->checkClient();
114 $this->assertEquals("MOZILLA", $this->object->get('PMA_USR_BROWSER_AGENT'), "Browser expected to be Mozilla");
115 $this->assertEquals("Linux", $this->object->get('PMA_USR_OS'), "User OS expected to be Linux");
119 public function testCheckGd2()
121 $prevIsGb2Val = $this->object->get('PMA_IS_GD2');
123 $this->object->set('GD2Available','yes');
124 $this->object->checkGd2();
125 $this->assertEquals(1, $this->object->get('PMA_IS_GD2'));
127 $this->object->set('GD2Available','no');
128 $this->object->checkGd2();
129 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'));
131 $this->object->set('GD2Available',$prevIsGb2Val);
133 if (!@function_exists('imagecreatetruecolor')) {
134 $this->object->checkGd2();
135 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'), 'Function imagecreatetruecolor does not exist, PMA_IS_GD2 should be 0');
138 if (@function_exists('gd_info')) {
139 $this->object->checkGd2();
140 $gd_nfo = gd_info();
141 if (strstr($gd_nfo["GD Version"], '2.')) {
142 $this->assertEquals(1, $this->object->get('PMA_IS_GD2'), 'GD Version >= 2, PMA_IS_GD2 should be 1');
143 } else {
144 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'), 'GD Version < 2, PMA_IS_GD2 should be 0');
148 /* Get GD version string from phpinfo output */
149 ob_start();
150 phpinfo(INFO_MODULES); /* Only modules */
151 $a = strip_tags(ob_get_contents());
152 ob_end_clean();
154 if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
155 if (strstr($v, '2.')) {
156 $this->assertEquals(1, $this->object->get('PMA_IS_GD2'), 'PMA_IS_GD2 should be 1');
157 } else {
158 $this->assertEquals(0, $this->object->get('PMA_IS_GD2'), 'PMA_IS_GD2 should be 0');
163 public function testCheckWebServer()
165 $_SERVER['SERVER_SOFTWARE'] = "Microsoft-IIS 7.0";
166 $this->object->checkWebServer();
167 $this->assertEquals(1, $this->object->get('PMA_IS_IIS'));
169 $_SERVER['SERVER_SOFTWARE'] = "Apache/2.2.17";
170 $this->object->checkWebServer();
171 $this->assertEquals(0, $this->object->get('PMA_IS_IIS'));
173 unset($_SERVER['SERVER_SOFTWARE']);
176 public function testCheckWebServerOs()
178 $this->object->checkWebServerOs();
180 if (defined('PHP_OS')) {
181 switch (PHP_OS) {
182 case stristr(PHP_OS,'win'):
183 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'), 'PHP_OS equals: ' . PHP_OS . ' PMA_IS_WINDOWS should be 1');
184 break;
185 case stristr(PHP_OS, 'OS/2'):
186 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'), 'PHP_OS is OS/2 PMA_IS_WINDOWS should be 1 (No file permissions like Windows)');
187 break;
188 case stristr(PHP_OS, 'Linux'):
189 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
190 break;
192 } else {
193 $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'), 'PMA_IS_WINDOWS Default to Unix or Equiv');
195 define('PHP_OS','Windows');
196 $this->assertEquals(1, $this->object->get('PMA_IS_WINDOWS'), 'PMA_IS_WINDOWS must be 1');
200 public function testCheckPhpVersion()
202 $this->object->checkPhpVersion();
204 $php_int_ver = 0;
205 $php_str_ver = phpversion();
207 $match = array();
208 preg_match('@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@', phpversion(), $match);
209 if (isset($match) && ! empty($match[1])) {
210 if (! isset($match[2])) {
211 $match[2] = 0;
213 if (! isset($match[3])) {
214 $match[3] = 0;
216 $php_int_ver = (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3]);
217 } else {
218 $php_int_ver = 0;
221 $this->assertEquals($php_str_ver, $this->object->get('PMA_PHP_STR_VERSION'));
222 $this->assertEquals($php_int_ver, $this->object->get('PMA_PHP_INT_VERSION'));
225 public function testLoadDefaults()
227 $prevDefaultSource = $this->object->default_source;
229 $this->object->default_source = 'unexisted.file.php';
230 $this->assertFalse($this->object->loadDefaults());
232 $this->object->default_source = $prevDefaultSource;
234 include $this->object->default_source;
236 $loadedConf = $cfg;
237 unset($cfg);
239 $this->assertTrue($this->object->loadDefaults());
241 $this->assertEquals($this->object->default_source_mtime, filemtime($prevDefaultSource));
242 $this->assertEquals($loadedConf['Servers'][1], $this->object->default_server);
244 unset($loadedConf['Servers']);
246 $this->assertEquals($loadedConf, $this->object->default);
248 $expectedSettings = PMA_array_merge_recursive($this->object->settings, $loadedConf);
250 $this->assertEquals($expectedSettings, $this->object->settings,'Settings loaded wrong');
252 $this->assertFalse($this->object->error_config_default_file);
255 public function testCheckConfigSource()
257 $this->object->setSource('unexisted.config.php');
258 $this->assertFalse($this->object->checkConfigSource());
259 $this->assertEquals(0, $this->object->source_mtime);
261 // if(! is_readable($this->object->getSource()))
262 // $this->markTestSkipped('Configuration file is read only');
264 $this->object->setSource('libraries/config.default.php');
266 $this->assertNotEmpty($this->object->getSource());
267 $this->assertTrue($this->object->checkConfigSource());
271 * @covers PMA_Config::get
272 * @covers PMA_Config::set
273 * @return void
275 public function testGetAndSet()
277 $this->assertNull($this->object->get("unresisting_setting"));
279 $this->object->set('test_setting', 'test_value');
281 $this->assertEquals('test_value', $this->object->get('test_setting'));
285 * @covers PMA_Config::getSource
286 * @covers PMA_Config::setSource
288 public function testGetSetSource()
290 echo $this->object->getSource();
292 $this->assertEmpty($this->object->getSource(), "Source is null by default");
294 $this->object->setSource("config.sample.inc.php");
296 $this->assertEquals("config.sample.inc.php", $this->object->getSource(), "Cant set new source");
299 public function testCheckPmaAbsoluteUriEmpty()
301 $this->object->set('PmaAbsoluteUri','');
302 $this->assertFalse($this->object->checkPmaAbsoluteUri(), 'PmaAbsoluteUri is not set and should be error');
303 $this->assertTrue($this->object->error_pma_uri, 'PmaAbsoluteUri is not set and should be error');
307 * @depends testCheckPmaAbsoluteUriEmpty
309 public function testCheckPmaAbsoluteUriNormal()
311 $this->object->set('PmaAbsoluteUri','http://localhost/phpmyadmin/');
312 $this->object->checkPmaAbsoluteUri();
313 $this->assertEquals("http://localhost/phpmyadmin/", $this->object->get('PmaAbsoluteUri'));
315 $this->object->set('PmaAbsoluteUri','http://localhost/phpmyadmin');
316 $this->object->checkPmaAbsoluteUri();
317 $this->assertEquals("http://localhost/phpmyadmin/", $this->object->get('PmaAbsoluteUri'), 'Expected trailing slash at the end of the phpMyAdmin uri');
322 * @depends testCheckPmaAbsoluteUriNormal
324 public function testCheckPmaAbsoluteUriScheme()
326 $_SERVER['HTTP_HOST'] = 'localhost';
327 $_SERVER['HTTP_SCHEME'] = 'http';
328 $_SERVER['HTTPS'] = 'off';
329 $GLOBALS['PMA_PHP_SELF'] = 'index.php';
331 $this->object->set('PmaAbsoluteUri','');
333 $this->object->checkPmaAbsoluteUri();
334 $this->assertEquals("http://localhost/", $this->object->get('PmaAbsoluteUri'));
338 * @depends testCheckPmaAbsoluteUriScheme
340 public function testCheckPmaAbsoluteUriUser()
342 $this->object->set('PmaAbsoluteUri','http://user:pwd@localhost/phpmyadmin/index.php');
344 $this->object->checkPmaAbsoluteUri();
345 $this->assertEquals("http://user:pwd@localhost/phpmyadmin/index.php/", $this->object->get('PmaAbsoluteUri'));
347 $this->object->set('PmaAbsoluteUri','https://user:pwd@localhost/phpmyadmin/index.php');
349 $this->object->checkPmaAbsoluteUri();
350 $this->assertEquals("https://user:pwd@localhost/phpmyadmin/index.php/", $this->object->get('PmaAbsoluteUri'));
353 public function testCheckCollationConnection()
355 $_REQUEST['collation_connection'] = 'utf-8';
356 $this->object->checkCollationConnection();
358 $this->assertEquals($_REQUEST['collation_connection'], $this->object->get('collation_connection'));
361 public function testIsHttps()
363 $this->object->set('PmaAbsoluteUri', 'http://some_host.com/phpMyAdmin');
364 $this->assertFalse($this->object->isHttps());
366 $this->object->set('PmaAbsoluteUri', 'https://some_host.com/phpMyAdmin');
367 $this->assertFalse($this->object->isHttps());
370 public function testDetectHttps()
372 unset($_SERVER['REQUEST_URI']);
373 unset($_SERVER['HTTP_SCHEME']);
374 unset($_SERVER['HTTPS']);
376 $this->assertFalse($this->object->detectHttps());
378 $_SERVER['REQUEST_URI'] = '/url:\this_is_not_url';
379 $this->assertFalse($this->object->detectHttps());
381 $_SERVER['REQUEST_URI'] = 'file://localhost/phpmyadmin/index.php';
382 $this->assertFalse($this->object->detectHttps());
384 $_ENV['REQUEST_URI'] = 'http://localhost/phpmyadmin/index.php';
385 $this->assertFalse($this->object->detectHttps());
387 $_SERVER['REQUEST_URI'] = 'https://localhost/phpmyadmin/index.php';
388 $this->assertTrue($this->object->detectHttps());
390 $_SERVER['REQUEST_URI'] = 'localhost/phpmyadmin/index.php';
391 $_SERVER['HTTP_SCHEME'] = 'https';
392 $_SERVER['HTTPS'] = 'on';
393 $this->assertTrue($this->object->detectHttps());
397 * @depends testDetectHttps
399 public function testCheckCookiePath()
401 $this->object->checkCookiePath();
402 echo $this->object->get('cookie_path');
403 $this->assertEquals('',$this->object->get('cookie_path'));
407 * @depends testCheckSystem
408 * @depends testCheckWebServer
409 * @depends testLoadDefaults
410 * @depends testLoad
412 public function testEnableBc()
414 $this->object->enableBc();
416 $defines = array(
417 'PMA_VERSION',
418 'PMA_THEME_VERSION',
419 'PMA_THEME_GENERATION',
420 'PMA_PHP_STR_VERSION',
421 'PMA_PHP_INT_VERSION',
422 'PMA_IS_WINDOWS',
423 'PMA_IS_IIS',
424 'PMA_IS_GD2',
425 'PMA_USR_OS',
426 'PMA_USR_BROWSER_VER',
427 'PMA_USR_BROWSER_AGENT'
430 foreach ($defines as $define) {
431 $this->assertTrue(defined($define));
432 $this->assertEquals(constant($define), $this->object->get($define));
437 * @todo Implement testSave().
439 public function testSave()
441 // Remove the following lines when you implement this test.
442 $this->markTestIncomplete(
443 'This test has not been implemented yet.'
448 * @todo Implement testGetFontsizeForm().
450 public function testGetFontsizeForm()
452 // Remove the following lines when you implement this test.
453 $this->markTestIncomplete(
454 'This test has not been implemented yet.'
459 * @todo Implement testRemoveCookie().
461 public function testRemoveCookie()
463 // Remove the following lines when you implement this test.
464 $this->markTestIncomplete(
465 'This test has not been implemented yet.'
469 * @todo Implement testCheckFontsize().
471 public function testCheckFontsize()
473 // Remove the following lines when you implement this test.
474 $this->markTestIncomplete(
475 'This test has not been implemented yet.'
480 * @todo Implement testCheckUpload().
482 public function testCheckUpload()
484 // Remove the following lines when you implement this test.
485 $this->markTestIncomplete(
486 'This test has not been implemented yet.'
491 * @todo Implement testCheckUploadSize().
493 public function testCheckUploadSize()
495 // Remove the following lines when you implement this test.
496 $this->markTestIncomplete(
497 'This test has not been implemented yet.'
502 * @todo Implement testCheckIsHttps().
504 public function testCheckIsHttps()
506 // Remove the following lines when you implement this test.
507 $this->markTestIncomplete(
508 'This test has not been implemented yet.'
513 * @todo Implement testGetCookiePath().
515 public function testGetCookiePath()
517 // Remove the following lines when you implement this test.
518 $this->markTestIncomplete(
519 'This test has not been implemented yet.'
524 * @todo finish implementing test + dependencies
526 public function testLoad()
528 $this->assertFalse($this->object->load());
530 $this->assertTrue($this->object->load('./libraries/config.default.php'));
534 * @todo Implement testLoadUserPreferences().
536 public function testLoadUserPreferences()
538 $this->assertNull($this->object->loadUserPreferences());
540 // echo $GLOBALS['cfg']['ServerDefault'];
544 * @todo Implement testSetUserValue().
546 public function testSetUserValue()
548 $this->object->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
549 $this->object->setUserValue("TEST_COOKIE_USER_VAL",'','cfg_val_1');
551 // Remove the following lines when you implement this test.
552 // $this->markTestIncomplete(
553 // 'This test has not been implemented yet.'
554 // );
558 * @todo Implement testGetUserValue().
560 public function testGetUserValue()
562 // Remove the following lines when you implement this test.
563 $this->markTestIncomplete(
564 'This test has not been implemented yet.'
569 * @todo Implement testGetThemeUniqueValue().
571 public function testGetThemeUniqueValue()
573 // Remove the following lines when you implement this test.
574 $this->markTestIncomplete(
575 'This test has not been implemented yet.'
580 * @todo Implement testCheckPermissions().
582 public function testCheckPermissions()
584 // Remove the following lines when you implement this test.
585 $this->markTestIncomplete(
586 'This test has not been implemented yet.'
592 * @todo Implement testSetCookie().
594 public function testSetCookie()
596 $this->assertFalse($this->object->setCookie('TEST_DEF_COOKIE', 'test_def_123', 'test_def_123'));
598 $this->assertTrue($this->object->setCookie('TEST_CONFIG_COOKIE', 'test_val_123', null, 3600));
600 $this->assertTrue($this->object->setCookie('TEST_CONFIG_COOKIE', '', 'default_val'));
602 $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
603 $this->assertTrue($this->object->setCookie('TEST_MANUAL_COOKIE', 'other', 'other'));