|
19 | 19 | use Zend\InputFilter\Exception\RuntimeException;
|
20 | 20 | use Zend\InputFilter\Input;
|
21 | 21 | use Zend\InputFilter\InputFilter;
|
| 22 | +use Zend\Validator\EmailAddress; |
| 23 | +use Zend\Validator\NotEmpty; |
22 | 24 |
|
23 | 25 | /**
|
24 | 26 | * @covers Zend\InputFilter\CollectionInputFilter
|
@@ -501,4 +503,106 @@ public function testSettingDataWithNonArrayNonTraversableRaisesException($data)
|
501 | 503 | $this->setExpectedException(InvalidArgumentException::class, 'invalid collection');
|
502 | 504 | $collectionInputFilter->setData($data);
|
503 | 505 | }
|
| 506 | + |
| 507 | + public function testCollectionValidationDoesNotReuseMessagesBetweenInputs() |
| 508 | + { |
| 509 | + $inputFilter = new InputFilter(); |
| 510 | + $inputFilter->add([ |
| 511 | + 'name' => 'email', |
| 512 | + 'required' => true, |
| 513 | + 'validators' => [ |
| 514 | + ['name' => EmailAddress::class], |
| 515 | + ['name' => NotEmpty::class], |
| 516 | + ], |
| 517 | + ]); |
| 518 | + $inputFilter->add([ |
| 519 | + 'name' => 'name', |
| 520 | + 'required' => true, |
| 521 | + 'validators' => [ |
| 522 | + ['name' => NotEmpty::class], |
| 523 | + ], |
| 524 | + ]); |
| 525 | + |
| 526 | + $collectionInputFilter = $this->inputFilter; |
| 527 | + $collectionInputFilter->setInputFilter($inputFilter); |
| 528 | + |
| 529 | + $collectionInputFilter->setData([ |
| 530 | + [ |
| 531 | + 'name' => 'Tom', |
| 532 | + ], |
| 533 | + [ |
| 534 | + 'email' => 'tom@tom', |
| 535 | + 'name' => 'Tom', |
| 536 | + ], |
| 537 | + ]); |
| 538 | + |
| 539 | + $isValid = $collectionInputFilter->isValid(); |
| 540 | + $messages = $collectionInputFilter->getMessages(); |
| 541 | + |
| 542 | + // @codingStandardsIgnoreStart |
| 543 | + $this->assertFalse($isValid); |
| 544 | + $this->assertCount(2, $messages); |
| 545 | + |
| 546 | + $this->assertArrayHasKey('email', $messages[0]); |
| 547 | + $this->assertCount(1, $messages[0]['email']); |
| 548 | + $this->assertContains('Value is required and can\'t be empty', $messages[0]['email']); |
| 549 | + |
| 550 | + $this->assertArrayHasKey('email', $messages[1]); |
| 551 | + $this->assertCount(3, $messages[1]['email']); |
| 552 | + $this->assertNotContains('Value is required and can\'t be empty', $messages[1]['email']); |
| 553 | + $this->assertContains('\'tom\' is not a valid hostname for the email address', $messages[1]['email']); |
| 554 | + $this->assertContains('The input does not match the expected structure for a DNS hostname', $messages[1]['email']); |
| 555 | + $this->assertContains('The input appears to be a local network name but local network names are not allowed', $messages[1]['email']); |
| 556 | + // @codingStandardsIgnoreEnd |
| 557 | + } |
| 558 | + |
| 559 | + public function testCollectionValidationUsesCustomInputErrorMessages() |
| 560 | + { |
| 561 | + $inputFilter = new InputFilter(); |
| 562 | + $inputFilter->add([ |
| 563 | + 'name' => 'email', |
| 564 | + 'required' => true, |
| 565 | + 'validators' => [ |
| 566 | + ['name' => EmailAddress::class], |
| 567 | + ['name' => NotEmpty::class], |
| 568 | + ], |
| 569 | + 'error_message' => 'CUSTOM ERROR MESSAGE', |
| 570 | + ]); |
| 571 | + $inputFilter->add([ |
| 572 | + 'name' => 'name', |
| 573 | + 'required' => true, |
| 574 | + 'validators' => [ |
| 575 | + ['name' => NotEmpty::class], |
| 576 | + ], |
| 577 | + ]); |
| 578 | + |
| 579 | + $collectionInputFilter = $this->inputFilter; |
| 580 | + $collectionInputFilter->setInputFilter($inputFilter); |
| 581 | + |
| 582 | + $collectionInputFilter->setData([ |
| 583 | + [ |
| 584 | + 'name' => 'Tom', |
| 585 | + ], |
| 586 | + [ |
| 587 | + 'email' => 'tom@tom', |
| 588 | + 'name' => 'Tom', |
| 589 | + ], |
| 590 | + ]); |
| 591 | + |
| 592 | + $isValid = $collectionInputFilter->isValid(); |
| 593 | + $messages = $collectionInputFilter->getMessages(); |
| 594 | + |
| 595 | + |
| 596 | + $this->assertFalse($isValid); |
| 597 | + $this->assertCount(2, $messages); |
| 598 | + |
| 599 | + $this->assertArrayHasKey('email', $messages[0]); |
| 600 | + $this->assertCount(1, $messages[0]['email']); |
| 601 | + $this->assertContains('CUSTOM ERROR MESSAGE', $messages[0]['email']); |
| 602 | + $this->assertNotContains('Value is required and can\'t be empty', $messages[0]['email']); |
| 603 | + |
| 604 | + $this->assertArrayHasKey('email', $messages[1]); |
| 605 | + $this->assertCount(1, $messages[1]['email']); |
| 606 | + $this->assertContains('CUSTOM ERROR MESSAGE', $messages[1]['email']); |
| 607 | + } |
504 | 608 | }
|
0 commit comments