2
0

CaptchaTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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-2012 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. /** Zend_Captcha_ReCaptcha */
  31. require_once 'Zend/Captcha/ReCaptcha.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Form
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Form
  39. */
  40. class Zend_Form_Element_CaptchaTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  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. * Prove the fluent interface on Zend_Form_Element_Captcha::loadDefaultDecorators
  152. *
  153. * @link http://framework.zend.com/issues/browse/ZF-9913
  154. * @return void
  155. */
  156. public function testFluentInterfaceOnLoadDefaultDecorators()
  157. {
  158. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  159. }
  160. /**
  161. * @group ZF-11609
  162. */
  163. public function testDefaultDecoratorsBeforeAndAfterRendering()
  164. {
  165. /**
  166. * Dumb captcha
  167. */
  168. // Before rendering
  169. $decorators = array_keys($this->element->getDecorators());
  170. $this->assertSame(
  171. array(
  172. 'Zend_Form_Decorator_Errors',
  173. 'Zend_Form_Decorator_Description',
  174. 'Zend_Form_Decorator_HtmlTag',
  175. 'Zend_Form_Decorator_Label',
  176. ),
  177. $decorators,
  178. var_export($decorators, true)
  179. );
  180. $this->element->render();
  181. // After rendering
  182. $decorators = array_keys($this->element->getDecorators());
  183. $this->assertSame(
  184. array(
  185. 'Zend_Form_Decorator_Captcha',
  186. 'Zend_Form_Decorator_Captcha_Word',
  187. 'Zend_Form_Decorator_Errors',
  188. 'Zend_Form_Decorator_Description',
  189. 'Zend_Form_Decorator_HtmlTag',
  190. 'Zend_Form_Decorator_Label',
  191. ),
  192. $decorators,
  193. var_export($decorators, true)
  194. );
  195. /**
  196. * ReCaptcha
  197. */
  198. // Reset element
  199. $this->setUp();
  200. $options = array(
  201. 'privKey' => 'privateKey',
  202. 'pubKey' => 'publicKey',
  203. 'ssl' => true,
  204. 'xhtml' => true,
  205. );
  206. $this->element->setCaptcha(new Zend_Captcha_ReCaptcha($options));
  207. // Before rendering
  208. $decorators = array_keys($this->element->getDecorators());
  209. $this->assertSame(
  210. array(
  211. 'Zend_Form_Decorator_Errors',
  212. 'Zend_Form_Decorator_Description',
  213. 'Zend_Form_Decorator_HtmlTag',
  214. 'Zend_Form_Decorator_Label',
  215. ),
  216. $decorators,
  217. var_export($decorators, true)
  218. );
  219. $this->element->render();
  220. // After rendering
  221. $decorators = array_keys($this->element->getDecorators());
  222. $this->assertSame(
  223. array(
  224. 'Zend_Form_Decorator_Captcha_ReCaptcha',
  225. 'Zend_Form_Decorator_Errors',
  226. 'Zend_Form_Decorator_Description',
  227. 'Zend_Form_Decorator_HtmlTag',
  228. 'Zend_Form_Decorator_Label',
  229. ),
  230. $decorators,
  231. var_export($decorators, true)
  232. );
  233. }
  234. /**
  235. * @group ZF-11609
  236. */
  237. public function testDefaultDecoratorsBeforeAndAfterRenderingWhenDefaultDecoratorsAreDisabled()
  238. {
  239. $element = new Zend_Form_Element_Captcha(
  240. 'foo',
  241. array(
  242. 'captcha' => 'Dumb',
  243. 'captchaOptions' => array(
  244. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  245. ),
  246. 'disableLoadDefaultDecorators' => true,
  247. )
  248. );
  249. // Before rendering
  250. $decorators = $element->getDecorators();
  251. $this->assertTrue(empty($decorators));
  252. $element->render();
  253. // After rendering
  254. $decorators = $element->getDecorators();
  255. $this->assertTrue(empty($decorators));
  256. }
  257. /**
  258. * @group ZF-11609
  259. */
  260. public function testIndividualDecoratorsBeforeAndAfterRendering()
  261. {
  262. // Disable default decorators is true
  263. $element = new Zend_Form_Element_Captcha(
  264. 'foo',
  265. array(
  266. 'captcha' => 'Dumb',
  267. 'captchaOptions' => array(
  268. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  269. ),
  270. 'disableLoadDefaultDecorators' => true,
  271. 'decorators' => array(
  272. 'Description',
  273. 'Errors',
  274. 'Captcha_Word',
  275. 'Captcha',
  276. 'Label',
  277. ),
  278. )
  279. );
  280. // Before rendering
  281. $decorators = array_keys($element->getDecorators());
  282. $this->assertSame(
  283. array(
  284. 'Zend_Form_Decorator_Description',
  285. 'Zend_Form_Decorator_Errors',
  286. 'Zend_Form_Decorator_Captcha_Word',
  287. 'Zend_Form_Decorator_Captcha',
  288. 'Zend_Form_Decorator_Label',
  289. ),
  290. $decorators,
  291. var_export($decorators, true)
  292. );
  293. $element->render();
  294. // After rendering
  295. $decorators = array_keys($element->getDecorators());
  296. $this->assertSame(
  297. array(
  298. 'Zend_Form_Decorator_Description',
  299. 'Zend_Form_Decorator_Errors',
  300. 'Zend_Form_Decorator_Captcha_Word',
  301. 'Zend_Form_Decorator_Captcha',
  302. 'Zend_Form_Decorator_Label',
  303. ),
  304. $decorators,
  305. var_export($decorators, true)
  306. );
  307. // Disable default decorators is false
  308. $element = new Zend_Form_Element_Captcha(
  309. 'foo',
  310. array(
  311. 'captcha' => 'Dumb',
  312. 'captchaOptions' => array(
  313. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  314. ),
  315. 'decorators' => array(
  316. 'Description',
  317. 'Errors',
  318. 'Captcha_Word',
  319. 'Captcha',
  320. 'Label',
  321. ),
  322. )
  323. );
  324. // Before rendering
  325. $decorators = array_keys($element->getDecorators());
  326. $this->assertSame(
  327. array(
  328. 'Zend_Form_Decorator_Description',
  329. 'Zend_Form_Decorator_Errors',
  330. 'Zend_Form_Decorator_Captcha_Word',
  331. 'Zend_Form_Decorator_Captcha',
  332. 'Zend_Form_Decorator_Label',
  333. ),
  334. $decorators,
  335. var_export($decorators, true)
  336. );
  337. $element->render();
  338. // After rendering
  339. $decorators = array_keys($element->getDecorators());
  340. $this->assertSame(
  341. array(
  342. 'Zend_Form_Decorator_Description',
  343. 'Zend_Form_Decorator_Errors',
  344. 'Zend_Form_Decorator_Captcha_Word',
  345. 'Zend_Form_Decorator_Captcha',
  346. 'Zend_Form_Decorator_Label',
  347. ),
  348. $decorators,
  349. var_export($decorators, true)
  350. );
  351. }
  352. }
  353. /**
  354. * @category Zend
  355. * @package Zend_Form
  356. * @subpackage UnitTests
  357. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  358. * @license http://framework.zend.com/license/new-bsd New BSD License
  359. * @group Zend_Form
  360. */
  361. class Zend_Form_Element_CaptchaTest_SessionContainer
  362. {
  363. protected static $_word;
  364. public function __get($name)
  365. {
  366. if ('word' == $name) {
  367. return self::$_word;
  368. }
  369. return null;
  370. }
  371. public function __set($name, $value)
  372. {
  373. if ('word' == $name) {
  374. self::$_word = $value;
  375. } else {
  376. $this->$name = $value;
  377. }
  378. }
  379. public function __isset($name)
  380. {
  381. if (('word' == $name) && (null !== self::$_word)) {
  382. return true;
  383. }
  384. return false;
  385. }
  386. public function __call($method, $args)
  387. {
  388. switch ($method) {
  389. case 'setExpirationHops':
  390. case 'setExpirationSeconds':
  391. $this->$method = array_shift($args);
  392. break;
  393. default:
  394. }
  395. }
  396. }
  397. // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
  398. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_CaptchaTest::main") {
  399. Zend_Form_Element_CaptchaTest::main();
  400. }