Skip to content
Snippets Groups Projects
Commit c94daffa authored by Elaman Imashov's avatar Elaman Imashov Committed by Ev Maslovskiy
Browse files

Issue #3372456 by elaman, Spleshka: Allow replacing the recipients of the...

Issue #3372456 by elaman, Spleshka: Allow replacing the recipients of the emails by settings a cookie for testing
parent c016230a
No related branches found
No related tags found
1 merge request!25Issue #3372456: Allow replacing the recipients of the emails by settings a cookie for testing
...@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. ...@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.3.3]
- #3372456: Allow replacing the recipients of the emails by settings a cookie for testing
## [3.3.2] ## [3.3.2]
- #3338569: Ensure all entities with an owner gracefully fallback to anonymous owner after truncating all user tables - #3338569: Ensure all entities with an owner gracefully fallback to anonymous owner after truncating all user tables
......
...@@ -53,6 +53,7 @@ Features ...@@ -53,6 +53,7 @@ Features
* [Emails rerouting for development environments](#reroute-email) * [Emails rerouting for development environments](#reroute-email)
* [Blocking super-admin](#blocking-super-admin) * [Blocking super-admin](#blocking-super-admin)
* [Tests users management for development and production environments](#test-users) * [Tests users management for development and production environments](#test-users)
* [Test email delivery using MailSlurp](#test-email-delivery-using-mailslurp)
* [Display emails as Drupal messages](#display-emails-as-drupal-messages) * [Display emails as Drupal messages](#display-emails-as-drupal-messages)
* [Display SMS as Drupal messages](#display-sms-as-drupal-messages) * [Display SMS as Drupal messages](#display-sms-as-drupal-messages)
* [Display message on all pages](#display-message-on-all-pages) * [Display message on all pages](#display-message-on-all-pages)
...@@ -183,6 +184,37 @@ $config['domino.settings']['test_users_additional_users'] = [ ...@@ -183,6 +184,37 @@ $config['domino.settings']['test_users_additional_users'] = [
]; ];
``` ```
### Test email delivery using MailSlurp
Domino allows you to test email delivery using MailSlurp service. In order to use it, first create a MailSlurp account.
Then add the api key provided by the service and enable the feature in `settings.php` file:
```yaml
// Reroute all emails to a MailSlurp inbox specified by a cookie.
$config['domino.settings']['test_email_delivery'] = TRUE;
$config['domino.settings']['mailslurp_key'] = getenv("MAILSLURP_KEY");
```
In your acceptance tests set `domino_test_email_inbox` cookie. For example:
```php
// Test email delivery using MailSlurp.
$apiKey = getenv("MAILSLURP_KEY");
$I->assertNotEmpty($apiKey, "MAILSLURP_KEY environmental variable should be set to test email delivery.");
$config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKey('x-api-key', $apiKey);
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $config);
$inbox = $inboxController->createInbox();
// Let domino know we want to test email.
$I->setCookie('domino_test_email_inbox', $inbox->getId());
// Submit contact the form as usual.
// ...
// Wait for the email to be received.
$waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $config);
$email = $waitForController->waitForLatestEmail($inbox->getId(), 6000, true);
$I->assertTrue($email instanceof MailSlurp\Models\Email, "Email sent by the website wasn't received.");
$inboxController->deleteInbox($inbox->getId());
```
### Display emails as Drupal messages ### Display emails as Drupal messages
This feature is particularly useful for writing automated tests. When it's activated, then on each email sending This feature is particularly useful for writing automated tests. When it's activated, then on each email sending
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
"require": { "require": {
"drupal/core": "^9 || ^10", "drupal/core": "^9 || ^10",
"drupal/config_split": "^2", "drupal/config_split": "^2",
"drupal/reroute_email": "^2.1" "drupal/reroute_email": "^2.1",
"mailslurp/mailslurp-client-php": "^15"
} }
} }
application_mode: production application_mode: production
display_emails_as_messages: 0 display_emails_as_messages: 0
mailslurp_key: ''
super_admin_user_check_frequency: 3600 super_admin_user_check_frequency: 3600
test_email_delivery: 0
test_users_active_on_production: { } test_users_active_on_production: { }
test_users_additional_users: { } test_users_additional_users: { }
test_users_check_frequency: 3600 test_users_check_frequency: 3600
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
use Drupal\domino\ApplicationInterface; use Drupal\domino\ApplicationInterface;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\user\EntityOwnerInterface; use Drupal\user\EntityOwnerInterface;
use Drupal\Component\Utility\Html;
/** /**
* Implements hook_cache_flush(). * Implements hook_cache_flush().
...@@ -48,6 +49,26 @@ function domino_cache_flush() { ...@@ -48,6 +49,26 @@ function domino_cache_flush() {
function domino_mail_alter(&$message) { function domino_mail_alter(&$message) {
$config = \Drupal::config('domino.settings'); $config = \Drupal::config('domino.settings');
// Debug emails by rerouting all emails to the MailSlurp inbox specified in the cookie.
if ($config->get('test_email_delivery')) {
if ($inbox = \Drupal::request()->cookies->get('domino_test_email_inbox')) {
try {
// Fetch the email address based on the MailSlurp inbox id.
$config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKey('x-api-key', $config->get('mailslurp_key'));
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $config);
$message['to'] = $inboxController->getInbox($inbox)->getEmailAddress();
// Disable reroute email module using headers.
$message['headers']['X-Rerouted-Force'] = FALSE;
}
catch (\Exception $e) {
\Drupal::logger('domino')->error('Email delivery test: Unable to fetch email address for the inbox with ID %id.', ['%id' => $inbox]);
}
return;
}
}
// Don't mess with emails sending on the production environment, // Don't mess with emails sending on the production environment,
if ($config->get('application_mode') === ApplicationInterface::MODE_PRODUCTION) { if ($config->get('application_mode') === ApplicationInterface::MODE_PRODUCTION) {
return; return;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment