From: ggargani Date: Fri, 16 Sep 2016 07:11:32 +0000 (+0200) Subject: CRM-18199 Stripping commnt regexp X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fc6159c223f06e9e7c9acce4384d7b0edcb46458;p=civicrm-core.git CRM-18199 Stripping commnt regexp Reviewed regexp Refactored in a static function in order to make it testable Added tests --- diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index eb61db9248..8c0a064059 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -324,7 +324,7 @@ class CRM_Utils_File { // get rid of comments starting with # and -- - $string = preg_replace("/^(#|--).*\R*/m", "", $string); + $string = self::stripComments($string); $queries = preg_split('/;\s*$/m', $string); foreach ($queries as $query) { @@ -343,6 +343,18 @@ class CRM_Utils_File { } } } + /** + * + * Strips comment from a possibly multiline SQL string + * + * @param string $string + * + * @return string + * stripped string + */ + public static function stripComments($string) { + return preg_replace("/^(#|--).*\R*/m", "", $string); + } /** * @param $ext diff --git a/tests/phpunit/CRM/Utils/FileTest.php b/tests/phpunit/CRM/Utils/FileTest.php index 7028427da8..db2a0a389c 100644 --- a/tests/phpunit/CRM/Utils/FileTest.php +++ b/tests/phpunit/CRM/Utils/FileTest.php @@ -23,5 +23,32 @@ class CRM_Utils_FileTest extends CiviUnitTestCase { )); } } + public function testStripComment() { + $strings = array( + "\nab\n-- cd\nef"=>"\nab\nef", + "ab\n-- cd\nef"=>"ab\nef", + "ab\n-- cd\nef\ngh"=>"ab\nef\ngh", + "ab\n--cd\nef"=>"ab\nef", + "ab\n--cd\nef\n"=>"ab\nef\n", + "ab\n#cd\nef\n"=>"ab\nef\n", + "ab\n--cd\nef"=>"ab\nef", + "ab\n#cd\nef"=>"ab\nef", + "ab\nfoo#cd\nef"=>"ab\nfoo#cd\nef", + "ab\r\n--cd\r\nef"=>"ab\r\nef", + "ab\r\n#cd\r\nef"=>"ab\r\nef", + "ab\r\nfoo#cd\r\nef"=>"ab\r\nfoo#cd\r\nef", + ); + foreach ($strings as $string => $check) { + $test=CRM_Utils_File::stripComments($string); + $this->assertEquals($test, + $check, + sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]", + json_encode($string), + json_encode($test), + json_encode($check) + ) + ); + } + } }