From 7b0b4482d807a4f46064f3fe82c7a93df079e0ff Mon Sep 17 00:00:00 2001 From: Yasitha Pandithawatta Date: Sat, 28 Jul 2012 00:00:10 +0530 Subject: [PATCH 1/2] Add test class for DbSearch.class.php --- test/classes/PMA_DbSearch_test.php | 189 +++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 test/classes/PMA_DbSearch_test.php diff --git a/test/classes/PMA_DbSearch_test.php b/test/classes/PMA_DbSearch_test.php new file mode 100644 index 0000000000..5072b6fe77 --- /dev/null +++ b/test/classes/PMA_DbSearch_test.php @@ -0,0 +1,189 @@ +object = $this->getMockForAbstractClass('PMA_DbSearch', array('pma')); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + * + * @access protected + * @return void + */ + protected function tearDown() + { + unset($this->object); + } + + /** + * Call protected functions by making the visibitlity to public. + * + * @param string $name method name + * @param array $params parameters for the invocation + * + * @return the output from the protected method. + */ + private function _callProtectedFunction($name, $params) + { + $class = new ReflectionClass('PMA_DbSearch'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method->invokeArgs($this->object, $params); + } + + /** + * Test for getCommonFunctions + */ + public function testGetCommonFunctions(){ + $this->assertTrue($this->object->getCommonFunctions() instanceof PMA_CommonFunctions); + } + + /** + * Test for _getSearchSqls + */ + public function testGetSearchSqls(){ + + $GLOBALS['db'] = 'pma'; + if (! function_exists('PMA_DBI_get_columns')) { + function PMA_DBI_get_columns() + { + return array( + 'column1', + 'column2' + ); + } + } + + $this->assertEquals( + $this->_callProtectedFunction( + '_getSearchSqls', + array('table1') + ), + array ( + 'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE', + 'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` WHERE FALSE', + 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE' + ) + ); + } + + /** + * Test for getSearchResults + */ + public function testGetSearchResults(){ + $this->assertEquals( + $this->object->getSearchResults(), + '
Search results for "" :
' + ); + } + + /** + * Test for _getResultsRow + * @param string $each_table Tables on which search is to be performed + * @param array $newsearchsqls Contains SQL queries + * @param bool $odd_row For displaying contrasting table rows + * @param $output + * + * @dataProvider providerForTestGetResultsRow + */ + public function testGetResultsRow($each_table, $newsearchsqls, $odd_row, $output){ + + if (! function_exists('PMA_DBI_fetch_value')) { + function PMA_DBI_fetch_value() + { + return 2; + } + } + $GLOBALS['cfg']['AjaxEnable'] = true; + $this->assertEquals( + $this->_callProtectedFunction( + '_getResultsRow', + array($each_table, $newsearchsqls, $odd_row) + ), + $output + ); + } + + /** + * @return array provider for testGetResultsRow + */ + public function providerForTestGetResultsRow(){ + return array( + array( + 'table1', + array( + 'SELECT * FROM `pma`.`table1` WHERE FALSE', + 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` WHERE FALSE', + 'select_count' => 2, + 'select_columns' => 'column1', + 'delete' => 'column2' + ), + true, + '2 matches in table1BrowseDelete' + ) + ); + } + + /** + * Test for getSelectionForm + */ + public function testGetSelectionForm(){ + $_SESSION[' PMA_token '] = 'token'; + $_SESSION['PMA_Theme'] = new PMA_Theme(); + $GLOBALS['pmaThemeImage'] = 'themes/dot.gif'; + $url_params = array('param1', 'param2'); + $this->assertEquals( + $this->object->getSelectionForm($url_params), + '
Search in database
Words or values to search for (wildcard: "%"):
Find: +
+ +
+ +
+ +
+
Inside tables:
Select All  / Unselect All
Inside column:

' + ); + } +} From 3e9a5f69dcbadd15ad848aec567b512456bd8dca Mon Sep 17 00:00:00 2001 From: Yasitha Pandithawatta Date: Sun, 29 Jul 2012 10:37:23 +0530 Subject: [PATCH 2/2] Test class for PMA_DBQbe --- test/classes/PMA_DBQbe_test.php | 400 +++++++++++++++++++++++++++++ test/classes/PMA_DbSearch_test.php | 14 + 2 files changed, 414 insertions(+) create mode 100644 test/classes/PMA_DBQbe_test.php diff --git a/test/classes/PMA_DBQbe_test.php b/test/classes/PMA_DBQbe_test.php new file mode 100644 index 0000000000..55ffb4a1b7 --- /dev/null +++ b/test/classes/PMA_DBQbe_test.php @@ -0,0 +1,400 @@ +object = $this->getMockForAbstractClass('PMA_DBQbe', array('pma')); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + * + * @access protected + * @return void + */ + protected function tearDown() + { + unset($this->object); + } + + /** + * Call protected functions by making the visibitlity to public. + * + * @param string $name method name + * @param array $params parameters for the invocation + * + * @return the output from the protected method. + */ + private function _callProtectedFunction($name, $params) + { + $class = new ReflectionClass('PMA_DBQbe'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method->invokeArgs($this->object, $params); + } + + /** + * Test for getCommonFunctions + */ + public function testGetCommonFunctions(){ + $this->assertTrue($this->object->getCommonFunctions() instanceof PMA_CommonFunctions); + } + + /** + * Test for _getSortSelectCell + */ + public function testGetSortSelectCell(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getSortSelectCell', + array(1) + ), + '' + ); + } + + /** + * Test for _getSortRow + */ + public function testGetSortRow(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getSortRow', + array() + ), + 'Sort:' + ); + } + + /** + * Test for _getShowRow + */ + public function testGetShowRow(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getShowRow', + array() + ), + 'Show:' + ); + } + + /** + * Test for _getCriteriaInputboxRow + */ + public function testGetCriteriaInputboxRow(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getCriteriaInputboxRow', + array() + ), + 'Criteria:' + ); + } + + /** + * Test for _getFootersOptions + */ + public function testGetFootersOptions(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getFootersOptions', + array('row') + ), + '
Add/Delete criteria rows:
' + ); + } + + /** + * Test for _getTableFooters + */ + public function testGetTableFooters(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getTableFooters', + array() + ), + '
Add/Delete criteria rows:
Add/Delete columns:
' + ); + } + + /** + * Test for _getAndOrColCell + */ + public function testGetAndOrColCell(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getAndOrColCell', + array(1) + ), + 'Or:  And:
Ins  Del' + ); + } + + /** + * Test for _getModifyColumnsRow + */ + public function testGetModifyColumnsRow(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getModifyColumnsRow', + array() + ), + 'Modify:Or:  And:
Ins  DelOr:  And:
Ins  DelOr:  And:
Ins  Del' + ); + } + + /** + * Test for _getInsDelAndOrCell + */ + public function testGetInsDelAndOrCell(){ + $GLOBALS['cell_align_right'] = 'cellAlign'; + $this->assertEquals( + $this->_callProtectedFunction( + '_getInsDelAndOrCell', + array(3, 'checked') + ), + '
Ins:And:
Del:Or:
' + ); + } + + /** + * Test for _getInputboxRow + */ + public function testGetInputboxRow(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getInputboxRow', + array(2,3) + ), + '' + ); + } + + /** + * Test for _getInsDelAndOrCriteriaRows + */ + public function testGetInsDelAndOrCriteriaRows(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getInsDelAndOrCriteriaRows', + array(2,3) + ), + '
Ins:And:
Del:Or:
' + ); + } + + /** + * Test for _getSelectClause + */ + public function testGetSelectClause(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getSelectClause', + array() + ), + '' + ); + } + + /** + * Test for _getWhereClause + */ + public function testGetWhereClause(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getWhereClause', + array() + ), + '' + ); + } + + /** + * Test for _getOrderByClause + */ + public function testGetOrderByClause(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getOrderByClause', + array() + ), + '' + ); + } + + /** + * Test for _getIndexes + */ + public function testGetIndexes(){ + if (! function_exists('PMA_DBI_get_table_indexes')) { + function PMA_DBI_get_table_indexes() + { + return array(2,3); + } + } + $this->assertEquals( + $this->_callProtectedFunction( + '_getIndexes', + array( + array('table1','table2'), + array('column1', 'column2', 'column3'), + array('column2') + ) + ), + array( + 'unique' => array(), + 'index' => array() + ) + ); + } + + /** + * Test for _getLeftJoinColumnCandidates + */ + public function test_getLeftJoinColumnCandidates(){ + if (! function_exists('PMA_DBI_select_db')) { + function PMA_DBI_select_db() + { + return 'pma'; + } + } + $this->assertEquals( + $this->_callProtectedFunction( + '_getLeftJoinColumnCandidates', + array( + array('table1','table2'), + array('column1', 'column2', 'column3'), + array('column2') + ) + ), + array( + 0 => 'column2' + ) + ); + } + + /** + * Test for _getMasterTable + */ + public function test_getMasterTable(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getMasterTable', + array( + array('table1','table2'), + array('column1', 'column2', 'column3'), + array('column2'), + array('pma') + ) + ), + 0 + ); + } + + /** + * Test for _getWhereClauseTablesAndColumns + */ + public function test_getWhereClauseTablesAndColumns(){ + $_POST['criteriaColumn'] = array('table1.id', 'table1.value', 'table1.name', 'table1.deleted'); + $this->assertEquals( + $this->_callProtectedFunction( + '_getWhereClauseTablesAndColumns', + array() + ), + array( + 'where_clause_tables' => array(), + 'where_clause_columns' => array() + ) + ); + } + + /** + * Test for _getFromClause + */ + public function testGetFromClause(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getFromClause', + array('relation') + ), + '`table1`' + ); + } + + /** + * Test for _getSQLQuery + */ + public function test_getSQLQuery(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getSQLQuery', + array('relation') + ), + 'FROM `table1` +' + ); + } +} diff --git a/test/classes/PMA_DbSearch_test.php b/test/classes/PMA_DbSearch_test.php index 5072b6fe77..cf88afc0fb 100644 --- a/test/classes/PMA_DbSearch_test.php +++ b/test/classes/PMA_DbSearch_test.php @@ -186,4 +186,18 @@ class PMA_DbSearch_test extends PHPUnit_Framework_TestCase Inside tables:Select All  / Unselect AllInside column:

' ); } + + /** + * Test for _getResultDivs + */ + public function testGetResultDivs(){ + $this->assertEquals( + $this->_callProtectedFunction( + '_getResultDivs', + array() + ), + '

' + ); + } + }