Problem/Motivation

While working on test coverage for #3078110: Notification emails should not send to blocked users I saw weird results at the test-only patch:

https://www.drupal.org/pift-ci-job/2139085

I was only expecting a single fail, but I got 2. The output shows the expected test failure, but twice.

Drupal\Tests\content_moderation_notifications\Kernel\TokenNotificationsTest
fail: [Other] Line 0 of sites/default/files/simpletest/phpunit-3.xml:
PHPUnit Test failed to complete; Error: PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\content_moderation_notifications\Kernel\TokenNotificationsTest
.F                                                                  2 / 2 (100%)

Time: 2.47 seconds, Memory: 4.00MB

There was 1 failure:

1) Drupal\Tests\content_moderation_notifications\Kernel\TokenNotificationsTest::testEmailDelivery
Failed asserting that actual size 7 matches expected size 6.

/var/www/html/modules/contrib/content_moderation_notifications/tests/src/Kernel/NotificationsTest.php:163

Huh? Oh, then I noticed:

class TokenNotificationsTest extends NotificationsTest

Whoops. That means every test / assertion added to NotificationsTest gets run twice:

  • Once as NotificationsTest
  • Again as TokenNotificationsTest

This wastes time / effort / DA $$ for testbot cycles, and carbon footprint.

Steps to reproduce

  1. Add a failing test to NotificationsTest
  2. Run all the Kernel tests.
  3. Watch it fail twice, not once.

Proposed resolution

Option A: Use the trait

We've already got a Trait for shared test code (ContentModerationNotificationTestTrait). Put stuff that should actually be shared in there. Make both existing Kernel tests extend KernelTestBase.

Option B: Create a ContentModerationNotificationKernelTestBase class

Create an abstract base class with shared stuff, including common modules, setUp() steps, etc.
Make both Kernel tests extend that.

Remaining tasks

  1. Decide which approach we prefer.
  2. Implement it.
  3. Verify the tests still pass.
  4. Make sure we don't get duplicate test results if something fails in NotificationTest.
  5. Reviews / refinements
  6. RTBC
  7. Commit

User interface changes

Nope.

API changes

Only to the Kernel test classes, which isn't an API.

Data model changes

Nope.

CommentFileSizeAuthor
#4 3226605-4.optionA.patch3.6 KBdww
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dww created an issue. See original summary.

dww’s picture

Issue summary: View changes
dww’s picture

Status: Active » Needs review

Here's option A in patch form. Not sure I love it, but it's a start. ;)

I discovered that we only care about filter_test because it creates a 'filtered_html' text format "for free" if we enable it and install its config. Only TokenNotificationsTest cares about that, so moved that as part of the cleanup (and added a comment for the next person looking at these tests).

option B might be cleaner, but then it's not clear we even need tests/src/Kernel/ContentModerationNotificationTestTrait.php anymore. tests/src/Functional/Form/CrudFormTest.php uses it, but never calls enableModeration().

Probably CrudFormTest should just use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait directly, we should remove ContentModerationNotificationTestTrait, and have a ContentModerationNotificationKernelTestBase to share code across the Kernel test classes... 🤔

dww’s picture

StatusFileSize
new3.6 KB

Whoops, attaching the patch would help. 🤦‍♂️😉

bkosborne made their first commit to this issue’s fork.

bkosborne’s picture

Category: Bug report » Task
Status: Needs review » Fixed

I was also confused by this. Thanks for starting the work on this. I took it further in an MR and will add you to issue credits. Thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • bkosborne committed 60a46dad on 8.x-3.x
    [#3226605] task: TokenNotificationsTest should not extend...

Status: Fixed » Closed (fixed)

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