From b805ce2e5fd677ed1185bc5b3f8bb1f63522c712 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 22 Jun 2015 16:59:14 +0200 Subject: [PATCH 1/9] [ticket/634] Increase test coverage of acp module B3P-634 --- tests/mock/template.php | 12 ++++- tests/unit/acp/move_module_test.php | 48 +++++++++++++++++-- tests/unit/acp/portal_module_test.php | 23 +++++++++ tests/unit/functions/check_file_src_test.php | 1 - tests/unit/functions/fetch_news_test.php | 1 - tests/unit/functions/functions_test.php | 1 - tests/unit/functions/get_user_groups_test.php | 2 - tests/unit/functions/simple_test.php | 2 - tests/unit/modules/calendar_test.php | 2 - tests/unit/portal/fetch_posts_test.php | 1 - 10 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 tests/unit/acp/portal_module_test.php diff --git a/tests/mock/template.php b/tests/mock/template.php index 36fc4caa..30e7bb5a 100644 --- a/tests/mock/template.php +++ b/tests/mock/template.php @@ -29,10 +29,15 @@ class template $this->data[$row] = array(); } + $index = (sizeof($this->data[$row])) ? sizeof($this->data[$row]) : 0; foreach ($values as $key => $column) { $this->test_case->assertArrayNotHasKey($key, $this->data[$row]); - $this->data[$row][$key] = $column; + if (!isset($this->data[$row][$index])) + { + $this->data[$row][$index] = array(); + } + $this->data[$row][$index][$key] = $column; } } @@ -63,4 +68,9 @@ class template { unset($this->data[$key]); } + + public function get_row($row) + { + return $this->data[$row]; + } } diff --git a/tests/unit/acp/move_module_test.php b/tests/unit/acp/move_module_test.php index 125b1977..6c1d8537 100644 --- a/tests/unit/acp/move_module_test.php +++ b/tests/unit/acp/move_module_test.php @@ -9,9 +9,6 @@ namespace board3\portal\portal\modules; -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); -require_once(dirname(__FILE__) . '/../../../acp/portal_module.php'); - class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\database_test_case { static public $redirected = false; @@ -31,12 +28,26 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data /** @var \board3\portal\includes\helper */ protected $portal_helper; + /** @var \board3\portal\acp\portal_module */ + protected $portal_module; + + /** @var \board3\portal\tests\mock\template */ + protected $template; + + /** @var \phpbb_mock_request */ + protected $request; + public function setUp() { parent::setUp(); global $db, $cache, $phpbb_root_path, $phpEx, $user, $phpbb_container, $request, $template, $table_prefix; + global $phpbb_dispatcher; + $user = new \board3\portal\tests\mock\user(); + $template = new \board3\portal\tests\mock\template($this); + $this->template = $template; $request = new \phpbb_mock_request; + $this->request = $request; $phpbb_container = new \phpbb_mock_container_builder(); // Mock module service collection $config = new \phpbb\config\config(array()); @@ -91,6 +102,11 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data )); $this->database_handler = new \board3\portal\portal\modules\database_handler($db); $this->constraints_handler = new \board3\portal\portal\modules\constraints_handler($this->portal_columns, $user); + $phpbb_dispatcher = $this->getMock('\phpbb\event\dispatcher', array('trigger_event'), array($phpbb_container)); + $phpbb_dispatcher->expects($this->any()) + ->method('trigger_event') + ->with($this->anything()) + ->will($this->returnArgument(1)); $path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( @@ -377,6 +393,32 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data { $this->assertSame($expected, $this->constraints_handler->can_add_module($this->portal_helper->get_module($module_class), $column)); } + + public function test_main_wrong_mode() + { + $this->setExpectedTriggerError(E_USER_ERROR, 'NO_MODE'); + $this->portal_module->main(5, 'foobar'); + } + + public function test_main_config() + { + $this->portal_module->main(5, 'config'); + $this->template->assert_equals(array(), 'options'); + $options = $this->template->get_row('options'); + $this->assertNotEmpty($this->template->get_row('options')); + $this->assertEquals(true, in_array('board3_show_all_pages', $options[9])); + } + + public function test_main_invalid_submit() + { + $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); + $this->portal_module->main(5, 'config'); + $this->template->assert_equals(array(), 'options'); + $options = $this->template->get_row('options'); + $this->assertNotEmpty($this->template->get_row('options')); + $this->assertEquals(true, in_array('board3_show_all_pages', $options[9])); + $this->template->assert_same(true, 'S_ERROR'); + } } function redirect($url) diff --git a/tests/unit/acp/portal_module_test.php b/tests/unit/acp/portal_module_test.php new file mode 100644 index 00000000..89fa8b87 --- /dev/null +++ b/tests/unit/acp/portal_module_test.php @@ -0,0 +1,23 @@ +module(); + $this->assertArrayHasKey('filename', $module_info); + $this->assertSame('\board3\portal\acp\portal_module', $module_info['filename']); + } +} \ No newline at end of file diff --git a/tests/unit/functions/check_file_src_test.php b/tests/unit/functions/check_file_src_test.php index 7fb7014f..1e63bef2 100644 --- a/tests/unit/functions/check_file_src_test.php +++ b/tests/unit/functions/check_file_src_test.php @@ -7,7 +7,6 @@ * */ -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions_acp.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions.php'); diff --git a/tests/unit/functions/fetch_news_test.php b/tests/unit/functions/fetch_news_test.php index 434a267b..aed5ebdd 100644 --- a/tests/unit/functions/fetch_news_test.php +++ b/tests/unit/functions/fetch_news_test.php @@ -7,7 +7,6 @@ * */ -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'); diff --git a/tests/unit/functions/functions_test.php b/tests/unit/functions/functions_test.php index c95ca4d2..7fdf7f16 100644 --- a/tests/unit/functions/functions_test.php +++ b/tests/unit/functions/functions_test.php @@ -7,7 +7,6 @@ * */ -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/utf/utf_tools.php'); require_once(dirname(__FILE__) . '/../../../../../../includes/functions_content.php'); diff --git a/tests/unit/functions/get_user_groups_test.php b/tests/unit/functions/get_user_groups_test.php index c5ea4a20..62763d1f 100644 --- a/tests/unit/functions/get_user_groups_test.php +++ b/tests/unit/functions/get_user_groups_test.php @@ -7,8 +7,6 @@ * */ -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); - class phpbb_unit_functions_get_user_groups_test extends \board3\portal\tests\testframework\database_test_case { public function getDataSet() diff --git a/tests/unit/functions/simple_test.php b/tests/unit/functions/simple_test.php index 0349b055..58906d41 100644 --- a/tests/unit/functions/simple_test.php +++ b/tests/unit/functions/simple_test.php @@ -7,8 +7,6 @@ * */ -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); - class phpbb_functions_simple_test extends PHPUnit_Framework_TestCase { public function test_ap_validate() diff --git a/tests/unit/modules/calendar_test.php b/tests/unit/modules/calendar_test.php index 86cce026..d8e855c6 100644 --- a/tests/unit/modules/calendar_test.php +++ b/tests/unit/modules/calendar_test.php @@ -9,8 +9,6 @@ namespace board3\portal\modules; -require_once(dirname(__FILE__) . '/../../../includes/functions.php'); - class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframework\database_test_case { protected $path_helper; diff --git a/tests/unit/portal/fetch_posts_test.php b/tests/unit/portal/fetch_posts_test.php index ef04a258..557b77c5 100644 --- a/tests/unit/portal/fetch_posts_test.php +++ b/tests/unit/portal/fetch_posts_test.php @@ -7,7 +7,6 @@ * */ -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'); From 98aee573098c9b79aed980d1ca2d26af8074409e Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 22 Jun 2015 17:22:11 +0200 Subject: [PATCH 2/9] [ticket/634] Add tests for welcome module B3P-634 --- tests/unit/modules/welcome_test.php | 176 ++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 tests/unit/modules/welcome_test.php diff --git a/tests/unit/modules/welcome_test.php b/tests/unit/modules/welcome_test.php new file mode 100644 index 00000000..55b228a7 --- /dev/null +++ b/tests/unit/modules/welcome_test.php @@ -0,0 +1,176 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/configs.xml'); + } + + public function setUp() + { + parent::setUp(); + global $cache, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request; + + $this->config = new \phpbb\config\config(array()); + $this->request = new \phpbb_mock_request(); + $request = $this->request; + $this->template = new \board3\portal\tests\mock\template($this); + $this->user = new \phpbb\user('\phpbb\datetime'); + $cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put', 'sql_load')); + $cache->expects($this->any()) + ->method('destroy') + ->with($this->equalTo('portal_config')); + $cache->expects($this->any()) + ->method('get') + ->with($this->anything()) + ->will($this->returnValue(false)); + $cache->expects($this->any()) + ->method('sql_exists') + ->with($this->anything()); + $cache->expects($this->any()) + ->method('sql_exists') + ->with($this->anything()) + ->will($this->returnValue(false)); + $cache->expects($this->any()) + ->method('put') + ->with($this->anything()); + $phpbb_dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') + ->disableOriginalConstructor() + ->getMock(); + $phpbb_dispatcher->expects($this->any()) + ->method('trigger_event') + ->with($this->anything()) + ->will($this->returnArgument(1)); + + $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, $phpbb_root_path, $phpEx); + \set_config('foobar', 0, false, $this->config); + $this->config->delete('foobar'); + } + + public function test_get_template_center() + { + \set_portal_config('board3_welcome_message_' . 5, 'Welcome to my Community!'); + $this->config['board3_welcome_message_uid_' . 5] = ''; + $this->config['board3_welcome_message_bitfield_' . 5] = ''; + $return = $this->welcome->get_template_center(5); + $this->assertEquals('welcome_center.html', $return); + $this->template->assert_same('Welcome to my Community!', 'PORTAL_WELCOME_MSG'); + } + + public function test_get_template_acp() + { + $acp_template = $this->welcome->get_template_acp(5); + $this->assertNotEmpty($acp_template); + $this->assertArrayHasKey('board3_welcome_message_5', $acp_template['vars']); + } + + public function test_install() + { + $this->assertTrue($this->welcome->install(1)); + + foreach ($this->config as $key => $value) + { + $this->expected_config[$key] = $value; + } + + $portal_config = obtain_portal_config(); + + foreach ($portal_config as $key => $value) + { + $this->expected_portal_config[$key] = $value; + } + } + + /** + * @dependsOn test_install + */ + public function test_uninstall() + { + $this->assertNotEmpty($this->welcome->uninstall(1, $this->db)); + + foreach ($this->expected_config as $key => $value) + { + $this->assertFalse(isset($this->config[$key])); + } + + $portal_config = obtain_portal_config(); + + foreach ($this->expected_config as $key => $value) + { + $this->assertFalse(isset($portal_config[$key])); + } + } + + public function test_update_welcome() + { + $this->welcome->update_welcome('foobar', 5); + $this->template->assert_same(true, 'S_EDIT'); + $this->request->overwrite('preview', true, \phpbb\request\request_interface::POST); + $this->request->overwrite('welcome_message', 'foobar101'); + $this->welcome->update_welcome('foobar', 5); + $this->template->assert_same(true, 'S_PREVIEW'); + $this->template->assert_same('foobar101', 'PREVIEW_TEXT'); + + $this->request = new \phpbb_mock_request(); + $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); + $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); + self::$form_key_valid = true; + $this->request->overwrite('welcome_message', 'foobar101'); + $this->welcome->update_welcome('foobar', 5); + self::$form_key_valid = false; + } + + public function test_update_welcome_invalid_form_key() + { + $this->request = new \phpbb_mock_request(); + $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); + $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); + $this->request->overwrite('welcome_message', 'foobar101'); + $this->setExpectedTriggerError(E_USER_WARNING); + $this->welcome->update_welcome('foobar', 5); + } + + public function test_update_welcome_empty_message() + { + $this->request = new \phpbb_mock_request(); + $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); + $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); + $this->request->overwrite('welcome_message', ''); + $this->setExpectedTriggerError(E_USER_WARNING); + $this->welcome->update_welcome('foobar', 5); + } +} + +function check_form_key($value) +{ + return phpbb_unit_modules_welcome_test::$form_key_valid; +} From 2b65156d472d80ec38846ad1fea7d5447b3ec8f4 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 22 Jun 2015 21:18:32 +0200 Subject: [PATCH 3/9] [ticket/634] Add more tests B3P-634 --- tests/unit/event/listener_test.php | 56 ++++++++++++++++++++++------- tests/unit/modules/search_test.php | 39 ++++++++++++++++++++ tests/unit/modules/welcome_test.php | 1 + 3 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 tests/unit/modules/search_test.php diff --git a/tests/unit/event/listener_test.php b/tests/unit/event/listener_test.php index 21d87988..46eee061 100644 --- a/tests/unit/event/listener_test.php +++ b/tests/unit/event/listener_test.php @@ -68,12 +68,12 @@ class listener_test extends \phpbb_template_template_test_case $this->config = new \phpbb\config\config(array( 'enable_mod_rewrite' => '1', - 'board3_portal_enable' => '1', + 'board3_enable' => '1', )); $provider = new \phpbb\controller\provider(); $provider->find_routing_files($finder); $provider->find(dirname(__FILE__) . '/'); - $this->controller_helper = new \phpbb_mock_controller_helper($this->template, $this->user, $this->config, $provider, $manager, new \phpbb\symfony_request($request), $request, new \phpbb\filesystem(), '', 'php', dirname(__FILE__) . '/'); + $this->controller_helper = new mock_controller_helper($this->template, $this->user, $this->config, $provider, $manager, new \phpbb\symfony_request($request), $request, new \phpbb\filesystem(), '', 'php', dirname(__FILE__) . '/'); $this->path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( @@ -85,14 +85,13 @@ class listener_test extends \phpbb_template_template_test_case $this->php_ext ); - $this->auth = new \phpbb\auth\auth(); - $userdata = array( - 'user_id' => 2, - ); - $this->auth->acl($userdata); - // Pretend to allow downloads - $this->auth->acl_options['global']['u_view_portal'] = 0; - $this->auth->acl[0][0] = true; + $this->auth = $this->getMockBuilder('\phpbb\auth\auth') + ->disableOriginalConstructor() + ->getMock(); + $this->auth->expects($this->any()) + ->method('acl_get') + ->with($this->anything()) + ->will($this->returnValue(true)); $this->controller = $this->getMockBuilder('\board3\portal\controller\main') ->disableOriginalConstructor() @@ -160,14 +159,30 @@ class listener_test extends \phpbb_template_template_test_case $this->assertEmpty($result); - $this->user->data['session_page'] = '/app.php/portal'; + $this->controller_helper->set_current_url('/app.php/portal'); $result = $this->phpbb_dispatcher->trigger_event('core.page_header', compact($vars)); $this->assertEmpty($result); // Make sure user shouldn't see link - unset($this->auth->cache[0]['u_view_portal']); - $this->auth->acl[0][0] = false; + $this->config->set('board3_enable', 0); + $this->controller_helper->set_current_url(''); + $result = $this->phpbb_dispatcher->trigger_event('core.page_header', compact($vars)); + + $this->assertEmpty($result); + $this->config->set('board3_enable', 1); + } + + public function test_check_portal_all() + { + $this->phpbb_dispatcher->addListener('core.page_header', array($this->listener, 'add_portal_link')); + + $this->controller_helper->set_current_url(''); + $result = $this->phpbb_dispatcher->trigger_event('core.page_header', compact($vars)); + + $this->assertEmpty($result); + + $this->config->set('board3_show_all_pages', true); $result = $this->phpbb_dispatcher->trigger_event('core.page_header', compact($vars)); $this->assertEmpty($result); @@ -196,3 +211,18 @@ class listener_test extends \phpbb_template_template_test_case )), $result['lang_set_ext']); } } + +class mock_controller_helper extends \phpbb_mock_controller_helper +{ + protected $current_url = ''; + + public function set_current_url($url) + { + $this->current_url = $url; + } + + public function get_current_url() + { + return $this->current_url; + } +} diff --git a/tests/unit/modules/search_test.php b/tests/unit/modules/search_test.php new file mode 100644 index 00000000..42715eeb --- /dev/null +++ b/tests/unit/modules/search_test.php @@ -0,0 +1,39 @@ +template = new \board3\portal\tests\mock\template($this); + $this->search = new \board3\portal\modules\search($this->template, '', ''); + } + + public function test_get_template_side() + { + $this->assertSame('search_side.html', $this->search->get_template_side(5)); + } + + public function test_get_template_acp() + { + $acp_template = $this->search->get_template_acp(5); + $this->assertNotEmpty($acp_template); + $this->assertEmpty($acp_template['vars']); + } +} diff --git a/tests/unit/modules/welcome_test.php b/tests/unit/modules/welcome_test.php index 55b228a7..70e2d272 100644 --- a/tests/unit/modules/welcome_test.php +++ b/tests/unit/modules/welcome_test.php @@ -166,6 +166,7 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); $this->request->overwrite('welcome_message', ''); $this->setExpectedTriggerError(E_USER_WARNING); + self::$form_key_valid = true; $this->welcome->update_welcome('foobar', 5); } } From 0cc4b4ea0a1be85e8461483e487078fa7d1e8540 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 00:48:52 +0200 Subject: [PATCH 4/9] [ticket/634] Add tests for clock module and improve tests for calendar B3P-634 --- tests/mock/check_form_key.php | 20 ++++++ tests/unit/modules/calendar_test.php | 100 ++++++++++++++++++++++++++- tests/unit/modules/clock_test.php | 57 +++++++++++++++ tests/unit/modules/welcome_test.php | 16 ++--- 4 files changed, 180 insertions(+), 13 deletions(-) create mode 100644 tests/mock/check_form_key.php create mode 100644 tests/unit/modules/clock_test.php diff --git a/tests/mock/check_form_key.php b/tests/mock/check_form_key.php new file mode 100644 index 00000000..7a1406cc --- /dev/null +++ b/tests/mock/check_form_key.php @@ -0,0 +1,20 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/configs.xml'); @@ -26,7 +37,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor public function setUp() { parent::setUp(); - global $cache, $phpbb_root_path, $phpEx; + global $cache, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request; $this->path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( @@ -37,11 +48,32 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor $phpbb_root_path, 'php' ); + $db = $this->new_dbal(); + $phpbb_dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') + ->disableOriginalConstructor() + ->getMock(); + $phpbb_dispatcher->expects($this->any()) + ->method('trigger_event') + ->with($this->anything()) + ->will($this->returnArgument(1)); self::$config = new \phpbb\config\config(array()); + $this->template = new \board3\portal\tests\mock\template($this); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper->add_route('board3_portal_controller', 'portal'); $modules_helper = new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), new \phpbb\config\config(array()), $controller_helper, new \phpbb_mock_request()); - $this->calendar = new \board3\portal\modules\calendar(self::$config, $modules_helper, null, null, null, dirname(__FILE__) . '/../../../', 'php', null, $this->path_helper, null); + $request = $this->request = new \phpbb_mock_request(); + $user = new \phpbb\user('\phpbb\datetime'); + $user->timezone = new \DateTimeZone('UTC'); + $user->add_lang('common'); + $log = $this->getMockBuilder('\phpbb\log') + ->setMethods(array('add')) + ->disableOriginalConstructor() + ->getMock(); + $log->expects($this->any()) + ->method('add') + ->with($this->anything()) + ->will($this->returnValue(true)); + $this->calendar = new \board3\portal\modules\calendar(self::$config, $modules_helper, $this->template, $db, $this->request, dirname(__FILE__) . '/../../../', 'php', $user, $this->path_helper, $log); define('PORTAL_MODULES_TABLE', 'phpbb_portal_modules'); define('PORTAL_CONFIG_TABLE', 'phpbb_portal_config'); $cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put')); @@ -114,6 +146,68 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor $this->assertFalse(isset($portal_config[$key])); } } + + public function test_get_template_side() + { + $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + self::$config->set('board3_sunday_first_5', true); + $this->request->overwrite('m5', 1); + $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + $this->request->overwrite('m5', -1); + $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + } + + public function test_update_events_no_error() + { + $this->calendar->update_events('foobar', 5); + } + + public function test_update_events_form_key_fail() + { + // Save event + check_form_key::$form_key_valid = false; + $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); + $this->setExpectedTriggerError(E_USER_WARNING); + $this->calendar->update_events('foobar', 5); + } + + public function test_update_events_wrong_start_time() + { + // Save event + check_form_key::$form_key_valid = true; + $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); + $this->setExpectedTriggerError(E_USER_WARNING); + $this->calendar->update_events('foobar', 5); + } + + public function test_update_events_wrong_end_time() + { + // Save event + check_form_key::$form_key_valid = true; + $this->request->overwrite('event_start_date', '15.06.2035 13:00'); + $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); + $this->setExpectedTriggerError(E_USER_WARNING); + $this->calendar->update_events('foobar', 5); + } + + public function test_update_events_all_day() + { + // Save event + check_form_key::$form_key_valid = true; + $this->request->overwrite('event_start_date', '15.06.2035 13:00'); + $this->request->overwrite('event_all_day', true); + $this->request->overwrite('event_title', 'foobar'); + $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); + $this->setExpectedTriggerError(E_USER_NOTICE, '

« '); + $this->calendar->update_events('foobar', 5); + } + + public function test_display_events() + { + set_portal_config('board3_calendar_events_5', '[{"title":"foobar","desc":" ","start_time":2065518000,"end_time":"","all_day":true,"permission":"","url":" "}]'); + check_form_key::$form_key_valid = false; + $this->calendar->manage_events('', 'foobar', 5); + } } function set_config($config_name, $config_value, $is_dynamic = false) diff --git a/tests/unit/modules/clock_test.php b/tests/unit/modules/clock_test.php new file mode 100644 index 00000000..192c072f --- /dev/null +++ b/tests/unit/modules/clock_test.php @@ -0,0 +1,57 @@ +template = new \board3\portal\tests\mock\template($this); + $this->config = new \phpbb\config\config(array()); + $this->clock = new \board3\portal\modules\clock($this->config, $this->template); + } + + public function test_get_template_side() + { + $this->assertSame('clock_side.html', $this->clock->get_template_side(5)); + $this->template->assert_same(null, 'B3P_CLOCK_SRC'); + $this->config->set('board3_clock_src_5', 'foobar'); + $this->assertSame('clock_side.html', $this->clock->get_template_side(5)); + $this->template->assert_same('foobar', 'B3P_CLOCK_SRC'); + } + + public function test_get_template_acp() + { + $acp_template = $this->clock->get_template_acp(5); + $this->assertNotEmpty($acp_template); + $this->assertArrayHasKey('board3_clock_src_5', $acp_template['vars']); + } + + public function test_install_uninstall() + { + $this->config->delete('board3_clock_src_5'); + $this->assertTrue($this->clock->install(5)); + $this->assertTrue(isset($this->config['board3_clock_src_5'])); + $this->assertSame('', $this->config['board3_clock_src_5']); + $this->assertTrue($this->clock->uninstall(5, '')); + $this->assertFalse(isset($this->config['board3_clock_src_5'])); + } +} diff --git a/tests/unit/modules/welcome_test.php b/tests/unit/modules/welcome_test.php index 70e2d272..1388f48d 100644 --- a/tests/unit/modules/welcome_test.php +++ b/tests/unit/modules/welcome_test.php @@ -9,6 +9,8 @@ namespace board3\portal\modules; +require_once dirname(__FILE__) . '/../../mock/check_form_key.php'; + class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework\database_test_case { protected $path_helper; @@ -28,8 +30,6 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework protected $expected_config = array(); protected $expected_portal_config = array(); - static public $form_key_valid = false; - public function getDataSet() { return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/configs.xml'); @@ -74,6 +74,7 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, $phpbb_root_path, $phpEx); \set_config('foobar', 0, false, $this->config); $this->config->delete('foobar'); + check_form_key::$form_key_valid = false; } public function test_get_template_center() @@ -143,10 +144,10 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->request = new \phpbb_mock_request(); $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); - self::$form_key_valid = true; + check_form_key::$form_key_valid = true; $this->request->overwrite('welcome_message', 'foobar101'); $this->welcome->update_welcome('foobar', 5); - self::$form_key_valid = false; + check_form_key::$form_key_valid = false; } public function test_update_welcome_invalid_form_key() @@ -166,12 +167,7 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); $this->request->overwrite('welcome_message', ''); $this->setExpectedTriggerError(E_USER_WARNING); - self::$form_key_valid = true; + check_form_key::$form_key_valid = true; $this->welcome->update_welcome('foobar', 5); } } - -function check_form_key($value) -{ - return phpbb_unit_modules_welcome_test::$form_key_valid; -} From 66a5d95ed967075a5d9b08503e59fa5c5dffa080 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 12:15:38 +0200 Subject: [PATCH 5/9] [ticket/634] Improve test coverage and fix coding issues B3P-634 --- includes/functions.php | 2 + modules/calendar.php | 11 +- tests/unit/modules/calendar_test.php | 223 +++++++++++++++++++----- tests/unit/modules/fixtures/configs.xml | 10 ++ 4 files changed, 199 insertions(+), 47 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 81001e34..d049ab01 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -465,6 +465,8 @@ function get_user_groups() if ($groups_ary === false) { + $groups_ary = array(); + // get user's groups $sql = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' diff --git a/modules/calendar.php b/modules/calendar.php index 3d9a526c..ebf0491d 100644 --- a/modules/calendar.php +++ b/modules/calendar.php @@ -437,12 +437,12 @@ class calendar extends module_base trigger_error($this->user->lang['FORM_INVALID']. adm_back_link($u_action), E_USER_WARNING); } - $event_title = $this->request->variable('event_title', ' ', true); - $event_desc = $this->request->variable('event_desc', ' ', true); + $event_title = $this->request->variable('event_title', '', true); + $event_desc = $this->request->variable('event_desc', '', true); $event_start_date = trim($this->request->variable('event_start_date', '')); $event_end_date = trim($this->request->variable('event_end_date', '')); $event_all_day = $this->request->variable('event_all_day', false); // default to false - $event_url = $this->request->variable('event_url', ' '); + $event_url = $this->request->variable('event_url', ''); $event_permission = $this->request->variable('permission-setting-calendar', array(0 => '')); $groups_ary = array(); @@ -488,11 +488,6 @@ class calendar extends module_base trigger_error($this->user->lang['NO_EVENT_TITLE'] . adm_back_link($u_action), E_USER_WARNING); } - if (!$start_time || $start_time == 0) - { - trigger_error($this->user->lang['NO_EVENT_START'] . adm_back_link($u_action), E_USER_WARNING); - } - // overwrite already existing events and make sure we don't try to save an event outside of the normal array size of $events if (isset($link_id) && $link_id < sizeof($events)) { diff --git a/tests/unit/modules/calendar_test.php b/tests/unit/modules/calendar_test.php index 6b872e36..1aee2e15 100644 --- a/tests/unit/modules/calendar_test.php +++ b/tests/unit/modules/calendar_test.php @@ -37,7 +37,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor public function setUp() { parent::setUp(); - global $cache, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request; + global $cache, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request, $user; $this->path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( @@ -57,6 +57,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor ->with($this->anything()) ->will($this->returnArgument(1)); self::$config = new \phpbb\config\config(array()); + \set_config('foobar', false, false, self::$config); $this->template = new \board3\portal\tests\mock\template($this); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper->add_route('board3_portal_controller', 'portal'); @@ -149,12 +150,35 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor public function test_get_template_side() { + set_portal_config('board3_calendar_events_5', '[{"title":"foobar","desc":" ","start_time":' . (time() - 3600) . ',"end_time":"","all_day":true,"permission":"","url":" "},{"title":"foobar","desc":" ","start_time":' . (time() + 90000) . ',"end_time":"","all_day":true,"permission":"","url":" "}]'); $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + self::$config->set('board3_sunday_first_5', true); $this->request->overwrite('m5', 1); $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + $this->request->overwrite('m5', -1); $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + + self::$config->set('board3_display_events_5', true); + $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + $this->assertSame(1, sizeof($this->template->get_row('minical.cur_events'))); + $this->assertSame(1, sizeof($this->template->get_row('minical.upcoming_events'))); + + set_portal_config('board3_calendar_events_5', '[{"title":"foobar","desc":" ","start_time":' . (time() - 10800) . ',"end_time":"","all_day":true,"permission":"","url":"http://example.com"},{"title":"foobar","desc":" ","start_time":' . (time() + 108000) . ',"end_time":"","all_day":true,"permission":"","url":"' . generate_board_url() . '"},{"title":"foobar3","desc":" ","start_time":' . (time() - 90000) . ',"end_time":' . (time() + 90000) . ',"all_day":false,"permission":"","url":" "}]'); + $this->template->delete_var('minical.cur_events'); + $this->template->delete_var('minical.upcoming_events'); + $this->assertSame('calendar_side.html', $this->calendar->get_template_side(5)); + $this->assertSame(2, sizeof($this->template->get_row('minical.cur_events'))); + $this->assertSame(1, sizeof($this->template->get_row('minical.upcoming_events'))); + } + + public function test_get_template_acp() + { + $acp_template = $this->calendar->get_template_acp(5); + $this->assertArrayHasKey('title', $acp_template); + $this->assertArrayHasKey('vars', $acp_template); + $this->assertArrayHasKey('board3_display_events_5', $acp_template['vars']); } public function test_update_events_no_error() @@ -162,51 +186,172 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor $this->calendar->update_events('foobar', 5); } - public function test_update_events_form_key_fail() + public function data_update_events() { - // Save event - check_form_key::$form_key_valid = false; - $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); - $this->setExpectedTriggerError(E_USER_WARNING); - $this->calendar->update_events('foobar', 5); + return array( + array( + array( + 'event_start_date' => date('d.m.Y G:i', time() + 3600 * 3), + 'event_all_day' => true, + 'event_title' => 'foobar', + 'id' => 0, + ), + array( + 'save' => true, + ), + E_USER_NOTICE, + '

« Back to previous page', + '[{"title":"foobar","desc":" ","start_time":' . (time() + 3600) . ',"end_time":"","all_day":true,"permission":"","url":" "}]', + true, + ), + // Form key invalid + array( + array(), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '', + false, + ), + // Wrong start time + array( + array(), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '', + true, + ), + // Wrong end time + array( + array( + 'event_start_date' => '15.06.2035 13:00', + ), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '', + true, + ), + // End time in past + array( + array( + 'event_start_date' => '15.06.2035 13:00', + 'event_end_date' => '15.06.2005 19:00', + ), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '', + true, + ), + // End time before start + array( + array( + 'event_start_date' => '15.06.2035 13:00', + 'event_end_date' => '15.06.2035 12:00', + ), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '', + true, + ), + // No event title + array( + array( + 'event_start_date' => date('d.m.Y G:i', time() + 3600 * 3), + 'event_all_day' => true, + ), + array( + 'save' => true, + ), + E_USER_WARNING, + '', + '[{"title":"foobar","desc":" ","start_time":' . (time() + 3600) . ',"end_time":"","all_day":true,"permission":"","url":" "}]', + true, + ), + // Create valid event + array( + array( + 'event_start_date' => date('d.m.Y G:i', time() + 3600 * 3), + 'event_all_day' => true, + 'event_title' => 'foobar', + ), + array( + 'save' => true, + ), + E_USER_NOTICE, + '

« Back to previous page', + '[{"title":"foobar","desc":" ","start_time":' . (time() + 3600) . ',"end_time":"","all_day":true,"permission":"","url":" "}]', + true, + ), + // Display existing events + array( + array(), + array(), + false, + '', + 'board3_calendar_events_5' => '[{"title":"foobar","desc":" ","start_time":2065518000,"end_time":"","all_day":true,"permission":"","url":" "}]', + false, + ), + // Edit event + array( + array( + 'id' => 0, + 'action' => 'edit', + ), + array(), + false, + '', + '[{"title":"foobar","desc":" ","start_time":' . (time() + 3600) . ',"end_time":"","all_day":true,"permission":"","url":" "}]', + true, + ), + ); } - public function test_update_events_wrong_start_time() + /** + * @dataProvider data_update_events + */ + public function test_update_events($get_variables, $post_variables, $expected_error = E_USER_WARNING, $expected_error_message = '', $portal_config = array(), $form_key_valid = false) { - // Save event - check_form_key::$form_key_valid = true; - $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); - $this->setExpectedTriggerError(E_USER_WARNING); - $this->calendar->update_events('foobar', 5); - } + check_form_key::$form_key_valid = $form_key_valid; - public function test_update_events_wrong_end_time() - { - // Save event - check_form_key::$form_key_valid = true; - $this->request->overwrite('event_start_date', '15.06.2035 13:00'); - $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); - $this->setExpectedTriggerError(E_USER_WARNING); - $this->calendar->update_events('foobar', 5); - } + foreach ($get_variables as $key => $value) + { + $this->request->overwrite($key, $value); + } - public function test_update_events_all_day() - { - // Save event - check_form_key::$form_key_valid = true; - $this->request->overwrite('event_start_date', '15.06.2035 13:00'); - $this->request->overwrite('event_all_day', true); - $this->request->overwrite('event_title', 'foobar'); - $this->request->overwrite('save', true, \phpbb\request\request_interface::POST); - $this->setExpectedTriggerError(E_USER_NOTICE, '

« '); - $this->calendar->update_events('foobar', 5); - } + foreach ($post_variables as $key => $value) + { + $this->request->overwrite($key, $value, \phpbb\request\request_interface::POST); + } - public function test_display_events() - { - set_portal_config('board3_calendar_events_5', '[{"title":"foobar","desc":" ","start_time":2065518000,"end_time":"","all_day":true,"permission":"","url":" "}]'); - check_form_key::$form_key_valid = false; - $this->calendar->manage_events('', 'foobar', 5); + set_portal_config('board3_calendar_events_5', $portal_config); + + if ($expected_error !== false) + { + if (!empty($expected_error_message)) + { + $this->setExpectedTriggerError($expected_error, $expected_error_message); + } + else + { + $this->setExpectedTriggerError($expected_error); + } + } + + $this->calendar->update_events('foobar', 5); } } diff --git a/tests/unit/modules/fixtures/configs.xml b/tests/unit/modules/fixtures/configs.xml index 5173d042..e93552ba 100644 --- a/tests/unit/modules/fixtures/configs.xml +++ b/tests/unit/modules/fixtures/configs.xml @@ -8,4 +8,14 @@ config_name config_value + + group_id + group_name + group_desc + + 5 + admins + + +
From 1eb4dc0e8e2d5354584e0deaf53e3f3ce5e0d792 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 12:29:33 +0200 Subject: [PATCH 6/9] [ticket/634] Assert that events row is empty B3P-634 --- tests/unit/modules/calendar_test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/modules/calendar_test.php b/tests/unit/modules/calendar_test.php index 1aee2e15..d17efa9d 100644 --- a/tests/unit/modules/calendar_test.php +++ b/tests/unit/modules/calendar_test.php @@ -184,6 +184,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor public function test_update_events_no_error() { $this->calendar->update_events('foobar', 5); + $this->assertNull($this->template->get_row('events')); } public function data_update_events() From 668040b48ef73c179e94cfc16dc232a997b27989 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Jun 2015 16:02:08 +0200 Subject: [PATCH 7/9] [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 d049ab01..093b75fd 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 9616b605..8931fe3b 100644 --- a/portal/fetch_posts.php +++ b/portal/fetch_posts.php @@ -627,7 +627,7 @@ class fetch_posts 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 @@ class fetch_posts 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 557b77c5..1d23faed 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 8/9] [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 093b75fd..cae58016 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 7fdf7f16..451d11d6 100644 --- a/tests/unit/functions/functions_test.php +++ b/tests/unit/functions/functions_test.php @@ -31,23 +31,6 @@ class phpbb_unit_functions_functions_test extends \board3\portal\tests\testframe ->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 9/9] [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 3df8512e..9fb839f3 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 @@ class birthday_list extends module_base */ 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 42e95190..bd56ac50 100644 --- a/tests/unit/includes/modules_helper_test.php +++ b/tests/unit/includes/modules_helper_test.php @@ -11,10 +11,12 @@ require_once dirname(__FILE__) . '/../../../../../../includes/functions_admin.ph 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 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe '', $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 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe $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 00000000..b933e6e9 --- /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 00000000..8b234a56 --- /dev/null +++ b/tests/unit/modules/fixtures/users.xml @@ -0,0 +1,9 @@ + + + + username + user_id + user_colour + user_birthday +
+