Skip to content

Commit

Permalink
Merge pull request #22 from mirko-pagliai/master
Browse files Browse the repository at this point in the history
updated for CakePHP 3.5+
  • Loading branch information
ADmad authored Apr 26, 2018
2 parents 997c86d + 043b04f commit 938185e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
17 changes: 10 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: php

sudo: false

php:
- 5.6
- 7.0
- 7.1
- 7.2

env:
global:
Expand All @@ -15,20 +14,24 @@ matrix:
fast_finish: true

include:
- php: 7.0
- php: 7.1
env: PHPCS=1 DEFAULT=0

- php: 7.1
env: PREFER_LOWEST=1

before_script:
- composer install --prefer-dist --no-interaction
- if [[ $PREFER_LOWEST != 1 ]]; then composer install --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.1 ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then bash <(curl -s https://codecov.io/bash); fi

notifications:
email: false
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build Status](https://travis-ci.org/crabstudio/Recaptcha.svg?branch=master)](https://travis-ci.org/crabstudio/Recaptcha) [![Latest Stable Version](https://poser.pugx.org/crabstudio/recaptcha/v/stable)](https://packagist.org/packages/crabstudio/recaptcha) [![Total Downloads](https://poser.pugx.org/crabstudio/recaptcha/downloads)](https://packagist.org/packages/crabstudio/recaptcha) [![Latest Unstable Version](https://poser.pugx.org/crabstudio/recaptcha/v/unstable)](https://packagist.org/packages/crabstudio/recaptcha) [![License](https://poser.pugx.org/crabstudio/recaptcha/license)](https://packagist.org/packages/crabstudio/recaptcha)
# Integrate Google Recaptcha v2 to your CakePHP v3.2+ project
# Integrate Google Recaptcha v2 to your CakePHP project

## Installation

Expand Down Expand Up @@ -54,18 +54,18 @@ $this->loadComponent('Recaptcha.Recaptcha', [

Display recaptcha in your view:
```
<?= $this->Form->create()?>
<?= $this->Form->input('email')?>
<?= $this->Recaptcha->display()?> // Display recaptcha box in your view, if configure enable = false, nothing to display here
<?= $this->Form->submit()?>
<?= $this->Form->end()?>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Recaptcha->display() ?> // Display recaptcha box in your view, if configure enable = false, nothing to display here
<?= $this->Form->submit() ?>
<?= $this->Form->end() ?>
```

Verify in your controller function
```
public function forgotPassword() {
if($this->request->is('post')){
if($this->Recaptcha->verify()) { // if configure enable = false, always return true
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) { // if configure enable = false, always return true
//do something here
}
$this->Flash->error(__('Please pass Google Recaptcha first'));
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
},
"require": {
"php": ">=5.6",
"cakephp/cakephp": "~3.2"
"cakephp/cakephp": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "*",
"cakephp/cakephp-codesniffer": "2.*"
"phpunit/phpunit": "^5.7.14|^6.0",
"cakephp/cakephp-codesniffer": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 4 additions & 5 deletions src/Controller/Component/RecaptchaComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
namespace Recaptcha\Controller\Component;

use Cake\Controller\Component;
use Cake\I18n\I18n;
use Cake\Network\Http\Client;
use Cake\Http\Client;

/**
* Recaptcha component
Expand Down Expand Up @@ -36,7 +35,7 @@ class RecaptchaComponent extends Component
*/
public function initialize(array $config = [])
{
$this->config($config);
$this->setConfig($config);
$this->_registry->getController()->viewBuilder()->helpers(['Recaptcha.Recaptcha' => $this->_config]);
}

Expand All @@ -51,7 +50,7 @@ public function verify()
}

$controller = $this->_registry->getController();
if (isset($controller->request->data['g-recaptcha-response'])) {
if ($controller->request->getData('g-recaptcha-response')) {
$response = json_decode($this->apiCall());

if (isset($response->success)) {
Expand All @@ -75,7 +74,7 @@ protected function apiCall()

return $client->post('https://www.google.com/recaptcha/api/siteverify', [
'secret' => $this->_config['secret'],
'response' => $controller->request->data['g-recaptcha-response'],
'response' => $controller->request->getData('g-recaptcha-response'),
'remoteip' => $controller->request->clientIp()
])->getBody();
}
Expand Down
4 changes: 2 additions & 2 deletions src/View/Helper/RecaptchaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RecaptchaHelper extends Helper
*/
public function initialize(array $config = [])
{
$this->config($config);
$this->setConfig($config);
}

/**
Expand All @@ -25,7 +25,7 @@ public function initialize(array $config = [])
*/
public function display()
{
$recaptcha = $this->config();
$recaptcha = $this->getConfig();
if (!$recaptcha['enable']) {
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Network\Request;
use Cake\Http\ServerRequest as Request;
use Cake\TestSuite\TestCase;
use Recaptcha\Controller\Component\RecaptchaComponent;

Expand Down Expand Up @@ -37,7 +37,7 @@ public function testVerifyFalse()
{
$this->assertFalse($this->Recaptcha->verify());

$this->controller->request->data['g-recaptcha-response'] = 'foo';
$this->controller->request = $this->controller->request->withData('g-recaptcha-response', 'foo');

$this->Recaptcha->expects($this->once())
->method('apiCall')
Expand All @@ -48,15 +48,15 @@ public function testVerifyFalse()

public function testVerifyTrue()
{
$this->controller->request->data['g-recaptcha-response'] = 'foo';
$this->controller->request = $this->controller->request->withData('g-recaptcha-response', 'foo');

$this->Recaptcha->expects($this->once())
->method('apiCall')
->will($this->returnValue('{"success":true}'));

$this->assertTrue($this->Recaptcha->verify());

$this->Recaptcha->config('enable', false);
$this->Recaptcha->setConfig('enable', false);
$this->assertTrue($this->Recaptcha->verify());
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/RecaptchaHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testDisplay()
$this->assertTrue(is_string($result));
$this->assertContains('class="g-recaptcha"', $result);

$this->Recaptcha->config('enable', false);
$this->Recaptcha->setConfig('enable', false);
$this->assertEmpty($this->Recaptcha->display());
}
}

0 comments on commit 938185e

Please sign in to comment.