Score:0

Functional test for emails (subject and title)

bo flag

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.

fr flag
The link in your post is a Kernel test, not a Functional test, and it tests the core EmailAction plugin. It does not test mail you send yourself via $mailManager->mail() with your own hook_mail(). We would have to see your hook_mail() and the full test function in order to help you figure out why your custom module doesn't include a body or subject. I suspect your hook_mail() is wrong - one of this mandatory things you must do in hook_mail() is to set the subject and body properly - it is not enough to pass the values to $mailManager->mail().
bo flag
Yes as I wrote the module parameter in mailmanager->mail was wrong and hook_mail was not called. Thats why the test is green but the email that was send has no body and no subject. The question here is: how can I write a test that is red because subject and body are missing ? And even more complicated: how can i write this test independet from what kind auf mail plugin ( drupal default, smyfony-/swiftmail, sesmailer,…) is installed ?
fr flag
No, the test is green because the test is testing EmailAction, and is not testing your code at all. Or at least that's what I gather from your post. You still haven't shown the test code you're using. If you want to use a Kernel test, copy something like core/modules/user/tests/src/Kernel/UserMailNotifyTest.php and modify it for your code. The basic outline of the test code is 1) setup your test environment, 2) call your function that sends mail, 3) use $this-getMails() to recover any sent mails from the buffer, then check the details of the message(s).
bo flag
So how is it possible to test hook_mail in a cutom module meaningful ? It is also never tested in core isn‘t it ?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.