Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Provide a negative unit test for the double translation of PR-104
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasKuehneEllerhold authored and weierophinney committed May 14, 2018
1 parent 4b78d2e commit 25f5b9b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/View/Helper/FormElementErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace ZendTest\Form\View\Helper;

use Zend\Form\Element;
use Zend\Form\Form;
use Zend\Form\View\Helper\FormElementErrors as FormElementErrorsHelper;
use Zend\Validator\AbstractValidator;

class FormElementErrorsTest extends CommonTestCase
{
Expand Down Expand Up @@ -76,6 +78,54 @@ public function testRendersErrorMessagesUsingUnorderedListTranslated()
// @codingStandardsIgnoreEnd
}

public function testRendersErrorMessagesWithoutDoubleTranslation()
{
$form = new Form('test_form');
$form->add(
[
'name' => 'test_element',
'type' => Element\Color::class,
]
);
$form->setData(['test_element' => 'This is invalid!']);

$mockValidatorTranslator = $this->createMock('Zend\Validator\Translator\TranslatorInterface');
$mockValidatorTranslator
->expects(self::once())
->method('translate')
->willReturnCallback(
function ($message) {
self::assertEquals(
'The input does not match against pattern \'%pattern%\'',
$message,
'Unexpected translation key.'
);

return 'TRANSLATED: The input does not match against pattern \'%pattern%\'';
}
);

AbstractValidator::setDefaultTranslator($mockValidatorTranslator, 'default');

self::assertFalse($form->isValid());

$mockFormTranslator = $this->createMock('Zend\I18n\Translator\Translator');
$mockFormTranslator
->expects(self::never())
->method('translate');

$this->helper->setTranslator($mockFormTranslator);
$this->assertTrue($this->helper->hasTranslator());

$this->helper->setTranslatorTextDomain('default');

$markup = $this->helper->render($form->get('test_element'));

// @codingStandardsIgnoreStart
$this->assertRegexp('#<ul>\s*<li>TRANSLATED: The input does not match against pattern \'/^#[0-9a-fA-F]{6}$/\'</li>\s*</ul>#s', $markup);
// @codingStandardsIgnoreEnd
}

public function testCanSpecifyAttributesForOpeningTag()
{
$messages = $this->getMessageList();
Expand Down

0 comments on commit 25f5b9b

Please sign in to comment.