ReCaptchaTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_Captcha
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Captcha_ReCaptchaTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Captcha.php';
  27. require_once 'Zend/View.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Captcha
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Captcha
  35. */
  36. class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Runs the test methods of this class.
  40. *
  41. * @return void
  42. */
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ReCaptchaTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture, for example, open a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @return void
  53. */
  54. public function setUp()
  55. {
  56. if (isset($this->word)) {
  57. unset($this->word);
  58. }
  59. $this->element = new Zend_Form_Element_Captcha(
  60. 'captchaR',
  61. array(
  62. 'captcha' => array(
  63. 'ReCaptcha',
  64. 'sessionClass' => 'Zend_Captcha_ReCaptchaTest_SessionContainer'
  65. )
  66. )
  67. );
  68. $this->captcha = $this->element->getCaptcha();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. }
  79. public function testConstructorShouldSetOptions()
  80. {
  81. $options = array(
  82. 'privKey' => 'privateKey',
  83. 'pubKey' => 'publicKey',
  84. 'ssl' => true,
  85. 'xhtml' => true,
  86. );
  87. $captcha = new Zend_Captcha_ReCaptcha($options);
  88. $test = $captcha->getOptions();
  89. $compare = array('privKey' => $options['privKey'], 'pubKey' => $options['pubKey']);
  90. $this->assertEquals($compare, $test);
  91. $service = $captcha->getService();
  92. $test = $service->getParams();
  93. $compare = array('ssl' => $options['ssl'], 'xhtml' => $options['xhtml']);
  94. foreach ($compare as $key => $value) {
  95. $this->assertTrue(array_key_exists($key, $test));
  96. $this->assertSame($value, $test[$key]);
  97. }
  98. }
  99. public function testShouldAllowSpecifyingServiceObject()
  100. {
  101. $captcha = new Zend_Captcha_ReCaptcha();
  102. $try = new Zend_Service_ReCaptcha();
  103. $this->assertNotSame($captcha->getService(), $try);
  104. $captcha->setService($try);
  105. $this->assertSame($captcha->getService(), $try);
  106. }
  107. public function testSetAndGetPublicAndPrivateKeys()
  108. {
  109. $captcha = new Zend_Captcha_ReCaptcha();
  110. $pubKey = 'pubKey';
  111. $privKey = 'privKey';
  112. $captcha->setPubkey($pubKey)
  113. ->setPrivkey($privKey);
  114. $this->assertSame($pubKey, $captcha->getPubkey());
  115. $this->assertSame($privKey, $captcha->getPrivkey());
  116. $this->assertSame($pubKey, $captcha->getService()->getPublicKey());
  117. $this->assertSame($privKey, $captcha->getService()->getPrivateKey());
  118. }
  119. /** @group ZF-7654 */
  120. public function testConstructorShouldAllowSettingLangOptionOnServiceObject()
  121. {
  122. $options = array('lang'=>'fr');
  123. $captcha = new Zend_Captcha_ReCaptcha($options);
  124. $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
  125. }
  126. /** @group ZF-7654 */
  127. public function testConstructorShouldAllowSettingThemeOptionOnServiceObject()
  128. {
  129. $options = array('theme'=>'black');
  130. $captcha = new Zend_Captcha_ReCaptcha($options);
  131. $this->assertEquals('black', $captcha->getService()->getOption('theme'));
  132. }
  133. /** @group ZF-7654 */
  134. public function testAllowsSettingLangOptionOnServiceObject()
  135. {
  136. $captcha = new Zend_Captcha_ReCaptcha;
  137. $captcha->setOption('lang', 'fr');
  138. $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
  139. }
  140. /** @group ZF-7654 */
  141. public function testAllowsSettingThemeOptionOnServiceObject()
  142. {
  143. $captcha = new Zend_Captcha_ReCaptcha;
  144. $captcha->setOption('theme', 'black');
  145. $this->assertEquals('black', $captcha->getService()->getOption('theme'));
  146. }
  147. /** @group ZF-10991 */
  148. public function testRenderWillUseElementNameWhenRenderingNoScriptFields()
  149. {
  150. $captcha = new Zend_Captcha_ReCaptcha;
  151. $pubKey = 'pubKey';
  152. $privKey = 'privKey';
  153. $captcha->setPubkey($pubKey)
  154. ->setPrivkey($privKey);
  155. $element = new Zend_Form_Element_Captcha('captcha', array(
  156. 'captcha' => $captcha,
  157. 'belongsTo' => 'contact',
  158. ));
  159. $view = new Zend_View();
  160. $html = $captcha->render($view, $element);
  161. $this->assertContains('contact[recaptcha_challenge_field]', $html);
  162. $this->assertContains('contact[recaptcha_response_field]', $html);
  163. }
  164. /** @group ZF-10991 */
  165. public function testUsesReCaptchaSpecificDecorator()
  166. {
  167. $captcha = new Zend_Captcha_ReCaptcha;
  168. $this->assertEquals('Captcha_ReCaptcha', $captcha->getDecorator());
  169. }
  170. /**
  171. * @group ZF-12086
  172. */
  173. public function testAllowsSettingCustomTranslationsOptionOnServiceObject()
  174. {
  175. $options = array(
  176. 'instructions_visual' => 'Wpisz dwa wyrazy:',
  177. 'instructions_audio' => 'Wpisz uslyszany tekst:'
  178. );
  179. $captcha = new Zend_Captcha_ReCaptcha;
  180. $captcha->setOption('custom_translations', $options);
  181. $this->assertEquals(
  182. $options,
  183. $captcha->getService()->getOption('custom_translations')
  184. );
  185. }
  186. }
  187. class Zend_Captcha_ReCaptchaTest_SessionContainer
  188. {
  189. protected static $_word;
  190. public function __get($name)
  191. {
  192. if ('word' == $name) {
  193. return self::$_word;
  194. }
  195. return null;
  196. }
  197. public function __set($name, $value)
  198. {
  199. if ('word' == $name) {
  200. self::$_word = $value;
  201. } else {
  202. $this->$name = $value;
  203. }
  204. }
  205. public function __isset($name)
  206. {
  207. if (('word' == $name) && (null !== self::$_word)) {
  208. return true;
  209. }
  210. return false;
  211. }
  212. public function __call($method, $args)
  213. {
  214. switch ($method) {
  215. case 'setExpirationHops':
  216. case 'setExpirationSeconds':
  217. $this->$method = array_shift($args);
  218. break;
  219. default:
  220. }
  221. }
  222. }
  223. // Call Zend_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  224. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ReCaptchaTest::main") {
  225. Zend_Captcha_ReCaptchaTest::main();
  226. }