PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Form/Element/CaptchaTest.php

https://github.com/Exercise/zf2
PHP | 156 lines | 97 code | 13 blank | 46 comment | 0 complexity | 09e403d3f96b88cbdfe77657c55e3b64 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_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. namespace ZendTest\Form\Element;
  23. use Zend\Form\Element\Captcha as CaptchaElement,
  24. Zend\Form\Form,
  25. Zend\Captcha\Dumb as DumbCaptcha,
  26. Zend\View\View;
  27. /**
  28. * @category Zend
  29. * @package Zend_Form
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Form
  34. */
  35. class CaptchaTest extends \PHPUnit_Framework_TestCase
  36. {
  37. public function setUp()
  38. {
  39. $this->element = new CaptchaElement(
  40. 'foo',
  41. array(
  42. 'captcha' => 'Dumb',
  43. 'captchaOptions' => array(
  44. 'sessionClass' => 'ZendTest\Form\Element\TestAsset\SessionContainer',
  45. ),
  46. )
  47. );
  48. }
  49. public function getCaptcha()
  50. {
  51. $captcha = new DumbCaptcha(array(
  52. 'sessionClass' => 'ZendTest\Form\Element\TestAsset\SessionContainer',
  53. ));
  54. return $captcha;
  55. }
  56. public function testConstructionShouldRequireCaptchaDetails()
  57. {
  58. $this->setExpectedException('Zend\Form\Exception');
  59. $this->element = new CaptchaElement('foo');
  60. }
  61. public function testShouldAllowSettingCaptcha()
  62. {
  63. $captcha = $this->getCaptcha();
  64. $this->assertNotSame($this->element->getCaptcha(), $captcha);
  65. $this->element->setCaptcha($captcha);
  66. $this->assertSame($captcha, $this->element->getCaptcha());
  67. }
  68. public function testShouldAllowAddingCaptchaPrefixPath()
  69. {
  70. $this->element->addPrefixPath('My\Captcha', 'My/Captcha/', 'captcha');
  71. $loader = $this->element->getPluginLoader('captcha');
  72. $paths = $loader->getPaths('My\Captcha');
  73. $this->assertTrue(is_array($paths));
  74. }
  75. public function testAddingNullPrefixPathShouldAddCaptchaPrefixPath()
  76. {
  77. $this->element->addPrefixPath('My', 'My');
  78. $loader = $this->element->getPluginLoader('captcha');
  79. $paths = $loader->getPaths('My\Captcha');
  80. $this->assertTrue(is_array($paths));
  81. }
  82. /**
  83. * @group ZF-4038
  84. */
  85. public function testCaptchaShouldRenderFullyQualifiedElementName()
  86. {
  87. $form = new Form();
  88. $form->addElement($this->element)
  89. ->setElementsBelongTo('bar');
  90. $html = $form->render(new View);
  91. $this->assertContains('name="bar[foo', $html, $html);
  92. $this->assertContains('id="bar-foo-', $html, $html);
  93. $this->form = $form;
  94. }
  95. /**
  96. * @group ZF-4038
  97. */
  98. public function testCaptchaShouldValidateUsingFullyQualifiedElementName()
  99. {
  100. $this->testCaptchaShouldRenderFullyQualifiedElementName();
  101. $word = $this->element->getCaptcha()->getWord();
  102. $id = $this->element->getCaptcha()->getId();
  103. $data = array(
  104. 'bar' => array(
  105. 'foo' => array(
  106. 'id' => $id,
  107. 'input' => $word,
  108. )
  109. )
  110. );
  111. $valid = $this->form->isValid($data);
  112. $this->assertTrue($valid, var_export($this->form->getMessages(), 1));
  113. }
  114. /**
  115. * @group ZF-4822
  116. */
  117. public function testDefaultDecoratorsShouldIncludeErrorsDescriptionHtmlTagAndLabel()
  118. {
  119. $decorators = $this->element->getDecorators();
  120. $this->assertTrue(is_array($decorators));
  121. $this->assertTrue(array_key_exists('Zend\Form\Decorator\Errors', $decorators), 'Missing Errors decorator' . var_export(array_keys($decorators), 1));
  122. $this->assertTrue(array_key_exists('Zend\Form\Decorator\Description', $decorators), 'Missing Description decorator' . var_export(array_keys($decorators), 1));
  123. $this->assertTrue(array_key_exists('Zend\Form\Decorator\HtmlTag', $decorators), 'Missing HtmlTag decorator' . var_export(array_keys($decorators), 1));
  124. $this->assertTrue(array_key_exists('Zend\Form\Decorator\Label', $decorators), 'Missing Label decorator' . var_export(array_keys($decorators), 1));
  125. }
  126. /**
  127. * @group ZF-5855
  128. */
  129. public function testHelperDoesNotShowUpInAttribs()
  130. {
  131. $this->assertFalse(array_key_exists('helper', $this->element->getAttribs()));
  132. }
  133. /**
  134. * Prove the fluent interface on Zend_Form_Element_Captcha::loadDefaultDecorators
  135. *
  136. * @link http://framework.zend.com/issues/browse/ZF-9913
  137. * @return void
  138. */
  139. public function testFluentInterfaceOnLoadDefaultDecorators()
  140. {
  141. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  142. }
  143. }