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 19, 2018
1 parent 75c9680 commit b7c4220
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
28 changes: 27 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 (is_string($value) && Carbon::hasRelativeKeywords($value)) {
$date = $this->parseDateTime($value);

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

return strtotime($value);
}

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

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

/**
* @param string $value
* @return \DateTime|null
*/
protected function parseDateTime($value)
{
try {
if (Carbon::hasRelativeKeywords($value)) {
return new Carbon($value);
}

return new DateTime($value);
} catch (Exception $e) {
//
Expand Down
44 changes: 44 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,49 @@ 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'], ['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 b7c4220

Please sign in to comment.