Problem/Motivation

AssertLegacyTrait::pass is meant for removal in Drupal 10.

Proposed resolution

Deprecate the method and remove its usage in core.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#48 rawdiff_42-47.txt76.83 KBmondrake
#48 rawdiff_42-46.txt71.25 KBmondrake
#47 3123120-47-8.9.x_branch.patch148.08 KBmrinalini9
#46 3123120-46-8.9.x_branch.patch148.07 KBmrinalini9
#46 3123120-46-9.0.x_branch.patch148.24 KBmrinalini9
#43 rawdiff_36-42.txt12.1 KBmondrake
#42 3123120-42.patch150.79 KBmondrake
#38 3123120-38.patch151.09 KBridhimaabrol24
#36 3123120-36.patch151.08 KBmondrake
#35 3123120-35.patch150.74 KBmondrake
#33 interdiff_28-33.txt16.11 KBmondrake
#33 3123120-33.patch150.75 KBmondrake
#28 3123120-28.patch142.99 KBmondrake
#28 interdiff_26-28.txt974 bytesmondrake
#26 interdiff_24-26.txt3.39 KBmondrake
#26 3123120-26.patch142.99 KBmondrake
#24 3123120-24.patch141.47 KBmondrake
#24 interdiff_24-24.txt24.03 KBmondrake
#23 interdiff_22-23.txt42.18 KBmondrake
#23 3123120-23.patch121.92 KBmondrake
#22 3123120-22.patch81.8 KBmondrake
#22 interdiff_18-22.txt47.67 KBmondrake
#18 interdiff_17-18.txt13.42 KBmondrake
#18 3123120-18.patch38.44 KBmondrake
#17 3123120-17.patch25.87 KBmondrake
#14 3123120-14.patch10.85 KBmondrake
#11 3123120-11.patch8.42 KBmondrake
#10 3123120-10.patch1.07 KBmondrake
#5 interdiff_2-5.txt19.85 KBmondrake
#5 3123120-5.patch19.98 KBmondrake
#2 3123120-2.patch1.07 KBmondrake
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Let's start with a deprecation trigger and see what neeeds to be fixed.

mondrake’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 3123120-2.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
19.98 KB
19.85 KB

It's not straightforward to replace these calls. Those that are there just to log a test entry for display in Simpletest, can be just removed since they have no point in PHPUnit. More complicated is the case when they are part of a try...catch construct to assert an exception is thrown. Sometimes they can be replaced by expectException, but need to be careful.

For the time being, making changes to the test *base* classes. Maybe this needs to be spinned off in sub-issues.

Status: Needs review » Needs work

The last submitted patch, 5: 3123120-5.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Postponed

Postponed on #3123253: Remove usage of AssertLegacyTrait::pass() from base test classes, so we can do a limited size patch there and discuss.

mondrake’s picture

mondrake’s picture

#3123253: Remove usage of AssertLegacyTrait::pass() from base test classes got committed, but I'd still suggest to keep this postponed on #3114617: Properly deprecate methods in Drupal\FunctionalTests\AssertLegacyTrait and Drupal\KernelTests\AssertLegacyTrait, while still keeping deprecations silenced. Once that is in, this could be a reference type of issue to remove the silenced deprecation, all the usages, and add in a deprecation test.

mondrake’s picture

mondrake’s picture

FileSize
8.42 KB

There are uses in traits, too, that generate most of the failures.

mondrake’s picture

jungle’s picture

Issue tags: +Deprecated assertions

Adding the "Deprecated assertions" tag to add this into the kanban board https://contribkanban.com/board/Deprecatedassertions

mondrake’s picture

FileSize
10.85 KB

Testing #3135077: Remove usage of AssertLegacyTrait::pass() from traits with the deprecation silencer removed to see how many are left out.

mondrake’s picture

Status: Postponed » Active
mondrake’s picture

Assigned: Unassigned » mondrake

On this.

mondrake’s picture

Status: Active » Needs review
FileSize
25.87 KB

Removing deprecation silencer, adding deprecation test, started removing usage (complete for Database kernel tests).

mondrake’s picture

FileSize
38.44 KB
13.42 KB
longwave’s picture

  1. +++ b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php
    @@ -545,10 +545,10 @@ public function testQueryFailureInTransaction() {
    +      $this->fail('Upset query should have failed.');
    

    Let's fix upset -> upsert while we are here

  2. +++ b/core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
    @@ -202,6 +202,14 @@ public function testAssertElementNotPresent() {
    +   * @expectedDeprecation AssertLegacyTrait::pass() is deprecated in drupal:8.0.0 and is removed from drupal:10.0.0. PHPUnit interrupts a test as soon as a test assertion fails, so there is usually no need to call this method. If a test\'s logic relies on this method, refactor the test. See https://www.drupal.org/node/3129738
    

    Does ' need escaping in the annotation?

mondrake’s picture

Thanks @longwave, will take #19 in next patch.

Status: Needs review » Needs work

The last submitted patch, 18: 3123120-18.patch, failed testing. View results

mondrake’s picture

FileSize
47.67 KB
81.8 KB

#19 fixed, continued removing usages, more to do.

mondrake’s picture

mondrake’s picture

Status: Needs work » Needs review
FileSize
24.03 KB
141.47 KB

Status: Needs review » Needs work

The last submitted patch, 24: 3123120-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mondrake’s picture

Status: Needs work » Needs review
FileSize
142.99 KB
3.39 KB

This may turn out to be green?

Status: Needs review » Needs work

The last submitted patch, 26: 3123120-26.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
FileSize
974 bytes
142.99 KB

Never, never say #26... :)

mondrake’s picture

Assigned: mondrake » Unassigned

For review.

longwave’s picture

Quite a few places where the failure message can be merged straight into the fail() method call to improve test readability, a couple of other questions.

Do you think it's worth opening a followup to refactor some of "Expected exception; just continue testing." into individual test methods that test only one thing and so can use expectException?

  1. +++ b/core/modules/block_content/tests/src/Functional/BlockContentTranslationUITest.php
    @@ -120,7 +120,6 @@ protected function doTestBasicTranslation() {
         try {
           $message = 'Blocks can have translations with the same "info" value.';
           $entity->save();
    -      $this->pass($message);
         }
         catch (\Exception $e) {
           $this->fail($message);
    

    Here we can refactor away $message.

  2. +++ b/core/modules/config/tests/src/Functional/SchemaConfigListenerWebTest.php
    @@ -35,7 +35,6 @@ public function testConfigSchemaChecker() {
           $this->fail($msg);
         }
         catch (SchemaIncompleteException $e) {
    -      $this->pass($msg);
    

    Here we can refactor away $msg.

  3. +++ b/core/modules/config/tests/src/Functional/SchemaConfigListenerWebTest.php
    @@ -44,7 +43,6 @@ public function testConfigSchemaChecker() {
    -      $this->pass($msg);
    

    Same here

  4. +++ b/core/modules/config/tests/src/Functional/SchemaConfigListenerWebTest.php
    @@ -60,7 +58,6 @@ public function testConfigSchemaChecker() {
    -      $this->pass($msg);
    

    Same here

  5. +++ b/core/tests/Drupal/KernelTests/Core/Asset/LibraryDiscoveryIntegrationTest.php
    @@ -257,7 +256,7 @@ protected function assertAssetInLibrary($asset, $extension, $library_name, $sub_
    -        return $this->pass($message);
    +        return TRUE;
    

    pass() returns void but this actually fixes this method to match the docblock, so this is better!

  6. +++ b/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php
    @@ -93,8 +84,9 @@ public function register(ContainerBuilder $container) {
    -    // Determine what database backend is running, and set the skip flag.
    -    $this->skipTests = Database::getConnection()->databaseType() !== 'mysql';
    +    if (Database::getConnection()->databaseType() !== 'mysql') {
    +      $this->markTestSkipped("Skipping test since the DbDumpCommand is currently only compatible with MySql");
    +    }
    

    Much nicer :)

  7. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php
    @@ -209,8 +209,8 @@ public function testNameValidation() {
           $this->fail($message);
         }
    -    catch (ConfigNameException $e) {
    -      $this->pass($message);
    

    $message can be refactored away here.

  8. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php
    @@ -235,7 +235,6 @@ public function testNameValidation() {
    -      $this->pass($message);
         }
         catch (ConfigNameException $e) {
           $this->fail($message);
    

    Same here.

  9. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php
    @@ -253,8 +252,8 @@ public function testValueValidation() {
           $this->fail($message);
         }
    -    catch (ConfigValueException $e) {
    -      $this->pass($message);
    

    Same here.

  10. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php
    @@ -263,8 +262,8 @@ public function testValueValidation() {
           $this->fail($message);
         }
    -    catch (ConfigValueException $e) {
    -      $this->pass($message);
    

    And again.

  11. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
    @@ -566,7 +566,6 @@ public function testConfigSchemaInfoAlter() {
           $this->fail($message);
         }
         catch (ConfigSchemaAlterException $e) {
    -      $this->pass($message);
    

    $message can be refactored away here.

  12. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
    @@ -577,7 +576,6 @@ public function testConfigSchemaInfoAlter() {
           $this->fail($message);
         }
         catch (ConfigSchemaAlterException $e) {
    -      $this->pass($message);
    

    And again.

  13. +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
    @@ -588,7 +586,6 @@ public function testConfigSchemaInfoAlter() {
           $this->fail($message);
         }
         catch (ConfigSchemaAlterException $e) {
    -      $this->pass($message);
    

    And again.

  14. +++ b/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
    @@ -144,33 +144,17 @@ public function testMultipleStatementsForNewPhp() {
    -    catch (DatabaseExceptionWrapper $e) {
    -      $this->pass('PDO exception thrown for multiple statements.');
    -    }
    +    $this->expectException(DatabaseExceptionWrapper::class);
    +    Database::getConnection('default', 'default')->query('SELECT * FROM {test}; SELECT * FROM {test_people}', [], ['allow_delimiter_in_query' => TRUE]);
    

    Much cleaner!

  15. +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
    @@ -577,12 +574,11 @@ public function testInvalidSelectCount() {
         catch (\Exception $e) {
    -      $this->pass('Exception thrown for invalid query.');
    -      return;
    +      $this->assertInstanceOf(\Exception::class, $e);
    

    When wouldn't $e be an Exception inside this catch block?

  16. +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php
    @@ -129,12 +129,7 @@ public function testSelectionSettingsHandling() {
         catch (AccessDeniedHttpException $e) {
    -      if ($e->getMessage() == 'Invalid selection settings key.') {
    -        $this->pass('Invalid selection settings key throws an exception.');
    -      }
    -      else {
    -        $this->fail('Invalid selection settings key throws an exception.');
    -      }
    +      $this->assertSame('Invalid selection settings key.', $e->getMessage());
    

    Can this use expectedException and expectedExceptionMessage?

  17. +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
    @@ -68,7 +68,7 @@ protected function doTestEntityLanguageMethods($entity_type) {
           $this->fail($message);
         }
         catch (\InvalidArgumentException $e) {
    -      $this->pass($message);
    

    All the $message variables can be refactored away here too.

mondrake’s picture

Thanks @longwave, yes, a follow up could be opened for most of that.

In general, the tricky part here is that in some tests, if you just remove the $this->pass call, you end up with a test with no assertions, hence 'risky' in PHPUnit world, and failure of the test. That's the reason for #30.15, for example.

On another note, there is a subtlety in using $this->expectException. Using this method means that the test execution STOPS after the exception is raised and intercepted. For this reason, we use the try...catch block instead in some cases, that allow keeping the test running. But also, this means that any method that extends a core test and expect continuing execution after calling parent::testXXX, will silently not executing if we turn its parent to use expectException.

This to say this probably requires more discussion in a follow up.

mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs review » Needs work

will take some of #30 here, on it

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review
FileSize
150.75 KB
16.11 KB

#30:

1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 17 - Done

5 - see #3131900: Refactor assertions that assign return values to variables and #3138078: [D9.3 beta - w/c Nov 8, 2021] Add a 'void' return typehint to custom assert* methods for follow-ups

6, 14 - :)

15 - Changed to test the appropriate exception

16 - see #31

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! The expectedException vs try/catch stuff is tricky and I wish we could just continue after an exception but I also see why we can't actually do that, so let's leave that all alone for now. Otherwise all issues look to be resolved so this is RTBC for me.

mondrake’s picture

FileSize
150.74 KB

Rerolled, I thought some recent commits were impacting the patch, in fact they didn't, still I have this one freshest.

mondrake’s picture

Reroll after commit of #3135538: Replace remaining assert* involving use of count() where relevant, only patch context changes solved by 3-way merge.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs another re-roll.

ridhimaabrol24’s picture

Status: Needs work » Needs review
FileSize
151.09 KB

Rerolling for 9.1.x!

longwave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs reroll

Thanks for rerolling!

catch’s picture

Status: Reviewed & tested by the community » Needs work

Needs yet another re-roll.

On reviewing this - I agree we should open the follow-up to refactor tests that could potentially use expectException() - the current ::pass() calls inside catch are giving us the illusion of test coverage and we don't lose anything by removing them, but refactoring the tests would improve things.

mondrake’s picture

#40 filed follow-up meta #3158997: [meta] Refactor tests that use try...catch blocks to use expectException instead. I think we'll need a meta there since each case of try...catch will likely need a refactoring of its own, big work.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
150.79 KB

Rerolled.

mondrake’s picture

FileSize
12.1 KB

  • catch committed b7627bc on 9.1.x
    Issue #3123120 by mondrake, ridhimaabrol24, longwave: Properly deprecate...
catch’s picture

Title: Properly deprecate AssertLegacyTrait::pass » [backport] Properly deprecate AssertLegacyTrait::pass
Version: 9.1.x-dev » 9.0.x-dev
Status: Reviewed & tested by the community » Needs work
core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php:502:33 - Unknown word (sould)

Fixed this typo (via cspell) on commit.

It would be good to backport the test changes (but not the deprecation itself) to 9.0.x and 8.9.x

mrinalini9’s picture

Added patch for 9.0.x and 8.9.x branch, please review.

mrinalini9’s picture

Please ignore previous patch for 8.9.x branch. Here is the updated patch after fixing PHPLint issue in #46, please review.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
71.25 KB
76.83 KB

Thanks, rawdiffs look good.

catch’s picture

Version: 9.0.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed the respective patches to 9.0.x and 8.9.x, thanks!

  • catch committed fd5fbe1 on 9.0.x
    Issue #3123120 by mondrake, mrinalini9, ridhimaabrol24, longwave, catch...

  • catch committed baab371 on 8.9.x
    Issue #3123120 by mondrake, mrinalini9, ridhimaabrol24, longwave, catch...
mondrake’s picture

Title: [backport] Properly deprecate AssertLegacyTrait::pass » Properly deprecate AssertLegacyTrait::pass

Status: Fixed » Closed (fixed)

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