2
0

CaptchaTest.php 6.8 KB

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