diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php deleted file mode 100644 index ab75d6256b..0000000000 --- a/tests/phpunit/tests/compat.php +++ /dev/null @@ -1,325 +0,0 @@ -assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) ); - } - - /** - * @dataProvider utf8_string_lengths - */ - function test_mb_strlen_via_regex( $string, $expected_character_length ) { - _wp_can_use_pcre_u( false ); - $this->assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) ); - _wp_can_use_pcre_u( 'reset' ); - } - - /** - * @dataProvider utf8_string_lengths - */ - function test_8bit_mb_strlen( $string, $expected_character_length, $expected_byte_length ) { - $this->assertSame( $expected_byte_length, _mb_strlen( $string, '8bit' ) ); - } - - /** - * @dataProvider utf8_substrings - */ - function test_mb_substr( $string, $start, $length, $expected_character_substring ) { - $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) ); - } - - /** - * @dataProvider utf8_substrings - */ - function test_mb_substr_via_regex( $string, $start, $length, $expected_character_substring ) { - _wp_can_use_pcre_u( false ); - $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) ); - _wp_can_use_pcre_u( 'reset' ); - } - - /** - * @dataProvider utf8_substrings - */ - function test_8bit_mb_substr( $string, $start, $length, $expected_character_substring, $expected_byte_substring ) { - $this->assertSame( $expected_byte_substring, _mb_substr( $string, $start, $length, '8bit' ) ); - } - - function test_mb_substr_phpcore() { - /* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_basic.phpt */ - $string_ascii = 'ABCDEF'; - $string_mb = base64_decode( '5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=' ); - - $this->assertSame( 'DEF', _mb_substr( $string_ascii, 3 ) ); - $this->assertSame( 'DEF', _mb_substr( $string_ascii, 3, 5, 'ISO-8859-1' ) ); - - // Specific latin-1 as that is the default the core PHP test operates under. - $this->assertSame( 'peacrOiqng==', base64_encode( _mb_substr( $string_mb, 2, 7, 'latin-1' ) ) ); - $this->assertSame( '6Kqe44OG44Kt44K544OI44Gn44GZ', base64_encode( _mb_substr( $string_mb, 2, 7, 'utf-8' ) ) ); - - /* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt */ - $start = 0; - $length = 5; - $unset_var = 10; - unset( $unset_var ); - $heredoc = <<assertSame( $outputs[ $iterator ], _mb_substr( $input, $start, $length ) ); - $iterator++; - } - - } - - function test_hash_hmac_simple() { - $this->assertSame( '140d1cb79fa12e2a31f32d35ad0a2723', _hash_hmac( 'md5', 'simple', 'key' ) ); - $this->assertSame( '993003b95758e0ac2eba451a4c5877eb1bb7b92a', _hash_hmac( 'sha1', 'simple', 'key' ) ); - } - - function test_hash_hmac_padding() { - $this->assertSame( '3c1399103807cf12ec38228614416a8c', _hash_hmac( 'md5', 'simple', '65 character key 65 character key 65 character key 65 character k' ) ); - $this->assertSame( '4428826d20003e309d6c2a6515891370daf184ea', _hash_hmac( 'sha1', 'simple', '65 character key 65 character key 65 character key 65 character k' ) ); - } - - function test_hash_hmac_output() { - $this->assertSame( array( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ), unpack( 'H32', _hash_hmac( 'md5', 'simple', 'key', true ) ) ); - $this->assertSame( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) ); - } - - function test_json_encode_decode() { - $this->expectDeprecation(); - - require_once ABSPATH . WPINC . '/class-json.php'; - $json = new Services_JSON(); - // Super basic test to verify Services_JSON is intact and working. - $this->assertSame( '["foo"]', $json->encodeUnsafe( array( 'foo' ) ) ); - $this->assertSame( array( 'foo' ), $json->decode( '["foo"]' ) ); - } - - /** - * Test that is_countable() is always available (either from PHP or WP). - * - * @ticket 43583 - */ - function test_is_countable_availability() { - $this->assertTrue( function_exists( 'is_countable' ) ); - } - - /** - * Test is_countable() polyfill. - * - * @ticket 43583 - * - * @dataProvider countable_variable_test_data - * - * @param mixed $variable Variable to check. - * @param bool $is_countable The expected return value of PHP 7.3 is_countable() function. - */ - function test_is_countable_functionality( $variable, $is_countable ) { - $this->assertSame( is_countable( $variable ), $is_countable ); - } - - /** - * Data provider for test_is_countable_functionality(). - * - * @ticket 43583 - * - * @return array { - * @type array { - * @type mixed $variable Variable to check. - * @type bool $is_countable The expected return value of PHP 7.3 is_countable() function. - * } - * } - */ - public function countable_variable_test_data() { - return array( - array( true, false ), - array( new stdClass(), false ), - array( new ArrayIteratorFake(), true ), - array( new CountableFake(), true ), - array( 16, false ), - array( null, false ), - array( array( 1, 2, 3 ), true ), - array( (array) 1, true ), - array( (object) array( 'foo', 'bar', 'baz' ), false ), - ); - } - - /** - * Test is_countable() polyfill for ResourceBundle. - * - * @ticket 43583 - * - * @requires extension intl - */ - function test_is_countable_ResourceBundle() { - $this->assertTrue( is_countable( new ResourceBundle( 'en', null ) ) ); - } - - /** - * Test is_countable() polyfill for SimpleXMLElement. - * - * @ticket 43583 - * - * @requires extension simplexml - */ - function test_is_countable_SimpleXMLElement() { - $this->assertTrue( is_countable( new SimpleXMLElement( '12' ) ) ); - } - - /** - * Test that is_iterable() is always available (either from PHP or WP). - * - * @ticket 43619 - */ - function test_is_iterable_availability() { - $this->assertTrue( function_exists( 'is_iterable' ) ); - } - - /** - * Test is_iterable() polyfill. - * - * @ticket 43619 - * - * @dataProvider iterable_variable_test_data - * - * @param mixed $variable Variable to check. - * @param bool $is_iterable The expected return value of PHP 7.1 is_iterable() function. - */ - function test_is_iterable_functionality( $variable, $is_iterable ) { - $this->assertSame( is_iterable( $variable ), $is_iterable ); - } - - /** - * Data provider for test_is_iterable_functionality(). - * - * @ticket 43619 - * - * @return array { - * @type array { - * @type mixed $variable Variable to check. - * @type bool $is_iterable The expected return value of PHP 7.1 is_iterable() function. - * } - * } - */ - public function iterable_variable_test_data() { - return array( - array( array(), true ), - array( array( 1, 2, 3 ), true ), - array( new ArrayIterator( array( 1, 2, 3 ) ), true ), - array( 1, false ), - array( 3.14, false ), - array( new stdClass(), false ), - ); - } -} - -/* used in test_mb_substr_phpcore */ -class ClassA { - public function __toString() { - return 'Class A object'; - } -} - -class ArrayIteratorFake extends ArrayIterator { -} - -class CountableFake implements Countable { - #[ReturnTypeWillChange] - public function count() { - return 16; - } -} diff --git a/tests/phpunit/tests/compat/hashHmac.php b/tests/phpunit/tests/compat/hashHmac.php new file mode 100644 index 0000000000..f9a61d5fac --- /dev/null +++ b/tests/phpunit/tests/compat/hashHmac.php @@ -0,0 +1,65 @@ +assertTrue( function_exists( 'hash_hmac' ) ); + } + + public function test_hash_hmac_simple() { + $data = 'simple'; + $key = 'key'; + + $this->assertSame( + '140d1cb79fa12e2a31f32d35ad0a2723', + _hash_hmac( 'md5', $data, $key ), + 'MD5 hash does not match' + ); + $this->assertSame( + '993003b95758e0ac2eba451a4c5877eb1bb7b92a', + _hash_hmac( 'sha1', $data, $key ), + 'sha1 hash does not match' + ); + } + + public function test_hash_hmac_padding() { + $data = 'simple'; + $key = '65 character key 65 character key 65 character key 65 character k'; + + $this->assertSame( + '3c1399103807cf12ec38228614416a8c', + _hash_hmac( 'md5', $data, $key ), + 'MD5 hash does not match' + ); + $this->assertSame( + '4428826d20003e309d6c2a6515891370daf184ea', + _hash_hmac( 'sha1', $data, $key ), + 'sha1 hash does not match' + ); + } + + public function test_hash_hmac_output() { + $data = 'simple'; + $key = 'key'; + + $this->assertSame( + array( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ), + unpack( 'H32', _hash_hmac( 'md5', $data, $key, true ) ), + 'unpacked MD5 hash does not match' + ); + $this->assertSame( + array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), + unpack( 'H40', _hash_hmac( 'sha1', $data, $key, true ) ), + 'unpacked sha1 hash does not match' + ); + } +} diff --git a/tests/phpunit/tests/compat/isCountable.php b/tests/phpunit/tests/compat/isCountable.php new file mode 100644 index 0000000000..66bf3c8c6c --- /dev/null +++ b/tests/phpunit/tests/compat/isCountable.php @@ -0,0 +1,117 @@ +assertTrue( function_exists( 'is_countable' ) ); + } + + /** + * Test is_countable() polyfill. + * + * @ticket 43583 + * + * @dataProvider data_is_countable_functionality + * + * @param mixed $variable Variable to check. + * @param bool $is_countable The expected return value of PHP 7.3 is_countable() function. + */ + public function test_is_countable_functionality( $variable, $is_countable ) { + $this->assertSame( $is_countable, is_countable( $variable ) ); + } + + /** + * Data provider for test_is_countable_functionality(). + * + * @ticket 43583 + * + * @return array { + * @type array { + * @type mixed $variable Variable to check. + * @type bool $is_countable The expected return value of PHP 7.3 is_countable() function. + * } + * } + */ + public function data_is_countable_functionality() { + return array( + 'boolean true' => array( + 'variable' => true, + 'is_countable' => false, + ), + 'plain stdClass object' => array( + 'variable' => new stdClass(), + 'is_countable' => false, + ), + 'Array iterator object' => array( + 'variable' => new ArrayIteratorFakeForIsCountable(), + 'is_countable' => true, + ), + 'Countable object' => array( + 'variable' => new CountableFakeForIsCountable(), + 'is_countable' => true, + ), + 'integer 16' => array( + 'variable' => 16, + 'is_countable' => false, + ), + 'null' => array( + 'variable' => null, + 'is_countable' => false, + ), + 'non-empty array, 3 items' => array( + 'variable' => array( 1, 2, 3 ), + 'is_countable' => true, + ), + 'non-empty array, 1 item via cast' => array( + 'variable' => (array) 1, + 'is_countable' => true, + ), + 'array cast to object' => array( + 'variable' => (object) array( 'foo', 'bar', 'baz' ), + 'is_countable' => false, + ), + ); + } + + /** + * Test is_countable() polyfill for ResourceBundle. + * + * @ticket 43583 + * + * @requires extension intl + */ + public function test_is_countable_ResourceBundle() { + $this->assertTrue( is_countable( new ResourceBundle( 'en', null ) ) ); + } + + /** + * Test is_countable() polyfill for SimpleXMLElement. + * + * @ticket 43583 + * + * @requires extension simplexml + */ + public function test_is_countable_SimpleXMLElement() { + $this->assertTrue( is_countable( new SimpleXMLElement( '12' ) ) ); + } +} + +class ArrayIteratorFakeForIsCountable extends ArrayIterator { +} + +class CountableFakeForIsCountable implements Countable { + #[ReturnTypeWillChange] + public function count() { + return 16; + } +} diff --git a/tests/phpunit/tests/compat/isIterable.php b/tests/phpunit/tests/compat/isIterable.php new file mode 100644 index 0000000000..e09bb18278 --- /dev/null +++ b/tests/phpunit/tests/compat/isIterable.php @@ -0,0 +1,77 @@ +assertTrue( function_exists( 'is_iterable' ) ); + } + + /** + * Test is_iterable() polyfill. + * + * @ticket 43619 + * + * @dataProvider data_is_iterable_functionality + * + * @param mixed $variable Variable to check. + * @param bool $is_iterable The expected return value of PHP 7.1 is_iterable() function. + */ + public function test_is_iterable_functionality( $variable, $is_iterable ) { + $this->assertSame( $is_iterable, is_iterable( $variable ) ); + } + + /** + * Data provider for test_is_iterable_functionality(). + * + * @ticket 43619 + * + * @return array { + * @type array { + * @type mixed $variable Variable to check. + * @type bool $is_iterable The expected return value of PHP 7.1 is_iterable() function. + * } + * } + */ + public function data_is_iterable_functionality() { + return array( + 'empty array' => array( + 'variable' => array(), + 'is_iterable' => true, + ), + 'non-empty array' => array( + 'variable' => array( 1, 2, 3 ), + 'is_iterable' => true, + ), + 'Iterator object' => array( + 'variable' => new ArrayIterator( array( 1, 2, 3 ) ), + 'is_iterable' => true, + ), + 'null' => array( + 'variable' => null, + 'is_iterable' => false, + ), + 'integer 1' => array( + 'variable' => 1, + 'is_iterable' => false, + ), + 'float 3.14' => array( + 'variable' => 3.14, + 'is_iterable' => false, + ), + 'plain stdClass object' => array( + 'variable' => new stdClass(), + 'is_iterable' => false, + ), + ); + } +} diff --git a/tests/phpunit/tests/compat/jsonEncodeDecode.php b/tests/phpunit/tests/compat/jsonEncodeDecode.php new file mode 100644 index 0000000000..475af85b9f --- /dev/null +++ b/tests/phpunit/tests/compat/jsonEncodeDecode.php @@ -0,0 +1,28 @@ +expectDeprecation(); + + require_once ABSPATH . WPINC . '/class-json.php'; + $json = new Services_JSON(); + + // Super basic test to verify Services_JSON is intact and working. + $this->assertSame( + '["foo"]', + $json->encodeUnsafe( array( 'foo' ) ), + 'encodeUnsafe() did not return expected output' + ); + $this->assertSame( + array( 'foo' ), + $json->decode( '["foo"]' ), + 'decode() did not return expected output' + ); + } +} diff --git a/tests/phpunit/tests/compat/mbStrlen.php b/tests/phpunit/tests/compat/mbStrlen.php new file mode 100644 index 0000000000..e34b44534b --- /dev/null +++ b/tests/phpunit/tests/compat/mbStrlen.php @@ -0,0 +1,91 @@ +assertTrue( function_exists( 'mb_strlen' ) ); + } + + /** + * @dataProvider utf8_string_lengths + */ + public function test_mb_strlen( $string, $expected_character_length ) { + $this->assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) ); + } + + /** + * @dataProvider utf8_string_lengths + */ + public function test_mb_strlen_via_regex( $string, $expected_character_length ) { + _wp_can_use_pcre_u( false ); + $this->assertSame( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) ); + _wp_can_use_pcre_u( 'reset' ); + } + + /** + * @dataProvider utf8_string_lengths + */ + public function test_8bit_mb_strlen( $string, $expected_character_length, $expected_byte_length ) { + $this->assertSame( $expected_byte_length, _mb_strlen( $string, '8bit' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function utf8_string_lengths() { + return array( + array( + 'string' => 'баба', + 'expected_character_length' => 4, + 'expected_byte_length' => 8, + ), + array( + 'string' => 'баб', + 'expected_character_length' => 3, + 'expected_byte_length' => 6, + ), + array( + 'string' => 'I am your б', + 'expected_character_length' => 11, + 'expected_byte_length' => 12, + ), + array( + 'string' => '1111111111', + 'expected_character_length' => 10, + 'expected_byte_length' => 10, + ), + array( + 'string' => '²²²²²²²²²²', + 'expected_character_length' => 10, + 'expected_byte_length' => 20, + ), + array( + 'string' => '3333333333', + 'expected_character_length' => 10, + 'expected_byte_length' => 30, + ), + array( + 'string' => '𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜', + 'expected_character_length' => 10, + 'expected_byte_length' => 40, + ), + array( + 'string' => '1²3𝟜1²3𝟜1²3𝟜', + 'expected_character_length' => 12, + 'expected_byte_length' => 30, + ), + ); + } +} diff --git a/tests/phpunit/tests/compat/mbSubstr.php b/tests/phpunit/tests/compat/mbSubstr.php new file mode 100644 index 0000000000..f03f5f0f19 --- /dev/null +++ b/tests/phpunit/tests/compat/mbSubstr.php @@ -0,0 +1,241 @@ +assertTrue( function_exists( 'mb_substr' ) ); + } + + /** + * @dataProvider utf8_substrings + */ + public function test_mb_substr( $string, $start, $length, $expected_character_substring ) { + $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) ); + } + + /** + * @dataProvider utf8_substrings + */ + public function test_mb_substr_via_regex( $string, $start, $length, $expected_character_substring ) { + _wp_can_use_pcre_u( false ); + $this->assertSame( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) ); + _wp_can_use_pcre_u( 'reset' ); + } + + /** + * @dataProvider utf8_substrings + */ + public function test_8bit_mb_substr( $string, $start, $length, $expected_character_substring, $expected_byte_substring ) { + $this->assertSame( $expected_byte_substring, _mb_substr( $string, $start, $length, '8bit' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function utf8_substrings() { + return array( + array( + 'string' => 'баба', + 'start' => 0, + 'length' => 3, + 'expected_character_substring' => 'баб', + 'expected_byte_substring' => "б\xD0", + ), + array( + 'string' => 'баба', + 'start' => 0, + 'length' => -1, + 'expected_character_substring' => 'баб', + 'expected_byte_substring' => "баб\xD0", + ), + array( + 'string' => 'баба', + 'start' => 1, + 'length' => null, + 'expected_character_substring' => 'аба', + 'expected_byte_substring' => "\xB1аба", + ), + array( + 'string' => 'баба', + 'start' => -3, + 'length' => null, + 'expected_character_substring' => 'аба', + 'expected_byte_substring' => "\xB1а", + ), + array( + 'string' => 'баба', + 'start' => -3, + 'length' => 2, + 'expected_character_substring' => 'аб', + 'expected_byte_substring' => "\xB1\xD0", + ), + array( + 'string' => 'баба', + 'start' => -1, + 'length' => 2, + 'expected_character_substring' => 'а', + 'expected_byte_substring' => "\xB0", + ), + array( + 'string' => 'I am your баба', + 'start' => 0, + 'length' => 11, + 'expected_character_substring' => 'I am your б', + 'expected_byte_substring' => "I am your \xD0", + ), + ); + } + + /** + * @link https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_basic.phpt + */ + public function test_mb_substr_phpcore_basic() { + $string_ascii = 'ABCDEF'; + $string_mb = base64_decode( '5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=' ); + + $this->assertSame( + 'DEF', + _mb_substr( $string_ascii, 3 ), + 'Substring does not match expected for offset 3' + ); + $this->assertSame( + 'DEF', + _mb_substr( $string_ascii, 3, 5, 'ISO-8859-1' ), + 'Substring does not match expected for offset 3, length 5, with iso charset' + ); + + // Specific latin-1 as that is the default the core PHP test operates under. + $this->assertSame( + 'peacrOiqng==', + base64_encode( _mb_substr( $string_mb, 2, 7, 'latin-1' ) ), + 'Substring does not match expected for offset 2, length 7, with latin-1 charset' + ); + $this->assertSame( + '6Kqe44OG44Kt44K544OI44Gn44GZ', + base64_encode( _mb_substr( $string_mb, 2, 7, 'utf-8' ) ), + 'Substring does not match expected for offset 2, length 7, with utf-8 charset' + ); + } + + /** + * @link https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt + * + * @dataProvider data_mb_substr_phpcore_input_type_handling + * + * @param mixed $input Input to pass to the function. + * @param string $expected Expected function output. + */ + public function test_mb_substr_phpcore_input_type_handling( $input, $expected ) { + $start = 0; + $length = 5; + + $this->assertSame( $expected, _mb_substr( $input, $start, $length ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_mb_substr_phpcore_input_type_handling() { + $heredoc = << array( + 'input' => 0, + 'expected' => '0', + ), + 'integer 1' => array( + 'input' => 1, + 'expected' => '1', + ), + 'positive integer' => array( + 'input' => 12345, + 'expected' => '12345', + ), + 'negative integer' => array( + 'input' => -2345, + 'expected' => '-2345', + ), + // Float data. + 'positive float with fraction' => array( + 'input' => 10.5, + 'expected' => '10.5', + ), + 'negative float with fraction' => array( + 'input' => -10.5, + 'expected' => '-10.5', + ), + 'float scientific whole number' => array( + 'input' => 12.3456789000e10, + 'expected' => '12345', + ), + 'float scientific with fraction' => array( + 'input' => 12.3456789000E-10, + 'expected' => '1.234', + ), + 'float, fraction only' => array( + 'input' => .5, + 'expected' => '0.5', + ), + // Null data. + 'null' => array( + 'input' => null, + 'expected' => '', + ), + // Boolean data. + 'boolean true' => array( + 'input' => true, + 'expected' => '1', + ), + 'boolean false' => array( + 'input' => false, + 'expected' => '', + ), + // Empty data. + 'empty string' => array( + 'input' => '', + 'expected' => '', + ), + // String data. + 'double quoted string' => array( + 'input' => "string'", + 'expected' => 'strin', + ), + 'single quoted string' => array( + 'input' => 'string', + 'expected' => 'strin', + ), + 'heredoc string' => array( + 'input' => $heredoc, + 'expected' => 'hello', + ), + // Object data. + 'object with __toString method' => array( + 'input' => new ClassWithToStringForMbSubstr(), + 'expected' => 'Class', + ), + ); + } +} + +/* used in data_mb_substr_phpcore_input_type_handling() */ +class ClassWithToStringForMbSubstr { + public function __toString() { + return 'Class object'; + } +}