Skip to content

Commit

Permalink
make validation time params testable
Browse files Browse the repository at this point in the history
Date validation rule parameters containing
relative time like 'today' or 'next monday'
do not respective Carbon::setTestNow(). To
get a date-adjusted value when these keywords
are present in a date parameter, instantiate
Carbon over DateTime or strtotime().
  • Loading branch information
derekmd committed Feb 20, 2018
1 parent c38b447 commit 7bcfa90
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rules\Exists;
use Illuminate\Validation\Rules\Unique;
use Illuminate\Validation\ValidationData;
Expand Down Expand Up @@ -180,7 +181,19 @@ protected function getDateFormat($attribute)
*/
protected function getDateTimestamp($value)
{
return $value instanceof DateTimeInterface ? $value->getTimestamp() : strtotime($value);
if ($value instanceof DateTimeInterface) {
return $value->getTimestamp();
}

if ($this->isTestingRelativeDateTime($value)) {
$date = $this->getDateTime($value);

if (! is_null($date)) {
return $date->getTimestamp();
}
}

return strtotime($value);
}

/**
Expand Down Expand Up @@ -214,13 +227,41 @@ protected function getDateTimeWithOptionalFormat($format, $value)
return $date;
}

return $this->getDateTime($value);
}

/**
* Get a DateTime instance from a string with no format.
*
* @param string $value
* @return \DateTime|null
*/
protected function getDateTime($value)
{
try {
if ($this->isTestingRelativeDateTime($value)) {
return new Carbon($value);
}

return new DateTime($value);
} catch (Exception $e) {
//
}
}

/**
* Check if the given value should be adjusted to Carbon::getTestNow().
*
* @param mixed $value
* @return bool
*/
protected function isTestingRelativeDateTime($value)
{
return Carbon::hasTestNow() && is_string($value) && (
$value === 'now' || Carbon::hasRelativeKeywords($value)
);
}

/**
* Validate that an attribute contains only alphabetic characters.
*
Expand Down
47 changes: 47 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ValidationValidatorTest extends TestCase
{
public function tearDown()
{
Carbon::setTestNow();
m::close();
}

Expand Down Expand Up @@ -2479,6 +2480,52 @@ public function testDateEquals()
$this->assertTrue($v->fails());
}

public function testDateEqualsRespectsCarbonTestNowWhenParameterIsRelative()
{
date_default_timezone_set('UTC');
$trans = $this->getIlluminateArrayTranslator();
Carbon::setTestNow(new Carbon('2018-01-01'));

$v = new Validator($trans, ['x' => '2018-01-01 00:00:00'], ['x' => 'date_equals:now']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:today']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:yesterday']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:tomorrow']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:today']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:yesterday']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:tomorrow']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:today']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:yesterday']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:tomorrow']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:today|after:yesterday|before:tomorrow']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:yesterday']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:tomorrow']);
$this->assertTrue($v->fails());
}

public function testBeforeAndAfter()
{
date_default_timezone_set('UTC');
Expand Down

0 comments on commit 7bcfa90

Please sign in to comment.