diff --git a/libraries/StringCType.class.php b/libraries/StringCType.class.php new file mode 100644 index 0000000000..dbd4bf054f --- /dev/null +++ b/libraries/StringCType.class.php @@ -0,0 +1,106 @@ + diff --git a/libraries/StringNativeType.class.php b/libraries/StringNativeType.class.php new file mode 100644 index 0000000000..4e995d0f2a --- /dev/null +++ b/libraries/StringNativeType.class.php @@ -0,0 +1,135 @@ + diff --git a/libraries/sqlparser.lib.php b/libraries/sqlparser.lib.php index 831d2d54fe..b968dd6c6f 100644 --- a/libraries/sqlparser.lib.php +++ b/libraries/sqlparser.lib.php @@ -317,7 +317,7 @@ function PMA_SQP_parse($sql) } // Checks for white space - if (PMA_STR_isSpace($c)) { + if ($GLOBALS['PMA_StringType']::isSpace($c)) { $this_was_space = true; $count2++; continue; @@ -492,7 +492,7 @@ function PMA_SQP_parse($sql) var_dump(PMA_STR_isSqlIdentifier($c, false)); var_dump($c == '@'); var_dump($c == '.'); - var_dump(PMA_STR_isDigit($GLOBALS['PMA_String']::substr($sql, $count2 + 1, 1))); + var_dump($GLOBALS['PMA_StringType']::isDigit($GLOBALS['PMA_String']::substr($sql, $count2 + 1, 1))); var_dump($previous_was_space); var_dump($previous_was_bracket); var_dump($previous_was_listsep); @@ -503,7 +503,7 @@ function PMA_SQP_parse($sql) if (PMA_STR_isSqlIdentifier($c, false) || $c == '@' || ($c == '.' - && PMA_STR_isDigit($GLOBALS['PMA_String']::substr($sql, $count2 + 1, 1)) + && $GLOBALS['PMA_StringType']::isDigit($GLOBALS['PMA_String']::substr($sql, $count2 + 1, 1)) && ($previous_was_space || $previous_was_bracket || $previous_was_listsep)) ) { /* DEBUG @@ -524,7 +524,7 @@ function PMA_SQP_parse($sql) $is_digit = ( !$is_identifier && !$is_sql_variable - && PMA_STR_isDigit($c) + && $GLOBALS['PMA_StringType']::isDigit($c) ); $is_hex_digit = ( $is_digit @@ -592,7 +592,7 @@ function PMA_SQP_parse($sql) $is_float_digit = false; } } - if (($is_hex_digit && PMA_STR_isHexDigit($c2)) || ($is_digit && PMA_STR_isDigit($c2))) { + if (($is_hex_digit && $GLOBALS['PMA_StringType']::isHexDigit($c2)) || ($is_digit && $GLOBALS['PMA_StringType']::isDigit($c2))) { $count2++; continue; } else { diff --git a/libraries/string.lib.php b/libraries/string.lib.php index a05939b2c4..cc5fedb444 100644 --- a/libraries/string.lib.php +++ b/libraries/string.lib.php @@ -32,9 +32,11 @@ if (@function_exists('mb_strlen')) { * Load ctype handler. */ if (@extension_loaded('ctype')) { - include './libraries/string_type_ctype.lib.php'; + include './libraries/StringCType.class.php'; + $PMA_StringType = new PMA_StringCType(); } else { - include './libraries/string_type_native.lib.php'; + include './libraries/String_NativeType.class.php'; + $PMA_StringType = new PMA_StringNativeType(); } /** @@ -92,8 +94,8 @@ function PMA_STR_numberInRangeInclusive($num, $lower, $upper) * @return boolean whether the character is an SQL identifier or not */ function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false) -{ - return (PMA_STR_isAlnum($c) +{ + return ($GLOBALS['PMA_StringType']::isAlnum($c) || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249 || $c == '_' || $c == '$' diff --git a/libraries/string_type_ctype.lib.php b/libraries/string_type_ctype.lib.php deleted file mode 100644 index 12469baf3a..0000000000 --- a/libraries/string_type_ctype.lib.php +++ /dev/null @@ -1,104 +0,0 @@ - diff --git a/libraries/string_type_native.lib.php b/libraries/string_type_native.lib.php deleted file mode 100644 index 4267a1923d..0000000000 --- a/libraries/string_type_native.lib.php +++ /dev/null @@ -1,133 +0,0 @@ - diff --git a/test/libraries/PMA_StringMB_test.php b/test/libraries/PMA_StringMB_test.php new file mode 100644 index 0000000000..10a5124e8b --- /dev/null +++ b/test/libraries/PMA_StringMB_test.php @@ -0,0 +1,184 @@ +internal_encoding = mb_internal_encoding(); + } + + /** + * TearDown function for tests, restores internal encoding + * + * @access protected + * @return void + */ + protected function tearDown() + { + mb_internal_encoding($this->internal_encoding); + } + + /** + * Test for PMA_StringMB::strlen + * + * @param integer $length Length of the string + * @param string $str String to check for + * @param string $encoding Encoding of the string + * + * @return void + * @test + * @dataProvider mbStrlenData + */ + public function testMbStrlen($length, $str, $encoding) + { + mb_internal_encoding($encoding); + $this->assertEquals( + $length, + PMA_StringMB::strlen($str) + ); + } + + /** + * Data provider for testMbStrlen + * + * @return array Test data + */ + public function mbStrlenData() + { + return array( + array(2, "ab", "UTF-8"), + array(9, "åèö - doo", "UTF-8"), + array(12, "åèö - doo", "ISO-8859-1") + ); + } + + /** + * Test for PMA_StringMB::substr + * + * @param string $str Expected substring + * @param string $haystack String to check in + * @param int $start Starting position of substring + * @param int $length Length of substring + * @param string $encoding Encoding of the string + * + * @return void + * @test + * @dataProvider mbSubStrData + */ + public function testMbSubStr($str, $haystack, $start, $length, $encoding) + { + mb_internal_encoding($encoding); + $this->assertEquals( + $str, + PMA_StringMB::substr($haystack, $start, $length) + ); + } + + /** + * Data provider for testMbSubStr + * + * @return array Test data + */ + public function mbSubStrData() + { + return array( + array("b", "ab", 1, 1, "UTF-8"), + array("èö", "åèö - doo", 1, 2, "UTF-8"), + ); + } + + /** + * Test for PMA_StringMB::strpos + * + * @param int $pos Expected position + * @param string $haystack String to search in + * @param string $needle String to search for + * @param int $offset Search offset + * @param string $encoding Encoding to test against + * + * @return void + * @test + * @dataProvider mbStrposData + */ + public function testMbStrpos($pos, $haystack, $needle, $offset, $encoding) + { + mb_internal_encoding($encoding); + $this->assertEquals( + $pos, + PMA_StringMB::strpos($haystack, $needle, $offset) + ); + } + + /** + * Data provider for testMbStrpos + * + * @return array Test data + */ + public function mbStrposData() + { + return array( + array(1, "ab", "b", 0, "UTF-8"), + array(2, "åèö - doo", "ö", 0, "UTF-8"), + array(6, "åèöè", "è", 3, "ISO-8859-1") + ); + } + + /** + * Test for PMA_StringMB::strtolower + * + * @param string $expected Expected lowercased string + * @param string $string String to convert to lowercase + * @param string $encoding Encoding to test against + * + * @return void + * @test + * @dataProvider mbStrToLowerData + */ + public function testMbStrToLower($expected, $string, $encoding) + { + mb_internal_encoding($encoding); + $this->assertEquals( + $expected, + PMA_StringMB::strtolower($string) + ); + } + + /** + * Data provider for testMbStrpos + * + * @return array Test data + */ + public function mbStrToLowerData() + { + return array( + array("mary had a", "Mary Had A", "UTF-8"), + array("τάχιστη", "Τάχιστη", "UTF-8") + ); + } + +} +?> diff --git a/test/libraries/PMA_StringNative_test.php b/test/libraries/PMA_StringNative_test.php new file mode 100644 index 0000000000..5b8e24bd08 --- /dev/null +++ b/test/libraries/PMA_StringNative_test.php @@ -0,0 +1,151 @@ +assertEquals( + $length, + PMA_StringNative::strlen($str) + ); + } + + /** + * Data provider for testNativeStrlen + * + * @return array Test data + */ + public function nativeStrlenData() + { + return array( + array(2, "ab"), + array(9, "test data"), + array(0, "") + ); + } + + /** + * Test for PMA_StringNative::substr + * + * @param string $str Expected substring + * @param string $haystack String to check in + * @param int $start Starting position of substring + * @param int $length Length of substring + * + * @return void + * @test + * @dataProvider nativeSubStrData + */ + public function testNativeSubStr($str, $haystack, $start, $length) + { + $this->assertEquals( + $str, + PMA_StringNative::substr($haystack, $start, $length) + ); + } + + /** + * Data provider for testNativeSubStr + * + * @return array Test data + */ + public function nativeSubStrData() + { + return array( + array("b", "ab", 1, 1), + array("data", "testdata", 4, 4) + ); + } + + /** + * Test for PMA_StringNative::strpos + * + * @param int $pos Expected position + * @param string $haystack String to search in + * @param string $needle String to search for + * @param int $offset Search offset + * + * @return void + * @test + * @dataProvider nativeStrposData + */ + public function testNativeStrpos($pos, $haystack, $needle, $offset) + { + $this->assertEquals( + $pos, + PMA_StringNative::strpos($haystack, $needle, $offset) + ); + } + + /** + * Data provider for testNativeStrpos + * + * @return array Test data + */ + public function nativeStrposData() + { + return array( + array(1, "ab", "b", 0), + array(4, "test data", " ", 0) + ); + } + + /** + * Test for PMA_StringNative::strtolower + * + * @param string $expected Expected lowercased string + * @param string $string String to convert to lowercase + * + * @return void + * @test + * @dataProvider nativeStrToLowerData + */ + public function testNativeStrToLower($expected, $string) + { + $this->assertEquals( + $expected, + PMA_StringNative::strtolower($string) + ); + } + + /** + * Data provider for testNativeStrpos + * + * @return array Test data + */ + public function nativeStrToLowerData() + { + return array( + array("mary had a", "Mary Had A", "UTF-8"), + array("test string", "TEST STRING", "UTF-8") + ); + } +} +?> diff --git a/test/libraries/PMA_string_test.php b/test/libraries/PMA_string_test.php new file mode 100644 index 0000000000..28994bfb9e --- /dev/null +++ b/test/libraries/PMA_string_test.php @@ -0,0 +1,148 @@ +assertEquals( + $expected, + PMA_STR_charIsEscaped($str, $pos, $start) + ); + } + + /** + * Data provider for testCharIsEscaped + * + * @return array Test data + */ + public function charIsEscapedData() + { + return array( + array(false, 'test', -1, 0), + array(false, 'test', 5, 3), + array(false, 'test', 3, 5), + array(true, '\\test', 1, -1), + array(false, '\\\\test', 2, -1), + array(true, '\\\\tes\\t', 6, 0) + ); + } + + /** + * Test for PMA_STR_numberInRangeInclusive + * + * @param bool $expected Expected value from test + * @param integer $num Number to check for + * @param integer $lower Lower bound + * @param integer $upper Upper bound + * + * @return void + * @test + * @dataProvider numberInRangeData + */ + public function testNumberInRangeInclusive( + $expected, $num, $lower, $upper + ) { + $this->assertEquals( + $expected, + PMA_STR_numberInRangeInclusive($num, $lower, $upper) + ); + } + + /** + * Data provider for testNumberInRangeInclusive + * + * @return void + */ + public function numberInRangeData() + { + return array( + array(true, 2, 2, 3), + array(true, 5, 4, 5), + array(true, 50, 0, 100), + array(false, -1, 0, 20), + array(false, 31, 0, 30) + ); + } + + /** + * Test for PMA_STR_isSqlIdentifier + * + * @param boolean $expected Expected value from test + * @param string $c Character to check for + * @param boolean $dot_is_valid whether the dot character is valid or not + * + * @return void + * @test + * @dataProvider isSqlIdentifierData + */ + public function testIsSqlIdentifier($expected, $c, $dot_is_valid = false) + { + $this->assertEquals( + $expected, + PMA_STR_isSqlIdentifier($c, $dot_is_valid) + ); + } + + /** + * Data provider for testIsSqlIdentifier + * + * @return array Test data + */ + public function isSqlIdentifierData() + { + return array( + array(true, '2'), + array(true, 'a'), + array(true, '.', true), + array(false, '.'), + array(true, chr(192)), + array(false, chr(215)), + array(false, chr(249)), + array(true, '_'), + array(true, '$') + ); + } + + +} +?>