2
0

CaptchaTest.php 6.5 KB

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