Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/32' into develop
Browse files Browse the repository at this point in the history
Forward port #32

Also appears to pick up changes on master that had not been merged
previously to develop?

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Apr 24, 2018
2 parents bd9feba + 0aff296 commit 49b7834
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
2 changes: 1 addition & 1 deletion doc/book/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<!-- Enable this if you have installed ZendService\ReCaptcha on the
include_path or via Composer. -->
<env name="TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT" value="false" />
<!-- Change these if you want to use your own keys -->
<!-- These are test keys. See: https://developers.google.com/recaptcha/docs/faq -->
<env name="TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY" value="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"/>
<env name="TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY" value="6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"/>
<env name="TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE" value="03AI-r6f4PlCfbU_-3HGwscafLbnxjinCf7GKEErJlUHDVF1uJ_SoVebG8gJewkGjNTwLxMZNQPJ4XRTvovB8J6vHLHCVZ1yV_KzJc1Mca6QVZ_6MsNJxYsXa-5NUWTHRvl7XGkju_oPTlxRNDC7DPPkd3eaav0HW1SfdJY1uRd_w5Fkin7KBXz-Eg8oQRSRQ-MplJacQQ3rlKkpEssu1seliTNNidAmuuHWksZwdVAi4ZMyT52BA98_KitLVipT7yiqSka2m8oJZMa9uYb2buNR7X3qnqsJfUc9BxT5lF2HLiX79STktdSAI"/>

<!-- Enable this to test GC operations. These often fail in parallel
build environments. -->
Expand Down
6 changes: 5 additions & 1 deletion src/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 51 additions & 9 deletions test/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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,
]);
}
}

0 comments on commit 49b7834

Please sign in to comment.