From bcaf65d7cd951b9a453cac413c420cb418b42f8d Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Sat, 3 Sep 2011 17:26:36 -0500 Subject: [PATCH 1/5] [ticket/10349] Unit tests: Remove comments while loading schema files Perform the same operations that the installer does when preparing the schema files. These functions come straight from /includes/functions_install.php and /includes/functions_admin.php. PHPBB3-10349 --- ...phpbb_database_test_connection_manager.php | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index a7559e2183..78be831ecb 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -234,7 +234,12 @@ class phpbb_database_test_connection_manager } $filename = $directory . $schema . '_schema.sql'; - $sql = $this->split_sql(file_get_contents($filename)); + $remove_remarks = $this->dbms['COMMENTS']; + + $queries = file_get_contents($filename); + $this->$remove_remarks($queries); + + $sql = $this->split_sql($queries); foreach ($sql as $query) { @@ -279,6 +284,49 @@ class phpbb_database_test_connection_manager return $data; } + /** + * remove_remarks will strip the sql comment lines out of an uploaded sql file + */ + protected function remove_remarks(&$sql) + { + $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); + } + + /** + * remove_comments will strip the sql comment lines out of an uploaded sql file + * specifically for mssql and postgres type files in the install.... + */ + protected function remove_comments(&$output) + { + $lines = explode("\n", $output); + $output = ''; + + // try to keep mem. use down + $linecount = sizeof($lines); + + $in_comment = false; + for ($i = 0; $i < $linecount; $i++) + { + if (trim($lines[$i]) == '/*') + { + $in_comment = true; + } + + if (!$in_comment) + { + $output .= $lines[$i] . "\n"; + } + + if (trim($lines[$i]) == '*/') + { + $in_comment = false; + } + } + + unset($lines); + return $output; + } + /** * Map a phpBB dbms driver name to dbms data array */ @@ -289,46 +337,55 @@ class phpbb_database_test_connection_manager 'SCHEMA' => 'firebird', 'DELIM' => ';;', 'PDO' => 'firebird', + 'COMMENTS' => 'remove_remarks', ), 'mysqli' => array( 'SCHEMA' => 'mysql_41', 'DELIM' => ';', 'PDO' => 'mysql', + 'COMMENTS' => 'remove_remarks', ), 'mysql' => array( 'SCHEMA' => 'mysql', 'DELIM' => ';', 'PDO' => 'mysql', + 'COMMENTS' => 'remove_remarks', ), 'mssql' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', + 'COMMENTS' => 'remove_comments', ), 'mssql_odbc'=> array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', + 'COMMENTS' => 'remove_comments', ), 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'sqlsrv', + 'COMMENTS' => 'remove_comments', ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', 'PDO' => 'oci', + 'COMMENTS' => 'remove_comments', ), 'postgres' => array( 'SCHEMA' => 'postgres', 'DELIM' => ';', 'PDO' => 'pgsql', + 'COMMENTS' => 'remove_comments', ), 'sqlite' => array( 'SCHEMA' => 'sqlite', 'DELIM' => ';', 'PDO' => 'sqlite2', + 'COMMENTS' => 'remove_remarks', ), ); From 4bc11db0ffc3b387e4ad38b0d9ceccfa58f2738d Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Tue, 20 Sep 2011 23:23:03 -0500 Subject: [PATCH 2/5] [ticket/10349] Unit tests: Consolidate schema comment removal functions PHPBB3-10349 --- ...phpbb_database_test_connection_manager.php | 69 +++---------------- 1 file changed, 11 insertions(+), 58 deletions(-) diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 78be831ecb..547361b41d 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -234,12 +234,11 @@ class phpbb_database_test_connection_manager } $filename = $directory . $schema . '_schema.sql'; - $remove_remarks = $this->dbms['COMMENTS']; $queries = file_get_contents($filename); - $this->$remove_remarks($queries); + $sql = $this->remove_comments($queries); - $sql = $this->split_sql($queries); + $sql = $this->split_sql($sql); foreach ($sql as $query) { @@ -271,60 +270,23 @@ class phpbb_database_test_connection_manager unset($data[key($data)]); } - if ($this->config['dbms'] == 'sqlite') - { - // remove comment lines starting with # - they are not proper sqlite - // syntax and break sqlite2 - foreach ($data as $i => $query) - { - $data[$i] = preg_replace('/^#.*$/m', "\n", $query); - } - } - return $data; } /** - * remove_remarks will strip the sql comment lines out of an uploaded sql file + * Removes comments from schema files + * + * Note: This performs the functions of remove_remarks() and remove_comments() used during installation */ - protected function remove_remarks(&$sql) + protected function remove_comments($sql) { + // Remove /* */ comments (http://ostermiller.org/findcomment.html) + $sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql); + + // Remove # style comments $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); - } - /** - * remove_comments will strip the sql comment lines out of an uploaded sql file - * specifically for mssql and postgres type files in the install.... - */ - protected function remove_comments(&$output) - { - $lines = explode("\n", $output); - $output = ''; - - // try to keep mem. use down - $linecount = sizeof($lines); - - $in_comment = false; - for ($i = 0; $i < $linecount; $i++) - { - if (trim($lines[$i]) == '/*') - { - $in_comment = true; - } - - if (!$in_comment) - { - $output .= $lines[$i] . "\n"; - } - - if (trim($lines[$i]) == '*/') - { - $in_comment = false; - } - } - - unset($lines); - return $output; + return $sql; } /** @@ -337,55 +299,46 @@ class phpbb_database_test_connection_manager 'SCHEMA' => 'firebird', 'DELIM' => ';;', 'PDO' => 'firebird', - 'COMMENTS' => 'remove_remarks', ), 'mysqli' => array( 'SCHEMA' => 'mysql_41', 'DELIM' => ';', 'PDO' => 'mysql', - 'COMMENTS' => 'remove_remarks', ), 'mysql' => array( 'SCHEMA' => 'mysql', 'DELIM' => ';', 'PDO' => 'mysql', - 'COMMENTS' => 'remove_remarks', ), 'mssql' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', - 'COMMENTS' => 'remove_comments', ), 'mssql_odbc'=> array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', - 'COMMENTS' => 'remove_comments', ), 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'sqlsrv', - 'COMMENTS' => 'remove_comments', ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', 'PDO' => 'oci', - 'COMMENTS' => 'remove_comments', ), 'postgres' => array( 'SCHEMA' => 'postgres', 'DELIM' => ';', 'PDO' => 'pgsql', - 'COMMENTS' => 'remove_comments', ), 'sqlite' => array( 'SCHEMA' => 'sqlite', 'DELIM' => ';', 'PDO' => 'sqlite2', - 'COMMENTS' => 'remove_remarks', ), ); From 26e84b12e817dd5e86a82c379a03a67f9f1bacd1 Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Wed, 28 Sep 2011 01:52:08 -0500 Subject: [PATCH 3/5] [ticket/10349] Use new schema comment function in installer This is what now runs in the unit tests. The COMMENTS field in the dbms data is no longer needed, so it has been removed. PHPBB3-10349 --- phpBB/includes/functions_admin.php | 35 ---------------------------- phpBB/includes/functions_install.php | 20 +++++++--------- phpBB/install/install_install.php | 6 ++--- 3 files changed, 11 insertions(+), 50 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index f7e19f3e7d..0c83674054 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -2293,41 +2293,6 @@ function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_fr return; } -/** -* remove_comments will strip the sql comment lines out of an uploaded sql file -* specifically for mssql and postgres type files in the install.... -*/ -function remove_comments(&$output) -{ - $lines = explode("\n", $output); - $output = ''; - - // try to keep mem. use down - $linecount = sizeof($lines); - - $in_comment = false; - for ($i = 0; $i < $linecount; $i++) - { - if (trim($lines[$i]) == '/*') - { - $in_comment = true; - } - - if (!$in_comment) - { - $output .= $lines[$i] . "\n"; - } - - if (trim($lines[$i]) == '*/') - { - $in_comment = false; - } - } - - unset($lines); - return $output; -} - /** * Cache moderators, called whenever permissions are changed via admin_permissions. Changes of username * and group names must be carried through for the moderators table diff --git a/phpBB/includes/functions_install.php b/phpBB/includes/functions_install.php index 992e8d6bb0..270fa0f0b3 100644 --- a/phpBB/includes/functions_install.php +++ b/phpBB/includes/functions_install.php @@ -50,7 +50,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'firebird', 'MODULE' => 'interbase', 'DELIM' => ';;', - 'COMMENTS' => 'remove_remarks', 'DRIVER' => 'firebird', 'AVAILABLE' => true, '2.0.x' => false, @@ -60,7 +59,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'mysql_41', 'MODULE' => 'mysqli', 'DELIM' => ';', - 'COMMENTS' => 'remove_remarks', 'DRIVER' => 'mysqli', 'AVAILABLE' => true, '2.0.x' => true, @@ -70,7 +68,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'mysql', 'MODULE' => 'mysql', 'DELIM' => ';', - 'COMMENTS' => 'remove_remarks', 'DRIVER' => 'mysql', 'AVAILABLE' => true, '2.0.x' => true, @@ -80,7 +77,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'mssql', 'MODULE' => 'mssql', 'DELIM' => 'GO', - 'COMMENTS' => 'remove_comments', 'DRIVER' => 'mssql', 'AVAILABLE' => true, '2.0.x' => true, @@ -90,7 +86,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'mssql', 'MODULE' => 'odbc', 'DELIM' => 'GO', - 'COMMENTS' => 'remove_comments', 'DRIVER' => 'mssql_odbc', 'AVAILABLE' => true, '2.0.x' => true, @@ -100,7 +95,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'mssql', 'MODULE' => 'sqlsrv', 'DELIM' => 'GO', - 'COMMENTS' => 'remove_comments', 'DRIVER' => 'mssqlnative', 'AVAILABLE' => true, '2.0.x' => false, @@ -110,7 +104,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'oracle', 'MODULE' => 'oci8', 'DELIM' => '/', - 'COMMENTS' => 'remove_comments', 'DRIVER' => 'oracle', 'AVAILABLE' => true, '2.0.x' => false, @@ -120,7 +113,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'postgres', 'MODULE' => 'pgsql', 'DELIM' => ';', - 'COMMENTS' => 'remove_comments', 'DRIVER' => 'postgres', 'AVAILABLE' => true, '2.0.x' => true, @@ -130,7 +122,6 @@ function get_available_dbms($dbms = false, $return_unavailable = false, $only_20 'SCHEMA' => 'sqlite', 'MODULE' => 'sqlite', 'DELIM' => ';', - 'COMMENTS' => 'remove_remarks', 'DRIVER' => 'sqlite', 'AVAILABLE' => true, '2.0.x' => false, @@ -514,11 +505,18 @@ function connect_check_db($error_connect, &$error, $dbms_details, $table_prefix, } /** -* remove_remarks will strip the sql comment lines out of an uploaded sql file +* Removes comments from schema files +* */ -function remove_remarks(&$sql) +function remove_comments($sql) { + // Remove /* */ comments (http://ostermiller.org/findcomment.html) + $sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql); + + // Remove # style comments $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); + + return $sql; } /** diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php index 9fe0c8aed5..9b37ee2be3 100644 --- a/phpBB/install/install_install.php +++ b/phpBB/install/install_install.php @@ -1173,14 +1173,13 @@ class install_install extends module $dbms_schema = 'schemas/' . $available_dbms[$data['dbms']]['SCHEMA'] . '_schema.sql'; // How should we treat this schema? - $remove_remarks = $available_dbms[$data['dbms']]['COMMENTS']; $delimiter = $available_dbms[$data['dbms']]['DELIM']; $sql_query = @file_get_contents($dbms_schema); $sql_query = preg_replace('#phpbb_#i', $data['table_prefix'], $sql_query); - $remove_remarks($sql_query); + $sql_query = remove_comments($sql_query); $sql_query = split_sql_file($sql_query, $delimiter); @@ -1218,8 +1217,7 @@ class install_install extends module // Change language strings... $sql_query = preg_replace_callback('#\{L_([A-Z0-9\-_]*)\}#s', 'adjust_language_keys_callback', $sql_query); - // Since there is only one schema file we know the comment style and are able to remove it directly with remove_remarks - remove_remarks($sql_query); + $sql_query = remove_comments($sql_query); $sql_query = split_sql_file($sql_query, ';'); foreach ($sql_query as $sql) From b4dcd4193e7ea52753eb3957bb247b768dc48d12 Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Wed, 28 Sep 2011 11:27:39 -0500 Subject: [PATCH 4/5] [ticket/10349] Update function comment PHPBB3-10349 --- tests/test_framework/phpbb_database_test_connection_manager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 547361b41d..5feab16496 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -275,8 +275,6 @@ class phpbb_database_test_connection_manager /** * Removes comments from schema files - * - * Note: This performs the functions of remove_remarks() and remove_comments() used during installation */ protected function remove_comments($sql) { From baac9e5d150dab88ed6f1eca8ca61bc4468ab01c Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Sat, 29 Oct 2011 17:10:10 -0500 Subject: [PATCH 5/5] [ticket/10349] Removed duplicated functions from schema loading in tests PHPBB3-10349 --- phpBB/includes/functions_install.php | 1 - ...phpbb_database_test_connection_manager.php | 47 ++----------------- 2 files changed, 4 insertions(+), 44 deletions(-) diff --git a/phpBB/includes/functions_install.php b/phpBB/includes/functions_install.php index 270fa0f0b3..6438f35ccc 100644 --- a/phpBB/includes/functions_install.php +++ b/phpBB/includes/functions_install.php @@ -506,7 +506,6 @@ function connect_check_db($error_connect, &$error, $dbms_details, $table_prefix, /** * Removes comments from schema files -* */ function remove_comments($sql) { diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 5feab16496..770cc84d7e 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -7,6 +7,8 @@ * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_install.php'; + class phpbb_database_test_connection_manager { private $config; @@ -236,9 +238,9 @@ class phpbb_database_test_connection_manager $filename = $directory . $schema . '_schema.sql'; $queries = file_get_contents($filename); - $sql = $this->remove_comments($queries); + $sql = remove_comments($queries); - $sql = $this->split_sql($sql); + $sql = split_sql_file($sql, $this->dbms['DELIM']); foreach ($sql as $query) { @@ -246,47 +248,6 @@ class phpbb_database_test_connection_manager } } - /** - * Split contents of an SQL file into an array of SQL statements - * - * Note: This method is not the same as split_sql_file from functions_install. - * - * @param string $sql Raw contents of an SQL file - * - * @return Array of runnable SQL statements - */ - protected function split_sql($sql) - { - $sql = str_replace("\r" , '', $sql); - $data = preg_split('/' . preg_quote($this->dbms['DELIM'], '/') . '$/m', $sql); - - $data = array_map('trim', $data); - - // The empty case - $end_data = end($data); - - if (empty($end_data)) - { - unset($data[key($data)]); - } - - return $data; - } - - /** - * Removes comments from schema files - */ - protected function remove_comments($sql) - { - // Remove /* */ comments (http://ostermiller.org/findcomment.html) - $sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql); - - // Remove # style comments - $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); - - return $sql; - } - /** * Map a phpBB dbms driver name to dbms data array */