Skip to content

Commit dec0a28

Browse files
committed
Merge pull request #40 from LaurentEsc/master
Make testing easier by adding a disable() method
2 parents 3047f85 + 953e042 commit dec0a28

File tree

5 files changed

+92
-59
lines changed

5 files changed

+92
-59
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ Please note that "honeytime" takes a parameter specifying number of seconds it s
6969

7070
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)
7171

72+
## Testing
73+
74+
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:
75+
76+
Honeypot::disable();
77+
78+
$this->visit('contact')
79+
->type('User', 'name')
80+
->type('[email protected]', 'email')
81+
->type('Hello World', 'message')
82+
->press('submit')
83+
->see('Your message has been sent!');
84+
7285
## Credits
7386

7487
Based on work originally created by Ian Landsman: <https://github.com/ianlandsman/Honeypot>

src/Msurguy/Honeypot/Honeypot.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44

55
class Honeypot {
66

7+
protected $disabled = false;
8+
9+
/**
10+
* Enable the Honeypot validation
11+
*/
12+
public function enable()
13+
{
14+
$this->disabled = false;
15+
}
16+
17+
/**
18+
* Disable the Honeypot validation
19+
*/
20+
public function disable()
21+
{
22+
$this->disabled = true;
23+
}
24+
725
/**
826
* Generate a new honeypot and return the form HTML
927
* @param string $honey_name
@@ -23,6 +41,44 @@ public function generate($honey_name, $honey_time)
2341
return $html;
2442
}
2543

44+
/**
45+
* Validate honeypot is empty
46+
*
47+
* @param string $attribute
48+
* @param mixed $value
49+
* @param array $parameters
50+
* @return boolean
51+
*/
52+
public function validateHoneypot($attribute, $value, $parameters)
53+
{
54+
if ($this->disabled) {
55+
return true;
56+
}
57+
58+
return $value == '';
59+
}
60+
61+
/**
62+
* Validate honey time was within the time limit
63+
*
64+
* @param string $attribute
65+
* @param mixed $value
66+
* @param array $parameters
67+
* @return boolean
68+
*/
69+
public function validateHoneytime($attribute, $value, $parameters)
70+
{
71+
if ($this->disabled) {
72+
return true;
73+
}
74+
75+
// Get the decrypted time
76+
$value = $this->decryptTime($value);
77+
78+
// The current time should be greater than the time the form was built + the speed option
79+
return ( is_numeric($value) && time() > ($value + $parameters[0]) );
80+
}
81+
2682
/**
2783
* Get encrypted time
2884
* @return string
@@ -32,4 +88,23 @@ public function getEncryptedTime()
3288
return Crypt::encrypt(time());
3389
}
3490

91+
/**
92+
* Decrypt the given time
93+
*
94+
* @param mixed $time
95+
* @return string|null
96+
*/
97+
public function decryptTime($time)
98+
{
99+
// Laravel will throw an uncaught exception if the value is empty
100+
// We will try and catch it to make it easier on users.
101+
try {
102+
return Crypt::decrypt($time);
103+
}
104+
catch (\Illuminate\Encryption\DecryptException $exception)
105+
{
106+
return null;
107+
}
108+
}
109+
35110
}

src/Msurguy/Honeypot/HoneypotServiceProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public function boot()
4949
$translator = $app['translator'];
5050

5151
// Add honeypot and honeytime custom validation rules
52-
$validator->extend('honeypot', 'Msurguy\Honeypot\HoneypotValidator@validateHoneypot', $translator->get('honeypot::validation.honeypot'));
53-
$validator->extend('honeytime', 'Msurguy\Honeypot\HoneypotValidator@validateHoneytime', $translator->get('honeypot::validation.honeytime'));
52+
$validator->extend('honeypot', 'honeypot@validateHoneypot', $translator->get('honeypot::validation.honeypot'));
53+
$validator->extend('honeytime', 'honeypot@validateHoneytime', $translator->get('honeypot::validation.honeytime'));
54+
5455
});
5556
}
5657

src/Msurguy/Honeypot/HoneypotValidator.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

tests/HoneypotValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class HoneypotValidatorTest extends \PHPUnit_Framework_TestCase {
88

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

1414
/** @test */

0 commit comments

Comments
 (0)