MessagePluginTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Dotdigitalgroup\Email\Test\Unit\Plugin;
  3. use Dotdigitalgroup\Email\Helper\Transactional;
  4. use Dotdigitalgroup\Email\Plugin\MessagePlugin;
  5. use Magento\Email\Model\ResourceModel\Template;
  6. use Magento\Email\Model\TemplateFactory;
  7. use Magento\Framework\Mail\MessageInterface;
  8. use Magento\Framework\Registry;
  9. use PHPUnit\Framework\TestCase;
  10. class MessagePluginTest extends TestCase
  11. {
  12. /**
  13. * @var Registry|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $registryMock;
  16. /**
  17. * @var Transactional|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $transactionalHelperMock;
  20. /**
  21. * @var Template|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $templateResourceModelMock;
  24. /**
  25. * @var TemplateFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $templateModelMock;
  28. /**
  29. * @var MessagePlugin
  30. */
  31. private $plugin;
  32. /**
  33. * @var MessageInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $messageMock;
  36. /**
  37. * @return void
  38. */
  39. protected function setUp()
  40. {
  41. $this->messageMock = $this->createMock(MessageInterface::class);
  42. $this->registryMock = $this->createMock(Registry::class);
  43. $this->transactionalHelperMock = $this->createMock(Transactional::class);
  44. $this->templateResourceModelMock = $this->createMock(Template::class);
  45. $this->templateModelMock = $this->createMock(TemplateFactory::class);
  46. $this->plugin = new MessagePlugin(
  47. $this->registryMock,
  48. $this->transactionalHelperMock,
  49. $this->templateResourceModelMock,
  50. $this->templateModelMock
  51. );
  52. }
  53. public function testNoActionTakenIfNotFromTemplateRoute()
  54. {
  55. $storeId = 1;
  56. $this->mockRegistry(null, $storeId);
  57. $this->mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, true);
  58. $this->templateModelMock->expects($this->never())
  59. ->method('create');
  60. $this->plugin->afterSetBody($this->messageMock, null);
  61. }
  62. public function testNoActionTakenIfDotmailerSMTPIsDisabled()
  63. {
  64. $storeId = 1;
  65. $this->mockRegistry(123456, $storeId);
  66. $this->mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, false);
  67. $this->templateModelMock->expects($this->never())
  68. ->method('create');
  69. $this->plugin->afterSetBody($this->messageMock, null);
  70. }
  71. public function testFromAddressNotSetWhenNotADotmailerTemplate()
  72. {
  73. $templateId = 123456;
  74. $storeId = 1;
  75. $this->mockRegistry($templateId, $storeId);
  76. $this->mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, true);
  77. $this->mockTemplateCollectionToReturnTemplate(false, $templateId, '', '');
  78. $this->messageMock->expects($this->never())
  79. ->method('setFrom');
  80. $this->plugin->afterSetBody($this->messageMock, null);
  81. }
  82. public function testFromSetWhenDotmailerTemplateAndDotmailerSmtpIsEnabled()
  83. {
  84. $templateId = 123456;
  85. $storeId = 1;
  86. $senderEmail = 'test@dotmailer.com';
  87. $senderName = 'dotmailer';
  88. $this->mockRegistry($templateId, $storeId);
  89. $this->mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, true);
  90. $this->mockTemplateCollectionToReturnTemplate(true, $templateId, $senderEmail, $senderName);
  91. $this->messageMock->expects($this->once())
  92. ->method('setFrom')
  93. ->with($senderEmail, $senderName);
  94. $this->plugin->afterSetBody($this->messageMock, null);
  95. }
  96. public function testFromClearedWhenZendMail()
  97. {
  98. $templateId = 123456;
  99. $storeId = 1;
  100. $senderEmail = 'test@dotmailer.com';
  101. $senderName = 'dotmailer';
  102. $this->mockRegistry($templateId, $storeId);
  103. $this->mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, true);
  104. $this->mockTemplateCollectionToReturnTemplate(true, $templateId, $senderEmail, $senderName);
  105. $this->messageMock = $this->createMock(Magento22MailClassTestDouble::class);
  106. $this->messageMock->expects($this->once())
  107. ->method('clearFrom');
  108. $this->messageMock->expects($this->once())
  109. ->method('setFrom')
  110. ->with($senderEmail, $senderName);
  111. $this->plugin->afterSetBody($this->messageMock, null);
  112. }
  113. private function mockRegistry($templateId, $storeId)
  114. {
  115. $this->registryMock->method('registry')
  116. ->withConsecutive(
  117. ['dotmailer_current_template_id'],
  118. ['transportBuilderPluginStoreId']
  119. )
  120. ->willReturnOnConsecutiveCalls($templateId, $storeId);
  121. }
  122. private function mockTransactionalHelperToReturnValueForSMTPEnabled($storeId, $value)
  123. {
  124. $this->transactionalHelperMock->method('isEnabled')
  125. ->with($storeId)
  126. ->willReturn($value);
  127. }
  128. private function mockTemplateCollectionToReturnTemplate(
  129. $isDotmailerTemplate,
  130. $templateId,
  131. $senderEmail,
  132. $senderName
  133. ) {
  134. $templateCode = 'dm_template_code';
  135. $templateModelMock = $this->createMock(\Magento\Email\Model\Template::class);
  136. $templateModelMock->method('__call')
  137. ->withConsecutive(
  138. [$this->equalTo('getTemplateCode')],
  139. [$this->equalTo('getTemplateSenderEmail')],
  140. [$this->equalTo('getTemplateSenderName')]
  141. )
  142. ->willReturnOnConsecutiveCalls(
  143. $templateCode,
  144. $senderEmail,
  145. $senderName
  146. );
  147. $this->templateModelMock->method('create')
  148. ->willReturn($templateModelMock);
  149. $this->templateResourceModelMock->expects($this->once())
  150. ->method('load')
  151. ->with($templateModelMock, $this->stringContains($templateId));
  152. $this->transactionalHelperMock->method('isDotmailerTemplate')
  153. ->willReturn($isDotmailerTemplate);
  154. return $templateModelMock;
  155. }
  156. }