Skip to content

Commit

Permalink
Merge pull request #40 from LaurentEsc/master
Browse files Browse the repository at this point in the history
Make testing easier by adding a disable() method
  • Loading branch information
msurguy committed Oct 22, 2015
2 parents 3047f85 + 953e042 commit dec0a28
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 59 deletions.
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

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('user@email.com', '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

0 comments on commit dec0a28

Please sign in to comment.