Make WordPress Core

Ticket #38777: 38777.5.diff

File 38777.5.diff, 7.2 KB (added by dlh, 4 years ago)
  • src/wp-includes/class-wp-error.php

    diff --git src/wp-includes/class-wp-error.php src/wp-includes/class-wp-error.php
    index e22357ac28..eb5e3f133a 100644
    class WP_Error { 
    2727        public $errors = array();
    2828
    2929        /**
    30          * Stores the list of data for error codes.
     30         * Stores the most-recently added data for error codes.
    3131         *
    3232         * @since 2.1.0
    3333         * @var array
    3434         */
    3535        public $error_data = array();
    3636
     37        /**
     38         * Stores previously added data added for error codes, oldest-to-newest by code.
     39         *
     40         * @since 5.6.0
     41         * @var array[]
     42         */
     43        protected $additional_data = array();
     44
    3745        /**
    3846         * Initialize the error.
    3947         *
    class WP_Error { 
    140148        }
    141149
    142150        /**
    143          * Retrieve error data for error code.
     151         * Retrieve the most-recently added error data for error code.
    144152         *
    145153         * @since 2.1.0
    146154         *
    class WP_Error { 
    203211        /**
    204212         * Add data for error code.
    205213         *
    206          * The error code can only contain one error data.
    207          *
    208214         * @since 2.1.0
     215         * @since 5.6.0 Error codes can now contain more than one error data. {@see WP_Error::$additional_data}.
    209216         *
    210217         * @param mixed      $data Error data.
    211218         * @param string|int $code Error code.
    class WP_Error { 
    215222                        $code = $this->get_error_code();
    216223                }
    217224
     225                if ( isset( $this->error_data[ $code ] ) ) {
     226                        $this->additional_data[ $code ][] = $this->error_data[ $code ];
     227                }
     228
    218229                $this->error_data[ $code ] = $data;
    219230        }
    220231
     232        /**
     233         * Retrieve all error data for an error code in the order in which the data was added.
     234         *
     235         * @since 5.6.0
     236         *
     237         * @param string|int $code Error code.
     238         * @return mixed[] Error data, if it exists.
     239         */
     240        public function get_all_error_data( $code ) {
     241                $data = array();
     242
     243                if ( isset( $this->additional_data[ $code ] ) ) {
     244                        $data = array_merge( $data, $this->additional_data[ $code ] );
     245                }
     246
     247                if ( isset( $this->error_data[ $code ] ) ) {
     248                        $data[] = $this->error_data[ $code ];
     249                }
     250
     251                return $data;
     252        }
     253
    221254        /**
    222255         * Removes the specified error.
    223256         *
    class WP_Error { 
    231264        public function remove( $code ) {
    232265                unset( $this->errors[ $code ] );
    233266                unset( $this->error_data[ $code ] );
     267                unset( $this->additional_data[ $code ] );
     268        }
     269
     270        /**
     271         * Merge the errors in another error object into this one.
     272         *
     273         * @since 5.6.0
     274         *
     275         * @param WP_Error $error Error object to merge.
     276         */
     277        public function merge_from( WP_Error $error ) {
     278                static::copy_errors( $error, $this );
     279        }
     280
     281        /**
     282         * Export the errors in this object into another one.
     283         *
     284         * @since 5.6.0
     285         *
     286         * @param WP_Error $error Error object to export into.
     287         */
     288        public function export_to( WP_Error $error ) {
     289                static::copy_errors( $this, $error );
     290        }
     291
     292        /**
     293         * Copy errors from one WP_Error to another.
     294         *
     295         * @since 5.6.0
     296         *
     297         * @param WP_Error $from From.
     298         * @param WP_Error $to   To.
     299         */
     300        protected static function copy_errors( WP_Error $from, WP_Error $to ) {
     301                foreach ( $from->get_error_codes() as $code ) {
     302                        foreach ( $from->get_error_messages( $code ) as $error_message ) {
     303                                $to->add( $code, $error_message );
     304                        }
     305
     306                        foreach ( $from->get_all_error_data( $code ) as $data ) {
     307                                $to->add_data( $data, $code );
     308                        }
     309                }
    234310        }
    235311}
  • tests/phpunit/tests/general/wpError.php

    diff --git tests/phpunit/tests/general/wpError.php tests/phpunit/tests/general/wpError.php
    index f7b234c0ce..2fbd0826a4 100644
    class Tests_WP_Error extends WP_UnitTestCase { 
    375375                $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code' ) );
    376376        }
    377377
     378        /**
     379         * @covers ::get_all_error_data
     380         */
     381        public function test_get_all_error_data_with_code_and_no_errors_should_evaluate_as_empty_array() {
     382                $this->assertSame( array(), $this->wp_error->get_all_error_data( 'code' ) );
     383        }
     384
     385        /**
     386         * @covers ::get_all_error_data
     387         */
     388        public function test_get_all_error_data_with_code_and_one_error_with_no_data_should_evaluate_as_empty_array() {
     389                $this->wp_error->add( 'code', 'message' );
     390
     391                $this->assertSame( array(), $this->wp_error->get_all_error_data( 'code' ) );
     392        }
     393
     394        /**
     395         * @covers ::get_all_error_data
     396         */
     397        public function test_get_all_error_data_with_code_and_one_error_with_data_should_return_that_data() {
     398                $expected = array( 'data-key' => 'data-value' );
     399                $this->wp_error->add( 'code', 'message', $expected );
     400
     401                $actual = $this->wp_error->get_all_error_data( 'code' );
     402                $this->assertCount( 1, $actual );
     403                $this->assertSameSetsWithIndex( $expected, $actual[0] );
     404        }
     405
     406        /**
     407         * @covers ::get_all_error_data
     408         */
     409        public function test_get_all_error_data_with_code_and_multiple_errors_same_code_should_return_all_data() {
     410                $this->wp_error->add( 'code', 'message', 'data' );
     411                $this->wp_error->add( 'code', 'message2', 'data2' );
     412                $this->wp_error->add( 'code2', 'message3', 'data3' );
     413
     414                $this->assertSame( array( 'data', 'data2' ), $this->wp_error->get_all_error_data( 'code' ) );
     415        }
     416
    378417        /**
    379418         * @covers ::has_errors
    380419         */
    class Tests_WP_Error extends WP_UnitTestCase { 
    711750         * @covers ::remove
    712751         */
    713752        public function test_remove_should_remove_the_error_data_associated_with_the_given_code() {
    714                 $this->wp_error->add( 'code', 'message', 'data' );
     753                $this->wp_error->add( 'code', 'message', 'data1' );
     754                $this->wp_error->add( 'code', 'message', 'data2' );
    715755
    716756                $this->wp_error->remove( 'code' );
    717757
    718758                $this->assertEmpty( $this->wp_error->error_data );
     759                $this->assertEmpty( $this->wp_error->get_all_error_data( 'code' ) );
    719760        }
    720761
     762        /**
     763         * @covers ::merge_from()
     764         */
     765        public function test_merge_from_should_copy_other_error_into_instance() {
     766                $this->wp_error->add( 'code1', 'message1', 'data1' );
     767
     768                $other = new \WP_Error( 'code1', 'message2', 'data2' );
     769                $other->add( 'code2', 'message3' );
     770                $this->wp_error->merge_from( $other );
     771
     772                $this->assertSame( array( 'message1', 'message2' ), $this->wp_error->get_error_messages( 'code1' ) );
     773                $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code1' ) );
     774                $this->assertSame( array( 'data1', 'data2' ), $this->wp_error->get_all_error_data( 'code1' ) );
     775                $this->assertSame( 'message3', $this->wp_error->get_error_message( 'code2' ) );
     776        }
     777
     778        /**
     779         * @covers ::merge_from()
     780         */
     781        public function test_merge_from_with_no_errors_should_not_add_to_instance() {
     782                $other = new \WP_Error();
     783
     784                $this->wp_error->merge_from( $other );
     785
     786                $this->assertFalse( $this->wp_error->has_errors() );
     787        }
     788
     789        /**
     790         * @covers ::export_to()
     791         */
     792        public function test_export_to_should_copy_instance_into_other_error() {
     793                $other = new \WP_Error();
     794                $other->add( 'code1', 'message1', 'data1' );
     795
     796                $this->wp_error->add( 'code1', 'message2', 'data2' );
     797                $this->wp_error->add( 'code2', 'message3' );
     798
     799                $this->wp_error->export_to( $other );
     800
     801                $this->assertSame( array( 'message1', 'message2' ), $other->get_error_messages( 'code1' ) );
     802                $this->assertSame( 'data2', $other->get_error_data( 'code1' ) );
     803                $this->assertSame( array( 'data1', 'data2' ), $other->get_all_error_data( 'code1' ) );
     804                $this->assertSame( 'message3', $other->get_error_message( 'code2' ) );
     805        }
     806
     807        /**
     808         * @covers ::export_to()
     809         */
     810        public function test_export_to_with_no_errors_should_not_add_to_other_error() {
     811                $other = new \WP_Error();
     812
     813                $this->wp_error->export_to( $other );
     814
     815                $this->assertFalse( $other->has_errors() );
     816        }
    721817}