Skip to content

Commit 6927c85

Browse files
committed
Add more tests
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 712d04d commit 6927c85

5 files changed

Lines changed: 533 additions & 30 deletions

File tree

acp/ideas_module.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function main()
4141
$request = $phpbb_container->get('request');
4242

4343
// Get an instance of the admin controller
44+
/** @var \phpbb\ideas\controller\admin_controller $admin_controller */
4445
$admin_controller = $phpbb_container->get('phpbb.ideas.admin.controller');
4546

4647
// Add the phpBB Ideas ACP lang file
@@ -58,7 +59,7 @@ public function main()
5859
$admin_controller->set_config_options();
5960
}
6061

61-
// Set Ideas forum options and registered usergroup forum permissions
62+
// Set Ideas forum options and registered user group forum permissions
6263
if ($request->is_set_post('ideas_forum_setup'))
6364
{
6465
$admin_controller->set_ideas_forum_options();

controller/admin_controller.php

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ class admin_controller
4242
/** @var string */
4343
protected $php_ext;
4444

45-
/** @var array */
46-
protected $cfg_array = array();
47-
4845
/** @var string */
4946
public $u_action;
5047

5148
/**
5249
* Constructor
5350
*
54-
* @param \phpbb\config\config $config Config object
51+
* @param \phpbb\config\config $config Config object
5552
* @param \phpbb\db\driver\driver_interface $db Database object
5653
* @param \phpbb\language\language $language Language object
5754
* @param \phpbb\log\log $log Log object
@@ -101,11 +98,6 @@ public function set_config_options()
10198
{
10299
$errors = array();
103100

104-
// This method is called on submit, so set flag to true initially
105-
$submit = true;
106-
107-
$this->cfg_array = $this->request->variable('config', array('' => ''), true);
108-
109101
// Check the form for validity
110102
if (!check_form_key('acp_phpbb_ideas_settings'))
111103
{
@@ -115,36 +107,34 @@ public function set_config_options()
115107
// Don't save settings if errors have occurred
116108
if (count($errors))
117109
{
118-
$submit = false;
119-
120110
$this->template->assign_vars(array(
121111
'S_ERROR' => true,
122112
'ERROR_MSG' => implode('<br />', $errors),
123113
));
114+
115+
return;
124116
}
125117

126-
if ($submit)
127-
{
128-
// Configuration options to list through
129-
$display_vars = array(
130-
'ideas_forum_id',
131-
'ideas_forum_setup',
132-
);
133-
134-
// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to
135-
foreach ($display_vars as $config_name)
136-
{
137-
if (!isset($this->cfg_array[$config_name]))
138-
{
139-
continue;
140-
}
118+
$cfg_array = $this->request->variable('config', array('' => ''));
141119

142-
$this->config->set($config_name, $this->cfg_array[$config_name]);
120+
// Configuration options to list through
121+
$display_vars = array(
122+
'ideas_forum_id',
123+
);
143124

125+
// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to
126+
foreach ($display_vars as $config_name)
127+
{
128+
if (!isset($cfg_array[$config_name]))
129+
{
130+
continue;
144131
}
145-
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_IDEAS_SETTINGS_LOG');
146-
trigger_error($this->language->lang('ACP_IDEAS_SETTINGS_UPDATED') . adm_back_link($this->u_action));
132+
133+
$this->config->set($config_name, $cfg_array[$config_name]);
147134
}
135+
136+
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_IDEAS_SETTINGS_LOG');
137+
trigger_error($this->language->lang('ACP_IDEAS_SETTINGS_UPDATED') . adm_back_link($this->u_action));
148138
}
149139

150140
/**

tests/acp/acp_module_test.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
require_once __DIR__ . '/../../../../../includes/functions_module.php';
12+
13+
class acp_module_test extends \phpbb_test_case
14+
{
15+
/** @var \phpbb_mock_extension_manager */
16+
protected $extension_manager;
17+
18+
/** @var \phpbb\module\module_manager */
19+
protected $module_manager;
20+
21+
protected function setUp(): void
22+
{
23+
global $phpbb_dispatcher, $phpbb_extension_manager, $phpbb_root_path, $phpEx;
24+
25+
$this->extension_manager = new \phpbb_mock_extension_manager(
26+
$phpbb_root_path,
27+
[
28+
'phpbb/ideas' => [
29+
'ext_name' => 'phpbb/ideas',
30+
'ext_active' => '1',
31+
'ext_path' => 'ext/phpbb/ideas/',
32+
],
33+
]);
34+
$phpbb_extension_manager = $this->extension_manager;
35+
36+
$this->module_manager = new \phpbb\module\module_manager(
37+
new \phpbb\cache\driver\dummy(),
38+
$this->getMockBuilder('\phpbb\db\driver\driver_interface')->getMock(),
39+
$this->extension_manager,
40+
MODULES_TABLE,
41+
$phpbb_root_path,
42+
$phpEx
43+
);
44+
45+
$phpbb_dispatcher = new \phpbb_mock_event_dispatcher();
46+
}
47+
48+
public function test_module_info()
49+
{
50+
self::assertEquals([
51+
'\\phpbb\\ideas\\acp\\ideas_module' => [
52+
'filename' => '\\phpbb\\ideas\\acp\\ideas_module',
53+
'title' => 'ACP_PHPBB_IDEAS',
54+
'modes' => [
55+
'settings' => [
56+
'title' => 'ACP_PHPBB_IDEAS_SETTINGS',
57+
'auth' => 'ext_phpbb/ideas && acl_a_board',
58+
'cat' => ['ACP_PHPBB_IDEAS']
59+
],
60+
],
61+
],
62+
], $this->module_manager->get_module_infos('acp', 'acp_ideas_module'));
63+
}
64+
65+
public function module_auth_test_data()
66+
{
67+
return [
68+
// module_auth, expected result
69+
['ext_foo/bar', false],
70+
['ext_phpbb/ideas', true],
71+
];
72+
}
73+
74+
/**
75+
* @dataProvider module_auth_test_data
76+
*/
77+
public function test_module_auth($module_auth, $expected)
78+
{
79+
self::assertEquals($expected, p_master::module_auth($module_auth, 0));
80+
}
81+
82+
public function main_module_test_data()
83+
{
84+
return [
85+
['submit'],
86+
['ideas_forum_setup'],
87+
['']
88+
];
89+
}
90+
91+
/**
92+
* @dataProvider main_module_test_data
93+
* @param string $post
94+
*/
95+
public function test_main_module($post)
96+
{
97+
global $phpbb_container, $request, $template;
98+
99+
if (!defined('IN_ADMIN'))
100+
{
101+
define('IN_ADMIN', true);
102+
}
103+
104+
if ($post)
105+
{
106+
$this->expectException('\Exception');
107+
}
108+
109+
$request = $this->getMockBuilder('\phpbb\request\request')
110+
->disableOriginalConstructor()
111+
->getMock();
112+
$language = $this->getMockBuilder('\phpbb\language\language')
113+
->disableOriginalConstructor()
114+
->getMock();
115+
$template = $this->getMockBuilder('\phpbb\template\template')
116+
->disableOriginalConstructor()
117+
->getMock();
118+
$phpbb_container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$admin_controller = $this->getMockBuilder('\phpbb\ideas\controller\admin_controller')
122+
->disableOriginalConstructor()
123+
->getMock();
124+
125+
$phpbb_container
126+
->expects(self::exactly(3))
127+
->method('get')
128+
->withConsecutive(['language'], ['request'], ['phpbb.ideas.admin.controller'])
129+
->willReturnOnConsecutiveCalls($language, $request, $admin_controller);
130+
131+
// Add the phpBB Ideas ACP lang file
132+
$language->expects(self::once())
133+
->method('add_lang')
134+
->with('phpbb_ideas_acp', 'phpbb/ideas');
135+
136+
// Make the $u_action url available in the admin controller
137+
$admin_controller
138+
->expects(self::once())
139+
->method('set_page_url');
140+
141+
if ($post === 'submit')
142+
{
143+
$request->expects(self::once())
144+
->method('is_set_post')
145+
->with('submit')
146+
->willReturn(true);
147+
}
148+
149+
if ($post === 'ideas_forum_setup')
150+
{
151+
$request->expects(self::exactly(2))
152+
->method('is_set_post')
153+
->withConsecutive(['submit'], ['ideas_forum_setup'])
154+
->willReturnOnConsecutiveCalls(false, true);
155+
}
156+
157+
// Apply Ideas configuration settings
158+
$admin_controller->expects($post === 'submit' ? self::once() : self::never())
159+
->method('set_config_options')
160+
->willThrowException(new \Exception());
161+
162+
// Set Ideas forum options and registered user group forum permissions
163+
$admin_controller->expects($post === 'ideas_forum_setup' ? self::once() : self::never())
164+
->method('set_ideas_forum_options')
165+
->willThrowException(new \Exception());
166+
167+
// Display/set ACP configuration settings
168+
$admin_controller->expects(empty($post) ? self::once() : self::never())
169+
->method('display_options');
170+
171+
$p_master = new p_master();
172+
$p_master->module_ary[0]['is_duplicate'] = 0;
173+
$p_master->module_ary[0]['url_extra'] = '';
174+
$p_master->load('acp', '\phpbb\ideas\acp\ideas_module');
175+
}
176+
}

0 commit comments

Comments
 (0)