diff options
Diffstat (limited to 'tests/migrator')
-rw-r--r-- | tests/migrator/convert_timezones_test.php | 98 | ||||
-rw-r--r-- | tests/migrator/fixtures/convert_timezones.xml | 66 | ||||
-rw-r--r-- | tests/migrator/get_schema_steps_test.php | 204 | ||||
-rw-r--r-- | tests/migrator/schema_generator_test.php | 138 |
4 files changed, 506 insertions, 0 deletions
diff --git a/tests/migrator/convert_timezones_test.php b/tests/migrator/convert_timezones_test.php new file mode 100644 index 0000000000..7501ed2ed0 --- /dev/null +++ b/tests/migrator/convert_timezones_test.php @@ -0,0 +1,98 @@ +<?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_migrator_convert_timezones_test extends phpbb_database_test_case +{ + protected $notifications, $db, $container, $user, $config, $auth, $cache; + + public function getDataSet() + { + $this->db = $this->new_dbal(); + $db_tools = new \phpbb\db\tools($this->db); + + // user_dst doesn't exist anymore, must re-add it to test this + $db_tools->sql_column_add('phpbb_users', 'user_dst', array('BOOL', 1)); + + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/convert_timezones.xml'); + } + + public function revert_schema() + { + return array( + 'drop_columns' => array( + $this->table_prefix . 'users' => array( + 'user_dst', + ), + ), + ); + } + + public function update_schema() + { + return array( + 'add_columns' => array( + $this->table_prefix . 'users' => array( + 'user_dst' => array('BOOL', 0), + ), + ), + ); + } + + protected function setUp() + { + parent::setUp(); + + global $phpbb_root_path, $phpEx; + + $this->db = $this->new_dbal(); + + $this->migration = new \phpbb\db\migration\data\v310\timezone( + new \phpbb\config\config(array()), + $this->db, + new \phpbb\db\tools($this->db), + $phpbb_root_path, + $phpEx, + 'phpbb_' + ); + } + + protected $expected_results = array( + //user_id => user_timezone + 1 => 'Etc/GMT+12', + 2 => 'Etc/GMT+11', + 3 => 'Etc/GMT-3', + 4 => 'Etc/GMT-4', + 5 => 'America/St_Johns', + 6 => 'Australia/Eucla', + ); + + public function test_convert() + { + $this->migration->update_timezones(0); + + $sql = 'SELECT user_id, user_timezone + FROM phpbb_users + ORDER BY user_id ASC'; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $this->assertEquals($this->expected_results[$row['user_id']], $row['user_timezone']); + } + $this->db->sql_freeresult($result); + + $db_tools = new \phpbb\db\tools($this->db); + + // Remove the user_dst field again + $db_tools->sql_column_remove('phpbb_users', 'user_dst'); + } +} diff --git a/tests/migrator/fixtures/convert_timezones.xml b/tests/migrator/fixtures/convert_timezones.xml new file mode 100644 index 0000000000..b02cf8393c --- /dev/null +++ b/tests/migrator/fixtures/convert_timezones.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_timezone</column> + <column>user_dst</column> + <row> + <value>1</value> + <value>1</value> + <value>1</value> + <value></value> + <value></value> + <value>-12</value> + <value>0</value> + </row> + <row> + <value>2</value> + <value>2</value> + <value>2</value> + <value></value> + <value></value> + <value>-12</value> + <value>1</value> + </row> + <row> + <value>3</value> + <value>3</value> + <value>3</value> + <value></value> + <value></value> + <value>3</value> + <value>0</value> + </row> + <row> + <value>4</value> + <value>4</value> + <value>4</value> + <value></value> + <value></value> + <value>3</value> + <value>1</value> + </row> + <row> + <value>5</value> + <value>5</value> + <value>5</value> + <value></value> + <value></value> + <value>-3.5</value> + <value>0</value> + </row> + <row> + <value>6</value> + <value>6</value> + <value>6</value> + <value></value> + <value></value> + <value>8.75</value> + <value>0</value> + </row> + </table> +</dataset> diff --git a/tests/migrator/get_schema_steps_test.php b/tests/migrator/get_schema_steps_test.php new file mode 100644 index 0000000000..3d15d2b200 --- /dev/null +++ b/tests/migrator/get_schema_steps_test.php @@ -0,0 +1,204 @@ +<?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 get_schema_steps_test extends phpbb_test_case +{ + public function setUp() + { + parent::setUp(); + + $this->helper = new \phpbb\db\migration\helper(); + } + + public function schema_provider() + { + return array( + array( + array( + 'add_tables' => array( + 'foo' => array( + 'COLUMNS' => array( + 'foobar' => array('BOOL', 0), + 'foobar2' => array('BOOL', 0), + ), + 'PRIMARY_KEY' => array('foobar'), + ), + 'bar' => array( + 'COLUMNS' => array( + 'barfoo' => array('BOOL', 0), + 'barfoor2' => array('BOOL', 0), + ), + 'PRIMARY_KEY' => array('barfoo'), + ), + ), + 'drop_tables' => array('table1', 'table2', 'table3'), + 'add_index' => array( + 'table1' => array( + 'index1' => 'column1', + 'index2' => 'column2', + ), + 'table2' => array( + 'index1' => 'column1', + 'index2' => 'column2', + ), + ), + 'add_columns' => array( + 'table1' => array( + 'column1' => array('foo'), + 'column2' => array('bar'), + ), + ), + 'change_columns' => array( + 'table1' => array( + 'column1' => array('foo'), + 'column2' => array('bar'), + ), + ), + 'drop_columns' => array( + 'table1' => array( + 'column1', + 'column2', + ), + ), + 'add_unique_index' => array( + 'table1' => array( + 'index1' => 'column1', + 'index2' => 'column2', + ), + ), + 'drop_keys' => array( + 'table1' => array( + 'column1', + 'column2', + ), + ), + 'add_primary_keys' => array( + 'table1' => array('foo'), + 'table2' => array('bar'), + 'table3' => array('foobar'), + ), + ), + array( + array('dbtools.perform_schema_changes', array(array('drop_tables' => array('table1')))), + array('dbtools.perform_schema_changes', array(array('drop_tables' => array('table2')))), + array('dbtools.perform_schema_changes', array(array('drop_tables' => array('table3')))), + array('dbtools.perform_schema_changes', array(array('add_tables' => array( + 'foo' => array( + 'COLUMNS' => array( + 'foobar' => array('BOOL', 0), + 'foobar2' => array('BOOL', 0), + ), + 'PRIMARY_KEY' => array('foobar'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_tables' => array( + 'bar' => array( + 'COLUMNS' => array( + 'barfoo' => array('BOOL', 0), + 'barfoor2' => array('BOOL', 0), + ), + 'PRIMARY_KEY' => array('barfoo'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('change_columns' => array( + 'table1' => array( + 'column1' => array('foo'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('change_columns' => array( + 'table1' => array( + 'column2' => array('bar'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_columns' => array( + 'table1' => array( + 'column1' => array('foo'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_columns' => array( + 'table1' => array( + 'column2' => array('bar'), + ), + )))), + array('dbtools.perform_schema_changes', array(array('drop_keys' => array( + 'table1' => array( + 0 => 'column1', + ), + )))), + array('dbtools.perform_schema_changes', array(array('drop_keys' => array( + 'table1' => array( + 1 => 'column2', + ), + )))), + array('dbtools.perform_schema_changes', array(array('drop_columns' => array( + 'table1' => array( + 0 => 'column1', + ), + )))), + array('dbtools.perform_schema_changes', array(array('drop_columns' => array( + 'table1' => array( + 1 => 'column2', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_primary_keys' => array( + 'table1' => array('foo'), + )))), + array('dbtools.perform_schema_changes', array(array('add_primary_keys' => array( + 'table2' => array('bar'), + )))), + array('dbtools.perform_schema_changes', array(array('add_primary_keys' => array( + 'table3' => array('foobar'), + )))), + array('dbtools.perform_schema_changes', array(array('add_unique_index' => array( + 'table1' => array( + 'index1' => 'column1', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_unique_index' => array( + 'table1' => array( + 'index2' => 'column2', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_index' => array( + 'table1' => array( + 'index1' => 'column1', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_index' => array( + 'table1' => array( + 'index2' => 'column2', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_index' => array( + 'table2' => array( + 'index1' => 'column1', + ), + )))), + array('dbtools.perform_schema_changes', array(array('add_index' => array( + 'table2' => array( + 'index2' => 'column2', + ), + )))), + ), + ), + ); + } + + /** + * @dataProvider schema_provider + */ + public function test_get_schema_steps($schema_changes, $expected) + { + $this->assertEquals($expected, $this->helper->get_schema_steps($schema_changes)); + } +} diff --git a/tests/migrator/schema_generator_test.php b/tests/migrator/schema_generator_test.php new file mode 100644 index 0000000000..9adf518a5d --- /dev/null +++ b/tests/migrator/schema_generator_test.php @@ -0,0 +1,138 @@ +<?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 __DIR__ . '/../dbal/migration/dummy_order.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_0.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_1.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_2.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_3.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_4.php'; +require_once __DIR__ . '/../dbal/migration/dummy_order_5.php'; + +class schema_generator_test extends phpbb_test_case +{ + /** @var \phpbb\db\migration\schema_generator */ + protected $generator; + + public function setUp() + { + parent::setUp(); + + $this->config = new \phpbb\config\config(array()); + $this->db = new \phpbb\db\driver\sqlite(); + $this->db_tools = new \phpbb\db\tools($this->db); + $this->table_prefix = 'phpbb_'; + } + + protected function get_schema_generator(array $class_names) + { + $this->generator = new \phpbb\db\migration\schema_generator($class_names, $this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix); + + return $this->generator; + } + + /** + * @expectedException \UnexpectedValueException + */ + public function test_check_dependencies_fail() + { + $this->get_schema_generator(array('\phpbb\db\migration\data\v310\forgot_password')); + + $this->generator->get_schema(); + } + + public function test_get_schema_success() + { + $this->get_schema_generator(array( + '\phpbb\db\migration\data\v30x\release_3_0_1_rc1', + '\phpbb\db\migration\data\v30x\release_3_0_0', + '\phpbb\db\migration\data\v310\boardindex' + )); + + $this->assertArrayHasKey('phpbb_users', $this->generator->get_schema()); + } + + public function column_add_after_data() + { + return array( + array( + 'phpbb_dbal_migration_dummy_order_0', + array( + 'foobar1', + 'foobar2', + 'foobar3', + ), + ), + array( + 'phpbb_dbal_migration_dummy_order_1', + array( + 'foobar1', + 'foobar3', + 'foobar4', + ), + ), + array( + 'phpbb_dbal_migration_dummy_order_2', + array( + 'foobar1', + 'foobar3', + 'foobar5', + ), + ), + array( + 'phpbb_dbal_migration_dummy_order_3', + array( + 'foobar1', + 'foobar3', + 'foobar6', + ), + ), + array( + 'phpbb_dbal_migration_dummy_order_4', + array( + 'foobar1', + 'foobar3', + 'foobar7', + ), + ), + array( + 'phpbb_dbal_migration_dummy_order_5', + array( + 'foobar1', + 'foobar3', + 'foobar9', + 'foobar8', + ), + ), + ); + } + + /** + * @dataProvider column_add_after_data + */ + public function test_column_add_after($migration, $expected) + { + $this->get_schema_generator(array( + 'phpbb_dbal_migration_dummy_order', + $migration, + )); + + $tables = $this->generator->get_schema(); + + $this->assertEquals( + $expected, + array_keys($tables[$this->table_prefix . 'column_order_test1']['COLUMNS']), + 'The schema generator could not position the column correctly, using the "after" option in the migration script.' + ); + } +} |