Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:5.6from
derekmd:testable-validation-relative-time-params
Feb 20, 2018
Merged

taylorotwell merged 1 commit into
laravel:5.6from
derekmd:testable-validation-relative-time-params

Conversation

@derekmd

@derekmd derekmd commented Feb 19, 2018 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

derekmd commented Feb 19, 2018

Copy link
Copy Markdown
Contributor Author

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

@taylorotwell

Copy link
Copy Markdown
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

derekmd commented Feb 20, 2018 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.

2 participants