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

[5.6] Testable date validation rules when comparison has relative time #23211

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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