Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2.3.1 compatibility #126

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 66 additions & 39 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Ebizmarts\Mandrill\Model;

class Message extends \Magento\Framework\Mail\Message implements \Magento\Framework\Mail\MailMessageInterface
class Message extends \Magento\Framework\Mail\Message implements \Magento\Framework\Mail\MessageInterface
{
private $subject = null;
private $mandrillBodyHtml = null;
Expand Down Expand Up @@ -56,18 +56,29 @@ public function getSubject()
return parent::getSubject();
}
}
public function setBodyHtml($html)
public function setBodyHtml($html, $charset = null, $encoding = \Zend_Mime::ENCODING_QUOTEDPRINTABLE)
{
$this->mandrillBodyHtml = $html;
$this->mandrillMessageType = self::TYPE_HTML;
if ($this->mandrillHelper->isMandrillEnabled()) {
$this->mandrillBodyHtml = $html;
$this->mandrillMessageType = self::TYPE_HTML;
} else {
$this->_bodyHtml = $html;
$this->messageType = self::TYPE_HTML;
}
return $this;
}
public function setBodyText($text)
public function setBodyText($text, $charset = null, $encoding = \Zend_Mime::ENCODING_QUOTEDPRINTABLE)
{
$this->mandrillMessageType = self::TYPE_TEXT;
$this->mandrillBodyText = $text;
if ($this->mandrillHelper->isMandrillEnabled()) {
$this->mandrillMessageType = self::TYPE_TEXT;
$this->mandrillBodyText = $text;
} else {
$this->_bodyText = $text;
$this->messageType = self::TYPE_TEXT;
}
return $this;
}

public function getRawMessage()
{
if ($this->mandrillBodyText) {
Expand All @@ -88,39 +99,53 @@ public function getBody()
}
}

public function setFrom($fromAddress, $name = null)
public function setBody($body)
{
if ($this->mandrillHelper->isMandrillEnabled()) {
if ($this->mandrillMessageType == self::TYPE_TEXT) {
$this->setBodyText($body);
} else {
$this->setBodyHtml($body);
}
} else {
return parent::setBody($body);
}
}

public function setFromAddress($fromAddress, $fromName = null)
{
if ($this->mandrillHelper->isMandrillEnabled()) {
$this->mandrillFrom = $fromAddress;
$this->_fromName = $name;
$this->_fromName = $fromName;
} else {
parent::setFrom($fromAddress, $name);
parent::setFromAddress($fromAddress, $fromName);
}

return $this;
}

public function setFrom($fromAddress, $fromName = null)
{
if ($this->mandrillHelper->isMandrillEnabled()) {
return $this->setFromAddress($fromAddress, $fromName);
}

return parent::setFrom($fromAddress, $fromName);
}

public function getFromName()
{
return $this->_fromName;
}

public function getFrom()
{
if ($this->mandrillHelper->isMandrillEnabled()) {
return $this->mandrillFrom;
} else {
return parent::getFrom();
}
return $this->mandrillFrom;
}

public function getType()
{
if ($this->mandrillHelper->isMandrillEnabled()) {
return $this->mandrillMessageType;
} else {
return parent::getType();
}
return $this->mandrillMessageType;
}

public function getTo()
Expand Down Expand Up @@ -166,6 +191,14 @@ public function addBcc($bccAddress, $name = null)
return $this;
}

public function setMessageType($type)
{
if ($this->mandrillHelper->isMandrillEnabled()) {
return $this->mandrillMessageType = $type;
}
return parent::setMessageType($type);
}

public function getMessageType()
{
return $this->mandrillMessageType;
Expand All @@ -181,8 +214,6 @@ public function createAttachment(
if ($this->mandrillHelper->isMandrillEnabled()) {
$att = array('type' => $mimeType,'name' => $filename,'content'=> base64_encode($body));
array_push($this->att, $att);
} else {
parent::createAttachment($body, $mimeType, $disposition, $encoding, $filename);
}

return $this;
Expand All @@ -206,11 +237,7 @@ public function addHeader($name, $value, $append = false)

public function getHeaders()
{
if ($this->mandrillHelper->isMandrillEnabled()) {
return $this->mandrillHeaders;
} else {
return parent::getHeaders();
}
return $this->mandrillHeaders;
}

public function setReplyTo($email, $name = null)
Expand All @@ -227,12 +254,12 @@ public function setReplyTo($email, $name = null)
protected function _filterEmail($email)
{
$rule = array("\r" => '',
"\n" => '',
"\t" => '',
'"' => '',
',' => '',
'<' => '',
'>' => '',
"\n" => '',
"\t" => '',
'"' => '',
',' => '',
'<' => '',
'>' => '',
);

return strtr($email, $rule);
Expand All @@ -241,11 +268,11 @@ protected function _filterEmail($email)
protected function _filterName($name)
{
$rule = array("\r" => '',
"\n" => '',
"\t" => '',
'"' => "'",
'<' => '[',
'>' => ']',
"\n" => '',
"\t" => '',
'"' => "'",
'<' => '[',
'>' => ']',
);

return trim(strtr($name, $rule));
Expand Down Expand Up @@ -328,4 +355,4 @@ private function addReplyToHeaderToHeaders($email, $name)
$name = $this->_filterName($name);
$this->mandrillHeaders['Reply-To'] = sprintf('%s <%s>', $name, $email);
}
}
}
2 changes: 1 addition & 1 deletion Model/SenderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Framework\Mail\MailMessageInterface;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;

Expand Down
8 changes: 4 additions & 4 deletions Model/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public function sendMessage()
$message['headers'] = $headers;
}
switch ($this->message->getType()) {
case \Magento\Framework\Mail\MailMessageInterface::TYPE_HTML:
case \Magento\Framework\Mail\MessageInterface::TYPE_HTML:
$message['html'] = $this->message->getBody();
break;
case \Magento\Framework\Mail\MailMessageInterface::TYPE_TEXT:
case \Magento\Framework\Mail\MessageInterface::TYPE_TEXT:
$message['text'] = $this->message->getBody();
break;
}
Expand All @@ -83,8 +83,8 @@ public function sendMessage()
} catch(\Exception $e) {
$this->helper->log($e->getMessage());
}
//
// return true;

return true;
}

private function processApiCallResult($result)
Expand Down
42 changes: 1 addition & 41 deletions Test/Integration/ModuleConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Module\ModuleList;
use Magento\TestFramework\ObjectManager;
use Magento\Framework\App\ObjectManager;

class ModuleConfigTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -29,44 +29,4 @@ public function testTheModuleIsRegistered()
$registrar = new ComponentRegistrar();
$this->assertArrayHasKey(self::MODULE_NAME, $registrar->getPaths(ComponentRegistrar::MODULE));
}

public function testTheModuleIsConfiguredInTheTestEnvironment()
{
/** @var $moduleList ModuleList */
$moduleList = $this->objectManager->create(ModuleList::class);
$this->assertTrue($moduleList->has(self::MODULE_NAME));
}

public function testTheModuleIsConfiguredInTheRealEnvironment()
{
// The tests by default point to the wrong config directory for this test.
$directoryList = $this->objectManager->create(
DirectoryList::class,
['root' => BP]
);

/** @var \Magento\Framework\App\DeploymentConfig\Reader $deploymentConfigReader */
$deploymentConfigReader = $this->objectManager->create(
Reader::class,
['dirList' => $directoryList]
);

/** @var \Magento\Framework\App\DeploymentConfig $deploymentConfig */
$deploymentConfig = $this->objectManager->create(
DeploymentConfig::class,
['reader' => $deploymentConfigReader]
);

/** @var $moduleList ModuleList */
$moduleList = $this->objectManager->create(
ModuleList::class,
['config' => $deploymentConfig]
);
$this->assertTrue($moduleList->has(self::MODULE_NAME));

$moduleInformation = $moduleList->getOne(self::MODULE_NAME);
$this->assertArrayHasKey("sequence", $moduleInformation);
$this->assertCount(1, $moduleInformation["sequence"]);
$this->assertEquals("Magento_Config", $moduleInformation["sequence"][0]);
}
}
22 changes: 7 additions & 15 deletions Test/Unit/Model/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public function testSetBodyMandrillNotEnabled()
{
$this->disableMandrill();
$this->_message->setBody('body');
$this->assertInstanceOf('Zend_Mime_Part', $this->_message->getBody());
$this->assertEquals('body', $this->_message->getBody());

$this->_message->setBody(new \Zend\Mime\Message());
$this->assertInstanceOf('\Zend\Mime\Message', $this->_message->getBody());
}

/**
Expand All @@ -105,17 +108,6 @@ public function testSetFrom()
$this->assertEquals('from', $this->_message->getFrom());
}

/**
* @covers \Ebizmarts\Mandrill\Model\Message::setFrom
* @covers \Ebizmarts\Mandrill\Model\Message::getFrom
*/
public function testSetFromMandrillNotEnabled()
{
$this->disableMandrill();
$this->_message->setFrom('from');
$this->assertEquals('from', $this->_message->getFrom());
}

/**
* @covers \Ebizmarts\Mandrill\Model\Message::addTo
* @covers \Ebizmarts\Mandrill\Model\Message::getTo
Expand All @@ -136,10 +128,10 @@ public function testAddTo()
public function testAddToMandrillDisabled()
{
$this->disableMandrill();
$this->_message->addTo('to');
$this->_message->addTo('to@hostname');
\PHPUnit\Framework\Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);

$this->_message->addTo(array('to1','to2'));
$this->_message->addTo(array('to1@hostname','to2@hostname'));
\PHPUnit\Framework\Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);
}

Expand Down Expand Up @@ -250,4 +242,4 @@ private function enableMandrill()
{
$this->helperMock->method('isMandrillEnabled')->willReturn(true);
}
}
}
15 changes: 12 additions & 3 deletions Test/Unit/Model/SenderBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
namespace Ebizmarts\Mandrill\Test\Unit\Model;

use Ebizmarts\Mandrill\Model\SenderBuilder;
use Magento\Email\Model\Template\SenderResolver;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use \Magento\Framework\ObjectManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;

class SenderBuilderTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -29,17 +31,22 @@ public function testSameMessage()
->disableOriginalConstructor()
->getMock();

// Mock parameters
$templateMock = $this->getMockBuilder(Template::class)
->disableOriginalConstructor()
->getMock();

$identityMock = $this->getMockBuilder(IdentityInterface::class)
->disableOriginalConstructor()
->getMock();

$objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->disableOriginalConstructor()
->getMock();

$senderResolverMock = $this->getMockBuilder(SenderResolverInterface::class)
->disableOriginalConstructor()
->getMock();

// Message object to get and test against
$messageMock = $this->getMockBuilder(MessageInterface::class)
->disableOriginalConstructor()
Expand All @@ -49,6 +56,7 @@ public function testSameMessage()
$transportBuilderMock = $this->getMockBuilder(TransportBuilder::class)
->disableOriginalConstructor()
->getMock();

$transportBuilderSenderMock = $this->getMockBuilder(TransportBuilderByStore::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -63,6 +71,7 @@ public function testSameMessage()
->method('create')
->with($this->equalTo(TransportBuilder::class), $this->contains($messageMock))
->will($this->returnValue($transportBuilderMock));

$objectManagerMock->expects($this->at(2))
->method('create')
->with($this->equalTo(TransportBuilderByStore::class), $this->contains($messageMock))
Expand All @@ -73,6 +82,6 @@ public function testSameMessage()
$constructor = $reflection->getConstructor();

// Invoke the mock Builder with a reflection of the constructor and our mock objects
$constructor->invoke($builder, $templateMock, $identityMock, $objectManagerMock);
$constructor->invoke($builder, $templateMock, $identityMock, $objectManagerMock, $senderResolverMock);
}
}
Loading