aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dbal/migrator_test.php
blob: 6416a2fcf7733432cdcd37cb0093ee09dae9713b (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?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';
require_once dirname(__FILE__) . '/migration/dummy.php';
require_once dirname(__FILE__) . '/migration/unfulfillable.php';
require_once dirname(__FILE__) . '/migration/if.php';
require_once dirname(__FILE__) . '/migration/recall.php';
require_once dirname(__FILE__) . '/migration/revert.php';
require_once dirname(__FILE__) . '/migration/revert_with_dependency.php';
require_once dirname(__FILE__) . '/migration/fail.php';
require_once dirname(__FILE__) . '/migration/installed.php';
require_once dirname(__FILE__) . '/migration/schema.php';

class phpbb_dbal_migrator_test extends phpbb_database_test_case
{
	protected $db;
	protected $db_tools;
	protected $migrator;

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

	public function setUp()
	{
		parent::setUp();

		$this->db = $this->new_dbal();
		$factory = new \phpbb\db\tools\factory();
		$this->db_tools = $factory->get($this->db);

		$this->config = new \phpbb\config\db($this->db, new phpbb_mock_cache, 'phpbb_config');

		$tools = array(
			new \phpbb\db\migration\tool\config($this->config),
		);

		$container = new phpbb_mock_container_builder();

		$this->migrator = new \phpbb\db\migrator(
			$container,
			$this->config,
			$this->db,
			$this->db_tools,
			'phpbb_migrations',
			dirname(__FILE__) . '/../../phpBB/',
			'php',
			'phpbb_',
			$tools,
			new \phpbb\db\migration\helper()
		);
		$container->set('migrator', $this->migrator);
		$container->set('dispatcher', new phpbb_mock_event_dispatcher());
		$user = new \phpbb\user('\phpbb\datetime');

		$this->extension_manager = new \phpbb\extension\manager(
			$container,
			$this->db,
			$this->config,
			new phpbb\filesystem(),
			$user,
			'phpbb_ext',
			dirname(__FILE__) . '/../../phpBB/',
			'php',
			null
		);
	}

	public function test_update()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_dummy'));

		// schema
		$this->migrator->update();
		$this->assertFalse($this->migrator->finished());

		$this->assertSqlResultEquals(
			array(array('success' => '1')),
			"SELECT 1 as success
				FROM phpbb_migrations
				WHERE migration_name = 'phpbb_dbal_migration_dummy'
					AND migration_start_time >= " . (time() - 1) . "
					AND migration_start_time <= " . (time() + 1),
			'Start time set correctly'
		);

		// data
		$this->migrator->update();
		$this->assertTrue($this->migrator->finished());

		$this->assertSqlResultEquals(
			array(array('extra_column' => '1')),
			"SELECT extra_column FROM phpbb_config WHERE config_name = 'foo'",
			'Dummy migration created extra_column with value 1 in all rows.'
		);

		$this->assertSqlResultEquals(
			array(array('success' => '1')),
			"SELECT 1 as success
				FROM phpbb_migrations
				WHERE migration_name = 'phpbb_dbal_migration_dummy'
					AND migration_start_time <= migration_end_time
					AND migration_end_time >= " . (time() - 1) . "
					AND migration_end_time <= " . (time() + 1),
			'End time set correctly'
		);

		// cleanup
		$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
	}

	public function test_unfulfillable()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_unfulfillable', 'phpbb_dbal_migration_dummy'));

		while (!$this->migrator->finished())
		{
			$this->migrator->update();
		}

		$this->assertTrue($this->migrator->finished());

		$this->assertSqlResultEquals(
			array(array('extra_column' => '1')),
			"SELECT extra_column FROM phpbb_config WHERE config_name = 'foo'",
			'Dummy migration was run, even though an unfulfillable migration was found.'
		);

		$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
	}

	public function test_if()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_if'));

		// Don't like this, but I'm not sure there is any other way to do this
		global $migrator_test_if_true_failed, $migrator_test_if_false_failed;
		$migrator_test_if_true_failed = true;
		$migrator_test_if_false_failed = false;

		while (!$this->migrator->finished())
		{
			$this->migrator->update();
		}

		$this->assertFalse($migrator_test_if_true_failed, 'True test failed');
		$this->assertFalse($migrator_test_if_false_failed, 'False test failed');
	}

	public function test_recall()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_recall'));

		global $migrator_test_call_input;

		// Run the schema first
		$this->migrator->update();

		$i = 0;
		while (!$this->migrator->finished())
		{
			$this->migrator->update();

			$this->assertSame($i, $migrator_test_call_input);

			$i++;
		}

		$this->assertSame(10, $migrator_test_call_input);
	}

	public function test_revert()
	{
		global $migrator_test_revert_counter;

		// Make sure there are no other migrations in the db, this could cause issues
		$this->db->sql_query("DELETE FROM phpbb_migrations");
		$this->migrator->load_migration_state();

		$migrator_test_revert_counter = 0;

		$this->migrator->set_migrations(array('phpbb_dbal_migration_revert', 'phpbb_dbal_migration_revert_with_dependency'));

		$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert'));
		$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency'));

		// Install the migration first
		while (!$this->migrator->finished())
		{
			$this->migrator->update();
		}

		$this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false);
		$this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency') !== false);

		$this->assertSqlResultEquals(
			array(array('bar_column' => '1')),
			"SELECT bar_column FROM phpbb_config WHERE config_name = 'foo'",
			'Installing revert migration failed to create bar_column.'
		);

		$this->assertTrue(isset($this->config['foobartest']));

		while ($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false)
		{
			$this->migrator->revert('phpbb_dbal_migration_revert');
		}

		$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert'));
		$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency'));

		$this->assertFalse(isset($this->config['foobartest']));

		$sql = 'SELECT * FROM phpbb_config';
		$result = $this->db->sql_query_limit($sql, 1);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if (isset($row['bar_column']))
		{
			$this->fail('Revert did not remove test_column.');
		}

		$this->assertEquals(1, $migrator_test_revert_counter, 'Revert did call custom function again');
	}

	public function test_fail()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_fail'));

		$this->assertFalse(isset($this->config['foobar3']));

		try
		{
			while (!$this->migrator->finished())
			{
				$this->migrator->update();
			}
		}
		catch (\phpbb\db\migration\exception $e) {}

		// Failure should have caused an automatic roll-back, so this should not exist.
		$this->assertFalse(isset($this->config['foobar3']));

		$sql = 'SELECT * FROM phpbb_config';
		$result = $this->db->sql_query_limit($sql, 1);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if (isset($row['test_column']))
		{
			$this->fail('Revert did not remove test_column.');
		}
	}

	public function test_installed()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_installed'));

		global $migrator_test_installed_failed;
		$migrator_test_installed_failed = false;

		while (!$this->migrator->finished())
		{
			$this->migrator->update();
		}

		$this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_installed') !== false);

		if ($migrator_test_installed_failed)
		{
			$this->fail('Installed test failed');
		}
	}

	public function test_schema()
	{
		$this->migrator->set_migrations(array('phpbb_dbal_migration_schema'));

		while (!$this->migrator->finished())
		{
			$this->migrator->update();
		}

		$this->assertTrue($this->db_tools->sql_column_exists('phpbb_config', 'test_column1'));
		$this->assertTrue($this->db_tools->sql_table_exists('phpbb_foobar'));

		while ($this->migrator->migration_state('phpbb_dbal_migration_schema'))
		{
			$this->migrator->revert('phpbb_dbal_migration_schema');
		}

		$this->assertFalse($this->db_tools->sql_column_exists('phpbb_config', 'test_column1'));
		$this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar'));
	}
}