ReCaptchaTest.php 5.1 KB

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