Ticket #38777: 38777.4.diff
File 38777.4.diff, 7.0 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 e22357ac28..4229abe32d 100644
class WP_Error { 27 27 public $errors = array(); 28 28 29 29 /** 30 * Stores the list ofdata for error codes.30 * Stores the most-recently added data for error codes. 31 31 * 32 32 * @since 2.1.0 33 33 * @var array 34 34 */ 35 35 public $error_data = array(); 36 36 37 /** 38 * Stores all data added for error codes, oldest-to-newest by code. 39 * 40 * @since 5.6.0 41 * @var array[] 42 */ 43 protected $all_error_data = array(); 44 37 45 /** 38 46 * Initialize the error. 39 47 * … … class WP_Error { 140 148 } 141 149 142 150 /** 143 * Retrieve error data for error code.151 * Retrieve the most-recently added error data for error code. 144 152 * 145 153 * @since 2.1.0 146 154 * … … class WP_Error { 203 211 /** 204 212 * Add data for error code. 205 213 * 206 * The error code can only contain one error data.207 *208 214 * @since 2.1.0 215 * @since 5.6.0 Error codes can now contain more than one error data. {@see WP_Error::$all_error_data}. 209 216 * 210 217 * @param mixed $data Error data. 211 218 * @param string|int $code Error code. … … class WP_Error { 215 222 $code = $this->get_error_code(); 216 223 } 217 224 218 $this->error_data[ $code ] = $data; 225 $this->error_data[ $code ] = $data; 226 $this->all_error_data[ $code ][] = $data; 227 } 228 229 /** 230 * Retrieve all error data for an error code in the order in which the data was added. 231 * 232 * @since 5.6.0 233 * 234 * @param string|int $code Error code. 235 * @return mixed[] Error data, if it exists. 236 */ 237 public function get_all_error_data( $code ) { 238 return isset( $this->all_error_data[ $code ] ) ? $this->all_error_data[ $code ] : array(); 219 239 } 220 240 221 241 /** … … class WP_Error { 231 251 public function remove( $code ) { 232 252 unset( $this->errors[ $code ] ); 233 253 unset( $this->error_data[ $code ] ); 254 unset( $this->all_error_data[ $code ] ); 255 } 256 257 /** 258 * Merge the errors in another error object into this one. 259 * 260 * @since 5.6.0 261 * 262 * @param WP_Error $error Error object to merge. 263 */ 264 public function merge_from( WP_Error $error ) { 265 static::copy_errors( $error, $this ); 266 } 267 268 /** 269 * Export the errors in this object into another one. 270 * 271 * @since 5.6.0 272 * 273 * @param WP_Error $error Error object to export into. 274 */ 275 public function export_to( WP_Error $error ) { 276 static::copy_errors( $this, $error ); 277 } 278 279 /** 280 * Copy errors from one WP_Error to another. 281 * 282 * @since 5.6.0 283 * 284 * @param WP_Error $from From. 285 * @param WP_Error $to To. 286 */ 287 protected static function copy_errors( WP_Error $from, WP_Error $to ) { 288 foreach ( $from->get_error_codes() as $code ) { 289 foreach ( $from->get_error_messages( $code ) as $error_message ) { 290 $to->add( $code, $error_message ); 291 } 292 293 foreach ( $from->get_all_error_data( $code ) as $data ) { 294 $to->add_data( $data, $code ); 295 } 296 } 234 297 } 235 298 } -
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 { 375 375 $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code' ) ); 376 376 } 377 377 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 378 417 /** 379 418 * @covers ::has_errors 380 419 */ … … class Tests_WP_Error extends WP_UnitTestCase { 711 750 * @covers ::remove 712 751 */ 713 752 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' ); 715 755 716 756 $this->wp_error->remove( 'code' ); 717 757 718 758 $this->assertEmpty( $this->wp_error->error_data ); 759 $this->assertEmpty( $this->wp_error->get_all_error_data( 'code' ) ); 719 760 } 720 761 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 } 721 817 }