ReCaptchaTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-2009 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-2009 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. class Zend_Captcha_ReCaptchaTest_SessionContainer
  122. {
  123. protected static $_word;
  124. public function __get($name)
  125. {
  126. if ('word' == $name) {
  127. return self::$_word;
  128. }
  129. return null;
  130. }
  131. public function __set($name, $value)
  132. {
  133. if ('word' == $name) {
  134. self::$_word = $value;
  135. } else {
  136. $this->$name = $value;
  137. }
  138. }
  139. public function __isset($name)
  140. {
  141. if (('word' == $name) && (null !== self::$_word)) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. public function __call($method, $args)
  147. {
  148. switch ($method) {
  149. case 'setExpirationHops':
  150. case 'setExpirationSeconds':
  151. $this->$method = array_shift($args);
  152. break;
  153. default:
  154. }
  155. }
  156. }
  157. // Call Zend_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  158. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ReCaptchaTest::main") {
  159. Zend_Captcha_ReCaptchaTest::main();
  160. }