Improve usability of AssertMailTrait for debugging tests

Created on 27 August 2018, over 6 years ago
Updated 6 April 2023, almost 2 years ago

Problem/Motivation

The AssertMailTrait does not provide good error messages upon failure. It works fine when things are passing, but if a call to assertMail fails, the error message is not at all clear as to what has failed:

Failed asserting that false is true.

Additionally, if somebody wants to test more than one mail per test, then they can either manually get all the emails and find the one they want, or they can reset the internal state which stores the emails to empty. The code to reset the internal state looks like this, but requires developers to backward engineer the mail trait:

  $this->container->get('state')->set('system.test_mail_collector', []);

Proposed resolution

  • Improve the default failure messages put out in assertMail
  • Add an optional index argument to that method so mails other than the most recent can be tested
  • Add a helper method to empty out the stored emails during tests

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

10.1

Component
PHPUnit 

Last updated about 21 hours ago

Created by

🇺🇸United States jhedstrom Portland, OR

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India sahil.goyal

    rerolled to the corresponding #19 to trying to fix CCF/CI errors

  • Status changed to Needs review about 2 years ago
  • 🇺🇸United States smustgrave

    Removed rerolled tag

    Change looks good now

  • Status changed to RTBC about 2 years ago
  • Status changed to Needs work about 2 years ago
  • 🇮🇹Italy mondrake 🇮🇹
    1. +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php
      @@ -50,16 +50,56 @@ protected function getMails(array $filter = []) {
      +   * @param string $group
      +   *   An asset group.
      

      This seems unused in the method? $group existed in the past in Simpletest times, now we dropped it.

    2. +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php
      @@ -50,16 +50,56 @@ protected function getMails(array $filter = []) {
      +  protected function assertMail($name, $value = '', $message = '', $group = 'Email', $index = NULL) {
      

      Since we are here, can we typehint the parameters and/or at least the return void?

    3. +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php
      @@ -50,16 +50,56 @@ protected function getMails(array $filter = []) {
      +          ? new FormattableMarkup(
      ...
      +          : new FormattableMarkup('No email with index @index found',
      

      We now tend to avoid use of FormattableMarkup to format the assertion failure messages. Better use "No email with index {$index} found" or sprintf("No email with index %d found", $index).

    4. +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php
      @@ -50,16 +50,56 @@ protected function getMails(array $filter = []) {
      +      ? new FormattableMarkup('@message (Fails because no email found)',
      ...
      +      ? new FormattableMarkup('@message (Fails because field "@name" is not set)',
      ...
      +      : new FormattableMarkup('Field "@name" is not set', ['@name' => $name])
      

      same here

    5. +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php
      @@ -50,16 +50,56 @@ protected function getMails(array $filter = []) {
      +  protected function resetStoredEmails() {
      

      this is introduced but not used anywhere? I think we need a test, at least.

  • 🇮🇳India TanujJain-TJ

    Updating patch after addressing points from #25, although i am not sure about how to move forward with point 5. please review.

Production build 0.71.5 2024