From b805ce2e5fd677ed1185bc5b3f8bb1f63522c712 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 22 Jun 2015 16:59:14 +0200 Subject: [PATCH 1/2] [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/2] [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; +}