aboutsummaryrefslogtreecommitdiffstats
path: root/tests/extension/modules_test.php
blob: cbcfdfb7873d54881b51e801c2dc12134ee07de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/ext/vendor2/foo/acp/a_info.php';
require_once dirname(__FILE__) . '/ext/vendor2/foo/mcp/a_info.php';
require_once dirname(__FILE__) . '/ext/vendor2/foo/acp/fail_info.php';
require_once dirname(__FILE__) . '/ext/vendor2/bar/acp/a_info.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/acp/acp_modules.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_module.php';

class phpbb_extension_modules_test extends phpbb_test_case
{
	protected $extension_manager;
	protected $finder;

	public function setUp()
	{
		global $phpbb_extension_manager;

		$this->extension_manager = new phpbb_mock_extension_manager(
			dirname(__FILE__) . '/',
			array(
				'vendor2/foo' => array(
					'ext_name' => 'vendor2/foo',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor2/foo/',
				),
				'vendor3/bar' => array(
					'ext_name' => 'vendor3/bar',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor3/bar/',
				),
			));
		$phpbb_extension_manager = $this->extension_manager;

		$this->acp_modules = new acp_modules();
	}

	public function test_get_module_infos()
	{
		global $phpbb_root_path;

//		$this->markTestIncomplete('Modules no speak namespace! Going to get rid of db modules altogether and fix this test after.');

		// Correctly set the root path for this test to this directory, so the classes can be found
		$phpbb_root_path = dirname(__FILE__) . '/';

		// Find acp module info files
		$this->acp_modules->module_class = 'acp';
		$acp_modules = $this->acp_modules->get_module_infos();
		$this->assertEquals(array(
				'vendor2\\foo\\acp\\a_module' => array(
					'filename'	=> 'vendor2\\foo\\acp\\a_module',
					'title'		=> 'Foobar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => 'ext_vendor2/foo', 'cat' => array('ACP_MODS')),
					),
				),
				'acp_foobar' => array(
					'filename'	=> 'acp_foobar',
					'title'		=> 'ACP Foobar',
					'modes'		=> array(
						'test'		=> array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')),
					),
				),
			), $acp_modules);

		// Find mcp module info files
		$this->acp_modules->module_class = 'mcp';
		$acp_modules = $this->acp_modules->get_module_infos();
		$this->assertEquals(array(
				'vendor2\\foo\\mcp\\a_module' => array(
					'filename'	=> 'vendor2\\foo\\mcp\\a_module',
					'title'		=> 'Foobar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => '', 'cat' => array('MCP_MAIN')),
					),
				),
			), $acp_modules);

		// Find a specific module info file (mcp_a_module)
		$this->acp_modules->module_class = 'mcp';
		$acp_modules = $this->acp_modules->get_module_infos('mcp_a_module');
		$this->assertEquals(array(
				'vendor2\\foo\\mcp\\a_module' => array(
					'filename'	=> 'vendor2\\foo\\mcp\\a_module',
					'title'		=> 'Foobar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => '', 'cat' => array('MCP_MAIN')),
					),
				),
			), $acp_modules);

		// Find a specific module info file (mcp_a_module) with passing the module_class
		$this->acp_modules->module_class = '';
		$acp_modules = $this->acp_modules->get_module_infos('mcp_a_module', 'mcp');
		$this->assertEquals(array(
				'vendor2\\foo\\mcp\\a_module' => array(
					'filename'	=> 'vendor2\\foo\\mcp\\a_module',
					'title'		=> 'Foobar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => '', 'cat' => array('MCP_MAIN')),
					),
				),
			), $acp_modules);

		// The mcp module info file we're looking for shouldn't exist
		$this->acp_modules->module_class = 'mcp';
		$acp_modules = $this->acp_modules->get_module_infos('mcp_a_fail');
		$this->assertEquals(array(), $acp_modules);

		// As there are no ucp modules we shouldn't find any
		$this->acp_modules->module_class = 'ucp';
		$acp_modules = $this->acp_modules->get_module_infos();
		$this->assertEquals(array(), $acp_modules);

		// Get module info of specified extension module
		$this->acp_modules->module_class = 'acp';
		$acp_modules = $this->acp_modules->get_module_infos('foo_acp_a_module');
		$this->assertEquals(array(
				'vendor2\\foo\\acp\\a_module' => array (
					'filename' => 'vendor2\\foo\\acp\\a_module',
					'title' => 'Foobar',
					'modes' => array (
						'config'		=> array ('title' => 'Config', 'auth' => 'ext_vendor2/foo', 'cat' => array ('ACP_MODS')),
					),
				),
			), $acp_modules);

		// No specific module and module class set to an incorrect name
		$acp_modules = $this->acp_modules->get_module_infos('', 'wcp', true);
		$this->assertEquals(array(), $acp_modules);

		// No specific module, no module_class set in the function parameter, and an incorrect module class
		$this->acp_modules->module_class = 'wcp';
		$acp_modules = $this->acp_modules->get_module_infos();
		$this->assertEquals(array(), $acp_modules);

		// No specific module, module class set to false (will default to the above acp)
		// Setting $use_all_available will cause get_module_infos() to also load not enabled extensions (vendor2/bar)
		$this->acp_modules->module_class = 'acp';
		$acp_modules = $this->acp_modules->get_module_infos('', false, true);
		$this->assertEquals(array(
				'vendor2\\foo\\acp\\a_module' => array(
					'filename'	=> 'vendor2\\foo\\acp\\a_module',
					'title'		=> 'Foobar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => 'ext_vendor2/foo', 'cat' => array('ACP_MODS')),
					),
				),
				'acp_foobar' => array(
					'filename'	=> 'acp_foobar',
					'title'		=> 'ACP Foobar',
					'modes'		=> array(
						'test'		=> array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')),
					),
				),
				'vendor2\\bar\\acp\\a_module' => array(
					'filename'	=> 'vendor2\\bar\\acp\\a_module',
					'title'		=> 'Bar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => '', 'cat' => array('ACP_MODS')),
					),
				)
			), $acp_modules);

		// Specific module set to disabled extension
		$acp_modules = $this->acp_modules->get_module_infos('vendor2_bar_acp_a_module', 'acp', true);
		$this->assertEquals(array(
				'vendor2\\bar\\acp\\a_module' => array(
					'filename'	=> 'vendor2\\bar\\acp\\a_module',
					'title'		=> 'Bar',
					'modes'		=> array(
						'config'		=> array('title' => 'Config',	'auth' => '', 'cat' => array('ACP_MODS')),
					),
				)
			), $acp_modules);
	}

	public function module_auth_test_data()
	{
		return array(
			// module_auth, expected result
			array('ext_foo', false),
			array('ext_foo/bar', false),
			array('ext_vendor3/bar', false),
			array('ext_vendor2/foo', true),
		);
	}

	/**
	* @dataProvider module_auth_test_data
	*/
	public function test_modules_auth($module_auth, $expected)
	{
		global $phpbb_extension_manager, $phpbb_dispatcher;

		$phpbb_extension_manager = $this->extension_manager = new phpbb_mock_extension_manager(
			dirname(__FILE__) . '/',
			array(
				'vendor2/foo' => array(
					'ext_name' => 'vendor2/foo',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor2/foo/',
				),
				'vendor3/bar' => array(
					'ext_name' => 'vendor3/bar',
					'ext_active' => '0',
					'ext_path' => 'ext/vendor3/bar/',
				),
			)
		);

		$phpbb_dispatcher = new phpbb_mock_event_dispatcher();

		$this->assertEquals($expected, p_master::module_auth($module_auth, 0));
	}
}