From 877c39bb7faaa76ac5e82a440e9f9762ab91d34f Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 30 Aug 2021 20:18:31 +0000 Subject: [PATCH] Tests: Test custom assertions parameter data type in `WP_UnitTestCase_Base`. The following changes improve tests stability. The `assertEqualFields()` method expects an object and a fields array as inputs and subsequently approaches the received parameters as such, but did not verify whether the received parameters are of the expected types. Along the same lines, the `assertSameSets()`, `assertEqualSets()`, `assertSameSetsWithIndex()` and the `assertEqualSetsWithIndex()` methods all expect arrays for both the actual as well as the expected values and uses the array function `[k]sort()` on both, but never verified that the received inputs were actually arrays, which could lead to PHP errors on the sorting function calls. Follow-up to [30687], [42343], [48937], [48939], [51480], [51481]. Props jrf. See #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@51697 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 744e3670e0..4225f82496 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -685,6 +685,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertEqualFields( $object, $fields, $message = '' ) { + $this->assertIsObject( $object, $message . ' Passed $object is not an object.' ); + $this->assertIsArray( $fields, $message . ' Passed $fields is not an array.' ); + $this->assertNotEmpty( $fields, $message . ' Fields array is empty.' ); + foreach ( $fields as $field_name => $field_value ) { $this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." ); $this->assertSame( $field_value, $object->$field_name, $message . " Value of property $field_name is not $field_value." ); @@ -760,6 +764,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertSameSets( $expected, $actual, $message = '' ) { + $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); + $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); + sort( $expected ); sort( $actual ); $this->assertSame( $expected, $actual, $message ); @@ -776,6 +783,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertEqualSets( $expected, $actual, $message = '' ) { + $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); + $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); + sort( $expected ); sort( $actual ); $this->assertEquals( $expected, $actual, $message ); @@ -792,6 +802,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) { + $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); + $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); + ksort( $expected ); ksort( $actual ); $this->assertSame( $expected, $actual, $message ); @@ -808,6 +821,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * @param string $message Optional. Message to display when the assertion fails. */ public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) { + $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); + $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); + ksort( $expected ); ksort( $actual ); $this->assertEquals( $expected, $actual, $message );