Skip to content

Commit 138dc92

Browse files
committed
MC-15295: Added integration test
1 parent cd11bda commit 138dc92

File tree

4 files changed

+273
-17
lines changed

4 files changed

+273
-17
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Mail;
9+
10+
use Magento\Framework\Exception\MailException;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Class EmailMessageTest
17+
*/
18+
class EmailMessageTest extends TestCase
19+
{
20+
private const ATTACHMENT_FILE_NAME = 'di.xml';
21+
private const XML_TYPE = 'text/xml';
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $di;
26+
27+
/**
28+
* @var MimePartInterfaceFactory
29+
*/
30+
private $mimePartFactory;
31+
32+
/**
33+
* @var MimeMessageInterfaceFactory
34+
*/
35+
private $mimeMessageFactory;
36+
37+
/**
38+
* @var AddressConverter
39+
*/
40+
private $messageConverter;
41+
42+
/**
43+
* @var EmailMessageInterfaceFactory
44+
*/
45+
private $messageFactory;
46+
47+
/**
48+
* @var AddressFactory
49+
*/
50+
private $addressFactory;
51+
52+
/**
53+
* @var array
54+
*/
55+
private $addressList = [
56+
'to' => [
57+
['email' => 'to@adobe.com', 'name' => 'Addressee']
58+
],
59+
'replyTo' => ['email' => 'replyTo@adobe.com', 'name' => 'Reply To Address'],
60+
'from' => 'from@adobe.com',
61+
'sender' => ['email' => 'sender@adobe.com', 'name' => 'Sender'],
62+
'cc' => [
63+
'cc1@adobe.com' => 'CC 1 Address',
64+
'cc2@adobe.com' => 'CC 2 Address',
65+
'cc3@adobe.com' => 'CC 3 Address',
66+
],
67+
'bcc' => ['bcc1@adobe.com', 'bcc2@adobe.com'],
68+
];
69+
70+
/**
71+
* @var string
72+
*/
73+
private $subject = 'Test subject';
74+
75+
/**
76+
* @var string
77+
*/
78+
private $description = 'Test description';
79+
80+
/**
81+
*
82+
* @return void
83+
*/
84+
protected function setUp()
85+
{
86+
$this->di = Bootstrap::getObjectManager();
87+
$this->mimePartFactory = $this->di->get(MimePartInterfaceFactory::class);
88+
$this->mimeMessageFactory = $this->di->get(MimeMessageInterfaceFactory::class);
89+
$this->messageConverter = $this->di->get(AddressConverter::class);
90+
$this->messageFactory = $this->di->get(EmailMessageInterfaceFactory::class);
91+
}
92+
93+
/**
94+
* @return array
95+
*/
96+
public function getEmailMessageDataProvider(): array
97+
{
98+
return [
99+
[
100+
'Content Test',
101+
MimeInterface::TYPE_TEXT
102+
], [
103+
104+
'<h1>Html message</h1>',
105+
MimeInterface::TYPE_HTML
106+
]
107+
];
108+
}
109+
110+
/**
111+
* Tests Email Message with Addresses
112+
*
113+
* @dataProvider getEmailMessageDataProvider
114+
* @param $content
115+
* @param $type
116+
* @return void
117+
* @throws MailException
118+
*/
119+
public function testEmailMessage($content, $type): void
120+
{
121+
$mimePart = $this->mimePartFactory->create([
122+
'content' => $content,
123+
'description' => $this->description,
124+
'type' => $type
125+
]);
126+
127+
$mimeMessage = $this->mimeMessageFactory->create([
128+
'parts' => [$mimePart]
129+
]);
130+
131+
$this->addressFactory = $this->di->get(AddressFactory::class);
132+
/** @var Address $addressTo */
133+
$to = [
134+
$this->addressFactory->create(
135+
[
136+
'email' => $this->addressList['to'][0]['email'],
137+
'name' => $this->addressList['to'][0]['name']
138+
]
139+
)
140+
];
141+
142+
$from = [$this->messageConverter->convert($this->addressList['from'])];
143+
$cc = $this->messageConverter->convertMany($this->addressList['cc']);
144+
$replyTo = [
145+
$this->messageConverter->convert(
146+
$this->addressList['replyTo']['email'],
147+
$this->addressList['replyTo']['name']
148+
)
149+
];
150+
$bcc = $this->messageConverter->convertMany($this->addressList['bcc']);
151+
$sender = $this->messageConverter->convert(
152+
$this->addressList['sender']['email'],
153+
$this->addressList['sender']['name']
154+
);
155+
$data = [
156+
'body' => $mimeMessage,
157+
'subject' => $this->subject,
158+
'from' => $from,
159+
'to' => $to,
160+
'cc' => $cc,
161+
'replyTo' => $replyTo,
162+
'bcc' => $bcc,
163+
'sender' => $sender
164+
];
165+
$message = $this->messageFactory->create($data);
166+
167+
$this->assertContains($content, $message->toString());
168+
$this->assertContains('Content-Type: ' . $type, $message->toString());
169+
$senderString = 'Sender: ' . $sender->getName() . ' <' . $sender->getEmail() . '>';
170+
$this->assertContains($senderString, $message->toString());
171+
$this->assertContains('From: ' . $from[0]->getEmail(), $message->toString());
172+
$replyToString = 'Reply-To: ' . $replyTo[0]->getName() . ' <' . $replyTo[0]->getEmail() . '>';
173+
$this->assertContains($replyToString, $message->toString());
174+
$toString = 'To: ' . $to[0]->getName() . ' <' . $to[0]->getEmail() . '>';
175+
$this->assertContains($toString, $message->toString());
176+
$ccString = 'Cc: ' . $cc[0]->getName() . ' <' . $cc[0]->getEmail() . '>';
177+
$this->assertContains($ccString, $message->toString());
178+
$this->assertContains('Bcc: ' . $bcc[0]->getEmail(), $message->toString());
179+
$this->assertContains('Content-Description: ' . $this->description, $message->toString());
180+
$this->assertContains('Subject: ' . $this->subject, $message->toString());
181+
$this->assertContains($content, $message->toString());
182+
//tests address factory
183+
$this->assertInstanceOf(Address::class, $message->getTo()[0]);
184+
//tests address converter convert method
185+
$this->assertInstanceOf(Address::class, $message->getFrom()[0]);
186+
//tests address converter convertMany method
187+
$this->assertInstanceOf(Address::class, $message->getCc()[0]);
188+
}
189+
190+
/**
191+
* Test Email Message with Xml Attachment
192+
*
193+
* @return void
194+
*/
195+
public function testEmailMessageWithAttachment(): void
196+
{
197+
$mimePartMain = $this->mimePartFactory->create([
198+
'content' => 'Test',
199+
'description' => $this->description,
200+
'type' => MimeInterface::TYPE_TEXT
201+
]);
202+
$mimePartAttachment = $this->mimePartFactory->create([
203+
'content' => $this->getXmlContent(),
204+
'disposition' => MimeInterface::DISPOSITION_ATTACHMENT,
205+
'fileName' => self::ATTACHMENT_FILE_NAME,
206+
'type' => self::XML_TYPE
207+
]);
208+
209+
$mimeMessage = $this->mimeMessageFactory->create([
210+
'parts' => [$mimePartMain, $mimePartAttachment]
211+
]);
212+
213+
$this->addressFactory = $this->di->get(AddressFactory::class);
214+
/** @var Address $addressTo */
215+
$addressTo = $this->addressFactory
216+
->create(
217+
[
218+
'email' => $this->addressList['to'][0]['email'],
219+
'name' => $this->addressList['to'][0]['name']
220+
]
221+
);
222+
223+
$data = [
224+
'body' => $mimeMessage,
225+
'subject' => $this->subject,
226+
'to' => [$addressTo],
227+
];
228+
$message = $this->messageFactory->create($data);
229+
230+
$this->assertContains($this->getXmlContent(), $message->toString());
231+
$this->assertContains('Content-Type: ' . self::XML_TYPE, $message->toString());
232+
$contentDisposition = 'Content-Disposition: ' . MimeInterface::DISPOSITION_ATTACHMENT
233+
. '; filename="' . self::ATTACHMENT_FILE_NAME . '"';
234+
$this->assertContains($contentDisposition, $message->toString());
235+
}
236+
237+
/**
238+
* Provides xml content
239+
*
240+
* @return string
241+
*/
242+
private function getXmlContent(): string
243+
{
244+
return '<?xml version="1.0"?>
245+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
246+
<type name="Magento\Framework\Console\CommandList">
247+
<arguments>
248+
<argument name="commands" xsi:type="array">
249+
<item name="furman_test_command_testbed" xsi:type="object">Furman\Test\Command\Testbed</item>
250+
</argument>
251+
</arguments>
252+
</type>
253+
</config>
254+
';
255+
}
256+
}

lib/internal/Magento/Framework/Mail/EmailMessage.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ public function __construct(
8080
if (count($to) < 1) {
8181
throw new MailException(__('Email message must have at list one addressee'));
8282
}
83-
$this->message->setTo(
84-
$this->convertAddressArrayToAddressList($to)
85-
);
86-
$this->message->setReplyTo(
87-
$this->convertAddressArrayToAddressList($replyTo)
88-
);
89-
$this->message->setFrom(
90-
$this->convertAddressArrayToAddressList($from)
91-
);
92-
$this->message->setCc(
93-
$this->convertAddressArrayToAddressList($cc)
94-
);
95-
$this->message->setBcc(
96-
$this->convertAddressArrayToAddressList($bcc)
97-
);
83+
if ($sender) {
84+
$this->message->setTo($this->convertAddressArrayToAddressList($to));
85+
}
86+
if ($sender) {
87+
$this->message->setReplyTo($this->convertAddressArrayToAddressList($replyTo));
88+
}
89+
if ($sender) {
90+
$this->message->setFrom($this->convertAddressArrayToAddressList($from));
91+
}
92+
if ($sender) {
93+
$this->message->setCc($this->convertAddressArrayToAddressList($cc));
94+
}
95+
if ($sender) {
96+
$this->message->setBcc($this->convertAddressArrayToAddressList($bcc));
97+
}
9898
$this->mimeMessageFactory = $mimeMessageFactory;
9999
$this->addressFactory = $addressFactory;
100100
}

lib/internal/Magento/Framework/Mail/MimePart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function getRawContent(): string
204204
/**
205205
* @inheritDoc
206206
*/
207-
public function getHeadersAsArray($endOfLine = MimeInterface::LINE_END): array
207+
public function getHeadersArray($endOfLine = MimeInterface::LINE_END): array
208208
{
209209
return $this->mimePart->getHeadersArray($endOfLine);
210210
}

lib/internal/Magento/Framework/Mail/MimePartInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getRawContent(): string;
120120
*
121121
* @return array
122122
*/
123-
public function getHeadersAsArray($endOfLine = MimeInterface::LINE_END): array;
123+
public function getHeadersArray($endOfLine = MimeInterface::LINE_END): array;
124124

125125
/**
126126
* Create and return the array of headers for this MIME part

0 commit comments

Comments
 (0)