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
SortArrayTest has several test fails in PHP7 as string comparison functions may return different values:
1) Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement with data set #1 (array(), array('test'), -4)
Failed asserting that -1 matches expected -4.
core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php:190
2) Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement with data set #2 (array('test'), array(), 4)
Failed asserting that 1 matches expected 4.
core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php:190
3) Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty with data set #1 (array(), array('test'), -4)
Failed asserting that -1 matches expected -4.
core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php:259
4) Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty with data set #2 (array('test'), array(), 4)
Failed asserting that 1 matches expected 4.
core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php:259
Proposed resolution
Instead of testing that input and expected values are exactly the same, test that they are either both positive, both negative or both zero.
Remaining tasks
Write patch
Review patch
User interface changes
None
API changes
None
Comment | File | Size | Author |
---|---|---|---|
#11 | interdiff-7-11.txt | 946 bytes | martin107 |
#11 | 2461773-sorttests-11.patch | 2.46 KB | martin107 |
#7 | interdiff.txt | 1.16 KB | stefan.r |
#7 | 2461773-sorttests-7.patch | 2.46 KB | stefan.r |
Comments
Comment #1
stefan.r CreditAttribution: stefan.r commentedComment #2
stefan.r CreditAttribution: stefan.r commentedComment #3
BerdirCan we add a comment to that method that the exact values differ between PHP versions and are apparently considered an implementation detail, so that's it is not changed again two years later ;)
Comment #4
stefan.r CreditAttribution: stefan.r commentedComment #5
BerdirI've verified that this fixes 4 phpunit test fails that currently happen with PHP7. Comment looks good to me.
Comment #6
alexpottDo we need the is_numeric test? If we do need this test then
$this->fail('Parameters are not numeric.');
is better. Also the return should be on a different line. Or even remove the if and just do$this->assertTrue(is_numeric($expected) && is_numeric($result), 'Some message');
This can be collapsed into a single assertion. I.e.
Comment #7
stefan.r CreditAttribution: stefan.r commentedComment #8
stefan.r CreditAttribution: stefan.r commentedComment #9
BerdirThat works too ;)
Comment #10
alexpottI think the word "not" is incorrect. We are asserting the both number are both negative, both positive or both equal to 0.
Comment #11
martin107 CreditAttribution: martin107 commentedYep, language was wonky.
Comment #12
stefan.r CreditAttribution: stefan.r commentedVerbiage was from a previous version of the patch, looks good now!
Comment #13
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 a87a166 and pushed to 8.0.x. Thanks!
The @return docs are not needed since we're not returning anything and PHPUnit assertions use exceptions anyway. Fixed on commit.