Skip to content

Add a mail collection assert trait.

Sam requested to merge Sam152/drupal-test-traits:add-mail-assert-trait into master

I have been working on a project that deals a lot of with a series of emails to various parties. I've been working with a test trait that has proved to be quite useful, so I thought I'd propose it here.

The PR should have plenty of explanation of how to use the trait, but here is a (modified) example of how I'm using it in a real project. I've found it much easier to get a high number of quality assertions about the mail being sent using this trait.

<?php
    // The user will get a welcome email from the standard user module
    // notifications.
    $this->assertMailCollection()
      ->seekToModule('user')
      ->seekToRecipient($officer->getEmail())
      ->countEquals(1)
      ->subjectEquals('Welcome, your application is pending');
    // The group membership module will notify the two group admins that there
    // is a new user that needs approval.
    $this->assertMailCollection()
      ->seekToModule('group_membership_notifications')
      ->countEquals(2)
      ->subjectEquals('Zig Zag has requested access to the site')
      ->bodyContains('The following user has requested access to the "Foo" group.');
    // Ensure the two group admins are the recipients of those emails.
    $this->assertMailCollection()
      ->seekToRecipient($group_admin_1->getEmail())
      ->countEquals(1);
    $this->assertMailCollection()
      ->seekToRecipient($group_admin_2->getEmail())
      ->countEquals(1);

If an addition like this is outside of the scope of the traits, doesn't seem that useful or would even just be a pain to maintain, no problems. I thought given I was using this anyway I'd see what others thought.

Merge request reports