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

Conversation

derekmd
Copy link
Contributor

@derekmd derekmd commented Feb 19, 2018

Change requested by: #19520

I noticed this issue recently in a feature test hitting an API endpoint and doing a HTTP request assertion equivalent to:

// Now a past date.
Carbon::setTestNow('2018-02-01');

$v = validator(['starts_on' => '2018-02-02'], ['starts_on' => 'date|after:today']);

$this->assertTrue($v->passes());

Only starts_on values after 2018-02-19 would pass validation.

Date validation rule parameters containing relative time like 'today' or 'next monday' don't respect Carbon::setTestNow(). To get a date-adjusted value when these keywords are present in a date parameter, instantiate Carbon instead of DateTime or strtotime().

The previous date parameter behaviour is left as-is. Only when Carbon::hasTestNow() and Carbon::hasRelativeKeywords($value) are true will an attempt be made to parse the string for an adjusted date.

Workaround for Carbon 1.20 & 1.21

Given carbon\carbon dependency of ~1.20, this code:

Carbon::setTestNow(new Carbon('last week'));
Carbon::parse('today');

wasn't supported until 1.22 (pull request: briannesbitt/Carbon#634) released Jan 15, 2017. So a few Carbon constructor lines were needed in ValidatesAttributes to support today as a validated relative time keyword.

@derekmd derekmd force-pushed the testable-validation-relative-time-params branch 4 times, most recently from b7c4220 to d91a0ca Compare February 19, 2018 08:12
@derekmd derekmd changed the title [5.6] Testable date validation rules when parameters have relative time [5.6] Testable date validation rules when comparison has relative time Feb 19, 2018
@derekmd derekmd force-pushed the testable-validation-relative-time-params branch 2 times, most recently from a5ae20b to 4ade081 Compare February 19, 2018 09:11
@taylorotwell
Copy link
Member

I updated Carbon dependency on laravel/framework.

@derekmd derekmd force-pushed the testable-validation-relative-time-params branch from 4ade081 to eab65c5 Compare February 19, 2018 16:35
@derekmd
Copy link
Contributor Author

derekmd commented Feb 19, 2018

Even better! The commit has been updated for just Carbon >= 1.22.

@taylorotwell
Copy link
Member

Your method hasRelativeDateKeyword isn't really a good description of what's totally happening I don't think. It also has checks regarding if Carbon has a test date in place. It does a little more than it indicates by its name.

@derekmd derekmd force-pushed the testable-validation-relative-time-params branch 2 times, most recently from 51f7094 to a4416eb Compare February 20, 2018 16:45
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().
@derekmd derekmd force-pushed the testable-validation-relative-time-params branch from a4416eb to 7bcfa90 Compare February 20, 2018 16:48
@derekmd
Copy link
Contributor Author

derekmd commented Feb 20, 2018

I sat for an unreasonable amount of time trying to think of a good name and isTestingRelativeDateTime() is the best I can come up with.

@taylorotwell
Copy link
Member

I think that makes sense.

@taylorotwell taylorotwell merged commit 7bcfa90 into laravel:5.6 Feb 20, 2018
@derekmd derekmd deleted the testable-validation-relative-time-params branch February 20, 2018 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants