Merge pull request #302 from marc1706/ticket/301

[ticket/301] Improve coverage of phpbb_fetch_posts() to 100%
This commit is contained in:
Marc Alexander
2014-07-09 22:32:02 +02:00
4 changed files with 192 additions and 23 deletions

View File

@@ -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)

View File

@@ -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',
));

View File

@@ -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);
}
}

View File

@@ -1,5 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_acl_options">
<column>auth_option_id</column>
<column>auth_option</column>
<column>is_global</column>
<column>is_local</column>
<column>founder_only</column>
<row>
<value>93</value>
<value>u_download</value>
<value>1</value>
<value>0</value>
<value>0</value>
</row>
<row>
<value>7</value>
<value>f_download</value>
<value>0</value>
<value>1</value>
<value>0</value>
</row>
</table>
<table name="phpbb_attachments">
<column>attach_id</column>
<column>post_msg_id</column>
<column>topic_id</column>
<column>poster_id</column>
<column>physical_filename</column>
<column>real_filename</column>
<column>attach_comment</column>
<row>
<value>1</value>
<value>1</value>
<value>1</value>
<value>2</value>
<value>foobar</value>
<value>foobar</value>
<value>foobar</value>
</row>
</table>
<table name="phpbb_topics">
<column>forum_id</column>
<column>topic_id</column>
@@ -25,14 +64,14 @@
<row>
<value>1</value>
<value>1</value>
<value>1</value>
<value>2</value>
<value>1404830663</value>
<value>1404830663</value>
<value>test post</value>
<value>0</value>
<value>1</value>
<value></value>
<value>1</value>
<value>2</value>
<value>0</value>
<value>0</value>
<value>2</value>
@@ -64,6 +103,20 @@
<value>2</value>
<value>1404830663</value>
<value>foobar post</value>
<value>1</value>
<value>foobar</value>
<value>1</value>
<value>1</value>
<value>1</value>
<value></value>
<value></value>
<value>1</value>
</row>
<row>
<value>2</value>
<value>2</value>
<value>1404830663</value>
<value>foobar post</value>
<value>0</value>
<value>foobar</value>
<value>1</value>
@@ -80,12 +133,14 @@
<column>forum_parents</column>
<column>forum_desc</column>
<column>forum_rules</column>
<column>forum_type</column>
<row>
<value>1</value>
<value>0</value>
<value></value>
<value></value>
<value></value>
<value>1</value>
</row>
<row>
<value>2</value>
@@ -93,6 +148,7 @@
<value></value>
<value></value>
<value></value>
<value>1</value>
</row>
</table>
<table name="phpbb_topics_posted">