From 07feb01a802494b78a5d07e05fe309f73adb8939 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 10 May 2018 17:57:38 +0000 Subject: [PATCH] General: In the `is_countable()` polyfill, if the provided object implements `SimpleXMLElement` or `ResourceBundle`, consider it countable. Props ayeshrajans, jrf, desrosj. Fixes #43583. git-svn-id: https://develop.svn.wordpress.org/trunk@43220 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/compat.php | 6 ++++- tests/phpunit/tests/compat.php | 40 ++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php index cabe71d211..7de662e366 100644 --- a/src/wp-includes/compat.php +++ b/src/wp-includes/compat.php @@ -520,7 +520,11 @@ if ( ! function_exists( 'is_countable' ) ) { * @return bool True if `$var` is countable, false otherwise. */ function is_countable( $var ) { - return ( is_array( $var ) || $var instanceof Countable ); + return ( is_array( $var ) + || $var instanceof Countable + || $var instanceof SimpleXMLElement + || $var instanceof ResourceBundle + ); } } diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php index 0f17b2d7a8..d6f348c195 100644 --- a/tests/phpunit/tests/compat.php +++ b/tests/phpunit/tests/compat.php @@ -188,6 +188,8 @@ EOT; } /** + * Test that is_countable() is always available (either from PHP or WP). + * * @ticket 43583 */ function test_is_countable_availability() { @@ -200,9 +202,12 @@ EOT; * @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->assertEquals( is_countable( $variable ), $is_countable ); + $this->assertSame( is_countable( $variable ), $is_countable ); } /** @@ -232,6 +237,34 @@ EOT; } /** + * 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().' ); + } + + $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( '12' ) ) ); + } + + /** + * Test that is_iterable() is always available (either from PHP or WP). + * * @ticket 43619 */ function test_is_iterable_availability() { @@ -244,9 +277,12 @@ EOT; * @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->assertEquals( is_iterable( $variable ), $is_iterable ); + $this->assertSame( is_iterable( $variable ), $is_iterable ); } /**