Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.48% covered (danger)
42.48%
48 / 113
45.45% covered (danger)
45.45%
5 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_dbal_migrator_tool_permission_test
42.48% covered (danger)
42.48%
48 / 113
45.45% covered (danger)
45.45%
5 / 11
114.12
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 exists_data
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
 test_exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_add
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
4.59
 test_remove
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
4.77
 test_reverse
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
4.12
 data_test_permission_set
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 test_permission_set
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
4
 data_test_permission_role_exists
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 test_permission_role_exists
100.00% covered (success)
100.00%
1 / 1
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_dbal_migrator_tool_permission_test extends phpbb_database_test_case
15{
16    /** @var \phpbb\auth\auth */
17    protected $auth;
18
19    /** @var \phpbb\db\driver\driver_interface */
20    protected $db;
21
22    /** @var \phpbb\cache\service */
23    protected $cache;
24
25    /** @var \phpbb\db\migration\tool\permission */
26    protected $tool;
27
28    public $group_ids = array(
29        'REGISTERED' => 2,
30        'GLOBAL_MODERATORS' => 4,
31        'ADMINISTRATORS' => 5,
32    );
33
34    public function getDataSet()
35    {
36        return $this->createXMLDataSet(__DIR__.'/fixtures/migrator_permission.xml');
37    }
38
39    protected function setUp(): void
40    {
41        // Global $db and $cache are needed in acp/auth.php constructor
42        global $phpbb_root_path, $phpEx, $db, $cache;
43
44        parent::setUp();
45
46        $db = $this->db = $this->new_dbal();
47        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
48        $cache = $this->cache = new \phpbb\cache\service(new \phpbb\cache\driver\dummy(), new \phpbb\config\config(array()), $this->db, $phpbb_dispatcher, $phpbb_root_path, $phpEx);
49        $this->auth = new \phpbb\auth\auth();
50
51        $this->tool = new \phpbb\db\migration\tool\permission($this->db, $this->cache, $this->auth, $phpbb_root_path, $phpEx);
52    }
53
54    public static function exists_data()
55    {
56        return array(
57            array(
58                'global',
59                true,
60                true,
61            ),
62            array(
63                'local',
64                false,
65                true,
66            ),
67            array(
68                'both',
69                true,
70                true,
71            ),
72            array(
73                'both',
74                false,
75                true,
76            ),
77            array(
78                'does_not_exist',
79                true,
80                false,
81            ),
82        );
83    }
84
85    /**
86    * @dataProvider exists_data
87    */
88    public function test_exists($auth_option, $global, $expected)
89    {
90        $this->assertEquals($expected, $this->tool->exists($auth_option, $global));
91    }
92
93    public function test_add()
94    {
95        try
96        {
97            $this->tool->add('new', true);
98        }
99        catch (Exception $e)
100        {
101            $this->fail($e);
102        }
103        $this->assertEquals(true, $this->tool->exists('new', true));
104        $this->assertEquals(false, $this->tool->exists('new', false));
105
106        try
107        {
108            $this->tool->add('new', false);
109        }
110        catch (Exception $e)
111        {
112            $this->fail($e);
113        }
114        $this->assertEquals(true, $this->tool->exists('new', false));
115
116        // Should fail (duplicate)
117        try
118        {
119            $this->tool->add('new', true);
120            $this->fail('Did not throw exception on duplicate');
121        }
122        catch (Exception $e) {}
123    }
124
125    public function test_remove()
126    {
127        try
128        {
129            $this->tool->remove('global', true);
130        }
131        catch (Exception $e)
132        {
133            $this->fail($e);
134        }
135        $this->assertEquals(false, $this->tool->exists('global', true));
136
137        try
138        {
139            $this->tool->remove('both', false);
140        }
141        catch (Exception $e)
142        {
143            $this->fail($e);
144        }
145        $this->assertEquals(false, $this->tool->exists('both', false));
146
147        // Should fail (does not exist)
148        try
149        {
150            $this->tool->remove('new', true);
151            $this->fail('Did not throw exception on duplicate');
152        }
153        catch (Exception $e) {}
154    }
155
156    public function test_reverse()
157    {
158        try
159        {
160            $this->tool->reverse('remove', 'global_test', true);
161        }
162        catch (Exception $e)
163        {
164            $this->fail($e);
165        }
166        $this->assertTrue($this->tool->exists('global_test', true));
167
168        try
169        {
170            $this->tool->reverse('add', 'global_test', true);
171        }
172        catch (Exception $e)
173        {
174            $this->fail($e);
175        }
176        $this->assertFalse($this->tool->exists('global_test', true));
177    }
178
179    public static function data_test_permission_set()
180    {
181        return array(
182            array(
183                'ADMINISTRATORS',
184                'a_test',
185                'group',
186                true,
187            ),
188            array(
189                'GLOBAL_MODERATORS',
190                'm_test',
191                'group',
192                true,
193            ),
194            array(
195                'REGISTERED',
196                'u_test',
197                'group',
198                true,
199            ),
200        );
201    }
202
203    /**
204    * @dataProvider data_test_permission_set
205    */
206    public function test_permission_set($group_name, $auth_option, $type, $has_permission)
207    {
208        $this->tool->permission_set($group_name, $auth_option, $type, $has_permission);
209        $administrators_perm = $this->auth->acl_group_raw_data($this->group_ids['ADMINISTRATORS'], $auth_option);
210        $global_moderators_perm = $this->auth->acl_group_raw_data($this->group_ids['GLOBAL_MODERATORS'], $auth_option);
211        $registered_users_perm = $this->auth->acl_group_raw_data($this->group_ids['REGISTERED'], $auth_option);
212
213        switch($group_name)
214        {
215            case 'GLOBAL_MODERATORS':
216                $this->assertEquals(false, empty($administrators_perm), 'm_test is not empty for Administrators');
217                $this->assertEquals(false, empty($global_moderators_perm), 'm_test is not empty for Global moderators');
218                $this->assertEquals(true, empty($registered_users_perm), 'm_test empty for Registered users');
219            break;
220
221            case 'ADMINISTRATORS':
222                $this->assertEquals(false, empty($administrators_perm), 'a_test is not empty for Administrators');
223                $this->assertEquals(true, empty($global_moderators_perm), 'a_test is empty for Global moderators');
224                $this->assertEquals(true, empty($registered_users_perm), 'a_test is empty for Registered users');
225            break;
226
227            case 'REGISTERED':
228                $this->assertEquals(false, empty($administrators_perm), 'u_test is not empty for Administrators');
229                $this->assertEquals(false, empty($global_moderators_perm), 'u_test is not empty for Global moderators');
230                $this->assertEquals(false, empty($registered_users_perm), 'u_test is not empty for Registered users');
231            break;
232        }
233    }
234
235    public static function data_test_permission_role_exists()
236    {
237        return array(
238            array('ROLE_MOD_FULL', true),
239            array('ROLE_USER_FULL', true),
240            array('ROLE_ADMIN_STANDARD', true),
241            array('ROLE_DOES_NOT_EXIST', false),
242        );
243    }
244
245    /**
246     * @dataProvider data_test_permission_role_exists
247     */
248    public function test_permission_role_exists($role_name, $expected)
249    {
250        $this->assertEquals($expected, $this->tool->role_exists($role_name));
251    }
252}