2
0

ReCaptchaTest.php 9.3 KB

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