new tests, thanks to Michal Biniek

This commit is contained in:
Marc Delisle
2008-12-11 18:26:41 +00:00
parent 0a82c8ff13
commit 85e9b88297
4 changed files with 793 additions and 0 deletions

View File

@@ -49,6 +49,9 @@ require_once './test/PMA_quoting_slashing_test.php';
require_once './test/PMA_stringOperations_test.php'; require_once './test/PMA_stringOperations_test.php';
require_once './test/PMA_printableBitValue_test.php'; require_once './test/PMA_printableBitValue_test.php';
require_once './test/PMA_foreignKeySupported_test.php'; require_once './test/PMA_foreignKeySupported_test.php';
require_once './test/PMA_headerLocation_test.php';
require_once './test/PMA_Message_test.php';
require_once './test/PMA_whichCrlf_test.php';
class AllTests class AllTests
{ {
@@ -85,6 +88,9 @@ class AllTests
$suite->addTestSuite('PMA_stringOperations_test'); $suite->addTestSuite('PMA_stringOperations_test');
$suite->addTestSuite('PMA_printableBitValue_test'); $suite->addTestSuite('PMA_printableBitValue_test');
$suite->addTestSuite('PMA_foreignKeySupported_test'); $suite->addTestSuite('PMA_foreignKeySupported_test');
$suite->addTestSuite('PMA_headerLocation_test');
$suite->addTestSuite('PMA_Message_test');
$suite->addTestSuite('PMA_whichCrlf_test');
return $suite; return $suite;
} }
} }

410
test/PMA_Message_test.php Normal file
View File

@@ -0,0 +1,410 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Test for PMA_Message class
*
* @author Michal Biniek <michal@bystrzyca.pl>
* @version $Id$
*/
/**
* Tests core.
*/
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Extensions/OutputTestCase.php';
/**
* Include to test.
*/
require_once './libraries/Message.class.php';
/**
* Test class PMA_Message.
*
* @package phpMyAdmin-test
*/
class PMA_Message_test extends PHPUnit_Extensions_OutputTestCase
{
/**
* @var PMA_Message
* @access protected
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp()
{
$this->object = new PMA_Message;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown()
{
}
/**
* to String casting test
*/
public function test__toString()
{
$this->object->setMessage('test<&>', true);
$this->assertEquals('test&lt;&amp;&gt;', (string)$this->object);
}
/**
* test success method
*/
public function testSuccess()
{
$this->object = new PMA_Message('test<&>', PMA_Message::SUCCESS);
$this->assertEquals($this->object, PMA_Message::success('test<&>'));
$this->assertEquals('strSuccess', PMA_Message::success()->getString());
}
/**
* test error method
*/
public function testError()
{
$this->object = new PMA_Message('test<&>', PMA_Message::ERROR);
$this->assertEquals($this->object, PMA_Message::error('test<&>'));
$this->assertEquals('strError', PMA_Message::error()->getString());
}
/**
* test warning method
*/
public function testWarning()
{
$this->object = new PMA_Message('test<&>', PMA_Message::WARNING);
$this->assertEquals($this->object, PMA_Message::warning('test<&>'));
}
/**
* test notice method
*/
public function testNotice()
{
$this->object = new PMA_Message('test<&>', PMA_Message::NOTICE);
$this->assertEquals($this->object, PMA_Message::notice('test<&>'));
}
/**
* test rawError method
*/
public function testRawError()
{
$this->object = new PMA_Message('', PMA_Message::ERROR);
$this->object->setMessage('test<&>');
$this->assertEquals($this->object, PMA_Message::rawError('test<&>'));
}
/**
* test rawWarning method
*/
public function testRawWarning()
{
$this->object = new PMA_Message('', PMA_Message::WARNING);
$this->object->setMessage('test<&>');
$this->assertEquals($this->object, PMA_Message::rawWarning('test<&>'));
}
/**
* test rawNotice method
*/
public function testRawNotice()
{
$this->object = new PMA_Message('', PMA_Message::NOTICE);
$this->object->setMessage('test<&>');
$this->assertEquals($this->object, PMA_Message::rawNotice('test<&>'));
}
/**
* test rawSuccess method
*/
public function testRawSuccess()
{
$this->object = new PMA_Message('', PMA_Message::SUCCESS);
$this->object->setMessage('test<&>');
$this->assertEquals($this->object, PMA_Message::rawSuccess('test<&>'));
}
/**
* testing isSuccess method
*/
public function testIsSuccess()
{
$this->assertFalse($this->object->isSuccess());
$this->assertTrue($this->object->isSuccess(true));
}
/**
* testing isNotice method
*/
public function testIsNotice()
{
$this->assertTrue($this->object->isNotice());
$this->object->isWarning(true);
$this->assertFalse($this->object->isNotice());
$this->assertTrue($this->object->isNotice(true));
}
/**
* testing isWarning method
*/
public function testIsWarning()
{
$this->assertFalse($this->object->isWarning());
$this->assertTrue($this->object->isWarning(true));
}
/**
* testing isError method
*/
public function testIsError()
{
$this->assertFalse($this->object->isError());
$this->assertTrue($this->object->isError(true));
}
/**
* testign setter of message
*/
public function testSetMessage()
{
$this->object->setMessage('test&<>', false);
$this->assertEquals('test&<>', $this->object->getMessage());
$this->object->setMessage('test&<>', true);
$this->assertEquals('test&amp;&lt;&gt;', $this->object->getMessage());
}
/**
* testing setter of string
*/
public function testSetString()
{
$this->object->setString('test&<>', false);
$this->assertEquals('test&<>', $this->object->getString());
$this->object->setString('test&<>', true);
$this->assertEquals('test&amp;&lt;&gt;', $this->object->getString());
}
/**
* testing add param method
*/
public function testAddParam()
{
$this->object->addParam(PMA_Message::notice('test'));
$this->assertEquals(array(PMA_Message::notice('test')), $this->object->getParams());
$this->object->addParam('test', true);
$this->assertEquals(array(PMA_Message::notice('test'), 'test'), $this->object->getParams());
$this->object->addParam('test', false);
$this->assertEquals(array(PMA_Message::notice('test'), 'test', PMA_Message::notice('test')), $this->object->getParams());
}
/**
* testing add string method
*/
public function testAddString()
{
$this->object->addString('test', '*');
$this->assertEquals(array('*', PMA_Message::notice('test')), $this->object->getAddedMessages());
$this->object->addString('test', '');
$this->assertEquals(array('*', PMA_Message::notice('test'), '', PMA_Message::notice('test')), $this->object->getAddedMessages());
}
/**
* testing add messages method
*/
public function testAddMessages()
{
$this->object->addMessages(array('test', PMA_Message::rawWarning('test')), '&');
$this->assertEquals(array('&', PMA_Message::rawNotice('test'), '&', PMA_Message::rawWarning('test')), $this->object->getAddedMessages());
}
/**
* testing add message method
*/
public function testAddMessage()
{
$this->object->addMessage('test', '');
$this->assertEquals(array(PMA_Message::rawNotice('test')), $this->object->getAddedMessages());
$this->object->addMessage('test');
$this->assertEquals(array(PMA_Message::rawNotice('test'), ' ', PMA_Message::rawNotice('test')), $this->object->getAddedMessages());
$this->object->addMessage(PMA_Message::rawWarning('test'), '&');
$this->assertEquals(array(PMA_Message::rawNotice('test'), ' ', PMA_Message::rawNotice('test'), '&', PMA_Message::rawWarning('test')), $this->object->getAddedMessages());
}
/**
* testing setter of params
*/
public function testSetParams()
{
$this->object->setParams('test&<>');
$this->assertEquals('test&<>', $this->object->getParams());
$this->object->setParams('test&<>', true);
$this->assertEquals('test&amp;&lt;&gt;', $this->object->getParams());
}
/**
* testing sanitize method
*/
public function testSanitize()
{
$this->object->setString('test&string<>', false);
$this->assertEquals('test&amp;string&lt;&gt;', PMA_Message::sanitize($this->object));
$this->assertEquals(array('test&amp;string&lt;&gt;', 'test&amp;string&lt;&gt;'), PMA_Message::sanitize(array($this->object, $this->object)));
}
public function decodeBBDataProvider()
{
return array(
array('[i]test[/i][i]aa[i/][em]test[/em]', '<em>test</em><em>aa[i/]<em>test</em>'),
array('[b]test[/b][strong]test[/strong]', '<strong>test</strong><strong>test</strong>'),
array('[tt]test[/tt][code]test[/code]', '<code>test</code><code>test</code>'),
array('[kbd]test[/kbd][br][sup]test[/sup]', '<kbd>test</kbd><br /><sup>test</sup>')
);
}
/**
* testing decodeBB method
* @dataProvider decodeBBDataProvider
*/
public function testDecodeBB($actual, $expected)
{
$this->assertEquals($expected, PMA_Message::decodeBB($actual));
}
/**
* testing format method
*/
public function testFormat()
{
$this->assertEquals('test string', PMA_Message::format('test string'));
$this->assertEquals('test string', PMA_Message::format('test string', 'a'));
$this->assertEquals('test string', PMA_Message::format('test string', array()));
$this->assertEquals('test string', PMA_Message::format('%s string', array('test')));
}
/**
* testing getHash method
*/
public function testGetHash()
{
$this->object->setString('<&>test', false);
$this->object->setMessage('<&>test', false);
$this->assertEquals(md5(PMA_Message::NOTICE . '<&>test<&>test'), $this->object->getHash());
}
/**
* getMessage test - with empty message and with non-empty string - not key in globals
* additional params are defined
*/
public function testGetMessageWithoutMessageWithStringWithParams()
{
$this->object->setMessage('');
$this->object->setString('test string %s %s');
$this->object->addParam('test param 1');
$this->object->addParam('test param 2');
$this->assertEquals('test string test param 1 test param 2', $this->object->getMessage());
}
/**
* getMessage test - with empty message and with empty string
*/
public function testGetMessageWithoutMessageWithEmptyString()
{
$this->object->setMessage('');
$this->object->setString('');
$this->assertEquals('', $this->object->getMessage());
}
/**
* getMessage test - with empty message and with string, which is key to GLOBALS
* additional messages are defined
*/
public function testGetMessageWithoutMessageWithGlobalStringWithAddMessages()
{
$GLOBALS['key'] = 'test message';
$this->object->setMessage('');
$this->object->setString('key');
$this->object->addMessage('test message 2', ' - ');
$this->object->addMessage('test message 3', '&');
$this->assertEquals('test message - test message 2&test message 3', $this->object->getMessage());
unset($GLOBALS['key']);
}
/**
* getMessage test - message is defined
* message with BBCode defined
*/
public function testGetMessageWithMessageWithBBCode()
{
$this->object->setMessage('[kbd]test[/kbd] [a@./Documentation.html#cfg_Example@_blank]test[/a]');
$this->assertEquals('<kbd>test</kbd> <a href="./Documentation.html#cfg_Example" target="_blank">test</a>', $this->object->getMessage());
}
/**
* getLevel test
*/
public function testGetLevel()
{
$this->assertEquals('notice', $this->object->getLevel());
$this->object->setNumber(PMA_Message::SUCCESS);
$this->assertEquals('success', $this->object->getLevel());
$this->object->setNumber(PMA_Message::ERROR);
$this->assertEquals('error', $this->object->getLevel());
$this->object->setNumber(PMA_Message::WARNING);
$this->assertEquals('warning', $this->object->getLevel());
}
/**
* testing display method (output string and _is_displayed varible)
*/
public function testDisplay()
{
$this->assertFalse($this->object->isDisplayed());
$this->object->setMessage('Test Message');
$this->expectOutputString('<div class="notice">Test Message</div>');
$this->object->display();
$this->assertTrue($this->object->isDisplayed());
}
/**
* getDisplay test
*/
public function testGetDisplay()
{
$this->object->setMessage('Test Message');
$this->assertEquals('<div class="notice">Test Message</div>', $this->object->getDisplay());
}
/**
* isDisplayed test
*/
public function testIsDisplayed()
{
$this->assertFalse($this->object->isDisplayed(false));
$this->assertTrue($this->object->isDisplayed(true));
$this->assertTrue($this->object->isDisplayed(false));
}
}
?>

View File

@@ -0,0 +1,305 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Test for PMA_sendHeaderLocation
*
* @author Michal Biniek <michal@bystrzyca.pl>
* @version $Id$
*/
/**
* Tests core.
*/
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Extensions/OutputTestCase.php';
/**
* Include to test.
*/
require_once './libraries/common.lib.php';
require_once './libraries/url_generating.lib.php';
require_once './libraries/core.lib.php';
/**
* Test function sending headers.
* Warning - these tests set constants, so it can interfere with other tests
* If you have runkit extension, then it is possible to back changes made on constants
* rest of options can be tested only with apd, when functions header and headers_sent are redefined
* rename_function() of header and headers_sent may cause CLI error report in Windows XP (but tests are done correctly)
* additional functions which were created during tests must be stored to coverage test e.g.
*
* <code>rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));</code>
*
* @package phpMyAdmin-test
*/
class PMA_headerLocation_test extends PHPUnit_Extensions_OutputTestCase
{
protected $oldIISvalue;
protected $oldSIDvalue;
protected $runkitExt;
protected $apdExt;
public function __construct()
{
parent::__construct();
$this->runkitExt = false;
if (function_exists("runkit_constant_redefine"))
$this->runkitExt = true;
$this->apdExt = false;
if (function_exists("rename_function"))
$this->apdExt = true;
if ($this->apdExt && !$GLOBALS['test_header']) {
// using apd extension to overriding header and headers_sent functions for test purposes
$GLOBALS['test_header'] = 1;
// rename_function() of header and headers_sent may cause CLI error report in Windows XP
rename_function('header', 'test_header');
rename_function('headers_sent', 'test_headers_sent');
// solution from: http://unixwars.com/2008/11/29/override_function-in-php/ to overriding more than one function
$substs = array(
'header' => 'if (isset($GLOBALS["header"])) $GLOBALS["header"] .= $a; else $GLOBALS["header"] = $a;',
'headers_sent' => 'return false;'
);
$args = array(
'header' => '$a',
'headers_sent' => ''
);
foreach ($substs as $func => $ren_func) {
if (function_exists("__overridden__"))
rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
override_function($func, $args[$func], $substs[$func]);
rename_function("__overridden__", str_replace(array('.', ' '),array('', ''),microtime()));
}
}
}
public function __destruct()
{
// rename_function may causes CLI error report in Windows XP, but nothing more happen
if ($this->apdExt && $GLOBALS['test_header']) {
$GLOBALS['test_header'] = 0;
rename_function('header', 'header'.str_replace(array('.', ' '),array('', ''),microtime()));
rename_function('headers_sent', 'headers_sent'.str_replace(array('.', ' '),array('', ''),microtime()));
rename_function('test_header', 'header');
rename_function('test_headers_sent', 'headers_sent');
}
}
public function setUp()
{
// cleaning constants
if ($this->runkitExt) {
$this->oldIISvalue = 'non-defined';
if (defined('PMA_IS_IIS')) {
$this->oldIISvalue = PMA_IS_IIS;
runkit_constant_redefine('PMA_IS_IIS', NULL);
}
else {
runkit_constant_add('PMA_IS_IIS', NULL);
}
$this->oldSIDvalue = 'non-defined';
if (defined('SID')) {
$this->oldSIDvalue = SID;
runkit_constant_redefine('SID', NULL);
}
else {
runkit_constant_add('SID', NULL);
}
}
}
public function tearDown()
{
// cleaning constants
if ($this->runkitExt) {
if ($this->oldIISvalue != 'non-defined')
runkit_constant_redefine('PMA_IS_IIS', $this->oldIISvalue);
elseif (defined('PMA_IS_IIS')) {
runkit_constant_remove('PMA_IS_IIS');
}
if ($this->oldSIDvalue != 'non-defined')
runkit_constant_redefine('SID', $this->oldSIDvalue);
elseif (defined('SID')) {
runkit_constant_remove('SID');
}
}
if ($this->apdExt)
unset($GLOBALS['header']);
}
public function testSendHeaderLocationWithSidUrlWithQuestionMark()
{
if ($this->runkitExt && $this->apdExt) {
runkit_constant_redefine('SID', md5('test_hash'));
$testUri = 'http://testurl.com/test.php?test=test';
$separator = PMA_get_arg_separator();
$header = 'Location: ' . $testUri . $separator . SID;
PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
$this->assertEquals($header, $GLOBALS['header']);
} else {
$this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
}
}
public function testSendHeaderLocationWithSidUrlWithoutQuestionMark()
{
if ($this->runkitExt && $this->apdExt) {
runkit_constant_redefine('SID', md5('test_hash'));
$testUri = 'http://testurl.com/test.php';
$separator = PMA_get_arg_separator();
$header = 'Location: ' . $testUri . '?' . SID;
PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
$this->assertEquals($header, $GLOBALS['header']);
} else {
$this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
}
}
public function testSendHeaderLocationWithoutSidWithIis()
{
if ($this->runkitExt && $this->apdExt) {
runkit_constant_redefine('PMA_IS_IIS', true);
runkit_constant_add('PMA_COMING_FROM_COOKIE_LOGIN', true);
$testUri = 'http://testurl.com/test.php';
$separator = PMA_get_arg_separator();
$header = 'Refresh: 0; ' . $testUri;
PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
// cleaning constant
runkit_constant_remove('PMA_COMING_FROM_COOKIE_LOGIN');
$this->assertEquals($header, $GLOBALS['header']);
} else {
$this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
}
}
public function testSendHeaderLocationWithoutSidWithoutIis()
{
if ($this->apdExt) {
$testUri = 'http://testurl.com/test.php';
$header = 'Location: ' . $testUri;
PMA_sendHeaderLocation($testUri); // sets $GLOBALS['header']
$this->assertEquals($header, $GLOBALS['header']);
} else {
$this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
}
}
public function testSendHeaderLocationIisLongUri()
{
if (defined('PMA_IS_IIS') && $this->runkitExt)
runkit_constant_redefine('PMA_IS_IIS', true);
elseif (!defined('PMA_IS_IIS'))
define('PMA_IS_IIS', true);
else
$this->markTestSkipped('Cannot redefine constant/function - missing APD or/and runkit extension');
// over 600 chars
$testUri = 'http://testurl.com/test.php?testlonguri=over600chars&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test&test=test';
$GLOBALS['strGo'] = 'test link';
$header = "<html><head><title>- - -</title>\n" .
"<meta http-equiv=\"expires\" content=\"0\">\n" .
"<meta http-equiv=\"Pragma\" content=\"no-cache\">\n" .
"<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n" .
"<meta http-equiv=\"Refresh\" content=\"0;url=" . $testUri . "\">\n" .
"<script type=\"text/javascript\">\n".
"//<![CDATA[\n" .
"setTimeout(\"window.location = unescape('\"" . $testUri . "\"')\", 2000);\n" .
"//]]>\n" .
"</script>\n" .
"</head>\n" .
"<body>\n" .
"<script type=\"text/javascript\">\n" .
"//<![CDATA[\n" .
"document.write('<p><a href=\"" . $testUri . "\">" . $GLOBALS['strGo'] . "</a></p>');\n" .
"//]]>\n" .
"</script></body></html>\n";
$this->expectOutputString($header);
PMA_sendHeaderLocation($testUri);
}
/**
* other output tests
*/
public function testWriteReloadNavigation()
{
$GLOBALS['reload'] = true;
$GLOBALS['db'] = 'test_db';
$url = './navigation.php?db='.$GLOBALS['db'];
$write = "\n" . '<script type="text/javascript">' . "\r\n" .
'//<![CDATA[' . "\r\n" .
'if (typeof(window.parent) != \'undefined\'' . "\r\n" .
' && typeof(window.parent.frame_navigation) != \'undefined\'' . "\r\n" .
' && window.parent.goTo) {' . "\r\n" .
' window.parent.goTo(\'' . $url . '\');' . "\r\n" .
'}' . "\r\n" .
'//]]>' . "\r\n" .
'</script>' . "\r\n ";
$this->expectOutputString($write);
PMA_reloadNavigation();
$this->assertFalse(isset($GLOBALS['reload']));
unset($GLOBALS['db']);
}
}
?>

View File

@@ -0,0 +1,72 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Test whichCrlf function
*
* @author Michal Biniek <michal@bystrzyca.pl>
* @package phpMyAdmin-test
* @version $Id: PMA_whichCrlf_test.php
*/
/**
* Tests core.
*/
require_once 'PHPUnit/Framework.php';
/**
* Include to test.
*/
require_once './libraries/common.lib.php';
/**
* Test whichCrlf function.
*
*/
class PMA_whichCrlf_test extends PHPUnit_Framework_TestCase
{
/**
* @using runkit pecl extension
* if not define PMA_USR_OS, then define it as Win
* if installed runkit, then constant will not change
*/
public function testWhichCrlf()
{
$runkit = function_exists('runkit_constant_redefine');
if ($runkit && defined('PMA_USR_OS'))
$pma_usr_os = PMA_USR_OS;
if (defined('PMA_USR_OS') && !$runkit) {
if (PMA_USR_OS == 'Win')
$this->assertEquals("\r\n", PMA_whichCrlf());
else
$this->assertEquals("\n", PMA_whichCrlf());
$this->markTestIncomplete('Cannot redefine constant');
} else {
if ($runkit)
define('PMA_USR_OS', 'Linux');
$this->assertEquals("\n", PMA_whichCrlf());
if ($runkit)
runkit_constant_redefine('PMA_USR_OS', 'Win');
else
define('PMA_USR_OS', 'Win');
$this->assertEquals("\r\n", PMA_whichCrlf());
}
if ($runkit) {
if (isset($pma_usr_os))
runkit_constant_redefine('PMA_USR_OS', 'Win');
else
runkit_constant_remove('PMA_USR_OS');
}
}
}
?>