aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dbal/migrator_tool_module_test.php
blob: 462b521f1a5e2f172599df3773386fb8a7583ecb (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?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.
*
*/

require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';

class phpbb_dbal_migrator_tool_module_test extends phpbb_database_test_case
{
	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/migrator_module.xml');
	}

	public function setup()
	{
		// Need global $db, $user for delete_module function in acp_modules
		global $phpbb_root_path, $phpEx, $skip_add_log, $db, $user, $phpbb_log;

		parent::setup();

		// Force add_log function to not be used
		$skip_add_log = true;

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

		$cache = new phpbb_mock_cache;
		$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
		$auth = $this->getMock('\phpbb\auth\auth');
		$phpbb_log = new \phpbb\log\log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE);

		$this->tool = new \phpbb\db\migration\tool\module($this->db, $this->cache, $this->user, $phpbb_root_path, $phpEx, 'phpbb_modules');
	}

	public function exists_data_acp()
	{
		return array(
			// Test the existing category
			array(
				'',
				'ACP_CAT',
				true,
			),
			array(
				0,
				'ACP_CAT',
				true,
			),

			// Test the existing module
			array(
				'',
				'ACP_MODULE',
				false,
			),
			array(
				false,
				'ACP_MODULE',
				true,
			),
			array(
				'ACP_CAT',
				'ACP_MODULE',
				true,
			),

			// Test for non-existant modules
			array(
				'',
				'ACP_NON_EXISTANT_CAT',
				false,
			),
			array(
				'ACP_CAT',
				'ACP_NON_EXISTANT_MODULE',
				false,
			),
		);
	}

	/**
	* @dataProvider exists_data_acp
	*/
	public function test_exists($parent, $module, $expected)
	{
		$this->assertEquals($expected, $this->tool->exists('acp', $parent, $module));
	}

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

		// Should throw an exception when trying to add a module that already exists
		try
		{
			$this->tool->add('acp', 0, 'ACP_NEW_CAT');
			$this->fail('Exception not thrown');
		}
		catch (Exception $e) {}

		try
		{
			$this->tool->add('acp', 'ACP_NEW_CAT', array(
				'module_basename'	=> 'acp_new_module',
				'module_langname'	=> 'ACP_NEW_MODULE',
				'module_mode'		=> 'test',
				'module_auth'		=> '',
			));
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('acp', 'ACP_NEW_CAT', 'ACP_NEW_MODULE'));

		// Test adding module when plural parent module_langname exists
		// PHPBB3-14703
		// Adding fail
		try
		{
			$this->tool->add('acp', 'ACP_FORUM_BASED_PERMISSIONS', array(
				'module_basename'	=> 'acp_new_permissions_module',
				'module_langname'	=> 'ACP_NEW_PERMISSIONS_MODULE',
				'module_mode'		=> 'test',
				'module_auth'		=> '',
			));
			$this->fail('Exception not thrown');
		}
		catch (Exception $e)
		{
			$this->assertEquals('phpbb\db\migration\exception', get_class($e));
			$this->assertEquals('MODULE_EXIST_MULTIPLE', $e->getMessage());
		}

		// Test adding module when plural parent module_langname exists
		// PHPBB3-14703
		// Adding success
		try
		{
			$this->tool->add('acp', 'ACP_FORUM_BASED_PERMISSIONS', array(
				'module_basename'	=> 'acp_new_permissions_module',
				'module_langname'	=> 'ACP_NEW_PERMISSIONS_MODULE',
				'module_mode'		=> 'test',
				'module_auth'		=> '',
				'after'				=> 'ACP_FORUM_BASED_PERMISSIONS_CHILD_1',
			));
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('acp', 'ACP_FORUM_BASED_PERMISSIONS', 'ACP_NEW_PERMISSIONS_MODULE'));

		// Test adding UCP modules
		// Test adding new UCP category
		try
		{
			$this->tool->add('ucp', 0, 'UCP_NEW_CAT');
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('ucp', 0, 'UCP_NEW_CAT'));

		// Test adding new UCP subcategory
		try
		{
			$this->tool->add('ucp', 'UCP_NEW_CAT', 'UCP_NEW_SUBCAT');
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('ucp', 'UCP_NEW_CAT', 'UCP_NEW_SUBCAT'));

		// Test adding new UCP module
		try
		{
			$this->tool->add('ucp', 'UCP_NEW_SUBCAT', array(
				'module_basename'	=> 'ucp_new_module',
				'module_langname'	=> 'UCP_NEW_MODULE',
				'module_mode'		=> 'ucp_test',
				'module_auth'		=> '',
			));
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}
		$this->assertEquals(true, $this->tool->exists('ucp', 'UCP_NEW_SUBCAT', 'UCP_NEW_MODULE'));
	}

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

	public function test_reverse()
	{
		try
		{
			$this->tool->add('acp', 0, 'ACP_NEW_CAT');
		}
		catch (Exception $e)
		{
			$this->fail($e);
		}

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