You are here

public function DefaultAddressesTest::testRerouteDefaultAddress in Reroute Email 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/DefaultAddressesTest.php \Drupal\Tests\reroute_email\Functional\DefaultAddressesTest::testRerouteDefaultAddress()

Test reroute email address is set to site_mail, sendmail_from or empty.

When reroute email addresses field is not configured and settings haven't been configured yet, check if the site email address or the sendmail_from system variable are properly used as fallbacks. Additionally, check that emails are aborted and a watchdog entry logged if reroute email address is set to an empty string.

File

tests/src/Functional/DefaultAddressesTest.php, line 42

Class

DefaultAddressesTest
Test for default address.

Namespace

Drupal\Tests\reroute_email\Functional

Code

public function testRerouteDefaultAddress() {

  // Check default value for reroute_email_address when not configured.
  // If system.site's 'mail' is not empty, it should be the default value.
  $site_mail = $this
    ->config('system.site')
    ->get('mail');
  $this
    ->assertTrue(isset($site_mail), new FormattableMarkup('Site mail is not empty: @site_mail.', [
    '@site_mail' => $site_mail,
  ]));

  // Programmatically enable email rerouting.
  $this->rerouteConfig
    ->set(REROUTE_EMAIL_ENABLE, TRUE)
    ->save();

  // Load the Reroute Email Settings form page. Ensure rerouting is enabled.
  $this
    ->drupalGet('admin/config/development/reroute_email');
  $this
    ->assertSession()
    ->checkboxChecked('edit-enable');
  $this
    ->assertTrue($this->rerouteConfig
    ->get(REROUTE_EMAIL_ENABLE), 'Rerouting is enabled.');

  // Email addresses field default value is system.site.mail.
  $this
    ->assertSession()
    ->fieldValueEquals(REROUTE_EMAIL_ADDRESS, $site_mail);

  // Ensure reroute_email_address is actually empty at this point.
  $this
    ->assertNull($this->rerouteConfig
    ->get(REROUTE_EMAIL_ADDRESS), 'Reroute email destination address is not configured.');

  // Submit a test email, check if it is rerouted to system.site.mail address.
  $this
    ->drupalGet('admin/config/development/reroute_email/test');
  $this
    ->submitForm([
    'to' => 'to@example.com',
  ], 'Send email');
  $this
    ->assertSession()
    ->pageTextContains(t('Test email submitted for delivery from test form.'));
  $this
    ->assertTrue(count($this
    ->getMails()) === 1, 'Exactly one email captured.');

  // Check rerouted email is the site email address.
  $this
    ->assertMail('to', $site_mail, new FormattableMarkup('Email was properly rerouted to site email address: @default_destination.', [
    '@default_destination' => $site_mail,
  ]));

  // Unset system.site.mail.
  $this
    ->config('system.site')
    ->set('mail', NULL)
    ->save();

  // Configure the allowed list of addresses as an empty string to abort all
  // emails.
  $this
    ->configureRerouteEmail(TRUE, '', '');

  // Make sure configured emails values are an empty string.
  $this
    ->assertTrue($this->rerouteConfig
    ->get(REROUTE_EMAIL_ADDRESS) === '', 'Reroute email destination address is an empty string.');
  $this
    ->assertTrue($this->rerouteConfig
    ->get(REROUTE_EMAIL_ALLOWLIST) === '', 'Allowed email address is an empty string.');

  // Flush the Test Mail collector to ensure it is empty for this tests.
  \Drupal::state()
    ->set('system.test_mail_collector', []);

  // Submit a test email to check if it is aborted.
  $this
    ->drupalGet('admin/config/development/reroute_email/test');
  $this
    ->submitForm([
    'to' => 'to@example.com',
  ], t('Send email'));
  $mails = $this
    ->getMails();
  $this
    ->assertTrue(count($mails) == 0, 'Email sending was properly aborted because rerouting email address is an empty string.');

  // Check status message is displayed properly after email form submission.
  $this
    ->assertSession()
    ->responseMatches(t('/@message_id.*was aborted by reroute email/', [
    '@message_id' => 'reroute_email_test_email_form',
  ]));

  // Check the watchdog entry logged with aborted email message.
  $this
    ->drupalGet('admin/reports/dblog');

  // Check the link to the watchdog detailed message.
  $dblog_link = $this
    ->xpath('//table[@id="admin-dblog"]/tbody/tr[contains(@class,"dblog-reroute-email")][1]/td[text()="reroute_email"]/following-sibling::td/a[contains(text(),"reroute_email")]');
  $link_label = $dblog_link[0]
    ->getText();
  $this
    ->assertTrue(isset($dblog_link[0]), new FormattableMarkup('Logged a message in dblog: <em>@link</em>.', [
    '@link' => $link_label,
  ]));

  // Open the full view page of the log message found for reroute_email.
  $this
    ->clickLink($link_label);

  // Ensure the correct email is logged with default 'to' placeholder.
  $this
    ->assertSession()
    ->responseMatches(t('/Aborted email sending for.*@message_id.*Detailed email data/', [
    '@message_id' => 'reroute_email_test_email_form',
  ]));
}