Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.61% covered (warning)
67.61%
144 / 213
30.77% covered (danger)
30.77%
4 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_extension_metadata_manager_test
67.61% covered (warning)
67.61%
144 / 213
30.77% covered (danger)
30.77%
4 / 13
35.99
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%
76 / 76
100.00% covered (success)
100.00%
1 / 1
1
 test_bar
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 test_foo
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
2.15
 validator_non_existing_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 test_validator_non_existing
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 test_validator_non_existing_authors
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
3.02
 validator_invalid_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 test_validator_invalid
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 test_validator_valid
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
2.02
 validator_requirements_data
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
2
 test_validator_requirements
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
2.00
 get_metadata_manager
100.00% covered (success)
100.00%
4 / 4
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
14class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
15{
16    protected $class_loader;
17    protected $extension_manager;
18
19    protected $cache;
20    protected $config;
21    protected $db;
22    protected $db_doctrine;
23    protected $db_tools;
24    protected $table_prefix;
25    protected $phpbb_root_path;
26    protected $phpEx;
27    protected $migrator;
28    protected $template;
29    protected $user;
30
31    public function getDataSet()
32    {
33        return $this->createXMLDataSet(__DIR__ . '/fixtures/extensions.xml');
34    }
35
36    protected function setUp(): void
37    {
38        parent::setUp();
39
40        $this->table_prefix = 'phpbb_';
41        $this->config = new \phpbb\config\config(array(
42            'version'        => '3.1.0',
43        ));
44        $this->db = $this->new_dbal();
45        $this->db_doctrine = $this->new_doctrine_dbal();
46        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
47        $factory = new \phpbb\db\tools\factory();
48        $this->db_tools = $factory->get($this->db_doctrine);
49        $this->db_tools->set_table_prefix($this->table_prefix);
50        $finder_factory = $this->createMock('\phpbb\finder\factory');
51        $this->phpbb_root_path = __DIR__ . '/';
52        $this->phpEx = 'php';
53
54        $this->cache =  new \phpbb\cache\service(new phpbb_mock_cache(), $this->config, $this->db, $phpbb_dispatcher, $this->phpbb_root_path, $this->phpEx);
55
56
57        $container = new phpbb_mock_container_builder();
58        $cache_path = $this->phpbb_root_path . 'cache/twig';
59        $context = new \phpbb\template\context();
60        $loader = new \phpbb\template\twig\loader('');
61        $filesystem = new \phpbb\filesystem\filesystem();
62        $phpbb_path_helper = new \phpbb\path_helper(
63            new \phpbb\symfony_request(
64                new phpbb_mock_request()
65            ),
66            $this->createMock('\phpbb\request\request'),
67            $this->phpbb_root_path,
68            $this->phpEx
69        );
70        $log = new \phpbb\log\dummy();
71        $assets_bag = new \phpbb\template\assets_bag();
72        $twig = new \phpbb\template\twig\environment(
73            $assets_bag,
74            $this->config,
75            $filesystem,
76            $phpbb_path_helper,
77            $cache_path,
78            null,
79            $loader,
80            $phpbb_dispatcher,
81            array(
82                'cache'            => false,
83                'debug'            => false,
84                'auto_reload'    => true,
85                'autoescape'    => false,
86            )
87        );
88
89        $this->migrator = new \phpbb\db\migrator(
90            $container,
91            $this->config,
92            $this->db,
93            $this->db_tools,
94            'phpbb_migrations',
95            $this->phpbb_root_path,
96            'php',
97            $this->table_prefix,
98            self::get_core_tables(),
99            array(),
100            new \phpbb\db\migration\helper()
101        );
102        $container->set('migrator', $this->migrator);
103
104        $this->extension_manager = new \phpbb\extension\manager(
105            $container,
106            $this->db,
107            $this->config,
108            $finder_factory,
109            'phpbb_ext',
110            $this->phpbb_root_path,
111            $this->cache
112        );
113
114        global $phpbb_root_path;
115
116        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $this->phpEx);
117        $lang_loader->set_extension_manager($this->extension_manager);
118        $lang = new \phpbb\language\language($lang_loader);
119        $this->user = new \phpbb\user($lang, '\phpbb\datetime');
120
121        $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
122        $twig->setLexer(new \phpbb\template\twig\lexer($twig));
123    }
124
125    // Should fail from missing composer.json
126    public function test_bar()
127    {
128        $ext_name = 'vendor3/bar';
129
130        $manager = $this->get_metadata_manager($ext_name);
131
132        try
133        {
134            $manager->get_metadata();
135        }
136        catch (\phpbb\extension\exception $e)
137        {
138            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
139            $this->assertEquals($message, $this->user->lang('FILE_NOT_FOUND', $this->phpbb_root_path . $this->extension_manager->get_extension_path($ext_name) . 'composer.json'));
140        }
141    }
142
143    // Should be the same as a direct json_decode of the composer.json file
144    public function test_foo()
145    {
146        $ext_name = 'vendor2/foo';
147
148        $manager = $this->get_metadata_manager($ext_name);
149
150        try
151        {
152            $metadata = $manager->get_metadata();
153        }
154        catch (\phpbb\extension\exception $e)
155        {
156            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
157            $this->fail($message);
158        }
159
160        $json = json_decode(file_get_contents($this->phpbb_root_path . 'ext/vendor2/foo/composer.json'), true);
161        array_walk_recursive($json, array($manager, 'sanitize_json'));
162
163        $this->assertEquals($metadata, $json);
164    }
165
166    public static function validator_non_existing_data()
167    {
168        return array(
169            array('name'),
170            array('type'),
171            array('license'),
172            array('version'),
173        );
174    }
175
176    /**
177    * @dataProvider validator_non_existing_data
178    */
179    public function test_validator_non_existing($field_name)
180    {
181        $manager = $this->get_metadata_manager('validator');
182        try
183        {
184            $manager->validate($field_name);
185            $this->fail('Exception not triggered');
186        }
187        catch (\phpbb\extension\exception $e)
188        {
189            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
190            $this->assertEquals($message, $this->user->lang('META_FIELD_NOT_SET', $field_name));
191        }
192    }
193
194    public function test_validator_non_existing_authors()
195    {
196        $manager = $this->get_metadata_manager('validator');
197        try
198        {
199            $manager->validate_authors();
200            $this->fail('Exception not triggered');
201        }
202        catch (\phpbb\extension\exception $e)
203        {
204            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
205            $this->assertEquals($message, $this->user->lang('META_FIELD_NOT_SET', 'authors'));
206        }
207
208        $manager->merge_metadata(array(
209            'authors'    => array(
210                array(),
211            ),
212        ));
213
214        try
215        {
216            $manager->validate_authors();
217            $this->fail('Exception not triggered');
218        }
219        catch (\phpbb\extension\exception $e)
220        {
221            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
222            $this->assertEquals($message, $this->user->lang('META_FIELD_NOT_SET', 'author name'));
223        }
224    }
225
226    public static function validator_invalid_data()
227    {
228        return array(
229            array('name', 'asdf'),
230            array('type', 'asdf'),
231            array('license', ''),
232            array('version', ''),
233        );
234    }
235
236    /**
237     * @dataProvider validator_invalid_data
238     */
239    public function test_validator_invalid($field_name, $field_value)
240    {
241        $manager = $this->get_metadata_manager('validator');
242
243        // Invalid data
244        $manager->set_metadata(array(
245            $field_name        => $field_value,
246        ));
247
248        try
249        {
250            $manager->validate($field_name);
251            $this->fail('Exception not triggered');
252        }
253        catch (\phpbb\extension\exception $e)
254        {
255            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
256            $this->assertEquals($message, $this->user->lang('META_FIELD_INVALID', $field_name));
257        }
258    }
259
260    public function test_validator_valid()
261    {
262        $ext_name = 'validator';
263
264        $manager = $this->get_metadata_manager($ext_name);
265
266        // Valid data
267        $manager->set_metadata(array(
268            'name'        => 'test/foo',
269            'type'        => 'phpbb-extension',
270            'license'    => 'GPL v2',
271            'version'    => '1.0.0',
272        ));
273
274        try
275        {
276            $this->assertEquals(true, $manager->validate('enable'));
277        }
278        catch (\phpbb\extension\exception $e)
279        {
280            $message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
281        }
282    }
283
284    public static function validator_requirements_data()
285    {
286        return array(
287            array(
288                '10.0.0',
289                '100.2.0',
290                false,
291                false,
292                'Versions are not compared at the moment',
293            ),
294            array(
295                '5.3.0',
296                '3.1.0-beta',
297                true,
298                true,
299            ),
300            array(
301                '>' . phpversion(),
302                '>3.1.0',
303                false,
304                false,
305                'Versions are not compared at the moment',
306            ),
307            array(
308                '<' . phpversion(),
309                '<3.1.0',
310                false,
311                false,
312                'Versions are not compared at the moment',
313            ),
314            array(
315                phpversion(),
316                '3.1.0',
317                true,
318                true,
319            ),
320            array(
321                '>=' . phpversion(),
322                '>=3.1.0',
323                true,
324                true,
325            ),
326            array(
327                '<=' . phpversion(),
328                '<=3.1.0',
329                true,
330                true,
331            ),
332        );
333    }
334
335    /**
336    * @dataProvider validator_requirements_data
337    */
338    public function test_validator_requirements($php_version, $phpbb_version, $expected_php, $expected_phpbb, $incomplete_reason = '')
339    {
340        if ($incomplete_reason)
341        {
342            $this->markTestIncomplete($incomplete_reason);
343        }
344
345        $ext_name = 'validator';
346        $manager = $this->get_metadata_manager($ext_name);
347        // Too high of requirements
348        $manager->merge_metadata(array(
349            'require'        => array(
350                'php'        => $php_version,
351            ),
352            'extra'        => array(
353                'soft-require'    => array(
354                    'phpbb/phpbb'        => $phpbb_version, // config is set to 3.1.0
355                ),
356            ),
357        ));
358
359        $this->assertEquals($expected_php, $manager->validate_require_php());
360        $this->assertEquals($expected_phpbb, $manager->validate_require_phpbb());
361    }
362
363    /**
364    * Get an instance of the metadata manager
365    *
366    * @param string $ext_name
367    * @return phpbb_mock_metadata_manager
368    */
369    private function get_metadata_manager($ext_name)
370    {
371        return new phpbb_mock_metadata_manager(
372            $ext_name,
373            $this->extension_manager->get_extension_path($ext_name, true)
374        );
375    }
376}