Make WordPress Core

Ticket #53363: 53363-10-custom-assertions-more-stabilizing.patch

File 53363-10-custom-assertions-more-stabilizing.patch, 4.0 KB (added by jrf, 2 years ago)

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

    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 { 
    685685         * @param string $message Optional. Message to display when the assertion fails.
    686686         */
    687687        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
    688692                foreach ( $fields as $field_name => $field_value ) {
    689693                        $this->assertObjectHasAttribute( $field_name, $object, $message . " Property $field_name does not exist on the object." );
    690694                        $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 { 
    760764         * @param string $message  Optional. Message to display when the assertion fails.
    761765         */
    762766        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
    763770                sort( $expected );
    764771                sort( $actual );
    765772                $this->assertSame( $expected, $actual, $message );
    abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { 
    776783         * @param string $message  Optional. Message to display when the assertion fails.
    777784         */
    778785        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
    779789                sort( $expected );
    780790                sort( $actual );
    781791                $this->assertEquals( $expected, $actual, $message );
    abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { 
    792802         * @param string $message  Optional. Message to display when the assertion fails.
    793803         */
    794804        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
    795808                ksort( $expected );
    796809                ksort( $actual );
    797810                $this->assertSame( $expected, $actual, $message );
    abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { 
    808821         * @param string $message  Optional. Message to display when the assertion fails.
    809822         */
    810823        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
    811827                ksort( $expected );
    812828                ksort( $actual );
    813829                $this->assertEquals( $expected, $actual, $message );