From 6d0d61e7f0597685dffe2b616580bf16621cac71 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 8 Aug 2015 21:11:32 +0800 Subject: [PATCH 1/8] Recover some logic tests - TableSearchController Signed-off-by: Jason --- .../TableSearchController.class.php | 2 - tbl_find_replace.php | 1 + tbl_select.php | 1 + tbl_zoom_select.php | 1 + test/classes/PMA_TableReplaceSearch_test.php | 164 ------------ ...php => PMA_TableSearchController_test.php} | 64 ++--- test/classes/PMA_Table_test.php | 30 +++ .../PMA_tbl_columns_definition_form_test.php | 239 ------------------ 8 files changed, 59 insertions(+), 443 deletions(-) delete mode 100644 test/classes/PMA_TableReplaceSearch_test.php rename test/classes/{PMA_TableSearch_test.php => PMA_TableSearchController_test.php} (89%) diff --git a/libraries/controllers/TableSearchController.class.php b/libraries/controllers/TableSearchController.class.php index 8d58837dcb..d6c22e34e6 100644 --- a/libraries/controllers/TableSearchController.class.php +++ b/libraries/controllers/TableSearchController.class.php @@ -13,8 +13,6 @@ use PMA\Controllers\TableController; use PMA_DatabaseInterface; use PMA_Util; -require_once 'libraries/common.inc.php'; -require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/Template.class.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/sql.lib.php'; diff --git a/tbl_find_replace.php b/tbl_find_replace.php index 8bff91c467..c9cd2464e9 100644 --- a/tbl_find_replace.php +++ b/tbl_find_replace.php @@ -13,6 +13,7 @@ */ require_once 'libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; +require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableSearchController.class.php'; diff --git a/tbl_select.php b/tbl_select.php index aa7e6d6d8b..66d62ddb6f 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -14,6 +14,7 @@ */ require_once 'libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; +require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableSearchController.class.php'; diff --git a/tbl_zoom_select.php b/tbl_zoom_select.php index f475b58a01..aedc098fc9 100644 --- a/tbl_zoom_select.php +++ b/tbl_zoom_select.php @@ -13,6 +13,7 @@ */ require_once './libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; +require_once 'libraries/tbl_info.inc.php'; require_once './libraries/di/Container.class.php'; require_once './libraries/controllers/TableSearchController.class.php'; diff --git a/test/classes/PMA_TableReplaceSearch_test.php b/test/classes/PMA_TableReplaceSearch_test.php deleted file mode 100644 index 422a5b6886..0000000000 --- a/test/classes/PMA_TableReplaceSearch_test.php +++ /dev/null @@ -1,164 +0,0 @@ -_object = $this->getMock( - 'PMA_TableSearch', - array('_loadTableInfo'), - array(), - '', - false - ); - - $reflection = new \ReflectionClass('PMA_TableSearch'); - - // set database, table names - $attrDb = $reflection->getProperty('_db'); - $attrDb->setAccessible(true); - $attrDb->setValue($this->_object, 'dbName'); - $attrTable = $reflection->getProperty('_table'); - $attrTable->setAccessible(true); - $attrTable->setValue($this->_object, 'tableName'); - - // set column names list - $attrColNames = $reflection->getProperty('_columnNames'); - $attrColNames->setAccessible(true); - $columnNames = array('column1'); - $attrColNames->setValue($this->_object, $columnNames);*/ - } - - /** - * Tests getReplacePreview() method - * - * @return void - * @group medium - */ - public function testGetReplacePreview() - { - // @todo: Replace this test with TableSearchController test - /*//mock DBI - $dbi = $this->getMockBuilder('PMA_DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $find = 'findValue'; - $replaceWith = 'replaceWithValue'; - $useRegex = false; - $charSet = 'charSetValue'; - - // set expectations - $dbi->expects($this->once()) - ->method('fetchResult') - ->will( - $this->returnValue( - array( - array('val1', 'replace1', 5), - array('va<2', 'replac<2', 1) - ) - ) - ); - $GLOBALS['dbi'] = $dbi; - - $ret = $this->_object->getReplacePreview( - 0, $find, $replaceWith, $useRegex, $charSet - ); - - // assert whether hidden values are properly set - $this->assertContains( - '', - $ret - ); - $this->assertContains( - '', - $ret - ); - $this->assertContains( - '', - $ret - ); - $this->assertContains( - '', - $ret - );*/ - - // assert values displayed in the preview and escaping - /** - * @todo Find out a better method to test for HTML - * - * $this->assertContains( - * '5val1replace1', - * $ret - * ); - * - * $this->assertContains( - * '1va<2replac<2', - * $ret - * ); - */ - } - - /** - * Tests replace() method - * - * @return void - */ - public function testReplace() - { - // @todo: Replace this test with TableSearchController test - /*//mock DBI - $dbi = $this->getMockBuilder('PMA_DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $find = 'findValue'; - $replaceWith = 'replaceWithValue'; - $useRegex = false; - $charSet = 'charSetValue'; - - $expectedQuery = "UPDATE `dbName`.`tableName`" - . " SET `column1` = REPLACE(`column1`, '" . $find . "', '" . $replaceWith - . "') WHERE `column1` LIKE '%" . $find . "%' COLLATE " - . $charSet . "_bin"; - // set expectations - $dbi->expects($this->once()) - ->method('query') - ->with($expectedQuery); - $GLOBALS['dbi'] = $dbi; - - $this->_object->replace(0, $find, $replaceWith, $useRegex, $charSet);*/ - } -} diff --git a/test/classes/PMA_TableSearch_test.php b/test/classes/PMA_TableSearchController_test.php similarity index 89% rename from test/classes/PMA_TableSearch_test.php rename to test/classes/PMA_TableSearchController_test.php index ffc20878a0..d858d8dfee 100644 --- a/test/classes/PMA_TableSearch_test.php +++ b/test/classes/PMA_TableSearchController_test.php @@ -9,9 +9,11 @@ /* * Include to test. */ +use PMA\Controllers\Table\TableSearchController; +use PMA\DI\Container; + require_once 'libraries/url_generating.lib.php'; require_once 'libraries/DatabaseInterface.class.php'; -//require_once 'libraries/TableSearch.class.php'; require_once 'libraries/Util.class.php'; require_once 'libraries/php-gettext/gettext.inc'; require_once 'libraries/database_interface.inc.php'; @@ -21,13 +23,15 @@ require_once 'libraries/Tracker.class.php'; require_once 'libraries/Types.class.php'; require_once 'libraries/relation.lib.php'; require_once 'libraries/url_generating.lib.php'; +require_once 'libraries/di/Container.class.php'; +require_once 'libraries/controllers/TableSearchController.class.php'; /** * Tests for PMA_TableSearch * * @package PhpMyAdmin-test */ -class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase +class PMA_TableSearchController_Test extends PHPUnit_Framework_TestCase { /** @@ -41,7 +45,7 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase /** * SET these to avoid undefined index error */ - /*$_SESSION['PMA_Theme'] = new PMA_Theme(); + $_SESSION['PMA_Theme'] = new PMA_Theme(); $_POST['zoom_submit'] = 'zoom'; $GLOBALS['server'] = 1; @@ -96,8 +100,12 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase $dbi->expects($this->any())->method('fetchValue') ->will($this->returnValue($show_create_table)); - $GLOBALS['dbi'] = $dbi;*/ - // @todo: Replace this test with TableSearchController test + $GLOBALS['dbi'] = $dbi; + + $container = Container::getDefaultContainer(); + $container->set('db', 'PMA'); + $container->set('table', 'PMA_BookMark'); + $container->set('dbi', $GLOBALS['dbi']); } /** @@ -111,27 +119,6 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase } - /** - * Test for __construct - * - * @return void - * @group medium - */ - public function testConstruct() - { - /*$tableSearch = new PMA_TableSearch("PMA", "PMA_BookMark", "normal"); - $columNames = $tableSearch->getColumnNames(); - $this->assertEquals( - 'Field1', - $columNames[0] - ); - $this->assertEquals( - 'Field2', - $columNames[1] - );*/ - // @todo: Replace this test with TableSearchController test - } - /** * Test for getSelectionForm * @@ -239,7 +226,7 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase */ public function testReplace() { - /*$tableSearch = new PMA_TableSearch("PMA", "PMA_BookMark", "zoom"); + $tableSearch = new TableSearchController("zoom", null); $columnIndex = 0; $find = "Field"; $replaceWith = "Column"; @@ -250,14 +237,13 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase ); $sql_query = $GLOBALS['sql_query']; - $result = "UPDATE `PMA`.`PMA_BookMark` SET `Field1` = " + $result = "UPDATE `PMA_BookMark` SET `Field1` = " . "REPLACE(`Field1`, 'Field', 'Column') " . "WHERE `Field1` LIKE '%Field%' COLLATE UTF-8_bin"; $this->assertEquals( $result, $sql_query - );*/ - // @todo: Replace this test with TableSearchController test + ); } /** @@ -361,18 +347,21 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase * * @return void */ - public function testBuildSqlQueryw() + public function testBuildSqlQuery() { - /*$_POST['distinct'] = true; + $_POST['distinct'] = true; $_POST['zoom_submit'] = true; $_POST['table'] = "PMA"; $_POST['orderByColumn'] = "name"; $_POST['order'] = "asc"; $_POST['customWhereClause'] = "name='pma'"; - $tableSearch = new PMA_TableSearch("PMA", "PMA_BookMark", "zoom"); + $class = new ReflectionClass('PMA\Controllers\Table\TableSearchController'); + $method = $class->getMethod('_buildSqlQuery'); + $method->setAccessible(true); + $tableSearch = new TableSearchController("zoom", null); - $sql = $tableSearch->buildSqlQuery(); + $sql = $method->invoke($tableSearch); $result = "SELECT DISTINCT * FROM `PMA` WHERE name='pma' " . "ORDER BY `name` asc"; @@ -382,7 +371,7 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase ); unset($_POST['customWhereClause']); - $sql = $tableSearch->buildSqlQuery(); + $sql = $method->invoke($tableSearch); $result = "SELECT DISTINCT * FROM `PMA` ORDER BY `name` asc"; $this->assertEquals( $result, @@ -435,7 +424,7 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase "BETWEEN" ); - $sql = $tableSearch->buildSqlQuery(); + $sql = $method->invoke($tableSearch); $result = "SELECT DISTINCT * FROM `PMA` WHERE `name` != 'value1'" . " AND `id` > value2 AND `index` IS NULL AND `index2` LIKE '%value4%'" . " AND `index3` REGEXP ^value5$ AND `index4` IN (value6) AND `index5`" @@ -443,7 +432,6 @@ class PMA_TableSearch_Test extends PHPUnit_Framework_TestCase $this->assertEquals( $result, $sql - );*/ - // @todo: Replace this test with TableSearchController test + ); } } diff --git a/test/classes/PMA_Table_test.php b/test/classes/PMA_Table_test.php index 6395161029..3457129ecc 100644 --- a/test/classes/PMA_Table_test.php +++ b/test/classes/PMA_Table_test.php @@ -754,6 +754,36 @@ class PMA_Table_Test extends PHPUnit_Framework_TestCase ); } + /** + * Test for getColumnsMeta + * + * @return void + */ + public function testGetColumnsMeta() + { + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $dbi->expects($this->once()) + ->method('tryQuery') + ->with("SELECT * FROM `db`.`table` LIMIT 1") + ->will($this->returnValue('v1')); + + $dbi->expects($this->once()) + ->method('getFieldsMeta') + ->with("v1") + ->will($this->returnValue('movecols')); + + $GLOBALS['dbi'] = $dbi; + + $tableObj = new PMA_Table('table', 'db'); + + $this->assertEquals( + $tableObj->getColumnsMeta(), + 'movecols' + ); + } /** * Test for getColumns diff --git a/test/libraries/PMA_tbl_columns_definition_form_test.php b/test/libraries/PMA_tbl_columns_definition_form_test.php index afd7db0502..1ae4511df7 100644 --- a/test/libraries/PMA_tbl_columns_definition_form_test.php +++ b/test/libraries/PMA_tbl_columns_definition_form_test.php @@ -73,245 +73,6 @@ class PMA_TblColumnsDefinitionFormTest extends PHPUnit_Framework_TestCase */ } - /** - * Test for PMA_getMoveColumns - * - * @return void - */ - public function testGetMoveColumns() - { - /** - * @todo Test against PMA_Table::getColumnsMeta - * $dbi = $this->getMockBuilder('PMA_DatabaseInterface') - * ->disableOriginalConstructor() - * ->getMock(); - * - * $dbi->expects($this->once()) - * ->method('tryQuery') - * ->with("SELECT * FROM `db`.`table` LIMIT 1") - * ->will($this->returnValue('v1')); - * - * $dbi->expects($this->once()) - * ->method('getFieldsMeta') - * ->with("v1") - * ->will($this->returnValue('movecols')); - * - * $GLOBALS['dbi'] = $dbi; - * - * $this->assertEquals( - * PMA_getMoveColumns('db', 'table'), - * 'movecols' - * ); - */ - } - - /** - * Test for PMA_getRowDataForRegeneration - * - * @return void - */ - public function testGetRowDataForRegeneration() - { - /** @todo Move test - $_REQUEST = array( - 'field_name' => array(1 => 'name'), - 'field_type' => array(1 => 'type'), - 'field_collation' => array(1 => 'colltn'), - 'field_null' => array(1 => true), - 'field_key' => array(1 => "fulltext_1"), - 'field_default_type' => array(1 => 'USER_DEFINED'), - 'field_default_value' => array(1 => 'DEF'), - 'field_extra' => array(1 => 'extra') - ); - - $submit_fulltext = array(1 => 1); - - $result = PMA_getRowDataForRegeneration(1, $submit_fulltext); - - $this->assertEquals( - array( - 'Field' => 'name', - 'Type' => 'type', - 'Collation' => 'colltn', - 'Null' => true, - 'Key' => 'FULLTEXT', - 'DefaultType' => 'USER_DEFINED', - 'DefaultValue' => 'DEF', - 'Default' => 'DEF', - 'Extra' => 'extra', - 'Comment' => 'FULLTEXT' - ), - $result - ); - */ - } - - /** - * Test for PMA_getSubmitPropertiesForRegeneration - * - * @return void - */ - public function testGetSubmitPropertiesForRegeneration() - { - /** @todo Move test - $_REQUEST = array( - 'field_length' => array(1 => 22), - 'field_attribute' => array(1 => 'attr'), - 'field_default_current_timestamp' => array() - ); - - $result = PMA_getSubmitPropertiesForRegeneration(1); - - $this->assertEquals( - array(22, 'attr', false), - $result - ); - */ - } - - /** - * Test for PMA_getColumnMetaForDefault - * - * @return void - */ - public function testHandleRegeneration() - { - /** @todo Move test - $_REQUEST = array( - 'field_comments' => array(1 => 'comm'), - 'field_mimetype' => array(1 => 'mime'), - 'field_transformation' => array(1 => 'trans'), - 'field_transformation_options' => array(1 => 'transops') - ); - - $result = PMA_handleRegeneration(1, 'FULLTEXT', array(), array()); - - $this->assertEquals( - array('comm'), - $result[4] - ); - - $this->assertEquals( - array( - array( - 'mimetype' => 'mime', - 'transformation' => 'trans', - 'transformation_options' => 'transops' - ) - ), - $result[5] - ); - */ - } - - /** - * Test for PMA_getColumnMetaForDefault - * - * @return void - */ - public function testGetColumnMetaForDefault() - { - // @todo Move test - // $cmeta = array( - // 'Default' => null, - // 'Null' => 'YES', - // 'DefaultType' => 'a', - // 'DefaultValue' => 'b', - // ); - - // $result = PMA_getColumnMetaForDefault($cmeta, null); - - // $this->assertEquals( - // 'NULL', - // $result['DefaultType'] - // ); - - // $this->assertEquals( - // '', - // $result['DefaultValue'] - // ); - - // // case 2 - // $cmeta = array( - // 'Default' => null, - // 'Null' => 'NO', - // 'DefaultType' => 'a', - // 'DefaultValue' => 'b', - // ); - - // $result = PMA_getColumnMetaForDefault($cmeta, true); - - // $this->assertEquals( - // 'USER_DEFINED', - // $result['DefaultType'] - // ); - - // $this->assertEquals( - // null, - // $result['DefaultValue'] - // ); - - // // case 3 - // $cmeta = array( - // 'Default' => null, - // 'Null' => 'NO', - // 'DefaultType' => 'a', - // 'DefaultValue' => 'b', - // ); - - // $result = PMA_getColumnMetaForDefault($cmeta, false); - - // $this->assertEquals( - // 'NONE', - // $result['DefaultType'] - // ); - - // $this->assertEquals( - // null, - // $result['DefaultValue'] - // ); - - // // case 4 - // $cmeta = array( - // 'Default' => 'CURRENT_TIMESTAMP', - // 'Null' => 'NO', - // 'DefaultType' => 'a', - // 'DefaultValue' => 'b', - // ); - - // $result = PMA_getColumnMetaForDefault($cmeta, false); - - // $this->assertEquals( - // 'CURRENT_TIMESTAMP', - // $result['DefaultType'] - // ); - - // $this->assertEquals( - // null, - // $result['DefaultValue'] - // ); - - // // case 5 - // $cmeta = array( - // 'Default' => 'SOMETHING_ELSE', - // 'Null' => 'NO', - // 'DefaultType' => 'a', - // 'DefaultValue' => 'b', - // ); - - // $result = PMA_getColumnMetaForDefault($cmeta, false); - - // $this->assertEquals( - // 'USER_DEFINED', - // $result['DefaultType'] - // ); - - // $this->assertEquals( - // 'SOMETHING_ELSE', - // $result['DefaultValue'] - // ); - } - /** * Test for PMA_getHtmlForColumnName * From 59b11954f4605ba972f32536850ea5c404831217 Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 9 Aug 2015 14:18:56 +0800 Subject: [PATCH 2/8] Recover tests for PMA_Table and rename sGetStatusInfo to getStatusInfo Signed-off-by: Jason --- db_datadict.php | 2 +- libraries/DisplayResults.class.php | 2 +- libraries/Table.class.php | 10 +- .../DatabaseStructureController.class.php | 2 +- .../TableRelationController.class.php | 5 +- .../TableStructureController.class.php | 4 +- .../schema/pdf/Pdf_Relation_Schema.class.php | 2 +- libraries/tbl_info.inc.php | 4 +- tbl_relation.php | 2 +- .../table/relation/foreign_key_row.phtml | 2 +- test/classes/PMA_Table_test.php | 140 +++++++++++++++++- test/libraries/PMA_tbl_relation_test.php | 102 ------------- 12 files changed, 154 insertions(+), 123 deletions(-) diff --git a/db_datadict.php b/db_datadict.php index 8602d95f4a..b1abe9f4d6 100644 --- a/db_datadict.php +++ b/db_datadict.php @@ -68,7 +68,7 @@ foreach ($tables as $table) { * Gets table information */ $show_comment = $GLOBALS['dbi']->getTable($db, $table) - ->sGetStatusInfo('TABLE_COMMENT'); + ->getStatusInfo('TABLE_COMMENT'); /** * Gets table keys and retains them diff --git a/libraries/DisplayResults.class.php b/libraries/DisplayResults.class.php index c043658a64..925324d41d 100644 --- a/libraries/DisplayResults.class.php +++ b/libraries/DisplayResults.class.php @@ -1677,7 +1677,7 @@ class PMA_DisplayResults $data_html .= ''; } diff --git a/libraries/Table.class.php b/libraries/Table.class.php index 9647d5bc69..dba8c38d60 100644 --- a/libraries/Table.class.php +++ b/libraries/Table.class.php @@ -172,7 +172,7 @@ class PMA_Table if ($this->_dbi->getCachedTableContent("${db}.${table}") != null || $GLOBALS['cfg']['Server']['DisableIS'] ) { - $type = $this->sGetStatusInfo('TABLE_TYPE'); + $type = $this->getStatusInfo('TABLE_TYPE'); return $type == 'VIEW' || $type == 'SYSTEM VIEW'; } @@ -272,7 +272,7 @@ class PMA_Table $engine = null; // if called static, with parameters if (! empty($this->_db_name) && ! empty($this->_name)) { - $engine = $this->sGetStatusInfo('ENGINE', null, true); + $engine = $this->getStatusInfo('ENGINE', null, true); } // did we get engine? @@ -300,7 +300,7 @@ class PMA_Table * * @return mixed */ - public function sGetStatusInfo( + public function getStatusInfo( $info = null, $force_read = false, $disable_error = false @@ -1637,7 +1637,7 @@ class PMA_Table && isset($this->uiprefs[$property]) ) { // check if the table has not been modified - if ($this->sGetStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) { + if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) { return $this->uiprefs[$property]; } else { // remove the property, since the table has been modified @@ -1678,7 +1678,7 @@ class PMA_Table && ($property == self::PROP_COLUMN_ORDER || $property == self::PROP_COLUMN_VISIB) ) { - $curr_create_time = $this->sGetStatusInfo('CREATE_TIME'); + $curr_create_time = $this->getStatusInfo('CREATE_TIME'); if (isset($table_create_time) && $table_create_time == $curr_create_time ) { diff --git a/libraries/controllers/DatabaseStructureController.class.php b/libraries/controllers/DatabaseStructureController.class.php index 7cbba0376f..77ffe301e7 100644 --- a/libraries/controllers/DatabaseStructureController.class.php +++ b/libraries/controllers/DatabaseStructureController.class.php @@ -283,7 +283,7 @@ class DatabaseStructureController extends DatabaseController $showtable = $this->dbi->getTable( $this->db, $current_table['TABLE_NAME'] - )->sGetStatusInfo(null, true); + )->getStatusInfo(null, true); if ($GLOBALS['cfg']['ShowDbStructureCreation']) { $create_time = isset($showtable['Create_time']) diff --git a/libraries/controllers/TableRelationController.class.php b/libraries/controllers/TableRelationController.class.php index 174bd0b97f..0d7b4ef34c 100644 --- a/libraries/controllers/TableRelationController.class.php +++ b/libraries/controllers/TableRelationController.class.php @@ -157,7 +157,8 @@ class TableRelationController extends TableController // display secondary level tabs if necessary $engine = $this->dbi->getTable($this->db, $this->table) - ->sGetStatusInfo('ENGINE'); + ->getStatusInfo('ENGINE'); + $this->response->addHTML( Template::get('table/secondary_tabs')->render( array( @@ -369,7 +370,7 @@ class TableRelationController extends TableController $GLOBALS['dbi']->getTable( $_REQUEST['foreignDb'], $row[0] - )->sGetStatusInfo('Engine') + )->getStatusInfo('Engine') ); if (isset($engine) && $engine == $this->tbl_storage_engine) { $tables[] = htmlspecialchars($row[0]); diff --git a/libraries/controllers/TableStructureController.class.php b/libraries/controllers/TableStructureController.class.php index 3f14b01da2..08cab844a9 100644 --- a/libraries/controllers/TableStructureController.class.php +++ b/libraries/controllers/TableStructureController.class.php @@ -243,7 +243,7 @@ class TableStructureController extends TableController } // display secondary level tabs if necessary - $engine = $this->table_obj->sGetStatusInfo('ENGINE'); + $engine = $this->table_obj->getStatusInfo('ENGINE'); $this->response->addHTML( Template::get('table/secondary_tabs')->render( array( @@ -1059,7 +1059,7 @@ class TableStructureController extends TableController if (empty($this->_showtable)) { $this->_showtable = $this->dbi->getTable( $this->db, $this->table - )->sGetStatusInfo(null, true); + )->getStatusInfo(null, true); } if (empty($this->_showtable['Data_length'])) { diff --git a/libraries/plugins/schema/pdf/Pdf_Relation_Schema.class.php b/libraries/plugins/schema/pdf/Pdf_Relation_Schema.class.php index 5c06c9cb11..0d34d1d311 100644 --- a/libraries/plugins/schema/pdf/Pdf_Relation_Schema.class.php +++ b/libraries/plugins/schema/pdf/Pdf_Relation_Schema.class.php @@ -926,7 +926,7 @@ class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema /** * Gets table information */ - $showtable = $GLOBALS['dbi']->getTable($this->db, $table)->sGetStatusInfo(); + $showtable = $GLOBALS['dbi']->getTable($this->db, $table)->getStatusInfo(); $show_comment = isset($showtable['Comment']) ? $showtable['Comment'] : ''; diff --git a/libraries/tbl_info.inc.php b/libraries/tbl_info.inc.php index d8df3887fa..1f70188e7b 100644 --- a/libraries/tbl_info.inc.php +++ b/libraries/tbl_info.inc.php @@ -38,14 +38,14 @@ $GLOBALS['dbi']->selectDb($GLOBALS['db']); */ $GLOBALS['showtable'] = array(); -// PMA_Table::sGetStatusInfo() does caching by default, but here +// PMA_Table::getStatusInfo() does caching by default, but here // we force reading of the current table status // if $reread_info is true (for example, coming from tbl_operations.php // and we just changed the table's storage engine) $GLOBALS['showtable'] = $GLOBALS['dbi']->getTable( $GLOBALS['db'], $GLOBALS['table'] -)->sGetStatusInfo( +)->getStatusInfo( null, (isset($reread_info) && $reread_info ? true : false) ); diff --git a/tbl_relation.php b/tbl_relation.php index 4a767856e1..9c38a9ef8b 100644 --- a/tbl_relation.php +++ b/tbl_relation.php @@ -46,7 +46,7 @@ $options_array = array( $cfgRelation = PMA_getRelationsParam(); $tbl_storage_engine = /*overload*/ mb_strtoupper( - $GLOBALS['dbi']->getTable($db, $table)->sGetStatusInfo('Engine') + $GLOBALS['dbi']->getTable($db, $table)->getStatusInfo('Engine') ); $upd_query = new PMA_Table($table, $db, $dbi); diff --git a/templates/table/relation/foreign_key_row.phtml b/templates/table/relation/foreign_key_row.phtml index bc464c0f20..9d2a74efdd 100644 --- a/templates/table/relation/foreign_key_row.phtml +++ b/templates/table/relation/foreign_key_row.phtml @@ -58,7 +58,7 @@ if ($foreign_db) { $engine = $GLOBALS['dbi']->getTable( $foreign_db, $row[0] - )->sGetStatusInfo('Engine'); + )->getStatusInfo('Engine'); if (isset($engine) && /*overload*/mb_strtoupper($engine) == $tbl_storage_engine ) { diff --git a/test/classes/PMA_Table_test.php b/test/classes/PMA_Table_test.php index 3457129ecc..6d40bd5fe3 100644 --- a/test/classes/PMA_Table_test.php +++ b/test/classes/PMA_Table_test.php @@ -592,13 +592,92 @@ class PMA_Table_Test extends PHPUnit_Framework_TestCase } /** - * Test for isMerge + * Test for isMerge -- when there's no ENGINE info cached * * @return void */ - public function testIsMerge() + public function testIsMergeCase1() { - $this->markTestIncomplete('Not yet implemented!'); + $tableObj = new PMA_Table('PMA_BookMark', 'PMA'); + $this->assertEquals( + '', + $tableObj->isMerge() + ); + + $GLOBALS['dbi']->expects($this->any()) + ->method('getCachedTableContent') + ->will($this->returnValue(array('table_name' => "PMA_BookMark"))); + $tableObj = new PMA_Table('PMA_BookMark', 'PMA'); + $this->assertEquals( + false, + $tableObj->isMerge() + ); + } + + /** + * Test for isMerge -- when ENGINE info is MERGE + * + * @return void + */ + public function testIsMergeCase2() + { + $map = array( + array('PMA.PMA_BookMark', null, array('ENGINE' => "MERGE")), + array('PMA.PMA_BookMark.ENGINE', null, "MERGE") + ); + $GLOBALS['dbi']->expects($this->any()) + ->method('getCachedTableContent') + ->will($this->returnValueMap($map)); + + $tableObj = new PMA_Table('PMA_BookMark', 'PMA'); + $this->assertEquals( + true, + $tableObj->isMerge() + ); + } + + /** + * Test for isMerge -- when ENGINE info is MRG_MYISAM + * + * @return void + */ + public function testIsMergeCase3() + { + $map = array( + array('PMA.PMA_BookMark', null, array('ENGINE' => "MRG_MYISAM")), + array('PMA.PMA_BookMark.ENGINE', null, "MRG_MYISAM") + ); + $GLOBALS['dbi']->expects($this->any()) + ->method('getCachedTableContent') + ->will($this->returnValueMap($map)); + + $tableObj = new PMA_Table('PMA_BookMark', 'PMA'); + $this->assertEquals( + true, + $tableObj->isMerge() + ); + } + + /** + * Test for isMerge -- when ENGINE info is ISDB + * + * @return void + */ + public function testIsMergeCase4() + { + $map = array( + array('PMA.PMA_BookMark', null, array('ENGINE' => "ISDB")), + array('PMA.PMA_BookMark.ENGINE', null, "ISDB") + ); + $GLOBALS['dbi']->expects($this->any()) + ->method('getCachedTableContent') + ->will($this->returnValueMap($map)); + + $tableObj = new PMA_Table('PMA_BookMark', 'PMA'); + $this->assertEquals( + false, + $tableObj->isMerge() + ); } /** @@ -785,6 +864,42 @@ class PMA_Table_Test extends PHPUnit_Framework_TestCase ); } + /** + * Tests for _getSQLToCreateForeignKey() method. + * + * @return void + * @test + */ + public function testGetSQLToCreateForeignKey() + { + $table = "PMA_table"; + $field = array("PMA_field1", "PMA_field2"); + $foreignDb = "foreignDb"; + $foreignTable = "foreignTable"; + $foreignField = array("foreignField1", "foreignField2"); + + $class = new ReflectionClass('PMA_Table'); + $method = $class->getMethod('_getSQLToCreateForeignKey'); + $method->setAccessible(true); + $tableObj = new PMA_Table('PMA_table', 'db'); + + $sql = $method->invokeArgs($tableObj, array( + $table, + $field, + $foreignDb, + $foreignTable, + $foreignField + ) + ); + $sql_excepted = 'ALTER TABLE `PMA_table` ADD ' + . 'FOREIGN KEY (`PMA_field1`, `PMA_field2`) REFERENCES ' + . '`foreignDb`.`foreignTable`(`foreignField1`, `foreignField2`);'; + $this->assertEquals( + $sql_excepted, + $sql + ); + } + /** * Test for getColumns * @@ -829,7 +944,24 @@ class PMA_Table_Test extends PHPUnit_Framework_TestCase */ public function testCountRecords() { - $this->markTestIncomplete('Not yet implemented!'); + $map = array( + array('PMA.PMA_BookMark', null, array('Comment' => "Comment222", 'TABLE_TYPE' => "VIEW")), + array('PMA.PMA_BookMark.TABLE_TYPE', null, 'VIEW') + ); + $GLOBALS['dbi']->expects($this->any()) + ->method('getCachedTableContent') + ->will($this->returnValueMap($map)); + + $table = 'PMA_BookMark'; + $db = 'PMA'; + $tableObj = new PMA_Table($table, $db); + + $return = $tableObj->countRecords(true); + $expect = 20; + $this->assertEquals( + $expect, + $return + ); } /** diff --git a/test/libraries/PMA_tbl_relation_test.php b/test/libraries/PMA_tbl_relation_test.php index 84889d1246..2210902657 100644 --- a/test/libraries/PMA_tbl_relation_test.php +++ b/test/libraries/PMA_tbl_relation_test.php @@ -47,35 +47,6 @@ class PMA_TblRelationTest extends PHPUnit_Framework_TestCase $GLOBALS['dbi'] = $dbi; } - /** - * Tests for PMA_getSQLToCreateForeignKey() method. - * - * @return void - * @test - */ - public function testPMAGetSQLToCreateForeignKey() - { - // @todo Move this test to PMA_Table_test - /* - $table = "PMA_table"; - $field = array("PMA_field1", "PMA_field2"); - $foreignDb = "foreignDb"; - $foreignTable = "foreignTable"; - $foreignField = array("foreignField1", "foreignField2"); - - $sql = PMA_getSQLToCreateForeignKey( - $table, $field, $foreignDb, $foreignTable, $foreignField - ); - $sql_excepted = 'ALTER TABLE `PMA_table` ADD ' - . 'FOREIGN KEY (`PMA_field1`, `PMA_field2`) REFERENCES ' - . '`foreignDb`.`foreignTable`(`foreignField1`, `foreignField2`);'; - $this->assertEquals( - $sql_excepted, - $sql - ); - */ - } - /** * Tests for PMA_getHtmlForCommonForm() method. * @@ -86,79 +57,6 @@ class PMA_TblRelationTest extends PHPUnit_Framework_TestCase { // @todo Find out a better method to test for HTML } - - /** - * Tests for PMA_getQueryForDisplayUpdate() method. - * @todo Move this test to PMA_Table_test - * - * @return void - * @test - */ - public function testPMAGetQueryForDisplayUpdate() - { - /* - $disp = true; - $display_field = ''; - $db = "pma_db"; - $table = "pma_table"; - $cfgRelation = array( - 'displaywork' => true, - 'relwork' => true, - 'displaywork' => true, - 'table_info' => 'table_info', - ); - - $GLOBALS['cfgRelation']['db'] = 'global_db'; - - //case 1: $disp == true && $display_field == '' - $query = PMA_getQueryForDisplayUpdate( - $disp, $display_field, $db, $table, $cfgRelation - ); - $query_expect = "DELETE FROM `global_db`.`table_info` " - . "WHERE db_name = 'pma_db' AND table_name = 'pma_table'"; - $this->assertEquals( - $query_expect, - $query - ); - - //case 2: $disp == true && $display_field == 'display_field' - $display_field == 'display_field'; - $query = PMA_getQueryForDisplayUpdate( - $disp, $display_field, $db, $table, $cfgRelation - ); - $query_expect = "DELETE FROM `global_db`.`table_info` " - . "WHERE db_name = 'pma_db' AND table_name = 'pma_table'"; - $this->assertEquals( - $query_expect, - $query - ); - - //case 3: $disp == false && $display_field == 'display_field' - $disp = false; - $display_field = 'display_field'; - $query = PMA_getQueryForDisplayUpdate( - $disp, $display_field, $db, $table, $cfgRelation - ); - $query_expect = "INSERT INTO `global_db`.`table_info`" - . "(db_name, table_name, display_field)" - . " VALUES('pma_db','pma_table','display_field')"; - $this->assertEquals( - $query_expect, - $query - ); - - //case 4: $disp == false && $display_field == '' - $disp = false; - $display_field = ''; - $query = PMA_getQueryForDisplayUpdate( - $disp, $display_field, $db, $table, $cfgRelation - ); - $query_expect = ''; - $this->assertEquals( - $query_expect, - $query - );*/ - } } /** From 3613f2b6bd11505c661919d0cb4572f499bfd416 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 10 Aug 2015 00:05:40 +0800 Subject: [PATCH 3/8] Introduce the ResponseStub Signed-off-by: Jason --- db_structure.php | 5 + libraries/controllers/Controller.class.php | 3 +- tbl_chart.php | 5 + tbl_find_replace.php | 3 + tbl_gis_visualization.php | 4 + tbl_indexes.php | 4 + tbl_relation.php | 4 + tbl_select.php | 3 + tbl_structure.php | 5 + tbl_zoom_select.php | 3 + test/libraries/stubs/ResponseStub.php | 134 +++++++++++++++++++++ 11 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 test/libraries/stubs/ResponseStub.php diff --git a/db_structure.php b/db_structure.php index 9d24d0d780..c7e99a7559 100644 --- a/db_structure.php +++ b/db_structure.php @@ -8,17 +8,22 @@ namespace PMA; +use PMA_Response; + require_once 'libraries/common.inc.php'; require_once 'libraries/db_common.inc.php'; require_once 'libraries/db_info.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/DatabaseStructureController.class.php'; +require_once 'libraries/Response.class.php'; $container = DI\Container::getDefaultContainer(); $container->factory('PMA\Controllers\DatabaseStructureController'); $container->alias( 'DatabaseStructureController', 'PMA\Controllers\DatabaseStructureController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); global $db, $pos, $db_is_system_schema, $total_num_tables, $tables, $num_tables; /* Define dependencies for the concerned controller */ diff --git a/libraries/controllers/Controller.class.php b/libraries/controllers/Controller.class.php index 6bac1e73d5..d57563de23 100644 --- a/libraries/controllers/Controller.class.php +++ b/libraries/controllers/Controller.class.php @@ -16,7 +16,6 @@ if (!defined('PHPMYADMIN')) { exit; } -require_once 'libraries/Response.class.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/database_interface.inc.php'; @@ -51,6 +50,6 @@ abstract class Controller $container = Container::getDefaultContainer(); $this->container = $container; $this->dbi = $this->container->get('dbi'); - $this->response = PMA_Response::getInstance(); + $this->response = $this->container->get('response'); } } diff --git a/tbl_chart.php b/tbl_chart.php index fcc5a9a6e2..a77d63581a 100644 --- a/tbl_chart.php +++ b/tbl_chart.php @@ -8,7 +8,10 @@ namespace PMA; +use PMA_Response; + require_once 'libraries/di/Container.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/controllers/TableChartController.class.php'; $container = DI\Container::getDefaultContainer(); @@ -16,6 +19,8 @@ $container->factory('PMA\Controllers\Table\TableChartController'); $container->alias( 'TableChartController', 'PMA\Controllers\Table\TableChartController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $dependency_definitions = array( diff --git a/tbl_find_replace.php b/tbl_find_replace.php index c9cd2464e9..dd5def5c9d 100644 --- a/tbl_find_replace.php +++ b/tbl_find_replace.php @@ -15,6 +15,7 @@ require_once 'libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/di/Container.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/controllers/TableSearchController.class.php'; use PMA\DI; @@ -24,6 +25,8 @@ $container->factory('PMA\Controllers\Table\TableSearchController'); $container->alias( 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); $dependency_definitions = array( 'searchType' => 'replace', diff --git a/tbl_gis_visualization.php b/tbl_gis_visualization.php index 7b597da5a2..94c8956cfc 100644 --- a/tbl_gis_visualization.php +++ b/tbl_gis_visualization.php @@ -8,9 +8,11 @@ namespace PMA; +use PMA_Response; use PMA_Util; require_once 'libraries/di/Container.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/controllers/TableGisVisualizationController.class.php'; require_once 'libraries/Util.class.php'; @@ -20,6 +22,8 @@ $container->alias( 'TableGisVisualizationController', 'PMA\Controllers\Table\TableGisVisualizationController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $dependency_definitions = array( diff --git a/tbl_indexes.php b/tbl_indexes.php index 854c94ef44..10d63ea25b 100644 --- a/tbl_indexes.php +++ b/tbl_indexes.php @@ -9,9 +9,11 @@ namespace PMA; use PMA_Index; +use PMA_Response; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableIndexesController.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/Index.class.php'; $container = DI\Container::getDefaultContainer(); @@ -19,6 +21,8 @@ $container->factory('PMA\Controllers\Table\TableIndexesController'); $container->alias( 'TableIndexesController', 'PMA\Controllers\Table\TableIndexesController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $db = $container->get('db'); diff --git a/tbl_relation.php b/tbl_relation.php index 9c38a9ef8b..7005611c03 100644 --- a/tbl_relation.php +++ b/tbl_relation.php @@ -19,11 +19,13 @@ */ namespace PMA; +use PMA_Response; use PMA_Table; use PMA_Util; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableRelationController.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/Table.class.php'; require_once 'libraries/Util.class.php'; @@ -32,6 +34,8 @@ $container->factory('PMA\Controllers\Table\TableRelationController'); $container->alias( 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $db = $container->get('db'); diff --git a/tbl_select.php b/tbl_select.php index 66d62ddb6f..cb4e195582 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -16,6 +16,7 @@ require_once 'libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/di/Container.class.php'; +require_once 'libraries/Response.class.php'; require_once 'libraries/controllers/TableSearchController.class.php'; use PMA\DI; @@ -25,6 +26,8 @@ $container->factory('PMA\Controllers\Table\TableSearchController'); $container->alias( 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $dependency_definitions = array( diff --git a/tbl_structure.php b/tbl_structure.php index ae93d3bdd4..814c9701ba 100644 --- a/tbl_structure.php +++ b/tbl_structure.php @@ -9,18 +9,23 @@ namespace PMA; +use PMA_Response; + require_once 'libraries/common.inc.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/config/page_settings.class.php'; require_once 'libraries/bookmark.lib.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableStructureController.class.php'; +require_once 'libraries/Response.class.php'; $container = DI\Container::getDefaultContainer(); $container->factory('PMA\Controllers\TableStructureController'); $container->alias( 'TableStructureController', 'PMA\Controllers\TableStructureController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); global $db, $table, $db_is_system_schema, $tbl_is_view, $tbl_storage_engine, $table_info_num_rows, $tbl_collation, $showtable; diff --git a/tbl_zoom_select.php b/tbl_zoom_select.php index aedc098fc9..621aa07790 100644 --- a/tbl_zoom_select.php +++ b/tbl_zoom_select.php @@ -15,6 +15,7 @@ require_once './libraries/common.inc.php'; require_once 'libraries/tbl_common.inc.php'; require_once 'libraries/tbl_info.inc.php'; require_once './libraries/di/Container.class.php'; +require_once './libraries/Response.class.php'; require_once './libraries/controllers/TableSearchController.class.php'; use PMA\DI; @@ -24,6 +25,8 @@ $container->factory('PMA\Controllers\Table\TableSearchController'); $container->alias( 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' ); +$container->set('PMA_Response', PMA_Response::getInstance()); +$container->alias('response', 'PMA_Response'); /* Define dependencies for the concerned controller */ $dependency_definitions = array( diff --git a/test/libraries/stubs/ResponseStub.php b/test/libraries/stubs/ResponseStub.php new file mode 100644 index 0000000000..14b8b0dff9 --- /dev/null +++ b/test/libraries/stubs/ResponseStub.php @@ -0,0 +1,134 @@ +htmlString = ''; + $this->json = array(); + $this->header = new PMA_Header(); + } + + /** + * Add HTML code to the response stub + * + * @param string $content A string to be appended to + * the current output buffer + * + * @return void + */ + public function addHTML($content) + { + if (is_array($content)) { + foreach ($content as $msg) { + $this->addHTML($msg); + } + } elseif ($content instanceof PMA_Message) { + $this->htmlString .= $content->getDisplay(); + } else { + $this->htmlString .= $content; + } + } + + /** + * Add JSON code to the response stub + * + * @param mixed $json Either a key (string) or an + * array or key-value pairs + * @param mixed $value Null, if passing an array in $json otherwise + * it's a string value to the key + * + * @return void + */ + public function addJSON($json, $value = null) + { + if (is_array($json)) { + foreach ($json as $key => $value) { + $this->addJSON($key, $value); + } + } else { + if ($value instanceof PMA_Message) { + $this->json[$json] = $value->getDisplay(); + } else { + $this->json[$json] = $value; + } + } + } + + /** + * Return the final concatenated HTML string + * + * @return string + */ + public function getHTMLResult() + { + return $this->htmlString; + } + + /** + * Return the final JSON array + * + * @return array + */ + public function getJSONResult() + { + return $this->json; + } + + /** + * Current I choose to return PMA_Header object directly because + * our test has nothing about the PMA_Scripts and PMA_Header class. + * + * @return PMA_Header + */ + public function getHeader() + { + return $this->header; + } +} From 0cfaa66032b2f7eb3bd3a7cadd358320f2999c11 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 12 Aug 2015 16:29:53 +0800 Subject: [PATCH 4/8] Complete tests for TableIndexesController Signed-off-by: Jason --- .../TableIndexesController.class.php | 13 +- tbl_indexes.php | 1 + test/classes/PMA_Table_test.php | 24 ++ .../PMA_TableIndexesController_test.php | 227 ++++++++++++++++++ test/libraries/PMA_tbl_indexes_test.php | 195 --------------- test/libraries/stubs/ResponseStub.php | 47 ++++ 6 files changed, 304 insertions(+), 203 deletions(-) create mode 100644 test/libraries/PMA_TableIndexesController_test.php delete mode 100644 test/libraries/PMA_tbl_indexes_test.php diff --git a/libraries/controllers/TableIndexesController.class.php b/libraries/controllers/TableIndexesController.class.php index 524d55807d..6d29877ee0 100644 --- a/libraries/controllers/TableIndexesController.class.php +++ b/libraries/controllers/TableIndexesController.class.php @@ -16,7 +16,6 @@ use PMA_Response; use PMA\Template; use PMA_Util; -require_once 'libraries/common.inc.php'; require_once 'libraries/Index.class.php'; require_once 'libraries/Message.class.php'; require_once 'libraries/Util.class.php'; @@ -144,7 +143,7 @@ class TableIndexesController extends TableController // If there is a request for SQL previewing. if (isset($_REQUEST['preview_sql'])) { - PMA_Response::getInstance()->addJSON( + $this->response->addJSON( 'sql_data', Template::get('preview_sql') ->render( @@ -161,11 +160,10 @@ class TableIndexesController extends TableController __('Table %1$s has been altered successfully.') ); $message->addParam($this->table); - $response = PMA_Response::getInstance(); - $response->addJSON( + $this->response->addJSON( 'message', PMA_Util::getMessage($message, $sql_query, 'success') ); - $response->addJSON( + $this->response->addJSON( 'index_table', PMA_Index::getHtmlForIndexes( $this->table, $this->db @@ -175,9 +173,8 @@ class TableIndexesController extends TableController include 'tbl_structure.php'; } } else { - $response = PMA_Response::getInstance(); - $response->isSuccess(false); - $response->addJSON('message', $error); + $this->response->isSuccess(false); + $this->response->addJSON('message', $error); } } } diff --git a/tbl_indexes.php b/tbl_indexes.php index 10d63ea25b..63fee93951 100644 --- a/tbl_indexes.php +++ b/tbl_indexes.php @@ -11,6 +11,7 @@ namespace PMA; use PMA_Index; use PMA_Response; +require_once 'libraries/common.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableIndexesController.class.php'; require_once 'libraries/Response.class.php'; diff --git a/test/classes/PMA_Table_test.php b/test/classes/PMA_Table_test.php index 6d40bd5fe3..81fcbb3f7e 100644 --- a/test/classes/PMA_Table_test.php +++ b/test/classes/PMA_Table_test.php @@ -900,6 +900,30 @@ class PMA_Table_Test extends PHPUnit_Framework_TestCase ); } + /** + * Tests for getSqlQueryForIndexCreateOrEdit() method. + * + * @return void + * @test + */ + public function testGetSqlQueryForIndexCreateOrEdit() + { + $db = "pma_db"; + $table = "pma_table"; + $index = new PMA_Index(); + $error = false; + + $_REQUEST['old_index'] = "PRIMARY"; + + $table = new PMA_Table($table, $db); + $sql = $table->getSqlQueryForIndexCreateOrEdit($index, $error); + + $this->assertEquals( + "ALTER TABLE `pma_db`.`pma_table` DROP PRIMARY KEY, ADD UNIQUE ;", + $sql + ); + } + /** * Test for getColumns * diff --git a/test/libraries/PMA_TableIndexesController_test.php b/test/libraries/PMA_TableIndexesController_test.php new file mode 100644 index 0000000000..6d00b3ab7d --- /dev/null +++ b/test/libraries/PMA_TableIndexesController_test.php @@ -0,0 +1,227 @@ + 'db', + 'server' => 1 + ); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $indexs = array( + array( + "Schema" => "Schema1", + "Key_name"=>"Key_name1", + "Column_name"=>"Column_name1" + ), + array( + "Schema" => "Schema2", + "Key_name"=>"Key_name2", + "Column_name"=>"Column_name2" + ), + array( + "Schema" => "Schema3", + "Key_name"=>"Key_name3", + "Column_name"=>"Column_name3" + ), + ); + + $dbi->expects($this->any())->method('getTableIndexes') + ->will($this->returnValue($indexs)); + + $GLOBALS['dbi'] = $dbi; + + //$_SESSION + $_SESSION['PMA_Theme'] = PMA_Theme::load('./themes/pmahomme'); + $_SESSION['PMA_Theme'] = new PMA_Theme(); + } + + /** + * Tests for doSaveDataAction() method + * + * @return void + * @test + */ + public function testDoSaveDataAction() + { + $sql_query = 'ALTER TABLE `db`.`table` DROP PRIMARY KEY, ADD UNIQUE ;'; + + $table = $this->getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + $table->expects($this->any())->method('getSqlQueryForIndexCreateOrEdit') + ->will($this->returnValue($sql_query)); + + $GLOBALS['dbi']->expects($this->any())->method('getTable') + ->will($this->returnValue($table)); + + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $response); + $container->alias('response', 'PMA_Response'); + + $ctrl = new TableIndexesController(null); + + // Preview SQL + $_REQUEST['preview_sql'] = true; + $ctrl->doSaveDataAction(); + $jsonArray = $response->getJSONResult(); + $this->assertArrayHasKey('sql_data', $jsonArray); + $this->assertContains( + $sql_query, + $jsonArray['sql_data'] + ); + + // Alter success + $response->clear(); + unset($_REQUEST['preview_sql']); + $GLOBALS['is_ajax_request'] = true; + $ctrl->doSaveDataAction(); + $jsonArray = $response->getJSONResult(); + $this->assertArrayHasKey('index_table', $jsonArray); + $this->assertArrayHasKey('message', $jsonArray); + } + + /** + * Tests for displayFormAction() + * + * @return void + * @test + */ + public function testDisplayFormAction() + { + $table = $this->getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + $table->expects($this->any())->method('getStatusInfo') + ->will($this->returnValue("")); + $table->expects($this->any())->method('isView') + ->will($this->returnValue(false)); + $table->expects($this->any())->method('getNameAndTypeOfTheColumns') + ->will($this->returnValue(array("field_name" => "field_type"))); + + $GLOBALS['dbi']->expects($this->any())->method('getTable') + ->will($this->returnValue($table)); + + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $response); + $container->alias('response', 'PMA_Response'); + $index = new PMA_Index(); + + $ctrl = new TableIndexesController($index); + + $_REQUEST['create_index'] = true; + $_REQUEST['added_fields'] = 3; + $ctrl->displayFormAction(); + $html = $response->getHTMLResult(); + + //PMA_URL_getHiddenInputs + $this->assertContains( + PMA_URL_getHiddenInputs( + array( + 'db' => 'db', + 'table' => 'table', + 'create_index' => 1, + ) + ), + $html + ); + + $doc_html = PMA_Util::showHint( + PMA_Message::notice( + __( + '"PRIMARY" must be the name of' + . ' and only of a primary key!' + ) + ) + ); + $this->assertContains( + $doc_html, + $html + ); + + $this->assertContains( + PMA_Util::showMySQLDocu('ALTER_TABLE'), + $html + ); + + // generateIndexSelector + $this->assertContains( + PMA\Template::trim($index->generateIndexChoiceSelector(false)), + $html + ); + + $this->assertContains( + sprintf(__('Add %s column(s) to index'), 1), + $html + ); + + //$field_name & $field_type + $this->assertContains( + "field_name", + $html + ); + $this->assertContains( + "field_type", + $html + ); + } +} diff --git a/test/libraries/PMA_tbl_indexes_test.php b/test/libraries/PMA_tbl_indexes_test.php deleted file mode 100644 index 91ec94ba8b..0000000000 --- a/test/libraries/PMA_tbl_indexes_test.php +++ /dev/null @@ -1,195 +0,0 @@ -getMockBuilder('PMA_DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $indexs = array( - array( - "Schema" => "Schema1", - "Key_name"=>"Key_name1", - "Column_name"=>"Column_name1" - ), - array( - "Schema" => "Schema2", - "Key_name"=>"Key_name2", - "Column_name"=>"Column_name2" - ), - array( - "Schema" => "Schema3", - "Key_name"=>"Key_name3", - "Column_name"=>"Column_name3" - ), - ); - - $dbi->expects($this->any())->method('getTableIndexes') - ->will($this->returnValue($indexs)); - - $GLOBALS['dbi'] = $dbi; - - //$_SESSION - $_SESSION['PMA_Theme'] = PMA_Theme::load('./themes/pmahomme'); - $_SESSION['PMA_Theme'] = new PMA_Theme(); - } - - /** - * Tests for PMA_getSqlQueryForIndexCreateOrEdit() method. - * - * @return void - * @test - */ - public function testPMAGetSqlQueryForIndexCreateOrEdit() - { - $db = "pma_db"; - $table = "pma_table"; - $index = new PMA_Index(); - $error = false; - - $_REQUEST['old_index'] = "PRIMARY"; - - $table = new PMA_Table($table, $db); - $sql = $table->getSqlQueryForIndexCreateOrEdit($index, $error); - - $this->assertEquals( - "ALTER TABLE `pma_db`.`pma_table` DROP PRIMARY KEY, ADD UNIQUE ;", - $sql - ); - } - - /** - * Tests for PMA_getHtmlForIndexForm() method. - * - * @return void - * @test - */ - public function testPMAGetHtmlForIndexForm() - { - /** - * @todo Find out a better method to test for HTML - * - * $fields = array("field_name" => "field_type"); - * $index = new PMA_Index(); - * $form_params = array( - * 'db' => 'db', - * 'table' => 'table', - * 'create_index' => 1, - * ); - * $add_fields = 3; - * - * $html = PMA_getHtmlForIndexForm( - * $fields, $index, $form_params, $add_fields - * ); - * - * //PMA_URL_getHiddenInputs - * $this->assertContains( - * PMA_URL_getHiddenInputs($form_params), - * $html - * ); - * - * //Index name - * $this->assertContains( - * __('Index name:'), - * $html - * ); - * $doc_html = PMA_Util::showHint( - * PMA_Message::notice( - * __( - * '"PRIMARY" must be the name of' - * . ' and only of a primary key!' - * ) - * ) - * ); - * $this->assertContains( - * $doc_html, - * $html - * ); - * - * //Index name - * $this->assertContains( - * __('Index name:'), - * $html - * ); - * $this->assertContains( - * PMA_Util::showMySQLDocu('ALTER_TABLE'), - * $html - * ); - * - * //generateIndexSelector - * $this->assertContains( - * PMA\Template::trim($index->generateIndexChoiceSelector(false)), - * $html - * ); - * - * //items - * $this->assertContains( - * __('Column'), - * $html - * ); - * $this->assertContains( - * __('Size'), - * $html - * ); - * $this->assertContains( - * sprintf(__('Add %s column(s) to index'), 1), - * $html - * ); - * - * //$field_name & $field_type - * $this->assertContains( - * "field_name", - * $html - * ); - * $this->assertContains( - * "field_type", - * $html - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } -} diff --git a/test/libraries/stubs/ResponseStub.php b/test/libraries/stubs/ResponseStub.php index 14b8b0dff9..8df9a0ea33 100644 --- a/test/libraries/stubs/ResponseStub.php +++ b/test/libraries/stubs/ResponseStub.php @@ -45,13 +45,25 @@ class PMA_Response */ protected $json; + /** + * Whether there were any errors during the processing of the request + * Only used for ajax responses + * + * @access private + * @var bool + */ + protected $_isSuccess; + /** * Creates a new class instance */ public function __construct() { + $this->_isSuccess = true; $this->htmlString = ''; $this->json = array(); + + $GLOBALS['lang'] = 'en'; $this->header = new PMA_Header(); } @@ -131,4 +143,39 @@ class PMA_Response { return $this->header; } + + /** + * Set the status of an ajax response, + * whether it is a success or an error + * + * @param bool $state Whether the request was successfully processed + * + * @return void + */ + public function isSuccess($state) + { + $this->_isSuccess = $state; + } + + /** + * Get the status of an ajax response. + * + * @return bool + */ + public function getSuccessSate() + { + return $this->_isSuccess; + } + + /** + * This function is used to clear all data to this + * stub after any operations. + * + */ + public function clear() + { + $this->_isSuccess = true; + $this->json = array(); + $this->htmlString = ''; + } } From a121073c31a5d3bc21b450b2cc62da79c728f2b8 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 17 Aug 2015 01:31:14 +0800 Subject: [PATCH 5/8] Add tests for DatabaseStructureController Signed-off-by: Jason --- .../DatabaseStructureController.class.php | 11 +- .../TableStructureController.class.php | 4 +- tbl_structure.php | 1 + .../DatabaseStructureController_test.php | 420 ++++++++++++++++++ .../TableIndexesController_test.php} | 0 .../TableSearchController_test.php} | 0 test/libraries/PMA_structure_test.php | 279 ------------ 7 files changed, 427 insertions(+), 288 deletions(-) create mode 100644 test/classes/controllers/DatabaseStructureController_test.php rename test/{libraries/PMA_TableIndexesController_test.php => classes/controllers/TableIndexesController_test.php} (100%) rename test/classes/{PMA_TableSearchController_test.php => controllers/TableSearchController_test.php} (100%) delete mode 100644 test/libraries/PMA_structure_test.php diff --git a/libraries/controllers/DatabaseStructureController.class.php b/libraries/controllers/DatabaseStructureController.class.php index 77ffe301e7..57127e4bb0 100644 --- a/libraries/controllers/DatabaseStructureController.class.php +++ b/libraries/controllers/DatabaseStructureController.class.php @@ -16,7 +16,6 @@ use PMA_Message; use PMA_PageSettings; use PMA_Util; -require_once 'libraries/common.inc.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/config/page_settings.class.php'; require_once 'libraries/display_create_table.lib.php'; @@ -94,7 +93,7 @@ class DatabaseStructureController extends DatabaseController { // Add/Remove favorite tables using Ajax request. if ($GLOBALS['is_ajax_request'] && !empty($_REQUEST['favorite_table'])) { - $this->addRemoveFavoriteTables(); + $this->addRemoveFavoriteTablesAction(); return; } @@ -693,7 +692,7 @@ class DatabaseStructureController extends DatabaseController * * @return void */ - protected function addRemoveFavoriteTables() + public function addRemoveFavoriteTablesAction() { $fav_instance = PMA_RecentFavoriteTable::getInstance('favorite'); if (isset($_REQUEST['favorite_tables'])) { @@ -829,7 +828,7 @@ class DatabaseStructureController extends DatabaseController } $favorite_tables[$user] = $fav_instance->getTables(); - $$this->response->addJSON( + $this->response->addJSON( array( 'favorite_tables' => json_encode($favorite_tables), 'list' => $fav_instance->getHtmlList() @@ -862,8 +861,8 @@ class DatabaseStructureController extends DatabaseController /** * Find table with truename * - * @param array $db DB to look into - * @param bool $truename Table name + * @param array $db DB to look into + * @param string $truename Table name * * @return bool */ diff --git a/libraries/controllers/TableStructureController.class.php b/libraries/controllers/TableStructureController.class.php index 08cab844a9..d61cb4cccd 100644 --- a/libraries/controllers/TableStructureController.class.php +++ b/libraries/controllers/TableStructureController.class.php @@ -18,8 +18,6 @@ use PMA_Util; use PMA\Util; use SqlParser; -require_once 'libraries/common.inc.php'; -require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/Index.class.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/config/page_settings.class.php'; @@ -101,7 +99,7 @@ class TableStructureController extends TableController $this->_table_info_num_rows = $table_info_num_rows; $this->_tbl_collation = $tbl_collation; $this->_showtable = $showtable; - $this->table_obj = new PMA_Table($this->table, $this->db); + $this->table_obj = $this->dbi->getTable($this->db, $this->table); } /** diff --git a/tbl_structure.php b/tbl_structure.php index 814c9701ba..a65748509a 100644 --- a/tbl_structure.php +++ b/tbl_structure.php @@ -12,6 +12,7 @@ namespace PMA; use PMA_Response; require_once 'libraries/common.inc.php'; +require_once 'libraries/tbl_info.inc.php'; require_once 'libraries/mysql_charsets.inc.php'; require_once 'libraries/config/page_settings.class.php'; require_once 'libraries/bookmark.lib.php'; diff --git a/test/classes/controllers/DatabaseStructureController_test.php b/test/classes/controllers/DatabaseStructureController_test.php new file mode 100644 index 0000000000..abd4562526 --- /dev/null +++ b/test/classes/controllers/DatabaseStructureController_test.php @@ -0,0 +1,420 @@ +getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + // Expect the table will have 6 rows + $table->expects($this->any())->method('getRealRowCountTable') + ->will($this->returnValue(6)); + $table->expects($this->any())->method('countRecords') + ->will($this->returnValue(6)); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $dbi->expects($this->any())->method('getTable') + ->will($this->returnValue($table)); + + $GLOBALS['dbi'] = $dbi; + + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $this->response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $this->response); + $container->alias('response', 'PMA_Response'); + } + + /** + * Tests for getValuesForInnodbTable() + * + * @return void + * @test + */ + public function testGetValuesForInnodbTable() + { + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $response); + $container->alias('response', 'PMA_Response'); + + $class = new ReflectionClass('PMA\Controllers\DatabaseStructureController'); + $method = $class->getMethod('getValuesForInnodbTable'); + $method->setAccessible(true); + // Showing statistics + $is_show_stats = true; + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, $is_show_stats); + + $GLOBALS['cfg']['MaxExactCount'] = 10; + $current_table = array( + 'ENGINE' => 'InnoDB', + 'TABLE_ROWS' => 5, + 'Data_length' => 16384, + 'Index_length' => 0, + 'TABLE_NAME' => 'table' + ); + list($current_table, $formatted_size, $unit, $sum_size) + = $method->invokeArgs($ctrl, array($current_table, 10)); + + $this->assertEquals( + true, + $current_table['COUNTED'] + ); + $this->assertEquals( + 6, + $current_table['TABLE_ROWS'] + ); + $this->assertEquals( + 16394, + $sum_size + ); + + $current_table['ENGINE'] = 'MYISAM'; + list($current_table, $formatted_size, $unit, $sum_size) + = $method->invokeArgs($ctrl, array($current_table, 10)); + + $this->assertEquals( + false, + $current_table['COUNTED'] + ); + $this->assertEquals( + 16394, + $sum_size + ); + // Not showing statistics + $is_show_stats = false; + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, $is_show_stats); + + $current_table['ENGINE'] = 'InnoDB'; + list($current_table, $formatted_size, $unit, $sum_size) + = $method->invokeArgs($ctrl, array($current_table, 10)); + $this->assertEquals( + true, + $current_table['COUNTED'] + ); + $this->assertEquals( + 10, + $sum_size + ); + + $current_table['ENGINE'] = 'MYISAM'; + list($current_table, $formatted_size, $unit, $sum_size) + = $method->invokeArgs($ctrl, array($current_table, 10)); + $this->assertEquals( + false, + $current_table['COUNTED'] + ); + $this->assertEquals( + 10, + $sum_size + ); + } + + /** + * Tests for the getValuesForAriaTable() + * + * @return void + * @test + */ + public function testGetValuesForAriaTable() + { + $class = new ReflectionClass('PMA\Controllers\DatabaseStructureController'); + $method = $class->getMethod('getValuesForAriaTable'); + $method->setAccessible(true); + + $db_is_system_schema = true; + $is_show_stats = true; + $ctrl = new DatabaseStructureController(null, null, null, $db_is_system_schema, + null, null, $is_show_stats); + + $current_table = array( + 'Data_length' => 16384, + 'Index_length' => 0, + 'Name' => 'table', + 'Data_free' => 300 + ); + list($current_table, $formatted_size, $unit, $formatted_overhead, + $overhead_unit, $overhead_size, $sum_size + ) = $method->invokeArgs($ctrl, array( + $current_table, 0, 0, 0, 0, + 0, 0 + )); + $this->assertEquals( + 6, + $current_table['Rows'] + ); + $this->assertEquals( + doubleval(16384), + $sum_size + ); + $this->assertEquals( + 300, + $overhead_size + ); + + unset($current_table['Data_free']); + list($current_table, $formatted_size, $unit, $formatted_overhead, + $overhead_unit, $overhead_size, $sum_size + ) = $method->invokeArgs($ctrl, array( + $current_table, 0, 0, 0, 0, + 0, 0 + )); + $this->assertEquals( + 0, + $overhead_size + ); + + $is_show_stats = false; + $ctrl = new DatabaseStructureController(null, null, null, $db_is_system_schema, + null, null, $is_show_stats); + list($current_table, $formatted_size, $unit, $formatted_overhead, + $overhead_unit, $overhead_size, $sum_size + ) = $method->invokeArgs($ctrl, array( + $current_table, 0, 0, 0, 0, + 0, 0 + )); + $this->assertEquals( + 0, + $sum_size + ); + + $db_is_system_schema = false; + $ctrl = new DatabaseStructureController(null, null, null, $db_is_system_schema, + null, null, $is_show_stats); + list($current_table, $formatted_size, $unit, $formatted_overhead, + $overhead_unit, $overhead_size, $sum_size + ) = $method->invokeArgs($ctrl, array( + $current_table, 0, 0, 0, 0, + 0, 0 + )); + $this->assertArrayNotHasKey( + 'Row', + $current_table + ); + } + + /** + * Tests for hasTable() + * + * @return void + * @test + */ + public function testHasTable() + { + $class = new ReflectionClass('PMA\Controllers\DatabaseStructureController'); + $method = $class->getMethod('hasTable'); + $method->setAccessible(true); + + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, null); + + // When parameter $db is empty + $this->assertEquals( + false, + $method->invokeArgs($ctrl, array(array(), 'table')) + ); + + // Correct parameter + $tables = array( + 'db.table' + ); + $this->assertEquals( + true, + $method->invokeArgs($ctrl, array($tables, 'table')) + ); + + // Table not in database + $tables = array( + 'db.tab1e' + ); + $this->assertEquals( + false, + $method->invokeArgs($ctrl, array($tables, 'table')) + ); + } + + /** + * Tests for checkFavoriteTable() + * + * @return void + * @test + */ + public function testCheckFavoriteTable() + { + $class = new ReflectionClass('PMA\Controllers\DatabaseStructureController'); + $method = $class->getMethod('checkFavoriteTable'); + $method->setAccessible(true); + + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, null); + + $_SESSION['tmpval']['favorite_tables'][$GLOBALS['server']] = array( + array('db' => 'db', 'table' => 'table') + ); + + $this->assertEquals( + false, + $method->invokeArgs($ctrl, array('')) + ); + + $this->assertEquals( + true, + $method->invokeArgs($ctrl, array('table')) + ); + } + + /** + * Tests for synchronizeFavoriteTables() + * + * @return void + * @test + */ + public function testSynchronizeFavoriteTables() + { + $fav_instance = $this->getMockBuilder('PMA_RecentFavoriteTable') + ->disableOriginalConstructor() + ->getMock(); + $fav_instance->expects($this->at(1))->method('getTables') + ->will($this->returnValue(array())); + $fav_instance->expects($this->at(2))->method('getTables') + ->will($this->returnValue( + array( + array('db' => 'db', 'table' => 'table') + ) + )); + + $class = new ReflectionClass('PMA\Controllers\DatabaseStructureController'); + $method = $class->getMethod('synchronizeFavoriteTables'); + $method->setAccessible(true); + + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, null); + + // The user hash for test + $user = 'abcdefg'; + $favorite_table[$user] = array( + array('db' => 'db', 'table' => 'table') + ); + + $method->invokeArgs($ctrl, array($fav_instance, $user, $favorite_table)); + $json = $this->response->getJSONResult(); + + $this->assertEquals(json_encode($favorite_table), $json['favorite_tables']); + $this->assertArrayHasKey('list', $json); + } + + /** + * Tests for handleRealRowCountRequestAction() + * + * @return void + * @test + */ + public function testHandleRealRowCountRequestAction() + { + $_REQUEST['table'] = 'table'; + + $ctrl = new DatabaseStructureController(null, null, null, null, + null, null, null); + + $ctrl->handleRealRowCountRequestAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + 6, + $json['real_row_count'] + ); + + // Fall into another branch + $_REQUEST['real_row_count_all'] = 'abc'; + $GLOBALS['tables'] = array( + array( + 'TABLE_NAME' => 'table' + ) + ); + $ctrl->handleRealRowCountRequestAction(); + $json = $this->response->getJSONResult(); + + $expected_result = array( + array( + 'table' => 'table', + 'row_count' => 6 + ) + ); + $this->assertEquals( + json_encode($expected_result), + $json['real_row_count_all'] + ); + } +} diff --git a/test/libraries/PMA_TableIndexesController_test.php b/test/classes/controllers/TableIndexesController_test.php similarity index 100% rename from test/libraries/PMA_TableIndexesController_test.php rename to test/classes/controllers/TableIndexesController_test.php diff --git a/test/classes/PMA_TableSearchController_test.php b/test/classes/controllers/TableSearchController_test.php similarity index 100% rename from test/classes/PMA_TableSearchController_test.php rename to test/classes/controllers/TableSearchController_test.php diff --git a/test/libraries/PMA_structure_test.php b/test/libraries/PMA_structure_test.php deleted file mode 100644 index 5eebe04096..0000000000 --- a/test/libraries/PMA_structure_test.php +++ /dev/null @@ -1,279 +0,0 @@ -markTestIncomplete('Not Implemented Yet!'); - /** - * $current_table = array( - * 'TABLE_ROWS' => 3, - * 'TABLE_NAME' => 'name1', - * 'TABLE_COMMENT' => 'This is a test comment' - * ); - * $table_is_view = false; - * $tbl_url_query = 'tbl_url_query'; - * $titles = array( - * 'Browse' => 'Browse1', - * 'NoBrowse' => 'NoBrowse1', - * 'Search' => 'Search1', - * 'NoSearch' => 'NoSearch1', - * 'Empty' => 'Empty1', - * 'NoEmpty' => 'NoEmpty1', - * );; - * $truename = 'truename'; - * $db_is_system_schema = null; - * $url_query = 'url_query'; - * - * //$table_is_view = true; - * list( - * $browse_table, $search_table,$browse_table_label, - * $empty_table, $tracking_icon - * ) = PMA_getHtmlForActionLinks( - * $current_table, $table_is_view, $tbl_url_query, - * $titles, $truename, $db_is_system_schema, $url_query - * ); - * - * //$browse_table - * $this->assertContains( - * $titles['Browse'], - * $browse_table - * ); - * - * //$search_table - * $this->assertContains( - * $titles['Search'], - * $search_table - * ); - * $this->assertContains( - * $tbl_url_query, - * $search_table - * ); - * - * //$browse_table_label - * $this->assertContains( - * $tbl_url_query, - * $browse_table_label - * ); - * - * //$empty_table - * $this->assertContains( - * $tbl_url_query, - * $empty_table - * ); - * $this->assertContains( - * urlencode( - * 'TRUNCATE ' . PMA_Util::backquote($current_table['TABLE_NAME']) - * ), - * $empty_table - * ); - * $this->assertContains( - * $titles['Empty'], - * $empty_table - * ); - * - * //$table_is_view = false; - * $current_table = array( - * 'TABLE_ROWS' => 0, - * 'TABLE_NAME' => 'name1', - * 'TABLE_COMMENT' => 'This is a test comment' - * ); - * $table_is_view = false; - * list( - * $browse_table, $search_table,$browse_table_label, - * $empty_table, $tracking_icon - * ) = PMA_getHtmlForActionLinks( - * $current_table, $table_is_view, $tbl_url_query, - * $titles, $truename, $db_is_system_schema, $url_query - * ); - * - * //$browse_table - * $this->assertContains( - * $titles['NoBrowse'], - * $browse_table - * ); - * - * //$search_table - * $this->assertContains( - * $titles['NoSearch'], - * $search_table - * ); - * - * //$browse_table_label - * $this->assertContains( - * $tbl_url_query, - * $browse_table_label - * ); - * $this->assertContains( - * $titles['NoEmpty'], - * $empty_table - * ); - */ - } - - /** - * Test for PMA_getTableDropQueryAndMessage - * - * @return void - */ - public function testPMAGetTableDropQueryAndMessage() - { - $this->markTestIncomplete('Not Implemented Yet!'); - /* - $current_table = array( - 'TABLE_ROWS' => 3, - 'TABLE_NAME' => 'name1', - 'ENGINE' => 'ENGINE1', - ); - $table_is_view = false; - - list($drop_query, $drop_message) = PMA_getTableDropQueryAndMessage( - $table_is_view, $current_table - ); - - //$drop_query - $ret = "DROP TABLE `name1`"; - $this->assertEquals( - $ret, - $drop_query - ); - - //$drop_message - $ret = "Table name1 has been dropped."; - $this->assertEquals( - $ret, - $drop_message - ); - */ - } - - /** - * Test for PMA_getHtmlShowCreate - * - * @return void - */ - public function testPMAGetHtmlShowCreate() - { - //@todo: Find out a better way to test front-end pages - $this->markTestIncomplete('Not Implemented Yet!'); - /*mock DBI - $dbi = $this->getMockBuilder('PMA_DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $db = 'PMA'; - $table = 'PMA_Table'; - $is_view_query = "SELECT TABLE_NAME - FROM information_schema.VIEWS - WHERE TABLE_SCHEMA = '" . PMA_Util::sqlAddSlashes($db) . "' - AND TABLE_NAME = '" . PMA_Util::sqlAddSlashes($table) . "'"; - - $show_create_query = 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' - . PMA_Util::backquote($table); - $expected_result = 'CREATE TABLE `PMA_Table` ( ' - . '`id` numeric ' - . ') ENGINE=InnoDB DEFAULT CHARSET=latin1'; - - $dbi->expects($this->any()) - ->method('fetchResult') - ->with($is_view_query) - ->will($this->returnValue(false)); - - $dbi->expects($this->any()) - ->method('fetchValue') - ->with($show_create_query, 0, 1) - ->will( - $this->returnValue( - $expected_result - ) - ); - - $GLOBALS['dbi'] = $dbi; - - $output = PMA_getHtmlShowCreate($db, array($table)); - - $this->assertContains( - 'Showing create queries', - $output - ); - - $this->assertContains( - 'Tables', - $output - ); - - $this->assertContains( - '', - $output - ); - - $this->assertContains( - PMA_mimeDefaultFunction($expected_result), - $output - ); - */ - } -} From fffcb7bc8b92036f34c32f6377bf8cc84e16f061 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 18 Aug 2015 02:35:18 +0800 Subject: [PATCH 6/8] Add tests for TableStructureController and TableRelationController Signed-off-by: Jason --- .../TableRelationController.class.php | 3 +- .../TableStructureController.class.php | 2 +- tbl_relation.php | 1 + .../TableRelationController_test.php | 338 ++++++++++++++++++ .../TableSearchController_test.php | 196 ---------- .../TableStructureController_test.php | 253 +++++++++++++ test/libraries/PMA_tbl_relation_test.php | 90 ----- 7 files changed, 594 insertions(+), 289 deletions(-) create mode 100644 test/classes/controllers/TableRelationController_test.php create mode 100644 test/classes/controllers/TableStructureController_test.php delete mode 100644 test/libraries/PMA_tbl_relation_test.php diff --git a/libraries/controllers/TableRelationController.class.php b/libraries/controllers/TableRelationController.class.php index 0d7b4ef34c..58705bae62 100644 --- a/libraries/controllers/TableRelationController.class.php +++ b/libraries/controllers/TableRelationController.class.php @@ -8,7 +8,6 @@ namespace PMA\Controllers\Table; -require_once 'libraries/common.inc.php'; require_once 'libraries/DatabaseInterface.class.php'; require_once 'libraries/controllers/TableController.class.php'; require_once 'libraries/index.lib.php'; @@ -302,7 +301,7 @@ class TableRelationController extends TableController public function getDropdownValueForTableAction() { $foreignTable = $_REQUEST['foreignTable']; - $table_obj = new PMA_Table($foreignTable, $_REQUEST['foreignDb']); + $table_obj = $this->dbi->getTable($_REQUEST['foreignDb'], $foreignTable); // Since views do not have keys defined on them provide the full list of // columns if ($table_obj->isView()) { diff --git a/libraries/controllers/TableStructureController.class.php b/libraries/controllers/TableStructureController.class.php index d61cb4cccd..c1832cd289 100644 --- a/libraries/controllers/TableStructureController.class.php +++ b/libraries/controllers/TableStructureController.class.php @@ -719,7 +719,7 @@ class TableStructureController extends TableController if ($result !== false) { $changed_privileges = $this->adjustColumnPrivileges( - $this->db, $this->table, $adjust_privileges + $adjust_privileges ); if ($changed_privileges) { diff --git a/tbl_relation.php b/tbl_relation.php index 7005611c03..1c05c72441 100644 --- a/tbl_relation.php +++ b/tbl_relation.php @@ -23,6 +23,7 @@ use PMA_Response; use PMA_Table; use PMA_Util; +require_once 'libraries/common.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/controllers/TableRelationController.class.php'; require_once 'libraries/Response.class.php'; diff --git a/test/classes/controllers/TableRelationController_test.php b/test/classes/controllers/TableRelationController_test.php new file mode 100644 index 0000000000..72f47dfd5a --- /dev/null +++ b/test/classes/controllers/TableRelationController_test.php @@ -0,0 +1,338 @@ +databases = new DataBaseMockForTblRelation(); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $GLOBALS['dbi'] = $dbi; + + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $this->response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $this->response); + $container->alias('response', 'PMA_Response'); + } + + /** + * Tests for getDropdownValueForTableAction() + * + * Case one: this case is for the situation when the target + * table is a view. + * + * @return void + * @test + */ + public function testGetDropdownValueForTableActionIsView() + { + $viewColumns = array( + 'viewCol', 'viewCol2', 'viewCol3' + ); + $tableMock = $this->getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + // Test the situation when the table is a view + $tableMock->expects($this->any())->method('isView') + ->will($this->returnValue(true)); + $tableMock->expects($this->any())->method('getColumns') + ->will($this->returnValue($viewColumns)); + + $GLOBALS['dbi']->expects($this->any())->method('getTable') + ->will($this->returnValue($tableMock)); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableRelationController'); + $container->alias( + 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' + ); + /** + * @var PMA\Controllers\Table\TableRelationController + */ + $ctrl = $container->get('TableRelationController'); + + $ctrl->getDropdownValueForTableAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + $viewColumns, + $json['columns'] + ); + } + + /** + * Tests for getDropdownValueForTableAction() + * + * Case one: this case is for the situation when the target + * table is not a view (real tabletable). + * + * @return void + * @test + */ + public function testGetDropdownValueForTableActionNotView() + { + $indexedColumns = array( + 'primaryTableCol' + ); + $tableMock = $this->getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + // Test the situation when the table is a view + $tableMock->expects($this->any())->method('isView') + ->will($this->returnValue(false)); + $tableMock->expects($this->any())->method('getIndexedColumns') + ->will($this->returnValue($indexedColumns)); + + $GLOBALS['dbi']->expects($this->any())->method('getTable') + ->will($this->returnValue($tableMock)); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableRelationController'); + $container->alias( + 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' + ); + $ctrl = $container->get('TableRelationController'); + + $ctrl->getDropdownValueForTableAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + $indexedColumns, + $json['columns'] + ); + } + + /** + * Tests for getDropdownValueForDbAction() + * + * Case one: foreign and not Drizzle + * + * @return void + * @test + */ + public function testGetDropdownValueForDbActionOne() + { + $GLOBALS['dbi']->expects($this->any())->method('fetchArray') + ->will($this->returnCallback( + function () { + static $count = 0; + if ($count == 0) { + $count++; + return array( + 'Engine' => 'InnoDB', + 'Name' => 'table' + ); + } else { + return null; + } + } + )); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableRelationController'); + $container->alias( + 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' + ); + $ctrl = $container->get('TableRelationController', array( + 'tbl_storage_engine' => 'INNODB' + ) + ); + + $_REQUEST['foreign'] = 'true'; + $ctrl->getDropdownValueForDbAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + array('table'), + $json['tables'] + ); + } + + /** + * Tests for getDropdownValueForDbAction() + * + * Case two: not foreign and not Drizzle + * + * @return void + * @test + */ + public function testGetDropdownValueForDbActionTwo() + { + $GLOBALS['dbi']->expects($this->any())->method('fetchArray') + ->will($this->returnCallback( + function () { + static $count = 0; + if ($count == 0) { + $count++; + return array('table'); + } else { + return null; + } + } + )); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableRelationController'); + $container->alias( + 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' + ); + $ctrl = $container->get('TableRelationController', array( + 'tbl_storage_engine' => 'INNODB' + ) + ); + + $_REQUEST['foreign'] = 'false'; + $ctrl->getDropdownValueForDbAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + array('table'), + $json['tables'] + ); + } + + /** + * Tests for getDropdownValueForDbAction() + * + * Case three: foreign and Drizzle + * + * @return void + * @test + */ + public function testGetDropdownValueForDbActionThree() + { + if (! PMA_HAS_RUNKIT) { + $this->markTestSkipped("Cannot redefine PMA_DRIZZLE constant"); + } + runkit_constant_redefine('PMA_DRIZZLE', true); + + $tableMock = $this->getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + + $statusInfo = array( + 'Engine' => 'InnoDB' + ); + $tableMock->expects($this->any())->method('getStatusInfo') + ->will($this->returnValue($statusInfo)); + + $GLOBALS['dbi']->expects($this->any())->method('getTable') + ->will($this->returnValue($tableMock)); + $GLOBALS['dbi']->expects($this->any())->method('fetchArray') + ->will($this->returnCallback( + function () { + static $count = 0; + if ($count == 0) { + $count++; + return array('table'); + } else { + return null; + } + } + )); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableRelationController'); + $container->alias( + 'TableRelationController', 'PMA\Controllers\Table\TableRelationController' + ); + $ctrl = $container->get('TableRelationController', array( + 'tbl_storage_engine' => 'INNODB' + ) + ); + + $_REQUEST['foreign'] = 'true'; + $ctrl->getDropdownValueForDbAction(); + $json = $this->response->getJSONResult(); + $this->assertEquals( + array('table'), + $json['tables'] + ); + } +} + +/** + * Mock class for DataBasePMAMock + * + * @package PhpMyAdmin-test + */ +Class DataBasePMAMockForTblRelation +{ + var $databases; +} + +/** + * Mock class for DataBaseMock + * + * @package PhpMyAdmin-test + */ +Class DataBaseMockForTblRelation +{ + /** + * mock function to return table is existed + * + * @param string $name table name + * + * @return bool + */ + function exists($name) + { + return true; + } +} diff --git a/test/classes/controllers/TableSearchController_test.php b/test/classes/controllers/TableSearchController_test.php index d858d8dfee..079d6b1759 100644 --- a/test/classes/controllers/TableSearchController_test.php +++ b/test/classes/controllers/TableSearchController_test.php @@ -119,106 +119,6 @@ class PMA_TableSearchController_Test extends PHPUnit_Framework_TestCase } - /** - * Test for getSelectionForm - * - * @return void - * @group medium - */ - public function testGetSelectionForm() - { - /*//$this->_searchType == 'zoom' - $tableSearch = new PMA_TableSearch("PMA", "PMA_BookMark", "zoom"); - $url_goto = "http://phpmyadmin.net"; - $form = $tableSearch->getSelectionForm($url_goto); - $this->assertContains( - '', - $html - ); - $this->assertContains( - '', - $html - ); - $this->assertContains( - '', - $html - );*/ - } - /** * Test for buildSqlQuery * diff --git a/test/classes/controllers/TableStructureController_test.php b/test/classes/controllers/TableStructureController_test.php new file mode 100644 index 0000000000..671da4ab01 --- /dev/null +++ b/test/classes/controllers/TableStructureController_test.php @@ -0,0 +1,253 @@ +getMockBuilder('PMA_Table') + ->disableOriginalConstructor() + ->getMock(); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $dbi->expects($this->any())->method('getTable') + ->will($this->returnValue($table)); + + $GLOBALS['dbi'] = $dbi; + + $container = Container::getDefaultContainer(); + $container->set('db', 'db'); + $container->set('table', 'table'); + $container->set('dbi', $GLOBALS['dbi']); + $this->response = new \PMA\Test\Stubs\PMA_Response(); + $container->set('PMA_Response', $this->response); + $container->alias('response', 'PMA_Response'); + } + + /** + * Tests for getKeyForTablePrimary() + * + * Case one: there are no primary key in the table + * + * @return void + * @test + */ + public function testGetKeyForTablePrimaryOne() + { + $GLOBALS['dbi']->expects($this->any())->method('fetchAssoc') + ->will($this->returnValue(null)); + + $class = new ReflectionClass('\PMA\Controllers\TableStructureController'); + $method = $class->getMethod('getKeyForTablePrimary'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + // No primary key in db.table2 + $this->assertEquals( + '', + $method->invoke($ctrl) + ); + } + + /** + * Tests for getKeyForTablePrimary() + * + * Case two: there are a primary key in the table + * + * @return void + * @test + */ + public function testGetKeyForTablePrimaryTwo() + { + $GLOBALS['dbi']->expects($this->any())->method('fetchAssoc') + ->will($this->returnCallback( + function () { + static $callCount = 0; + if ($callCount == 0) { + $callCount++; + return array( + 'Key_name' => 'PRIMARY', + 'Column_name' => 'column' + ); + } else { + return null; + } + } + )); + + $class = new ReflectionClass('\PMA\Controllers\TableStructureController'); + $method = $class->getMethod('getKeyForTablePrimary'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + // With db.table, it has a primary key `column` + $this->assertEquals( + 'column, ', + $method->invoke($ctrl) + ); + } + + /** + * Tests for adjustColumnPrivileges() + * + * @return void + * @test + */ + public function testAdjustColumnPrivileges() + { + $class = new ReflectionClass('\PMA\Controllers\TableStructureController'); + $method = $class->getMethod('adjustColumnPrivileges'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + + $this->assertEquals( + false, + $method->invokeArgs($ctrl, array(null)) + ); + + $adjust_privileges = array( + 'col1' => 'col2' + ); + $GLOBALS['col_priv'] = true; + $GLOBALS['flush_priv'] = true; + $this->assertEquals( + true, + $method->invokeArgs($ctrl, array($adjust_privileges)) + ); + } + + /** + * Tests for getMultipleFieldCommandType() + * + * @return void + * @test + */ + public function testGetMultipleFieldCommandType() + { + $class = new ReflectionClass('\PMA\Controllers\TableStructureController'); + $method = $class->getMethod('getMultipleFieldCommandType'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + + $this->assertEquals( + null, + $method->invoke($ctrl) + ); + + $_REQUEST['submit_mult_drop_x'] = true; + $this->assertEquals( + 'drop', + $method->invoke($ctrl) + ); + unset($_REQUEST['submit_mult_drop_x']); + + $_REQUEST['submit_mult'] = 'create'; + $this->assertEquals( + 'create', + $method->invoke($ctrl) + ); + unset($_REQUEST['submit_mult']); + + $_REQUEST['mult_btn'] = __('Yes'); + $this->assertEquals( + 'row_delete', + $method->invoke($ctrl) + ); + + $_REQUEST['selected'] = array('a', 'b'); + $method->invoke($ctrl); + $this->assertEquals( + $_REQUEST['selected'], + $_REQUEST['selected_fld'] + ); + } +} diff --git a/test/libraries/PMA_tbl_relation_test.php b/test/libraries/PMA_tbl_relation_test.php deleted file mode 100644 index 2210902657..0000000000 --- a/test/libraries/PMA_tbl_relation_test.php +++ /dev/null @@ -1,90 +0,0 @@ -databases = new DataBaseMockForTblRelation(); - - $dbi = $this->getMockBuilder('PMA_DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $GLOBALS['dbi'] = $dbi; - } - - /** - * Tests for PMA_getHtmlForCommonForm() method. - * - * @return void - * @test - */ - public function testPMAGetHtmlForCommonForm() - { - // @todo Find out a better method to test for HTML - } -} - -/** - * Mock class for DataBasePMAMock - * - * @package PhpMyAdmin-test - */ -Class DataBasePMAMockForTblRelation -{ - var $databases; -} - -/** - * Mock class for DataBaseMock - * - * @package PhpMyAdmin-test - */ -Class DataBaseMockForTblRelation -{ - /** - * mock function to return table is existed - * - * @param string $name table name - * - * @return bool - */ - function exists($name) - { - return true; - } -} From fae4d6ce142fd4a0183e85ee8710e0d40cca1db5 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 18 Aug 2015 14:21:06 +0800 Subject: [PATCH 7/8] Fix failures in TableStructureController_Test Signed-off-by: Jason --- .../TableRelationController_test.php | 4 +-- .../TableStructureController_test.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/test/classes/controllers/TableRelationController_test.php b/test/classes/controllers/TableRelationController_test.php index 72f47dfd5a..091c531599 100644 --- a/test/classes/controllers/TableRelationController_test.php +++ b/test/classes/controllers/TableRelationController_test.php @@ -265,9 +265,7 @@ class TableRelationController_Test extends PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); - $statusInfo = array( - 'Engine' => 'InnoDB' - ); + $statusInfo = 'InnoDB'; $tableMock->expects($this->any())->method('getStatusInfo') ->will($this->returnValue($statusInfo)); diff --git a/test/classes/controllers/TableStructureController_test.php b/test/classes/controllers/TableStructureController_test.php index 671da4ab01..895a517644 100644 --- a/test/classes/controllers/TableStructureController_test.php +++ b/test/classes/controllers/TableStructureController_test.php @@ -186,7 +186,39 @@ class TableStructureController_Test extends PHPUnit_Framework_TestCase false, $method->invokeArgs($ctrl, array(null)) ); + } + /** + * Tests for adjustColumnPrivileges() + * For the Drizzle environment + * + * @return void + * @test + */ + public function testAdjustColumnPrivilegesDrizzle() + { + if (! PMA_HAS_RUNKIT) { + $this->markTestSkipped("Cannot redefine PMA_DRIZZLE constant"); + } + + $class = new ReflectionClass('\PMA\Controllers\TableStructureController'); + $method = $class->getMethod('adjustColumnPrivileges'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + + $this->assertEquals( + false, + $method->invokeArgs($ctrl, array(null)) + ); + + runkit_constant_redefine('PMA_DRIZZLE', false); $adjust_privileges = array( 'col1' => 'col2' ); From d6577f5d1a060895989c5cdaafcb0c93bd51c6a8 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 20 Aug 2015 01:20:06 +0800 Subject: [PATCH 8/8] Add tests for TableSearchController, TableStructureController. - Also fix a missing include in operations.lib.php Signed-off-by: Jason --- .../TableChartController.class.php | 1 - .../TableSearchController.class.php | 4 + libraries/operations.lib.php | 2 + tbl_chart.php | 1 + .../TableIndexesController_test.php | 2 +- .../TableSearchController_test.php | 162 +++++- .../TableStructureController_test.php | 108 ++++ test/libraries/PMA_central_columns_test.php | 10 - test/libraries/PMA_mult_submits_test.php | 94 ---- .../PMA_tbl_columns_definition_form_test.php | 505 ------------------ 10 files changed, 272 insertions(+), 617 deletions(-) delete mode 100644 test/libraries/PMA_tbl_columns_definition_form_test.php diff --git a/libraries/controllers/TableChartController.class.php b/libraries/controllers/TableChartController.class.php index 92dc43c3da..2457dcb1a9 100644 --- a/libraries/controllers/TableChartController.class.php +++ b/libraries/controllers/TableChartController.class.php @@ -15,7 +15,6 @@ use PMA_Message; use PMA\Template; use PMA\Controllers\TableController; -require_once 'libraries/common.inc.php'; require_once 'libraries/Util.class.php'; require_once 'libraries/Message.class.php'; require_once 'libraries/Template.class.php'; diff --git a/libraries/controllers/TableSearchController.class.php b/libraries/controllers/TableSearchController.class.php index d6c22e34e6..25c31557e9 100644 --- a/libraries/controllers/TableSearchController.class.php +++ b/libraries/controllers/TableSearchController.class.php @@ -171,6 +171,10 @@ class TableSearchController extends TableController */ public function indexAction() { + $_REQUEST['db'] = 'TEST'; + $_REQUEST['table'] = 'fuck2'; + $_REQUEST['where_clause'] = '`c` = 2'; + $this->getDataRowAction(); switch ($this->_searchType) { case 'replace': if (isset($_POST['find'])) { diff --git a/libraries/operations.lib.php b/libraries/operations.lib.php index 5f92e6030c..2a2ac0725f 100644 --- a/libraries/operations.lib.php +++ b/libraries/operations.lib.php @@ -11,6 +11,8 @@ if (! defined('PHPMYADMIN')) { exit; } +require_once 'libraries/Partition.class.php'; + /** * Get HTML output for database comment * diff --git a/tbl_chart.php b/tbl_chart.php index a77d63581a..a435b5e485 100644 --- a/tbl_chart.php +++ b/tbl_chart.php @@ -10,6 +10,7 @@ namespace PMA; use PMA_Response; +require_once 'libraries/common.inc.php'; require_once 'libraries/di/Container.class.php'; require_once 'libraries/Response.class.php'; require_once 'libraries/controllers/TableChartController.class.php'; diff --git a/test/classes/controllers/TableIndexesController_test.php b/test/classes/controllers/TableIndexesController_test.php index 6d00b3ab7d..8632384fbd 100644 --- a/test/classes/controllers/TableIndexesController_test.php +++ b/test/classes/controllers/TableIndexesController_test.php @@ -1,7 +1,7 @@ response = new PMA\Test\Stubs\PMA_Response(); + $container = Container::getDefaultContainer(); $container->set('db', 'PMA'); $container->set('table', 'PMA_BookMark'); $container->set('dbi', $GLOBALS['dbi']); + $container->set('response', $this->response); + $container->set('searchType', 'replace'); } /** @@ -238,4 +241,151 @@ class PMA_TableSearchController_Test extends PHPUnit_Framework_TestCase $sql ); } + + /** + * Tests for getColumnMinMax() + * + * @return void + * @test + */ + public function testGetColumnMinMax() + { + $GLOBALS['dbi']->expects($this->any())->method('fetchSingleRow') + ->will($this->returnArgument(0)); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('PMA\Controllers\Table\TableSearchController'); + $container->alias( + 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' + ); + $ctrl = $container->get('TableSearchController'); + + $result = $ctrl->getColumnMinMax('column'); + $expected = 'SELECT MIN(`column`) AS `min`, ' + . 'MAX(`column`) AS `max` ' + . 'FROM `PMA`.`PMA_BookMark`'; + $this->assertEquals( + $expected, + $result + ); + } + + /** + * Tests for _generateWhereClause() + * + * @return void + * @test + */ + public function testGenerateWhereClause() + { + $types = $this->getMockBuilder('PMA_Types') + ->disableOriginalConstructor() + ->getMock(); + $types->expects($this->any())->method('isUnaryOperator') + ->will($this->returnValue(false)); + $GLOBALS['PMA_Types'] = $types; + + $class = new ReflectionClass('\PMA\Controllers\Table\TableSearchController'); + $method = $class->getMethod('_generateWhereClause'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->factory('\PMA\Controllers\Table\TableSearchController'); + $container->alias( + 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' + ); + $ctrl = $container->get('TableSearchController'); + + $_POST['customWhereClause'] = '`table` = \'PMA_BookMark\''; + $result = $method->invoke($ctrl); + $this->assertEquals( + ' WHERE `table` = \'PMA_BookMark\'', + $result + ); + + unset($_POST['customWhereClause']); + $this->assertEquals( + '', + $method->invoke($ctrl) + ); + + $_POST['criteriaColumnNames'] = array( + 'b', 'a' + ); + $_POST['criteriaColumnOperators'] = array( + '<=', '=' + ); + $_POST['criteriaValues'] = array( + '10', '2' + ); + $_POST['criteriaColumnTypes'] = array( + 'int(11)', 'int(11)' + ); + $result = $method->invoke($ctrl); + $this->assertEquals( + ' WHERE `b` <= 10 AND `a` = 2', + $result + ); + } + + /** + * Tests for getDataRowAction() + * + * @return void + * @test + */ + public function testGetDataRowAction() + { + $meta_one = new stdClass(); + $meta_one->type = 'int'; + $meta_one->length = 11; + $meta_two = new stdClass(); + $meta_two->length = 11; + $meta_two->type = 'int'; + $fields_meta = array( + $meta_one, $meta_two + ); + $GLOBALS['dbi']->expects($this->any())->method('getFieldsMeta') + ->will($this->returnValue($fields_meta)); + + $GLOBALS['dbi']->expects($this->any())->method('fetchAssoc') + ->will($this->returnCallback( + function () { + static $count = 0; + if ($count == 0) { + $count++; + return array( + 'col1' => 1, + 'col2' => 2 + ); + } else { + return null; + } + } + )); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $GLOBALS['dbi']); + $container->factory('\PMA\Controllers\Table\TableSearchController'); + $container->alias( + 'TableSearchController', 'PMA\Controllers\Table\TableSearchController' + ); + $ctrl = $container->get('TableSearchController'); + + $_REQUEST['db'] = 'PMA'; + $_REQUEST['table'] = 'PMA_BookMark'; + $_REQUEST['where_clause'] = '`col1` = 1'; + $expected = array( + 'col1' => 1, + 'col2' => 2 + ); + $ctrl->getDataRowAction(); + + $json = $this->response->getJSONResult(); + $this->assertEquals( + $expected, + $json['row_info'] + ); + } } diff --git a/test/classes/controllers/TableStructureController_test.php b/test/classes/controllers/TableStructureController_test.php index 895a517644..f4c4e511d7 100644 --- a/test/classes/controllers/TableStructureController_test.php +++ b/test/classes/controllers/TableStructureController_test.php @@ -282,4 +282,112 @@ class TableStructureController_Test extends PHPUnit_Framework_TestCase $_REQUEST['selected_fld'] ); } + + /** + * Test for getDataForSubmitMult() + * + * @return void + * @test + */ + public function testPMAGetDataForSubmitMult() + { + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $dbi->expects($this->any()) + ->method('query') + ->will($this->returnValue(true)); + + $class = new ReflectionClass('PMA\Controllers\TableStructureController'); + $method = $class->getMethod('getDataForSubmitMult'); + $method->setAccessible(true); + + $container = Container::getDefaultContainer(); + $container->set('dbi', $dbi); + $container->factory('PMA\Controllers\TableStructureController'); + $container->alias( + 'TableStructureController', 'PMA\Controllers\TableStructureController' + ); + $ctrl = $container->get('TableStructureController'); + + $submit_mult = "index"; + $db = "PMA_db"; + $table = "PMA_table"; + $selected = array( + "table1", "table2" + ); + $action = 'db_delete_row'; + + list( + $what, $query_type, $is_unset_submit_mult, $mult_btn, $centralColsError + ) + = $method->invokeArgs($ctrl, array($submit_mult, $db, $table, $selected, $action)); + + //validate 1: $what + $this->assertEquals( + null, + $what + ); + + //validate 2: $query_type + $this->assertEquals( + 'index_fld', + $query_type + ); + + //validate 3: $is_unset_submit_mult + $this->assertEquals( + true, + $is_unset_submit_mult + ); + + //validate 4: + $this->assertEquals( + __('Yes'), + $mult_btn + ); + + //validate 5: $centralColsError + $this->assertEquals( + null, + $centralColsError + ); + + $submit_mult = "unique"; + + list( + $what, $query_type, $is_unset_submit_mult, $mult_btn, $centralColsError + ) + = $method->invokeArgs($ctrl, array($submit_mult, $db, $table, $selected, $action)); + + //validate 1: $what + $this->assertEquals( + null, + $what + ); + + //validate 2: $query_type + $this->assertEquals( + 'unique_fld', + $query_type + ); + + //validate 3: $is_unset_submit_mult + $this->assertEquals( + true, + $is_unset_submit_mult + ); + + //validate 4: $mult_btn + $this->assertEquals( + __('Yes'), + $mult_btn + ); + + //validate 5: $centralColsError + $this->assertEquals( + null, + $centralColsError + ); + } } diff --git a/test/libraries/PMA_central_columns_test.php b/test/libraries/PMA_central_columns_test.php index 250347fa3e..53bd28b92b 100644 --- a/test/libraries/PMA_central_columns_test.php +++ b/test/libraries/PMA_central_columns_test.php @@ -496,16 +496,6 @@ class PMA_Central_Columns_Test extends PHPUnit_Framework_TestCase ); } - /** - * Test for PMA_getHTMLforCentralColumnsTableRow - * - * @return void - */ - public function testPMAGetHTMLforCentralColumnsTableRow() - { - // @todo Find a better way to test page - } - /** * Test for PMA_getCentralColumnsListRaw * diff --git a/test/libraries/PMA_mult_submits_test.php b/test/libraries/PMA_mult_submits_test.php index af898484c5..33c036984d 100644 --- a/test/libraries/PMA_mult_submits_test.php +++ b/test/libraries/PMA_mult_submits_test.php @@ -333,100 +333,6 @@ class PMA_MultSubmits_Test extends PHPUnit_Framework_TestCase ); } - /** - * Test for PMA_getDataForSubmitMult - * - * @return void - */ - public function testPMAGetDataForSubmitMult() - { - $this->markTestIncomplete('Not Implemented Yet!'); - /* todo: now this test to be moved to new StructureController - $submit_mult = "index"; - $db = "PMA_db"; - $table = "PMA_table"; - $selected = array( - "table1", "table2" - ); - $action = 'db_delete_row'; - - list( - $what, $query_type, $is_unset_submit_mult, $mult_btn, $centralColsError - ) - = PMA_getDataForSubmitMult( - $submit_mult, $db, $table, $selected, $action - ); - - //validate 1: $what - $this->assertEquals( - null, - $what - ); - - //validate 2: $query_type - $this->assertEquals( - 'index_fld', - $query_type - ); - - //validate 3: $is_unset_submit_mult - $this->assertEquals( - true, - $is_unset_submit_mult - ); - - //validate 4: - $this->assertEquals( - __('Yes'), - $mult_btn - ); - - //validate 5: $centralColsError - $this->assertEquals( - null, - $centralColsError - ); - - $submit_mult = "unique"; - - list( - $what, $query_type, $is_unset_submit_mult, $mult_btn, $centralColsError - ) - = PMA_getDataForSubmitMult( - $submit_mult, $db, $table, $selected, $action - ); - - //validate 1: $what - $this->assertEquals( - null, - $what - ); - - //validate 2: $query_type - $this->assertEquals( - 'unique_fld', - $query_type - ); - - //validate 3: $is_unset_submit_mult - $this->assertEquals( - true, - $is_unset_submit_mult - ); - - //validate 4: $mult_btn - $this->assertEquals( - __('Yes'), - $mult_btn - ); - - //validate 5: $centralColsError - $this->assertEquals( - null, - $centralColsError - );*/ - } - /** * Test for PMA_getQueryFromSelected * diff --git a/test/libraries/PMA_tbl_columns_definition_form_test.php b/test/libraries/PMA_tbl_columns_definition_form_test.php deleted file mode 100644 index 1ae4511df7..0000000000 --- a/test/libraries/PMA_tbl_columns_definition_form_test.php +++ /dev/null @@ -1,505 +0,0 @@ -assertContains( - * 'Index', - * $result - * ); - * - * $this->assertContains( - * 'Move column', - * $result - * ); - * - * $this->assertContains( - * 'MIME type', - * $result - * ); - */ - } - - /** - * Test for PMA_getHtmlForColumnName - * - * @return void - */ - public function testGetHtmlForColumnName() - { - /** - * @todo Create test for page - */ - } - - /** - * Test for PMA_getHtmlForColumnType - * - * @return void - */ - public function testGetHtmlForColumnType() - { - /** - * @todo Find out a better method to test for HTML - * - * $GLOBALS['PMA_Types'] = new PMA_Types; - * $result = PMA_getHtmlForColumnType( - * 1, 4, 3, false, array('column_status' => array('isReferenced' => false, - * 'isForeignKey' => false, 'isEditable' => true)) - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } - - /** - * Test for PMA_getHtmlForTransformation - * - * @return void - */ - public function testGetHtmlForTransformation() - { - /** - * @todo Find out a better method to test for HTML - * - * $cmeta = array( - * 'Field' => 'fieldname' - * ); - * - * $mime = array( - * 'fieldname' => array( - * 'transformation' => 'Text_Plain_Preappend.class.php', - * 'transformation_options' => 'transops' - * ) - * ); - * - * $avail_mime = array( - * 'transformation' => array( - * 'foo' => 'text/plain: bar' - * ), - * 'transformation_file' => array( - * 'foo' => 'Text_Plain_Preappend.class.php' - * ) - * ); - * $result = PMA_getHtmlForTransformation( - * 2, 0, 0, $avail_mime, $cmeta, $mime, '' - * ); - * - * $this->assertContains( - * '', - * $result - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - - /** - * @todo Find out a better method to test for HTML - * - * $this->assertContains( - * '', - * $result - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - } - - /** - * Test for PMA_getHtmlForColumnComment - * - * @return void - */ - public function testGetHtmlForColumnComment() - { - /** - * @todo Find out a better method to test for HTML - * - * $cmeta = array( - * 'Field' => 'fieldname' - * ); - * - * $commentMeta = array( - * 'fieldname' => 'fieldnamecomment<' - * ); - * - * $result = PMA_getHtmlForColumnComment( - * 2, 1, 0, $cmeta, $commentMeta - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } - - /** - * Test for PMA_getHtmlForColumnAutoIncrement - * - * @return void - */ - public function testGetHtmlForColumnAutoIncrement() - { - /** - * @todo Find out a better method to test for HTML - * - * $cmeta = array( - * 'Extra' => 'auto_increment' - * ); - * - * $result = PMA_getHtmlForColumnAutoIncrement( - * 2, 1, 0, $cmeta - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } - - /** - * Test for PMA_getHtmlForColumnIndexes - * - * @return void - */ - public function testGetHtmlForColumnIndexes() - { - /** - * @todo Find out a better method to test for HTML - * - * $this->assertContains( - * '', - * $result - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } - - /** - * Test for PMA_getHtmlForColumnAttribute - * - * @return void - */ - public function testGetHtmlForColumnAttribute() - { - /** - * @todo Find out a better method to test for HTML - * - * $cmeta = array( - * 'Null' => 'YES', - * 'Extra' => 'on update CURRENT_TIMESTAMP', - * 'Field' => 'f' - * ); - * - * $colspec = array('attribute' => 'attr'); - * - * $analyzed_sql = array( - * array( - * 'create_table_fields' => array( - * 'f' => array( - * 'default_current_timestamp' => true, - * ) - * ) - * ) - * ); - * - * $types = $this->getMockBuilder('PMA_Types') - * ->disableOriginalConstructor() - * ->setMethods(array('getAttributes')) - * ->getMock(); - * - * $types->expects($this->once()) - * ->method('getAttributes') - * ->will( - * $this->returnValue( - * array('on update CURRENT_TIMESTAMP') - * ) - * ); - * - * $GLOBALS['PMA_Types'] = $types; - * $result = PMA_getHtmlForColumnAttribute( - * 2, 3, 1, $colspec, $cmeta, true, $analyzed_sql - * ); - * - * $this->assertContains( - * '', - * $result - * ); - * - * $this->assertContains( - * '

', - * $result - * ); - */ - } - - /** - * Test for PMA_getHtmlForColumnDefault - * - * @return void - */ - public function testGetHtmlForColumnDefault() - { - /** - * @todo Find out a better method to test for HTML - * - * $cmeta = array( - * 'Default' => 'YES', - * 'DefaultType' => 'NONE', - * 'DefaultValue' => '2222' - * ); - * - * $result = PMA_getHtmlForColumnDefault( - * 2, 3, 1, 'TIMESTAMP', true, $cmeta - * ); - * - * $this->assertContains( - * '', - * $result - * ); - */ - $this->markTestIncomplete('Not yet implemented!'); - } - - /** - * Test for PMA_getFormParamsForOldColumn - * - * @return void - */ - public function testGetFormParamsForOldColumn() - { - // Function needs correction - $this->markTestIncomplete('Not yet implemented!'); - } -}

Create Tablevaluereplace_valuecount