Ticket #38777: 38777.5.diff
File 38777.5.diff, 7.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 e22357ac28..eb5e3f133a 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 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 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::$additional_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 225 if ( isset( $this->error_data[ $code ] ) ) { 226 $this->additional_data[ $code ][] = $this->error_data[ $code ]; 227 } 228 218 229 $this->error_data[ $code ] = $data; 219 230 } 220 231 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 221 254 /** 222 255 * Removes the specified error. 223 256 * … … class WP_Error { 231 264 public function remove( $code ) { 232 265 unset( $this->errors[ $code ] ); 233 266 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 } 234 310 } 235 311 } -
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 }