[5.6] Testable date validation rules when comparison has relative time #23211
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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, instantiateCarbon
instead ofDateTime
orstrtotime()
.The previous date parameter behaviour is left as-is. Only when
Carbon::hasTestNow()
andCarbon::hasRelativeKeywords($value)
aretrue
will an attempt be made to parse the string for an adjusted date.Workaround forCarbon
1.20 & 1.21Given
carbon\carbon
dependency of~1.20
, this code:wasn't supported until 1.22 (pull request: briannesbitt/Carbon#634) released Jan 15, 2017. So a few
Carbon
constructor lines were needed inValidatesAttributes
to supporttoday
as a validated relative time keyword.