From 606d82f7dd03da2646333c9ca868071b3e47a970 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 5 Jul 2014 00:57:34 -0700 Subject: [PATCH 1/4] [ticket/12812] Add a migrator tool for config_text database changes PHPBB3-12812 --- phpBB/config/migrator.yml | 7 + phpBB/phpbb/db/migration/tool/config_text.php | 125 ++++++++++++++++++ tests/dbal/fixtures/migrator_config_text.xml | 7 + tests/dbal/migrator_tool_config_text_test.php | 79 +++++++++++ 4 files changed, 218 insertions(+) create mode 100644 phpBB/phpbb/db/migration/tool/config_text.php create mode 100644 tests/dbal/fixtures/migrator_config_text.xml create mode 100644 tests/dbal/migrator_tool_config_text_test.php diff --git a/phpBB/config/migrator.yml b/phpBB/config/migrator.yml index 202421c09f..cd04eea5c2 100644 --- a/phpBB/config/migrator.yml +++ b/phpBB/config/migrator.yml @@ -29,6 +29,13 @@ services: tags: - { name: migrator.tool } + migrator.tool.config_text: + class: phpbb\db\migration\tool\config_text + arguments: + - @config_text + tags: + - { name: migrator.tool } + migrator.tool.module: class: phpbb\db\migration\tool\module arguments: diff --git a/phpBB/phpbb/db/migration/tool/config_text.php b/phpBB/phpbb/db/migration/tool/config_text.php new file mode 100644 index 0000000000..bf8ac55023 --- /dev/null +++ b/phpBB/phpbb/db/migration/tool/config_text.php @@ -0,0 +1,125 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\db\migration\tool; + +/** +* Migration config_text tool +*/ +class config_text implements \phpbb\db\migration\tool\tool_interface +{ + /** @var \phpbb\config\db_text */ + protected $config_text; + + /** + * Constructor + * + * @param \phpbb\config\db_text $config_text + */ + public function __construct(\phpbb\config\db_text $config_text) + { + $this->config_text = $config_text; + } + + /** + * {@inheritdoc} + */ + public function get_name() + { + return 'config_text'; + } + + /** + * Add a config_text setting. + * + * @param string $config_name The name of the config_text setting + * you would like to add + * @param mixed $config_value The value of the config_text setting + * @return null + */ + public function add($config_name, $config_value) + { + if (!is_null($this->config_text->get($config_name))) + { + return; + } + + $this->config_text->set($config_name, $config_value); + } + + /** + * Update an existing config_text setting. + * + * @param string $config_name The name of the config_text setting you would + * like to update + * @param mixed $config_value The value of the config_text setting + * @return null + * @throws \phpbb\db\migration\exception + */ + public function update($config_name, $config_value) + { + if (is_null($this->config_text->get($config_name))) + { + throw new \phpbb\db\migration\exception('CONFIG_NOT_EXIST', $config_name); + } + + $this->config_text->set($config_name, $config_value); + } + + /** + * Remove an existing config_text setting. + * + * @param string $config_name The name of the config_text setting you would + * like to remove + * @return null + */ + public function remove($config_name) + { + if (is_null($this->config_text->get($config_name))) + { + return; + } + + $this->config_text->delete($config_name); + } + + /** + * {@inheritdoc} + */ + public function reverse() + { + $arguments = func_get_args(); + $original_call = array_shift($arguments); + + $call = false; + switch ($original_call) + { + case 'add': + $call = 'remove'; + break; + + case 'remove': + $call = 'add'; + if (sizeof($arguments) == 1) + { + $arguments[] = ''; + } + break; + } + + if ($call) + { + return call_user_func_array(array(&$this, $call), $arguments); + } + } +} diff --git a/tests/dbal/fixtures/migrator_config_text.xml b/tests/dbal/fixtures/migrator_config_text.xml new file mode 100644 index 0000000000..ba8e1fcfcc --- /dev/null +++ b/tests/dbal/fixtures/migrator_config_text.xml @@ -0,0 +1,7 @@ + + + + config_name + config_value +
+
diff --git a/tests/dbal/migrator_tool_config_text_test.php b/tests/dbal/migrator_tool_config_text_test.php new file mode 100644 index 0000000000..b530bb92dd --- /dev/null +++ b/tests/dbal/migrator_tool_config_text_test.php @@ -0,0 +1,79 @@ + +* @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_config_text_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/migrator_config_text.xml'); + } + + public function setup() + { + global $db; + + parent::setup(); + + $db = $this->db = $this->new_dbal(); + $this->config_text = new \phpbb\config\db_text($this->db, 'phpbb_config_text'); + + $this->tool = new \phpbb\db\migration\tool\config_text($this->config_text); + } + + public function test_add() + { + $this->tool->add('foo', 'bar'); + $this->assertEquals('bar', $this->config_text->get('foo')); + } + + public function test_add_twice() + { + $this->tool->add('foo', 'bar'); + $this->assertEquals('bar', $this->config_text->get('foo')); + + $this->tool->add('foo', 'bar2'); + $this->assertEquals('bar', $this->config_text->get('foo')); + } + + public function test_update() + { + $this->config_text->set('foo', 'bar'); + + $this->tool->update('foo', 'bar2'); + $this->assertEquals('bar2', $this->config_text->get('foo')); + } + + public function test_remove() + { + $this->config_text->set('foo', 'bar'); + + $this->tool->remove('foo'); + $this->assertTrue(is_null($this->config_text->get('foo'))); + } + + public function test_reverse_add() + { + $this->config_text->set('foo', 'bar'); + + $this->tool->reverse('add', 'foo'); + $this->assertTrue(is_null($this->config_text->get('foo'))); + } + + public function test_reverse_remove() + { + $this->config_text->delete('foo'); + + $this->tool->reverse('remove', 'foo'); + $this->assertEquals('', $this->config_text->get('foo')); + } +} From 9dfce860e7b13985897aba0b785d4d6257cf5d4c Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 5 Jul 2014 08:26:01 -0700 Subject: [PATCH 2/4] [ticket/12812] Improve migrator config_text tool tests PHPBB3-12812 --- tests/dbal/migrator_tool_config_text_test.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/dbal/migrator_tool_config_text_test.php b/tests/dbal/migrator_tool_config_text_test.php index b530bb92dd..7f9c076fe0 100644 --- a/tests/dbal/migrator_tool_config_text_test.php +++ b/tests/dbal/migrator_tool_config_text_test.php @@ -71,9 +71,7 @@ class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case public function test_reverse_remove() { - $this->config_text->delete('foo'); - $this->tool->reverse('remove', 'foo'); - $this->assertEquals('', $this->config_text->get('foo')); + $this->assertSame('', $this->config_text->get('foo')); } } From 5527044a00c7bdff1a3a6edfabdbe7583935bbaa Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 6 Jul 2014 08:13:20 -0700 Subject: [PATCH 3/4] [ticket/12812] Improve migrator config_text tool tests PHPBB3-12812 --- tests/dbal/migrator_tool_config_text_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/dbal/migrator_tool_config_text_test.php b/tests/dbal/migrator_tool_config_text_test.php index 7f9c076fe0..f832105e00 100644 --- a/tests/dbal/migrator_tool_config_text_test.php +++ b/tests/dbal/migrator_tool_config_text_test.php @@ -58,7 +58,7 @@ class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case $this->config_text->set('foo', 'bar'); $this->tool->remove('foo'); - $this->assertTrue(is_null($this->config_text->get('foo'))); + $this->assertNull($this->config_text->get('foo')); } public function test_reverse_add() @@ -66,7 +66,7 @@ class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case $this->config_text->set('foo', 'bar'); $this->tool->reverse('add', 'foo'); - $this->assertTrue(is_null($this->config_text->get('foo'))); + $this->assertNull($this->config_text->get('foo')); } public function test_reverse_remove() From 5ef7510fb3c7dac8ef6e74c37c97e0efb59a4799 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 7 Jul 2014 12:50:18 -0700 Subject: [PATCH 4/4] [ticket/12812] Remove unused global $db from tests PHPBB3-12812 --- tests/dbal/migrator_tool_config_text_test.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/dbal/migrator_tool_config_text_test.php b/tests/dbal/migrator_tool_config_text_test.php index f832105e00..b271c2d62e 100644 --- a/tests/dbal/migrator_tool_config_text_test.php +++ b/tests/dbal/migrator_tool_config_text_test.php @@ -20,11 +20,9 @@ class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case public function setup() { - global $db; - parent::setup(); - $db = $this->db = $this->new_dbal(); + $this->db = $this->new_dbal(); $this->config_text = new \phpbb\config\db_text($this->db, 'phpbb_config_text'); $this->tool = new \phpbb\db\migration\tool\config_text($this->config_text);