2
0

ReCaptchaTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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-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_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 dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once 'Zend/Form/Element/Captcha.php';
  28. require_once 'Zend/View.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Captcha
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Captcha
  36. */
  37. class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ReCaptchaTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. if (isset($this->word)) {
  58. unset($this->word);
  59. }
  60. $this->element = new Zend_Form_Element_Captcha(
  61. 'captchaR',
  62. array(
  63. 'captcha' => array(
  64. 'ReCaptcha',
  65. 'sessionClass' => 'Zend_Captcha_ReCaptchaTest_SessionContainer'
  66. )
  67. )
  68. );
  69. $this->captcha = $this->element->getCaptcha();
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. }
  80. public function testConstructorShouldSetOptions()
  81. {
  82. $options = array(
  83. 'privKey' => 'privateKey',
  84. 'pubKey' => 'publicKey',
  85. 'ssl' => true,
  86. 'xhtml' => true,
  87. );
  88. $captcha = new Zend_Captcha_ReCaptcha($options);
  89. $test = $captcha->getOptions();
  90. $compare = array('privKey' => $options['privKey'], 'pubKey' => $options['pubKey']);
  91. $this->assertEquals($compare, $test);
  92. $service = $captcha->getService();
  93. $test = $service->getParams();
  94. $compare = array('ssl' => $options['ssl'], 'xhtml' => $options['xhtml']);
  95. foreach ($compare as $key => $value) {
  96. $this->assertTrue(array_key_exists($key, $test));
  97. $this->assertSame($value, $test[$key]);
  98. }
  99. }
  100. public function testShouldAllowSpecifyingServiceObject()
  101. {
  102. $captcha = new Zend_Captcha_ReCaptcha();
  103. $try = new Zend_Service_ReCaptcha();
  104. $this->assertNotSame($captcha->getService(), $try);
  105. $captcha->setService($try);
  106. $this->assertSame($captcha->getService(), $try);
  107. }
  108. public function testSetAndGetPublicAndPrivateKeys()
  109. {
  110. $captcha = new Zend_Captcha_ReCaptcha();
  111. $pubKey = 'pubKey';
  112. $privKey = 'privKey';
  113. $captcha->setPubkey($pubKey)
  114. ->setPrivkey($privKey);
  115. $this->assertSame($pubKey, $captcha->getPubkey());
  116. $this->assertSame($privKey, $captcha->getPrivkey());
  117. $this->assertSame($pubKey, $captcha->getService()->getPublicKey());
  118. $this->assertSame($privKey, $captcha->getService()->getPrivateKey());
  119. }
  120. /**
  121. * Regression tests for ZF-7654
  122. */
  123. public function testConstructorShouldAllowSettingLangOptionOnServiceObject()
  124. {
  125. $options = array('lang'=>'fr');
  126. $captcha = new Zend_Captcha_ReCaptcha($options);
  127. $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
  128. }
  129. public function testConstructorShouldAllowSettingThemeOptionOnServiceObject()
  130. {
  131. $options = array('theme'=>'black');
  132. $captcha = new Zend_Captcha_ReCaptcha($options);
  133. $this->assertEquals('black', $captcha->getService()->getOption('theme'));
  134. }
  135. public function testAllowsSettingLangOptionOnServiceObject()
  136. {
  137. $captcha = new Zend_Captcha_ReCaptcha;
  138. $captcha->setOption('lang', 'fr');
  139. $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
  140. }
  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. /**
  148. * End ZF-7654 tests
  149. */
  150. }
  151. class Zend_Captcha_ReCaptchaTest_SessionContainer
  152. {
  153. protected static $_word;
  154. public function __get($name)
  155. {
  156. if ('word' == $name) {
  157. return self::$_word;
  158. }
  159. return null;
  160. }
  161. public function __set($name, $value)
  162. {
  163. if ('word' == $name) {
  164. self::$_word = $value;
  165. } else {
  166. $this->$name = $value;
  167. }
  168. }
  169. public function __isset($name)
  170. {
  171. if (('word' == $name) && (null !== self::$_word)) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. public function __call($method, $args)
  177. {
  178. switch ($method) {
  179. case 'setExpirationHops':
  180. case 'setExpirationSeconds':
  181. $this->$method = array_shift($args);
  182. break;
  183. default:
  184. }
  185. }
  186. }
  187. // Call Zend_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  188. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ReCaptchaTest::main") {
  189. Zend_Captcha_ReCaptchaTest::main();
  190. }