From 072d79bd2d92328e71f111632699475d4ce95d59 Mon Sep 17 00:00:00 2001 From: xmujay Date: Sun, 15 Sep 2013 21:17:13 +0800 Subject: [PATCH 1/2] UTs for PMA_server_privileges --- test/libraries/PMA_server_privileges_test.php | 432 +++++++++++++++++- 1 file changed, 431 insertions(+), 1 deletion(-) diff --git a/test/libraries/PMA_server_privileges_test.php b/test/libraries/PMA_server_privileges_test.php index 13642f4340..622312e350 100644 --- a/test/libraries/PMA_server_privileges_test.php +++ b/test/libraries/PMA_server_privileges_test.php @@ -65,12 +65,14 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $GLOBALS['cfg']['DefaultTabTable'] = "db_structure.php"; $GLOBALS['cfg']['NavigationTreeDefaultTabTable'] = "db_structure.php"; $GLOBALS['cfg']['Confirm'] = "Confirm"; + $GLOBALS['cfg']['ShowHint'] = true; $GLOBALS['table'] = "table"; $GLOBALS['PMA_PHP_SELF'] = PMA_getenv('PHP_SELF'); $GLOBALS['pmaThemeImage'] = 'image'; $GLOBALS['server'] = 1; - $GLOBALS['username'] = "pma_username"; + $GLOBALS['hostname'] = "hostname"; + $GLOBALS['username'] = "username"; $GLOBALS['collation_connection'] = "collation_connection"; $GLOBALS['text_dir'] = "text_dir"; @@ -230,6 +232,12 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase " WHERE `User` LIKE 'INIT%' OR `User` LIKE 'init%'", $ret ); + + $ret = PMA_rangeOfUsers(); + $this->assertEquals( + '', + $ret + ); } /** @@ -970,4 +978,426 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $message->getMessage() ); } + + /** + * Test for PMA_getHtmlForTableSpecificPrivileges + * + * @return void + */ + public function testPMAGetHtmlForTableSpecificPrivileges() + { + $GLOBALS['strPrivDescCreate_viewTbl'] = "strPrivDescCreate_viewTbl"; + $GLOBALS['strPrivDescShowViewTbl'] = "strPrivDescShowViewTbl"; + $username = "PMA_username"; + $hostname = "PMA_hostname"; + $db = "PMA_db"; + $table = "PMA_table"; + $columns = array( + 'row1' => 'name1' + ); + $row = array( + 'Select_priv' => 'Y', + 'Insert_priv' => 'Y', + 'Update_priv' => 'Y', + 'References_priv' => 'Y', + 'Create_view_priv' => 'Y', + 'ShowView_priv' => 'Y', + ); + + $html = PMA_getHtmlForTableSpecificPrivileges( + $username, $hostname, $db, $table, $columns, $row + ); + + //validate 1: PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn + $item = PMA_getHtmlForAttachedPrivilegesToTableSpecificColumn( + $columns, $row + ); + $this->assertContains( + $item, + $html + ); + $this->assertContains( + __('Allows reading data.'), + $html + ); + $this->assertContains( + __('Allows inserting and replacing data'), + $html + ); + $this->assertContains( + __('Allows changing data.'), + $html + ); + $this->assertContains( + __('Has no effect in this MySQL version.'), + $html + ); + + //validate 2: PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn + $item = PMA_getHtmlForNotAttachedPrivilegesToTableSpecificColumn( + $row + ); + $this->assertContains( + $item, + $html + ); + $this->assertContains( + 'Create_view_priv', + $html + ); + $this->assertContains( + 'ShowView_priv', + $html + ); + } + + /** + * Test for PMA_getHtmlForDisplayLoginInformationFields + * + * @return void + */ + public function testPMAGetHtmlForDisplayLoginInformationFields() + { + $GLOBALS['username'] = 'pma_username'; + + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $fields_info = array( + "Host" => array( + "Field" => "host", + "Type" => "char(60)", + "Null" => "NO", + ) + ); + $dbi->expects($this->any())->method('getColumns') + ->will($this->returnValue($fields_info)); + + $fetchValue = "fetchValue"; + $dbi->expects($this->any())->method('fetchValue') + ->will($this->returnValue($fetchValue)); + + $GLOBALS['dbi'] = $dbi; + + $html = PMA_getHtmlForDisplayLoginInformationFields(); + list($username_length, $hostname_length) + = PMA_getUsernameAndHostnameLength(); + + //validate 1: __('Login Information') + $this->assertContains( + __('Login Information'), + $html + ); + $this->assertContains( + __('User name:'), + $html + ); + $this->assertContains( + __('Any user'), + $html + ); + $this->assertContains( + __('Use text field'), + $html + ); + + $output = PMA_Util::showHint( + __( + 'When Host table is used, this field is ignored ' + . 'and values stored in Host table are used instead.' + ) + ); + $this->assertContains( + $output, + $html + ); + + $GLOBALS['dbi'] = $dbi_old; + } + + /** + * Test for PMA_getWithClauseForAddUserAndUpdatePrivs + * + * @return void + */ + public function testPMAGetWithClauseForAddUserAndUpdatePrivs() + { + $_POST['Grant_priv'] = 'Y'; + $_POST['max_questions'] = 10; + $_POST['max_connections'] = 20; + $_POST['max_updates'] = 30; + $_POST['max_user_connections'] = 40; + + $sql_query = PMA_getWithClauseForAddUserAndUpdatePrivs(); + $expect = "WITH GRANT OPTION MAX_QUERIES_PER_HOUR 10 " + . "MAX_CONNECTIONS_PER_HOUR 20" + . " MAX_UPDATES_PER_HOUR 30 MAX_USER_CONNECTIONS 40"; + $this->assertContains( + $expect, + $sql_query + ); + + } + + /** + * Test for PMA_getListOfPrivilegesAndComparedPrivileges + * + * @return void + */ + public function testPMAGetListOfPrivilegesAndComparedPrivileges() + { + list($list_of_privileges, $list_of_compared_privileges) + = PMA_getListOfPrivilegesAndComparedPrivileges(); + $expect = "`User`, `Host`, `Select_priv`, `Insert_priv`"; + $this->assertContains( + $expect, + $list_of_privileges + ); + $expect = "`Select_priv` = 'N' AND `Insert_priv` = 'N'"; + $this->assertContains( + $expect, + $list_of_compared_privileges + ); + $expect = "`Create_routine_priv` = 'N' AND `Alter_routine_priv` = 'N'"; + $this->assertContains( + $expect, + $list_of_compared_privileges + ); + } + + /** + * Test for PMA_getHtmlForAddUser + * + * @return void + */ + public function testPMAGetHtmlForAddUser() + { + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $fields_info = array( + "Host" => array( + "Field" => "host", + "Type" => "char(60)", + "Null" => "NO", + ) + ); + $dbi->expects($this->any())->method('getColumns') + ->will($this->returnValue($fields_info)); + + $GLOBALS['dbi'] = $dbi; + + $dbname = "pma_dbname"; + + $html = PMA_getHtmlForAddUser($dbname); + + //validate 1: PMA_URL_getHiddenInputs + $this->assertContains( + PMA_URL_getHiddenInputs('', ''), + $html + ); + + //validate 2: PMA_getHtmlForDisplayLoginInformationFields + $this->assertContains( + PMA_getHtmlForDisplayLoginInformationFields('new'), + $html + ); + + //validate 3: Database for user + $this->assertContains( + __('Database for user'), + $html + ); + + $item = PMA_Util::getCheckbox( + 'createdb-2', + __('Grant all privileges on wildcard name (username\\_%).'), + false, false + ); + $this->assertContains( + $item, + $html + ); + + //validate 4: PMA_getHtmlToDisplayPrivilegesTable + $this->assertContains( + PMA_getHtmlToDisplayPrivilegesTable('*', '*', false), + $html + ); + + //validate 5: button + $this->assertContains( + __('Go'), + $html + ); + + $GLOBALS['dbi'] = $dbi_old; + } + + /** + * Test for PMA_getHtmlForSpecificDbPrivileges + * + * @return void + */ + public function testPMAGetHtmlForSpecificDbPrivileges() + { + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $fields_info = array( + "Host" => array( + "Field" => "host", + "Type" => "char(60)", + "Null" => "NO", + ) + ); + $dbi->expects($this->any())->method('getColumns') + ->will($this->returnValue($fields_info)); + + $GLOBALS['dbi'] = $dbi; + + $db = "pma_dbname"; + + $html = PMA_getHtmlForSpecificDbPrivileges($db); + + //validate 1: PMA_URL_getCommon + $this->assertContains( + PMA_URL_getCommon($db), + $html + ); + + //validate 2: htmlspecialchars + $this->assertContains( + htmlspecialchars($db), + $html + ); + + //validate 3: items + $this->assertContains( + __('User'), + $html + ); + $this->assertContains( + __('Host'), + $html + ); + $this->assertContains( + __('Type'), + $html + ); + $this->assertContains( + __('Privileges'), + $html + ); + $this->assertContains( + __('Grant'), + $html + ); + $this->assertContains( + __('Action'), + $html + ); + + //_pgettext('Create new user', 'New') + $this->assertContains( + _pgettext('Create new user', 'New'), + $html + ); + $this->assertContains( + PMA_URL_getCommon(array('checkprivsdb' => $db)), + $html + ); + + $GLOBALS['dbi'] = $dbi_old; + } + + /** + * Test for PMA_getHtmlForSpecificTablePrivileges + * + * @return void + */ + public function testPMAGetHtmlForSpecificTablePrivileges() + { + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $fields_info = array( + "Host" => array( + "Field" => "host", + "Type" => "char(60)", + "Null" => "NO", + ) + ); + $dbi->expects($this->any())->method('getColumns') + ->will($this->returnValue($fields_info)); + + $GLOBALS['dbi'] = $dbi; + + $db = "pma_dbname"; + $table = "pma_table"; + + $html = PMA_getHtmlForSpecificTablePrivileges($db, $table); + + //validate 1: $db, $table + $this->assertContains( + htmlspecialchars($db) . '.' . htmlspecialchars($table), + $html + ); + + //validate 2: PMA_URL_getCommon + $item = PMA_URL_getCommon( + array( + 'db' => $db, + 'table' => $table, + ) + ); + $this->assertContains( + $item, + $html + ); + + //validate 3: items + $this->assertContains( + __('User'), + $html + ); + $this->assertContains( + __('Host'), + $html + ); + $this->assertContains( + __('Type'), + $html + ); + $this->assertContains( + __('Privileges'), + $html + ); + $this->assertContains( + __('Grant'), + $html + ); + $this->assertContains( + __('Action'), + $html + ); + + //_pgettext('Create new user', 'New') + $this->assertContains( + _pgettext('Create new user', 'New'), + $html + ); + $this->assertContains( + PMA_URL_getCommon( + array('checkprivsdb' => $db, 'checkprivstable' => $table) + ), + $html + ); + + $GLOBALS['dbi'] = $dbi_old; + } } From 130bff3527bb0dc450595a0e04c6d2a67dea0229 Mon Sep 17 00:00:00 2001 From: xmujay Date: Sun, 15 Sep 2013 23:48:56 +0800 Subject: [PATCH 2/2] refactor the code style --- test/libraries/PMA_server_privileges_test.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test/libraries/PMA_server_privileges_test.php b/test/libraries/PMA_server_privileges_test.php index 622312e350..f94bc06fa5 100644 --- a/test/libraries/PMA_server_privileges_test.php +++ b/test/libraries/PMA_server_privileges_test.php @@ -232,7 +232,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase " WHERE `User` LIKE 'INIT%' OR `User` LIKE 'init%'", $ret ); - + $ret = PMA_rangeOfUsers(); $this->assertEquals( '', @@ -817,7 +817,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase . 'Create_user,Repl_slave,Repl_client', 'Type' => "'Super1','Select','Insert','Update','Create','Alter','Index'," . "'Drop','Delete','File','Super','Process','Reload','Shutdown','" - . "Show_db','Repl_slave','Create_tmp_table'," + . "Show_db','Repl_slave','Create_tmp_table'," . "'Show_view','Create_routine','" . "Repl_client','Lock_tables','References','Alter_routine','" . "Create_user','Repl_slave','Repl_client','Execute','Grant','ddd", @@ -1077,11 +1077,11 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $fetchValue = "fetchValue"; $dbi->expects($this->any())->method('fetchValue') ->will($this->returnValue($fetchValue)); - + $GLOBALS['dbi'] = $dbi; $html = PMA_getHtmlForDisplayLoginInformationFields(); - list($username_length, $hostname_length) + list($username_length, $hostname_length) = PMA_getUsernameAndHostnameLength(); //validate 1: __('Login Information') @@ -1101,7 +1101,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase __('Use text field'), $html ); - + $output = PMA_Util::showHint( __( 'When Host table is used, this field is ignored ' @@ -1112,7 +1112,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $output, $html ); - + $GLOBALS['dbi'] = $dbi_old; } @@ -1128,16 +1128,16 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $_POST['max_connections'] = 20; $_POST['max_updates'] = 30; $_POST['max_user_connections'] = 40; - + $sql_query = PMA_getWithClauseForAddUserAndUpdatePrivs(); - $expect = "WITH GRANT OPTION MAX_QUERIES_PER_HOUR 10 " - . "MAX_CONNECTIONS_PER_HOUR 20" + $expect = "WITH GRANT OPTION MAX_QUERIES_PER_HOUR 10 " + . "MAX_CONNECTIONS_PER_HOUR 20" . " MAX_UPDATES_PER_HOUR 30 MAX_USER_CONNECTIONS 40"; $this->assertContains( $expect, $sql_query ); - + } /** @@ -1163,7 +1163,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $this->assertContains( $expect, $list_of_compared_privileges - ); + ); } /** @@ -1186,9 +1186,9 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase ); $dbi->expects($this->any())->method('getColumns') ->will($this->returnValue($fields_info)); - + $GLOBALS['dbi'] = $dbi; - + $dbname = "pma_dbname"; $html = PMA_getHtmlForAddUser($dbname); @@ -1210,7 +1210,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase __('Database for user'), $html ); - + $item = PMA_Util::getCheckbox( 'createdb-2', __('Grant all privileges on wildcard name (username\\_%).'), @@ -1232,7 +1232,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase __('Go'), $html ); - + $GLOBALS['dbi'] = $dbi_old; } @@ -1256,9 +1256,9 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase ); $dbi->expects($this->any())->method('getColumns') ->will($this->returnValue($fields_info)); - + $GLOBALS['dbi'] = $dbi; - + $db = "pma_dbname"; $html = PMA_getHtmlForSpecificDbPrivileges($db); @@ -1300,7 +1300,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase __('Action'), $html ); - + //_pgettext('Create new user', 'New') $this->assertContains( _pgettext('Create new user', 'New'), @@ -1310,7 +1310,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase PMA_URL_getCommon(array('checkprivsdb' => $db)), $html ); - + $GLOBALS['dbi'] = $dbi_old; } @@ -1334,9 +1334,9 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase ); $dbi->expects($this->any())->method('getColumns') ->will($this->returnValue($fields_info)); - + $GLOBALS['dbi'] = $dbi; - + $db = "pma_dbname"; $table = "pma_table"; @@ -1385,7 +1385,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase __('Action'), $html ); - + //_pgettext('Create new user', 'New') $this->assertContains( _pgettext('Create new user', 'New'), @@ -1397,7 +1397,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase ), $html ); - + $GLOBALS['dbi'] = $dbi_old; } }