From d41bd0eab9713e0312fe1a02b7966a265993677a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 24 Jul 2014 19:18:25 +0200 Subject: [PATCH] [ticket/289] Move store_selected_forums to modules_helper --- acp/portal_module.php | 20 ++++++++++----- config/services.yml | 1 + includes/modules_helper.php | 27 ++++++++++++++++++++- modules/announcements.php | 18 +------------- modules/attachments.php | 18 +------------- modules/news.php | 21 +--------------- modules/poll.php | 21 +--------------- modules/recent.php | 20 +-------------- tests/unit/acp/move_module_test.php | 4 +-- tests/unit/functions/fetch_news_test.php | 3 ++- tests/unit/includes/modules_helper_test.php | 14 +++++++++-- tests/unit/portal/fetch_posts_test.php | 3 ++- 12 files changed, 64 insertions(+), 106 deletions(-) diff --git a/acp/portal_module.php b/acp/portal_module.php index 85ec7ce4..8309bb9c 100644 --- a/acp/portal_module.php +++ b/acp/portal_module.php @@ -182,18 +182,26 @@ class portal_module { if ($submit && ((isset($null['type']) && $null['type'] == 'custom') || (isset($null['submit_type']) && $null['submit_type'] == 'custom'))) { - $func = array($this->c_class, $null['submit']); - - if(method_exists($this->c_class, $null['submit'])) + if (!is_array($null['submit'])) { + $func = array($this->c_class, $null['submit']); $args = ($module_id != 0) ? array($config_name, $module_id) : $config_name; - call_user_func_array($func, $args); } else { - $args = ($module_id != 0) ? array($cfg_array[$config_name], $config_name, $module_id) : $config_name; - call_user_func_array($null['submit'], $args); + if ($null['submit'][0] == 'board3.portal.modules_helper') + { + $func = array($this->modules_helper, $null['submit'][1]); + $args = ($module_id != 0) ? array($config_name, $module_id) : $config_name; + } + else + { + $args = ($module_id != 0) ? array($cfg_array[$config_name], $config_name, $module_id) : $config_name; + $func = $null['submit']; + } } + + call_user_func_array($func, $args); } if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false) diff --git a/config/services.yml b/config/services.yml index a84e42e4..597aefe6 100644 --- a/config/services.yml +++ b/config/services.yml @@ -56,6 +56,7 @@ services: arguments: - @auth - @config + - @request board3.portal.fetch_posts: class: board3\portal\portal\fetch_posts diff --git a/includes/modules_helper.php b/includes/modules_helper.php index fcc9d6fa..0a5d7673 100644 --- a/includes/modules_helper.php +++ b/includes/modules_helper.php @@ -23,17 +23,25 @@ class modules_helper */ protected $config; + /** + * phpBB request + * @var \phpbb\request\request + */ + protected $request; + /** * Constructor * NOTE: The parameters of this method must match in order and type with * the dependencies defined in the services.yml file for this service. * @param \phpbb\auth\auth $auth Auth object * @param \phpbb\config\config $config phpBB config + * @param \phpbb\request\request $request phpBB request */ - public function __construct($auth, $config) + public function __construct($auth, $config, $request) { $this->auth = $auth; $this->config = $config; + $this->request = $request; } /** @@ -111,4 +119,21 @@ class modules_helper return $this->generate_select_box($key, $select_ary, $selected_options); } + + /** + * Store selected forums + * + * @param string $key Key name + * @param int $module_id Module ID + * + * @return null + * @access public + */ + public function store_selected_forums($key) + { + // Get selected extensions + $values = $this->request->variable($key, array(0 => '')); + $news = implode(',', $values); + $this->config->set($key, $news); + } } diff --git a/modules/announcements.php b/modules/announcements.php index 09ad54df..3edb1169 100644 --- a/modules/announcements.php +++ b/modules/announcements.php @@ -457,7 +457,7 @@ class announcements extends module_base 'board3_number_of_announcements_' . $module_id => array('lang' => 'PORTAL_NUMBER_OF_ANNOUNCEMENTS' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_announcements_day_' . $module_id => array('lang' => 'PORTAL_ANNOUNCEMENTS_DAY' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_announcements_length_' . $module_id => array('lang' => 'PORTAL_ANNOUNCEMENTS_LENGTH' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), - 'board3_global_announcements_forum_' . $module_id => array('lang' => 'PORTAL_GLOBAL_ANNOUNCEMENTS_FORUM' , 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => 'store_selected_forums'), + 'board3_global_announcements_forum_' . $module_id => array('lang' => 'PORTAL_GLOBAL_ANNOUNCEMENTS_FORUM' , 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => array('board3.portal.modules_helper', 'store_selected_forums')), 'board3_announcements_forum_exclude_' . $module_id => array('lang' => 'PORTAL_ANNOUNCEMENTS_FORUM_EXCLUDE', 'validate' => 'string', 'type' => 'radio:yes_no', 'explain' => true), 'board3_announcements_archive_' . $module_id => array('lang' => 'PORTAL_ANNOUNCEMENTS_ARCHIVE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_announcements_permissions_' . $module_id => array('lang' => 'PORTAL_ANNOUNCEMENTS_PERMISSIONS' , 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -504,20 +504,4 @@ class announcements extends module_base WHERE ' . $db->sql_in_set('config_name', $del_config); return $db->sql_query($sql); } - - /** - * Store selected forums - * - * @param string $key Key name - * @param int $module_id Module ID - * - * @return null - */ - public function store_selected_forums($key, $module_id) - { - // Get selected forums - $values = $this->request->variable($key, array(0 => '')); - $news = implode(',', $values); - set_config($key, $news); - } } diff --git a/modules/attachments.php b/modules/attachments.php index 1c0d2057..e7a0acf1 100644 --- a/modules/attachments.php +++ b/modules/attachments.php @@ -121,7 +121,7 @@ class attachments extends module_base 'legend1' => 'ACP_PORTAL_ATTACHMENTS_NUMBER_SETTINGS', 'board3_attachments_number_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_NUMBER' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_attach_max_length_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_MAX_LENGTH' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), - 'board3_attachments_forum_ids_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_FORUM_IDS', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => 'store_selected_forums'), + 'board3_attachments_forum_ids_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_FORUM_IDS', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => array('board3.portal.modules_helper', 'store_selected_forums')), 'board3_attachments_forum_exclude_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_FORUM_EXCLUDE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_attachments_filetype_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_FILETYPE', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => 'select_filetype', 'submit' => 'store_filetypes'), 'board3_attachments_exclude_' . $module_id => array('lang' => 'PORTAL_ATTACHMENTS_EXCLUDE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -212,22 +212,6 @@ class attachments extends module_base } - /** - * Store selected forums - * - * @param string $key Key name - * @param int $module_id Module ID - * - * @return null - */ - public function store_selected_forums($key) - { - // Get selected extensions - $values = $this->request->variable($key, array(0 => '')); - $news = implode(',', $values); - set_config($key, $news); - } - /** * Parse template variables for module * diff --git a/modules/news.php b/modules/news.php index 13d9c95a..8e12e536 100644 --- a/modules/news.php +++ b/modules/news.php @@ -436,7 +436,7 @@ class news extends module_base 'board3_show_all_news_' . $module_id => array('lang' => 'PORTAL_SHOW_ALL_NEWS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_number_of_news_' . $module_id => array('lang' => 'PORTAL_NUMBER_OF_NEWS', 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_news_length_' . $module_id => array('lang' => 'PORTAL_NEWS_LENGTH', 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), - 'board3_news_forum_' . $module_id => array('lang' => 'PORTAL_NEWS_FORUM', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => 'store_selected_forums'), + 'board3_news_forum_' . $module_id => array('lang' => 'PORTAL_NEWS_FORUM', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => array('board3.portal.modules_helper', 'store_selected_forums')), 'board3_news_exclude_' . $module_id => array('lang' => 'PORTAL_NEWS_EXCLUDE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_news_show_last_' . $module_id => array('lang' => 'PORTAL_NEWS_SHOW_LAST', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_news_archive_' . $module_id => array('lang' => 'PORTAL_NEWS_ARCHIVE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -485,23 +485,4 @@ class news extends module_base WHERE ' . $db->sql_in_set('config_name', $del_config); return $db->sql_query($sql); } - - /** - * Store selected forums - * - * @param string $key Key name - * @param int $module_id Module ID - * - * @return null - */ - public function store_selected_forums($key, $module_id) - { - // Get selected extensions - $values = $this->request->variable($key, array(0 => '')); - - $news = implode(',', $values); - - set_config($key, $news); - - } } diff --git a/modules/poll.php b/modules/poll.php index 2e09f126..c4fe9d0b 100644 --- a/modules/poll.php +++ b/modules/poll.php @@ -120,7 +120,7 @@ class poll extends module_base 'title' => 'ACP_PORTAL_POLLS_SETTINGS', 'vars' => array( 'legend1' => 'ACP_PORTAL_POLLS_SETTINGS', - 'board3_poll_topic_id_' . $module_id => array('lang' => 'PORTAL_POLL_TOPIC_ID' , 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => 'store_selected_forums'), + 'board3_poll_topic_id_' . $module_id => array('lang' => 'PORTAL_POLL_TOPIC_ID' , 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => array('board3.portal.modules_helper', 'store_selected_forums')), 'board3_poll_exclude_id_' . $module_id => array('lang' => 'PORTAL_POLL_EXCLUDE_ID' , 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'board3_poll_limit_' . $module_id => array('lang' => 'PORTAL_POLL_LIMIT' , 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_poll_allow_vote_' . $module_id => array('lang' => 'PORTAL_POLL_ALLOW_VOTE' , 'validate' => 'ibool', 'type' => 'radio:yes_no', 'explain' => true), @@ -159,25 +159,6 @@ class poll extends module_base return $db->sql_query($sql); } - /** - * Store selected forums - * - * @param string $key Key name - * @param int $module_id Module ID - * - * @return null - */ - public function store_selected_forums($key, $module_id) - { - // Get selected forums - $values = $this->request->variable($key, array(0 => '')); - - $news = implode(',', $values); - - set_config($key, $news); - - } - /** * Parse template variables for module * diff --git a/modules/recent.php b/modules/recent.php index ebad0736..2ff0f7d4 100644 --- a/modules/recent.php +++ b/modules/recent.php @@ -219,7 +219,7 @@ class recent extends module_base 'legend1' => 'ACP_PORTAL_RECENT_SETTINGS', 'board3_max_topics_' . $module_id => array('lang' => 'PORTAL_MAX_TOPIC', 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), 'board3_recent_title_limit_' . $module_id => array('lang' => 'PORTAL_RECENT_TITLE_LIMIT', 'validate' => 'int', 'type' => 'text:3:3', 'explain' => true), - 'board3_recent_forum_' . $module_id => array('lang' => 'PORTAL_RECENT_FORUM', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => 'store_selected_forums'), + 'board3_recent_forum_' . $module_id => array('lang' => 'PORTAL_RECENT_FORUM', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => array('board3.portal.modules_helper', 'generate_forum_select'), 'submit' => array('board3.portal.modules_helper', 'store_selected_forums')), 'board3_recent_exclude_forums_' . $module_id => array('lang' => 'PORTAL_EXCLUDE_FORUM', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), ) ); @@ -252,22 +252,4 @@ class recent extends module_base WHERE ' . $db->sql_in_set('config_name', $del_config); return $db->sql_query($sql); } - - /** - * Store selected forums - * - * @param string $key Key name - * @param int $module_id Module ID - * - * @return null - */ - public function store_selected_forums($key, $module_id) - { - // Get selected extensions - $values = $this->request->variable($key, array(0 => '')); - - $news = implode(',', $values); - - set_config($key, $news); - } } diff --git a/tests/unit/acp/move_module_test.php b/tests/unit/acp/move_module_test.php index 5a1e49b8..060b40bf 100644 --- a/tests/unit/acp/move_module_test.php +++ b/tests/unit/acp/move_module_test.php @@ -23,6 +23,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data parent::setUp(); global $db, $cache, $phpbb_root_path, $phpEx, $user, $phpbb_container, $request, $template, $table_prefix; $user = new \board3\portal\tests\mock\user(); + $request = new \phpbb_mock_request; $phpbb_container = new \phpbb_mock_container_builder(); // Mock version check $phpbb_container->set('board3.version.check', @@ -39,7 +40,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data new \board3\portal\modules\donation($config, $template, $user), )); $phpbb_container->set('board3.portal.helper', new \board3\portal\includes\helper($phpbb_container->get('board3.module_collection'))); - $phpbb_container->set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), $config)); + $phpbb_container->set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), $config, $request)); $phpbb_container->setParameter('board3.modules.table', $table_prefix . 'portal_modules'); $phpbb_container->setParameter('board3.config.table', $table_prefix . 'portal_config'); $cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put')); @@ -61,7 +62,6 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data 'UNABLE_TO_MOVE' => 'UNABLE_TO_MOVE', 'UNABLE_TO_MOVE_ROW' => 'UNABLE_TO_MOVE_ROW', )); - $request = new \phpbb_mock_request; $this->portal_module = new \board3\portal\acp\portal_module(); $this->update_portal_modules(); } diff --git a/tests/unit/functions/fetch_news_test.php b/tests/unit/functions/fetch_news_test.php index 1c5a592f..6ce4f81f 100644 --- a/tests/unit/functions/fetch_news_test.php +++ b/tests/unit/functions/fetch_news_test.php @@ -27,6 +27,7 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework $user->timezone = new \DateTimeZone('UTC'); $user->add_lang('common'); $user->add_lang('../../ext/board3/portal/language/en/portal'); + $request = new \phpbb_mock_request; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $cache = $this->getMock('\phpbb\cache\cache', array('obtain_word_list', 'get', 'sql_exists', 'put', 'obtain_attach_extensions')); $cache->expects($this->any()) @@ -51,7 +52,7 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework $this->auth = $auth; $this->user = $user; $phpbb_container = new \phpbb_mock_container_builder(); - $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config); + $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $request); $phpbb_container->set('board3.portal.modules_helper', $this->modules_helper); $phpbb_container->set('board3.portal.fetch_posts', new \board3\portal\portal\fetch_posts($auth, $cache, $this->config, $this->db, $this->modules_helper, $user)); $template = $this->getMock('\phpbb\template', array('set_filenames', 'destroy_block_vars', 'assign_block_vars', 'assign_display')); diff --git a/tests/unit/includes/modules_helper_test.php b/tests/unit/includes/modules_helper_test.php index d7ed2e94..03b05134 100644 --- a/tests/unit/includes/modules_helper_test.php +++ b/tests/unit/includes/modules_helper_test.php @@ -14,6 +14,8 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe protected $modules; + protected $config; + public function getDataSet() { return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/auth.xml'); @@ -24,9 +26,10 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe parent::setUp(); $auth = new \phpbb\auth\auth(); - $config = new \phpbb\config\config(array()); + $this->config = new \phpbb\config\config(array()); + $request = new \phpbb_mock_request(array('foo' => array('bar'))); - $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $config); + $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $request); } public function data_get_disallowed_forums() @@ -92,4 +95,11 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe $this->modules_helper->generate_forum_select('foo', 'bar') ); } + + public function test_store_selected_forums() + { + $this->assertEmpty($this->config['foo']); + $this->modules_helper->store_selected_forums('foo'); + $this->assertEquals('bar', $this->config['foo']); + } } diff --git a/tests/unit/portal/fetch_posts_test.php b/tests/unit/portal/fetch_posts_test.php index 4e2ff7c0..45c988ca 100644 --- a/tests/unit/portal/fetch_posts_test.php +++ b/tests/unit/portal/fetch_posts_test.php @@ -50,7 +50,8 @@ class phpbb_portal_fetch_posts_test extends \board3\portal\tests\testframework\d // Pretend to allow downloads in forum 1 $auth->acl[1][0] = true; $this->auth = $auth; - $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config); + $request = new \phpbb_mock_request; + $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $request); $this->user = $user; $template = $this->getMock('\phpbb\template', array('set_filenames', 'destroy_block_vars', 'assign_block_vars', 'assign_display')); $this->fetch_posts = new \board3\portal\portal\fetch_posts($auth, $cache, $this->config, $this->db, $this->modules_helper, $user);