Merge pull request #300 from marc1706/ticket/299
[ticket/299] Add tests for phpbb_fetch_posts
This commit is contained in:
138
tests/unit/functions/fetch_news_test.php
Normal file
138
tests/unit/functions/fetch_news_test.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Board3 Portal Testing
|
||||
* @copyright (c) Board3 Group ( www.board3.de )
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
require_once(dirname(__FILE__) . '/../../../includes/functions.php');
|
||||
require_once(dirname(__FILE__) . '/../../../../../../includes/functions_acp.php');
|
||||
require_once(dirname(__FILE__) . '/../../../../../../includes/functions.php');
|
||||
|
||||
class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework\database_test_case
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
global $auth, $cache, $phpbb_container, $phpbb_dispatcher, $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');
|
||||
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
|
||||
$cache = $this->getMock('\phpbb\cache\cache', array('obtain_word_list', 'get', 'sql_exists'));
|
||||
$cache->expects($this->any())
|
||||
->method('obtain_word_list')
|
||||
->with()
|
||||
->will($this->returnValue(array()));
|
||||
require_once(dirname(__FILE__) . '/../../../../../../includes/functions_content.php');
|
||||
}
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/news.xml');
|
||||
}
|
||||
|
||||
public function data_phpbb_fetch_news()
|
||||
{
|
||||
return array(
|
||||
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('news_all', 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('announcements', array(), true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_phpbb_fetch_news
|
||||
*/
|
||||
public function test_phpbb_fetch_news($type, $expected_columns, $empty = false)
|
||||
{
|
||||
$module_id = 5;
|
||||
$forum_from = '';
|
||||
$permissions = false;
|
||||
$number_of_posts = 5;
|
||||
$text_length = 150;
|
||||
$time = time();
|
||||
|
||||
$fetch_posts = phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type);
|
||||
|
||||
if (!$empty)
|
||||
{
|
||||
$this->assertArrayHasKey('topic_count', $fetch_posts);
|
||||
$this->assertArrayHasKey('global_id', $fetch_posts);
|
||||
$this->assertArrayHasKey('topic_icons', $fetch_posts);
|
||||
|
||||
unset($fetch_posts['topic_count']);
|
||||
unset($fetch_posts['global_id']);
|
||||
unset($fetch_posts['topic_icons']);
|
||||
|
||||
foreach ($fetch_posts as $post)
|
||||
{
|
||||
foreach ($expected_columns as $column)
|
||||
{
|
||||
$this->assertArrayHasKey($column, $post);
|
||||
$this->assertNotNull($post[$column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->assertEmpty($fetch_posts);
|
||||
}
|
||||
}
|
||||
}
|
||||
137
tests/unit/functions/fixtures/news.xml
Normal file
137
tests/unit/functions/fixtures/news.xml
Normal file
@@ -0,0 +1,137 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<dataset>
|
||||
<table name="phpbb_topics">
|
||||
<column>forum_id</column>
|
||||
<column>topic_id</column>
|
||||
<column>topic_last_post_id</column>
|
||||
<column>topic_last_post_time</column>
|
||||
<column>topic_time</column>
|
||||
<column>topic_title</column>
|
||||
<column>topic_attachment</column>
|
||||
<column>topic_views</column>
|
||||
<column>poll_title</column>
|
||||
<column>topic_posts_approved</column>
|
||||
<column>topic_posts_unapproved</column>
|
||||
<column>topic_posts_softdeleted</column>
|
||||
<column>topic_poster</column>
|
||||
<column>topic_type</column>
|
||||
<column>topic_status</column>
|
||||
<column>topic_last_poster_name</column>
|
||||
<column>topic_last_poster_id</column>
|
||||
<column>topic_last_poster_colour</column>
|
||||
<column>icon_id</column>
|
||||
<column>topic_visibility</column>
|
||||
<column>topic_first_post_id</column>
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
<value>1404830663</value>
|
||||
<value>1404830663</value>
|
||||
<value>test post</value>
|
||||
<value>0</value>
|
||||
<value>1</value>
|
||||
<value></value>
|
||||
<value>1</value>
|
||||
<value>0</value>
|
||||
<value>0</value>
|
||||
<value>2</value>
|
||||
<value>0</value>
|
||||
<value>0</value>
|
||||
<value>foobar</value>
|
||||
<value>2</value>
|
||||
<value></value>
|
||||
<value>0</value>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
</row>
|
||||
</table>
|
||||
<table name="phpbb_posts">
|
||||
<column>post_id</column>
|
||||
<column>poster_id</column>
|
||||
<column>post_time</column>
|
||||
<column>post_text</column>
|
||||
<column>post_attachment</column>
|
||||
<column>post_username</column>
|
||||
<column>enable_smilies</column>
|
||||
<column>enable_bbcode</column>
|
||||
<column>enable_magic_url</column>
|
||||
<column>bbcode_bitfield</column>
|
||||
<column>bbcode_uid</column>
|
||||
<column>forum_id</column>
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>2</value>
|
||||
<value>1404830663</value>
|
||||
<value>foobar post</value>
|
||||
<value>0</value>
|
||||
<value>foobar</value>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
<value>1</value>
|
||||
</row>
|
||||
</table>
|
||||
<table name="phpbb_forums">
|
||||
<column>forum_id</column>
|
||||
<column>parent_id</column>
|
||||
<column>forum_parents</column>
|
||||
<column>forum_desc</column>
|
||||
<column>forum_rules</column>
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>0</value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
</row>
|
||||
<row>
|
||||
<value>2</value>
|
||||
<value>1</value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
</row>
|
||||
</table>
|
||||
<table name="phpbb_topics_posted">
|
||||
<column>topic_id</column>
|
||||
<column>user_id</column>
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
</row>
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>2</value>
|
||||
</row>
|
||||
</table>
|
||||
<table name="phpbb_users">
|
||||
<column>username</column>
|
||||
<column>username_clean</column>
|
||||
<column>user_id</column>
|
||||
<column>user_type</column>
|
||||
<column>user_colour</column>
|
||||
<column>user_permissions</column>
|
||||
<column>user_sig</column>
|
||||
<row>
|
||||
<value>foobar</value>
|
||||
<value>foobar</value>
|
||||
<value>2</value>
|
||||
<value>5</value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
</row>
|
||||
<row>
|
||||
<value>barfoo</value>
|
||||
<value>barfoo</value>
|
||||
<value>3</value>
|
||||
<value>5</value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
<value></value>
|
||||
</row>
|
||||
</table>
|
||||
</dataset>
|
||||
@@ -48,76 +48,7 @@ class phpbb_unit_functions_functions_test extends \board3\portal\tests\testframe
|
||||
*/
|
||||
public function test_character_limit($expected, $input, $length)
|
||||
{
|
||||
$this->markTestIncomplete('Cannot test this due to issues with censor_text() and truncate_string()');
|
||||
$this->assertSame($expected, character_limit($input, $length));
|
||||
}
|
||||
}
|
||||
|
||||
function censor_text($text)
|
||||
{
|
||||
$text = trim(str_replace('disallowed_word', '', $text));
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates string while retaining special characters if going over the max length
|
||||
* The default max length is 60 at the moment
|
||||
* The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
|
||||
* For example: string given is 'a "quote"' (length: 9), would be a stored as 'a "quote"' (length: 19)
|
||||
*
|
||||
* @param string $string The text to truncate to the given length. String is specialchared.
|
||||
* @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
|
||||
* @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
|
||||
* @param bool $allow_reply Allow Re: in front of string
|
||||
* NOTE: This parameter can cause undesired behavior (returning strings longer than $max_store_length) and is deprecated.
|
||||
* @param string $append String to be appended
|
||||
*/
|
||||
function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = false, $append = '')
|
||||
{
|
||||
$chars = array();
|
||||
|
||||
$strip_reply = false;
|
||||
$stripped = false;
|
||||
if ($allow_reply && strpos($string, 'Re: ') === 0)
|
||||
{
|
||||
$strip_reply = true;
|
||||
$string = substr($string, 4);
|
||||
}
|
||||
|
||||
$_chars = utf8_str_split(htmlspecialchars_decode($string));
|
||||
$chars = array_map('utf8_htmlspecialchars', $_chars);
|
||||
|
||||
// Now check the length ;)
|
||||
if (sizeof($chars) > $max_length)
|
||||
{
|
||||
// Cut off the last elements from the array
|
||||
$string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
|
||||
$stripped = true;
|
||||
}
|
||||
|
||||
// Due to specialchars, we may not be able to store the string...
|
||||
if (utf8_strlen($string) > $max_store_length)
|
||||
{
|
||||
// let's split again, we do not want half-baked strings where entities are split
|
||||
$_chars = utf8_str_split(htmlspecialchars_decode($string));
|
||||
$chars = array_map('utf8_htmlspecialchars', $_chars);
|
||||
|
||||
do
|
||||
{
|
||||
array_pop($chars);
|
||||
$string = implode('', $chars);
|
||||
}
|
||||
while (!empty($chars) && utf8_strlen($string) > $max_store_length);
|
||||
}
|
||||
|
||||
if ($strip_reply)
|
||||
{
|
||||
$string = 'Re: ' . $string;
|
||||
}
|
||||
|
||||
if ($append != '' && $stripped)
|
||||
{
|
||||
$string = $string . $append;
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor
|
||||
new \phpbb_mock_request()
|
||||
),
|
||||
new \phpbb\filesystem(),
|
||||
new \phpbb_mock_request(),
|
||||
$phpbb_root_path,
|
||||
'php'
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user