Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
I was trying to improve the code coverage on DateTimePlus under PHPUnit, and discovered this:
foreach (array('hour', 'minute', 'second') as $key) {
if (array_key_exists($key, $array)) {
$value = $array[$key];
switch ($value) {
It's switching on $value instead of $key.
This leads the function to always use the default switch code path, validating against the minute/second regex instead of the hour one.
Proposed resolution
Switch on $key.
Remaining tasks
User interface changes
API changes
Related Issues
#2084477: Expand phpunit tests for \Drupal\Component\Datetime\DateTimePlus
Comment | File | Size | Author |
---|---|---|---|
#14 | interdiff.txt | 2.06 KB | Mile23 |
#14 | 2084455_14.patch | 6.1 KB | Mile23 |
#11 | 2084455_11.patch | 5.42 KB | Mile23 |
#11 | 2084455_11_test_only.patch | 3.81 KB | Mile23 |
#8 | 2084455_8.patch | 4.01 KB | Mile23 |
Comments
Comment #1
Mile23And a patch.
Comment #2
Mile23Comment #3
Mile23Better yet...
Comment #3.0
Mile23Added link to test issue
Comment #4
Mile23Comment #4.0
Mile23better link to test
Comment #7
Mile23Comment #8
Mile23This patch combines the code change with a regression test to illustrate it.
I discovered that
DateTimePlusTest::providerTestInvalidDateArrays()
isn't hooked up to any test, so I added regression testing for this issue, as well as folding in the existing data.I had to delete one of the invalid data arrays, because apparently the year 10,000 is a valid year in PHP now. :-)
Ignore the previous patches please. :-)
Comment #10
neclimdulIs this assert needed? Is it just there to provide output about the failure?
I don't feel strongly about it but this is could still sort of a valid test. The value should be 32768 though. 32767 is the documented upper limit of checkdate().
Comment #11
Mile23Historically, 10,000 is outside the allowable date range for dates, leading to the year-10000 problem. PHP fixed this, so 10000 won't throw an exception.
Good catch on
checkdate()
though, and on theassertNotNull()
. It should beassertInstanceOf()
for DateTimePlus, which is a more specific expectation. IfcreateFromArray()
doesn't throw an exception then we get an immediate indication of what it did in the fail message.Comment #13
neclimdulSorry, a couple documentation nits I missed in the last review
This could fit on a single line I'd wager.
I really dislike the settings array. If we're going to add documentation can we just say we pass it to the constructor? From what I've been able to piece together, debug isn't used anywhere ever and langcode is only used on the Core version of the class so that's the most accurate we can be here.
Comment #14
Mile23That's not how documentation works. :-)
Comment #15
neclimdulComment #16
alexpottThis issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed 357b503 and pushed to 8.0.x. Thanks!