You are here

public function EasyEmailSendTest::testSendDuplicateCheck in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/EasyEmailSendTest.php \Drupal\Tests\easy_email\Functional\EasyEmailSendTest::testSendDuplicateCheck()

Tests email sending with a unique key to prevent duplicates

Throws

\Behat\Mink\Exception\ExpectationException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/EasyEmailSendTest.php, line 1384

Class

EasyEmailSendTest
Class EasyEmailSendTest

Namespace

Drupal\Tests\easy_email\Functional

Code

public function testSendDuplicateCheck() {
  $template_id = 'test_unique_key';
  $template_label = 'Test: Unique Key';
  $template = $this
    ->createTemplate([
    'id' => $template_id,
    'label' => $template_label,
  ]);
  $this
    ->addUserField($template, 'field_user', 'User');
  $this
    ->drupalGet('admin/structure/email-templates/templates');
  $this
    ->assertSession()
    ->pageTextContains($template_id);
  $this
    ->assertSession()
    ->pageTextContains($template_label);
  $template
    ->setRecipient([
    '[easy_email:field_user:0:entity:mail]',
  ])
    ->setSubject('Test email for [easy_email:field_user:0:entity:display-name]')
    ->setBodyHtml([
    'value' => '<p>This is a test email (HTML) for user account [easy_email:field_user:0:entity:account-name].</p>',
    'format' => 'html',
  ])
    ->setBodyPlain('This is a test email (Plain Text) for user account [easy_email:field_user:0:entity:account-name].')
    ->setInboxPreview('This is the inbox preview for user account [easy_email:field_user:0:entity:account-name].')
    ->setKey('test1:::[easy_email:field_user:0:target_id]')
    ->save();
  $this
    ->drupalGet('admin/content/email/add/' . $template
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('field_user');
  $user1 = $this
    ->createUser();
  $this
    ->submitForm([
    'field_user[0][target_id]' => $user1
      ->getAccountName() . ' (' . $user1
      ->id() . ')',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Created new email.');
  $this
    ->assertSession()
    ->pageTextContains('Email sent.');
  $this
    ->drupalGet('admin/content/email/add/' . $template
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('field_user');
  $this
    ->submitForm([
    'field_user[0][target_id]' => $user1
      ->getAccountName() . ' (' . $user1
      ->id() . ')',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains('Created new email.');
  $this
    ->assertSession()
    ->pageTextNotContains('Email sent.');
  $this
    ->assertSession()
    ->pageTextContains('Email matching unique key already exists.');
  $this
    ->assertSession()
    ->addressEquals('admin/content/email/add/' . $template
    ->id());
  $emails = $this
    ->getSentEmails([]);
  $this
    ->assertEquals(1, count($emails));
}