aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dbal/migrator_tool_permission_test.php
blob: d84f6a68ff4dcb9c8165b323980d0224c34d00f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case
{
	public $group_ids = array(
		'REGISTERED' => 2,
		'GLOBAL_MODERATORS' => 4,
		'ADMINISTRATORS' => 5,
	);

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/migrator_permission.xml');
	}

	public function setup()
	{
		// Global $db and $cache are needed in acp/auth.php constructor
		global $phpbb_root_path, $phpEx, $db, $cache;

		parent::setup();

		$db = $this->db = $this->new_dbal();
		$cache = $this->cache = new \phpbb\cache\service(new \phpbb\cache\driver\dummy(), new \phpbb\config\config(array()), $this->db, $phpbb_root_path, $phpEx);
		$this->auth = new \phpbb\auth\auth();

		$this->tool = new \phpbb\db\migration\tool\permission($this->db, $this->cache, $this->auth, $phpbb_root_path, $phpEx);
	}

	public function exists_data()
	{
		return array(
			array(
				'global',
				true,
				true,
			),
			array(
				'local',
				false,
				true,
			),
			array(
				'both',
				true,
				true,
			),
			array(
				'both',
				false,
				true,
			),
			array(
				'does_not_exist',
				true,
				false,
			),
		);
	}

	/**
	* @dataProvider exists_data
	*/
	public function test_exists($auth_option, $global, $expected)
	{
		$this->assertEquals($expected, $this->tool->exists($auth_option, $global));
	}

	public function test_add()
	{
		try
		{
			$this->tool->add('new', true);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('new', true));
		$this->assertEquals(false, $this->tool->exists('new', false));

		try
		{
			$this->tool->add('new', false);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('new', false));

		// Should fail (duplicate)
		try
		{
			$this->tool->add('new', true);
			$this->fail('Did not throw exception on duplicate');
		}
		catch (Exception $e) {}
	}

	public function test_remove()
	{
		try
		{
			$this->tool->remove('global', true);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(false, $this->tool->exists('global', true));

		try
		{
			$this->tool->remove('both', false);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(false, $this->tool->exists('both', false));

		// Should fail (does not exist)
		try
		{
			$this->tool->remove('new', true);
			$this->fail('Did not throw exception on duplicate');
		}
		catch (Exception $e) {}
	}

	public function test_reverse()
	{
		try
		{
			$this->tool->reverse('remove', 'global_test', true);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertTrue($this->tool->exists('global_test', true));

		try
		{
			$this->tool->reverse('add', 'global_test', true);
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertFalse($this->tool->exists('global_test', true));
	}

	public function test_permission_set_data()
	{
		return array(
			array(
				'ADMINISTRATORS',
				'a_test',
				'group',
				true,
			),
			array(
				'GLOBAL_MODERATORS',
				'm_test',
				'group',
				true,
			),
			array(
				'REGISTERED',
				'u_test',
				'group',
				true,
			),
		);
	}

	/**
	* @dataProvider test_permission_set_data
	*/
	public function test_permission_set($group_name, $auth_option, $type, $has_permission)
	{
		$this->tool->permission_set($group_name, $auth_option, $type, $has_permission);
		$administrators_perm = $this->auth->acl_group_raw_data($this->group_ids['ADMINISTRATORS'], $auth_option);
		$global_moderators_perm = $this->auth->acl_group_raw_data($this->group_ids['GLOBAL_MODERATORS'], $auth_option);
		$registered_users_perm = $this->auth->acl_group_raw_data($this->group_ids['REGISTERED'], $auth_option);

		switch($group_name)
		{
			case 'GLOBAL_MODERATORS':
				$this->assertEquals(false, empty($administrators_perm), 'm_test is not empty for Administrators');
				$this->assertEquals(false, empty($global_moderators_perm), 'm_test is not empty for Global moderators');
				$this->assertEquals(true, empty($registered_users_perm), 'm_test empty for Registered users');
			break;

			case 'ADMINISTRATORS':
				$this->assertEquals(false, empty($administrators_perm), 'a_test is not empty for Administrators');
				$this->assertEquals(true, empty($global_moderators_perm), 'a_test is empty for Global moderators');
				$this->assertEquals(true, empty($registered_users_perm), 'a_test is empty for Registered users');
			break;

			case 'REGISTERED':
				$this->assertEquals(false, empty($administrators_perm), 'u_test is not empty for Administrators');
				$this->assertEquals(false, empty($global_moderators_perm), 'u_test is not empty for Global moderators');
				$this->assertEquals(false, empty($registered_users_perm), 'u_test is not empty for Registered users');
			break;
		}
	}
}