2013-08-07 08:38:38 +02:00
< ? php
/**
* @ group compat
2015-05-06 04:59:50 +02:00
* @ group security - 153
2013-08-07 08:38:38 +02:00
*/
class Tests_Compat extends WP_UnitTestCase {
2015-05-06 04:59:50 +02:00
function utf8_string_lengths () {
return array (
// string, character_length, byte_length
2017-12-01 00:09:33 +01:00
array ( 'б а б а ' , 4 , 8 ),
array ( 'б а б ' , 3 , 6 ),
array ( 'I am your б ' , 11 , 12 ),
array ( '1111111111' , 10 , 10 ),
array ( '²²²²²²²²²²' , 10 , 20 ),
array ( '3 3 3 3 3 3 3 3 3 3 ' , 10 , 30 ),
array ( '𝟜 𝟜 𝟜 𝟜 𝟜 𝟜 𝟜 𝟜 𝟜 𝟜 ' , 10 , 40 ),
array ( '1²3 𝟜 1²3 𝟜 1²3 𝟜 ' , 12 , 30 ),
2015-05-06 04:59:50 +02:00
);
}
function utf8_substrings () {
return array (
// string, start, length, character_substring, byte_substring
2017-12-01 00:09:33 +01:00
array ( 'б а б а ' , 0 , 3 , 'б а б ' , " б \xD0 " ),
array ( 'б а б а ' , 0 , - 1 , 'б а б ' , " б а б \xD0 " ),
array ( 'б а б а ' , 1 , null , 'а б а ' , " \xB1 а б а " ),
array ( 'б а б а ' , - 3 , null , 'а б а ' , " \xB1 а " ),
array ( 'б а б а ' , - 3 , 2 , 'а б ' , " \xB1 \xD0 " ),
array ( 'б а б а ' , - 1 , 2 , 'а ' , " \xB0 " ),
array ( 'I am your б а б а ' , 0 , 11 , 'I am your б ' , " I am your \xD0 " ),
2015-05-06 04:59:50 +02:00
);
}
/**
* @ dataProvider utf8_string_lengths
*/
function test_mb_strlen ( $string , $expected_character_length ) {
$this -> assertEquals ( $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 -> assertEquals ( $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 -> assertEquals ( $expected_byte_length , _mb_strlen ( $string , '8bit' ) );
}
/**
* @ dataProvider utf8_substrings
*/
function test_mb_substr ( $string , $start , $length , $expected_character_substring ) {
$this -> assertEquals ( $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 -> assertEquals ( $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 -> assertEquals ( $expected_byte_substring , _mb_substr ( $string , $start , $length , '8bit' ) );
}
2017-12-01 00:09:33 +01:00
function test_mb_substr_phpcore () {
2015-05-06 04:59:50 +02:00
/* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_basic.phpt */
$string_ascii = 'ABCDEF' ;
2017-12-01 00:09:33 +01:00
$string_mb = base64_decode ( '5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=' );
2015-05-06 04:59:50 +02:00
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( 'DEF' , _mb_substr ( $string_ascii , 3 ) );
$this -> assertEquals ( 'DEF' , _mb_substr ( $string_ascii , 3 , 5 , 'ISO-8859-1' ) );
2015-05-06 04:59:50 +02:00
2017-12-01 00:09:33 +01:00
// specific latin-1 as that is the default the core php test opporates under
$this -> assertEquals ( 'peacrOiqng==' , base64_encode ( _mb_substr ( $string_mb , 2 , 7 , 'latin-1' ) ) );
$this -> assertEquals ( '6Kqe44OG44Kt44K544OI44Gn44GZ' , base64_encode ( _mb_substr ( $string_mb , 2 , 7 , 'utf-8' ) ) );
2015-05-06 04:59:50 +02:00
/* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt */
2017-12-01 00:09:33 +01:00
$start = 0 ;
$length = 5 ;
2015-05-06 04:59:50 +02:00
$unset_var = 10 ;
2017-12-01 00:09:33 +01:00
unset ( $unset_var );
2018-08-17 03:50:26 +02:00
$heredoc = <<< EOT
2015-05-06 04:59:50 +02:00
hello world
EOT ;
2018-08-17 03:50:26 +02:00
$inputs = array (
2017-11-27 00:56:25 +01:00
0 ,
1 ,
12345 ,
- 2345 ,
// float data
10.5 ,
- 10.5 ,
12.3456789000e10 ,
12.3456789000E-10 ,
. 5 ,
// null data
2017-12-01 00:09:33 +01:00
null ,
2017-11-27 00:56:25 +01:00
null ,
// boolean data
true ,
false ,
2017-12-01 00:09:33 +01:00
true ,
false ,
2017-11-27 00:56:25 +01:00
// empty data
2017-12-01 00:09:33 +01:00
'' ,
2017-11-27 00:56:25 +01:00
'' ,
// string data
2017-12-01 00:09:33 +01:00
'string' ,
2017-11-27 00:56:25 +01:00
'string' ,
$heredoc ,
// object data
2019-07-08 02:55:20 +02:00
new ClassA (),
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentionally undefined data
2017-11-27 00:56:25 +01:00
@ $undefined_var ,
2019-07-08 02:55:20 +02:00
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentionally unset data
2017-11-27 00:56:25 +01:00
@ $unset_var ,
2015-05-06 04:59:50 +02:00
);
2017-12-01 00:09:33 +01:00
$outputs = array (
'0' ,
'1' ,
'12345' ,
'-2345' ,
'10.5' ,
'-10.5' ,
'12345' ,
'1.234' ,
'0.5' ,
'' ,
'' ,
'1' ,
'' ,
'1' ,
'' ,
'' ,
'' ,
'strin' ,
'strin' ,
'hello' ,
'Class' ,
'' ,
'' ,
2015-05-06 04:59:50 +02:00
);
$iterator = 0 ;
2017-12-01 00:09:33 +01:00
foreach ( $inputs as $input ) {
$this -> assertEquals ( $outputs [ $iterator ], _mb_substr ( $input , $start , $length ) );
2015-05-06 04:59:50 +02:00
$iterator ++ ;
}
2013-08-07 08:38:38 +02:00
}
function test_hash_hmac_simple () {
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( '140d1cb79fa12e2a31f32d35ad0a2723' , _hash_hmac ( 'md5' , 'simple' , 'key' ) );
$this -> assertEquals ( '993003b95758e0ac2eba451a4c5877eb1bb7b92a' , _hash_hmac ( 'sha1' , 'simple' , 'key' ) );
2013-08-07 08:38:38 +02:00
}
function test_hash_hmac_padding () {
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( '3c1399103807cf12ec38228614416a8c' , _hash_hmac ( 'md5' , 'simple' , '65 character key 65 character key 65 character key 65 character k' ) );
$this -> assertEquals ( '4428826d20003e309d6c2a6515891370daf184ea' , _hash_hmac ( 'sha1' , 'simple' , '65 character key 65 character key 65 character key 65 character k' ) );
2013-08-07 08:38:38 +02:00
}
function test_hash_hmac_output () {
2017-12-01 00:09:33 +01:00
$this -> assertEquals ( array ( 1 => '140d1cb79fa12e2a31f32d35ad0a2723' ), unpack ( 'H32' , _hash_hmac ( 'md5' , 'simple' , 'key' , true ) ) );
$this -> assertEquals ( array ( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack ( 'H40' , _hash_hmac ( 'sha1' , 'simple' , 'key' , true ) ) );
2013-08-07 08:38:38 +02:00
}
function test_json_encode_decode () {
require_once ( ABSPATH . WPINC . '/class-json.php' );
$json = new Services_JSON ();
// Super basic test to verify Services_JSON is intact and working.
$this -> assertEquals ( '["foo"]' , $json -> encodeUnsafe ( array ( 'foo' ) ) );
$this -> assertEquals ( array ( 'foo' ), $json -> decode ( '["foo"]' ) );
}
2018-04-30 05:42:46 +02:00
/**
2018-05-10 19:57:38 +02:00
* Test that is_countable () is always available ( either from PHP or WP ) .
*
2018-04-30 05:42:46 +02:00
* @ ticket 43583
*/
function test_is_countable_availability () {
$this -> assertTrue ( function_exists ( 'is_countable' ) );
}
/**
* Test is_countable () polyfill .
*
* @ ticket 43583
*
* @ dataProvider countable_variable_test_data
2018-05-10 19:57:38 +02:00
*
* @ param mixed $variable Variable to check .
* @ param bool $is_countable The expected return value of PHP 7.3 is_countable () function .
2018-04-30 05:42:46 +02:00
*/
function test_is_countable_functionality ( $variable , $is_countable ) {
2018-05-10 19:57:38 +02:00
$this -> assertSame ( is_countable ( $variable ), $is_countable );
2018-04-30 05:42:46 +02:00
}
/**
* 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 ),
);
}
2018-04-30 06:14:30 +02:00
/**
2018-05-10 19:57:38 +02:00
* Test is_countable () polyfill for ResourceBundle .
*
* @ ticket 43583
*/
function test_is_countable_ResourceBundle () {
if ( ! class_exists ( 'ResourceBundle' ) ) {
$this -> markTestSkipped ( 'The intl extension is not loaded. ResourceBundle not tested for is_countable().' );
}
2018-05-10 22:15:18 +02:00
if ( version_compare ( PHP_VERSION , '5.4' , '<' ) ) {
$this -> markTestSkipped ( 'ResourceBundle is only countable in PHP 5.4+' );
return ;
}
2018-05-10 19:57:38 +02:00
$this -> assertTrue ( is_countable ( new ResourceBundle ( 'en' , null ) ) );
}
/**
* Test is_countable () polyfill for SimpleXMLElement .
*
* @ ticket 43583
*/
function test_is_countable_SimpleXMLElement () {
if ( ! class_exists ( 'SimpleXMLElement' ) ) {
$this -> markTestSkipped ( 'The xml extension is not loaded. SimpleXMLElement not tested for is_countable().' );
}
$this -> assertTrue ( is_countable ( new SimpleXMLElement ( '<xml><tag>1</tag><tag>2</tag></xml>' ) ) );
}
/**
* Test that is_iterable () is always available ( either from PHP or WP ) .
*
2018-04-30 06:14:30 +02:00
* @ ticket 43619
*/
function test_is_iterable_availability () {
$this -> assertTrue ( function_exists ( 'is_iterable' ) );
}
/**
* Test is_iterable () polyfill .
*
* @ ticket 43619
*
* @ dataProvider iterable_variable_test_data
2018-05-10 19:57:38 +02:00
*
* @ param mixed $variable Variable to check .
* @ param bool $is_iterable The expected return value of PHP 7.1 is_iterable () function .
2018-04-30 06:14:30 +02:00
*/
function test_is_iterable_functionality ( $variable , $is_iterable ) {
2018-05-10 19:57:38 +02:00
$this -> assertSame ( is_iterable ( $variable ), $is_iterable );
2018-04-30 06:14:30 +02:00
}
/**
* 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 ),
);
}
2013-08-07 08:38:38 +02:00
}
2015-05-06 04:59:50 +02:00
2017-12-01 00:09:33 +01:00
/* used in test_mb_substr_phpcore */
2019-07-08 02:55:20 +02:00
class ClassA {
2015-05-06 04:59:50 +02:00
public function __toString () {
2017-12-01 00:09:33 +01:00
return 'Class A object' ;
2015-05-06 04:59:50 +02:00
}
}
2018-04-30 05:42:46 +02:00
class ArrayIteratorFake extends ArrayIterator {
}
class CountableFake implements Countable {
public function count () {
return 16 ;
}
}