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;
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Email\Email;
|
2017-10-18 05:09:14 +02:00
|
|
|
use SilverStripe\Control\Email\Mailer;
|
2017-01-13 02:48:46 +01:00
|
|
|
use SilverStripe\Control\Email\SwiftMailer;
|
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\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\Security\Member;
|
2017-10-18 05:09:14 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2017-01-13 02:48:46 +01:00
|
|
|
use Swift_Attachment;
|
|
|
|
use Swift_Mailer;
|
|
|
|
use Swift_Message;
|
|
|
|
use Swift_NullTransport;
|
|
|
|
use Swift_RfcComplianceException;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class EmailTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testAddAttachment()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
|
|
|
$this->assertCount(1, $children);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
/** @var Swift_Attachment $child */
|
|
|
|
$child = reset($children);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Swift_Attachment::class, $child);
|
|
|
|
$this->assertEquals('text/plain', $child->getContentType());
|
|
|
|
$this->assertEquals('attachment.txt', $child->getFilename());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testAddAttachmentFromData()
|
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');
|
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $children);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
/** @var Swift_Attachment $child */
|
|
|
|
$child = reset($children);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertInstanceOf(Swift_Attachment::class, $child);
|
|
|
|
$this->assertEquals('foo bar', $child->getBody());
|
|
|
|
$this->assertEquals('text/plain', $child->getContentType());
|
|
|
|
$this->assertEquals('foo.txt', $child->getFilename());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:15:18 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideValidEmailAddresses
|
|
|
|
*/
|
|
|
|
public function testValidEmailAddress($email)
|
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
|
|
|
|
*/
|
|
|
|
public function testInvalidEmailAddress($email)
|
|
|
|
{
|
|
|
|
$this->assertFalse(Email::is_valid_address($email));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-09-26 00:15:18 +02:00
|
|
|
public function provideValidEmailAddresses()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['test@example.com', 'test-123@sub.example.com'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidEmailAddresses()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['foo.bar@', '@example.com', 'foo@'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testObfuscate()
|
|
|
|
{
|
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendPlain()
|
|
|
|
{
|
2017-10-18 05:09:14 +02:00
|
|
|
$email = $this->makeEmailMock('Test send plain');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
// email should not call render if a body is supplied
|
2017-10-18 05:09:14 +02:00
|
|
|
$email->expects($this->never())->method('renderWith');
|
2017-01-13 02:48:46 +01:00
|
|
|
$successful = $email->sendPlain();
|
|
|
|
|
|
|
|
$this->assertTrue($successful);
|
|
|
|
$this->assertEmpty($email->getFailedRecipients());
|
|
|
|
|
2017-10-18 05:09:14 +02:00
|
|
|
/** @var TestMailer $mailer */
|
|
|
|
$mailer = Injector::inst()->get(Mailer::class);
|
|
|
|
$sentMail = $mailer->findEmail('to@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($sentMail));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertEquals('to@example.com', $sentMail['To']);
|
|
|
|
$this->assertEquals('from@example.com', $sentMail['From']);
|
|
|
|
$this->assertEquals('Test send plain', $sentMail['Subject']);
|
2017-10-18 05:09:14 +02:00
|
|
|
$this->assertEquals('Body for Test send plain', $sentMail['Content']);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']);
|
|
|
|
$child = reset($sentMail['AttachedFiles']);
|
|
|
|
$this->assertEquals('text/plain', $child['mimetype']);
|
|
|
|
$this->assertEquals('attachment.txt', $child['filename']);
|
|
|
|
$this->assertEquals('Hello, I\'m a text document.', $child['contents']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSend()
|
|
|
|
{
|
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
|
2017-10-18 05:09:14 +02:00
|
|
|
$email = $this->makeEmailMock('Test send HTML');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
// email should not call render if a body is supplied
|
2017-10-18 05:09:14 +02:00
|
|
|
$email->expects($this->never())->method('renderWith');
|
2017-01-13 02:48:46 +01:00
|
|
|
$successful = $email->send();
|
|
|
|
|
|
|
|
$this->assertTrue($successful);
|
|
|
|
$this->assertEmpty($email->getFailedRecipients());
|
|
|
|
|
2017-10-18 05:09:14 +02:00
|
|
|
/** @var TestMailer $mailer */
|
|
|
|
$mailer = Injector::inst()->get(Mailer::class);
|
|
|
|
$sentMail = $mailer->findEmail('to@example.com');
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($sentMail));
|
|
|
|
|
|
|
|
$this->assertEquals('to@example.com', $sentMail['To']);
|
|
|
|
$this->assertEquals('from@example.com', $sentMail['From']);
|
|
|
|
$this->assertEquals('Test send HTML', $sentMail['Subject']);
|
2017-10-18 05:09:14 +02:00
|
|
|
$this->assertEquals('Body for Test send HTML', $sentMail['Content']);
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']);
|
|
|
|
$child = reset($sentMail['AttachedFiles']);
|
|
|
|
$this->assertEquals('text/plain', $child['mimetype']);
|
|
|
|
$this->assertEquals('attachment.txt', $child['filename']);
|
|
|
|
$this->assertEquals('Hello, I\'m a text document.', $child['contents']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderedSend()
|
|
|
|
{
|
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
|
|
|
|
$email = $this->getMockBuilder(Email::class)
|
|
|
|
->enableProxyingToOriginalMethods()
|
|
|
|
->getMock();
|
2017-10-18 05:09:14 +02:00
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$email->setTo('to@example.com');
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-01-13 02:48:46 +01:00
|
|
|
'EmailContent' => 'test',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertFalse($email->hasPlainPart());
|
|
|
|
$this->assertEmpty($email->getBody());
|
|
|
|
// these seem to fail for some reason :/
|
|
|
|
//$email->expects($this->once())->method('render');
|
|
|
|
//$email->expects($this->once())->method('generatePlainPartFromBody');
|
|
|
|
$email->send();
|
|
|
|
$this->assertTrue($email->hasPlainPart());
|
|
|
|
$this->assertNotEmpty($email->getBody());
|
|
|
|
}
|
|
|
|
|
2017-10-18 05:09:14 +02:00
|
|
|
public function testRenderedSendSubclass()
|
|
|
|
{
|
|
|
|
// Include dev theme
|
|
|
|
SSViewer::set_themes([
|
|
|
|
'silverstripe/framework:/tests/php/Control/Email/EmailTest',
|
|
|
|
'$default',
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
|
|
|
|
$email = $this->getMockBuilder(EmailSubClass::class)
|
|
|
|
->enableProxyingToOriginalMethods()
|
|
|
|
->getMock();
|
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$email->setTo('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
|
|
|
$this->assertFalse($email->hasPlainPart());
|
|
|
|
$this->assertEmpty($email->getBody());
|
|
|
|
$email->send();
|
|
|
|
$this->assertTrue($email->hasPlainPart());
|
|
|
|
$this->assertNotEmpty($email->getBody());
|
|
|
|
$this->assertContains('<h1>Email Sub-class</h1>', $email->getBody());
|
|
|
|
}
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testConsturctor()
|
|
|
|
{
|
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',
|
|
|
|
'body',
|
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());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
$this->assertCount(1, $email->getTo());
|
|
|
|
$this->assertContains('to@example.com', array_keys($email->getTo()));
|
|
|
|
$this->assertEquals('subject', $email->getSubject());
|
|
|
|
$this->assertEquals('body', $email->getBody());
|
|
|
|
$this->assertCount(1, $email->getCC());
|
|
|
|
$this->assertContains('cc@example.com', array_keys($email->getCC()));
|
|
|
|
$this->assertCount(1, $email->getBCC());
|
|
|
|
$this->assertContains('bcc@example.com', array_keys($email->getBCC()));
|
|
|
|
$this->assertEquals('bounce@example.com', $email->getReturnPath());
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testGetSwiftMessage()
|
|
|
|
{
|
|
|
|
$email = new Email(
|
|
|
|
'from@example.com',
|
|
|
|
'to@example.com',
|
|
|
|
'subject',
|
|
|
|
'body',
|
|
|
|
'cc@example.com',
|
|
|
|
'bcc@example.com',
|
|
|
|
'bounce@example.com'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
$swiftMessage = $email->getSwiftMessage();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Swift_Message::class, $swiftMessage);
|
|
|
|
|
|
|
|
$this->assertCount(1, $swiftMessage->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($swiftMessage->getFrom()));
|
|
|
|
$this->assertCount(1, $swiftMessage->getTo());
|
|
|
|
$this->assertContains('to@example.com', array_keys($swiftMessage->getTo()));
|
|
|
|
$this->assertEquals('subject', $swiftMessage->getSubject());
|
|
|
|
$this->assertEquals('body', $swiftMessage->getBody());
|
|
|
|
$this->assertCount(1, $swiftMessage->getCC());
|
|
|
|
$this->assertContains('cc@example.com', array_keys($swiftMessage->getCc()));
|
|
|
|
$this->assertCount(1, $swiftMessage->getBCC());
|
|
|
|
$this->assertContains('bcc@example.com', array_keys($swiftMessage->getBcc()));
|
|
|
|
$this->assertEquals('bounce@example.com', $swiftMessage->getReturnPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSwiftMessage()
|
|
|
|
{
|
|
|
|
Email::config()->update('admin_email', 'admin@example.com');
|
|
|
|
DBDatetime::set_mock_now('2017-01-01 07:00:00');
|
|
|
|
$email = new Email();
|
|
|
|
$swiftMessage = new Swift_Message();
|
|
|
|
$email->setSwiftMessage($swiftMessage);
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('admin@example.com', array_keys($swiftMessage->getFrom()));
|
|
|
|
$this->assertEquals(strtotime('2017-01-01 07:00:00'), $swiftMessage->getDate());
|
|
|
|
$this->assertEquals($swiftMessage, $email->getSwiftMessage());
|
|
|
|
|
|
|
|
// check from field is retained
|
|
|
|
$swiftMessage = new Swift_Message();
|
|
|
|
$swiftMessage->setFrom('from@example.com');
|
|
|
|
$email->setSwiftMessage($swiftMessage);
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdminEmailApplied()
|
|
|
|
{
|
|
|
|
Email::config()->update('admin_email', 'admin@example.com');
|
|
|
|
$email = new Email();
|
|
|
|
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('admin@example.com', array_keys($email->getFrom()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFrom()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com');
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetFrom()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com');
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
$email->setFrom('new-from@example.com');
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('new-from@example.com', array_keys($email->getFrom()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddFrom()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com');
|
|
|
|
$this->assertCount(1, $email->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
$email->addFrom('new-from@example.com');
|
|
|
|
$this->assertCount(2, $email->getFrom());
|
|
|
|
$this->assertContains('from@example.com', array_keys($email->getFrom()));
|
|
|
|
$this->assertContains('new-from@example.com', array_keys($email->getFrom()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetSender()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getSender());
|
|
|
|
$email->setSender('sender@example.com', 'Silver Stripe');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['sender@example.com' => 'Silver Stripe'], $email->getSender());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetReturnPath()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getReturnPath());
|
|
|
|
$email->setReturnPath('return@example.com');
|
|
|
|
$this->assertEquals('return@example.com', $email->getReturnPath());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testSetGetTo()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-01-13 02:48:46 +01:00
|
|
|
$email = new Email('from@example.com', 'to@example.com');
|
|
|
|
$this->assertCount(1, $email->getTo());
|
|
|
|
$this->assertContains('to@example.com', array_keys($email->getTo()));
|
|
|
|
$email->setTo('new-to@example.com', 'Silver Stripe');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['new-to@example.com' => 'Silver Stripe'], $email->getTo());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddTo()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com', 'to@example.com');
|
|
|
|
$this->assertCount(1, $email->getTo());
|
|
|
|
$this->assertContains('to@example.com', array_keys($email->getTo()));
|
|
|
|
$email->addTo('new-to@example.com');
|
|
|
|
$this->assertCount(2, $email->getTo());
|
|
|
|
$this->assertContains('to@example.com', array_keys($email->getTo()));
|
|
|
|
$this->assertContains('new-to@example.com', array_keys($email->getTo()));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
public function testSetGetCC()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com', 'to@example.com', 'subject', 'body', 'cc@example.com');
|
|
|
|
$this->assertCount(1, $email->getCC());
|
|
|
|
$this->assertContains('cc@example.com', array_keys($email->getCC()));
|
|
|
|
$email->setCC('new-cc@example.com', 'Silver Stripe');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['new-cc@example.com' => 'Silver Stripe'], $email->getCC());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddCC()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com', 'to@example.com', 'subject', 'body', 'cc@example.com');
|
|
|
|
$this->assertCount(1, $email->getCC());
|
|
|
|
$this->assertContains('cc@example.com', array_keys($email->getCC()));
|
|
|
|
$email->addCC('new-cc@example.com', 'Silver Stripe');
|
|
|
|
$this->assertCount(2, $email->getCC());
|
|
|
|
$this->assertContains('cc@example.com', array_keys($email->getCC()));
|
|
|
|
$this->assertContains('new-cc@example.com', array_keys($email->getCC()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetGetBCC()
|
|
|
|
{
|
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',
|
|
|
|
'body',
|
2016-12-16 05:34:21 +01:00
|
|
|
'cc@example.com',
|
|
|
|
'bcc@example.com'
|
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getBCC());
|
|
|
|
$this->assertContains('bcc@example.com', array_keys($email->getBCC()));
|
|
|
|
$email->setBCC('new-bcc@example.com', 'Silver Stripe');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['new-bcc@example.com' => 'Silver Stripe'], $email->getBCC());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddBCC()
|
|
|
|
{
|
|
|
|
$email = new Email(
|
|
|
|
'from@example.com',
|
|
|
|
'to@example.com',
|
|
|
|
'subject',
|
|
|
|
'body',
|
|
|
|
'cc@example.com',
|
|
|
|
'bcc@example.com'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getBCC());
|
|
|
|
$this->assertContains('bcc@example.com', array_keys($email->getBCC()));
|
|
|
|
$email->addBCC('new-bcc@example.com', 'Silver Stripe');
|
|
|
|
$this->assertCount(2, $email->getBCC());
|
|
|
|
$this->assertContains('bcc@example.com', array_keys($email->getBCC()));
|
|
|
|
$this->assertContains('new-bcc@example.com', array_keys($email->getBCC()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReplyTo()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getReplyTo());
|
|
|
|
$email->setReplyTo('reply-to@example.com', 'Silver Stripe');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['reply-to@example.com' => 'Silver Stripe'], $email->getReplyTo());
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->addReplyTo('new-reply-to@example.com');
|
|
|
|
$this->assertCount(2, $email->getReplyTo());
|
|
|
|
$this->assertContains('reply-to@example.com', array_keys($email->getReplyTo()));
|
|
|
|
$this->assertContains('new-reply-to@example.com', array_keys($email->getReplyTo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubject()
|
|
|
|
{
|
|
|
|
$email = new Email('from@example.com', 'to@example.com', 'subject');
|
|
|
|
$this->assertEquals('subject', $email->getSubject());
|
|
|
|
$email->setSubject('new subject');
|
|
|
|
$this->assertEquals('new subject', $email->getSubject());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPriority()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEquals(3, $email->getPriority());
|
|
|
|
$email->setPriority(5);
|
|
|
|
$this->assertEquals(5, $email->getPriority());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testData()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getData());
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-01-13 02:48:46 +01:00
|
|
|
'Title' => 'My Title',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertCount(1, $email->getData());
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Title' => 'My Title'], $email->getData());
|
2017-01-13 02:48:46 +01:00
|
|
|
|
|
|
|
$email->addData('Content', 'My content');
|
|
|
|
$this->assertCount(2, $email->getData());
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([
|
2017-01-13 02:48:46 +01:00
|
|
|
'Title' => 'My Title',
|
|
|
|
'Content' => 'My content',
|
2020-04-20 19:58:09 +02:00
|
|
|
], $email->getData());
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->removeData('Title');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Content' => 'My content'], $email->getData());
|
2017-01-13 02:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataWithViewableData()
|
|
|
|
{
|
|
|
|
$member = new Member();
|
|
|
|
$member->FirstName = 'First Name';
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getData());
|
|
|
|
$email->setData($member);
|
|
|
|
$this->assertEquals($member, $email->getData());
|
|
|
|
$email->addData('Test', 'Test value');
|
|
|
|
$this->assertEquals('Test value', $email->getData()->Test);
|
|
|
|
$email->removeData('Test');
|
|
|
|
$this->assertNull($email->getData()->Test);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBody()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getBody());
|
|
|
|
$email->setBody('<h1>Title</h1>');
|
|
|
|
$this->assertEquals('<h1>Title</h1>', $email->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHTMLTemplate()
|
|
|
|
{
|
2017-10-18 05:09:14 +02:00
|
|
|
// Include dev theme
|
|
|
|
SSViewer::set_themes([
|
|
|
|
'silverstripe/framework:/tests/php/Control/Email/EmailTest',
|
|
|
|
'$default',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPlainTemplate()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$this->assertEmpty($email->getPlainTemplate());
|
|
|
|
$email->setPlainTemplate('MyTemplate');
|
|
|
|
$this->assertEquals('MyTemplate', $email->getPlainTemplate());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFailedRecipients()
|
|
|
|
{
|
|
|
|
$mailer = new SwiftMailer();
|
|
|
|
/** @var Swift_NullTransport|PHPUnit_Framework_MockObject_MockObject $transport */
|
|
|
|
$transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock();
|
|
|
|
$transport->expects($this->once())
|
2017-10-18 05:09:14 +02:00
|
|
|
->method('send')
|
|
|
|
->willThrowException(new Swift_RfcComplianceException('Bad email'));
|
2017-01-13 02:48:46 +01:00
|
|
|
$mailer->setSwiftMailer(new Swift_Mailer($transport));
|
|
|
|
$email = new Email();
|
|
|
|
$email->setTo('to@example.com');
|
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$mailer->send($email);
|
|
|
|
$this->assertCount(1, $email->getFailedRecipients());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEmail()
|
|
|
|
{
|
|
|
|
$this->assertTrue((new Email)->IsEmail());
|
|
|
|
}
|
|
|
|
|
2017-10-18 05:09:14 +02:00
|
|
|
public function testRenderAgain()
|
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' => 'my content',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->render();
|
|
|
|
$this->assertContains('my content', $email->getBody());
|
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
|
|
|
$this->assertCount(1, $children);
|
|
|
|
$plainPart = reset($children);
|
|
|
|
$this->assertEquals('my content', $plainPart->getBody());
|
|
|
|
|
|
|
|
// ensure repeat renders don't add multiple plain parts
|
|
|
|
$email->render();
|
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenderPlainOnly()
|
|
|
|
{
|
|
|
|
$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
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$email->render(true);
|
|
|
|
$this->assertEquals('text/plain', $email->getSwiftMessage()->getContentType());
|
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasPlainPart()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-01-13 02:48:46 +01:00
|
|
|
'EmailContent' => 'test',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
//emails are assumed to be HTML by default
|
|
|
|
$this->assertFalse($email->hasPlainPart());
|
|
|
|
//make sure plain attachments aren't picked up as a plain part
|
|
|
|
$email->addAttachmentFromData('data', 'attachent.txt', 'text/plain');
|
|
|
|
$this->assertFalse($email->hasPlainPart());
|
|
|
|
$email->getSwiftMessage()->addPart('plain', 'text/plain');
|
|
|
|
$this->assertTrue($email->hasPlainPart());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneratePlainPartFromBody()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
|
|
|
$email->setBody('<h1>Test</h1>');
|
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren());
|
|
|
|
$email->generatePlainPartFromBody();
|
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
|
|
|
$this->assertCount(1, $children);
|
|
|
|
$plainPart = reset($children);
|
|
|
|
$this->assertContains('Test', $plainPart->getBody());
|
|
|
|
$this->assertNotContains('<h1>Test</h1>', $plainPart->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleEmailSends()
|
|
|
|
{
|
|
|
|
$email = new Email();
|
2020-04-20 19:58:09 +02:00
|
|
|
$email->setData([
|
2017-01-13 02:48:46 +01:00
|
|
|
'EmailContent' => 'Test',
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-01-13 02:48:46 +01:00
|
|
|
$this->assertEmpty($email->getBody());
|
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren());
|
|
|
|
$email->send();
|
|
|
|
$this->assertContains('Test', $email->getBody());
|
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
|
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
|
|
|
/** @var \Swift_MimePart $plainPart */
|
|
|
|
$plainPart = reset($children);
|
|
|
|
$this->assertContains('Test', $plainPart->getBody());
|
|
|
|
|
|
|
|
|
|
|
|
//send again
|
|
|
|
$email->send();
|
|
|
|
$this->assertContains('Test', $email->getBody());
|
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
|
|
|
|
$children = $email->getSwiftMessage()->getChildren();
|
|
|
|
/** @var \Swift_MimePart $plainPart */
|
|
|
|
$plainPart = reset($children);
|
|
|
|
$this->assertContains('Test', $plainPart->getBody());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-10-18 05:09:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|Email
|
|
|
|
*/
|
|
|
|
protected function makeEmailMock($subject)
|
|
|
|
{
|
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */
|
|
|
|
$email = $this->getMockBuilder(Email::class)
|
|
|
|
->enableProxyingToOriginalMethods()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$email->setFrom('from@example.com');
|
|
|
|
$email->setTo('to@example.com');
|
|
|
|
$email->setSubject($subject);
|
|
|
|
$email->setBody("Body for {$subject}");
|
|
|
|
$email->setCC('cc@example.com');
|
|
|
|
$email->setBCC('bcc@example.com');
|
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain');
|
|
|
|
return $email;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|