From 4fc8d2b37e046ac7de5805ff28df2b830730de12 Mon Sep 17 00:00:00 2001 From: ayushchd Date: Fri, 12 Jul 2013 13:07:01 +0545 Subject: [PATCH 01/33] Fix failing tests because of static variable --- libraries/user_preferences.lib.php | 4 ++++ test/libraries/PMA_user_preferences_test.php | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libraries/user_preferences.lib.php b/libraries/user_preferences.lib.php index 8697a75b8b..910e603dae 100644 --- a/libraries/user_preferences.lib.php +++ b/libraries/user_preferences.lib.php @@ -175,6 +175,10 @@ function PMA_readUserprefsFieldNames(array $forms = null) { static $names; + if (defined('TESTSUITE')) { + $names = null; + } + // return cached results if ($names !== null) { return $names; diff --git a/test/libraries/PMA_user_preferences_test.php b/test/libraries/PMA_user_preferences_test.php index f71420aa63..5320ba5976 100644 --- a/test/libraries/PMA_user_preferences_test.php +++ b/test/libraries/PMA_user_preferences_test.php @@ -293,16 +293,26 @@ class PMA_User_Preferences_Test extends PHPUnit_Framework_TestCase /** * Test for PMA_readUserprefsFieldNames - * This test would only work when executed with other tests. - * This is to test "static" nature of this function * * @return void */ public function testReadUserprefsFieldNames() { + $this->assertCount( + 216, + PMA_readUserprefsFieldNames() + ); + + $forms = array( + 'form1' => array( + array('Servers/1/hide_db', 'bar'), + array('test' => 'val') + ) + ); + $this->assertEquals( array('Servers/1/hide_db', 'bar', 'test'), - PMA_readUserprefsFieldNames(array()) + PMA_readUserprefsFieldNames($forms) ); } From 550a6064432b531df59c39209a939bdd6cf31c1b Mon Sep 17 00:00:00 2001 From: ayushchd Date: Fri, 12 Jul 2013 13:07:30 +0545 Subject: [PATCH 02/33] Improve tests and coverage --- test/libraries/PMA_ConfigGenerator_test.php | 1 + test/libraries/PMA_FormDisplay_tpl_test.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/libraries/PMA_ConfigGenerator_test.php b/test/libraries/PMA_ConfigGenerator_test.php index c6a8a8aad2..41912fa0a4 100644 --- a/test/libraries/PMA_ConfigGenerator_test.php +++ b/test/libraries/PMA_ConfigGenerator_test.php @@ -39,6 +39,7 @@ class PMA_ConfigGenerator_Test extends PHPUnit_Framework_TestCase $GLOBALS['server'] = 0; $cf = ConfigFile::getInstance(); + $_SESSION['ConfigFile0'] = array('a', 'b', 'c'); $_SESSION['ConfigFile0']['Servers'] = array( array(1, 2, 3) ); diff --git a/test/libraries/PMA_FormDisplay_tpl_test.php b/test/libraries/PMA_FormDisplay_tpl_test.php index 97ceed7d8d..72d492505c 100644 --- a/test/libraries/PMA_FormDisplay_tpl_test.php +++ b/test/libraries/PMA_FormDisplay_tpl_test.php @@ -233,6 +233,7 @@ class PMA_FormDisplay_Tpl_Test extends PHPUnit_Framework_TestCase $opts['doc'] = "http://doclink"; $opts['wiki'] = "http://wikilink"; $opts['comment'] = "testComment"; + $opts['comment_warning'] = true; $opts['show_restore_default'] = true; ob_start(); PMA_displayInput( @@ -319,7 +320,7 @@ class PMA_FormDisplay_Tpl_Test extends PHPUnit_Framework_TestCase $this->assertTag( $this->_getTagArray( - '', + '', array('content' => 'i') ), $result From dcd8d12cf6c4a3459a67b151cf5978f916b41d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 12 Jul 2013 10:21:17 +0200 Subject: [PATCH 03/33] Move version gathering into function and test it --- libraries/Util.class.php | 99 ++++++++++++++++++++++++++++++---- test/classes/PMA_Util_test.php | 15 ++++++ version_check.php | 67 +++-------------------- 3 files changed, 111 insertions(+), 70 deletions(-) diff --git a/libraries/Util.class.php b/libraries/Util.class.php index cd4f74d25d..b7f80932e7 100644 --- a/libraries/Util.class.php +++ b/libraries/Util.class.php @@ -89,7 +89,7 @@ class PMA_Util /** * Returns an HTML IMG tag for a particular icon from a theme, * which may be an actual file or an icon from a sprite. - * This function takes into account the ActionLinksMode + * This function takes into account the ActionLinksMode * configuration setting and wraps the image tag in a span tag. * * @param string $icon name of icon file @@ -106,19 +106,19 @@ class PMA_Util ) { $include_icon = $include_text = false; if (in_array( - $GLOBALS['cfg'][$control_param], + $GLOBALS['cfg'][$control_param], array('icons', 'both') ) - ) { + ) { $include_icon = true; - } + } if ($force_text || in_array( - $GLOBALS['cfg'][$control_param], + $GLOBALS['cfg'][$control_param], array('text', 'both') ) ) { - $include_text = true; + $include_text = true; } // Sometimes use a span (we rely on this in js/sql.js). But for menu bar // we don't need a span @@ -1811,10 +1811,10 @@ class PMA_Util // the text that follows and if browser does not display // images, the text is duplicated $tab['text'] = self::getIcon( - $tab['icon'], - $tab['text'], - false, - true, + $tab['icon'], + $tab['text'], + false, + true, 'TabsMode' ); @@ -4152,5 +4152,84 @@ class PMA_Util } return $regex; } + + /** + * Returns information with latest version from phpmyadmin.net + * + * @return JSON decoded object with the data + */ + public static function getLatestVersion() + { + global $cfg; + + $response = '{}'; + // Get response text from phpmyadmin.net or from the session + // Update cache every 6 hours + if (isset($_SESSION['cache']['version_check']) + && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6 + ) { + $save = false; + $response = $_SESSION['cache']['version_check']['response']; + } else { + $save = true; + $file = 'http://www.phpmyadmin.net/home_page/version.json'; + if (ini_get('allow_url_fopen')) { + if (strlen($cfg['VersionCheckProxyUrl'])) { + $context = array( + 'http' => array( + 'proxy' => $cfg['VersionCheckProxyUrl'], + 'request_fulluri' => true + ) + ); + if (strlen($cfg['VersionCheckProxyUser'])) { + $auth = base64_encode( + $cfg['VersionCheckProxyUser'] . ':' . $cfg['VersionCheckProxyPass'] + ); + $context['http']['header'] = 'Proxy-Authorization: Basic ' . $auth; + } + $response = file_get_contents( + $file, + false, + stream_context_create($context) + ); + } else { + $response = file_get_contents($file); + } + } else if (function_exists('curl_init')) { + $curl_handle = curl_init($file); + if (strlen($cfg['VersionCheckProxyUrl'])) { + curl_setopt($curl_handle, CURLOPT_PROXY, $cfg['VersionCheckProxyUrl']); + if (strlen($cfg['VersionCheckProxyUser'])) { + curl_setopt( + $curl_handle, + CURLOPT_PROXYUSERPWD, + $cfg['VersionCheckProxyUser'] . ':' . $cfg['VersionCheckProxyPass'] + ); + } + } + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + $response = curl_exec($curl_handle); + } + } + + if ($save) { + $_SESSION['cache']['version_check'] = array( + 'response' => $response, + 'timestamp' => time() + ); + } + + $data = json_decode($response); + if (is_object($data) && strlen($data->version) && strlen($data->date)) { + if ($save) { + $_SESSION['cache']['version_check'] = array( + 'response' => $response, + 'timestamp' => time() + ); + } + } + + return $data; + } } ?> diff --git a/test/classes/PMA_Util_test.php b/test/classes/PMA_Util_test.php index ae157a9bfb..b18299fcc6 100644 --- a/test/classes/PMA_Util_test.php +++ b/test/classes/PMA_Util_test.php @@ -97,4 +97,19 @@ class PMA_Util_Test extends PHPUnit_Framework_TestCase PMA_Util::pageselector("pma", 3) ); } + + /** + * Test version checking + * + * @return void + * + * @group large + */ + public function testGetLatestVersion() + { + $GLOBALS['cfg']['VersionCheckProxyUrl'] = ''; + $version = PMA_Util::getLatestVersion(); + $this->assertNotEmpty($version->version); + $this->assertNotEmpty($version->date); + } } diff --git a/version_check.php b/version_check.php index eac1fd2c44..d4ad963802 100644 --- a/version_check.php +++ b/version_check.php @@ -9,69 +9,16 @@ // Sets up the session define('PMA_MINIMUM_COMMON', true); require_once 'libraries/common.inc.php'; - -// Get response text from phpmyadmin.net or from the session -// Update cache every 6 hours -if (isset($_SESSION['cache']['version_check']) - && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6 -) { - $save = false; - $response = $_SESSION['cache']['version_check']['response']; -} else { - $save = true; - $file = 'http://www.phpmyadmin.net/home_page/version.json'; - if (ini_get('allow_url_fopen')) { - if (strlen($cfg['VersionCheckProxyUrl'])) { - $context = array( - 'http' => array( - 'proxy' => $cfg['VersionCheckProxyUrl'], - 'request_fulluri' => true - ) - ); - if (strlen($cfg['VersionCheckProxyUser'])) { - $auth = base64_encode( - $cfg['VersionCheckProxyUser'] . ':' . $cfg['VersionCheckProxyPass'] - ); - $context['http']['header'] = 'Proxy-Authorization: Basic ' . $auth; - } - $response = file_get_contents( - $file, - false, - stream_context_create($context) - ); - } else { - $response = file_get_contents($file); - } - } else if (function_exists('curl_init')) { - $curl_handle = curl_init($file); - if (strlen($cfg['VersionCheckProxyUrl'])) { - curl_setopt($curl_handle, CURLOPT_PROXY, $cfg['VersionCheckProxyUrl']); - if (strlen($cfg['VersionCheckProxyUser'])) { - curl_setopt( - $curl_handle, - CURLOPT_PROXYUSERPWD, - $cfg['VersionCheckProxyUser'] . ':' . $cfg['VersionCheckProxyPass'] - ); - } - } - curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl_handle); - } -} +require_once 'libraries/Util.class.php'; // Always send the correct headers header('Content-type: application/json; charset=UTF-8'); -// Save and forward the response only if in valid format -$data = json_decode($response); -if (is_object($data) && strlen($data->version) && strlen($data->date)) { - if ($save) { - $_SESSION['cache']['version_check'] = array( - 'response' => $response, - 'timestamp' => time() - ); - } - echo $response; -} +$version = PMA_Util::getLatestVersion(); + +echo json_encode(array( + 'version' => $version->version, + 'date' => $version->date, +)); ?> From e4e6e1e46f812ceac70e0a50f2cc30c438f0270f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 12 Jul 2013 10:25:34 +0200 Subject: [PATCH 04/33] Set timeout for version check requests Ported from setup script code (setup/lib/index.lib.php). --- libraries/Util.class.php | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libraries/Util.class.php b/libraries/Util.class.php index b7f80932e7..d499d4439a 100644 --- a/libraries/Util.class.php +++ b/libraries/Util.class.php @@ -4162,6 +4162,10 @@ class PMA_Util { global $cfg; + // wait 3s at most for server response, it's enough to get information + // from a working server + $connection_timeout = 3; + $response = '{}'; // Get response text from phpmyadmin.net or from the session // Update cache every 6 hours @@ -4174,27 +4178,26 @@ class PMA_Util $save = true; $file = 'http://www.phpmyadmin.net/home_page/version.json'; if (ini_get('allow_url_fopen')) { + $context = array( + 'http' => array( + 'request_fulluri' => true, + 'timeout' => $connection_timeout, + ) + ); if (strlen($cfg['VersionCheckProxyUrl'])) { - $context = array( - 'http' => array( - 'proxy' => $cfg['VersionCheckProxyUrl'], - 'request_fulluri' => true - ) - ); + $context['http']['proxy'] = $cfg['VersionCheckProxyUrl']; if (strlen($cfg['VersionCheckProxyUser'])) { $auth = base64_encode( $cfg['VersionCheckProxyUser'] . ':' . $cfg['VersionCheckProxyPass'] ); $context['http']['header'] = 'Proxy-Authorization: Basic ' . $auth; } - $response = file_get_contents( - $file, - false, - stream_context_create($context) - ); - } else { - $response = file_get_contents($file); } + $response = file_get_contents( + $file, + false, + stream_context_create($context) + ); } else if (function_exists('curl_init')) { $curl_handle = curl_init($file); if (strlen($cfg['VersionCheckProxyUrl'])) { @@ -4208,6 +4211,9 @@ class PMA_Util } } curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl_handle, CURLOPT_HEADER, false); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl_handle, CURLOPT_TIMEOUT, $connection_timeout); $response = curl_exec($curl_handle); } } From e11f562be4c205895701b46bb82bdb978531c265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 12 Jul 2013 10:31:12 +0200 Subject: [PATCH 05/33] Use single code for fetching sremote version information --- setup/lib/index.lib.php | 43 +++++------------------------------------ 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/setup/lib/index.lib.php b/setup/lib/index.lib.php index e855081a5a..4a7ab40dab 100644 --- a/setup/lib/index.lib.php +++ b/setup/lib/index.lib.php @@ -106,37 +106,11 @@ function PMA_version_check() // version check messages should always be visible so let's make // a unique message id each time we run it $message_id = uniqid('version_check'); - // wait 3s at most for server response, it's enough to get information - // from a working server - $connection_timeout = 3; - $url = 'http://phpmyadmin.net/home_page/version.php'; - $context = stream_context_create( - array( - 'http' => array('timeout' => $connection_timeout) - ) - ); - $data = @file_get_contents($url, null, $context); - if ($data === false) { - if (function_exists('curl_init')) { - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_HEADER, false); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_TIMEOUT, $connection_timeout); - $data = curl_exec($ch); - curl_close($ch); - } else { - messages_set( - 'error', - $message_id, - __('Version check'), - __('Neither URL wrapper nor CURL is available. Version check is not possible.') - ); - return; - } - } + // Fetch data + $version_data = PMA_Util::getLatestVersion(); - if (empty($data)) { + if (empty($version_data)) { messages_set( 'error', $message_id, @@ -146,15 +120,8 @@ function PMA_version_check() return; } - /* Format: version\ndate\n(download\n)* */ - $data_list = explode("\n", $data); - - if (count($data_list) > 1) { - $version = $data_list[0]; - $date = $data_list[1]; - } else { - $version = $date = ''; - } + $version = $version_data->version; + $date = $version_data->date; $version_upstream = version_to_int($version); if ($version_upstream === false) { From fc163bb51273f6a7a66e04c26d75158e8b78c739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 12 Jul 2013 10:39:14 +0200 Subject: [PATCH 06/33] Move version parsing to Util class --- libraries/Util.class.php | 49 +++++++++++++++++ setup/lib/index.lib.php | 53 +----------------- test/classes/PMA_Util_test.php | 47 ++++++++++++++++ test/libraries/PMA_Index_test.php | 90 ++++++++----------------------- 4 files changed, 121 insertions(+), 118 deletions(-) diff --git a/libraries/Util.class.php b/libraries/Util.class.php index d499d4439a..602e6b8659 100644 --- a/libraries/Util.class.php +++ b/libraries/Util.class.php @@ -4237,5 +4237,54 @@ class PMA_Util return $data; } + + /** + * Calculates numerical equivalent of phpMyAdmin version string + * + * @param string $version version + * + * @return mixed false on failure, integer on success + */ + public static function versionToInt($version) + { + $matches = array(); + if (!preg_match('/^(\d+)\.(\d+)\.(\d+)((\.|-(pl|rc|dev|beta|alpha))(\d+)?(-dev)?)?$/', $version, $matches)) { + return false; + } + if (!empty($matches[6])) { + switch ($matches[6]) { + case 'pl': + $added = 60; + break; + case 'rc': + $added = 30; + break; + case 'beta': + $added = 20; + break; + case 'alpha': + $added = 10; + break; + case 'dev': + $added = 0; + break; + default: + messages_set( + 'notice', + 'version_match', + __('Version check'), + 'Unknown version part: ' . htmlspecialchars($matches[6]) + ); + $added = 0; + break; + } + } else { + $added = 50; // for final + } + if (!empty($matches[7])) { + $added = $added + $matches[7]; + } + return $matches[1] * 1000000 + $matches[2] * 10000 + $matches[3] * 100 + $added; + } } ?> diff --git a/setup/lib/index.lib.php b/setup/lib/index.lib.php index 4a7ab40dab..1d930becff 100644 --- a/setup/lib/index.lib.php +++ b/setup/lib/index.lib.php @@ -123,7 +123,7 @@ function PMA_version_check() $version = $version_data->version; $date = $version_data->date; - $version_upstream = version_to_int($version); + $version_upstream = PMA_Util::versionToInt($version); if ($version_upstream === false) { messages_set( 'error', @@ -134,7 +134,7 @@ function PMA_version_check() return; } - $version_local = version_to_int($GLOBALS['PMA_Config']->get('PMA_VERSION')); + $version_local = PMA_Util::versionToInt($GLOBALS['PMA_Config']->get('PMA_VERSION')); if ($version_local === false) { messages_set( 'error', @@ -173,55 +173,6 @@ function PMA_version_check() } } -/** - * Calculates numerical equivalent of phpMyAdmin version string - * - * @param string $version version - * - * @return mixed false on failure, integer on success - */ -function version_to_int($version) -{ - $matches = array(); - if (!preg_match('/^(\d+)\.(\d+)\.(\d+)((\.|-(pl|rc|dev|beta|alpha))(\d+)?(-dev)?)?$/', $version, $matches)) { - return false; - } - if (!empty($matches[6])) { - switch ($matches[6]) { - case 'pl': - $added = 60; - break; - case 'rc': - $added = 30; - break; - case 'beta': - $added = 20; - break; - case 'alpha': - $added = 10; - break; - case 'dev': - $added = 0; - break; - default: - messages_set( - 'notice', - 'version_match', - __('Version check'), - 'Unknown version part: ' . htmlspecialchars($matches[6]) - ); - $added = 0; - break; - } - } else { - $added = 50; // for final - } - if (!empty($matches[7])) { - $added = $added + $matches[7]; - } - return $matches[1] * 1000000 + $matches[2] * 10000 + $matches[3] * 100 + $added; -} - /** * Checks whether config file is readable/writable * diff --git a/test/classes/PMA_Util_test.php b/test/classes/PMA_Util_test.php index b18299fcc6..322c36daec 100644 --- a/test/classes/PMA_Util_test.php +++ b/test/classes/PMA_Util_test.php @@ -112,4 +112,51 @@ class PMA_Util_Test extends PHPUnit_Framework_TestCase $this->assertNotEmpty($version->version); $this->assertNotEmpty($version->date); } + + /** + * Test version to int conversion. + * + * @param string $version Version string + * @param int $numberic Integer matching version + * + * @return void + * + * @dataProvider dataVersions + */ + public function testVersionToInt($version, $numeric) + { + $this->assertEquals( + $numeric, + PMA_Util::versionToInt($version) + ); + } + + /** + * Data provider for version parsing + * + * @return array with test data + */ + public function dataVersions() + { + return array( + array('1.0.0', 1000050), + array('2.0.0.2-dev', 2000052), + array('3.4.2.1', 3040251), + array('3.4.2-dev3', 3040203), + array('3.4.2-dev', 3040200), + array('3.4.2-pl', 3040260), + array('3.4.2-pl3', 3040263), + array('4.4.2-rc22', 4040252), + array('4.4.2-rc', 4040230), + array('4.4.22-beta22', 4042242), + array('4.4.22-beta', 4042220), + array('4.4.21-alpha22', 4042132), + array('4.4.20-alpha', 4042010), + array('4.40.20-alpha-dev', 4402010), + array('4.4a', false), + array('4.4.4-test', false), + array('4.1.0', 4010050), + ); + } + } diff --git a/test/libraries/PMA_Index_test.php b/test/libraries/PMA_Index_test.php index af754c370a..77ac1a5bac 100644 --- a/test/libraries/PMA_Index_test.php +++ b/test/libraries/PMA_Index_test.php @@ -23,7 +23,7 @@ class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase { /** * Test for messages_begin() - * + * * @return void */ public function testMessagesBegin() @@ -70,7 +70,7 @@ class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase /** * Test for messages_set - * + * * @return void */ public function testMessagesSet() @@ -90,7 +90,7 @@ class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase /** * Test for messages_end - * + * * @return void */ public function testMessagesEnd() @@ -119,7 +119,7 @@ class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase /** * Test for messages_show_html - * + * * @return void */ public function testMessagesShowHTML() @@ -149,7 +149,7 @@ class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase '