You are here

RerouteEmailTestBase.php in Reroute Email 2.x

Same filename and directory in other branches
  1. 8 tests/src/Functional/RerouteEmailTestBase.php

File

tests/src/Functional/RerouteEmailTestBase.php
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;

/**
 * @defgroup reroute_email_tests Test Suit
 *
 * @{
 * The automated test suit for Reroute Email.
 *
 * @}
 */

/**
 * Base test class for Reroute Email test cases.
 */
abstract class RerouteEmailTestBase extends BrowserTestBase {
  use AssertMailTrait;
  use StringTranslationTrait;

  /**
   * An editable config.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $rerouteConfig;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'reroute_email',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * User object to perform site browsing.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $adminUser;

  /**
   * Original email address used for the tests.
   *
   * @var string
   */
  protected $originalDestination = 'original@example.com';

  /**
   * Reroute email destination address used for the tests.
   *
   * @var string
   */
  protected $rerouteDestination = 'rerouted@example.com';

  /**
   * An email to add a domain to the allowed list.
   *
   * @var string
   */
  protected $allowlistedDomain = '*@example.com';

  /**
   * Permissions required by the user to perform the tests.
   *
   * @var array
   */
  protected $permissions = [
    'administer reroute email',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->rerouteConfig = $this
      ->config('reroute_email.settings');

    // Authenticate test user.
    $this->adminUser = $this
      ->drupalCreateUser($this->permissions);
    $this
      ->drupalLogin($this->adminUser);
  }

  /**
   * Helper function to configure Reroute Email Settings.
   *
   * @param bool $enable
   *   (optional) Set to TRUE to enable email Rerouting.
   * @param string $addresses
   *   (optional) The email addresses to which emails should be rerouted.
   * @param string $allowlisted
   *   (optional) The email addresses from the allowed list.
   * @param bool $description
   *   (optional) Set to TRUE to show rerouting description.
   * @param bool $message
   *   (optional) Set to TRUE to display a status message after rerouting.
   * @param string $mailkeys
   *   (optional) A list of modules or mail keys should be rerouted, defaults to
   *   empty string (all outgoing emails are rerouted).
   */
  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);
    }

    // Configure to Reroute Email settings form.
    $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,
    ];

    // Submit Reroute Email Settings form and check if it was successful.
    $this
      ->drupalGet('admin/config/development/reroute_email');
    $this
      ->submitForm($post, t('Save configuration'));
    $this
      ->assertSession()
      ->pageTextContains(t('The configuration options have been saved.'));

    // Rebuild config values after form submit.
    $this->rerouteConfig = $this
      ->config('reroute_email.settings');
  }

  /**
   * Assert whether the text "Originally to: @to_email" is found in email body.
   *
   * This method should be used ONLY for emails that were rerouted.
   *
   * @param string $original_destination
   *   (optional) The original email address to be found in rerouted email
   *   body. Defaults to $this->originalDestination if set to NULL.
   */
  public function assertEmailOriginallyTo($original_destination = NULL) {

    // Check the most recent email.
    $mails = $this
      ->getMails();
    if (empty($mails)) {
      $this
        ->assertTrue(FALSE, 'Email was not sent.');
      return;
    }

    // Initialize $original_destination by default if no value is provided.
    if (NULL === $original_destination) {
      $original_destination = $this->originalDestination;
    }

    // Destination address can contain display name with symbols "<" and ">".
    // So, we can't use $this->t() or FormattableMarkup here.
    $search_for = sprintf('Originally to: %s', $original_destination);

    // Search in mail body for "Originally to: $original_destination" string.
    $mail_body = end($mails)['body'];
    $this
      ->assertStringContainsString($search_for, $mail_body, 'Found the correct "Originally to" line in the body.');
  }

  /**
   * Submit test email form and assert rerouting.
   *
   * @param array $post
   *   An array of post data: 'to', 'cc', 'bcc', 'subject', 'body'.
   *
   * @throws \Behat\Mink\Exception\ResponseTextException
   */
  public function assertEmailReroutedFromTestForm(array $post) : void {

    // Submit the test form.
    $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.'));

    // Check rerouted email.
    $this
      ->assertMail('to', $this->rerouteConfig
      ->get(REROUTE_EMAIL_ADDRESS), new FormattableMarkup('An email was rerouted to @address.', [
      '@address' => $this->rerouteDestination,
    ]));
    $this
      ->assertEmailOriginallyTo($post['to']);
  }

  /**
   * Submit test email form and assert not rerouting.
   *
   * @param array $post
   *   An array of post data: 'to', 'cc', 'bcc', 'subject', 'body'.
   *
   * @throws \Behat\Mink\Exception\ResponseTextException
   */
  public function assertEmailNotReroutedFromTestForm(array $post) : void {

    // Submit the test form.
    $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.'));

    // Check not rerouted email.
    $this
      ->assertMail('to', $post['to'], new FormattableMarkup('An email was properly sent to the email address: @destination.', [
      '@destination' => $post['to'],
    ]));
  }

}

Classes

Namesort descending Description
RerouteEmailTestBase Base test class for Reroute Email test cases.