<commit_before><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendTest\Form\View\Helper;

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

class FormElementErrorsTest extends CommonTestCase
{
    public function setUp()
    {
        $this->helper = new FormElementErrorsHelper();
        parent::setUp();
    }

    public function getMessageList()
    {
        return [
            'First error message',
            'Second error message',
            'Third error message',
        ];
    }

    public function testLackOfMessagesResultsInEmptyMarkup()
    {
        $element = new Element('foo');
        $markup  = $this->helper->render($element);
        $this->assertEquals('', $markup);
    }

    public function testRendersErrorMessagesUsingUnorderedListByDefault()
    {
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element);
        // @codingStandardsIgnoreStart
        $this->assertRegexp('#<ul>\s*<li>First error message</li>\s*<li>Second error message</li>\s*<li>Third error message</li>\s*</ul>#s', $markup);
        // @codingStandardsIgnoreEnd
    }

    public function testRendersErrorMessagesUsingUnorderedListTranslated()
    {
        $mockTranslator = $this->createMock('Zend\I18n\Translator\Translator');
        $mockTranslator->expects($this->at(0))
            ->method('translate')
            ->will($this->returnValue('Translated first error message'));
        $mockTranslator->expects($this->at(1))
            ->method('translate')
            ->will($this->returnValue('Translated second error message'));
        $mockTranslator->expects($this->at(2))
            ->method('translate')
            ->will($this->returnValue('Translated third error message'));

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

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

        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element);
        // @codingStandardsIgnoreStart
        $this->assertRegexp('#<ul>\s*<li>Translated first error message</li>\s*<li>Translated second error message</li>\s*<li>Translated third error message</li>\s*</ul>#s', $markup);
        // @codingStandardsIgnoreEnd
    }

    public function testCanSpecifyAttributesForOpeningTag()
    {
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element, ['class' => 'error']);
        $this->assertContains('ul class="error"', $markup);
    }

    public function testCanSpecifyAttributesForOpeningTagUsingInvoke()
    {
        $helper = $this->helper;
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $helper($element, ['class' => 'error']);
        $this->assertContains('ul class="error"', $markup);
    }

    public function testCanSpecifyAlternateMarkupStringsViaSetters()
    {
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);
<commit_msg>Provide a negative unit test for the double translation of PR-104<commit_after><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

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
{
    public function setUp()
    {
        $this->helper = new FormElementErrorsHelper();
        parent::setUp();
    }

    public function getMessageList()
    {
        return [
            'First error message',
            'Second error message',
            'Third error message',
        ];
    }

    public function testLackOfMessagesResultsInEmptyMarkup()
    {
        $element = new Element('foo');
        $markup  = $this->helper->render($element);
        $this->assertEquals('', $markup);
    }

    public function testRendersErrorMessagesUsingUnorderedListByDefault()
    {
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element);
        // @codingStandardsIgnoreStart
        $this->assertRegexp('#<ul>\s*<li>First error message</li>\s*<li>Second error message</li>\s*<li>Third error message</li>\s*</ul>#s', $markup);
        // @codingStandardsIgnoreEnd
    }

    public function testRendersErrorMessagesUsingUnorderedListTranslated()
    {
        $mockTranslator = $this->createMock('Zend\I18n\Translator\Translator');
        $mockTranslator->expects($this->at(0))
            ->method('translate')
            ->will($this->returnValue('Translated first error message'));
        $mockTranslator->expects($this->at(1))
            ->method('translate')
            ->will($this->returnValue('Translated second error message'));
        $mockTranslator->expects($this->at(2))
            ->method('translate')
            ->will($this->returnValue('Translated third error message'));

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

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

        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element);
        // @codingStandardsIgnoreStart
        $this->assertRegexp('#<ul>\s*<li>Translated first error message</li>\s*<li>Translated second error message</li>\s*<li>Translated third error message</li>\s*</ul>#s', $markup);
        // @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();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $this->helper->render($element, ['class' => 'error']);
        $this->assertContains('ul class="error"', $markup);
    }

    public function testCanSpecifyAttributesForOpeningTagUsingInvoke()
    {
        $helper = $this->helper;
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

        $markup = $helper($element, ['class' => 'error']);
        $this->assertContains('ul class="error"', $markup);
    }

    public function testCanSpecifyAlternateMarkupStringsViaSetters()
    {
        $messages = $this->getMessageList();
        $element  = new Element('foo');
        $element->setMessages($messages);

{'fraction_non_alphanumeric': 0.15205627705627706, 'fraction_numerical': 0.003896103896103896, 'mean_word_length': 2.8013163307280955, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 163, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1448435', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2552, 'n_words': 441}