ReCaptchaTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_Service_ReCaptcha
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** @see Zend_Service_ReCaptcha */
  27. require_once 'Zend/Service/ReCaptcha.php';
  28. /** @see Zend_Http_Client_Adapter_Test */
  29. require_once 'Zend/Http/Client/Adapter/Test.php';
  30. /** @see Zend_Config */
  31. require_once 'Zend/Config.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Service_ReCaptcha
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Service_ReCaptcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
  40. {
  41. protected $_publicKey = TESTS_ZEND_SERVICE_RECAPTCHA_PUBLIC_KEY;
  42. protected $_privateKey = TESTS_ZEND_SERVICE_RECAPTCHA_PRIVATE_KEY;
  43. protected $_reCaptcha = null;
  44. public function setUp() {
  45. $this->_reCaptcha = new Zend_Service_ReCaptcha();
  46. }
  47. public function testSetAndGet() {
  48. /* Set and get IP address */
  49. $ip = '127.0.0.1';
  50. $this->_reCaptcha->setIp($ip);
  51. $this->assertSame($ip, $this->_reCaptcha->getIp());
  52. /* Set and get public key */
  53. $this->_reCaptcha->setPublicKey($this->_publicKey);
  54. $this->assertSame($this->_publicKey, $this->_reCaptcha->getPublicKey());
  55. /* Set and get private key */
  56. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  57. $this->assertSame($this->_privateKey, $this->_reCaptcha->getPrivateKey());
  58. }
  59. public function testSingleParam() {
  60. $key = 'ssl';
  61. $value = true;
  62. $this->_reCaptcha->setParam($key, $value);
  63. $this->assertSame($value, $this->_reCaptcha->getParam($key));
  64. }
  65. public function tetsGetNonExistingParam() {
  66. $this->assertNull($this->_reCaptcha->getParam('foobar'));
  67. }
  68. public function testMultipleParams() {
  69. $params = array(
  70. 'ssl' => true,
  71. 'error' => 'errorMsg',
  72. 'xhtml' => true,
  73. );
  74. $this->_reCaptcha->setParams($params);
  75. $_params = $this->_reCaptcha->getParams();
  76. $this->assertSame($params['ssl'], $_params['ssl']);
  77. $this->assertSame($params['error'], $_params['error']);
  78. $this->assertSame($params['xhtml'], $_params['xhtml']);
  79. }
  80. public function testSingleOption() {
  81. $key = 'theme';
  82. $value = 'black';
  83. $this->_reCaptcha->setOption($key, $value);
  84. $this->assertSame($value, $this->_reCaptcha->getOption($key));
  85. }
  86. public function tetsGetNonExistingOption() {
  87. $this->assertNull($this->_reCaptcha->getOption('foobar'));
  88. }
  89. public function testMultipleOptions() {
  90. $options = array(
  91. 'theme' => 'black',
  92. 'lang' => 'no',
  93. );
  94. $this->_reCaptcha->setOptions($options);
  95. $_options = $this->_reCaptcha->getOptions();
  96. $this->assertSame($options['theme'], $_options['theme']);
  97. $this->assertSame($options['lang'], $_options['lang']);
  98. }
  99. public function testSetMultipleParamsFromZendConfig() {
  100. $params = array(
  101. 'ssl' => true,
  102. 'error' => 'errorMsg',
  103. 'xhtml' => true,
  104. );
  105. $config = new Zend_Config($params);
  106. $this->_reCaptcha->setParams($config);
  107. $_params = $this->_reCaptcha->getParams();
  108. $this->assertSame($params['ssl'], $_params['ssl']);
  109. $this->assertSame($params['error'], $_params['error']);
  110. $this->assertSame($params['xhtml'], $_params['xhtml']);
  111. }
  112. public function testSetInvalidParams() {
  113. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  114. $var = 'string';
  115. $this->_reCaptcha->setParams($var);
  116. }
  117. public function testSetMultipleOptionsFromZendConfig() {
  118. $options = array(
  119. 'theme' => 'black',
  120. 'lang' => 'no',
  121. );
  122. $config = new Zend_Config($options);
  123. $this->_reCaptcha->setOptions($config);
  124. $_options = $this->_reCaptcha->getOptions();
  125. $this->assertSame($options['theme'], $_options['theme']);
  126. $this->assertSame($options['lang'], $_options['lang']);
  127. }
  128. public function testSetInvalidOptions() {
  129. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  130. $var = 'string';
  131. $this->_reCaptcha->setOptions($var);
  132. }
  133. public function testConstructor() {
  134. $params = array(
  135. 'ssl' => true,
  136. 'error' => 'errorMsg',
  137. 'xhtml' => true,
  138. );
  139. $options = array(
  140. 'theme' => 'black',
  141. 'lang' => 'no',
  142. );
  143. $ip = '127.0.0.1';
  144. $reCaptcha = new Zend_Service_ReCaptcha($this->_publicKey, $this->_privateKey, $params, $options, $ip);
  145. $_params = $reCaptcha->getParams();
  146. $_options = $reCaptcha->getOptions();
  147. $this->assertSame($this->_publicKey, $reCaptcha->getPublicKey());
  148. $this->assertSame($this->_privateKey, $reCaptcha->getPrivateKey());
  149. $this->assertSame($params['ssl'], $_params['ssl']);
  150. $this->assertSame($params['error'], $_params['error']);
  151. $this->assertSame($params['xhtml'], $_params['xhtml']);
  152. $this->assertSame($options['theme'], $_options['theme']);
  153. $this->assertSame($options['lang'], $_options['lang']);
  154. $this->assertSame($ip, $reCaptcha->getIp());
  155. }
  156. public function testConstructorWithNoIp() {
  157. // Fake the _SERVER value
  158. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  159. $reCaptcha = new Zend_Service_ReCaptcha(null, null, null, null, null);
  160. $this->assertSame($_SERVER['REMOTE_ADDR'], $reCaptcha->getIp());
  161. unset($_SERVER['REMOTE_ADDR']);
  162. }
  163. public function testGetHtmlWithNoPublicKey() {
  164. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  165. $html = $this->_reCaptcha->getHtml();
  166. }
  167. public function testVerify() {
  168. $this->_reCaptcha->setPublicKey($this->_publicKey);
  169. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  170. $this->_reCaptcha->setIp('127.0.0.1');
  171. if (defined('TESTS_ZEND_SERVICE_RECAPTCHA_ONLINE_ENABLED') &&
  172. constant('TESTS_ZEND_SERVICE_RECAPTCHA_ONLINE_ENABLED')) {
  173. $this->_testVerifyOnline();
  174. } else {
  175. $this->_testVerifyOffline();
  176. }
  177. }
  178. protected function _testVerifyOnline() {
  179. }
  180. protected function _testVerifyOffline() {
  181. $adapter = new Zend_Http_Client_Adapter_Test();
  182. $client = new Zend_Http_Client(null, array(
  183. 'adapter' => $adapter
  184. ));
  185. Zend_Service_ReCaptcha::setHttpClient($client);
  186. $resp = $this->_reCaptcha->verify('challengeField', 'responseField');
  187. // See if we have a valid object and that the status is false
  188. $this->assertTrue($resp instanceof Zend_Service_ReCaptcha_Response);
  189. $this->assertFalse($resp->getStatus());
  190. }
  191. public function testGetHtml() {
  192. $this->_reCaptcha->setPublicKey($this->_publicKey);
  193. $errorMsg = 'errorMsg';
  194. $this->_reCaptcha->setParam('ssl', true);
  195. $this->_reCaptcha->setParam('xhtml', true);
  196. $this->_reCaptcha->setParam('error', $errorMsg);
  197. $html = $this->_reCaptcha->getHtml();
  198. // See if the options for the captcha exist in the string
  199. $this->assertNotSame(false, strstr($html, 'var RecaptchaOptions = {"theme":"red","lang":"en"};'));
  200. // See if the js/iframe src is correct
  201. $this->assertNotSame(false, strstr($html, 'src="' . Zend_Service_ReCaptcha::API_SECURE_SERVER . '/challenge?k=' . $this->_publicKey . '&error=' . $errorMsg . '"'));
  202. }
  203. public function testVerifyWithMissingPrivateKey() {
  204. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  205. $this->_reCaptcha->verify('challenge', 'response');
  206. }
  207. public function testVerifyWithMissingIp() {
  208. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  209. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  210. $this->_reCaptcha->verify('challenge', 'response');
  211. }
  212. public function testVerifyWithMissingChallengeField() {
  213. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  214. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  215. $this->_reCaptcha->setIp('127.0.0.1');
  216. $this->_reCaptcha->verify('', 'response');
  217. }
  218. public function testVerifyWithMissingResponseField() {
  219. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  220. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  221. $this->_reCaptcha->setIp('127.0.0.1');
  222. $this->_reCaptcha->verify('challenge', '');
  223. }
  224. }