PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Service/ReCaptcha/ReCaptchaTest.php

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