Support system.test_mail_collector

Created on 31 October 2023, 8 months ago
Updated 1 November 2023, 8 months ago

Problem/Motivation

Some tests use test_mail_collector to check the message of the emails, example https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/modules/sys...

  public function testCancelMessage() {
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();

    // Reset the state variable that holds sent messages.
    \Drupal::state()->set('system.test_mail_collector', []);

    // Send a test message that mail_cancel_test_alter should cancel.
    \Drupal::service('plugin.manager.mail')->mail('mail_cancel_test', 'cancel_test', 'cancel@example.com', $language_interface->getId());
    // Retrieve sent message.
    $captured_emails = \Drupal::state()->get('system.test_mail_collector');
    $sent_message = end($captured_emails);

    // Assert that the message was not actually sent.
    // Message was canceled.
    $this->assertFalse($sent_message);
  }

The example test is a kernel test, and will not use Symfony Mailer.

Example of how to set up the test_mail_collector as default mail sender.

    $config->getEditable('system.mail')
      ->set('interface.default', 'test_mail_collector')
      ->save();

Because Symfony Mailer overrides the core plugin.manager.mail https://git.drupalcode.org/project/symfony_mailer/-/blob/1.x/src/Symfony...

  public function alter(ContainerBuilder $container) {
    $definition = $container->getDefinition('plugin.manager.mail');
    // Cancel any method calls, for example from mailsystem.
    $definition->setClass('Drupal\symfony_mailer\MailManagerReplacement')
      ->addArgument(new Reference('email_factory'))
      ->addArgument(new Reference('plugin.manager.email_builder'))
      ->addArgument(new Reference('symfony_mailer.legacy_helper'))
      ->setMethodCalls([]);
  }

The 'system.mail' configuration will be ignored because the MailManagerReplacement do not take into account this configuration, see Core Mail Manager https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/...

$configuration = $this->configFactory->get('system.mail')->get('interface');

We need this service for testing purposes.

Proposed resolution

Add a plugin compatible with system.mail configuration or at least with test_mail_collector on Symfony Mailer.

πŸ“Œ Task
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024