diff --git a/libraries/plugins/auth/AuthenticationConfig.class.php b/libraries/plugins/auth/AuthenticationConfig.class.php index b33ebe570a..5e33a07f1a 100644 --- a/libraries/plugins/auth/AuthenticationConfig.class.php +++ b/libraries/plugins/auth/AuthenticationConfig.class.php @@ -153,7 +153,9 @@ class AuthenticationConfig extends AuthenticationPlugin echo '' . "\n"; } echo '' . "\n"; - exit; + if (!defined('TESTSUITE')) { + exit; + } return true; } diff --git a/libraries/plugins/auth/AuthenticationCookie.class.php b/libraries/plugins/auth/AuthenticationCookie.class.php index b1a0c08bb8..2b68f358ab 100644 --- a/libraries/plugins/auth/AuthenticationCookie.class.php +++ b/libraries/plugins/auth/AuthenticationCookie.class.php @@ -102,7 +102,11 @@ class AuthenticationCookie extends AuthenticationPlugin ) ); } - exit; + if (defined('TESTSUITE')) { + return true; + } else { + exit; + } } /* Perform logout to custom URL */ @@ -110,7 +114,11 @@ class AuthenticationCookie extends AuthenticationPlugin && ! empty($GLOBALS['cfg']['Server']['LogoutURL']) ) { PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']); - exit; + if (defined('TESTSUITE')) { + return true; + } else { + exit; + } } // No recall if blowfish secret is not configured as it would produce @@ -301,7 +309,11 @@ class AuthenticationCookie extends AuthenticationPlugin if (file_exists(CUSTOM_FOOTER_FILE)) { include CUSTOM_FOOTER_FILE; } - exit; + if (! defined('TESTSUITE')) { + exit; + } else { + return true; + } } /** @@ -408,7 +420,9 @@ class AuthenticationCookie extends AuthenticationPlugin // according to the PHP manual we should do this before the destroy: //$_SESSION = array(); - session_destroy(); + if (! defined('TESTSUITE')) { + session_destroy(); + } // -> delete password cookie(s) if ($GLOBALS['cfg']['LoginCookieDeleteAll']) { foreach ($GLOBALS['cfg']['Servers'] as $key => $val) { @@ -478,7 +492,11 @@ class AuthenticationCookie extends AuthenticationPlugin PMA_Util::cacheUnset('dbs_where_create_table_allowed', true); $GLOBALS['no_activity'] = true; $this->authFails(); - exit; + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } } // password @@ -625,7 +643,11 @@ class AuthenticationCookie extends AuthenticationPlugin $redirect_url . PMA_generate_common_url($url_params, '&'), true ); - exit; + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } } // end if return true; diff --git a/test/bootstrap-dist.php b/test/bootstrap-dist.php index 0edf05e329..95cca261bd 100644 --- a/test/bootstrap-dist.php +++ b/test/bootstrap-dist.php @@ -71,4 +71,31 @@ if (PMA_HAS_RUNKIT && $GLOBALS['runkit_internal_override']) { echo "Please install runkit and enable runkit.internal_override!\n"; } +/** + * Return the tag array to be used with assertTag by parsing + * a given HTML element + * + * @param string $elementHTML HTML for element to be parsed + * @param array $arr Additional array elements like content, parent + * + * @return array Tag array to be used with assertTag + */ +function PMA_getTagArray($elementHTML, $arr = array()) +{ + + // get attributes + preg_match_all("/\s+(.*?)\=\s*\"(.*?)\"/is", $elementHTML, $matches); + foreach ($matches[1] as $key => $val) { + $arr['attributes'][trim($val)] = trim($matches[2][$key]); + } + $matches = array(); + + // get tag + preg_match("/^\<(.*?)(\s|\>)/i", $elementHTML, $matches); + if (isset($matches[1])) { + $arr['tag'] = trim($matches[1]); + } + + return $arr; +} ?> diff --git a/test/classes/plugin/auth/PMA_AuthenticationConfig_test.php b/test/classes/plugin/auth/PMA_AuthenticationConfig_test.php new file mode 100644 index 0000000000..fe40806f77 --- /dev/null +++ b/test/classes/plugin/auth/PMA_AuthenticationConfig_test.php @@ -0,0 +1,143 @@ +enableBc(); + $GLOBALS['server'] = 0; + $this->object = new AuthenticationConfig(null); + } + + /** + * tearDown for test cases + * + * @return void + */ + public function tearDown() + { + unset($this->object); + } + + /** + * Test for AuthenticationConfig::auth + * + * @return void + */ + public function testAuth() + { + $this->assertTrue( + $this->object->auth() + ); + } + + /** + * Test for AuthenticationConfig::authCheck + * + * @return void + */ + public function testAuthCheck() + { + $this->assertTrue( + $this->object->authCheck() + ); + } + + /** + * Test for AuthenticationConfig::authSetUser + * + * @return void + */ + public function testAuthSetUser() + { + $this->assertTrue( + $this->object->authSetUser() + ); + } + + /** + * Test for AuthenticationConfig::authFails + * + * @return void + */ + public function testAuthFails() + { + $removeConstant = false; + $GLOBALS['error_handler'] = new PMA_Error_Handler; + $GLOBALS['cfg']['Servers'] = array(1); + $GLOBALS['allowDeny_forbidden'] = false; + if (!defined('PMA_USR_BROWSER_AGENT')) { + define('PMA_USR_BROWSER_AGENT', 'chrome'); + + $removeConstant = true; + + if (! PMA_HAS_RUNKIT) { + $this->markTestSkipped('Cannot remove constant'); + } + } + + ob_start(); + $result = $this->object->authFails(); + $html = ob_get_clean(); + + $this->assertTrue( + $result + ); + + $this->assertContains( + 'You probably did not create a configuration file. You might want ' . + 'to use the setup script to create one.', + $html + ); + + $this->assertContains( + 'MySQL said: ' . + 'Documentation', + $html + ); + + $this->assertContains( + 'Cannot connect: invalid settings.', + $html + ); + + $this->assertContains( + 'Retry to connect', + $html + ); + if ($removeConstant) { + runkit_constant_remove('PMA_USR_BROWSER_AGENT'); + } + } +} +?> diff --git a/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php b/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php new file mode 100644 index 0000000000..53e7b76bf9 --- /dev/null +++ b/test/classes/plugin/auth/PMA_AuthenticationCookie_test.php @@ -0,0 +1,1109 @@ +enableBc(); + $GLOBALS['server'] = 0; + $this->object = new AuthenticationCookie(null); + } + + /** + * tearDown for test cases + * + * @return void + */ + public function tearDown() + { + unset($this->object); + } + + /** + * Test for AuthenticationConfig::auth + * + * @return void + */ + public function testAuth() + { + $restoreInstance = PMA_Response::getInstance(); + // Case 1 + + $mockResponse = $this->getMockBuilder('PMA_Response') + ->disableOriginalConstructor() + ->setMethods(array('isAjax', 'isSuccess', 'addJSON')) + ->getMock(); + + $mockResponse->expects($this->once()) + ->method('isAjax') + ->with() + ->will($this->returnValue(true)); + + $mockResponse->expects($this->once()) + ->method('isSuccess') + ->with(false); + + $mockResponse->expects($this->once()) + ->method('addJSON') + ->with( + 'message', + PMA_Message::error( + '1

[ Log in ]' + ) + ); + + $attrInstance = new ReflectionProperty('PMA_Response', '_instance'); + $attrInstance->setAccessible(true); + $attrInstance->setValue(null, $mockResponse); + $GLOBALS['conn_error'] = true; + $GLOBALS['cfg']['PmaAbsoluteUri'] = 'https://phpmyadmin.net/'; + $this->assertTrue( + $this->object->auth() + ); + // Case 2 + + $mockResponse = $this->getMockBuilder('PMA_Response') + ->disableOriginalConstructor() + ->setMethods(array('isAjax', 'isSuccess', 'addJSON')) + ->getMock(); + + $mockResponse->expects($this->once()) + ->method('isAjax') + ->with() + ->will($this->returnValue(true)); + + $mockResponse->expects($this->once()) + ->method('isSuccess') + ->with(false); + + $mockResponse->expects($this->once()) + ->method('addJSON') + ->with( + 'message', + PMA_Message::error( + 'Your session has expired. Please log in again.' . + '

[ Log in ]' + ) + ); + + $attrInstance = new ReflectionProperty('PMA_Response', '_instance'); + $attrInstance->setAccessible(true); + $attrInstance->setValue(null, $mockResponse); + $GLOBALS['conn_error'] = ''; + + $this->assertTrue( + $this->object->auth() + ); + + // case 3 + + $mockResponse = $this->getMockBuilder('PMA_Response') + ->disableOriginalConstructor() + ->setMethods(array('isAjax', 'getFooter', 'getHeader')) + ->getMock(); + + $mockResponse->expects($this->once()) + ->method('isAjax') + ->with() + ->will($this->returnValue(false)); + + + + $_REQUEST['old_usr'] = ''; + $GLOBALS['cfg']['LoginCookieRecall'] = true; + $GLOBALS['cfg']['blowfish_secret'] = 'secret'; + $GLOBALS['PHP_AUTH_USER'] = 'pmauser'; + $GLOBALS['pma_auth_server'] = 'localhost'; + + // mock footer + $mockFooter = $this->getMockBuilder('PMA_Footer') + ->disableOriginalConstructor() + ->setMethods(array('setMinimal')) + ->getMock(); + + $mockFooter->expects($this->once()) + ->method('setMinimal') + ->with(); + + // mock header + + $mockHeader = $this->getMockBuilder('PMA_Header') + ->disableOriginalConstructor() + ->setMethods( + array('setBodyId', 'setTitle', 'disableMenu', 'disableWarnings') + ) + ->getMock(); + + $mockHeader->expects($this->once()) + ->method('setBodyId') + ->with('loginform'); + + $mockHeader->expects($this->once()) + ->method('setTitle') + ->with('phpMyAdmin'); + + $mockHeader->expects($this->once()) + ->method('disableMenu') + ->with(); + + $mockHeader->expects($this->once()) + ->method('disableWarnings') + ->with(); + + // set mocked headers and footers + + $mockResponse->expects($this->once()) + ->method('getFooter') + ->with() + ->will($this->returnValue($mockFooter)); + + $mockResponse->expects($this->once()) + ->method('getHeader') + ->with() + ->will($this->returnValue($mockHeader)); + + $attrInstance = new ReflectionProperty('PMA_Response', '_instance'); + $attrInstance->setAccessible(true); + $attrInstance->setValue(null, $mockResponse); + + $GLOBALS['pmaThemeImage'] = 'test'; + $GLOBALS['conn_error'] = true; + $GLOBALS['cfg']['Lang'] = 'en'; + $GLOBALS['cfg']['AllowArbitraryServer'] = true; + $GLOBALS['cfg']['Servers'] = array(1, 2); + $_SESSION['last_valid_captcha'] = true; + $GLOBALS['target'] = 'testTarget'; + $GLOBALS['db'] = 'testDb'; + $GLOBALS['table'] = 'testTable'; + + file_put_contents('testlogo_right.png', ''); + + // mock error handler + + $mockErrorHandler = $this->getMockBuilder('PMA_Error_Handler') + ->disableOriginalConstructor() + ->setMethods(array('hasDisplayErrors', 'dispErrors')) + ->getMock(); + + $mockErrorHandler->expects($this->once()) + ->method('hasDisplayErrors') + ->with() + ->will($this->returnValue(true)); + + $mockErrorHandler->expects($this->once()) + ->method('dispErrors') + ->with(); + + $GLOBALS['error_handler'] = $mockErrorHandler; + + ob_start(); + $this->object->auth(); + $result = ob_get_clean(); + + // assertions + + $this->assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + '
' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '
' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + @unlink('testlogo_right.png'); + + // case 4 + + $mockResponse = $this->getMockBuilder('PMA_Response') + ->disableOriginalConstructor() + ->setMethods(array('isAjax', 'getFooter', 'getHeader')) + ->getMock(); + + $mockResponse->expects($this->once()) + ->method('isAjax') + ->with() + ->will($this->returnValue(false)); + + $mockResponse->expects($this->once()) + ->method('getFooter') + ->with() + ->will($this->returnValue(new PMA_Footer())); + + $mockResponse->expects($this->once()) + ->method('getHeader') + ->with() + ->will($this->returnValue(new PMA_Header())); + + $_REQUEST['old_usr'] = ''; + $GLOBALS['cfg']['LoginCookieRecall'] = false; + + $attrInstance = new ReflectionProperty('PMA_Response', '_instance'); + $attrInstance->setAccessible(true); + $attrInstance->setValue(null, $mockResponse); + + $GLOBALS['pmaThemeImage'] = 'test'; + $GLOBALS['cfg']['Lang'] = ''; + $GLOBALS['cfg']['AllowArbitraryServer'] = false; + $GLOBALS['cfg']['Servers'] = array(1); + $_SESSION['last_valid_captcha'] = false; + $GLOBALS['cfg']['CaptchaLoginPrivateKey'] = 'testprivkey'; + $GLOBALS['cfg']['CaptchaLoginPublicKey'] = 'testpubkey'; + $GLOBALS['server'] = 0; + + $GLOBALS['error_handler'] = new PMA_Error_Handler; + + ob_start(); + $this->object->auth(); + $result = ob_get_clean(); + + // assertions + + $this->assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertContains( + 'src="https://www.google.com/recaptcha/api/challenge?k=testpubkey">', + $result + ); + + $this->assertContains( + 'iframe src="https://www.google.com/recaptcha/api/noscript' . + '?k=testpubkey"', + $result + ); + + $this->assertContains( + '