From bcad25a84af4b2d28b7903208b2bd0399f55a73a Mon Sep 17 00:00:00 2001 From: Leonardo Strozzi Date: Thu, 5 Jul 2018 22:27:36 -0300 Subject: [PATCH 1/3] Refactor table_tracking main tamplates Signed-off-by: Leonardo Strozzi --- libraries/classes/Tracking.php | 187 +++++------------- tbl_tracking.php | 44 +---- ...sion.twig => create_tracking_version.twig} | 0 .../table/tracking/activate_deactivate.twig | 27 --- templates/table/tracking/main.twig | 128 ++++++++++++ .../table/tracking/selectable_tables.twig | 17 -- 6 files changed, 181 insertions(+), 222 deletions(-) rename templates/{table/tracking/create_version.twig => create_tracking_version.twig} (100%) delete mode 100644 templates/table/tracking/activate_deactivate.twig create mode 100644 templates/table/tracking/main.twig delete mode 100644 templates/table/tracking/selectable_tables.twig diff --git a/libraries/classes/Tracking.php b/libraries/classes/Tracking.php index a7e369b199..826f96c178 100644 --- a/libraries/classes/Tracking.php +++ b/libraries/classes/Tracking.php @@ -101,7 +101,7 @@ class Tracking array $selected, $type = 'both' ) { - return $this->template->render('table/tracking/create_version', [ + return $this->template->render('create_tracking_version', [ 'url_query' => $urlQuery, 'last_version' => $lastVersion, 'db' => $db, @@ -156,129 +156,62 @@ class Tracking } /** - * Function to get html for displaying last version number + * Function to get html for main page parts that do not use $_REQUEST * - * @param array $sql_result sql result - * @param int $last_version last version - * @param array $url_params url parameters - * @param string $url_query url query + * @param string $urlQuery url query + * @param array $urlParams url parameters * @param string $pmaThemeImage path to theme's image folder - * @param string $text_dir text direction + * @param string $textDir text direction + * @param int $lastVersion last tracking version * * @return string */ - public function getHtmlForTableVersionDetails( - $sql_result, - $last_version, - array $url_params, - $url_query, + public function getHtmlForMainPage( + $urlQuery, + $urlParams, $pmaThemeImage, - $text_dir + $textDir, + $lastVersion = null ) { - $tracking_active = false; - - $html = '
'; - $html .= Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - - $GLOBALS['dbi']->dataSeek($sql_result, 0); - $delete = Util::getIcon('b_drop', __('Delete version')); - $report = Util::getIcon('b_report', __('Tracking report')); - $structure = Util::getIcon('b_props', __('Structure snapshot')); - - while ($version = $GLOBALS['dbi']->fetchArray($sql_result)) { - if ($version['version'] == $last_version) { - if ($version['tracking_active'] == 1) { - $tracking_active = true; - } else { - $tracking_active = false; - } - } - $delete_link = 'tbl_tracking.php' . $url_query . '&version=' - . htmlspecialchars($version['version']) - . '&submit_delete_version=true'; - $checkbox_id = 'selected_versions_' . htmlspecialchars($version['version']); - - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; + $selectableTablesEntries[] = $entry; + } + $selectableTablesNumRows = $GLOBALS['dbi']->numRows($selectableTablesSqlResult); + + $versionSqlResult = $this->getListOfVersionsOfTable(); + if ($lastVersion === null) { + $lastVersion = $this->getTableLastVersionNumber($versionSqlResult); + } + $GLOBALS['dbi']->dataSeek($versionSqlResult, 0); + $versions = array(); + while ($version = $GLOBALS['dbi']->fetchArray($versionSqlResult)) { + $versions[] = $version; } - $html .= ''; - $html .= '
' . __('Version') . '' . __('Created') . '' . __('Updated') . '' . __('Status') . '' . __('Action') . '' . __('Show') . '
'; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= '' . htmlspecialchars($version['date_created']) . '' . htmlspecialchars($version['date_updated']) . '' . $this->getVersionStatus($version) . '' . $delete . '' . $report . ''; - $html .= '  '; - $html .= '' . $structure . ''; - $html .= '
'; + $type = $GLOBALS['dbi']->getTable($GLOBALS['db'], $GLOBALS['table']) + ->isView() ? 'view' : 'table'; - $html .= $this->template->render('select_all', [ - 'pma_theme_image' => $pmaThemeImage, - 'text_dir' => $text_dir, - 'form_name' => 'versionsForm', + return $this->template->render('table/tracking/main', [ + 'url_query' => $urlQuery, + 'url_params' => $urlParams, + 'db' => $GLOBALS['db'], + 'table' => $GLOBALS['table'], + 'selectable_tables_num_rows' => $selectableTablesNumRows, + 'selectable_tables_entries' => $selectableTablesEntries, + 'selected_table' => isset($_REQUEST['table']) ? $_REQUEST['table'] : null, + 'last_version' => $lastVersion, + 'versions' => $versions, + 'type' => $type, + 'default_statements' => $GLOBALS['cfg']['Server']['tracking_default_statements'], + 'pmaThemeImage' => $pmaThemeImage, + 'text_dir' => $textDir ]); - $html .= Util::getButtonOrImage( - 'submit_mult', - 'mult_submit', - __('Delete version'), - 'b_drop', - 'delete_version' - ); - - $html .= '
'; - - if ($tracking_active) { - $html .= $this->getHtmlForActivateDeactivateTracking( - 'deactivate', - $url_query, - $last_version - ); - } else { - $html .= $this->getHtmlForActivateDeactivateTracking( - 'activate', - $url_query, - $last_version - ); - } - - return $html; } /** @@ -314,36 +247,6 @@ class Tracking return $relation->queryAsControlUser($sql_query); } - /** - * Function to get html for selectable table rows - * - * @param array $selectableTablesSqlResult sql results for selectable rows - * @param string $urlQuery url query - * - * @return string - */ - public function getHtmlForSelectableTables( - $selectableTablesSqlResult, - $urlQuery - ) { - $entries = []; - while ($entry = $GLOBALS['dbi']->fetchArray($selectableTablesSqlResult)) { - $entry['is_tracked'] = Tracker::isTracked( - $entry['db_name'], - $entry['table_name'] - ); - $entries[] = $entry; - } - - return $this->template->render('table/tracking/selectable_tables', [ - 'url_query' => $urlQuery, - 'db' => $GLOBALS['db'], - 'table' => $GLOBALS['table'], - 'entries' => $entries, - 'selected_table' => isset($_REQUEST['table']) ? $_REQUEST['table'] : null, - ]); - } - /** * Function to get html for tracking report and tracking report export * diff --git a/tbl_tracking.php b/tbl_tracking.php index e5b1771e35..d43a43d47d 100644 --- a/tbl_tracking.php +++ b/tbl_tracking.php @@ -94,7 +94,7 @@ if (isset($_REQUEST['report_export']) $tracking->exportAsFileDownload($entries); } -$html = '
'; +$html = '
'; /** * Actions @@ -184,42 +184,14 @@ if (isset($_REQUEST['report']) || isset($_REQUEST['report_export'])) { /* - * List selectable tables + * Main page */ -$selectable_tables_sql_result = $tracking->getSqlResultForSelectableTables(); -if ($GLOBALS['dbi']->numRows($selectable_tables_sql_result) > 0) { - $html .= $tracking->getHtmlForSelectableTables( - $selectable_tables_sql_result, - $url_query - ); -} -$html .= '
'; - -/* - * List versions of current table - */ -$sql_result = $tracking->getListOfVersionsOfTable(); -$last_version = $tracking->getTableLastVersionNumber($sql_result); -if ($last_version > 0) { - $html .= $tracking->getHtmlForTableVersionDetails( - $sql_result, - $last_version, - $url_params, - $url_query, - $pmaThemeImage, - $text_dir - ); -} - -$type = $GLOBALS['dbi']->getTable($GLOBALS['db'], $GLOBALS['table']) - ->isView() ? 'view' : 'table'; -$html .= $tracking->getHtmlForDataDefinitionAndManipulationStatements( - 'tbl_tracking.php' . $url_query, - $last_version, - $GLOBALS['db'], - [$GLOBALS['table']], - $type -); + $html .= $tracking->getHtmlForMainPage( + $url_query, + $url_params, + $pmaThemeImage, + $text_dir + ); $html .= '
'; diff --git a/templates/table/tracking/create_version.twig b/templates/create_tracking_version.twig similarity index 100% rename from templates/table/tracking/create_version.twig rename to templates/create_tracking_version.twig diff --git a/templates/table/tracking/activate_deactivate.twig b/templates/table/tracking/activate_deactivate.twig deleted file mode 100644 index 5817e42039..0000000000 --- a/templates/table/tracking/activate_deactivate.twig +++ /dev/null @@ -1,27 +0,0 @@ -
-
- {{ Url_getHiddenInputs(db, table) }} -
- - {% if action == 'activate' %} - {% set legend = 'Activate tracking for %s'|trans %} - {% set value = 'activate_now' %} - {% set button = 'Activate now'|trans %} - {% elseif action == 'deactivate' %} - {% set legend = 'Deactivate tracking for %s'|trans %} - {% set value = 'deactivate_now' %} - {% set button = 'Deactivate now'|trans %} - {% else %} - {% set legend = '' %} - {% set value = '' %} - {% set button = '' %} - {% endif %} - - {{ legend|format(db ~ '.' ~ table) }} - - - - -
-
-
diff --git a/templates/table/tracking/main.twig b/templates/table/tracking/main.twig new file mode 100644 index 0000000000..10e39ef23d --- /dev/null +++ b/templates/table/tracking/main.twig @@ -0,0 +1,128 @@ +{% if selectable_tables_num_rows > 0 %} +
+ {{ Url_getHiddenInputs(db, table) }} + + +
+{% endif %} +
+{% if last_version > 0 %} +
+ {{ Url_getHiddenInputs(db, table) }} + + + + + + + + + + + + + + {% for version in versions %} + + + + + + {% if version['tracking_active'] == 1 %} + {% set last_version_status = 1 %} + + {% else %} + {% set last_version_status = 0 %} + + {% endif %} + + + + {% endfor %} + +
{% trans 'Version' %}{% trans 'Created' %}{% trans 'Updated' %}{% trans 'Status' %}{% trans 'Action' %}{% trans 'Show' %}
+ + + + {{ version['date_created']|escape }}{{ version['date_updated']|escape }}{% trans 'active' %}{% trans 'not active' %} + + {{ Util_getIcon('b_drop', 'Delete version'|trans) }} + + + + {{ Util_getIcon('b_report', 'Tracking report'|trans) }} + +    + + {{ Util_getIcon('b_props', 'Structure snapshot'|trans) }} + +
+ {% include 'select_all.twig' with { + 'pma_theme_image': pmaThemeImage, + 'text_dir': text_dir, + 'form_name': 'versionsForm', + } only %} + {{ Util_getButtonOrImage( + 'submit_mult', + 'mult_submit', + 'Delete version'|trans, + 'b_drop', + 'delete_version' + ) }} +
+ {% set last_version_element = versions|first %} +
+
+ {{ Url_getHiddenInputs(db, table) }} +
+ + {% if last_version_element['tracking_active'] == 0 %} + {% set legend = 'Activate tracking for %s'|trans %} + {% set value = 'activate_now' %} + {% set button = 'Activate now'|trans %} + {% else %} + {% set legend = 'Deactivate tracking for %s'|trans %} + {% set value = 'deactivate_now' %} + {% set button = 'Deactivate now'|trans %} + {% endif %} + + {{ legend|format(db ~ '.' ~ table) }} + + + + +
+
+
+{% endif %} +{% include 'create_tracking_version.twig' with { + 'url_query': url_query, + 'last_version': last_version, + 'db': db, + 'selected': [table], + 'type': type, + 'default_statements': default_statements, +} only %} diff --git a/templates/table/tracking/selectable_tables.twig b/templates/table/tracking/selectable_tables.twig deleted file mode 100644 index eca71bf121..0000000000 --- a/templates/table/tracking/selectable_tables.twig +++ /dev/null @@ -1,17 +0,0 @@ -
- {{ Url_getHiddenInputs(db, table) }} - - -
From e95505239d5fc8e8ef31faf7f4c1b2a477de372a Mon Sep 17 00:00:00 2001 From: Leonardo Strozzi Date: Thu, 5 Jul 2018 22:28:02 -0300 Subject: [PATCH 2/3] Adapt table tracking main tests Signed-off-by: Leonardo Strozzi --- test/classes/TrackingTest.php | 328 ++++++++++------------------------ 1 file changed, 96 insertions(+), 232 deletions(-) diff --git a/test/classes/TrackingTest.php b/test/classes/TrackingTest.php index 1c4c938271..49a1d9a54d 100644 --- a/test/classes/TrackingTest.php +++ b/test/classes/TrackingTest.php @@ -163,17 +163,84 @@ class TrackingTest extends TestCase * @return void * @test */ - public function testGetHtmlForDataDefinitionAndManipulationStatements() + public function testGetHtmlForMain() { - $url_query = "url_query"; - $last_version = 10; - $html = $this->tracking->getHtmlForDataDefinitionAndManipulationStatements( + $sql_result = true; + $last_version = 3; + $url_params = []; + $url_query = "select * from PMA"; + $pmaThemeImage = "themePath/img"; + $text_dir = "ltr"; + + // Mock dbi + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $fetchArray = [ + 'tracking_active' => 1, + 'version' => 1, + 'db_name' => 'db_name', + 'table_name' => 'table_name', + 'date_created' => 'date_created', + 'date_updated' => 'date_updated' + ]; + // return fetchArray for selectable entries + for ($i = 2; $i < 6; $i++) { + $dbi->expects($this->at($i)) + ->method('fetchArray') + ->will($this->returnValue($fetchArray)); + } + $dbi->expects($this->at(6)) + ->method('fetchArray') + ->will($this->returnValue(false)); + // return fetchArray for Activate/Deactivate tracking + for ($i = 7; $i < 13; $i++) { + $dbi->expects($this->at($i)) + ->method('fetchArray') + ->will($this->returnValue($fetchArray)); + } + $dbi->expects($this->at(13)) + ->method('fetchArray') + ->will($this->returnValue(false)); + + $dbi->method('numRows') + ->will($this->returnValue(1)); + + $GLOBALS['dbi'] = $dbi; + + $html = $this->tracking->getHtmlForMainPage( $url_query, - $last_version, - $GLOBALS['db'], - [$GLOBALS['table']] + $url_params, + $pmaThemeImage, + $text_dir, + $last_version ); + /* + * test selectables panel + */ + $this->assertContains( + htmlspecialchars($fetchArray['db_name']).'.'.htmlspecialchars($fetchArray['table_name']), + $html + ); + + /* + * test versions table + */ + $this->assertContains( + "date_created", + $html + ); + $this->assertContains( + __('Delete version'), + $html + ); + + /* + * test create panel + */ $this->assertContains( '
', $html @@ -210,77 +277,34 @@ class TrackingTest extends TestCase __('Create version'), $html ); - } - /** - * Tests for getHtmlForActivateDeactivateTracking() method. - * - * @return void - * @test - */ - public function testGetHtmlForActivateDeactivateTracking() - { - $url_query = "url_query"; - $last_version = "10"; - $html = $this->tracking->getHtmlForActivateDeactivateTracking( - 'activate', + /* + * test deactivate/activate panel + */ + $this->assertContains( + 'Deactivate now', + $html + ); + $fetchArray['tracking_active'] = 0; + $dbi->expects($this->at(9)) + ->method('fetchArray') + ->will($this->returnValue($fetchArray)); + $GLOBALS['dbi'] = $dbi; + $html = $this->tracking->getHtmlForMainPage( $url_query, + $url_params, + $pmaThemeImage, + $text_dir, $last_version ); + $this->assertContains( + 'Activate now', + $html + ); - $this->assertContains( - $url_query, - $html - ); - $item = sprintf( - __('Activate tracking for %s'), - htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']) - ); - $this->assertContains( - $item, - $html - ); - - $this->assertContains( - $last_version, - $html - ); - - $this->assertContains( - __('Activate now'), - $html - ); - - $html = $this->tracking->getHtmlForActivateDeactivateTracking( - 'deactivate', - $url_query, - $last_version - ); - - $this->assertContains( - $url_query, - $html - ); - - $item = sprintf( - __('Deactivate tracking for %s'), - htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']) - ); - $this->assertContains( - $item, - $html - ); - - $this->assertContains( - $last_version, - $html - ); - - $this->assertContains( - __('Deactivate now'), - $html - ); + //restore DBI + $GLOBALS['dbi'] = $dbi_old; } /** @@ -431,166 +455,6 @@ class TrackingTest extends TestCase ); } - /** - * Tests for getHtmlForTableVersionDetails() method. - * - * @return void - * @test - */ - public function testGetHtmlForTableVersionDetails() - { - $sql_result = true; - $last_version = "10"; - $url_params = []; - $url_query = "select * from PMA"; - $pmaThemeImage = "themePath/img"; - $text_dir = "ltr"; - - $dbi_old = $GLOBALS['dbi']; - $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $fetchArray = [ - 'tracking_active' => 1, - 'version' => "10", - 'db_name' => 'db_name', - 'table_name' => 'table_name', - 'date_created' => 'date_created', - 'date_updated' => 'date_updated' - ]; - $dbi->expects($this->at(0)) - ->method('fetchArray') - ->will($this->returnValue($fetchArray)); - $dbi->expects($this->at(1)) - ->method('fetchArray') - ->will($this->returnValue($fetchArray)); - $dbi->expects($this->at(2)) - ->method('fetchArray') - ->will($this->returnValue(false)); - - $GLOBALS['dbi'] = $dbi; - - $ret = $this->tracking->getHtmlForTableVersionDetails( - $sql_result, - $last_version, - $url_params, - $url_query, - $pmaThemeImage, - $text_dir - ); - - $this->assertContains( - __('Version'), - $ret - ); - $this->assertContains( - __('Created'), - $ret - ); - $this->assertContains( - __('Updated'), - $ret - ); - $this->assertContains( - __('Status'), - $ret - ); - $this->assertContains( - __('Action'), - $ret - ); - $this->assertContains( - __('Show'), - $ret - ); - $this->assertContains( - $fetchArray['version'], - $ret - ); - $this->assertContains( - $fetchArray['date_created'], - $ret - ); - $this->assertContains( - $fetchArray['date_updated'], - $ret - ); - $this->assertContains( - __('Tracking report'), - $ret - ); - $this->assertContains( - __('Structure snapshot'), - $ret - ); - $html = sprintf( - __('Deactivate tracking for %s'), - htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']) - ); - $this->assertContains( - $html, - $ret - ); - - //restore DBI - $GLOBALS['dbi'] = $dbi_old; - } - - /** - * Tests for getHtmlForSelectableTables() method. - * - * @return void - * @test - */ - public function testGetHtmlForSelectableTables() - { - $selectable_tables_sql_result = true; - $url_query = "select * from PMA"; - - $dbi_old = $GLOBALS['dbi']; - $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface') - ->disableOriginalConstructor() - ->getMock(); - - $fetchArray = [ - 'tracking_active' => 1, - 'version' => "10", - 'db_name' => 'db_name', - 'table_name' => 'table_name', - 'date_created' => 'date_created', - 'date_updated' => 'date_updated' - ]; - $dbi->expects($this->at(0)) - ->method('fetchArray') - ->will($this->returnValue($fetchArray)); - $dbi->expects($this->at(1)) - ->method('fetchArray') - ->will($this->returnValue($fetchArray)); - $dbi->expects($this->at(2)) - ->method('fetchArray') - ->will($this->returnValue(false)); - - $GLOBALS['dbi'] = $dbi; - - $ret = $this->tracking->getHtmlForSelectableTables( - $selectable_tables_sql_result, - $url_query - ); - - $this->assertContains( - htmlspecialchars($fetchArray['table_name']), - $ret - ); - $this->assertContains( - htmlspecialchars($fetchArray['db_name']), - $ret - ); - - //restore DBI - $GLOBALS['dbi'] = $dbi_old; - } - /** * Tests for getHtmlForTrackingReport() method. * From 3b8746547dd35dba0e2bf59bb608df3fcc62f997 Mon Sep 17 00:00:00 2001 From: Leonardo Strozzi Date: Sun, 8 Jul 2018 18:20:55 -0300 Subject: [PATCH 3/3] Fix php code sniffer errors Signed-off-by: Leonardo Strozzi --- libraries/classes/Tracking.php | 8 ++++---- test/classes/TrackingTest.php | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/libraries/classes/Tracking.php b/libraries/classes/Tracking.php index 826f96c178..dff336d02e 100644 --- a/libraries/classes/Tracking.php +++ b/libraries/classes/Tracking.php @@ -158,11 +158,11 @@ class Tracking /** * Function to get html for main page parts that do not use $_REQUEST * - * @param string $urlQuery url query - * @param array $urlParams url parameters + * @param string $urlQuery url query + * @param array $urlParams url parameters * @param string $pmaThemeImage path to theme's image folder - * @param string $textDir text direction - * @param int $lastVersion last tracking version + * @param string $textDir text direction + * @param int $lastVersion last tracking version * * @return string */ diff --git a/test/classes/TrackingTest.php b/test/classes/TrackingTest.php index 49a1d9a54d..aba9cdf526 100644 --- a/test/classes/TrackingTest.php +++ b/test/classes/TrackingTest.php @@ -222,7 +222,7 @@ class TrackingTest extends TestCase * test selectables panel */ $this->assertContains( - htmlspecialchars($fetchArray['db_name']).'.'.htmlspecialchars($fetchArray['table_name']), + htmlspecialchars($fetchArray['db_name']) . '.' . htmlspecialchars($fetchArray['table_name']), $html ); @@ -302,7 +302,6 @@ class TrackingTest extends TestCase $html ); - //restore DBI $GLOBALS['dbi'] = $dbi_old; }