Ticket #38777: 38777.3.diff
File 38777.3.diff, 8.9 KB (added by , 4 years ago) |
---|
-
package-lock.json
diff --git package-lock.json package-lock.json index 1060098a64..e8f757225a 100644
7506 7506 }, 7507 7507 "create-hash": { 7508 7508 "version": "1.2.0", 7509 "resolved": "http ://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz",7509 "resolved": "https://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz", 7510 7510 "integrity": "sha512-z00bCGNHDG8mHAkP7CtT1qVu+bFQUPjYq/4Iv3C3kWjTFV10zIjfSoeqXo9Asws8gwSHDGj/hl2u4OGIjapeCg==", 7511 7511 "dev": true, 7512 7512 "requires": { … … 7519 7519 }, 7520 7520 "create-hmac": { 7521 7521 "version": "1.1.7", 7522 "resolved": "http ://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz",7522 "resolved": "https://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz", 7523 7523 "integrity": "sha512-MJG9liiZ+ogc4TzUwuvbER1JRdgvUFSB5+VR/g5h82fGaIRWMWddtKBHi7/sVhfjQZ6SehlyhvQYrcYkaUIpLg==", 7524 7524 "dev": true, 7525 7525 "requires": { … … 7585 7585 }, 7586 7586 "css-color-names": { 7587 7587 "version": "0.0.4", 7588 "resolved": "http ://registry.npmjs.org/css-color-names/-/css-color-names-0.0.4.tgz",7588 "resolved": "https://registry.npmjs.org/css-color-names/-/css-color-names-0.0.4.tgz", 7589 7589 "integrity": "sha1-gIrcLnnPhHOAabZGyyDsJ762KeA=", 7590 7590 "dev": true 7591 7591 }, … … 8570 8570 }, 8571 8571 "diffie-hellman": { 8572 8572 "version": "5.0.3", 8573 "resolved": "http ://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz",8573 "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", 8574 8574 "integrity": "sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==", 8575 8575 "dev": true, 8576 8576 "requires": { … … 22124 22124 }, 22125 22125 "sha.js": { 22126 22126 "version": "2.4.11", 22127 "resolved": "http ://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz",22127 "resolved": "https://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", 22128 22128 "integrity": "sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==", 22129 22129 "dev": true, 22130 22130 "requires": { -
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 }