/tests/Zend/View/Helper/FormErrorsTest.php

https://github.com/jtai/zf2 · PHP · 161 lines · 96 code · 16 blank · 49 comment · 0 complexity · 2e3ef0c4d61fff76c68f1dada1130808 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\View\Helper;
  25. use Zend\View\PhpRenderer as View,
  26. Zend\View\Helper\FormErrors;
  27. /**
  28. * Test class for Zend_View_Helper_FormErrors
  29. *
  30. * @category Zend
  31. * @package Zend_View
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_View
  36. * @group Zend_View_Helper
  37. */
  38. class FormErrorsTest extends \PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Sets up the fixture, for example, open a network connection.
  42. * This method is called before a test is executed.
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. $this->view = new View();
  49. $this->helper = new FormErrors();
  50. $this->helper->setView($this->view);
  51. ob_start();
  52. }
  53. /**
  54. * Tears down the fixture, for example, close a network connection.
  55. * This method is called after a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function tearDown()
  60. {
  61. ob_end_clean();
  62. }
  63. public function testGetElementEndReturnsDefaultValue()
  64. {
  65. $this->assertEquals('</li></ul>', $this->helper->getElementEnd());
  66. }
  67. public function testGetElementSeparatorReturnsDefaultValue()
  68. {
  69. $this->assertEquals('</li><li>', $this->helper->getElementSeparator());
  70. }
  71. public function testGetElementStartReturnsDefaultValue()
  72. {
  73. $this->assertEquals('<ul%s><li>', $this->helper->getElementStart());
  74. }
  75. public function testCanSetElementEndString()
  76. {
  77. $this->testGetElementEndReturnsDefaultValue();
  78. $this->helper->setElementEnd('</pre></div>');
  79. $this->assertEquals('</pre></div>', $this->helper->getElementEnd());
  80. }
  81. public function testCanSetElementSeparatorString()
  82. {
  83. $this->testGetElementSeparatorReturnsDefaultValue();
  84. $this->helper->setElementSeparator('<br />');
  85. $this->assertEquals('<br />', $this->helper->getElementSeparator());
  86. }
  87. public function testCanSetElementStartString()
  88. {
  89. $this->testGetElementStartReturnsDefaultValue();
  90. $this->helper->setElementStart('<div><pre>');
  91. $this->assertEquals('<div><pre>', $this->helper->getElementStart());
  92. }
  93. public function testFormErrorsRendersUnorderedListByDefault()
  94. {
  95. $errors = array('foo', 'bar', 'baz');
  96. $html = $this->helper->__invoke($errors);
  97. $this->assertContains('<ul', $html);
  98. foreach ($errors as $error) {
  99. $this->assertContains('<li>' . $error . '</li>', $html);
  100. }
  101. $this->assertContains('</ul>', $html);
  102. }
  103. public function testFormErrorsRendersWithSpecifiedStrings()
  104. {
  105. $this->helper->setElementStart('<dl><dt>')
  106. ->setElementSeparator('</dt><dt>')
  107. ->setElementEnd('</dt></dl>');
  108. $errors = array('foo', 'bar', 'baz');
  109. $html = $this->helper->__invoke($errors);
  110. $this->assertContains('<dl>', $html);
  111. foreach ($errors as $error) {
  112. $this->assertContains('<dt>' . $error . '</dt>', $html);
  113. }
  114. $this->assertContains('</dl>', $html);
  115. }
  116. public function testFormErrorsPreventsXssAttacks()
  117. {
  118. $errors = array(
  119. 'bad' => '\"><script>alert("xss");</script>',
  120. );
  121. $html = $this->helper->__invoke($errors);
  122. $this->assertNotContains($errors['bad'], $html);
  123. $this->assertContains('&', $html);
  124. }
  125. public function testCanDisableEscapingErrorMessages()
  126. {
  127. $errors = array(
  128. 'foo' => '<b>Field is required</b>',
  129. 'bar' => '<a href="/help">Please click here for more information</a>'
  130. );
  131. $html = $this->helper->__invoke($errors, array('escape' => false));
  132. $this->assertContains($errors['foo'], $html);
  133. $this->assertContains($errors['bar'], $html);
  134. }
  135. /**
  136. * @issue ZF-3477
  137. * @link http://framework.zend.com/issues/browse/ZF-3477
  138. */
  139. public function testCanSetClassAttribute()
  140. {
  141. $options = array('class' => 'custom-class');
  142. $acutallHtml = $this->helper->__invoke(array(), $options);
  143. $this->assertEquals('<ul class="custom-class"><li></li></ul>', $acutallHtml);
  144. }
  145. }