From 4e1bce320e545e16b755e420d766a1e8207f1ee1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 20 Sep 2014 01:39:23 +0200 Subject: [PATCH] [ticket/351] Use version_helper and json version file for version check B3P-351 --- config/services.yml | 9 +- includes/version_check.php | 127 ++++++-------------- tests/unit/functions/version_check_test.php | 69 ++++++++--- 3 files changed, 90 insertions(+), 115 deletions(-) diff --git a/config/services.yml b/config/services.yml index 524127d3..aee298b3 100644 --- a/config/services.yml +++ b/config/services.yml @@ -5,12 +5,11 @@ parameters: board3.version_data: author: Marc title: Board3 Portal - tag: board3_portal_v2_dev version: board3_portal_version file: - board3.de - - updatecheck - - board3_portal.xml + - /updatecheck + - board3_portal.json board3.portal.config.table: %core.table_prefix%portal_config board3.portal.modules.table: %core.table_prefix%portal_modules @@ -42,11 +41,11 @@ services: board3.portal.version.check: class: board3\portal\includes\version_check + scope: prototype arguments: - %board3.version_data% - @config - - %core.root_path% - - %core.php_ext% + - @version_helper - @template - @user diff --git a/includes/version_check.php b/includes/version_check.php index f34b5c5f..04716df7 100644 --- a/includes/version_check.php +++ b/includes/version_check.php @@ -22,14 +22,9 @@ class version_check protected $config; /** - * @var string phpbb_root_path + * @var \phpbb\version_helper $version_helper phpBB version helper */ - protected $phpbb_root_path; - - /** - * @var string PHP file extension - */ - protected $php_ext; + protected $version_helper; /** * @var \phpbb\template\twig\twig @@ -41,125 +36,75 @@ class version_check */ protected $user; + /** + * @var string Current version + */ + protected $current_version; + /** * Construct a version_check object * * @param array $version_data Version data * @param \phpbb\config\config $config phpBB config - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension + * @param \phpbb\version_helper $version_helper phpBB version helper * @param \phpbb\template\twig\twig $template phpBB template object * @param \phpbb\user $user phpBB user object */ - public function __construct($version_data, $config, $phpbb_root_path, $php_ext, $template, $user) + public function __construct($version_data, $config, $version_helper, $template, $user) { $this->version_data = $version_data; $this->config = $config; - $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $php_ext; + $this->version_helper = $version_helper; $this->template = $template; $this->user = $user; + $this->current_version = $this->config[str_replace(' ', '', $this->version_data['version'])]; } /** - * Check MOD version + * Check MOD version and assign template variables for version info if not + * returning current version * * @param bool $return_version Yes if current version should be returned * @return string Current version if $return_version is set to true */ public function check($return_version = false) { - if (!function_exists('get_remote_file')) - { - include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext); - } + // Set file location + $this->version_helper->set_file_location($this->version_data['file'][0], $this->version_data['file'][1], $this->version_data['file'][2]); + // Set current version + $this->version_helper->set_current_version($this->current_version); - // Fill with bogus data - $this->get_empty_data($mod_version, $data); + $this->version_helper->force_stability(($this->config['extension_force_unstable'] || !$this->version_helper->is_stable($this->current_version)) ? 'unstable' : null); - // Get version info from server - $this->get_version_info($mod_version, $data); + $updates = $this->version_helper->get_suggested_updates(true); - // remove spaces from the version in the mod file stored locally - $version = $this->config[str_replace(' ', '', $this->version_data['version'])]; + // Return version if $return_version is set to true if ($return_version) { - return $version; + return $this->current_version; } - $version_compare = (version_compare($version, $mod_version, '<')) ? false : true; + $version_up_to_date = empty($updates); - $this->template->assign_block_vars('mods', array( - 'ANNOUNCEMENT' => (string) $data['announcement'], + $template_data = array( 'AUTHOR' => $this->version_data['author'], - 'CURRENT_VERSION' => $version, - 'DESCRIPTION' => (string) $data['description'], - 'DOWNLOAD' => (string) $data['download'], - 'LATEST_VERSION' => $mod_version, - 'TITLE' => (string) $data['title'], - - 'UP_TO_DATE' => sprintf((!$version_compare) ? $this->user->lang['NOT_UP_TO_DATE'] : $this->user->lang['UP_TO_DATE'], $data['title']), - - 'S_UP_TO_DATE' => $version_compare, - + 'CURRENT_VERSION' => $this->current_version, + 'UP_TO_DATE' => sprintf((!$version_up_to_date) ? $this->user->lang['NOT_UP_TO_DATE'] : $this->user->lang['UP_TO_DATE'], $this->version_data['title']), + 'S_UP_TO_DATE' => $version_up_to_date, 'U_AUTHOR' => 'http://www.phpbb.com/community/memberlist.php?mode=viewprofile&un=' . $this->version_data['author'], - )); - } - - /** - * Fill variables with empty bogus data - * - * @param string $mod_version Mod version - * @param array $data Array containing mod info - * - * @return null - */ - protected function get_empty_data(&$mod_version, &$data) - { - // Fill with bogus data - $mod_version = $this->user->lang['NO_INFO']; - $data = array( - 'title' => $this->version_data['title'], - 'description' => $this->user->lang['NO_INFO'], - 'download' => $this->user->lang['NO_INFO'], - 'announcement' => $this->user->lang['NO_INFO'], + 'TITLE' => (string) $this->version_data['title'], + 'LATEST_VERSION' => $this->current_version, ); - } - /** - * Get version info from remote server - * - * @param string $mod_version Mod version - * @param array $data Array containing mod info - * - * @return null - */ - protected function get_version_info(&$mod_version, &$data) - { - // Get current and latest version - $errstr = ''; - $errno = 0; - $var = $this->version_data; - - $file = get_remote_file($this->version_data['file'][0], '/' . $this->version_data['file'][1], $this->version_data['file'][2], $errstr, $errno); - - if ($file) + if (!$version_up_to_date) { - // let's not stop the page from loading if a mod author messed up their mod check file - // also take care of one of the easiest ways to mess up an xml file: "&" - $mod = @simplexml_load_string(str_replace('&', '&', $file)); - if (isset($mod->$var['tag'])) - { - $row = $mod->$var['tag']; - $mod_version = $row->mod_version->major . '.' . $row->mod_version->minor . '.' . $row->mod_version->revision . $row->mod_version->release; - - $data = array( - 'title' => $row->title, - 'description' => $row->description, - 'download' => $row->download, - 'announcement' => $row->announcement, - ); - } + $updates = array_shift($updates); + $template_data = array_merge($template_data, array( + 'ANNOUNCEMENT' => (string) $updates['announcement'], + 'DOWNLOAD' => (string) $updates['download'], + 'LATEST_VERSION' => $updates['current'], + )); } + $this->template->assign_block_vars('mods', $template_data); } } diff --git a/tests/unit/functions/version_check_test.php b/tests/unit/functions/version_check_test.php index 79be8081..55da6162 100644 --- a/tests/unit/functions/version_check_test.php +++ b/tests/unit/functions/version_check_test.php @@ -12,40 +12,71 @@ require_once(dirname(__FILE__) . '/../../../../../../includes/utf/utf_tools.php' class phpbb_functions_version_check_test extends \board3\portal\tests\testframework\test_case { protected $version_check; + protected $template; + protected $config; public function setUp() { global $phpbb_root_path, $phpEx; - $this->template = new \board3\portal\tests\mock\template($this); + include_once($phpbb_root_path . 'includes/functions.' . $phpEx); + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - $version_data = array( - 'author' => 'Saint_hh', - 'title' => 'Board3 Portal', - 'tag' => 'board3_portal_v2_dev', + $this->version_data = array( + 'author' => 'Marc', 'version' => 'board3_portal_version', - 'file' => array('board3.de', 'updatecheck', 'board3_portal.xml'), + 'title' => 'Board3 Portal', + 'file' => array('board3.de', '/updatecheck', 'board3_portal.json'), ); - $config = array('board3_portal_version' => '2.1.0'); - $user = new \board3\portal\tests\mock\user; - $user->set(array( + $this->config = new \phpbb\config\config(array()); + $this->user = new \board3\portal\tests\mock\user; + $this->user->set(array( 'NO_INFO' => 'NO_INFO', 'NOT_UP_TO_DATE' => 'NOT_UP_TO_DATE', 'UP_TO_DATE' => 'UP_TO_DATE', )); - $this->version_check = new \board3\portal\includes\version_check($version_data, $config, $phpbb_root_path, $phpEx, $this->template, $user); + $this->cache = $this->getMockBuilder('\phpbb\cache\service') + ->disableOriginalConstructor() + ->getMock(); } - public function test_version_check() + protected function get_version_helper($version) { - $this->assertTrue(true); + $this->config->set('board3_portal_version', $version); + + $this->template = new \board3\portal\tests\mock\template($this); + $version_helper = new \phpbb\version_helper($this->cache, $this->config, new \phpbb\user('\phpbb\datetime')); + $this->version_check = new \board3\portal\includes\version_check($this->version_data, $this->config, $version_helper, $this->template, $this->user); + } + + public function data_version_check() + { + return array( + array('2.1.0', array( + 'CURRENT_VERSION' => '2.1.0', + 'TITLE' => 'Board3 Portal', + 'UP_TO_DATE' => 'UP_TO_DATE', + 'S_UP_TO_DATE' => true, + 'LATEST_VERSION' => '2.1.0', + )), + array('2.1.0-a1', array( + 'CURRENT_VERSION' => '2.1.0-a1', + 'TITLE' => 'Board3 Portal', + 'UP_TO_DATE' => 'NOT_UP_TO_DATE', + 'S_UP_TO_DATE' => false, + 'LATEST_VERSION' => '2.1.0-b1', + )), + ); + } + + /** + * @dataProvider data_version_check + */ + public function test_version_up_to_date($version, $template_data) + { + $this->get_version_helper($version); + $this->assertEquals($version, $this->version_check->check(true)); $this->assertNull($this->version_check->check()); - $this->assertEquals('2.1.0', $this->version_check->check(true)); - $this->template->assert_equals(array( - 'CURRENT_VERSION' => '2.1.0', - 'TITLE' => 'Board3 Portal', - 'UP_TO_DATE' => 'UP_TO_DATE', - 'S_UP_TO_DATE' => true, - ), 'mods'); + $this->template->assert_equals($template_data, 'mods'); } }