You are here

public function EasyEmailSendTest::testSendWithCustomizedEmail 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::testSendWithCustomizedEmail()

Tests email sending with customized text at send time.

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 288

Class

EasyEmailSendTest
Class EasyEmailSendTest

Namespace

Drupal\Tests\easy_email\Functional

Code

public function testSendWithCustomizedEmail() {
  $template_id = 'test_customized';
  $template_label = 'Test: Customized';
  $template = $this
    ->createTemplate([
    'id' => $template_id,
    'label' => $template_label,
  ]);
  $this
    ->addUserField($template, 'field_user', 'User');
  $this
    ->addUserField($template, 'field_cc_user', 'CC User');
  $this
    ->addUserField($template, 'field_bcc_user', 'BCC User');
  $this
    ->drupalGet('admin/structure/email-templates/templates');
  $this
    ->assertSession()
    ->pageTextContains($template_id);
  $this
    ->assertSession()
    ->pageTextContains($template_label);
  $template
    ->setRecipient([
    'test@example.com',
    '[easy_email:field_user:0:entity:mail]',
  ])
    ->setCc([
    'cc@example.com',
    '[easy_email:field_cc_user:0:entity:mail]',
  ])
    ->setBcc([
    'bcc@example.com',
    '[easy_email:field_bcc_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].')
    ->setAttachment([
    '/core/misc/druplicon.png',
  ])
    ->save();
  $this
    ->drupalGet('admin/content/email/add/' . $template
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('field_user');
  $this
    ->assertSession()
    ->pageTextContains('field_cc_user');
  $this
    ->assertSession()
    ->pageTextContains('field_bcc_user');
  $user1 = $this
    ->createUser();
  $user2 = $this
    ->createUser();
  $user3 = $this
    ->createUser();
  $this
    ->submitForm([
    'field_user[0][target_id]' => $user1
      ->getAccountName() . ' (' . $user1
      ->id() . ')',
    'field_cc_user[0][target_id]' => $user2
      ->getAccountName() . ' (' . $user2
      ->id() . ')',
    'field_bcc_user[0][target_id]' => $user3
      ->getAccountName() . ' (' . $user3
      ->id() . ')',
    'recipient' => 'test@example.com, [easy_email:field_user:0:entity:mail], overridden-recipient@example.com',
    'cc' => 'cc@example.com, [easy_email:field_cc_user:0:entity:mail], overridden-cc@example.com',
    'bcc' => 'bcc@example.com, [easy_email:field_bcc_user:0:entity:mail], overridden-bcc@example.com',
    'bodyHtml[value]' => '<p>This is the overridden HTML body for user account [easy_email:field_user:0:entity:account-name].</p>',
    'inboxPreview' => 'This is the overridden inbox preview for user account [easy_email:field_user:0:entity:account-name].',
    'bodyPlain' => 'This is the overridden plain text body for user account [easy_email:field_user:0:entity:account-name].',
    'subjectText' => 'Overridden subject for [easy_email:field_user:0:entity:display-name]',
    'fromName' => 'Overridden Person',
    'fromAddress' => 'overridden@example.com',
    'replyToAddress' => 'override-reply-to@example.com',
    'attachment_paths' => '/core/misc/druplicon.png, /core/misc/help.png',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Created new email.');
  $this
    ->assertSession()
    ->pageTextContains('Email sent.');

  /** @var \Drupal\Core\Config\ImmutableConfig $site_config */
  $site_config = \Drupal::config('system.site');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Return-Path"] span.value', $site_config
    ->get('mail'));
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Sender"] span.value', $site_config
    ->get('mail'));
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-From"] span.value', 'Overridden Person <overridden@example.com>');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Cc"] span.value', 'cc@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Cc"] span.value', 'overridden-cc@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Cc"] span.value', $user2
    ->getEmail());
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Bcc"] span.value', 'bcc@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Bcc"] span.value', 'overridden-bcc@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-Bcc"] span.value', $user3
    ->getEmail());
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-To"] span.value', 'test@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-To"] span.value', 'overridden-recipient@example.com');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="header-To"] span.value', $user1
    ->getEmail());
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="inbox-preview"] .from-name', 'Overridden Person');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="inbox-preview"] .subject', 'Overridden subject for ' . $user1
    ->getDisplayName());
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="inbox-preview"] .body-preview', 'This is the overridden inbox preview for user account ' . $user1
    ->getDisplayName() . '.');
  $this
    ->assertSession()
    ->linkByHrefNotExists(file_create_url('/core/misc/druplicon.png'));
  $this
    ->assertSession()
    ->linkByHrefNotExists(file_create_url('/core/misc/help.png'));
  $this
    ->assertSession()
    ->pageTextNotContains('Attachments');
  $html_body_iframe = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="html-body"] iframe');
  $html_body_url = $this
    ->getIframeUrlAndQuery($html_body_iframe);
  $plain_body_iframe = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="plain-body"] iframe');
  $plain_body_url = $this
    ->getIframeUrlAndQuery($plain_body_iframe);
  $this
    ->drupalGet($html_body_url['path'], [
    'query' => $html_body_url['query'],
  ]);
  $this
    ->assertSession()
    ->responseContains('<p>This is the overridden HTML body for user account ' . $user1
    ->getAccountName() . '.</p>');
  $this
    ->drupalGet($plain_body_url['path'], [
    'query' => $plain_body_url['query'],
  ]);
  $this
    ->assertSession()
    ->responseContains('This is the overridden plain text body for user account ' . $user1
    ->getAccountName() . '.');
  $emails = $this
    ->getSentEmails([]);
  $this
    ->assertEquals(1, count($emails));
  $email = reset($emails);
  $this
    ->assertEquals($template
    ->id(), $email['key']);
  $this
    ->assertEquals('test@example.com, ' . $user1
    ->getEmail() . ', overridden-recipient@example.com', $email['to']);
  $this
    ->assertEquals('cc@example.com, ' . $user2
    ->getEmail() . ', overridden-cc@example.com', $email['headers']['Cc']);
  $this
    ->assertEquals('bcc@example.com, ' . $user3
    ->getEmail() . ', overridden-bcc@example.com', $email['headers']['Bcc']);
  $this
    ->assertEquals('Overridden Person <overridden@example.com>', $email['headers']['From']);
  $this
    ->assertEquals($site_config
    ->get('mail'), $email['headers']['Sender']);
  $this
    ->assertEquals('override-reply-to@example.com', $email['headers']['Reply-to']);
  $this
    ->assertEquals('text/html; charset=UTF-8;', $email['headers']['Content-Type']);
  $this
    ->assertStringContainsString('<p>This is the overridden HTML body for user account ' . $user1
    ->getAccountName() . '.</p>', (string) $email['body']);
  $this
    ->assertStringContainsString('This is the overridden inbox preview for user account ' . $user1
    ->getAccountName() . '.', (string) $email['body']);
  $this
    ->assertStringContainsString('This is the overridden plain text body for user account ' . $user1
    ->getAccountName() . '.', (string) $email['plain']);
  $this
    ->assertEquals('Overridden subject for ' . $user1
    ->getDisplayName(), $email['subject']);
  $this
    ->assertEquals(2, count($email['params']['files']));
  $attachment = array_shift($email['params']['files']);
  $this
    ->assertEquals('core/misc/druplicon.png', $attachment->uri);
  $this
    ->assertEquals('druplicon.png', $attachment->filename);
  $this
    ->assertEquals('image/png', $attachment->filemime);
  $attachment = array_shift($email['params']['files']);
  $this
    ->assertEquals('core/misc/help.png', $attachment->uri);
  $this
    ->assertEquals('help.png', $attachment->filename);
  $this
    ->assertEquals('image/png', $attachment->filemime);
}