From 668040b48ef73c179e94cfc16dc232a997b27989 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 16:02:08 +0200 Subject: [PATCH 1/3] [ticket/634] Move get_sub_taged_string to fetch_posts B3P-634 --- includes/functions.php | 21 --------------------- portal/fetch_posts.php | 23 ++++++++++++++++++++++- tests/unit/portal/fetch_posts_test.php | 3 +++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index d049ab018..093b75fd3 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -128,27 +128,6 @@ function character_limit(&$title, $limit = 0) } } -/** -* Cut post text to given length -* -* @param string $message post text -* @param string $bbcode_uid bbcode uid -* @param int $length The desired length -* -* @return string Shortened message -*/ -function get_sub_taged_string($message, $bbcode_uid, $length) -{ - if (class_exists('\Nickvergessen\TrimMessage\TrimMessage')) - { - $trim = new \Nickvergessen\TrimMessage\TrimMessage($message, $bbcode_uid, $length); - $message = $trim->message(); - unset($trim); - } - - return $message; -} - function ap_validate($str) { $s = str_replace('
', '
', $str); diff --git a/portal/fetch_posts.php b/portal/fetch_posts.php index 9616b6053..8931fe3b9 100644 --- a/portal/fetch_posts.php +++ b/portal/fetch_posts.php @@ -627,7 +627,7 @@ protected function format_message($row, $text_length, &$posts_striped) if ($text_length > 0 && (strlen($row['post_text']) > $text_length)) { $message = str_replace(array("\n", "\r"), array('
', "\n"), $row['post_text']); - $message = get_sub_taged_string($message, $row['bbcode_uid'], $text_length); + $message = $this->shorten_message($message, $row['bbcode_uid'], $text_length); $posts_striped = true; } else @@ -637,4 +637,25 @@ protected function format_message($row, $text_length, &$posts_striped) return $message; } + + /** + * Shorten message to specified length + * + * @param string $message Post text + * @param string $bbcode_uid BBCode UID + * @param int $length Length the text should have after shortening + * + * @return string Shortened messsage + */ + public function shorten_message($message, $bbcode_uid, $length) + { + if (class_exists('\Nickvergessen\TrimMessage\TrimMessage')) + { + $trim = new \Nickvergessen\TrimMessage\TrimMessage($message, $bbcode_uid, $length); + $message = $trim->message(); + unset($trim); + } + + return $message; + } } diff --git a/tests/unit/portal/fetch_posts_test.php b/tests/unit/portal/fetch_posts_test.php index 557b77c58..1d23faed3 100644 --- a/tests/unit/portal/fetch_posts_test.php +++ b/tests/unit/portal/fetch_posts_test.php @@ -10,6 +10,9 @@ require_once(dirname(__FILE__) . '/../../../../../../includes/functions_acp.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/utf/utf_tools.php'); +require_once(dirname(__FILE__) . '/../../../vendor/nickvergessen/phpbb-tool-trimmessage/src/Nickvergessen/TrimMessage/TrimMessage.php'); +require_once(dirname(__FILE__) . '/../../../vendor/nickvergessen/phpbb-tool-trimmessage/src/Nickvergessen/TrimMessage/PhpbbBbcodes.php'); + class phpbb_portal_fetch_posts_test extends \board3\portal\tests\testframework\database_test_case { From c3297982f73b8a6c5b3cd9dfae0238646bf2f331 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 16:32:55 +0200 Subject: [PATCH 2/3] [ticket/634] Remove unused sql_table_exists() function B3P-634 --- includes/functions.php | 23 ----------------------- tests/unit/functions/functions_test.php | 17 ----------------- 2 files changed, 40 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 093b75fd3..cae58016e 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -236,29 +236,6 @@ function generate_portal_pagination($base_url, $num_items, $per_page, $start_ite return $page_string; } -/** -* Check if table exists -* @copyright (c) 2007 phpBB Group -* -* @param string $table_name The table name to check for -* @return bool true if table exists, else false -*/ -function sql_table_exists($table_name) -{ - global $db; - $db->sql_return_on_error(true); - $result = $db->sql_query_limit('SELECT * FROM ' . $db->sql_escape($table_name), 1); - $db->sql_return_on_error(false); - - if ($result) - { - $db->sql_freeresult($result); - return true; - } - - return false; -} - /** * get topic tracking info for news * based on get_complete_tracking_info of phpBB3 diff --git a/tests/unit/functions/functions_test.php b/tests/unit/functions/functions_test.php index 7fdf7f16e..451d11d69 100644 --- a/tests/unit/functions/functions_test.php +++ b/tests/unit/functions/functions_test.php @@ -31,23 +31,6 @@ public function setUp() ->will($this->returnValue(array('match' => array('/disallowed_word/'), 'replace' => array('')))); } - public function data_sql_table_exists() - { - return array( - array(true, 'phpbb_config'), - array(true, 'phpbb_styles'), - array(false, 'phpbb_foobar'), - ); - } - - /** - * @dataProvider data_sql_table_exists - */ - public function test_sql_table_exists($expected, $table) - { - $this->assertEquals($expected, sql_table_exists($table)); - } - public function data_character_limit() { return array( From f82794df110686c841d782e4ff0eceb4d65f99b8 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 17:56:54 +0200 Subject: [PATCH 3/3] [ticket/634] Add tests for birthday_list and improve modules_helper_test B3P-634 --- modules/birthday_list.php | 16 ++- tests/unit/includes/modules_helper_test.php | 14 +++ tests/unit/modules/birthday_list_test.php | 108 ++++++++++++++++++++ tests/unit/modules/fixtures/users.xml | 9 ++ 4 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 tests/unit/modules/birthday_list_test.php create mode 100644 tests/unit/modules/fixtures/users.xml diff --git a/modules/birthday_list.php b/modules/birthday_list.php index 3df8512e8..9fb839f39 100644 --- a/modules/birthday_list.php +++ b/modules/birthday_list.php @@ -44,10 +44,10 @@ class birthday_list extends module_base /** @var \phpbb\config\config */ protected $config; - /** @var \phpbb\template */ + /** @var \phpbb\template\template */ protected $template; - /** @var \phpbb\db\driver */ + /** @var \phpbb\db\driver\driver_interface */ protected $db; /** @var \phpbb\user */ @@ -57,8 +57,8 @@ class birthday_list extends module_base * Construct a birthday_list object * * @param \phpbb\config\config $config phpBB config - * @param \phpbb\template $template phpBB template - * @param \phpbb\db\driver $db Database driver + * @param \phpbb\template\template $template phpBB template + * @param \phpbb\db\driver\driver_interface $db Database driver * @param \phpbb\user $user phpBB user object */ public function __construct($config, $template, $db, $user) @@ -185,12 +185,8 @@ public function install($module_id) */ public function uninstall($module_id, $db) { - $del_config = array( - 'board3_birthdays_ahead_' . $module_id, - ); - $sql = 'DELETE FROM ' . CONFIG_TABLE . ' - WHERE ' . $db->sql_in_set('config_name', $del_config); - return $db->sql_query($sql); + $this->config->delete('board3_birthdays_ahead_' . $module_id); + return true; } /** diff --git a/tests/unit/includes/modules_helper_test.php b/tests/unit/includes/modules_helper_test.php index 42e951906..bd56ac503 100644 --- a/tests/unit/includes/modules_helper_test.php +++ b/tests/unit/includes/modules_helper_test.php @@ -11,10 +11,12 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframework\database_test_case { + /** @var \board3\portal\includes\modules_helper */ protected $modules_helper; protected $modules; + /** @var \phpbb\config\config */ protected $config; public function getDataSet() @@ -113,6 +115,11 @@ public function test_generate_forum_select() '', $this->modules_helper->generate_forum_select('foo', 'bar') ); + $this->config->set('bar', '1,2'); + $this->assertEquals( + '', + $this->modules_helper->generate_forum_select('foo', 'bar') + ); } public function test_store_selected_forums() @@ -121,4 +128,11 @@ public function test_store_selected_forums() $this->modules_helper->store_selected_forums('foo'); $this->assertEquals('bar', $this->config['foo']); } + + public function test_store_left_right() + { + $this->assertEmpty($this->config['store_left_right']); + $this->modules_helper->store_left_right('store_left_right'); + $this->assertEquals(0, $this->config['store_left_right']); + } } diff --git a/tests/unit/modules/birthday_list_test.php b/tests/unit/modules/birthday_list_test.php new file mode 100644 index 000000000..b933e6e9b --- /dev/null +++ b/tests/unit/modules/birthday_list_test.php @@ -0,0 +1,108 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/users.xml'); + } + + public function setUp() + { + global $auth, $phpbb_dispatcher; + + parent::setUp(); + + $this->template = new \board3\portal\tests\mock\template($this); + $this->config = new \phpbb\config\config(array()); + $this->user = new \phpbb\user('\phpbb\datetime'); + $this->user->timezone = new \DateTimeZone('UTC'); + $this->user->add_lang('common'); + $this->birthday_list = new \board3\portal\modules\birthday_list($this->config, $this->template, $this->new_dbal(), $this->user); + $auth = $this->getMock('\phpbb\auth\auth', array('acl_get')); + $auth->expects($this->any()) + ->method('acl_get') + ->with($this->anything()) + ->will($this->returnValue(true)); + $phpbb_dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') + ->disableOriginalConstructor() + ->getMock(); + $phpbb_dispatcher->expects($this->any()) + ->method('trigger_event') + ->with($this->anything()) + ->will($this->returnArgument(1)); + } + + public function test_get_template_side() + { + $this->assertSame('birthdays_side.html', $this->birthday_list->get_template_side(5)); + $this->template->assert_same('', 'BIRTHDAY_LIST'); + $this->config->set('allow_birthdays', true); + $this->config->set('load_birthdays', true); + $this->config->set('board3_birthdays_ahead_5', 5); + $sql_ary = array( + array( + 'username' => 'foobar', + 'username_clean' => 'foobar', + 'user_birthday' => preg_replace('/([0-9]+)-([0-9])-([0-9]+)/', '$1- $2-$3', date('d-n-Y', time())), + 'user_id' => 2, + 'user_permissions' => '', + 'user_sig' => '', + 'user_type' => USER_NORMAL, + ), + array( + 'username' => 'foobar2', + 'username_clean' => 'foobar2', + 'user_birthday' => preg_replace('/([0-9]+)-([0-9])-([0-9]+)/', '$1- $2-$3', date('d-n-Y', time() + 86400 * 3)), + 'user_id' => 3, + 'user_permissions' => '', + 'user_sig' => '', + 'user_type' => USER_NORMAL, + ), + ); + $this->db->sql_multi_insert(USERS_TABLE, $sql_ary); + $this->assertSame('birthdays_side.html', $this->birthday_list->get_template_side(5)); + } + + public function test_get_template_acp() + { + $acp_template = $this->birthday_list->get_template_acp(5); + $this->assertArrayHasKey('title', $acp_template); + $this->assertArrayHasKey('vars', $acp_template); + $this->assertArrayHasKey('board3_birthdays_ahead_5', $acp_template['vars']); + } + + public function test_install_uninstall() + { + $this->assertFalse(isset($this->config['board3_birthdays_ahead_5'])); + $this->assertTrue($this->birthday_list->install(5)); + $this->assertTrue(isset($this->config['board3_birthdays_ahead_5'])); + $this->assertTrue($this->birthday_list->uninstall(5, $this->db)); + $this->assertFalse(isset($this->config['board3_birthdays_ahead_5'])); + } + +} diff --git a/tests/unit/modules/fixtures/users.xml b/tests/unit/modules/fixtures/users.xml new file mode 100644 index 000000000..8b234a566 --- /dev/null +++ b/tests/unit/modules/fixtures/users.xml @@ -0,0 +1,9 @@ + + + + username + user_id + user_colour + user_birthday +
+