From ac51d4abcba1492a33587c31d503f9dbaa16a9cc Mon Sep 17 00:00:00 2001
From: jrfnl <jrfnl@users.noreply.github.com>
Date: Sun, 29 Aug 2021 01:43:41 +0200
Subject: [PATCH] WP_UnitTestCase_Base: more improvements to custom assertions
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.
---
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
|
b
|
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { |
685 | 685 | * @param string $message Optional. Message to display when the assertion fails. |
686 | 686 | */ |
687 | 687 | public function assertEqualFields( $object, $fields, $message = '' ) { |
| 688 | $this->assertIsObject( $object, $message . ' Passed $object is not an object.' ); |
| 689 | $this->assertIsArray( $fields, $message . ' Passed $fields is not an array.' ); |
| 690 | $this->assertNotEmpty( $fields, $message . ' Fields array is empty.' ); |
| 691 | |
688 | 692 | foreach ( $fields as $field_name => $field_value ) { |
689 | 693 | $this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." ); |
690 | 694 | $this->assertSame( $field_value, $object->$field_name, $message . " Value of property $field_name is not $field_value." ); |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { |
760 | 764 | * @param string $message Optional. Message to display when the assertion fails. |
761 | 765 | */ |
762 | 766 | public function assertSameSets( $expected, $actual, $message = '' ) { |
| 767 | $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); |
| 768 | $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); |
| 769 | |
763 | 770 | sort( $expected ); |
764 | 771 | sort( $actual ); |
765 | 772 | $this->assertSame( $expected, $actual, $message ); |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { |
776 | 783 | * @param string $message Optional. Message to display when the assertion fails. |
777 | 784 | */ |
778 | 785 | public function assertEqualSets( $expected, $actual, $message = '' ) { |
| 786 | $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); |
| 787 | $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); |
| 788 | |
779 | 789 | sort( $expected ); |
780 | 790 | sort( $actual ); |
781 | 791 | $this->assertEquals( $expected, $actual, $message ); |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { |
792 | 802 | * @param string $message Optional. Message to display when the assertion fails. |
793 | 803 | */ |
794 | 804 | public function assertSameSetsWithIndex( $expected, $actual, $message = '' ) { |
| 805 | $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); |
| 806 | $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); |
| 807 | |
795 | 808 | ksort( $expected ); |
796 | 809 | ksort( $actual ); |
797 | 810 | $this->assertSame( $expected, $actual, $message ); |
… |
… |
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { |
808 | 821 | * @param string $message Optional. Message to display when the assertion fails. |
809 | 822 | */ |
810 | 823 | public function assertEqualSetsWithIndex( $expected, $actual, $message = '' ) { |
| 824 | $this->assertIsArray( $expected, $message . ' Expected value must be an array.' ); |
| 825 | $this->assertIsArray( $actual, $message . ' Value under test is not an array.' ); |
| 826 | |
811 | 827 | ksort( $expected ); |
812 | 828 | ksort( $actual ); |
813 | 829 | $this->assertEquals( $expected, $actual, $message ); |