PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 281 lines | 182 code | 58 blank | 41 comment | 0 complexity | a566c6a0308623a691d433b25d74bfad 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-2012 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 25153 2012-11-28 11:56:23Z cogo $
  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-2012 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. $adapter = new Zend_Http_Client_Adapter_Test();
  170. $client = new Zend_Http_Client(null, array(
  171. 'adapter' => $adapter
  172. ));
  173. Zend_Service_ReCaptcha::setHttpClient($client);
  174. // Set a header that will be reset in the recaptcha class before sending the request
  175. $client->setHeaders('host', 'example.com');
  176. $resp = $this->_reCaptcha->verify('challengeField', 'responseField');
  177. $this->assertNotSame('example.com', $client->getHeader('host'));
  178. // See if we have a valid object and that the status is false
  179. $this->assertTrue($resp instanceof Zend_Service_ReCaptcha_Response);
  180. $this->assertFalse($resp->getStatus());
  181. }
  182. public function testGetHtml() {
  183. $this->_reCaptcha->setPublicKey($this->_publicKey);
  184. $errorMsg = 'errorMsg';
  185. $this->_reCaptcha->setParam('ssl', true);
  186. $this->_reCaptcha->setParam('xhtml', true);
  187. $this->_reCaptcha->setParam('error', $errorMsg);
  188. $html = $this->_reCaptcha->getHtml();
  189. // See if the options for the captcha exist in the string
  190. $this->assertNotSame(false, strstr($html, 'var RecaptchaOptions = {"theme":"red","lang":"en"};'));
  191. // See if the js/iframe src is correct
  192. $this->assertNotSame(false, strstr($html, 'src="' . Zend_Service_ReCaptcha::API_SECURE_SERVER . '/challenge?k=' . $this->_publicKey . '&error=' . $errorMsg . '"'));
  193. }
  194. /** @group ZF-10991 */
  195. public function testHtmlGenerationWillUseSuppliedNameForNoScriptElements()
  196. {
  197. $this->_reCaptcha->setPublicKey($this->_publicKey);
  198. $html = $this->_reCaptcha->getHtml('contact');
  199. $this->assertContains('contact[recaptcha_challenge_field]', $html);
  200. $this->assertContains('contact[recaptcha_response_field]', $html);
  201. }
  202. public function testVerifyWithMissingPrivateKey() {
  203. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  204. $this->_reCaptcha->verify('challenge', 'response');
  205. }
  206. public function testVerifyWithMissingIp() {
  207. $this->setExpectedException('Zend_Service_ReCaptcha_Exception');
  208. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  209. $this->_reCaptcha->verify('challenge', 'response');
  210. }
  211. public function testVerifyWithMissingChallengeField() {
  212. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  213. $this->_reCaptcha->setIp('127.0.0.1');
  214. $response = $this->_reCaptcha->verify('', 'response');
  215. $this->assertFalse($response->getStatus());
  216. }
  217. public function testVerifyWithMissingResponseField() {
  218. $this->_reCaptcha->setPrivateKey($this->_privateKey);
  219. $this->_reCaptcha->setIp('127.0.0.1');
  220. $response = $this->_reCaptcha->verify('challenge', '');
  221. $this->assertFalse($response->getStatus());
  222. }
  223. }