Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make testing easier by adding a disable() method #40

Merged
merged 1 commit into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ Please note that "honeytime" takes a parameter specifying number of seconds it s

That's it! Enjoy getting less spam in your inbox. If you need stronger spam protection, consider using [Akismet](https://github.com/kenmoini/akismet) or [reCaptcha](https://github.com/dontspamagain/recaptcha)

## Testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs updating


If you want to test the submission of a form using this package, you might want to disable Honeypot so that the validation passes. To do so, simply call the `disable()` method in your test:

Honeypot::disable();

$this->visit('contact')
->type('User', 'name')
->type('[email protected]', 'email')
->type('Hello World', 'message')
->press('submit')
->see('Your message has been sent!');

## Credits

Based on work originally created by Ian Landsman: <https://github.com/ianlandsman/Honeypot>
Expand Down
75 changes: 75 additions & 0 deletions src/Msurguy/Honeypot/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

class Honeypot {

protected $disabled = false;

/**
* Enable the Honeypot validation
*/
public function enable()
{
$this->disabled = false;
}

/**
* Disable the Honeypot validation
*/
public function disable()
{
$this->disabled = true;
}

/**
* Generate a new honeypot and return the form HTML
* @param string $honey_name
Expand All @@ -23,6 +41,44 @@ public function generate($honey_name, $honey_time)
return $html;
}

/**
* Validate honeypot is empty
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return boolean
*/
public function validateHoneypot($attribute, $value, $parameters)
{
if ($this->disabled) {
return true;
}

return $value == '';
}

/**
* Validate honey time was within the time limit
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return boolean
*/
public function validateHoneytime($attribute, $value, $parameters)
{
if ($this->disabled) {
return true;
}

// Get the decrypted time
$value = $this->decryptTime($value);

// The current time should be greater than the time the form was built + the speed option
return ( is_numeric($value) && time() > ($value + $parameters[0]) );
}

/**
* Get encrypted time
* @return string
Expand All @@ -32,4 +88,23 @@ public function getEncryptedTime()
return Crypt::encrypt(time());
}

/**
* Decrypt the given time
*
* @param mixed $time
* @return string|null
*/
public function decryptTime($time)
{
// Laravel will throw an uncaught exception if the value is empty
// We will try and catch it to make it easier on users.
try {
return Crypt::decrypt($time);
}
catch (\Illuminate\Encryption\DecryptException $exception)
{
return null;
}
}

}
5 changes: 3 additions & 2 deletions src/Msurguy/Honeypot/HoneypotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public function boot()
$translator = $app['translator'];

// Add honeypot and honeytime custom validation rules
$validator->extend('honeypot', 'Msurguy\Honeypot\HoneypotValidator@validateHoneypot', $translator->get('honeypot::validation.honeypot'));
$validator->extend('honeytime', 'Msurguy\Honeypot\HoneypotValidator@validateHoneytime', $translator->get('honeypot::validation.honeytime'));
$validator->extend('honeypot', 'honeypot@validateHoneypot', $translator->get('honeypot::validation.honeypot'));
$validator->extend('honeytime', 'honeypot@validateHoneytime', $translator->get('honeypot::validation.honeytime'));

});
}

Expand Down
56 changes: 0 additions & 56 deletions src/Msurguy/Honeypot/HoneypotValidator.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/HoneypotValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HoneypotValidatorTest extends \PHPUnit_Framework_TestCase {

public function setUp()
{
$this->validator = Mockery::mock('Msurguy\Honeypot\HoneypotValidator[decryptTime]');
$this->validator = Mockery::mock('Msurguy\Honeypot\Honeypot[decryptTime]');
}

/** @test */
Expand Down