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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stefan.r’s picture

FileSize
2.49 KB
stefan.r’s picture

Status: Active » Needs review
FileSize
2.49 KB
Berdir’s picture

Can 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 ;)

stefan.r’s picture

FileSize
2.63 KB
Berdir’s picture

Status: Needs review » Reviewed & tested by the community

I've verified that this fixes 4 phpunit test fails that currently happen with PHP7. Comment looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
    @@ -309,4 +309,28 @@ public function providerSortByTitleProperty() {
    +    if (!is_numeric($expected) || !is_numeric($result)) {
    +      return $this->assertTrue(FALSE, 'Parameters are not numeric.');
    +    }
    

    Do 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');

  2. +++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
    @@ -309,4 +309,28 @@ public function providerSortByTitleProperty() {
    +    if (($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0))  {
    +      return $this->assertTrue(TRUE, 'Numbers are either both negative, both positive or both zero.');
    +    }
    +    $this->assertTrue(FALSE, 'Numbers are not either both negative, both positive or both zero.');
    

    This can be collapsed into a single assertion. I.e.

    $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are not either both negative, both positive or both zero.');
    
stefan.r’s picture

FileSize
2.46 KB
1.16 KB
stefan.r’s picture

Status: Needs work » Needs review
Berdir’s picture

Status: Needs review » Reviewed & tested by the community

That works too ;)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
@@ -324,13 +324,8 @@
+    $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are not either both negative, both positive or both zero.');

I think the word "not" is incorrect. We are asserting the both number are both negative, both positive or both equal to 0.

martin107’s picture

Status: Needs work » Needs review
FileSize
2.46 KB
946 bytes

Yep, language was wonky.

stefan.r’s picture

Status: Needs review » Reviewed & tested by the community

Verbiage was from a previous version of the patch, looks good now!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This 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!

diff --git a/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
index 7720e30..4411095 100644
--- a/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
@@ -319,9 +319,6 @@ public function providerSortByTitleProperty() {
    *   Expected comparison function return value.
    * @param int $result
    *   Actual comparison function return value.
-   *
-   * @return bool
-   *   TRUE if the assertion passed; FALSE otherwise.
    */
   protected function assertBothNegativePositiveOrZero($expected, $result) {
     $this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');

The @return docs are not needed since we're not returning anything and PHPUnit assertions use exceptions anyway. Fixed on commit.

  • alexpott committed a87a166 on 8.0.x
    Issue #2461773 by stefan.r, martin107: SortArrayTest has hardcoded...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.