diff options
Diffstat (limited to 'tests/dbal')
25 files changed, 1405 insertions, 33 deletions
| diff --git a/tests/dbal/case_test.php b/tests/dbal/case_test.php new file mode 100644 index 0000000000..57a1729a39 --- /dev/null +++ b/tests/dbal/case_test.php @@ -0,0 +1,69 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_case_test extends phpbb_database_test_case +{ +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); +	} + +	public function test_case_int() +	{ +		$db = $this->new_dbal(); + +		$sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '2') . ' AS test_num +			FROM phpbb_config'; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals(1, (int) $db->sql_fetchfield('test_num')); + +		$sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS test_num +			FROM phpbb_config'; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals(2, (int) $db->sql_fetchfield('test_num')); +	} + +	public function test_case_string() +	{ +		$db = $this->new_dbal(); + +		$sql = 'SELECT ' . $db->sql_case('1 = 1', "'foo'", "'bar'") . ' AS test_string +			FROM phpbb_config'; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals('foo', $db->sql_fetchfield('test_string')); + +		$sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS test_string +			FROM phpbb_config'; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals('bar', $db->sql_fetchfield('test_string')); +	} + +	public function test_case_column() +	{ +		$db = $this->new_dbal(); + +		$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string +			FROM phpbb_config +			WHERE config_name = 'config1'"; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals('config1', $db->sql_fetchfield('test_string')); + +		$sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string +			FROM phpbb_config +			WHERE config_value = 'bar'"; +		$result = $db->sql_query_limit($sql, 1); + +		$this->assertEquals('bar', $db->sql_fetchfield('test_string')); +	} +} diff --git a/tests/dbal/concatenate_test.php b/tests/dbal/concatenate_test.php new file mode 100644 index 0000000000..0891fa58a0 --- /dev/null +++ b/tests/dbal/concatenate_test.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_concatenate_test extends phpbb_database_test_case +{ +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); +	} + +	public function test_concatenate_string() +	{ +		$db = $this->new_dbal(); + +		$sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', "'" . $db->sql_escape('append') . "'") . ' AS string +			FROM phpbb_config'; +		$result = $db->sql_query($sql); + +		$db->sql_return_on_error(false); + +		$this->assertEquals(array( +				array( +					'config_name'	=> 'config1', +					'string'		=> 'config1append', +				), +				array( +					'config_name'	=> 'config2', +					'string'		=> 'config2append', +				), +			), +			$db->sql_fetchrowset($result) +		); +	} + +	public function test_concatenate_statement() +	{ +		$db = $this->new_dbal(); + +		$sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', 'config_value') . ' AS string +			FROM phpbb_config'; +		$result = $db->sql_query($sql); + +		$db->sql_return_on_error(false); + +		$this->assertEquals(array( +				array( +					'config_name'	=> 'config1', +					'string'		=> 'config1foo', +				), +				array( +					'config_name'	=> 'config2', +					'string'		=> 'config2bar', +				), +			), +			$db->sql_fetchrowset($result) +		); +	} +} diff --git a/tests/dbal/connect_test.php b/tests/dbal/connect_test.php index 505ce28fa1..1e352d6b03 100644 --- a/tests/dbal/connect_test.php +++ b/tests/dbal/connect_test.php @@ -22,9 +22,7 @@ class phpbb_dbal_connect_test extends phpbb_database_test_case  		$config = $this->get_database_config(); -		require_once dirname(__FILE__) . '/../../phpBB/includes/db/' . $config['dbms'] . '.php'; -		$dbal = 'dbal_' . $config['dbms']; -		$db = new $dbal(); +		$db = new $config['dbms']();  		// Failure to connect results in a trigger_error call in dbal.  		// phpunit converts triggered errors to exceptions. diff --git a/tests/dbal/fixtures/migrator.xml b/tests/dbal/fixtures/migrator.xml new file mode 100644 index 0000000000..25be4d4129 --- /dev/null +++ b/tests/dbal/fixtures/migrator.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> +	<table name="phpbb_migrations"> +		<column>migration_name</column> +		<column>migration_depends_on</column> +		<column>migration_schema_done</column> +		<column>migration_data_done</column> +		<column>migration_data_state</column> +		<column>migration_start_time</column> +		<column>migration_end_time</column> +		<row> +			<value>installed_migration</value> +			<value></value> +			<value>1</value> +			<value>1</value> +			<value></value> +			<value>1234</value> +			<value>5678</value> +		</row> +	</table> +	<table name="phpbb_config"> +		<column>config_name</column> +		<column>config_value</column> +		<row> +			<value>foo</value> +			<value>bar</value> +		</row> +	</table> +</dataset> diff --git a/tests/dbal/fixtures/migrator_module.xml b/tests/dbal/fixtures/migrator_module.xml new file mode 100644 index 0000000000..32afe7e6f3 --- /dev/null +++ b/tests/dbal/fixtures/migrator_module.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> +	<table name="phpbb_modules"> +		<column>module_id</column> +		<column>module_enabled</column> +		<column>module_display</column> +		<column>module_basename</column> +		<column>module_class</column> +		<column>parent_id</column> +		<column>left_id</column> +		<column>right_id</column> +		<column>module_langname</column> +		<column>module_mode</column> +		<column>module_auth</column> +		<row> +			<value>1</value> +			<value>1</value> +			<value>1</value> +			<value></value> +			<value>acp</value> +			<value>0</value> +			<value>1</value> +			<value>4</value> +			<value>ACP_CAT</value> +			<value></value> +			<value></value> +		</row> +		<row> +			<value>2</value> +			<value>1</value> +			<value>1</value> +			<value>acp_test</value> +			<value>acp</value> +			<value>1</value> +			<value>2</value> +			<value>3</value> +			<value>ACP_MODULE</value> +			<value>test</value> +			<value></value> +		</row> +	</table> +</dataset> diff --git a/tests/dbal/fixtures/migrator_permission.xml b/tests/dbal/fixtures/migrator_permission.xml new file mode 100644 index 0000000000..08cec42a42 --- /dev/null +++ b/tests/dbal/fixtures/migrator_permission.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> +	<table name="phpbb_acl_options"> +		<column>auth_option_id</column> +		<column>auth_option</column> +		<column>is_global</column> +		<column>is_local</column> +		<column>founder_only</column> +		<row> +			<value>1</value> +			<value>global</value> +			<value>1</value> +			<value>0</value> +			<value>0</value> +		</row> +		<row> +			<value>2</value> +			<value>local</value> +			<value>0</value> +			<value>1</value> +			<value>0</value> +		</row> +		<row> +			<value>3</value> +			<value>both</value> +			<value>1</value> +			<value>1</value> +			<value>0</value> +		</row> +	</table> +</dataset> diff --git a/tests/dbal/fixtures/styles.xml b/tests/dbal/fixtures/styles.xml index 47b384c47f..dcbe39d3b0 100644 --- a/tests/dbal/fixtures/styles.xml +++ b/tests/dbal/fixtures/styles.xml @@ -5,35 +5,39 @@  		<column>style_name</column>  		<column>style_copyright</column>  		<column>style_active</column> -		<column>template_id</column> -		<column>theme_id</column> -		<column>imageset_id</column> +		<column>style_path</column> +		<column>bbcode_bitfield</column> +		<column>style_parent_id</column> +		<column>style_parent_tree</column>  		<row>  			<value>1</value>  			<value>prosilver</value>  			<value>&copy; phpBB Group</value>  			<value>1</value> -			<value>1</value> -			<value>1</value> -			<value>1</value> +			<value>prosilver</value> +			<value>kNg=</value> +			<value>0</value> +			<value></value>  		</row>  		<row>  			<value>2</value>  			<value>prosilver2</value>  			<value>&copy; phpBB Group</value>  			<value>0</value> -			<value>2</value> -			<value>2</value> -			<value>2</value> +			<value>prosilver2</value> +			<value>kNg=</value> +			<value>0</value> +			<value></value>  		</row>  		<row>  			<value>3</value>  			<value>Prosilver1</value>  			<value>&copy; phpBB Group</value>  			<value>0</value> -			<value>3</value> -			<value>3</value> -			<value>3</value> +			<value>prosilver1</value> +			<value>kNg=</value> +			<value>1</value> +			<value>prosilver</value>  		</row>  	</table>  </dataset> diff --git a/tests/dbal/migration/dummy.php b/tests/dbal/migration/dummy.php new file mode 100644 index 0000000000..0ac6e733a1 --- /dev/null +++ b/tests/dbal/migration/dummy.php @@ -0,0 +1,27 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_dummy extends phpbb_db_migration +{ +	static public function depends_on() +	{ +		return array('installed_migration'); +	} + +	function update_schema() +	{ +		return array( +			'add_columns' => array( +				'phpbb_config' => array( +					'extra_column' => array('UINT', 1), +				), +			), +		); +	} +} diff --git a/tests/dbal/migration/fail.php b/tests/dbal/migration/fail.php new file mode 100644 index 0000000000..f88d8169f5 --- /dev/null +++ b/tests/dbal/migration/fail.php @@ -0,0 +1,41 @@ +<?php +/** +* +* @package migration +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2 +* +*/ + +class phpbb_dbal_migration_fail extends phpbb_db_migration +{ +	function update_schema() +	{ +		return array( +			'add_columns' => array( +				$this->table_prefix . 'config' => array( +					'test_column' => array('BOOL', 1), +				), +			), +		); +	} + +	function revert_schema() +	{ +		return array( +			'drop_columns' => array( +				$this->table_prefix . 'config' => array( +					'test_column', +				), +			), +		); +	} + +	function update_data() +	{ +		return array( +			array('config.add', array('foobar3', true)), +			array('config.update', array('does_not_exist', true)), +		); +	} +} diff --git a/tests/dbal/migration/if.php b/tests/dbal/migration/if.php new file mode 100644 index 0000000000..83fe21bd21 --- /dev/null +++ b/tests/dbal/migration/if.php @@ -0,0 +1,44 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_if extends phpbb_db_migration +{ +	function update_schema() +	{ +		return array(); +	} + +	function update_data() +	{ +		return array( +			array('if', array( +				true, +				array('custom', array(array(&$this, 'test_true'))), +			)), +			array('if', array( +				false, +				array('custom', array(array(&$this, 'test_false'))), +			)), +		); +	} + +	function test_true() +	{ +		global $migrator_test_if_true_failed; + +		$migrator_test_if_true_failed = false; +	} + +	function test_false() +	{ +		global $migrator_test_if_false_failed; + +		$migrator_test_if_false_failed = true; +	} +} diff --git a/tests/dbal/migration/installed.php b/tests/dbal/migration/installed.php new file mode 100644 index 0000000000..01829f7a99 --- /dev/null +++ b/tests/dbal/migration/installed.php @@ -0,0 +1,30 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_installed extends phpbb_db_migration +{ +	function effectively_installed() +	{ +		return true; +	} + +	function update_data() +	{ +		return array( +			array('custom', array(array(&$this, 'test'))), +		); +	} + +	function test() +	{ +		global $migrator_test_installed_failed; + +		$migrator_test_installed_failed = true; +	} +} diff --git a/tests/dbal/migration/recall.php b/tests/dbal/migration/recall.php new file mode 100644 index 0000000000..6c2f04bf08 --- /dev/null +++ b/tests/dbal/migration/recall.php @@ -0,0 +1,38 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_recall extends phpbb_db_migration +{ +	function update_schema() +	{ +		return array(); +	} + +	function update_data() +	{ +		return array( +			array('custom', array(array(&$this, 'test_call'))), +		); +	} + +	// This function should be called 10 times +	function test_call($input) +	{ +		global $migrator_test_call_input; + +		$migrator_test_call_input = (int) $input; + +		if ($migrator_test_call_input < 10) +		{ +			return ($migrator_test_call_input + 1); +		} + +		return; +	} +} diff --git a/tests/dbal/migration/revert.php b/tests/dbal/migration/revert.php new file mode 100644 index 0000000000..ac01987cd4 --- /dev/null +++ b/tests/dbal/migration/revert.php @@ -0,0 +1,40 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_revert extends phpbb_db_migration +{ +	function update_schema() +	{ +		return array( +			'add_columns' => array( +				'phpbb_config' => array( +					'bar_column' => array('UINT', 1), +				), +			), +		); +	} + +	function revert_schema() +	{ +		return array( +			'drop_columns' => array( +				'phpbb_config' => array( +					'bar_column', +				), +			), +		); +	} + +	function update_data() +	{ +		return array( +			array('config.add', array('foobartest', 0)), +		); +	} +} diff --git a/tests/dbal/migration/revert_with_dependency.php b/tests/dbal/migration/revert_with_dependency.php new file mode 100644 index 0000000000..ca2c070e8c --- /dev/null +++ b/tests/dbal/migration/revert_with_dependency.php @@ -0,0 +1,16 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_revert_with_dependency extends phpbb_db_migration +{ +	static public function depends_on() +	{ +		return array('phpbb_dbal_migration_revert'); +	} +} diff --git a/tests/dbal/migration/unfulfillable.php b/tests/dbal/migration/unfulfillable.php new file mode 100644 index 0000000000..6d375e6880 --- /dev/null +++ b/tests/dbal/migration/unfulfillable.php @@ -0,0 +1,26 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_dbal_migration_unfulfillable extends phpbb_db_migration +{ +	static public function depends_on() +	{ +		return array('installed_migration', 'phpbb_dbal_migration_dummy', 'non_existant_migration'); +	} + +	function update_schema() +	{ +		trigger_error('Schema update of migration with unfulfillable dependency was run!'); +	} + +	function update_data() +	{ +		trigger_error('Data update of migration with unfulfillable dependency was run!'); +	} +} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php new file mode 100644 index 0000000000..1e40c9c6d6 --- /dev/null +++ b/tests/dbal/migrator_test.php @@ -0,0 +1,274 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migrator.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/migration.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.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'; + +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(); +		$this->db_tools = new phpbb_db_tools($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), +		); + +		$this->migrator = new phpbb_db_migrator( +			$this->config, +			$this->db, +			$this->db_tools, +			'phpbb_migrations', +			dirname(__FILE__) . '/../../phpBB/', +			'php', +			'phpbb_', +			$tools +		); + +		$container = new phpbb_mock_container_builder(); +		$container->set('migrator', $migrator); + +		$this->extension_manager = new phpbb_extension_manager( +			$container, +			$this->db, +			$this->config, +			new phpbb_filesystem(), +			'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() +	{ +		// 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(); + +		$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.'); +		} +	} + +	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'); +		} +	} +} diff --git a/tests/dbal/migrator_tool_config_test.php b/tests/dbal/migrator_tool_config_test.php new file mode 100644 index 0000000000..7d582f230b --- /dev/null +++ b/tests/dbal/migrator_tool_config_test.php @@ -0,0 +1,124 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/tool/config.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/exception.php'; + +class phpbb_dbal_migrator_tool_config_test extends phpbb_test_case +{ +	public function setup() +	{ +		$this->config = new phpbb_config(array()); + +		$this->tool = new phpbb_db_migration_tool_config($this->config); + +		parent::setup(); +	} + +	public function test_add() +	{ +		try +		{ +			$this->tool->add('foo', 'bar'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertEquals('bar', $this->config['foo']); + +		try +		{ +			$this->tool->add('foo', 'bar'); +			$this->fail('Exception not thrown'); +		} +		catch (Exception $e) {} +	} + +	public function test_update() +	{ +		$this->config->set('foo', 'bar'); +		try +		{ +			$this->tool->update('foo', 'bar2'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertEquals('bar2', $this->config['foo']); +	} + +	public function test_update_if_equals() +	{ +		$this->config->set('foo', 'bar'); + +		try +		{ +			$this->tool->update_if_equals('', 'foo', 'bar2'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertEquals('bar', $this->config['foo']); + +		try +		{ +			$this->tool->update_if_equals('bar', 'foo', 'bar2'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertEquals('bar2', $this->config['foo']); +	} + +	public function test_remove() +	{ +		$this->config->set('foo', 'bar'); + +		try +		{ +			$this->tool->remove('foo'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertFalse(isset($this->config['foo'])); +	} + +	public function test_reverse() +	{ +		$this->config->set('foo', 'bar'); + +		try +		{ +			$this->tool->reverse('add', 'foo'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertFalse(isset($this->config['foo'])); + +		$this->config->set('foo', 'bar'); + +		try +		{ +			$this->tool->reverse('update_if_equals', 'test', 'foo', 'bar'); +		} +		catch (Exception $e) +		{ +			$this->fail($e); +		} +		$this->assertEquals('test', $this->config['foo']); +	} +} diff --git a/tests/dbal/migrator_tool_module_test.php b/tests/dbal/migrator_tool_module_test.php new file mode 100644 index 0000000000..3303086b26 --- /dev/null +++ b/tests/dbal/migrator_tool_module_test.php @@ -0,0 +1,155 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/tool/module.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/exception.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(array()), $this->db, $phpbb_root_path, $phpEx); +		$user = $this->user = new phpbb_user(); + +		$cache = new phpbb_mock_cache; +		$phpbb_dispatcher = new phpbb_mock_event_dispatcher(); +		$auth = $this->getMock('phpbb_auth'); +		$phpbb_log = new phpbb_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() +	{ +		return array( +			// Test the category +			array( +				'', +				'ACP_CAT', +				true, +			), +			array( +				0, +				'ACP_CAT', +				true, +			), + +			// Test the module +			array( +				'', +				'ACP_MODULE', +				false, +			), +			array( +				false, +				'ACP_MODULE', +				true, +			), +			array( +				'ACP_CAT', +				'ACP_MODULE', +				true, +			), +		); +	} + +	/** +	* @dataProvider exists_data +	*/ +	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')); +	} + +	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')); +	} +} diff --git a/tests/dbal/migrator_tool_permission_test.php b/tests/dbal/migrator_tool_permission_test.php new file mode 100644 index 0000000000..438ab2b28e --- /dev/null +++ b/tests/dbal/migrator_tool_permission_test.php @@ -0,0 +1,159 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/tool/permission.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/exception.php'; + +class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case +{ +	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_null(), new phpbb_config(array()), $this->db, $phpbb_root_path, $phpEx); +		$this->auth = new phpbb_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)); +	} +} diff --git a/tests/dbal/order_lower_test.php b/tests/dbal/order_lower_test.php index b50494d506..84d454742f 100644 --- a/tests/dbal/order_lower_test.php +++ b/tests/dbal/order_lower_test.php @@ -14,7 +14,7 @@ class phpbb_dbal_order_lower_test extends phpbb_database_test_case  		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/styles.xml');  	} -	public function test_cross_join() +	public function test_order_lower()  	{  		$db = $this->new_dbal(); @@ -33,27 +33,30 @@ class phpbb_dbal_order_lower_test extends phpbb_database_test_case  					'style_name'		=> 'prosilver',  					'style_copyright'	=> '© phpBB Group',  					'style_active'		=> 1, -					'template_id'		=> 1, -					'theme_id'			=> 1, -					'imageset_id'		=> 1 +					'style_path'		=> 'prosilver', +					'bbcode_bitfield'	=> 'kNg=', +					'style_parent_id'	=> 0, +					'style_parent_tree'	=> '',  				),  				array(  					'style_id' 			=> 3,  					'style_name'		=> 'Prosilver1',  					'style_copyright'	=> '© phpBB Group',  					'style_active'		=> 0, -					'template_id'		=> 3, -					'theme_id'			=> 3, -					'imageset_id'		=> 3 +					'style_path'		=> 'prosilver1', +					'bbcode_bitfield'	=> 'kNg=', +					'style_parent_id'	=> 1, +					'style_parent_tree'	=> 'prosilver',  				),  				array(  					'style_id' 			=> 2,  					'style_name'		=> 'prosilver2',  					'style_copyright'	=> '© phpBB Group',  					'style_active'		=> 0, -					'template_id'		=> 2, -					'theme_id'			=> 2, -					'imageset_id'		=> 2 +					'style_path'		=> 'prosilver2', +					'bbcode_bitfield'	=> 'kNg=', +					'style_parent_id'	=> 0, +					'style_parent_tree'	=> '',  				)  			),   			$db->sql_fetchrowset($result) diff --git a/tests/dbal/schema_test.php b/tests/dbal/schema_test.php new file mode 100644 index 0000000000..2a332fddba --- /dev/null +++ b/tests/dbal/schema_test.php @@ -0,0 +1,38 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_dbal_schema_test extends phpbb_database_test_case +{ +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); +	} + +	public function test_config_value_multibyte() +	{ +		$db = $this->new_dbal(); + +		$value = str_repeat("\xC3\x84", 255); +		$sql = "INSERT INTO phpbb_config +			(config_name, config_value) +			VALUES ('name', '$value')"; +		$result = $db->sql_query($sql); + +		$sql = "SELECT config_value +			FROM phpbb_config +			WHERE config_name = 'name'"; +		$result = $db->sql_query_limit($sql, 1); +		$row = $db->sql_fetchrow($result); +		$db->sql_freeresult($result); + +		$this->assertEquals($value, $row['config_value']); +	} +} diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php index 6dbab05a41..c8cfad04e0 100644 --- a/tests/dbal/select_test.php +++ b/tests/dbal/select_test.php @@ -17,7 +17,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml');  	} -	static public function return_on_error_select_data() +	public function return_on_error_select_data()  	{  		return array(  			array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), @@ -44,7 +44,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$this->assertEquals($expected, $db->sql_fetchrowset($result));  	} -	static public function fetchrow_data() +	public function fetchrow_data()  	{  		return array(  			array('', array(array('username_clean' => 'barfoo'), @@ -95,7 +95,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$db->sql_freeresult($result);  	} -	static public function fetchfield_data() +	public function fetchfield_data()  	{  		return array(  			array('', array('barfoo', 'foobar', 'bertie')), @@ -192,7 +192,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$this->assertEquals($expected, $ary);  	} -	static public function like_expression_data() +	public function like_expression_data()  	{  		// * = any_char; # = one_char  		return array( @@ -229,7 +229,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$db->sql_freeresult($result);  	} -	static public function in_set_data() +	public function in_set_data()  	{  		return array(  			array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), @@ -303,7 +303,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$db->sql_freeresult($result);  	} -	static public function build_array_data() +	public function build_array_data()  	{  		return array(  			array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), diff --git a/tests/dbal/sql_insert_buffer_test.php b/tests/dbal/sql_insert_buffer_test.php new file mode 100644 index 0000000000..45339a6b50 --- /dev/null +++ b/tests/dbal/sql_insert_buffer_test.php @@ -0,0 +1,116 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_sql_insert_buffer_test extends phpbb_database_test_case +{ +	protected $db; +	protected $buffer; + +	public function setUp() +	{ +		parent::setUp(); + +		$this->db = $this->new_dbal(); +		$this->buffer = new phpbb_db_sql_insert_buffer($this->db, 'phpbb_config', 2); +		$this->assert_config_count(2); +	} + +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); +	} + +	public function test_multi_insert_disabled_insert_and_flush() +	{ +		$this->db->multi_insert = false; +		$this->assertTrue($this->buffer->insert($this->get_row(1))); +		$this->assert_config_count(3); +		$this->assertFalse($this->buffer->flush()); +		$this->assert_config_count(3); +	} + +	public function test_multi_insert_enabled_insert_and_flush() +	{ +		$this->check_multi_insert_support(); +		$this->assertFalse($this->buffer->insert($this->get_row(1))); +		$this->assert_config_count(2); +		$this->assertTrue($this->buffer->flush()); +		$this->assert_config_count(3); +	} + +	public function test_multi_insert_disabled_insert_with_flush() +	{ +		$this->db->multi_insert = false; +		$this->assertTrue($this->buffer->insert($this->get_row(1))); +		$this->assert_config_count(3); +		$this->assertTrue($this->buffer->insert($this->get_row(2))); +		$this->assert_config_count(4); +	} + +	public function test_multi_insert_enabled_insert_with_flush() +	{ +		$this->check_multi_insert_support(); +		$this->assertFalse($this->buffer->insert($this->get_row(1))); +		$this->assert_config_count(2); +		$this->assertTrue($this->buffer->insert($this->get_row(2))); +		$this->assert_config_count(4); +	} + +	public function test_multi_insert_disabled_insert_all_and_flush() +	{ +		$this->db->multi_insert = false; +		$this->assertTrue($this->buffer->insert_all($this->get_rows(3))); +		$this->assert_config_count(5); +	} + +	public function test_multi_insert_enabled_insert_all_and_flush() +	{ +		$this->check_multi_insert_support(); +		$this->assertTrue($this->buffer->insert_all($this->get_rows(3))); +		$this->assert_config_count(4); +		$this->assertTrue($this->buffer->flush()); +		$this->assert_config_count(5); +	} + +	protected function assert_config_count($num_configs) +	{ +		$sql = 'SELECT COUNT(*) AS num_configs +			FROM phpbb_config'; +		$result = $this->db->sql_query($sql); +		$this->assertEquals($num_configs, $this->db->sql_fetchfield('num_configs')); +		$this->db->sql_freeresult($result); +	} + +	protected function check_multi_insert_support() +	{ +		if (!$this->db->multi_insert) +		{ +			$this->markTestSkipped('Database does not support multi_insert'); +		} +	} + +	protected function get_row($rownum) +	{ +		return array( +			'config_name'	=> "name$rownum", +			'config_value'	=> "value$rownum", +			'is_dynamic'	=> '0', +		); +	} + +	protected function get_rows($n) +	{ +		$result = array(); +		for ($i = 0; $i < $n; ++$i) +		{ +			$result[] = $this->get_row($i); +		} +		return $result; +	} +} diff --git a/tests/dbal/write_sequence_test.php b/tests/dbal/write_sequence_test.php index 8975cfbfb1..f382a971a5 100644 --- a/tests/dbal/write_sequence_test.php +++ b/tests/dbal/write_sequence_test.php @@ -33,6 +33,10 @@ class phpbb_dbal_write_sequence_test extends phpbb_database_test_case  	{  		$db = $this->new_dbal(); +		// dbal uses cache +		global $cache; +		$cache = new phpbb_mock_cache(); +  		$sql = 'INSERT INTO phpbb_users ' . $db->sql_build_array('INSERT', array(  			'username'			=> $username,  			'username_clean'	=> $username, diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php index ecfd774896..987161a831 100644 --- a/tests/dbal/write_test.php +++ b/tests/dbal/write_test.php @@ -16,7 +16,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case  		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');  	} -	static public function build_array_insert_data() +	public function build_array_insert_data()  	{  		return array(  			array(array( @@ -104,7 +104,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case  		$db->sql_freeresult($result);  	} -	static public function update_data() +	public function update_data()  	{  		return array(  			array( | 
