Question: How can a Drupal Functional Test (extenting BrowserTestBase) check for a certain Mail-Subject and Mail-Body defined in hook_mail in a custom module ?
Context:
I have a custom module which sends an email to certain people in case a new node of a certain bundle is created.
There is a Functional Test (BrowserTestbase) for this in the same way the User Module is doing it for Core:
https://api.drupal.org/api/drupal/core%21tests%21Drupal%21KernelTests%21Core%21Action%21EmailActionTest.php/function/EmailActionTest%3A%3AtestEmailAction/8.9.x
This test is green. Subject and Body are Checked after
$captured_emails = $this->drupalGetMails();
$this->assertEqual($captured_emails[0]['params']['subject'],"My desired Subject");
$this->assertEqual($captured_emails[0]["params"]["body"], "My desired Body");
BUT the actual Mails which are delivered do not have a subject AND do not have a body ! How is that possible ? I made a mistake in the custom module when I wanted to send the Email:
$mailManager->mail('my_custom', $key, $recipient, $langcode, $params, $from_email);
The first Parameter value was wrong. Like the module is named "my_awesome" but not "my_custom". In this case Drupal won't call the hook_mail Implementation in my_awesome_mail therefore the $message Object will not be filled with subject and body. There are no complains or exceptions. The Test is green the mail is empty.