public function EasyEmailSendTest::testSendWithAttachmentsSaved in Easy Email 8
Same name and namespace in other branches
- 2.0.x tests/src/Functional/EasyEmailSendTest.php \Drupal\Tests\easy_email\Functional\EasyEmailSendTest::testSendWithAttachmentsSaved()
Tests email sending with attachments that are saved.
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 1120
Class
- EasyEmailSendTest
- Class EasyEmailSendTest
Namespace
Drupal\Tests\easy_email\FunctionalCode
public function testSendWithAttachmentsSaved() {
$template_id = 'test_attachments_saved';
$template_label = 'Test: Attachments Saved';
$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].')
->setSaveAttachment(TRUE)
->setAttachmentScheme('private')
->setAttachment([
'/core/misc/druplicon.png',
'/core/misc/help.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() . ')',
], 'Save');
$url = explode('/', $this
->getSession()
->getCurrentUrl());
$email_id = array_pop($url);
/** @var \Drupal\easy_email\Entity\EasyEmailInterface $email_entity */
\Drupal::entityTypeManager()
->getStorage('easy_email')
->resetCache();
$email_entity = \Drupal::entityTypeManager()
->getStorage('easy_email')
->load($email_id);
$this
->assertSession()
->pageTextContains('Created new email.');
$this
->assertSession()
->pageTextContains('Email sent.');
$this
->assertSession()
->pageTextContains('Attachments');
$this
->assertSession()
->linkExists('druplicon.png');
$this
->assertSession()
->linkExists('help.png');
$attachments = $email_entity
->getAttachments();
$this
->assertEquals(2, count($attachments));
/** @var \Drupal\file\FileInterface $attachment */
$attachment = array_shift($attachments);
$this
->assertEquals('druplicon.png', $attachment
->getFilename());
$this
->assertEquals('image/png', $attachment
->getMimeType());
$attachment = array_shift($attachments);
$this
->assertEquals('help.png', $attachment
->getFilename());
$this
->assertEquals('image/png', $attachment
->getMimeType());
$emails = $this
->getSentEmails([]);
$this
->assertEquals(1, count($emails));
$email = reset($emails);
$this
->assertEquals($template
->id(), $email['key']);
$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);
}