Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.10% covered (danger)
21.10%
65 / 308
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_dbal_migrator_tool_module_test
21.10% covered (danger)
21.10%
65 / 308
44.44% covered (danger)
44.44%
4 / 9
237.57
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUp
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 exists_data_acp
0.00% covered (danger)
0.00%
0 / 110
0.00% covered (danger)
0.00%
0 / 1
2
 test_exists_acp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exists_data_ucp
0.00% covered (danger)
0.00%
0 / 110
0.00% covered (danger)
0.00%
0 / 1
2
 test_exists_ucp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_add
70.69% covered (warning)
70.69%
41 / 58
0.00% covered (danger)
0.00%
0 / 1
12.52
 test_remove
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
 test_reverse
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
4.68
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/foo/bar/acp/acp_test_info.php';
15require_once __DIR__ . '/ext/foo/bar/ucp/ucp_test_info.php';
16
17class phpbb_dbal_migrator_tool_module_test extends phpbb_database_test_case
18{
19    protected $db;
20    protected $cache;
21    protected $user;
22    protected $tool;
23
24    public function getDataSet()
25    {
26        return $this->createXMLDataSet(__DIR__.'/fixtures/migrator_module.xml');
27    }
28
29    protected function setUp(): void
30    {
31        // Need global $db, $user for delete_module function in acp_modules
32        global $phpbb_root_path, $phpEx, $skip_add_log, $db, $user, $phpbb_log;
33
34        parent::setUp();
35
36        // Disable the logs
37        $skip_add_log = true;
38
39        $db = $this->db = $this->new_dbal();
40        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
41        $this->cache = new \phpbb\cache\service(new \phpbb\cache\driver\dummy(), new \phpbb\config\config(array()), $this->db, $phpbb_dispatcher, $phpbb_root_path, $phpEx);
42        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
43        $lang = new \phpbb\language\language($lang_loader);
44        $user = $this->user = new \phpbb\user($lang, '\phpbb\datetime');
45
46        $cache = new phpbb_mock_cache;
47        $auth = $this->createMock('\phpbb\auth\auth');
48        $phpbb_log = new \phpbb\log\log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE);
49
50        // Correctly set the root path for this test to this directory, so the classes can be found
51        $phpbb_root_path = __DIR__ . '/';
52
53        $phpbb_extension_manager = new phpbb_mock_extension_manager($phpbb_root_path);
54        $module_manager = new \phpbb\module\module_manager($cache, $this->db, $phpbb_extension_manager, MODULES_TABLE, $phpbb_root_path, $phpEx);
55
56        $this->tool = new \phpbb\db\migration\tool\module($this->db, $this->user, $module_manager, 'phpbb_modules');
57    }
58
59    public static function exists_data_acp()
60    {
61        return array(
62            // Test the existing category
63            array(
64                '',
65                'ACP_CAT',
66                false,
67                true,
68            ),
69            array(
70                0,
71                'ACP_CAT',
72                false,
73                true,
74            ),
75            array(
76                false,
77                'ACP_CAT',
78                false,
79                true,
80            ),
81
82            // Test the existing category lazily
83            array(
84                '',
85                'ACP_CAT',
86                true,
87                true,
88            ),
89            array(
90                0,
91                'ACP_CAT',
92                true,
93                true,
94            ),
95            array(
96                false,
97                'ACP_CAT',
98                true,
99                true,
100            ),
101
102            // Test the existing module
103            array(
104                '',
105                'ACP_MODULE',
106                false,
107                false,
108            ),
109            array(
110                false,
111                'ACP_MODULE',
112                false,
113                true,
114            ),
115            array(
116                'ACP_CAT',
117                'ACP_MODULE',
118                false,
119                true,
120            ),
121
122            // Test the existing module lazily
123            array(
124                '',
125                'ACP_MODULE',
126                true,
127                false,
128            ),
129            array(
130                false,
131                'ACP_MODULE',
132                true,
133                true,
134            ),
135            array(
136                'ACP_CAT',
137                'ACP_MODULE',
138                true,
139                true,
140            ),
141
142            // Test for non-existant modules
143            array(
144                '',
145                'ACP_NON_EXISTANT_CAT',
146                false,
147                false,
148            ),
149            array(
150                false,
151                'ACP_NON_EXISTANT_CAT',
152                false,
153                false,
154            ),
155            array(
156                'ACP_CAT',
157                'ACP_NON_EXISTANT_MODULE',
158                false,
159                false,
160            ),
161
162            // Test for non-existant modules lazily
163            array(
164                '',
165                'ACP_NON_EXISTANT_CAT',
166                true,
167                false,
168            ),
169            array(
170                false,
171                'ACP_NON_EXISTANT_CAT',
172                true,
173                false,
174            ),
175            array(
176                'ACP_CAT',
177                'ACP_NON_EXISTANT_MODULE',
178                true,
179                false,
180            ),
181        );
182    }
183
184    /**
185    * @dataProvider exists_data_acp
186    */
187    public function test_exists_acp($parent, $module, $lazy, $expected)
188    {
189        $this->assertEquals($expected, $this->tool->exists('acp', $parent, $module, $lazy));
190    }
191
192    public static function exists_data_ucp()
193    {
194        return array(
195            // Test the existing category
196            array(
197                '',
198                'UCP_MAIN_CAT',
199                false,
200                true,
201            ),
202            array(
203                0,
204                'UCP_MAIN_CAT',
205                false,
206                true,
207            ),
208            array(
209                false,
210                'UCP_MAIN_CAT',
211                false,
212                true,
213            ),
214
215            // Test the existing category lazily
216            array(
217                '',
218                'UCP_MAIN_CAT',
219                true,
220                true,
221            ),
222            array(
223                0,
224                'UCP_MAIN_CAT',
225                true,
226                true,
227            ),
228            array(
229                false,
230                'UCP_MAIN_CAT',
231                true,
232                true,
233            ),
234
235            // Test the existing module
236            array(
237                '',
238                'UCP_SUBCATEGORY',
239                false,
240                false,
241            ),
242            array(
243                false,
244                'UCP_SUBCATEGORY',
245                false,
246                true,
247            ),
248            array(
249                'UCP_MAIN_CAT',
250                'UCP_SUBCATEGORY',
251                false,
252                true,
253            ),
254            array(
255                'UCP_SUBCATEGORY',
256                'UCP_MODULE',
257                false,
258                true,
259            ),
260
261            // Test the existing module lazily
262            array(
263                '',
264                'UCP_SUBCATEGORY',
265                true,
266                false,
267            ),
268            array(
269                false,
270                'UCP_SUBCATEGORY',
271                true,
272                true,
273            ),
274            array(
275                'UCP_MAIN_CAT',
276                'UCP_SUBCATEGORY',
277                true,
278                true,
279            ),
280            array(
281                'UCP_SUBCATEGORY',
282                'UCP_MODULE',
283                true,
284                true,
285            ),
286
287            // Test for non-existant modules
288            array(
289                '',
290                'UCP_NON_EXISTANT_CAT',
291                false,
292                false,
293            ),
294            array(
295                'UCP_MAIN_CAT',
296                'UCP_NON_EXISTANT_MODULE',
297                false,
298                false,
299            ),
300
301            // Test for non-existant modules lazily
302            array(
303                '',
304                'UCP_NON_EXISTANT_CAT',
305                true,
306                false,
307            ),
308            array(
309                'UCP_MAIN_CAT',
310                'UCP_NON_EXISTANT_MODULE',
311                true,
312                false,
313            ),
314        );
315    }
316
317    /**
318    * @dataProvider exists_data_ucp
319    */
320    public function test_exists_ucp($parent, $module, $lazy, $expected)
321    {
322        $this->assertEquals($expected, $this->tool->exists('ucp', $parent, $module, $lazy));
323    }
324
325    public function test_add()
326    {
327        try
328        {
329            $this->tool->add('acp', 0, 'ACP_NEW_CAT');
330        }
331        catch (Exception $e)
332        {
333            $this->fail($e);
334        }
335        $this->assertEquals(true, $this->tool->exists('acp', 0, 'ACP_NEW_CAT'));
336
337        // Should throw an exception when trying to add a module that already exists
338        try
339        {
340            $this->tool->add('acp', 0, 'ACP_NEW_CAT');
341            $this->fail('Exception not thrown');
342        }
343        catch (Exception $e) {}
344
345        try
346        {
347            $this->tool->add('acp', 'ACP_NEW_CAT', array(
348                'module_basename'    => 'acp_new_module',
349                'module_langname'    => 'ACP_NEW_MODULE',
350                'module_mode'        => 'test',
351                'module_auth'        => '',
352            ));
353        }
354        catch (Exception $e)
355        {
356            $this->fail($e);
357        }
358        $this->assertEquals(true, $this->tool->exists('acp', 'ACP_NEW_CAT', 'ACP_NEW_MODULE'));
359
360        // Test adding module when plural parent module_langname exists
361        // PHPBB3-14703
362        // Adding success
363        try
364        {
365            $this->tool->add('acp', 'ACP_FORUM_BASED_PERMISSIONS', array(
366                'module_basename'    => 'acp_new_permissions_module',
367                'module_langname'    => 'ACP_NEW_PERMISSIONS_MODULE',
368                'module_mode'        => 'test',
369                'module_auth'        => '',
370                'after'                => 'ACP_FORUM_BASED_PERMISSIONS_CHILD_1',
371            ));
372        }
373        catch (Exception $e)
374        {
375            $this->fail($e);
376        }
377        $this->assertEquals(true, $this->tool->exists('acp', 'ACP_FORUM_BASED_PERMISSIONS', 'ACP_NEW_PERMISSIONS_MODULE'));
378
379        // Test adding UCP modules
380        // Test adding new UCP category
381        try
382        {
383            $this->tool->add('ucp', 0, 'UCP_NEW_CAT');
384        }
385        catch (Exception $e)
386        {
387            $this->fail($e);
388        }
389        $this->assertEquals(true, $this->tool->exists('ucp', 0, 'UCP_NEW_CAT'));
390
391        // Test adding new UCP subcategory
392        try
393        {
394            $this->tool->add('ucp', 'UCP_NEW_CAT', 'UCP_NEW_SUBCAT');
395        }
396        catch (Exception $e)
397        {
398            $this->fail($e);
399        }
400        $this->assertEquals(true, $this->tool->exists('ucp', 'UCP_NEW_CAT', 'UCP_NEW_SUBCAT'));
401
402        // Test adding new UCP module
403        try
404        {
405            $this->tool->add('ucp', 'UCP_NEW_SUBCAT', array(
406                'module_basename'    => 'ucp_new_module',
407                'module_langname'    => 'UCP_NEW_MODULE',
408                'module_mode'        => 'ucp_test',
409                'module_auth'        => '',
410            ));
411        }
412        catch (Exception $e)
413        {
414            $this->fail($e);
415        }
416        $this->assertEquals(true, $this->tool->exists('ucp', 'UCP_NEW_SUBCAT', 'UCP_NEW_MODULE'));
417
418        // Test adding new UCP module the automatic way, single mode
419        try
420        {
421            $this->tool->add('ucp', 'UCP_NEW_CAT', array(
422                'module_basename'    => '\foo\bar\ucp\ucp_test_module',
423                'modes'                => array('mode_1'),
424            ));
425        }
426        catch (Exception $e)
427        {
428            $this->fail($e);
429        }
430        $this->assertEquals(true, $this->tool->exists('ucp', 'UCP_NEW_CAT', 'UCP_NEW_MODULE_MODE_1'));
431        $this->assertEquals(false, $this->tool->exists('ucp', 'UCP_NEW_CAT', 'UCP_NEW_MODULE_MODE_2'));
432
433        // Test adding new ACP module the automatic way, all modes
434        try
435        {
436            $this->tool->add('acp', 'ACP_NEW_CAT', array(
437                'module_basename' => '\foo\bar\acp\acp_test_module',
438            ));
439        }
440        catch (Exception $e)
441        {
442            $this->fail($e);
443        }
444        $this->assertEquals(true, $this->tool->exists('acp', 'ACP_NEW_CAT', 'ACP_NEW_MODULE_MODE_1'));
445        $this->assertEquals(true, $this->tool->exists('acp', 'ACP_NEW_CAT', 'ACP_NEW_MODULE_MODE_2'));
446    }
447
448    public function test_remove()
449    {
450        try
451        {
452            $this->tool->remove('acp', 'ACP_CAT', 'ACP_MODULE');
453        }
454        catch (Exception $e)
455        {
456            $this->fail($e);
457        }
458        $this->assertEquals(false, $this->tool->exists('acp', 'ACP_CAT', 'ACP_MODULE'));
459    }
460
461    public function test_reverse()
462    {
463        try
464        {
465            $this->tool->add('acp', 0, 'ACP_NEW_CAT');
466        }
467        catch (Exception $e)
468        {
469            $this->fail($e);
470        }
471
472        try
473        {
474            $this->tool->reverse('add', 'acp', 0, 'ACP_NEW_CAT');
475        }
476        catch (Exception $e)
477        {
478            $this->fail($e);
479        }
480        $this->assertFalse($this->tool->exists('acp', 0, 'ACP_NEW_CAT'));
481    }
482}