- Timestamp:
- 11/29/2017 02:18:25 AM (7 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/general/wpError.php
r42254 r42255 4 4 * 5 5 * @group general 6 * @group errors 6 7 */ 7 class Tests_General_Errors extends WP_UnitTestCase { 8 9 function test_create_error() { 10 $error = new WP_Error( 'foo', 'message', 'data' ); 11 12 $this->assertWPError( $error ); 13 $this->assertEquals( 'foo', $error->get_error_code() ); 14 $this->assertEquals( 'message', $error->get_error_message() ); 15 $this->assertEquals( 'data', $error->get_error_data() ); 16 } 17 18 function test_add_error() { 19 $error = new WP_Error(); 20 $error->add( 'foo', 'message', 'data' ); 21 22 $this->assertWPError( $error ); 23 $this->assertEquals( 'foo', $error->get_error_code() ); 24 $this->assertEquals( 'message', $error->get_error_message() ); 25 $this->assertEquals( 'data', $error->get_error_data() ); 26 } 27 28 function test_multiple_errors() { 29 $error = new WP_Error(); 30 $error->add( 'foo', 'foo message', 'foo data' ); 31 $error->add( 'bar', 'bar message', 'bar data' ); 32 33 $this->assertWPError( $error ); 34 $this->assertEquals( 'foo', $error->get_error_code() ); 35 $this->assertEquals( 'foo message', $error->get_error_message() ); 36 $this->assertEquals( 'foo data', $error->get_error_data() ); 37 38 $this->assertEquals( array( 'foo', 'bar' ), $error->get_error_codes() ); 39 $this->assertEquals( array( 'foo message', 'bar message' ), $error->get_error_messages() ); 40 $this->assertEquals( 'foo data', $error->get_error_data( 'foo' ) ); 41 $this->assertEquals( 'bar data', $error->get_error_data( 'bar' ) ); 42 } 43 44 /** 45 * @ticket 28092 46 */ 47 function test_remove_error() { 48 $error = new WP_Error(); 49 $error->add( 'foo', 'This is the first error message', 'some error data' ); 50 $error->add( 'foo', 'This is the second error message' ); 51 $error->add( 'bar', 'This is another error' ); 52 53 $error->remove( 'foo' ); 54 55 // Check the error has been removed. 56 $this->assertEmpty( $error->get_error_data( 'foo' ) ); 57 $this->assertEmpty( $error->get_error_messages( 'foo' ) ); 58 59 // The 'bar' error should now be the 'first' error retrieved. 60 $this->assertEquals( 'bar', $error->get_error_code() ); 61 $this->assertEmpty( $error->get_error_data() ); 62 } 8 class Tests_WP_Error extends WP_UnitTestCase { 9 10 /** 11 * WP_Error fixture. 12 * 13 * @var WP_Error 14 */ 15 public $WP_Error; 16 17 /** 18 * Set up. 19 */ 20 public function setUp() { 21 parent::setUp(); 22 23 $this->WP_Error = new WP_Error(); 24 } 25 26 /** 27 * @covers WP_Error 28 */ 29 public function test_WP_Error_should_be_of_type_WP_Error() { 30 $this->assertWPError( $this->WP_Error ); 31 } 32 33 /** 34 * @covers WP_Error 35 */ 36 public function test_WP_Error_with_default_empty_parameters_should_add_no_errors() { 37 $this->assertEmpty( $this->WP_Error->errors ); 38 } 39 40 /** 41 * @covers WP_Error 42 */ 43 public function test_WP_Error_with_empty_code_should_add_no_code() { 44 $this->assertSame( '', $this->WP_Error->get_error_code() ); 45 } 46 47 /** 48 * @covers WP_Error 49 */ 50 public function test_WP_Error_with_empty_code_should_add_no_message() { 51 $this->assertSame( '', $this->WP_Error->get_error_message() ); 52 } 53 54 /** 55 * @covers WP_Error 56 */ 57 public function test_WP_Error_with_empty_code_should_add_no_error_data() { 58 $this->assertEmpty( $this->WP_Error->error_data ); 59 } 60 61 /** 62 * @covers WP_Error 63 */ 64 public function test_WP_Error_with_code_and_empty_message_should_add_error_with_that_code() { 65 $wp_error = new WP_Error( 'code' ); 66 67 $this->assertSame( 'code', $wp_error->get_error_code() ); 68 } 69 70 /** 71 * @covers WP_Error 72 */ 73 public function test_WP_Error_with_code_and_empty_message_should_add_error_with_that_code_and_empty_message() { 74 $wp_error = new WP_Error( 'code' ); 75 76 $this->assertSame( '', $wp_error->get_error_message( 'code' ) ); 77 } 78 79 /** 80 * @covers WP_Error 81 */ 82 public function test_WP_Error_with_code_and_empty_message_and_empty_data_should_add_error_but_not_associated_data() { 83 $wp_error = new WP_Error( 'code' ); 84 85 $this->assertSame( null, $wp_error->get_error_data( 'code' ) ); 86 } 87 88 /** 89 * @covers WP_Error 90 */ 91 public function test_WP_Error_with_code_and_empty_message_and_non_empty_data_should_add_error_with_empty_message_and_that_stored_data() { 92 $wp_error = new WP_Error( 'code', '', 'data' ); 93 94 $this->assertSame( 'data', $wp_error->get_error_data( 'code' ) ); 95 } 96 97 /** 98 * @covers WP_Error 99 */ 100 public function test_WP_Error_with_code_and_message_should_add_error_with_that_code() { 101 $wp_error = new WP_Error( 'code', 'message' ); 102 103 $this->assertSame( 'code', $wp_error->get_error_code() ); 104 } 105 106 /** 107 * @covers WP_Error 108 */ 109 public function test_WP_Error_with_code_and_message_should_add_error_with_that_message() { 110 $wp_error = new WP_Error( 'code', 'message' ); 111 112 $this->assertSame( 'message', $wp_error->get_error_message( 'code' ) ); 113 } 114 115 /** 116 * @covers WP_Error 117 */ 118 public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_code() { 119 $wp_error = new WP_Error( 'code', 'message', 'data' ); 120 121 $this->assertSame( 'code', $wp_error->get_error_code() ); 122 } 123 124 /** 125 * @covers WP_Error 126 */ 127 public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_message() { 128 $wp_error = new WP_Error( 'code', 'message', 'data' ); 129 130 $this->assertSame( 'message', $wp_error->get_error_message( 'code' ) ); 131 } 132 133 /** 134 * @covers WP_Error 135 */ 136 public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_data() { 137 $wp_error = new WP_Error( 'code', 'message', 'data' ); 138 139 $this->assertSame( 'data', $wp_error->get_error_data( 'code' ) ); 140 } 141 142 /** 143 * @covers WP_Error::get_error_codes() 144 */ 145 public function test_get_error_codes_with_no_errors_should_return_empty_array() { 146 $this->assertEmpty( $this->WP_Error->get_error_codes() ); 147 } 148 149 /** 150 * @covers WP_Error::get_error_codes() 151 */ 152 public function test_get_error_codes_with_one_error_should_return_an_array_with_only_that_code() { 153 $this->WP_Error->add( 'code', 'message' ); 154 155 $this->assertEqualSets( array( 'code' ), $this->WP_Error->get_error_codes() ); 156 } 157 158 /** 159 * @covers WP_Error::get_error_codes() 160 */ 161 public function test_get_error_codes_with_nultiple_errors_should_return_an_array_of_those_codes() { 162 $this->WP_Error->add( 'code', 'message' ); 163 $this->WP_Error->add( 'code2', 'message2' ); 164 165 $expected = array( 'code', 'code2' ); 166 167 $this->assertEqualSets( $expected, $this->WP_Error->get_error_codes() ); 168 } 169 170 /** 171 * @covers WP_Error:get_error_code() 172 */ 173 public function test_get_error_code_with_no_errors_should_return_an_empty_string() { 174 $this->assertSame( '', $this->WP_Error->get_error_code() ); 175 } 176 177 /** 178 * @covers WP_Error:get_error_code() 179 */ 180 public function test_get_error_code_with_one_error_should_return_that_error_code() { 181 $this->WP_Error->add( 'code', 'message' ); 182 183 $this->assertSame( 'code', $this->WP_Error->get_error_code() ); 184 } 185 186 /** 187 * @covers WP_Error:get_error_code() 188 */ 189 public function test_get_error_code_with_multiple_errors_should_return_only_the_first_error_code() { 190 $this->WP_Error->add( 'code', 'message' ); 191 $this->WP_Error->add( 'code2', 'message2' ); 192 193 $this->assertSame( 'code', $this->WP_Error->get_error_code() ); 194 } 195 196 /** 197 * @covers WP_Error::get_error_messages() 198 */ 199 public function test_get_error_messages_with_empty_code_and_no_errors_should_return_an_empty_array() { 200 $this->assertEmpty( $this->WP_Error->get_error_messages() ); 201 } 202 203 /** 204 * @covers WP_Error::get_error_messages() 205 */ 206 public function test_get_error_messages_with_empty_code_one_error_should_return_an_array_with_that_message() { 207 $this->WP_Error->add( 'code', 'message' ); 208 209 $this->assertEqualSets( array( 'message' ), $this->WP_Error->get_error_messages() ); 210 } 211 212 /** 213 * @covers WP_Error::get_error_messages() 214 */ 215 public function test_get_error_messages_with_empty_code_multiple_errors_should_return_an_array_of_messages() { 216 $this->WP_Error->add( 'code', 'message' ); 217 $this->WP_Error->add( 'code2', 'message2' ); 218 219 $this->assertEqualSets( array( 'message', 'message2' ), $this->WP_Error->get_error_messages() ); 220 } 221 222 /** 223 * @covers WP_Error::get_error_messages() 224 */ 225 public function test_get_error_messages_with_an_invalid_code_should_return_an_empty_array() { 226 $this->assertEmpty( $this->WP_Error->get_error_messages( 'code' ) ); 227 } 228 229 /** 230 * @covers WP_Error::get_error_messages() 231 */ 232 public function test_get_error_messages_with_one_error_should_return_an_array_with_that_message() { 233 $this->WP_Error->add( 'code', 'message' ); 234 235 $this->assertEqualSets( array( 'message' ), $this->WP_Error->get_error_messages( 'code' ) ); 236 } 237 238 /** 239 * @covers WP_Error::get_error_messages() 240 */ 241 public function test_get_error_messages_with_multiple_errors_same_code_should_return_an_array_with_all_messages() { 242 $this->WP_Error->add( 'code', 'message' ); 243 $this->WP_Error->add( 'code', 'message2' ); 244 245 $this->assertequalSets( array( 'message', 'message2' ), $this->WP_Error->get_error_messages( 'code' ) ); 246 } 247 248 /** 249 * @covers WP_Error::get_error_message() 250 */ 251 public function test_get_error_message_with_empty_code_and_no_errors_should_return_an_empty_string() { 252 $this->assertSame( '', $this->WP_Error->get_error_message() ); 253 } 254 255 /** 256 * @covers WP_Error::get_error_message() 257 */ 258 public function test_get_error_message_with_empty_code_and_one_error_should_return_that_message() { 259 $this->WP_Error->add( 'code', 'message' ); 260 261 $this->assertSame( 'message', $this->WP_Error->get_error_message() ); 262 } 263 264 /** 265 * @covers WP_Error::get_error_message() 266 */ 267 public function test_get_error_message_with_empty_code_and_multiple_errors_should_return_the_first_message() { 268 $this->WP_Error->add( 'code', 'message' ); 269 $this->WP_Error->add( 'code2', 'message2' ); 270 271 $this->assertSame( 'message', $this->WP_Error->get_error_message() ); 272 } 273 274 /** 275 * @covers WP_Error::get_error_message() 276 */ 277 public function test_get_error_message_with_empty_code_and_multiple_errors_multiple_codes_should_return_the_first_message() { 278 $this->WP_Error->add( 'code', 'message' ); 279 $this->WP_Error->add( 'code2', 'message2' ); 280 $this->WP_Error->add( 'code', 'message2' ); 281 282 $this->assertSame( 'message', $this->WP_Error->get_error_message() ); 283 } 284 285 /** 286 * @covers WP_Error::get_error_message() 287 */ 288 public function test_get_error_message_with_invalid_code_and_no_errors_should_return_empty_string() { 289 $this->assertSame( '', $this->WP_Error->get_error_message( 'invalid' ) ); 290 } 291 292 /** 293 * @covers WP_Error::get_error_message() 294 */ 295 public function test_get_error_message_with_invalid_code_and_one_error_should_return_an_empty_string() { 296 $this->WP_Error->add( 'code', 'message' ); 297 298 $this->assertSame( '', $this->WP_Error->get_error_message( 'invalid' ) ); 299 } 300 301 /** 302 * @covers WP_Error::get_error_message() 303 */ 304 public function test_get_error_message_with_invalid_code_and_multiple_errors_should_return_an_empty_string() { 305 $this->WP_Error->add( 'code', 'message' ); 306 $this->WP_Error->add( 'code2', 'message2' ); 307 308 $this->assertSame( '', $this->WP_Error->get_error_message( 'invalid' ) ); 309 } 310 311 /** 312 * @covers WP_Error::get_error_data() 313 */ 314 public function test_get_error_data_with_empty_code_and_no_errors_should_evaluate_as_null() { 315 $this->assertSame( null, $this->WP_Error->get_error_data() ); 316 } 317 318 /** 319 * @covers WP_Error::get_error_data() 320 */ 321 public function test_get_error_data_with_empty_code_one_error_no_data_should_evaluate_as_null() { 322 $this->WP_Error->add( 'code', 'message' ); 323 324 $this->assertSame( null, $this->WP_Error->get_error_data() ); 325 } 326 327 /** 328 * @covers WP_Error::get_error_data() 329 */ 330 public function test_get_error_data_with_empty_code_multiple_errors_no_data_should_evaluate_as_null() { 331 $this->WP_Error->add( 'code', 'message' ); 332 $this->WP_Error->add( 'code2', 'message2' ); 333 334 $this->assertSame( null, $this->WP_Error->get_error_data() ); 335 } 336 337 /** 338 * @covers WP_Error::get_error_data() 339 */ 340 public function test_get_error_data_with_empty_code_and_one_error_with_data_should_return_that_data() { 341 $expected = array( 'data-key' => 'data-value' ); 342 $this->WP_Error->add( 'code', 'message', $expected ); 343 344 $this->assertEqualSetsWithIndex( $expected, $this->WP_Error->get_error_data() ); 345 } 346 347 /** 348 * @covers WP_Error::get_error_data() 349 */ 350 public function test_get_error_data_with_empty_code_and_multiple_errors_different_codes_should_return_the_last_data_of_the_first_code() { 351 $expected = array( 'data-key' => 'data-value' ); 352 $this->WP_Error->add( 'code', 'message', $expected ); 353 $this->WP_Error->add( 'code2', 'message2', 'data2' ); 354 355 $this->assertEqualSetsWithIndex( $expected, $this->WP_Error->get_error_data() ); 356 } 357 358 /** 359 * @covers WP_Error::get_error_data() 360 */ 361 public function test_get_error_data_with_empty_code_and_multiple_errors_same_code_should_return_the_last_data_of_the_first_code() { 362 $this->WP_Error->add( 'code', 'message', 'data' ); 363 $this->WP_Error->add( 'code', 'message2', 'data2' ); 364 $this->WP_Error->add( 'code2', 'message2', 'data3' ); 365 366 $this->assertSame( 'data2', $this->WP_Error->get_error_data() ); 367 } 368 369 /** 370 * @covers WP_Error::get_error_data() 371 */ 372 public function test_get_error_data_with_code_and_no_errors_should_evaluate_as_null() { 373 $this->assertSame( null, $this->WP_Error->get_error_data( 'code' ) ); 374 } 375 376 /** 377 * @covers WP_Error::get_error_data() 378 */ 379 public function test_get_error_data_with_code_and_one_error_with_no_data_should_evaluate_as_null() { 380 $this->WP_Error->add( 'code', 'message' ); 381 382 $this->assertSame( null, $this->WP_Error->get_error_data( 'code' ) ); 383 } 384 385 /** 386 * @covers WP_Error::get_error_data() 387 */ 388 public function test_get_error_data_with_code_and_one_error_with_data_should_return_that_data() { 389 $expected = array( 'data-key' => 'data-value' ); 390 $this->WP_Error->add( 'code', 'message', $expected ); 391 392 $this->assertEqualSetsWithIndex( $expected, $this->WP_Error->get_error_data( 'code' ) ); 393 } 394 395 /** 396 * @covers WP_Error::get_error_data() 397 */ 398 public function test_get_error_data_with_code_and_multiple_errors_different_codes_should_return_the_last_stored_data_of_the_code() { 399 $expected = array( 'data3' ); 400 $this->WP_Error->add( 'code', 'message', 'data' ); 401 $this->WP_Error->add( 'code2', 'message2', 'data2' ); 402 $this->WP_Error->add( 'code', 'message3', $expected ); 403 404 $this->assertEqualSetsWithIndex( $expected, $this->WP_Error->get_error_data( 'code' ) ); 405 } 406 407 /** 408 * @covers WP_Error::get_error_data() 409 */ 410 public function test_get_error_data_with_code_and_multiple_errors_same_code_should_return_the_last_stored_data() { 411 $this->WP_Error->add( 'code', 'message', 'data' ); 412 $this->WP_Error->add( 'code', 'message2', 'data2' ); 413 $this->WP_Error->add( 'code2', 'message3', 'data3' ); 414 415 $this->assertSame( 'data2', $this->WP_Error->get_error_data( 'code' ) ); 416 } 417 418 /** 419 * @covers WP_Error::add() 420 */ 421 public function test_add_with_empty_code_empty_message_empty_data_should_add_empty_key_to_errors_array() { 422 $this->WP_Error->add( '', '', 'data' ); 423 424 $this->assertArrayHasKey( '', $this->WP_Error->errors ); 425 } 426 427 /** 428 * @covers WP_Error::add() 429 */ 430 public function test_add_with_empty_code_empty_message_empty_data_should_add_empty_message_to_errors_array_under_empty_key() { 431 $this->WP_Error->add( '', '', 'data' ); 432 433 $this->assertEqualSetsWithIndex( array( '' => array( '' ) ), $this->WP_Error->errors ); 434 } 435 436 /** 437 * @covers WP_Error::add() 438 */ 439 public function test_add_with_empty_code_empty_message_empty_data_should_not_alter_data() { 440 $this->WP_Error->add( '', '', '' ); 441 442 $this->assertEmpty( $this->WP_Error->error_data ); 443 } 444 445 /** 446 * @covers WP_Error::add() 447 */ 448 public function test_add_with_empty_code_empty_message_non_empty_data_should_store_data_under_an_empty_code_key() { 449 $this->WP_Error->add( '', '', 'data' ); 450 451 $this->assertEqualSetsWithIndex( array( '' => 'data' ), $this->WP_Error->error_data ); 452 } 453 454 /** 455 * @covers WP_Error::add() 456 */ 457 public function test_add_with_code_empty_message_empty_data_should_add_error_with_code() { 458 $this->WP_Error->add( 'code', '' ); 459 460 $this->assertSame( 'code', $this->WP_Error->get_error_code() ); 461 } 462 463 /** 464 * @covers WP_Error::add() 465 */ 466 public function test_add_with_code_empty_message_empty_data_should_add_error_with_empty_message() { 467 $this->WP_Error->add( 'code', '' ); 468 469 $this->assertSame( '', $this->WP_Error->get_error_message( 'code' ) ); 470 } 471 472 /** 473 * @covers WP_Error::add() 474 */ 475 public function test_add_with_code_empty_message_empty_data_should_not_add_error_data() { 476 $this->WP_Error->add( 'code', '' ); 477 478 $this->assertSame( null, $this->WP_Error->get_error_data( 'code' ) ); 479 } 480 481 /** 482 * @covers WP_Error::add() 483 */ 484 public function test_add_with_code_and_message_and_empty_data_should_should_add_error_with_that_message() { 485 $this->WP_Error->add( 'code', 'message' ); 486 487 $this->assertSame( 'message', $this->WP_Error->get_error_message( 'code' ) ); 488 } 489 490 /** 491 * @covers WP_Error::add() 492 */ 493 public function test_add_with_code_and_message_and_empty_data_should_not_alter_stored_data() { 494 $this->WP_Error->add( 'code', 'message' ); 495 496 $this->assertSame( null, $this->WP_Error->get_error_data( 'code' ) ); 497 } 498 499 /** 500 * @covers WP_Error::add() 501 */ 502 public function test_add_with_code_and_empty_message_and_data_should_add_error_with_that_code() { 503 $this->WP_Error->add( 'code', '', 'data' ); 504 505 $this->assertSame( 'code', $this->WP_Error->get_error_code() ); 506 } 507 508 /** 509 * @covers WP_Error::add() 510 */ 511 public function test_add_with_code_and_empty_message_and_data_should_store_that_data() { 512 $this->WP_Error->add( 'code', '', 'data' ); 513 514 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 515 } 516 517 /** 518 * @covers WP_Error::add() 519 */ 520 public function test_add_with_code_and_message_and_data_should_add_an_error_with_that_code() { 521 $this->WP_Error->add( 'code', 'message', 'data' ); 522 523 $this->assertSame( 'code', $this->WP_Error->get_error_code() ); 524 } 525 526 /** 527 * @covers WP_Error::add() 528 */ 529 public function test_add_with_code_and_message_and_data_should_add_an_error_with_that_message() { 530 $this->WP_Error->add( 'code', 'message', 'data' ); 531 532 $this->assertSame( 'message', $this->WP_Error->get_error_message( 'code' ) ); 533 } 534 535 /** 536 * @covers WP_Error::add() 537 */ 538 public function test_add_with_code_and_message_and_data_should_store_that_data() { 539 $this->WP_Error->add( 'code', 'message', 'data' ); 540 541 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 542 } 543 544 /** 545 * @covers WP_Error::add() 546 */ 547 public function test_add_multiple_times_with_the_same_code_should_add_additional_messages_for_that_code() { 548 $this->WP_Error->add( 'code', 'message' ); 549 $this->WP_Error->add( 'code', 'message2' ); 550 551 $expected = array( 'message', 'message2' ); 552 553 $this->assertEqualSets( $expected, $this->WP_Error->get_error_messages( 'code' ) ); 554 } 555 556 /** 557 * @covers WP_Error::add() 558 */ 559 public function test_add_multiple_times_with_the_same_code_and_different_data_should_store_only_the_last_added_data() { 560 $this->WP_Error->add( 'code', 'message', 'data-bar' ); 561 $this->WP_Error->add( 'code', 'message2', 'data-baz' ); 562 563 $this->assertSame( 'data-baz', $this->WP_Error->get_error_data( 'code' ) ); 564 } 565 566 /** 567 * @covers WP_Error::add_data() 568 */ 569 public function test_add_data_with_empty_data_empty_code_should_create_orphaned_data_with_no_error() { 570 $this->WP_Error->add_data( '' ); 571 572 $this->assertEmpty( $this->WP_Error->errors ); 573 } 574 575 /** 576 * @covers WP_Error::add_data() 577 */ 578 public function test_add_data_with_empty_data_empty_code_no_errors_should_create_data_under_an_empty_code_key() { 579 $this->WP_Error->add_data( '' ); 580 581 $this->assertEqualSets( array( '' => '' ), $this->WP_Error->error_data ); 582 } 583 584 /** 585 * @covers WP_Error::add_data() 586 */ 587 public function test_add_data_with_data_empty_code_and_one_error_should_store_the_data_under_that_code() { 588 $this->WP_Error->add( 'code', 'message' ); 589 $this->WP_Error->add_data( 'data' ); 590 591 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 592 } 593 594 /** 595 * @covers WP_Error::add_data() 596 */ 597 public function test_add_data_with_data_empty_code_and_multiple_errors_with_different_codes_should_store_it_under_the_first_code() { 598 $this->WP_Error->add( 'code', 'message' ); 599 $this->WP_Error->add( 'code2', 'message2' ); 600 601 $this->WP_Error->add_data( 'data' ); 602 603 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 604 } 605 606 /** 607 * @covers WP_Error::add_data() 608 */ 609 public function test_add_data_with_data_empty_code_and_multiple_errors_with_same_code_should_store_it_under_the_first_code() { 610 $this->WP_Error->add( 'code', 'message' ); 611 $this->WP_Error->add( 'code2', 'message2' ); 612 $this->WP_Error->add( 'code', 'message3' ); 613 614 $this->WP_Error->add_data( 'data' ); 615 616 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 617 } 618 619 /** 620 * @covers WP_Error::add_data() 621 */ 622 public function test_add_data_with_data_and_code_and_no_errors_should_create_orphaned_data_with_no_error() { 623 $this->WP_Error->add_data( 'data', 'code' ); 624 625 $this->assertEmpty( $this->WP_Error->errors ); 626 } 627 628 /** 629 * @covers WP_Error::add_data() 630 */ 631 public function test_add_data_with_data_and_code_no_errors_should_create_data_under_that_code_key() { 632 $this->WP_Error->add_data( 'data', 'code' ); 633 634 $this->assertEqualSets( array( 'code' => 'data' ), $this->WP_Error->error_data ); 635 } 636 637 /** 638 * @covers WP_Error::add_data() 639 */ 640 public function test_add_data_with_data_and_code_one_error_different_code_should_create_orphaned_data_with_no_error() { 641 $this->WP_Error->add( 'code', 'message' ); 642 643 $this->WP_Error->add_data( 'data', 'code2' ); 644 645 $this->assertEqualSetsWithIndex( array( 'code' => array( 'message' ) ), $this->WP_Error->errors ); 646 } 647 648 /** 649 * @covers WP_Error::add_data() 650 */ 651 public function test_add_data_with_data_and_code_one_error_different_code_should_create_data_under_that_code_key() { 652 $this->WP_Error->add( 'code', 'message' ); 653 654 $this->WP_Error->add_data( 'data', 'code2' ); 655 656 $this->assertEqualSetsWithIndex( array( 'code2' => 'data' ), $this->WP_Error->error_data ); 657 } 658 659 /** 660 * @covers WP_Error::add_data() 661 */ 662 public function test_add_data_with_data_and_code_should_add_data() { 663 $this->WP_Error->add( 'code', 'message' ); 664 665 $this->WP_Error->add_data( 'data', 'code' ); 666 667 $this->assertSame( 'data', $this->WP_Error->get_error_data( 'code' ) ); 668 } 669 670 /** 671 * @covers WP_Error::remove() 672 */ 673 public function test_remove_with_no_errors_should_affect_nothing() { 674 $before = $this->WP_Error->errors; 675 676 $this->WP_Error->remove( 'code' ); 677 678 $after = $this->WP_Error->errors; 679 680 $this->assertEqualSetsWithIndex( $before, $after ); 681 } 682 683 /** 684 * @covers WP_Error::remove() 685 */ 686 public function test_remove_empty_code_no_errors_should_affect_nothing() { 687 $before = $this->WP_Error->errors; 688 689 $this->WP_Error->remove( '' ); 690 691 $after = $this->WP_Error->errors; 692 693 $this->assertEqualSetsWithIndex( $before, $after ); 694 } 695 696 /** 697 * @covers WP_Error::remove() 698 */ 699 public function test_remove_empty_code_and_one_error_with_empty_string_code_should_remove_error() { 700 $before = $this->WP_Error->errors; 701 702 $this->WP_Error->add( '', 'message' ); 703 704 $this->WP_Error->remove( '' ); 705 706 $after = $this->WP_Error->errors; 707 708 $this->assertEqualSetsWithIndex( $before, $after ); 709 } 710 711 /** 712 * @covers WP_Error::remove() 713 */ 714 public function test_remove_empty_code_and_one_error_with_empty_string_code_should_remove_error_data() { 715 $this->WP_Error->add( '', 'message', 'data' ); 716 717 $this->WP_Error->remove( '' ); 718 719 $after = $this->WP_Error->error_data; 720 721 $this->assertEmpty( $this->WP_Error->error_data ); 722 } 723 724 /** 725 * @covers WP_Error::remove() 726 */ 727 public function test_remove_should_remove_the_error_with_the_given_code() { 728 $this->WP_Error->add( 'code', 'message' ); 729 730 $this->WP_Error->remove( 'code' ); 731 732 $this->assertEmpty( $this->WP_Error->errors ); 733 } 734 735 /** 736 * @covers WP_Error::remove() 737 */ 738 public function test_remove_should_remove_the_error_data_associated_with_the_given_code() { 739 $this->WP_Error->add( 'code', 'message', 'data' ); 740 741 $this->WP_Error->remove( 'code' ); 742 743 $this->assertEmpty( $this->WP_Error->error_data ); 744 } 745 63 746 }
Note: See TracChangeset
for help on using the changeset viewer.