Skip to content

Commit

Permalink
first test for multiple widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
cake17 committed Mar 6, 2015
1 parent 9de35b9 commit 93fe158
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ It is recommended that you add `/vendor` to your .gitignore file.

In your `config/bootstrap.php` file:

Plugin::load('Recaptcha', ['routes' => false, 'bootstrap' => true]);
Plugin::load('Recaptcha', ['routes' => true, 'bootstrap' => true]);

### 2. Go to Google reCAPTCHA site

Expand Down Expand Up @@ -123,7 +123,7 @@ For example:

See another example of contact with no form in
`src/Controller/ContactController.php`, `src/Template/Contact/index.ctp` and
`src/Form/ContactForm.php`
`src/Form/ContactForm.php`. You can test it by going to `http://yoursite.com/recaptcha/contact`.

## What's inside ? ##

Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"wiki": "https://github.com/google/ReCAPTCHA"
},
"require": {
"cakephp/cakephp": "3.0.*-dev"
"php": ">=5.4",
"cakephp/cakephp": "3.0.x-dev"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand All @@ -39,10 +40,8 @@
},
"autoload-dev": {
"psr-4": {
"Recaptcha\\": "tests"
"Recaptcha\\Test\\": "tests",
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
}
},
"extra": {
"installer-name": "Recaptcha"
}
}
8 changes: 4 additions & 4 deletions config/recaptcha.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
return [
'Recaptcha' => [
// Register API keys at https://www.google.com/recaptcha/admin
'siteKey' => '',
'sitekey' => '',
'secret' => '',
// reCAPTCHA supported 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
'defaultLang' => 'en',
'lang' => 'en',
// either light or dark
'defaultTheme' => 'light',
'theme' => 'light',
// either image or audio
'defaultType' => 'image',
'type' => 'image',
]
];
6 changes: 6 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
use Cake\Routing\Router;

Router::plugin('Recaptcha', function ($routes) {
$routes->fallbacks('DashedRoute');
});
53 changes: 38 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
syntaxCheck="false">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Recaptcha">
<directory>./tests/</directory>
<testsuite name="Recaptcha Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests and vendors -->
<filter>
<blacklist>
<directory suffix=".php">./vendor/</directory>
<directory suffix=".ctp">./vendor/</directory>

<directory suffix=".php">./tests/</directory>
<directory suffix=".ctp">./tests/</directory>
</blacklist>
</filter>

</phpunit>
11 changes: 7 additions & 4 deletions src/Controller/Component/RecaptchaComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ public function startup(Event $event)
// The error code from reCAPTCHA, if any
$error = null;
$reCaptcha = new ReCaptcha($this->config('secret'));
// unset secret from config
$this->config('secret', '');

// send siteKey, lang, theme and type from config to helper
$controller->helpers['Recaptcha.Recaptcha']['siteKey'] = $this->config('siteKey');
$controller->helpers['Recaptcha.Recaptcha']['lang'] = $this->config('defaultLang');
$controller->helpers['Recaptcha.Recaptcha']['theme'] = $this->config('defaultTheme');
$controller->helpers['Recaptcha.Recaptcha']['type'] = $this->config('defaultType');
// $controller->helpers['Recaptcha.Recaptcha'] = $this->config();
// $controller->helpers['Recaptcha.Recaptcha']['siteKey'] = $this->config('siteKey');
// $controller->helpers['Recaptcha.Recaptcha']['lang'] = $this->config('defaultLang');
// $controller->helpers['Recaptcha.Recaptcha']['theme'] = $this->config('defaultTheme');
// $controller->helpers['Recaptcha.Recaptcha']['type'] = $this->config('defaultType');

if ($controller->request->is(['post', 'put', 'patch'])) {
// if Recaptcha is not checked
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* @link http://cake17.github.io/
*
*/
namespace App\Controller;
namespace Recaptcha\Controller;

use App\Controller\AppController;
use App\Form\ContactForm;
use Recaptcha\Form\ContactForm;

class ContactController extends AppController
{
Expand Down
4 changes: 2 additions & 2 deletions src/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @link http://book.cakephp.org/3.0/en/core-libraries/form.html#
*
*/
namespace App\Form;
namespace Recaptcha\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
Expand Down Expand Up @@ -36,7 +36,7 @@ protected function _buildSchema(Schema $schema)
protected function _buildValidator(Validator $validator)
{
return $validator->add('name', 'length', [
'rule' => ['minLength', 10],
'rule' => ['minLength', 5],
'message' => 'A name is required'
])->add('email', 'format', [
'rule' => 'email',
Expand Down
51 changes: 27 additions & 24 deletions src/View/Helper/RecaptchaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,10 @@
use Cake\I18n\I18n;
use Cake\View\Helper;
use Cake\View\View;
use Cake\Core\Configure;

class RecaptchaHelper extends Helper
{
/**
* Default language
* If no language is found anywhere
*
* @var string
*/
protected $defaultLang = 'en';

/**
* Default theme
* If no theme is found anywhere
*
* @var string
*/
protected $defaultTheme = 'light';

/**
* Default type
* If no type is found anywhere
*
* @var string
*/
protected $defaultType = 'image';

/**
* Infos for Widgets
*
Expand All @@ -52,6 +29,12 @@ class RecaptchaHelper extends Helper
* @var array
*/
protected $_defaultConfig = [
// If no language is found anywhere
'lang' => 'en',
// If no theme is found anywhere
'theme' => 'light',
// If no type is found anywhere
'type' => 'image',
'secureApiUrl' => 'https://www.google.com/recaptcha/api',
// reCAPTCHA supported 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
'langAccepted' => [
Expand Down Expand Up @@ -112,6 +95,26 @@ class RecaptchaHelper extends Helper
]
];

/**
* Constructor
*
* @param View $view View
* @param array $config Config
* @return void
*/
public function __construct(View $view, $config = [])
{
parent::__construct($view, $config);

// Merge Options given by user in config/recaptcha
$configRecaptcha = Configure::read('Recaptcha');

$this->config($config);

// debug($this->config());
$this->config('secret', '');
}

/**
* Render the recaptcha div and js script.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/Component/RecaptchaComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function testInitialization()
$this->markTestIncomplete('Not implemented yet.');
}

/**
* Test StartupWithNonExistingConfigFile
*
* @return void
*/
public function testStartupWithNonExistingConfigFile()
{
Configure::config('default', new PhpConfig(TESTS . DS . 'config' . DS));
Expand All @@ -69,6 +74,11 @@ public function testStartupWithNonExistingConfigFile()
$this->fail('An expected exception has not been raised.');
}

/**
* Test StartupWithExistingConfigFile
*
* @return void
*/
public function testStartupWithExistingConfigFile()
{
Configure::config('default', new PhpConfig(TESTS . DS . 'config' . DS));
Expand All @@ -85,6 +95,11 @@ public function testStartupWithExistingConfigFile()
//$this->Recaptcha->startup();
}

/**
* Test StartupWithEmptyOptions
*
* @return void
*/
public function testStartupWithEmptyOptions()
{
Configure::config('default', new PhpConfig(TESTS . DS . 'config' . DS));
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase/View/Helper/RecaptchaHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function tearDown()
parent::tearDown();
}

/**
* testConstruct method
*
* @return void
*/
public function testConstruct()
{
$this->assertEquals([], $this->Recaptcha->config());
}

public function testDisplay()
{
$lang = 'en';
Expand Down
28 changes: 19 additions & 9 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* Test suite bootstrap for Re.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* This function is used to find the location of CakePHP whether CakePHP
* has been installed as a dependency of the plugin, or the plugin is itself
* installed as a dependency of an application.
*/
require dirname(__DIR__) . '/vendor/autoload.php';
$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);

throw new Exception("Cannot find the root of the application, unable to run tests");
};
$root = $findRoot(__FILE__);
unset($findRoot);

require dirname(__DIR__) . '/tests/paths.php';
chdir($root);
require $root . '/config/bootstrap.php';

0 comments on commit 93fe158

Please sign in to comment.