View source
<?php
namespace Drupal\Tests\reroute_email\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\BrowserTestBase;
abstract class RerouteEmailTestBase extends BrowserTestBase {
use AssertMailTrait;
use StringTranslationTrait;
protected $rerouteConfig;
protected static $modules = [
'reroute_email',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $originalDestination = 'original@example.com';
protected $rerouteDestination = 'rerouted@example.com';
protected $allowlistedDomain = '*@example.com';
protected $permissions = [
'administer reroute email',
];
protected function setUp() : void {
parent::setUp();
$this->rerouteConfig = $this
->config('reroute_email.settings');
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalLogin($this->adminUser);
}
public function configureRerouteEmail($enable = NULL, $addresses = NULL, $allowlisted = NULL, $description = NULL, $message = NULL, $mailkeys = NULL) {
$current_values = $install_values = [
REROUTE_EMAIL_ENABLE => FALSE,
REROUTE_EMAIL_ADDRESS => '',
REROUTE_EMAIL_ALLOWLIST => '',
REROUTE_EMAIL_DESCRIPTION => TRUE,
REROUTE_EMAIL_MESSAGE => TRUE,
REROUTE_EMAIL_MAILKEYS => '',
];
foreach ($install_values as $key => $value) {
$current_values[$key] = NULL === $this->rerouteConfig
->get($key) ? $value : $this->rerouteConfig
->get($key);
}
$post = [
REROUTE_EMAIL_ENABLE => NULL === $enable ? $current_values[REROUTE_EMAIL_ENABLE] : $enable,
REROUTE_EMAIL_ADDRESS => NULL === $addresses ? $current_values[REROUTE_EMAIL_ADDRESS] : $addresses,
REROUTE_EMAIL_ALLOWLIST => NULL === $allowlisted ? $current_values[REROUTE_EMAIL_ALLOWLIST] : $allowlisted,
REROUTE_EMAIL_DESCRIPTION => NULL === $description ? $current_values[REROUTE_EMAIL_DESCRIPTION] : $description,
REROUTE_EMAIL_MESSAGE => NULL === $message ? $current_values[REROUTE_EMAIL_MESSAGE] : $message,
REROUTE_EMAIL_MAILKEYS => NULL === $mailkeys ? $current_values[REROUTE_EMAIL_MAILKEYS] : $mailkeys,
];
$this
->drupalGet('admin/config/development/reroute_email');
$this
->submitForm($post, t('Save configuration'));
$this
->assertSession()
->pageTextContains(t('The configuration options have been saved.'));
$this->rerouteConfig = $this
->config('reroute_email.settings');
}
public function assertEmailOriginallyTo($original_destination = NULL) {
$mails = $this
->getMails();
if (empty($mails)) {
$this
->assertTrue(FALSE, 'Email was not sent.');
return;
}
if (NULL === $original_destination) {
$original_destination = $this->originalDestination;
}
$search_for = sprintf('Originally to: %s', $original_destination);
$mail_body = end($mails)['body'];
$this
->assertStringContainsString($search_for, $mail_body, 'Found the correct "Originally to" line in the body.');
}
public function assertEmailReroutedFromTestForm(array $post) : void {
$this
->drupalGet('admin/config/development/reroute_email/test');
$this
->submitForm($post, $this
->t('Send email'));
$this
->assertSession()
->pageTextContains($this
->t('Test email submitted for delivery from test form.'));
$this
->assertMail('to', $this->rerouteConfig
->get(REROUTE_EMAIL_ADDRESS), new FormattableMarkup('An email was rerouted to @address.', [
'@address' => $this->rerouteDestination,
]));
$this
->assertEmailOriginallyTo($post['to']);
}
public function assertEmailNotReroutedFromTestForm(array $post) : void {
$this
->drupalGet('admin/config/development/reroute_email/test');
$this
->submitForm($post, $this
->t('Send email'));
$this
->assertSession()
->pageTextContains($this
->t('Test email submitted for delivery from test form.'));
$this
->assertMail('to', $post['to'], new FormattableMarkup('An email was properly sent to the email address: @destination.', [
'@destination' => $post['to'],
]));
}
}