Make WordPress Core


Ignore:
Timestamp:
10/09/2020 10:20:50 PM (4 years ago)
Author:
johnbillion
Message:

General: Introduce the ability to merge multiple WP_Error objects into one another, and to store more than one item of data for an error.

This allows multiple errors to be instantiated independently but collected into one without having to manually combine their properties.

Props rmccue, dlh, TimothyBlynJacobs

Fixes #38777

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/general/wpError.php

    r48940 r49115  
    377377
    378378    /**
     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
     417    /**
     418     * @covers ::get_all_error_data
     419     */
     420    public function test_get_all_error_data_should_handle_manipulation_of_error_data_property() {
     421        $this->wp_error->add_data( 'data1', 'code' );
     422        $this->wp_error->add_data( 'data2', 'code' );
     423
     424        $this->wp_error->error_data['code'] = 'dataX';
     425
     426        $this->assertSame( 'dataX', $this->wp_error->get_error_data( 'code' ) );
     427        $this->assertSame( array( 'data1', 'dataX' ), $this->wp_error->get_all_error_data( 'code' ) );
     428    }
     429
     430    /**
    379431     * @covers ::has_errors
    380432     */
     
    713765    public function test_remove_should_remove_the_error_data_associated_with_the_given_code() {
    714766        $this->wp_error->add( 'code', 'message', 'data' );
     767        $this->wp_error->add( 'code', 'message', 'data2' );
    715768
    716769        $this->wp_error->remove( 'code' );
    717770
    718771        $this->assertEmpty( $this->wp_error->error_data );
    719     }
    720 
     772        $this->assertEmpty( $this->wp_error->get_error_data( 'code' ) );
     773        $this->assertEmpty( $this->wp_error->get_all_error_data( 'code' ) );
     774    }
     775
     776    /**
     777     * @covers ::merge_from()
     778     */
     779    public function test_merge_from_should_copy_other_error_into_instance() {
     780        $this->wp_error->add( 'code1', 'message1', 'data1' );
     781
     782        $other = new WP_Error( 'code1', 'message2', 'data2' );
     783        $other->add( 'code2', 'message3' );
     784        $this->wp_error->merge_from( $other );
     785
     786        $this->assertSame( array( 'message1', 'message2' ), $this->wp_error->get_error_messages( 'code1' ) );
     787        $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code1' ) );
     788        $this->assertSame( array( 'data1', 'data2' ), $this->wp_error->get_all_error_data( 'code1' ) );
     789        $this->assertSame( 'message3', $this->wp_error->get_error_message( 'code2' ) );
     790    }
     791
     792    /**
     793     * @covers ::merge_from()
     794     */
     795    public function test_merge_from_with_no_errors_should_not_add_to_instance() {
     796        $other = new WP_Error();
     797
     798        $this->wp_error->merge_from( $other );
     799
     800        $this->assertFalse( $this->wp_error->has_errors() );
     801    }
     802
     803    /**
     804     * @covers ::export_to()
     805     */
     806    public function test_export_to_should_copy_instance_into_other_error() {
     807        $other = new WP_Error();
     808        $other->add( 'code1', 'message1', 'data1' );
     809
     810        $this->wp_error->add( 'code1', 'message2', 'data2' );
     811        $this->wp_error->add( 'code2', 'message3' );
     812
     813        $this->wp_error->export_to( $other );
     814
     815        $this->assertSame( array( 'message1', 'message2' ), $other->get_error_messages( 'code1' ) );
     816        $this->assertSame( 'data2', $other->get_error_data( 'code1' ) );
     817        $this->assertSame( array( 'data1', 'data2' ), $other->get_all_error_data( 'code1' ) );
     818        $this->assertSame( 'message3', $other->get_error_message( 'code2' ) );
     819    }
     820
     821    /**
     822     * @covers ::export_to()
     823     */
     824    public function test_export_to_with_no_errors_should_not_add_to_other_error() {
     825        $other = new WP_Error();
     826
     827        $this->wp_error->export_to( $other );
     828
     829        $this->assertFalse( $other->has_errors() );
     830    }
    721831}
Note: See TracChangeset for help on using the changeset viewer.