Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jungle created an issue. See original summary.

jungle’s picture

Title: Replace assertions involving calls to is_numeric() assertIsNumeric()/assertIsNotNumeric() » Replace assertions involving calls to is_numeric() with assertIsNumeric()/assertIsNotNumeric()
quietone’s picture

I used this egrep -r ".*assert.*is_numeric" core | awk -F: '{print $1}' | nl to find lines to change.

quietone’s picture

Status: Active » Needs review

NR for testing.

jungle’s picture

Status: Needs review » Needs work

Thanks, @quietone!

Checked with regex assert.+is_numeric, no more assertions to replace. But setting back to NW to remove the assertion messages. See #3130811: Remove redundant $message from assert(Not)InstanceOf calls and #3131946: [META] [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages for more.

+++ b/core/modules/block_content/tests/src/Functional/BlockContentRevisionsTest.php
@@ -81,8 +81,8 @@ public function testRevisions() {
+        $this->assertIsNumeric($loaded->getRevisionUserId(), 'Revision User ID found.');
+        $this->assertIsNumeric($loaded->getRevisionCreationTime(), 'Revision time found.');

+++ b/core/modules/history/tests/src/Functional/HistoryTest.php
@@ -119,7 +119,7 @@ public function testHistory() {
+    $this->assertIsNumeric($timestamp, 'Node has been marked as read. Timestamp received.');

+++ b/core/modules/jsonapi/src/Access/TemporaryQueryGuard.php
@@ -169,7 +169,7 @@ protected static function secureQuery(QueryInterface $query, $entity_type_id, ar
+          assertIsNumeric($property_name, 'The specifier is not a property name, it must be a delta.');

+++ b/core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
@@ -286,7 +286,8 @@ public function testModuleMetaData() {
+    $this->assertIsNumeric($test_mtime, 'The system.info.yml file modification time field contains a timestamp.');
+    $this->assertTrue(($test_mtime > 0), 'The system.info.yml file modification time field is greater than 0.');

@@ -317,7 +318,8 @@ public function testThemeMetaData() {
+    $this->assertIsNumeric($test_mtime, 'The bartik.info.yml file modification time field contains a timestamp.');
+    $this->assertTrue(($test_mtime > 0), 'The bartik.info.yml file modification time field is grater than 0.');

+++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
@@ -316,7 +316,8 @@ public function providerSortByTitleProperty() {
+    $this->assertIsNumeric($expected, 'Expected value is numeric.');
+    $this->assertIsNumeric($result, 'Result value is numeric.');
Suresh Prabhu Parkala’s picture

Status: Needs work » Needs review
FileSize
5.77 KB
4.61 KB

Please review the patch.

The last submitted patch, 3: 3131817-3.patch, failed testing. View results

quietone’s picture

Oh my, I was commenting and uploading a my new patch when I found out it was already done. @Suresh Prabhu Parkala, to avoid more than one person working on a patch please assign it to yourself. Or, in this case, since it was only an hour since I uploaded my patch maybe ask first or wait a day?

@jungle, I have looked at the two issues referred to plus the Meta for this one and I don't see any decision in those issues that states that messages are to be moved from all assertions. This isn't an area I know much about, maybe that requirement should be added to the Meta?

jungle’s picture

@quietone, yes, it's still unclear. But #3130811 got committed already which did remove assertion messages. and @catch as the committer said before +1 to doing so.

Setting back to NW, as testing in #3 failed, and #6 should not pass

jungle’s picture

Status: Needs review » Needs work
quietone’s picture

jungle’s picture

Status: Needs review » Reviewed & tested by the community

LGTM, thanks @quietone!

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
@@ -316,8 +316,9 @@ public function providerSortByTitleProperty() {

-    $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are either both negative, both positive or both zero.');
+    $this->assertIsNumeric($expected);

I think the assertion message here is worth converting into a comment - it's a long comparison to scan.

jungle’s picture

Status: Needs work » Reviewed & tested by the community

Thank you @catch!

  /**
   * Asserts that numbers are either both negative, both positive or both zero. // ①
   *
   * The exact values returned by comparison functions differ between PHP
   * versions and are considered an "implementation detail".
   *
   * @param int $expected
   *   Expected comparison function return value.
   * @param int $result
   *   Actual comparison function return value.
   */
  protected function assertBothNegativePositiveOrZero($expected, $result) {
+  // Numbers are either both negative, both positive or both zero. // ②
    $this->assertIsNumeric($expected);
    $this->assertIsNumeric($result);
+  // Numbers are either both negative, both positive or both zero. // ③
    $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0));
  }

Checking the whole method, found that if we add an inline comment to ② or ③, it is redundant with the method level comment ① Asserts that numbers are either both negative, both positive or both zero.

I am setting back to RTBC, if you think it's still necessary, I am happy to submit a new patch.

jungle’s picture

Status: Reviewed & tested by the community » Needs review

Setting back to NW waiting for testings finishing running, and checking next if a new patch is necessary for 8.x

daffie’s picture

Status: Needs review » Needs work

The method assertIsNumeric() does not exist for Drupal 8 and the patch does not apply for 8.8.

jungle’s picture

daffie’s picture

daffie’s picture

Patch looks good for 9.0. Just 1 nitpick:

+++ b/core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
@@ -286,7 +286,8 @@ public function testModuleMetaData() {
+    $this->assertTrue(($test_mtime > 0));

@@ -317,7 +318,8 @@ public function testThemeMetaData() {
+    $this->assertTrue(($test_mtime > 0));

Double round brackets are not needed.

xjm’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Postponed » Needs work

The shim is committed to 8.9.x, which might be as far as it goes. So this can continue.

mondrake’s picture

  1. +++ b/core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
    @@ -286,7 +286,8 @@ public function testModuleMetaData() {
    +    $this->assertTrue(($test_mtime > 0));
    
    @@ -317,7 +318,8 @@ public function testThemeMetaData() {
    +    $this->assertTrue(($test_mtime > 0));
    

    These can be $this->assertGreaterThan(0, $test_mtime);

  2. +++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
    @@ -316,8 +316,9 @@ public function providerSortByTitleProperty() {
    +    $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0));
    

    Removal of the custom $message is out of scope of this patch. (It could be part of a general issue chain to remove redundant messages, but not here specifically)

quietone’s picture

Status: Needs work » Needs review
FileSize
1.08 KB
4.92 KB

Fixes for #19 and #21.1.

#21,2. No change. In #5 it was requested that messages are to be removed. See also #8 and #9.

mondrake’s picture

Re. #21.2, in this case this is in an assert method. If you drop the message entirely, all you'll get in case of test failure will be

Failed asserting that false is true.

which is very obscure since you were calling $this->assertBothNegativePositiveOrZero($expected, $result);.

So probably we may take PHPUnit's way here and come up with sth like

Failed asserting that both numbers are negative, or both positive, or both zero.

But again IMHO this is out of scope in this issue.

Status: Needs review » Needs work

The last submitted patch, 22: 3131817-22.patch, failed testing. View results

quietone’s picture

Yes, I see. This fixes 21.2

And for my own sanity I see that was introduced in #6 and I missed it.

quietone’s picture

Arg, I didn't mean to abort the test.

mondrake’s picture

+++ b/core/tests/Drupal/Tests/Component/Utility/SortArrayTest.php
@@ -316,7 +316,8 @@ public function providerSortByTitleProperty() {
    *   Actual comparison function return value.
    */
   protected function assertBothNegativePositiveOrZero($expected, $result) {
-    $this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');
+    $this->assertIsNumeric($expected);
+    $this->assertIsNumeric($result);
     $this->assertTrue(($expected < 0 && $result < 0) || ($expected > 0 && $result > 0) || ($expected === 0 && $result === 0), 'Numbers are either both negative, both positive or both zero.');
   }
 

This is a testing a component, and extending the test directly from PHPUnit\Framework\TestCase, so by itself does not have the logic to load the compatibility methods.

To get this running on D8.9, we'll have to add the trait

/**
 * Tests the SortArray component.
 *
 * @group Utility
 *
 * @coversDefaultClass \Drupal\Component\Utility\SortArray
 */
class SortArrayTest extends TestCase {

  use PhpunitCompatibilityTrait;

or, just leave this untouched in D8.9.

mondrake’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs review » Reviewed & tested by the community

I think this is OK for D9 where it’s not been committed, yet. Needs a specific D8.9 patch.

quietone’s picture

Thanks. Decided to make a patch for 8.9.x. The 9.1.x patch here is the same as the one in #25, just renamed.

xjm’s picture

Alright, I'm OK with the comment from #14 and looked at the method myself, and I'm confortable since the method is so short and it's documented in the docblock that that is what it does.

Saving credit while I've pushed 9.1.x only to do an experiment about a problem I'm getting with crossposts from the bot...

  • xjm committed f6e4b88 on 9.1.x
    Issue #3131817 by quietone, Suresh Prabhu Parkala, jungle, mondrake,...

  • xjm committed f80a05c on 9.0.x
    Issue #3131817 by quietone, Suresh Prabhu Parkala, jungle, mondrake,...

  • xjm committed b1a6665 on 8.9.x
    Issue #3131817 by quietone, Suresh Prabhu Parkala, jungle, mondrake,...
xjm’s picture

Version: 9.1.x-dev » 8.9.x-dev

Committed to 9.1.x, 9.0.x, and 8.9.x. Thanks!

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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