CaptchaTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_CaptchaTest::main");
  25. }
  26. /** Zend_Form_Element_Captcha */
  27. require_once 'Zend/Form/Element/Captcha.php';
  28. /** Zend_Captcha_Dumb */
  29. require_once 'Zend/Captcha/Dumb.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Form
  37. */
  38. class Zend_Form_Element_CaptchaTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_Element_CaptchaTest');
  43. PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function setUp()
  46. {
  47. $this->element = new Zend_Form_Element_Captcha(
  48. 'foo',
  49. array(
  50. 'captcha' => 'Dumb',
  51. 'captchaOptions' => array(
  52. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  53. ),
  54. )
  55. );
  56. }
  57. public function getCaptcha()
  58. {
  59. $captcha = new Zend_Captcha_Dumb(array(
  60. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  61. ));
  62. return $captcha;
  63. }
  64. /**
  65. * @expectedException Zend_Form_Exception
  66. */
  67. public function testConstructionShouldRequireCaptchaDetails()
  68. {
  69. $this->element = new Zend_Form_Element_Captcha('foo');
  70. }
  71. public function testShouldAllowSettingCaptcha()
  72. {
  73. $captcha = $this->getCaptcha();
  74. $this->assertNotSame($this->element->getCaptcha(), $captcha);
  75. $this->element->setCaptcha($captcha);
  76. $this->assertSame($captcha, $this->element->getCaptcha());
  77. }
  78. public function testShouldAllowAddingCaptchaPrefixPath()
  79. {
  80. $this->element->addPrefixPath('My_Captcha', 'My/Captcha/', 'captcha');
  81. $loader = $this->element->getPluginLoader('captcha');
  82. $paths = $loader->getPaths('My_Captcha');
  83. $this->assertTrue(is_array($paths));
  84. }
  85. public function testAddingNullPrefixPathShouldAddCaptchaPrefixPath()
  86. {
  87. $this->element->addPrefixPath('My', 'My');
  88. $loader = $this->element->getPluginLoader('captcha');
  89. $paths = $loader->getPaths('My_Captcha');
  90. $this->assertTrue(is_array($paths));
  91. }
  92. /**
  93. * @see ZF-4038
  94. * @group ZF-4038
  95. */
  96. public function testCaptchaShouldRenderFullyQualifiedElementName()
  97. {
  98. require_once 'Zend/Form.php';
  99. require_once 'Zend/View.php';
  100. $form = new Zend_Form();
  101. $form->addElement($this->element)
  102. ->setElementsBelongTo('bar');
  103. $html = $form->render(new Zend_View);
  104. $this->assertContains('name="bar[foo', $html, $html);
  105. $this->assertContains('id="bar-foo-', $html, $html);
  106. $this->form = $form;
  107. }
  108. /**
  109. * @see ZF-4038
  110. * @group ZF-4038
  111. */
  112. public function testCaptchaShouldValidateUsingFullyQualifiedElementName()
  113. {
  114. $this->testCaptchaShouldRenderFullyQualifiedElementName();
  115. $word = $this->element->getCaptcha()->getWord();
  116. $id = $this->element->getCaptcha()->getId();
  117. $data = array(
  118. 'bar' => array(
  119. 'foo' => array(
  120. 'id' => $id,
  121. 'input' => $word,
  122. )
  123. )
  124. );
  125. $valid = $this->form->isValid($data);
  126. $this->assertTrue($valid, var_export($this->form->getMessages(), 1));
  127. }
  128. /**
  129. * @group ZF-4822
  130. */
  131. public function testDefaultDecoratorsShouldIncludeErrorsDescriptionHtmlTagAndLabel()
  132. {
  133. $decorators = $this->element->getDecorators();
  134. $this->assertTrue(is_array($decorators));
  135. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Errors', $decorators), 'Missing Errors decorator' . var_export(array_keys($decorators), 1));
  136. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Description', $decorators), 'Missing Description decorator' . var_export(array_keys($decorators), 1));
  137. $this->assertTrue(array_key_exists('Zend_Form_Decorator_HtmlTag', $decorators), 'Missing HtmlTag decorator' . var_export(array_keys($decorators), 1));
  138. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Label', $decorators), 'Missing Label decorator' . var_export(array_keys($decorators), 1));
  139. }
  140. /**
  141. * @group ZF-5855
  142. */
  143. public function testHelperDoesNotShowUpInAttribs()
  144. {
  145. require_once 'Zend/View.php';
  146. $this->assertFalse(array_key_exists('helper', $this->element->getAttribs()));
  147. }
  148. /**
  149. * Prove the fluent interface on Zend_Form_Element_Captcha::loadDefaultDecorators
  150. *
  151. * @link http://framework.zend.com/issues/browse/ZF-9913
  152. * @return void
  153. */
  154. public function testFluentInterfaceOnLoadDefaultDecorators()
  155. {
  156. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  157. }
  158. }
  159. /**
  160. * @category Zend
  161. * @package Zend_Form
  162. * @subpackage UnitTests
  163. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  164. * @license http://framework.zend.com/license/new-bsd New BSD License
  165. * @group Zend_Form
  166. */
  167. class Zend_Form_Element_CaptchaTest_SessionContainer
  168. {
  169. protected static $_word;
  170. public function __get($name)
  171. {
  172. if ('word' == $name) {
  173. return self::$_word;
  174. }
  175. return null;
  176. }
  177. public function __set($name, $value)
  178. {
  179. if ('word' == $name) {
  180. self::$_word = $value;
  181. } else {
  182. $this->$name = $value;
  183. }
  184. }
  185. public function __isset($name)
  186. {
  187. if (('word' == $name) && (null !== self::$_word)) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. public function __call($method, $args)
  193. {
  194. switch ($method) {
  195. case 'setExpirationHops':
  196. case 'setExpirationSeconds':
  197. $this->$method = array_shift($args);
  198. break;
  199. default:
  200. }
  201. }
  202. }
  203. // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
  204. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_CaptchaTest::main") {
  205. Zend_Form_Element_CaptchaTest::main();
  206. }