diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a115da..ca0c818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 2.7.1 - TBD +## 2.8.0 - TBD ### Added @@ -25,6 +25,25 @@ All notable changes to this project will be documented in this file, in reverse - [#23](https://github.com/zendframework/zend-captcha/pull/23) fixes an issue with garbage collection of expired CAPTCHA images when concurrent requests trigger collection. +## 2.7.1 - TBD + +### Added + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- [#31](https://github.com/zendframework/zend-captcha/pull/31) fixes using the + ReCaptcha response as the value parameter to isValid(). + ## 2.7.0 - 2017-02-20 ### Added diff --git a/README.md b/README.md index 006e7dc..f1c59b6 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,4 @@ submissions where authenticated users are not necessary, but you want to prevent spam submissions. - File issues at https://github.com/zendframework/zend-captcha/issues -- Documentation is at https://zendframework.github.io/zend-captcha/ +- Documentation is at https://docs.zendframework.com/zend-captcha/ diff --git a/doc/book/usage.md b/doc/book/usage.md index d787906..08c71c9 100644 --- a/doc/book/usage.md +++ b/doc/book/usage.md @@ -44,5 +44,5 @@ you will use the combination of: > > [zend-form](https://github.com/zendframework/zend-form) contains integration > with zend-captcha via the class `Zend\Form\Element\Captcha`; read the -> [documentation on the CAPTCHA form element](http://framework.zend.com/manual/current/en/modules/zend.form.elements.html#captcha) +> [documentation on the CAPTCHA form element](https://docs.zendframework.com/zend-form/element/captcha/) > for more details. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 73980df..2b314b3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,6 +27,11 @@ + + + + + diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index 722fcf3..cbfbac7 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -260,8 +260,12 @@ public function isValid($value, $context = null) } $service = $this->getService(); + if ((is_string($value) || is_int($value)) && array_key_exists($value, $context)) { + $res = $service->verify($context[$value]); + } else { + $res = $service->verify($value); + } - $res = $service->verify($context[$value]); if (! $res) { $this->error(self::ERR_CAPTCHA); return false; diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index 7ec57c8..c51e89d 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -10,6 +10,8 @@ namespace ZendTest\Captcha; use Zend\Captcha\ReCaptcha; +use Zend\Http\Client as HttpClient; +use Zend\Http\Client\Adapter\Socket; use ZendService\ReCaptcha\ReCaptcha as ReCaptchaService; /** @@ -26,16 +28,8 @@ class ReCaptchaTest extends \PHPUnit_Framework_TestCase public function setUp() { if (! getenv('TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT')) { - $this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test PDF render'); + $this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test Recaptcha'); } - - if (isset($this->word)) { - unset($this->word); - } - - $this->captcha = new ReCaptcha([ - 'sessionClass' => 'ZendTest\Captcha\TestAsset\SessionContainer' - ]); } public function testConstructorShouldSetOptions() @@ -163,4 +157,52 @@ public function testUsesReCaptchaHelper() $captcha = new ReCaptcha; $this->assertEquals('captcha/recaptcha', $captcha->getHelperName()); } + + public function testValidationForDifferentElementName() + { + $captcha = new ReCaptcha([ + 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), + 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), + ]); + $service = $captcha->getService(); + $service->setIp('127.0.0.1'); + $service->setHttpClient($this->getHttpClient()); + + $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $value = 'g-recaptcha-response'; + $context = ['g-recaptcha-response' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE')]; + + $this->assertTrue($captcha->isValid($value, $context)); + } + + public function testValidationForResponseElementName() + { + $captcha = new ReCaptcha([ + 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), + 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), + ]); + $service = $captcha->getService(); + $service->setIp('127.0.0.1'); + $service->setHttpClient($this->getHttpClient()); + + $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $value = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $context = ['g-recaptcha-response' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE')]; + + $this->assertTrue($captcha->isValid($value, $context)); + } + + /** + * @return HttpClient + */ + private function getHttpClient() + { + $socket = new Socket(); + $socket->setOptions([ + 'ssltransport' => 'tls', + ]); + return new HttpClient(null, [ + 'adapter' => $socket, + ]); + } }