From de70b17b1dc19e19faa0d56395a1aba8868c9624 Mon Sep 17 00:00:00 2001
From: Patrick Webster <noxwizard@phpbb.com>
Date: Fri, 20 Apr 2012 23:50:49 -0500
Subject: [PATCH] [ticket/10492] Separate config generation from the installer

PHPBB3-10492
---
 phpBB/includes/functions_install.php          | 52 +++++++++++++++++++
 phpBB/install/install_install.php             | 28 +---------
 .../phpbb_database_test_case.php              | 40 ++------------
 .../phpbb_test_case_helpers.php               | 45 ++++++++++++++++
 4 files changed, 102 insertions(+), 63 deletions(-)

diff --git a/phpBB/includes/functions_install.php b/phpBB/includes/functions_install.php
index 6caa5c943f..633b2755f0 100644
--- a/phpBB/includes/functions_install.php
+++ b/phpBB/includes/functions_install.php
@@ -512,4 +512,56 @@ function adjust_language_keys_callback($matches)
 	}
 }
 
+/**
+* Creates the output to be stored in a phpBB config.php file
+*
+* @param	array	$data Array containing the database connection information
+* @param	string	$dbms The name of the DBAL class to use
+* @param	array	$load_extensions Array of additional extensions that should be loaded
+* @param	bool	$debug If the debug constants should be enabled by default or not
+*
+* @return	string	The output to write to the file
+*/
+function phpbb_create_config_file_data($data, $dbms, $load_extensions, $debug = false)
+{
+	$load_extensions = implode(',', $load_extensions);
+
+	$config_data = "<?php\n";
+	$config_data .= "// phpBB 3.0.x auto-generated configuration file\n// Do not change anything in this file!\n";
+
+	$config_data_array = array(
+		'dbms'			=> $dbms,
+		'dbhost'		=> $data['dbhost'],
+		'dbport'		=> $data['dbport'],
+		'dbname'		=> $data['dbname'],
+		'dbuser'		=> $data['dbuser'],
+		'dbpasswd'		=> htmlspecialchars_decode($data['dbpasswd']),
+		'table_prefix'	=> $data['table_prefix'],
+		'acm_type'		=> 'file',
+		'load_extensions'	=> $load_extensions,
+	);
+
+	foreach ($config_data_array as $key => $value)
+	{
+		$config_data .= "\${$key} = '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $value)) . "';\n";
+	}
+
+	$config_data .= "\n@define('PHPBB_INSTALLED', true);\n";
+
+	if ($debug)
+	{
+		$config_data .= "@define('DEBUG', true);\n";
+		$config_data .= "@define('DEBUG_EXTRA', true);\n";
+	}
+	else
+	{
+		$config_data .= "// @define('DEBUG', true);\n";
+		$config_data .= "// @define('DEBUG_EXTRA', true);\n";
+	}
+
+	$config_data .= '?' . '>'; // Done this to prevent highlighting editors getting confused!
+
+	return $config_data;
+}
+
 ?>
\ No newline at end of file
diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php
index 454c8b4df0..81dac9ecde 100644
--- a/phpBB/install/install_install.php
+++ b/phpBB/install/install_install.php
@@ -884,34 +884,8 @@ class install_install extends module
 
 		@chmod($phpbb_root_path . 'cache/install_lock', 0777);
 
-		$load_extensions = implode(',', $load_extensions);
-
 		// Time to convert the data provided into a config file
-		$config_data = "<?php\n";
-		$config_data .= "// phpBB 3.0.x auto-generated configuration file\n// Do not change anything in this file!\n";
-
-		$config_data_array = array(
-			'dbms'			=> $available_dbms[$data['dbms']]['DRIVER'],
-			'dbhost'		=> $data['dbhost'],
-			'dbport'		=> $data['dbport'],
-			'dbname'		=> $data['dbname'],
-			'dbuser'		=> $data['dbuser'],
-			'dbpasswd'		=> htmlspecialchars_decode($data['dbpasswd']),
-			'table_prefix'	=> $data['table_prefix'],
-			'acm_type'		=> 'file',
-			'load_extensions'	=> $load_extensions,
-		);
-
-		foreach ($config_data_array as $key => $value)
-		{
-			$config_data .= "\${$key} = '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $value)) . "';\n";
-		}
-		unset($config_data_array);
-
-		$config_data .= "\n@define('PHPBB_INSTALLED', true);\n";
-		$config_data .= "// @define('DEBUG', true);\n";
-		$config_data .= "// @define('DEBUG_EXTRA', true);\n";
-		$config_data .= '?' . '>'; // Done this to prevent highlighting editors getting confused!
+		$config_data = phpbb_create_config_file_data($data, $available_dbms[$data['dbms']]['DRIVER'], $load_extensions);
 
 		// Attempt to write out the config file directly. If it works, this is the easiest way to do it ...
 		if ((file_exists($phpbb_root_path . 'config.' . $phpEx) && phpbb_is_writable($phpbb_root_path . 'config.' . $phpEx)) || phpbb_is_writable($phpbb_root_path))
diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php
index fdb662b284..e742b543b0 100644
--- a/tests/test_framework/phpbb_database_test_case.php
+++ b/tests/test_framework/phpbb_database_test_case.php
@@ -40,46 +40,14 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
 
 	public function get_database_config()
 	{
-		if (isset($_SERVER['PHPBB_TEST_DBMS']))
-		{
-			return array(
-				'dbms'		=> isset($_SERVER['PHPBB_TEST_DBMS']) ? $_SERVER['PHPBB_TEST_DBMS'] : '',
-				'dbhost'	=> isset($_SERVER['PHPBB_TEST_DBHOST']) ? $_SERVER['PHPBB_TEST_DBHOST'] : '',
-				'dbport'	=> isset($_SERVER['PHPBB_TEST_DBPORT']) ? $_SERVER['PHPBB_TEST_DBPORT'] : '',
-				'dbname'	=> isset($_SERVER['PHPBB_TEST_DBNAME']) ? $_SERVER['PHPBB_TEST_DBNAME'] : '',
-				'dbuser'	=> isset($_SERVER['PHPBB_TEST_DBUSER']) ? $_SERVER['PHPBB_TEST_DBUSER'] : '',
-				'dbpasswd'	=> isset($_SERVER['PHPBB_TEST_DBPASSWD']) ? $_SERVER['PHPBB_TEST_DBPASSWD'] : '',
-			);
-		}
-		else if (file_exists(dirname(__FILE__) . '/../test_config.php'))
-		{
-			include(dirname(__FILE__) . '/../test_config.php');
+		$config = phpbb_test_case_helpers::get_test_config();
 
-			return array(
-				'dbms'		=> $dbms,
-				'dbhost'	=> $dbhost,
-				'dbport'	=> $dbport,
-				'dbname'	=> $dbname,
-				'dbuser'	=> $dbuser,
-				'dbpasswd'	=> $dbpasswd,
-			);
-		}
-		else if (extension_loaded('sqlite') && version_compare(PHPUnit_Runner_Version::id(), '3.4.15', '>='))
-		{
-			// Silently use sqlite
-			return array(
-				'dbms'		=> 'sqlite',
-				'dbhost'	=> dirname(__FILE__) . '/../phpbb_unit_tests.sqlite2', // filename
-				'dbport'	=> '',
-				'dbname'	=> '',
-				'dbuser'	=> '',
-				'dbpasswd'	=> '',
-			);
-		}
-		else
+		if (!isset($config['dbms']))
 		{
 			$this->markTestSkipped('Missing test_config.php: See first error.');
 		}
+
+		return $config;
 	}
 
 	public function getConnection()
diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php
index 9a7ab2d237..9c60969d76 100644
--- a/tests/test_framework/phpbb_test_case_helpers.php
+++ b/tests/test_framework/phpbb_test_case_helpers.php
@@ -41,4 +41,49 @@ class phpbb_test_case_helpers
 		$this->expectedTriggerError = true;
 		$this->test_case->setExpectedException($exceptionName, (string) $message, $errno);
 	}
+
+	static public function get_test_config()
+	{
+		$config = array();
+
+		if (extension_loaded('sqlite') && version_compare(PHPUnit_Runner_Version::id(), '3.4.15', '>='))
+		{
+			$config = array_merge($config, array(
+				'dbms'		=> 'sqlite',
+				'dbhost'	=> dirname(__FILE__) . '/../phpbb_unit_tests.sqlite2', // filename
+				'dbport'	=> '',
+				'dbname'	=> '',
+				'dbuser'	=> '',
+				'dbpasswd'	=> '',
+			));
+		}
+
+		if (file_exists(dirname(__FILE__) . '/../test_config.php'))
+		{
+			include(dirname(__FILE__) . '/../test_config.php');
+
+			$config = array_merge($config, array(
+				'dbms'		=> $dbms,
+				'dbhost'	=> $dbhost,
+				'dbport'	=> $dbport,
+				'dbname'	=> $dbname,
+				'dbuser'	=> $dbuser,
+				'dbpasswd'	=> $dbpasswd,
+			));
+		}
+
+		if (isset($_SERVER['PHPBB_TEST_DBMS']))
+		{
+			$config = array_merge($config, array(
+				'dbms'		=> isset($_SERVER['PHPBB_TEST_DBMS']) ? $_SERVER['PHPBB_TEST_DBMS'] : '',
+				'dbhost'	=> isset($_SERVER['PHPBB_TEST_DBHOST']) ? $_SERVER['PHPBB_TEST_DBHOST'] : '',
+				'dbport'	=> isset($_SERVER['PHPBB_TEST_DBPORT']) ? $_SERVER['PHPBB_TEST_DBPORT'] : '',
+				'dbname'	=> isset($_SERVER['PHPBB_TEST_DBNAME']) ? $_SERVER['PHPBB_TEST_DBNAME'] : '',
+				'dbuser'	=> isset($_SERVER['PHPBB_TEST_DBUSER']) ? $_SERVER['PHPBB_TEST_DBUSER'] : '',
+				'dbpasswd'	=> isset($_SERVER['PHPBB_TEST_DBPASSWD']) ? $_SERVER['PHPBB_TEST_DBPASSWD'] : ''
+			));
+		}
+
+		return $config;
+	}
 }