2013-10-09 12:51:29 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Control\Tests\Email;
|
|
|
|
|
2022-03-07 04:18:57 +01:00
|
|
|
use SilverStripe\Control\Director;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Email\Email;
|
2017-10-18 05:09:14 +02:00
|
|
|
use SilverStripe\Control\Tests\Email\EmailTest\EmailSubClass;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader;
|
2017-01-13 02:48:46 +01:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-10-18 05:09:14 +02:00
|
|
|
use SilverStripe\Dev\TestMailer;
|
2017-01-13 02:48:46 +01:00
|
|
|
use SilverStripe\Security\Member;
|
2017-10-18 05:09:14 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2022-10-19 04:16:14 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
|
|
use Symfony\Component\Mime\Address;
|
|
|
|
use Symfony\Component\Mime\Part\DataPart;
|
|
|
|
use Symfony\Component\Mime\Part\AbstractPart;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class EmailTest extends SapphireTest
|
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
private array $origThemes = [];
|
|
|
|
|
2022-03-08 02:18:18 +01:00
|
|
|
protected function setUp(): void
|
2022-03-07 04:18:57 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/');
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->origThemes = SSViewer::get_themes();
|
|
|
|
SSViewer::set_themes([
|
|
|
|
'silverstripe/framework:/tests/php/Control/Email/EmailTest',
|
|
|
|
'$default',
|
|
|
|
]);
|
2022-03-07 04:18:57 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
parent::tearDown();
|
|
|
|
SSViewer::set_themes($this->origThemes);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testAddAttachment(): void
|
|
|
|
{
|
|
|
|
$email = new Email();
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
|
2022-10-19 04:16:14 +02:00
|
|
|
$attachments = $email->getAttachments();
|
|
|
|
$this->assertCount(1, $attachments);
|
|
|
|
$attachment = $this->getFirstAttachment($attachments);
|
|
|
|
$this->assertSame('text/plain', $attachment->getContentType());
|
|
|
|
$this->assertSame('attachment.txt', $attachment->getFilename());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testAddAttachmentFromData(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addAttachmentFromData('foo bar', 'foo.txt', 'text/plain');
|
2022-10-19 04:16:14 +02:00
|
|
|
$attachments = $email->getAttachments();
|
|
|
|
$this->assertCount(1, $attachments);
|
|
|
|
$attachment = $this->getFirstAttachment($attachments);
|
|
|
|
$this->assertSame('text/plain', $attachment->getContentType());
|
|
|
|
$this->assertSame('foo.txt', $attachment->getFilename());
|
|
|
|
$this->assertSame('foo bar', $attachment->getBody());
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
private function getFirstAttachment(array $attachments): DataPart
|
|
|
|
{
|
|
|
|
return $attachments[0];
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:15:18 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideValidEmailAddresses
|
|
|
|
*/
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testValidEmailAddress($email): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2018-09-26 00:15:18 +02:00
|
|
|
$this->assertTrue(Email::is_valid_address($email));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-09-26 00:15:18 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideInvalidEmailAddresses
|
|
|
|
*/
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testInvalidEmailAddress($email): void
|
2018-09-26 00:15:18 +02:00
|
|
|
{
|
|
|
|
$this->assertFalse(Email::is_valid_address($email));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function provideValidEmailAddresses(): array
|
2018-09-26 00:15:18 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['test@example.com', 'test-123@sub.example.com'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function provideInvalidEmailAddresses(): array
|
2018-09-26 00:15:18 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['foo.bar@', '@example.com', 'foo@'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testObfuscate(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$emailAddress = 'test-1@example.com';
|
|
|
|
|
|
|
|
$direction = Email::obfuscate($emailAddress, 'direction');
|
|
|
|
$visible = Email::obfuscate($emailAddress, 'visible');
|
|
|
|
$hex = Email::obfuscate($emailAddress, 'hex');
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertEquals('<span class="codedirection">moc.elpmaxe@1-tset</span>', $direction);
|
|
|
|
$this->assertEquals('test [dash] 1 [at] example [dot] com', $visible);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-01-13 02:48:46 +01:00
|
|
|
'test-1@example.com',
|
|
|
|
$hex
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
private function getTemplateClass(string $templateName): string
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
return implode('\\', ['SilverStripe', 'Control', 'Tests', 'Email', 'EmailTest', $templateName]);
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
private function getMailer(): TestMailer
|
|
|
|
{
|
|
|
|
return Injector::inst()->get(MailerInterface::class);
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
private function createTestEmail(string $subject = 'My subject', $setPlain = true): Email
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$email->setTo('to@example.com');
|
|
|
|
$email->setSubject($subject);
|
|
|
|
if ($setPlain) {
|
|
|
|
$email->text("Plain body for $subject");
|
|
|
|
}
|
|
|
|
$email->html("<p>HTML body for $subject</p>");
|
|
|
|
$email->setCC('cc@example.com');
|
|
|
|
$email->setBCC('bcc@example.com');
|
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
|
|
|
|
return $email;
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSendPlain(): void
|
|
|
|
{
|
|
|
|
$email = $this->createTestEmail('Test send plain');
|
|
|
|
$email->sendPlain();
|
|
|
|
$this->assertStringNotContainsString($email->getTextBody(), 'My Plain Template');
|
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('to@example.com', $sentMail['To']);
|
|
|
|
$this->assertSame('from@example.com', $sentMail['From']);
|
|
|
|
$this->assertSame('Test send plain', $sentMail['Subject']);
|
|
|
|
$this->assertStringContainsString('Plain body for Test send plain', $sentMail['Content']);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']);
|
|
|
|
$child = reset($sentMail['AttachedFiles']);
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('text/plain', $child['mimetype']);
|
|
|
|
$this->assertSame('attachment.txt', $child['filename']);
|
|
|
|
$this->assertSame('Hello, I\'m a text document.', $child['contents']);
|
|
|
|
|
|
|
|
// assert MIME types
|
|
|
|
// explicitly setting $email->html(null) because sendPlain() will itself set $this->html(null), and then
|
|
|
|
// revert it to its previous AFTER sending the email. For testing purposes, we need to manuall set it
|
|
|
|
// to null in order to test the MIME types for what would have been sent in practice
|
|
|
|
$email->html(null);
|
|
|
|
$this->assertSame([
|
|
|
|
'text/plain charset: utf-8',
|
|
|
|
'text/plain disposition: attachment filename: attachment.txt'
|
|
|
|
], array_map(fn(AbstractPart $part) => $part->asDebugString(), $email->getBody()->getParts()));
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSendPlainFallback(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = $this->createTestEmail('Test send plain', false);
|
|
|
|
$email->sendPlain();
|
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
|
|
|
// assert that it has HTML body with HTML tags removed
|
|
|
|
$this->assertSame('HTML body for Test send plain', $sentMail['Content']);
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSendPlainThenNormalWithSetData(): void
|
|
|
|
{
|
|
|
|
$email = $this->createTestEmail('Test send plain', false);
|
|
|
|
$email->setData([
|
|
|
|
'EmailContent' => 'This is the content of the email',
|
|
|
|
]);
|
|
|
|
$email->sendPlain();
|
|
|
|
$email->send();
|
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
|
|
|
$this->assertSame('This is the content of the email', $sentMail['Content']);
|
|
|
|
$email->to('to2@example.com');
|
|
|
|
$email->send();
|
|
|
|
$sentMail = $this->getMailer()->findEmail('to2@example.com');
|
|
|
|
$this->assertStringContainsString('This is the content of the email', $sentMail['Content']);
|
|
|
|
}
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSend(): void
|
|
|
|
{
|
|
|
|
$email = $this->createTestEmail('Test send HTML');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
// email should not call render if a body is supplied
|
|
|
|
$email->setHTMLTemplate($this->getTemplateClass('HtmlTemplate'));
|
|
|
|
$email->send();
|
|
|
|
$this->assertStringNotContainsString($email->getHtmlBody(), 'My HTML Template');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('to@example.com', $sentMail['To']);
|
|
|
|
$this->assertSame('from@example.com', $sentMail['From']);
|
|
|
|
$this->assertSame('Test send HTML', $sentMail['Subject']);
|
|
|
|
$this->assertStringContainsString('<p>HTML body for Test send HTML</p>', $sentMail['Content']);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']);
|
|
|
|
$child = reset($sentMail['AttachedFiles']);
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('text/plain', $child['mimetype']);
|
|
|
|
$this->assertSame('attachment.txt', $child['filename']);
|
|
|
|
$this->assertSame('Hello, I\'m a text document.', $child['contents']);
|
|
|
|
|
|
|
|
// assert MIME types
|
|
|
|
$this->assertSame([
|
|
|
|
implode("\n └ ", [
|
|
|
|
'multipart/alternative',
|
|
|
|
'text/plain charset: utf-8',
|
|
|
|
'text/html charset: utf-8'
|
|
|
|
]),
|
|
|
|
'text/plain disposition: attachment filename: attachment.txt'
|
|
|
|
], array_map(fn(AbstractPart $part) => $part->asDebugString(), $email->getBody()->getParts()));
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testRenderedSend(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email(to: 'to@example.com');
|
|
|
|
$email->setHTMLTemplate($this->getTemplateClass('HtmlTemplate'));
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2022-10-19 04:16:14 +02:00
|
|
|
'EmailContent' => '<p>test</p>',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->send();
|
2022-10-19 04:16:14 +02:00
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
|
|
|
$this->assertStringContainsString('My HTML Template', $sentMail['Content']);
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testRenderedSendSubclass(): void
|
2017-10-18 05:09:14 +02:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new EmailSubClass(to: 'to@example.com');
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-10-18 05:09:14 +02:00
|
|
|
'EmailContent' => 'test',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-10-18 05:09:14 +02:00
|
|
|
$email->send();
|
2022-10-19 04:16:14 +02:00
|
|
|
$sentMail = $this->getMailer()->findEmail('to@example.com');
|
|
|
|
$this->assertStringContainsString('<h1>Email Sub-class</h1>', $sentMail['Content']);
|
2017-10-18 05:09:14 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testConstructor(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
$email = new Email(
|
|
|
|
'from@example.com',
|
|
|
|
'to@example.com',
|
2017-01-13 02:48:46 +01:00
|
|
|
'subject',
|
2022-10-19 04:16:14 +02:00
|
|
|
'<p>body</p>',
|
2016-12-16 05:34:21 +01:00
|
|
|
'cc@example.com',
|
2017-01-13 02:48:46 +01:00
|
|
|
'bcc@example.com',
|
|
|
|
'bounce@example.com'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getFrom());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('from@example.com', $email->getFrom()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getTo());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('to@example.com', $email->getTo()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertEquals('subject', $email->getSubject());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertEquals('<p>body</p>', $email->getHtmlBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getCC());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertEquals('cc@example.com', $email->getCC()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getBCC());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertEquals('bcc@example.com', $email->getBcc()[0]->getAddress());
|
|
|
|
$this->assertEquals('bounce@example.com', $email->getReturnPath()->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 02:40:13 +01:00
|
|
|
public function testConstructorArray(): void
|
|
|
|
{
|
|
|
|
$email = new Email(
|
|
|
|
['from@example.com' => 'From name'],
|
|
|
|
['a@example.com' => "A", 'b@example.com' => "B", 'c@example.com', 'd@example.com'],
|
|
|
|
'subject',
|
|
|
|
'<p>body</p>',
|
|
|
|
['cca@example.com' => 'CCA', 'ccb@example.com' => "CCB", 'ccc@example.com', 'ccd@example.com'],
|
|
|
|
['bcca@example.com' => 'BCCA', 'bccb@example.com' => "BCCB", 'bccc@example.com', 'bccd@example.com'],
|
|
|
|
'bounce@example.com'
|
|
|
|
);
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertSame('from@example.com', $email->getFrom()[0]->getAddress());
|
|
|
|
$this->assertSame('From name', $email->getFrom()[0]->getName());
|
|
|
|
$this->assertCount(4, $email->getTo());
|
|
|
|
$this->assertSame('a@example.com', $email->getTo()[0]->getAddress());
|
|
|
|
$this->assertSame('A', $email->getTo()[0]->getName());
|
|
|
|
$this->assertSame('b@example.com', $email->getTo()[1]->getAddress());
|
|
|
|
$this->assertSame('B', $email->getTo()[1]->getName());
|
|
|
|
$this->assertSame('c@example.com', $email->getTo()[2]->getAddress());
|
|
|
|
$this->assertSame('', $email->getTo()[2]->getName());
|
|
|
|
$this->assertCount(4, $email->getCC());
|
|
|
|
$this->assertEquals('cca@example.com', $email->getCC()[0]->getAddress());
|
|
|
|
$this->assertEquals('CCA', $email->getCC()[0]->getName());
|
|
|
|
$this->assertEquals('ccb@example.com', $email->getCC()[1]->getAddress());
|
|
|
|
$this->assertEquals('CCB', $email->getCC()[1]->getName());
|
|
|
|
$this->assertEquals('ccc@example.com', $email->getCC()[2]->getAddress());
|
|
|
|
$this->assertEquals('', $email->getCC()[2]->getName());
|
|
|
|
$this->assertEquals('ccd@example.com', $email->getCC()[3]->getAddress());
|
|
|
|
$this->assertEquals('', $email->getCC()[2]->getName());
|
|
|
|
$this->assertCount(4, $email->getBCC());
|
|
|
|
$this->assertEquals('bcca@example.com', $email->getBCC()[0]->getAddress());
|
|
|
|
$this->assertEquals('BCCA', $email->getBCC()[0]->getName());
|
|
|
|
$this->assertEquals('bccb@example.com', $email->getBCC()[1]->getAddress());
|
|
|
|
$this->assertEquals('BCCB', $email->getBCC()[1]->getName());
|
|
|
|
$this->assertEquals('bccc@example.com', $email->getBCC()[2]->getAddress());
|
|
|
|
$this->assertEquals('', $email->getBCC()[2]->getName());
|
|
|
|
$this->assertEquals('bccd@example.com', $email->getBCC()[3]->getAddress());
|
|
|
|
$this->assertEquals('', $email->getBCC()[2]->getName());
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetBody(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setBody('<p>body</p>');
|
|
|
|
$this->assertSame('<p>body</p>', $email->getHtmlBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetFrom(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setFrom('from@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getFrom());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('from@example.com', $email->getFrom()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSender(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setSender('sender@example.com');
|
|
|
|
$this->assertSame('sender@example.com', $email->getSender()->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetTo(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setTo('to@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getTo());
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame('to@example.com', $email->getTo()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetReplyTo(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email();
|
|
|
|
$email->setReplyTo('reply-to@example.com');
|
|
|
|
$this->assertCount(1, $email->getReplyTo());
|
|
|
|
$this->assertSame('reply-to@example.com', $email->getReplyTo()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetSubject(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email();
|
|
|
|
$email->setSubject('my subject');
|
|
|
|
$this->assertSame('my subject', $email->getSubject());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetReturnPath(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setReturnPath('return-path@example.com');
|
|
|
|
$this->assertSame('return-path@example.com', $email->getReturnPath()->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testSetPriority(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email();
|
|
|
|
// Intentionally set above 5 to test that Symfony\Component\Mime\Email->priority() is being called
|
|
|
|
$email->setPriority(7);
|
|
|
|
$this->assertSame(5, $email->getPriority());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testAdminEmailApplied()
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-11-21 23:41:53 +01:00
|
|
|
Email::config()->set('admin_email', 'admin@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertSame('admin@example.com', $email->getFrom()[0]->getAddress());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testDataWithArray(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame(true, $email->getData()->IsEmail);
|
|
|
|
$this->assertSame(Director::absoluteBaseURL(), $email->getData()->BaseURL);
|
|
|
|
$email->setData(['Lorem' => 'Ipsum']);
|
|
|
|
$this->assertSame(true, $email->getData()->IsEmail);
|
|
|
|
$this->assertSame(Director::absoluteBaseURL(), $email->getData()->BaseURL);
|
|
|
|
$this->assertSame('Ipsum', $email->getData()->Lorem);
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addData('Content', 'My content');
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame(true, $email->getData()->IsEmail);
|
|
|
|
$this->assertSame(Director::absoluteBaseURL(), $email->getData()->BaseURL);
|
|
|
|
$this->assertSame('Ipsum', $email->getData()->Lorem);
|
|
|
|
$this->assertSame('My content', $email->getData()->Content);
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testDataWithViewableData(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email();
|
|
|
|
$viewableData = new ViewableData();
|
|
|
|
$viewableData->ABC = 'XYZ';
|
|
|
|
$email->setData($viewableData);
|
|
|
|
$data = $email->getData();
|
|
|
|
$this->assertSame('XYZ', $data->ABC);
|
|
|
|
$this->assertSame(true, $data->IsEmail);
|
|
|
|
$this->assertSame(Director::absoluteBaseURL(), $data->BaseURL);
|
2017-01-13 02:48:46 +01:00
|
|
|
$member = new Member();
|
|
|
|
$member->FirstName = 'First Name';
|
|
|
|
$email->setData($member);
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame($member->FirstName, $email->getData()->FirstName);
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addData('Test', 'Test value');
|
|
|
|
$this->assertEquals('Test value', $email->getData()->Test);
|
|
|
|
$email->removeData('Test');
|
|
|
|
$this->assertNull($email->getData()->Test);
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testHTMLTemplate(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2017-10-18 05:09:14 +02:00
|
|
|
// Find template on disk
|
|
|
|
$emailTemplate = ModuleResourceLoader::singleton()->resolveResource(
|
|
|
|
'silverstripe/framework:templates/SilverStripe/Control/Email/Email.ss'
|
|
|
|
);
|
|
|
|
$subClassTemplate = ModuleResourceLoader::singleton()->resolveResource(
|
|
|
|
'silverstripe/framework:tests/php/Control/Email/EmailTest/templates/'
|
|
|
|
. str_replace('\\', '/', EmailSubClass::class)
|
2018-01-16 19:39:30 +01:00
|
|
|
. '.ss'
|
2017-10-18 05:09:14 +02:00
|
|
|
);
|
|
|
|
$this->assertTrue($emailTemplate->exists());
|
|
|
|
$this->assertTrue($subClassTemplate->exists());
|
|
|
|
|
|
|
|
// Check template is auto-found
|
2017-01-13 02:48:46 +01:00
|
|
|
$email = new Email();
|
2017-10-18 05:09:14 +02:00
|
|
|
$this->assertEquals($emailTemplate->getPath(), $email->getHTMLTemplate());
|
|
|
|
$email->setHTMLTemplate('MyTemplate');
|
|
|
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
|
|
|
|
|
|
|
// Check subclass template is found
|
|
|
|
$email2 = new EmailSubClass();
|
|
|
|
$this->assertEquals($subClassTemplate->getPath(), $email2->getHTMLTemplate());
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->setHTMLTemplate('MyTemplate');
|
|
|
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testPlainTemplate(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getPlainTemplate());
|
|
|
|
$email->setPlainTemplate('MyTemplate');
|
|
|
|
$this->assertEquals('MyTemplate', $email->getPlainTemplate());
|
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testRerender(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->setPlainTemplate($this->getTemplateClass('PlainTemplate'));
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2022-10-19 04:16:14 +02:00
|
|
|
'EmailContent' => '<p>my content</p>',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->send();
|
|
|
|
$this->assertStringContainsString('<p>my content</p>', $email->getHtmlBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
// Ensure setting data causes html() to be updated
|
2021-03-03 23:18:46 +01:00
|
|
|
$email->setData([
|
2022-10-19 04:16:14 +02:00
|
|
|
'EmailContent' => '<p>your content</p>'
|
2021-03-03 23:18:46 +01:00
|
|
|
]);
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->send();
|
|
|
|
$this->assertStringContainsString('<p>your content</p>', $email->getHtmlBody());
|
2021-03-03 23:18:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
// Ensure removing data causes html() to be updated
|
2021-03-03 23:18:46 +01:00
|
|
|
$email->removeData('EmailContent');
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->send();
|
|
|
|
$this->assertStringNotContainsString('<p>your content</p>', $email->getHtmlBody());
|
2021-03-03 23:18:46 +01:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
// Ensure adding data causes html() to be updated
|
2021-03-03 23:18:46 +01:00
|
|
|
$email->addData([
|
2022-10-19 04:16:14 +02:00
|
|
|
'EmailContent' => '<p>their content</p>'
|
2021-03-03 23:18:46 +01:00
|
|
|
]);
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->send();
|
|
|
|
$this->assertStringContainsString('<p>their content</p>', $email->getHtmlBody());
|
2021-03-03 23:18:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testRenderPlainOnly(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-01-13 02:48:46 +01:00
|
|
|
'EmailContent' => 'test content',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2022-10-19 04:16:14 +02:00
|
|
|
$email->sendPlain();
|
|
|
|
$this->assertSame('test content', $email->getTextBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testMultipleEmailSends(): void
|
2017-01-13 02:48:46 +01:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$email = new Email(to: 'to@example.com');
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2022-10-19 04:16:14 +02:00
|
|
|
'EmailContent' => '<p>Test</p>',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertSame(null, $email->getHtmlBody());
|
|
|
|
$this->assertSame(null, $email->getTextBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->send();
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertStringContainsString('<p>Test</p>', $email->getHtmlBody());
|
|
|
|
$this->assertSame('Test', $email->getTextBody());
|
2017-01-13 02:48:46 +01:00
|
|
|
//send again
|
|
|
|
$email->send();
|
2022-10-19 04:16:14 +02:00
|
|
|
$this->assertStringContainsString('<p>Test</p>', $email->getHtmlBody());
|
|
|
|
$this->assertSame('Test', $email->getTextBody());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-10-18 05:09:14 +02:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testGetDefaultFrom(): void
|
2022-03-07 04:18:57 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$class = new \ReflectionClass(Email::class);
|
|
|
|
$method = $class->getMethod('getDefaultFrom');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
// default to no-reply@mydomain.com if admin_email config not set
|
2022-03-09 22:37:47 +01:00
|
|
|
Email::config()->set('admin_email', null);
|
2022-03-07 04:18:57 +01:00
|
|
|
$this->assertSame('no-reply@www.mysite.com', $method->invokeArgs($email, []));
|
|
|
|
|
2022-03-09 22:37:47 +01:00
|
|
|
// default to no-reply@mydomain.com if admin_email config is misconfigured
|
|
|
|
Email::config()->set('admin_email', 123);
|
|
|
|
$this->assertSame('no-reply@www.mysite.com', $method->invokeArgs($email, []));
|
|
|
|
|
|
|
|
// use admin_email config string syntax
|
2022-03-07 04:18:57 +01:00
|
|
|
Email::config()->set('admin_email', 'myadmin@somewhere.com');
|
|
|
|
$this->assertSame('myadmin@somewhere.com', $method->invokeArgs($email, []));
|
2022-03-09 22:37:47 +01:00
|
|
|
$this->assertTrue(true);
|
|
|
|
|
|
|
|
// use admin_email config array syntax
|
|
|
|
Email::config()->set('admin_email', ['anotheradmin@somewhere.com' => 'Admin-email']);
|
2023-01-26 02:40:13 +01:00
|
|
|
$this->assertSame(
|
|
|
|
['anotheradmin@somewhere.com' => 'Admin-email'],
|
|
|
|
$method->invokeArgs($email, [])
|
|
|
|
);
|
2022-03-09 22:37:47 +01:00
|
|
|
$this->assertTrue(true);
|
2022-03-07 04:18:57 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 05:09:14 +02:00
|
|
|
/**
|
2022-10-19 04:16:14 +02:00
|
|
|
* @dataProvider provideCreateAddressArray
|
2017-10-18 05:09:14 +02:00
|
|
|
*/
|
2022-10-19 04:16:14 +02:00
|
|
|
public function testCreateAddressArray(string|array $address, string $name, array $expected): void
|
2017-10-18 05:09:14 +02:00
|
|
|
{
|
2022-10-19 04:16:14 +02:00
|
|
|
$method = new \ReflectionMethod(Email::class, 'createAddressArray');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$obj = new Email();
|
|
|
|
$actual = $method->invoke($obj, $address, $name);
|
|
|
|
for ($i = 0; $i < count($expected); $i++) {
|
|
|
|
$this->assertSame($expected[$i]->getAddress(), $actual[$i]->getAddress());
|
|
|
|
$this->assertSame($expected[$i]->getName(), $actual[$i]->getName());
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 05:09:14 +02:00
|
|
|
|
2022-10-19 04:16:14 +02:00
|
|
|
public function provideCreateAddressArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'my@email.com',
|
|
|
|
'My name',
|
|
|
|
[
|
|
|
|
new Address('my@email.com', 'My name'),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'my@email.com' => 'My name',
|
|
|
|
'other@email.com' => 'My other name',
|
|
|
|
'no-name@email.com'
|
|
|
|
],
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
new Address('my@email.com', 'My name'),
|
|
|
|
new Address('other@email.com', 'My other name'),
|
|
|
|
new Address('no-name@email.com', ''),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
2017-10-18 05:09:14 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|