CaptchaTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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-2014 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-2014 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. * @group ZF-12161
  96. */
  97. public function testSettingCustomCaptchaAdapterPerConstructor()
  98. {
  99. $element = new Zend_Form_Element_Captcha(
  100. 'foo',
  101. array(
  102. 'prefixPath' => array(
  103. 'prefix' => 'Zend_Form_Element_CaptchaTest',
  104. 'path' => dirname(__FILE__) . '/_files',
  105. ),
  106. 'captcha' => 'Foo',
  107. )
  108. );
  109. $this->assertTrue(
  110. $element->getCaptcha() instanceof
  111. Zend_Form_Element_CaptchaTest_Captcha_Foo
  112. );
  113. }
  114. /**
  115. * @see ZF-4038
  116. * @group ZF-4038
  117. */
  118. public function testCaptchaShouldRenderFullyQualifiedElementName()
  119. {
  120. require_once 'Zend/Form.php';
  121. require_once 'Zend/View.php';
  122. $form = new Zend_Form();
  123. $form->addElement($this->element)
  124. ->setElementsBelongTo('bar');
  125. $html = $form->render(new Zend_View);
  126. $this->assertContains('name="bar[foo', $html, $html);
  127. $this->assertContains('id="bar-foo-', $html, $html);
  128. $this->form = $form;
  129. }
  130. /**
  131. * @see ZF-4038
  132. * @group ZF-4038
  133. */
  134. public function testCaptchaShouldValidateUsingFullyQualifiedElementName()
  135. {
  136. $this->testCaptchaShouldRenderFullyQualifiedElementName();
  137. $word = $this->element->getCaptcha()->getWord();
  138. $id = $this->element->getCaptcha()->getId();
  139. $data = array(
  140. 'bar' => array(
  141. 'foo' => array(
  142. 'id' => $id,
  143. 'input' => $word,
  144. )
  145. )
  146. );
  147. $valid = $this->form->isValid($data);
  148. $this->assertTrue($valid, var_export($this->form->getMessages(), 1));
  149. }
  150. /**
  151. * @group ZF-4822
  152. */
  153. public function testDefaultDecoratorsShouldIncludeErrorsDescriptionHtmlTagAndLabel()
  154. {
  155. $decorators = $this->element->getDecorators();
  156. $this->assertTrue(is_array($decorators));
  157. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Errors', $decorators), 'Missing Errors decorator' . var_export(array_keys($decorators), 1));
  158. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Description', $decorators), 'Missing Description decorator' . var_export(array_keys($decorators), 1));
  159. $this->assertTrue(array_key_exists('Zend_Form_Decorator_HtmlTag', $decorators), 'Missing HtmlTag decorator' . var_export(array_keys($decorators), 1));
  160. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Label', $decorators), 'Missing Label decorator' . var_export(array_keys($decorators), 1));
  161. }
  162. /**
  163. * @group ZF-5855
  164. */
  165. public function testHelperDoesNotShowUpInAttribs()
  166. {
  167. require_once 'Zend/View.php';
  168. $this->assertFalse(array_key_exists('helper', $this->element->getAttribs()));
  169. }
  170. /**
  171. * Prove the fluent interface on Zend_Form_Element_Captcha::loadDefaultDecorators
  172. *
  173. * @link http://framework.zend.com/issues/browse/ZF-9913
  174. * @return void
  175. */
  176. public function testFluentInterfaceOnLoadDefaultDecorators()
  177. {
  178. $this->assertSame($this->element, $this->element->loadDefaultDecorators());
  179. }
  180. /**
  181. * @group ZF-11609
  182. */
  183. public function testDefaultDecoratorsBeforeAndAfterRendering()
  184. {
  185. /**
  186. * Dumb captcha
  187. */
  188. // Before rendering
  189. $decorators = array_keys($this->element->getDecorators());
  190. $this->assertSame(
  191. array(
  192. 'Zend_Form_Decorator_Errors',
  193. 'Zend_Form_Decorator_Description',
  194. 'Zend_Form_Decorator_HtmlTag',
  195. 'Zend_Form_Decorator_Label',
  196. ),
  197. $decorators,
  198. var_export($decorators, true)
  199. );
  200. $this->element->render();
  201. // After rendering
  202. $decorators = array_keys($this->element->getDecorators());
  203. $this->assertSame(
  204. array(
  205. 'Zend_Form_Decorator_Captcha',
  206. 'Zend_Form_Decorator_Captcha_Word',
  207. 'Zend_Form_Decorator_Errors',
  208. 'Zend_Form_Decorator_Description',
  209. 'Zend_Form_Decorator_HtmlTag',
  210. 'Zend_Form_Decorator_Label',
  211. ),
  212. $decorators,
  213. var_export($decorators, true)
  214. );
  215. /**
  216. * ReCaptcha
  217. */
  218. // Reset element
  219. $this->setUp();
  220. $options = array(
  221. 'privKey' => 'privateKey',
  222. 'pubKey' => 'publicKey',
  223. 'ssl' => true,
  224. 'xhtml' => true,
  225. );
  226. $this->element->setCaptcha(new Zend_Captcha_ReCaptcha($options));
  227. // Before rendering
  228. $decorators = array_keys($this->element->getDecorators());
  229. $this->assertSame(
  230. array(
  231. 'Zend_Form_Decorator_Errors',
  232. 'Zend_Form_Decorator_Description',
  233. 'Zend_Form_Decorator_HtmlTag',
  234. 'Zend_Form_Decorator_Label',
  235. ),
  236. $decorators,
  237. var_export($decorators, true)
  238. );
  239. $this->element->render();
  240. // After rendering
  241. $decorators = array_keys($this->element->getDecorators());
  242. $this->assertSame(
  243. array(
  244. 'Zend_Form_Decorator_Captcha_ReCaptcha',
  245. 'Zend_Form_Decorator_Errors',
  246. 'Zend_Form_Decorator_Description',
  247. 'Zend_Form_Decorator_HtmlTag',
  248. 'Zend_Form_Decorator_Label',
  249. ),
  250. $decorators,
  251. var_export($decorators, true)
  252. );
  253. }
  254. /**
  255. * @group ZF-11609
  256. */
  257. public function testDefaultDecoratorsBeforeAndAfterRenderingWhenDefaultDecoratorsAreDisabled()
  258. {
  259. $element = new Zend_Form_Element_Captcha(
  260. 'foo',
  261. array(
  262. 'captcha' => 'Dumb',
  263. 'captchaOptions' => array(
  264. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  265. ),
  266. 'disableLoadDefaultDecorators' => true,
  267. )
  268. );
  269. // Before rendering
  270. $decorators = $element->getDecorators();
  271. $this->assertTrue(empty($decorators));
  272. $element->render();
  273. // After rendering
  274. $decorators = $element->getDecorators();
  275. $this->assertTrue(empty($decorators));
  276. }
  277. /**
  278. * @group ZF-11609
  279. */
  280. public function testIndividualDecoratorsBeforeAndAfterRendering()
  281. {
  282. // Disable default decorators is true
  283. $element = new Zend_Form_Element_Captcha(
  284. 'foo',
  285. array(
  286. 'captcha' => 'Dumb',
  287. 'captchaOptions' => array(
  288. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  289. ),
  290. 'disableLoadDefaultDecorators' => true,
  291. 'decorators' => array(
  292. 'Description',
  293. 'Errors',
  294. 'Captcha_Word',
  295. 'Captcha',
  296. 'Label',
  297. ),
  298. )
  299. );
  300. // Before rendering
  301. $decorators = array_keys($element->getDecorators());
  302. $this->assertSame(
  303. array(
  304. 'Zend_Form_Decorator_Description',
  305. 'Zend_Form_Decorator_Errors',
  306. 'Zend_Form_Decorator_Captcha_Word',
  307. 'Zend_Form_Decorator_Captcha',
  308. 'Zend_Form_Decorator_Label',
  309. ),
  310. $decorators,
  311. var_export($decorators, true)
  312. );
  313. $element->render();
  314. // After rendering
  315. $decorators = array_keys($element->getDecorators());
  316. $this->assertSame(
  317. array(
  318. 'Zend_Form_Decorator_Description',
  319. 'Zend_Form_Decorator_Errors',
  320. 'Zend_Form_Decorator_Captcha_Word',
  321. 'Zend_Form_Decorator_Captcha',
  322. 'Zend_Form_Decorator_Label',
  323. ),
  324. $decorators,
  325. var_export($decorators, true)
  326. );
  327. // Disable default decorators is false
  328. $element = new Zend_Form_Element_Captcha(
  329. 'foo',
  330. array(
  331. 'captcha' => 'Dumb',
  332. 'captchaOptions' => array(
  333. 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
  334. ),
  335. 'decorators' => array(
  336. 'Description',
  337. 'Errors',
  338. 'Captcha_Word',
  339. 'Captcha',
  340. 'Label',
  341. ),
  342. )
  343. );
  344. // Before rendering
  345. $decorators = array_keys($element->getDecorators());
  346. $this->assertSame(
  347. array(
  348. 'Zend_Form_Decorator_Description',
  349. 'Zend_Form_Decorator_Errors',
  350. 'Zend_Form_Decorator_Captcha_Word',
  351. 'Zend_Form_Decorator_Captcha',
  352. 'Zend_Form_Decorator_Label',
  353. ),
  354. $decorators,
  355. var_export($decorators, true)
  356. );
  357. $element->render();
  358. // After rendering
  359. $decorators = array_keys($element->getDecorators());
  360. $this->assertSame(
  361. array(
  362. 'Zend_Form_Decorator_Description',
  363. 'Zend_Form_Decorator_Errors',
  364. 'Zend_Form_Decorator_Captcha_Word',
  365. 'Zend_Form_Decorator_Captcha',
  366. 'Zend_Form_Decorator_Label',
  367. ),
  368. $decorators,
  369. var_export($decorators, true)
  370. );
  371. }
  372. /**
  373. * @group ZF-12173
  374. */
  375. public function testShouldAllowAddingCaptchaPrefixPathWithBackslash()
  376. {
  377. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  378. $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
  379. return;
  380. }
  381. $this->element->addPrefixPath('My\Captcha', 'My/Captcha/', 'captcha');
  382. $loader = $this->element->getPluginLoader('captcha');
  383. $paths = $loader->getPaths('My\Captcha');
  384. $this->assertTrue(is_array($paths));
  385. }
  386. /**
  387. * @group ZF-12173
  388. */
  389. public function testAddingCaptchaPrefixPathWithBackslash()
  390. {
  391. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  392. $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
  393. return;
  394. }
  395. $this->element->addPrefixPath('My\\', 'My/');
  396. $loader = $this->element->getPluginLoader('captcha');
  397. $paths = $loader->getPaths('My\Captcha');
  398. $this->assertTrue(is_array($paths));
  399. }
  400. }
  401. /**
  402. * @category Zend
  403. * @package Zend_Form
  404. * @subpackage UnitTests
  405. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  406. * @license http://framework.zend.com/license/new-bsd New BSD License
  407. * @group Zend_Form
  408. */
  409. class Zend_Form_Element_CaptchaTest_SessionContainer
  410. {
  411. protected static $_word;
  412. public function __get($name)
  413. {
  414. if ('word' == $name) {
  415. return self::$_word;
  416. }
  417. return null;
  418. }
  419. public function __set($name, $value)
  420. {
  421. if ('word' == $name) {
  422. self::$_word = $value;
  423. } else {
  424. $this->$name = $value;
  425. }
  426. }
  427. public function __isset($name)
  428. {
  429. if (('word' == $name) && (null !== self::$_word)) {
  430. return true;
  431. }
  432. return false;
  433. }
  434. public function __call($method, $args)
  435. {
  436. switch ($method) {
  437. case 'setExpirationHops':
  438. case 'setExpirationSeconds':
  439. $this->$method = array_shift($args);
  440. break;
  441. default:
  442. }
  443. }
  444. }
  445. // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
  446. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_CaptchaTest::main") {
  447. Zend_Form_Element_CaptchaTest::main();
  448. }