Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
88 / 88
100.00% covered (success)
100.00%
16 / 16
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_extension_manager_test
100.00% covered (success)
100.00%
88 / 88
100.00% covered (success)
100.00%
16 / 16
17
100.00% covered (success)
100.00%
1 / 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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_all_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_all_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_all_configured
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_is_enabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_is_disabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_is_purged
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_is_configured
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_is_available
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_enable
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_enable_not_enableable
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_disable
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_purge
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 test_enabled_no_cache
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create_extension_manager
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
2
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/bar/ext.php';
15require_once __DIR__ . '/ext/vendor2/foo/ext.php';
16require_once __DIR__ . '/ext/vendor3/foo/ext.php';
17require_once __DIR__ . '/ext/vendor/moo/ext.php';
18
19class phpbb_extension_manager_test extends phpbb_database_test_case
20{
21    protected $extension_manager;
22    protected $class_loader;
23
24    public function getDataSet()
25    {
26        return $this->createXMLDataSet(__DIR__ . '/fixtures/extensions.xml');
27    }
28
29    protected function setUp(): void
30    {
31        parent::setUp();
32
33        $this->extension_manager = $this->create_extension_manager();
34    }
35
36    public function test_all_available()
37    {
38        // barfoo and vendor3/bar should not listed due to missing composer.json. barfoo also has incorrect dir structure.
39        $this->assertEquals(array('vendor/moo', 'vendor2/bar', 'vendor2/foo', 'vendor3/foo', 'vendor4/bar', 'vendor5/foo'), array_keys($this->extension_manager->all_available()));
40    }
41
42    public function test_all_enabled()
43    {
44        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
45    }
46
47    public function test_all_configured()
48    {
49        $this->assertEquals(array('vendor/moo', 'vendor2/foo'), array_keys($this->extension_manager->all_configured()));
50    }
51
52    public function test_is_enabled()
53    {
54        $this->assertSame(true, $this->extension_manager->is_enabled('vendor2/foo'));
55        $this->assertSame(false, $this->extension_manager->is_enabled('vendor/moo'));
56        $this->assertSame(false, $this->extension_manager->is_enabled('vendor2/bar'));
57        $this->assertSame(false, $this->extension_manager->is_enabled('bertie/worlddominationplan'));
58    }
59
60    public function test_is_disabled()
61    {
62        $this->assertSame(false, $this->extension_manager->is_disabled('vendor2/foo'));
63        $this->assertSame(true, $this->extension_manager->is_disabled('vendor/moo'));
64        $this->assertSame(false, $this->extension_manager->is_disabled('vendor2/bar'));
65        $this->assertSame(false, $this->extension_manager->is_disabled('bertie/worlddominationplan'));
66    }
67
68    public function test_is_purged()
69    {
70        $this->assertSame(false, $this->extension_manager->is_purged('vendor2/foo'));
71        $this->assertSame(false, $this->extension_manager->is_purged('vendor/moo'));
72        $this->assertSame(true, $this->extension_manager->is_purged('vendor2/bar'));
73        $this->assertSame(false, $this->extension_manager->is_purged('bertie/worlddominationplan'));
74    }
75
76    public function test_is_configured()
77    {
78        $this->assertSame(true, $this->extension_manager->is_configured('vendor2/foo'));
79        $this->assertSame(true, $this->extension_manager->is_configured('vendor/moo'));
80        $this->assertSame(false, $this->extension_manager->is_configured('vendor2/bar'));
81        $this->assertSame(false, $this->extension_manager->is_configured('bertie/worlddominationplan'));
82    }
83
84    public function test_is_available()
85    {
86        $this->assertSame(true, $this->extension_manager->is_available('vendor2/foo'));
87        $this->assertSame(true, $this->extension_manager->is_available('vendor/moo'));
88        $this->assertSame(true, $this->extension_manager->is_available('vendor2/bar'));
89        $this->assertSame(false, $this->extension_manager->is_available('bertie/worlddominationplan'));
90    }
91
92    public function test_enable()
93    {
94        vendor2\bar\ext::$state = 0;
95
96        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
97        $this->extension_manager->enable('vendor2/bar');
98
99        // We should see the extension as being disabled
100        $this->assertEquals(array('vendor2/bar', 'vendor2/foo'), array_keys($this->create_extension_manager()->all_enabled()));
101
102        $this->assertEquals(array('vendor/moo', 'vendor2/bar', 'vendor2/foo'), array_keys($this->extension_manager->all_configured()));
103
104        $this->assertEquals(4, vendor2\bar\ext::$state);
105    }
106
107    public function test_enable_not_enableable()
108    {
109        vendor3\foo\ext::$enabled = false;
110
111        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
112        $this->extension_manager->enable('vendor3/foo');
113        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
114        $this->assertEquals(array('vendor/moo', 'vendor2/foo'), array_keys($this->extension_manager->all_configured()));
115
116        $this->assertSame(false, vendor3\foo\ext::$enabled);
117    }
118
119    public function test_disable()
120    {
121        vendor2\foo\ext::$disabled = false;
122
123        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
124        $this->extension_manager->disable('vendor2/foo');
125
126        $this->assertEquals([], array_keys($this->extension_manager->all_enabled()));
127
128        $this->assertEquals(array('vendor/moo', 'vendor2/foo'), array_keys($this->extension_manager->all_configured()));
129
130        $this->assertTrue(vendor2\foo\ext::$disabled);
131    }
132
133    public function test_purge()
134    {
135        vendor\moo\ext::$purged = false;
136
137        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
138        $this->assertEquals(array('vendor/moo', 'vendor2/foo'), array_keys($this->extension_manager->all_configured()));
139        $this->extension_manager->purge('vendor/moo');
140        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_enabled()));
141        $this->assertEquals(array('vendor2/foo'), array_keys($this->extension_manager->all_configured()));
142
143        $this->assertTrue(vendor\moo\ext::$purged);
144    }
145
146    public function test_enabled_no_cache()
147    {
148        $extension_manager = $this->create_extension_manager(false);
149
150        $this->assertEquals(array('vendor2/foo'), array_keys($extension_manager->all_enabled()));
151    }
152
153    protected function create_extension_manager($with_cache = true)
154    {
155        $phpbb_root_path = __DIR__ . './../../phpBB/';
156        $php_ext = 'php';
157        $table_prefix = 'phpbb_';
158
159        $config = new \phpbb\config\config(array('version' => PHPBB_VERSION));
160        $db = $this->new_dbal();
161        $db_doctrine = $this->new_doctrine_dbal();
162        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
163        $factory = new \phpbb\db\tools\factory();
164        $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $php_ext);
165        $db_tools = $factory->get($db_doctrine);
166        $db_tools->set_table_prefix($table_prefix);
167
168        $container = new phpbb_mock_container_builder();
169
170        $migrator = new \phpbb\db\migrator(
171            $container,
172            $config,
173            $db,
174            $db_tools,
175            'phpbb_migrations',
176            $phpbb_root_path,
177            $php_ext,
178            $table_prefix,
179            self::get_core_tables(),
180            array(),
181            new \phpbb\db\migration\helper()
182        );
183        $container->set('migrator', $migrator);
184
185        return new \phpbb\extension\manager(
186            $container,
187            $db,
188            $config,
189            $finder_factory,
190            'phpbb_ext',
191            __DIR__ . '/',
192            ($with_cache) ? new \phpbb\cache\service(new phpbb_mock_cache(), $config, $db, $phpbb_dispatcher, $phpbb_root_path, $php_ext) : null
193        );
194    }
195}