diff --git a/includes/functions.php b/includes/functions.php index 3dc643ae..b09cfd5a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -166,9 +166,8 @@ function phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_pos break; default: - $topic_type = $str_where = $user_link = $post_link = ''; - $topic_order = 't.topic_time DESC'; - // maybe use trigger_error here, as this shouldn't happen + // Method was called with unsupported type + throw new \InvalidArgumentexception($user->lang('B3P_WRONG_METHOD_CALL', __FUNCTION__)); } if ($type == 'announcements' && $global_f < 1) diff --git a/language/en/portal.php b/language/en/portal.php index 74b10f48..b4a8e963 100644 --- a/language/en/portal.php +++ b/language/en/portal.php @@ -37,4 +37,5 @@ $lang = array_merge($lang, array( 'PORTAL' => 'Portal', 'VIEWING_PORTAL' => 'Portal page', 'BACK' => 'Back', + 'B3P_WRONG_METHOD_CALL' => 'Incorrect call to method %s', )); diff --git a/tests/unit/functions/fetch_news_test.php b/tests/unit/functions/fetch_news_test.php index 21949e14..dc70be8f 100644 --- a/tests/unit/functions/fetch_news_test.php +++ b/tests/unit/functions/fetch_news_test.php @@ -10,29 +10,47 @@ require_once(dirname(__FILE__) . '/../../../includes/functions.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions_acp.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions.php'); +require_once(dirname(__FILE__) . '/../../../../../../includes/utf/utf_tools.php'); class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework\database_test_case { + protected $default_main_columns = array('topic_count', 'global_id', 'topic_icons'); + public function setUp() { parent::setUp(); - global $auth, $cache, $phpbb_container, $phpbb_dispatcher, $user; + global $auth, $cache, $config, $phpbb_container, $phpbb_dispatcher, $template, $user; - $auth = new \phpbb\auth\auth(); - $phpbb_container = new \phpbb_mock_container_builder(); - $phpbb_container->set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper($auth)); $user = new \phpbb\user(); $user->data['user_id'] = 2; $user->timezone = new \DateTimeZone('UTC'); $user->add_lang('common'); + $user->add_lang('../../ext/board3/portal/language/en/portal'); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); - $cache = $this->getMock('\phpbb\cache\cache', array('obtain_word_list', 'get', 'sql_exists')); + $cache = $this->getMock('\phpbb\cache\cache', array('obtain_word_list', 'get', 'sql_exists', 'put', 'obtain_attach_extensions')); $cache->expects($this->any()) ->method('obtain_word_list') ->with() ->will($this->returnValue(array())); + $cache->expects($this->any()) + ->method('get') + ->with($this->anything()) + ->will($this->returnValue(false)); require_once(dirname(__FILE__) . '/../../../../../../includes/functions_content.php'); + $config = new \phpbb\config\config(array('allow_attachments' => 1)); + $auth = new \phpbb\auth\auth(); + $userdata = array( + 'user_id' => 2, + ); + $auth->acl($userdata); + // Pretend to allow downloads + $auth->acl[0][0] = true; + // Pretend to allow downloads in forum 1 + $auth->acl[1][0] = true; + $phpbb_container = new \phpbb_mock_container_builder(); + $phpbb_container->set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper($auth)); + $template = $this->getMock('\phpbb\template', array('set_filenames', 'destroy_block_vars', 'assign_block_vars', 'assign_display')); } public function getDataSet() @@ -93,33 +111,96 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework 'attachments', 'forum_name', )), - array('announcements', array(), true), + array('announcements', array(), array('topic_icons', 'topic_count'), 5, ''), + array('news', array(), array(), 0), + array('foobar', array(), array(), 5, '', false, false, false, 150, '\InvalidArgumentException'), + array('news', array( + 'forum_id', + 'topic_id', + 'topic_last_post_time', + 'topic_replies', + 'topic_replies_real', + 'topic_type', + 'topic_status', + 'topic_posted', + 'attachment', + 'forum_name', + 'topic_title', + 'username', + 'username_full', + 'username_full_last', + 'user_type', + 'user_id', + 'topic_time', + 'post_text', + 'topic_views', + 'icon_id', + 'poll', + 'attachments', + 'forum_name', + ), array(), 5, '', false, true), + array('announcements', array(), array('topic_icons', 'topic_count'), 5, '3'), + array('announcements', array(), array('topic_icons', 'topic_count'), 5, '1,2', false, true), + array('news', array(), array(), 5, '1,2', true, false, true), + array('announcements', array(), array('topic_icons', 'topic_count'), 5, '', false, true), + array('announcements', array(), array(), 5, '1,2', true, true, true), + array('news', array( + 'forum_id', + 'topic_id', + 'topic_last_post_time', + 'topic_replies', + 'topic_replies_real', + 'topic_type', + 'topic_status', + 'topic_posted', + 'attachment', + 'forum_name', + 'topic_title', + 'username', + 'username_full', + 'username_full_last', + 'user_type', + 'user_id', + 'topic_time', + 'post_text', + 'topic_views', + 'icon_id', + 'poll', + 'attachments', + 'forum_name', + ), array(), 5, '', false, true, false, 5), ); } /** * @dataProvider data_phpbb_fetch_news */ - public function test_phpbb_fetch_news($type, $expected_columns, $empty = false) + public function test_phpbb_fetch_news($type, $expected_columns, $expected_main_columns = array(), $number_of_posts = 5, $forum_from = '', $empty = false, $permissions = false, + $invert = false, $text_length = 150, $expected_exception = false) { $module_id = 5; - $forum_from = ''; - $permissions = false; - $number_of_posts = 5; - $text_length = 150; $time = time(); + $start = 0; - $fetch_posts = phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type); + if ($expected_exception) + { + $this->setExpectedException($expected_exception); + } + + $fetch_posts = phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start, $invert); if (!$empty) { - $this->assertArrayHasKey('topic_count', $fetch_posts); - $this->assertArrayHasKey('global_id', $fetch_posts); - $this->assertArrayHasKey('topic_icons', $fetch_posts); + if (empty($expected_main_columns)) + { + $expected_main_columns = $this->default_main_columns; + } - unset($fetch_posts['topic_count']); - unset($fetch_posts['global_id']); - unset($fetch_posts['topic_icons']); + foreach ($expected_main_columns as $main_column) + { + $this->assertArrayHasKey($main_column, $fetch_posts); + unset($fetch_posts[$main_column]); + } foreach ($fetch_posts as $post) { @@ -132,7 +213,39 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework } else { + if (!empty($fetch_posts)) + { + var_export($fetch_posts); + } $this->assertEmpty($fetch_posts); } } + + public function test_cached_first_forum_id() + { + global $cache; + + $cache = $this->getMock('\phpbb\cache\cache', array('obtain_word_list', 'get', 'sql_exists', 'put')); + $cache->expects($this->any()) + ->method('obtain_word_list') + ->with() + ->will($this->returnValue(array())); + $cache->expects($this->any()) + ->method('get') + ->with($this->anything()) + ->will($this->returnValue(array())); + + $fetch_posts = phpbb_fetch_posts(5, '', false, 5, 150, time(), 'announcements'); + $this->assertEmpty($fetch_posts); + } + + public function test_no_allowed_forums() + { + global $auth; + + $auth = new \phpbb\auth\auth(); + + $fetch_posts = phpbb_fetch_posts(5, '2', true, 5, 150, time(), 'announcements'); + $this->assertSame(array(), $fetch_posts); + } } diff --git a/tests/unit/functions/fixtures/news.xml b/tests/unit/functions/fixtures/news.xml index 8a7e0f1f..3688ac4c 100644 --- a/tests/unit/functions/fixtures/news.xml +++ b/tests/unit/functions/fixtures/news.xml @@ -1,5 +1,44 @@ + + auth_option_id + auth_option + is_global + is_local + founder_only + + 93 + u_download + 1 + 0 + 0 + + + 7 + f_download + 0 + 1 + 0 + +
+ + attach_id + post_msg_id + topic_id + poster_id + physical_filename + real_filename + attach_comment + + 1 + 1 + 1 + 2 + foobar + foobar + foobar + +
forum_idtopic_id @@ -25,14 +64,14 @@ 1 1 - 1 + 2 1404830663 1404830663 test post 0 1 - 1 + 2 0 0 2 @@ -64,6 +103,20 @@ 2 1404830663 foobar post + 1 + foobar + 1 + 1 + 1 + + + 1 + + + 2 + 2 + 1404830663 + foobar post 0 foobar 1 @@ -80,12 +133,14 @@ forum_parents forum_desc forum_rules + forum_type 1 0 + 1 2 @@ -93,6 +148,7 @@ + 1