Ticket #38777: 38777.2.diff
File 38777.2.diff, 3.2 KB (added by , 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 3de8ae7dae..4801d67648 100644
class WP_Error { 223 223 unset( $this->errors[ $code ] ); 224 224 unset( $this->error_data[ $code ] ); 225 225 } 226 227 /** 228 * Merge the errors in another error object into this one. 229 * 230 * @since 5.5.0 231 * 232 * @param WP_Error $error Error object to merge. 233 */ 234 public function merge( WP_Error $error ) { 235 static::copy_errors( $error, $this ); 236 } 237 238 /** 239 * Export the errors in this object into another one. 240 * 241 * @since 5.5.0 242 * 243 * @param WP_Error $error Error object to export into. 244 */ 245 public function export( WP_Error $error ) { 246 static::copy_errors( $this, $error ); 247 } 248 249 /** 250 * Copy errors from one WP_Error to another. 251 * 252 * @since 5.5.0 253 * 254 * @param WP_Error $from From. 255 * @param WP_Error $to To. 256 */ 257 protected static function copy_errors( WP_Error $from, WP_Error $to ) { 258 foreach ( $from->get_error_codes() as $code ) { 259 foreach ( $from->get_error_messages( $code ) as $error_message ) { 260 $to->add( $code, $error_message ); 261 } 262 263 $data = $from->get_error_data( $code ); 264 265 if ( $data ) { 266 $to->add_data( $data, $code ); 267 } 268 } 269 } 226 270 } -
tests/phpunit/tests/general/wpError.php
diff --git tests/phpunit/tests/general/wpError.php tests/phpunit/tests/general/wpError.php index 3fccc470c8..1b88563c9c 100644
class Tests_WP_Error extends WP_UnitTestCase { 718 718 $this->assertEmpty( $this->wp_error->error_data ); 719 719 } 720 720 721 /** 722 * @covers ::merge() 723 */ 724 public function test_merge_should_copy_other_error_into_instance() { 725 $this->wp_error->add( 'code1', 'message1', 'data1' ); 726 727 $other = new \WP_Error( 'code1', 'message2', 'data2' ); 728 $other->add( 'code2', 'message3' ); 729 $this->wp_error->merge( $other ); 730 731 $this->assertSame( array( 'message1', 'message2' ), $this->wp_error->get_error_messages( 'code1' ) ); 732 $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code1' ) ); 733 $this->assertSame( 'message3', $this->wp_error->get_error_message( 'code2' ) ); 734 } 735 736 /** 737 * @covers ::merge() 738 */ 739 public function test_merge_with_no_errors_should_not_add_to_instance() { 740 $other = new \WP_Error(); 741 742 $this->wp_error->merge( $other ); 743 744 $this->assertFalse( $this->wp_error->has_errors() ); 745 } 746 747 /** 748 * @covers ::export() 749 */ 750 public function test_export_should_copy_instance_into_other_error() { 751 $other = new \WP_Error(); 752 $other->add( 'code1', 'message1', 'data1' ); 753 754 $this->wp_error->add( 'code1', 'message2', 'data2' ); 755 $this->wp_error->add( 'code2', 'message3' ); 756 757 $this->wp_error->export( $other ); 758 759 $this->assertSame( array( 'message1', 'message2' ), $other->get_error_messages( 'code1' ) ); 760 $this->assertSame( 'data2', $other->get_error_data( 'code1' ) ); 761 $this->assertSame( 'message3', $other->get_error_message( 'code2' ) ); 762 } 763 764 /** 765 * @covers ::export() 766 */ 767 public function test_export_with_no_errors_should_not_add_to_other_error() { 768 $other = new \WP_Error(); 769 770 $this->wp_error->export( $other ); 771 772 $this->assertFalse( $other->has_errors() ); 773 } 721 774 }