Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.68% covered (success)
95.68%
133 / 139
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_extension_modules_test
95.68% covered (success)
95.68%
133 / 139
75.00% covered (warning)
75.00%
3 / 4
4
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
 test_get_module_infos
100.00% covered (success)
100.00%
91 / 91
100.00% covered (success)
100.00%
1 / 1
1
 module_auth_test_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 test_modules_auth
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of 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* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/ext/vendor2/foo/acp/a_info.php';
15require_once __DIR__ . '/ext/vendor2/foo/mcp/a_info.php';
16require_once __DIR__ . '/ext/vendor2/foo/acp/fail_info.php';
17require_once __DIR__ . '/ext/vendor2/bar/acp/a_info.php';
18require_once __DIR__ . '/../../phpBB/includes/acp/acp_modules.php';
19require_once __DIR__ . '/../../phpBB/includes/functions_module.php';
20
21class phpbb_extension_modules_test extends phpbb_test_case
22{
23    protected $extension_manager;
24    protected $finder;
25    protected $module_manager;
26
27    protected function setUp(): void
28    {
29        global $phpbb_extension_manager;
30
31        $this->extension_manager = new phpbb_mock_extension_manager(
32            __DIR__ . '/',
33            array(
34                'vendor2/foo' => array(
35                    'ext_name' => 'vendor2/foo',
36                    'ext_active' => '1',
37                    'ext_path' => 'ext/vendor2/foo/',
38                ),
39                'vendor3/bar' => array(
40                    'ext_name' => 'vendor3/bar',
41                    'ext_active' => '1',
42                    'ext_path' => 'ext/vendor3/bar/',
43                ),
44            ));
45        $phpbb_extension_manager = $this->extension_manager;
46
47        $this->module_manager = new \phpbb\module\module_manager(
48            new \phpbb\cache\driver\dummy(),
49            $this->createMock('\phpbb\db\driver\driver_interface'),
50            $this->extension_manager,
51            MODULES_TABLE,
52            __DIR__ . '/',
53            'php'
54        );
55    }
56
57    public function test_get_module_infos()
58    {
59        global $phpbb_root_path;
60
61//        $this->markTestIncomplete('Modules no speak namespace! Going to get rid of db modules altogether and fix this test after.');
62
63        // Correctly set the root path for this test to this directory, so the classes can be found
64        $phpbb_root_path = __DIR__ . '/';
65
66        // Find acp module info files
67        $acp_modules = $this->module_manager->get_module_infos('acp');
68        $this->assertEquals(array(
69                'vendor2\\foo\\acp\\a_module' => array(
70                    'filename'    => 'vendor2\\foo\\acp\\a_module',
71                    'title'        => 'Foobar',
72                    'modes'        => array(
73                        'config'        => array('title' => 'Config',    'auth' => 'ext_vendor2/foo', 'cat' => array('ACP_MODS')),
74                    ),
75                ),
76                'acp_foobar' => array(
77                    'filename'    => 'acp_foobar',
78                    'title'        => 'ACP Foobar',
79                    'modes'        => array(
80                        'test'        => array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')),
81                    ),
82                ),
83            ), $acp_modules);
84
85        // Find mcp module info files
86        $acp_modules = $this->module_manager->get_module_infos('mcp');
87        $this->assertEquals(array(
88                'vendor2\\foo\\mcp\\a_module' => array(
89                    'filename'    => 'vendor2\\foo\\mcp\\a_module',
90                    'title'        => 'Foobar',
91                    'modes'        => array(
92                        'config'        => array('title' => 'Config',    'auth' => '', 'cat' => array('MCP_MAIN')),
93                    ),
94                ),
95            ), $acp_modules);
96
97        // Find a specific module info file (mcp_a_module)
98        $acp_modules = $this->module_manager->get_module_infos('mcp', 'mcp_a_module');
99        $this->assertEquals(array(
100                'vendor2\\foo\\mcp\\a_module' => array(
101                    'filename'    => 'vendor2\\foo\\mcp\\a_module',
102                    'title'        => 'Foobar',
103                    'modes'        => array(
104                        'config'        => array('title' => 'Config',    'auth' => '', 'cat' => array('MCP_MAIN')),
105                    ),
106                ),
107            ), $acp_modules);
108
109        // The mcp module info file we're looking for shouldn't exist
110        $acp_modules = $this->module_manager->get_module_infos('mcp', 'mcp_a_fail');
111        $this->assertEquals(array(), $acp_modules);
112
113        // As there are no ucp modules we shouldn't find any
114        $acp_modules = $this->module_manager->get_module_infos('ucp');
115        $this->assertEquals(array(), $acp_modules);
116
117        // Get module info of specified extension module
118        $acp_modules = $this->module_manager->get_module_infos('acp', 'foo_acp_a_module');
119        $this->assertEquals(array(
120                'vendor2\\foo\\acp\\a_module' => array (
121                    'filename' => 'vendor2\\foo\\acp\\a_module',
122                    'title' => 'Foobar',
123                    'modes' => array (
124                        'config'        => array ('title' => 'Config', 'auth' => 'ext_vendor2/foo', 'cat' => array ('ACP_MODS')),
125                    ),
126                ),
127            ), $acp_modules);
128
129        // No specific module and module class set to an incorrect name
130        $acp_modules = $this->module_manager->get_module_infos('wcp', '', true);
131        $this->assertEquals(array(), $acp_modules);
132
133        // No specific module, no module_class set in the function parameter, and an incorrect module class
134        $acp_modules = $this->module_manager->get_module_infos('wcp');
135        $this->assertEquals(array(), $acp_modules);
136
137        // No specific module, module class set to false (will default to the above acp)
138        // Setting $use_all_available will cause get_module_infos() to also load not enabled extensions (vendor2/bar)
139        $acp_modules = $this->module_manager->get_module_infos('acp', '', true);
140        $this->assertEquals(array(
141                'vendor2\\foo\\acp\\a_module' => array(
142                    'filename'    => 'vendor2\\foo\\acp\\a_module',
143                    'title'        => 'Foobar',
144                    'modes'        => array(
145                        'config'        => array('title' => 'Config',    'auth' => 'ext_vendor2/foo', 'cat' => array('ACP_MODS')),
146                    ),
147                ),
148                'acp_foobar' => array(
149                    'filename'    => 'acp_foobar',
150                    'title'        => 'ACP Foobar',
151                    'modes'        => array(
152                        'test'        => array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')),
153                    ),
154                ),
155                'vendor2\\bar\\acp\\a_module' => array(
156                    'filename'    => 'vendor2\\bar\\acp\\a_module',
157                    'title'        => 'Bar',
158                    'modes'        => array(
159                        'config'        => array('title' => 'Config',    'auth' => '', 'cat' => array('ACP_MODS')),
160                    ),
161                )
162            ), $acp_modules);
163
164        // Specific module set to disabled extension
165        $acp_modules = $this->module_manager->get_module_infos('acp', 'vendor2_bar_acp_a_module', true);
166        $this->assertEquals(array(
167                'vendor2\\bar\\acp\\a_module' => array(
168                    'filename'    => 'vendor2\\bar\\acp\\a_module',
169                    'title'        => 'Bar',
170                    'modes'        => array(
171                        'config'        => array('title' => 'Config',    'auth' => '', 'cat' => array('ACP_MODS')),
172                    ),
173                )
174            ), $acp_modules);
175    }
176
177    public static function module_auth_test_data()
178    {
179        return array(
180            // module_auth, expected result
181            array('ext_foo', false),
182            array('ext_foo/bar', false),
183            array('ext_vendor3/bar', false),
184            array('ext_vendor2/foo', true),
185        );
186    }
187
188    /**
189    * @dataProvider module_auth_test_data
190    */
191    public function test_modules_auth($module_auth, $expected)
192    {
193        global $phpbb_extension_manager, $phpbb_dispatcher;
194
195        $phpbb_extension_manager = $this->extension_manager = new phpbb_mock_extension_manager(
196            __DIR__ . '/',
197            array(
198                'vendor2/foo' => array(
199                    'ext_name' => 'vendor2/foo',
200                    'ext_active' => '1',
201                    'ext_path' => 'ext/vendor2/foo/',
202                ),
203                'vendor3/bar' => array(
204                    'ext_name' => 'vendor3/bar',
205                    'ext_active' => '0',
206                    'ext_path' => 'ext/vendor3/bar/',
207                ),
208            )
209        );
210
211        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
212
213        $this->assertEquals($expected, p_master::module_auth($module_auth, 0));
214    }
215}