Skip to content

Commit 5c5f654

Browse files
ENGCOM-9061: #32760 fix order items missing in invoice email when invoice created … #32902
- Merge Pull Request #32902 from vovsky/magento2:32760-invoice-created-with-rest-api-missing-items-in-email - Merged commits: 1. c47505d 2. 158331d
2 parents 912ceab + 158331d commit 5c5f654

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

app/code/Magento/Sales/Model/Order/Invoice/Sender/EmailSender.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public function send(
104104

105105
$transport = [
106106
'order' => $order,
107+
'order_id' => $order->getId(),
107108
'invoice' => $invoice,
109+
'invoice_id' => $invoice->getId(),
108110
'comment' => $comment ? $comment->getComment() : '',
109111
'billing' => $order->getBillingAddress(),
110112
'payment_html' => $this->getPaymentHtml($order),

app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Sender/EmailSenderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ protected function setUp(): void
137137
$this->storeMock->expects($this->any())
138138
->method('getStoreId')
139139
->willReturn(1);
140+
140141
$this->orderMock->expects($this->any())
141142
->method('getStore')
142143
->willReturn($this->storeMock);
@@ -152,7 +153,7 @@ protected function setUp(): void
152153

153154
$this->invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
154155
->disableOriginalConstructor()
155-
->setMethods(['setSendEmail', 'setEmailSent'])
156+
->setMethods(['setSendEmail', 'setEmailSent', 'getId'])
156157
->getMock();
157158

158159
$this->commentMock = $this->getMockBuilder(InvoiceCommentCreationInterface::class)
@@ -170,6 +171,7 @@ protected function setUp(): void
170171
$this->orderMock->expects($this->any())
171172
->method('getBillingAddress')
172173
->willReturn($this->addressMock);
174+
173175
$this->orderMock->expects($this->any())
174176
->method('getShippingAddress')
175177
->willReturn($this->addressMock);
@@ -280,7 +282,9 @@ public function testSend($configValue, $forceSyncMode, $isComment, $emailSending
280282
if (!$configValue || $forceSyncMode) {
281283
$transport = [
282284
'order' => $this->orderMock,
285+
'order_id' => 1,
283286
'invoice' => $this->invoiceMock,
287+
'invoice_id' => 1,
284288
'comment' => $isComment ? 'Comment text' : '',
285289
'billing' => $this->addressMock,
286290
'payment_html' => 'Payment Info Block',
@@ -315,6 +319,14 @@ public function testSend($configValue, $forceSyncMode, $isComment, $emailSending
315319
->method('isEnabled')
316320
->willReturn($emailSendingResult);
317321

322+
$this->orderMock->expects($this->once())
323+
->method('getId')
324+
->willReturn(1);
325+
326+
$this->invoiceMock->expects($this->once())
327+
->method('getId')
328+
->willReturn(1);
329+
318330
if ($emailSendingResult) {
319331
$this->identityContainerMock->expects($this->once())
320332
->method('getCopyMethod')
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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\Sales\Model\Order\Invoice\Sender;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Sales\Api\Data\InvoiceInterface;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
use Magento\Sales\Model\Order\Invoice\Sender\EmailSender;
19+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
20+
21+
/**
22+
* Test Order\Invoice\Sender\EmailSender model
23+
*
24+
* @see \Magento\Sales\Model\Order\Invoice\Sender\EmailSender
25+
* @magentoDbIsolation enabled
26+
* @magentoDataFixture Magento/Sales/_files/invoice.php
27+
*/
28+
class EmailSenderTest extends TestCase
29+
{
30+
/**
31+
* @var ObjectManagerInterface
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* @var EmailSender
37+
*/
38+
private $emailSender;
39+
40+
/**
41+
* @var CollectionFactory
42+
*/
43+
private $invoiceCollectionFactory;
44+
45+
/**
46+
* @var TransportBuilderMock
47+
*/
48+
private $transportBuilder;
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
protected function setUp(): void
54+
{
55+
parent::setUp();
56+
57+
$this->objectManager = Bootstrap::getObjectManager();
58+
$this->emailSender = $this->objectManager->get(EmailSender::class);
59+
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
60+
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class);
61+
}
62+
63+
/**
64+
* Test that order item(s) present in email
65+
*
66+
* @magentoAppArea frontend
67+
* @return void
68+
* @throws \Exception
69+
*/
70+
public function testOrderItemsPresentInEmail()
71+
{
72+
$order = $this->getOrder('100000001');
73+
$invoice = $this->getInvoiceByOrder($order);
74+
$this->emailSender->send($order, $invoice);
75+
$message = $this->transportBuilder->getSentMessage();
76+
$this->assertStringContainsString(
77+
'SKU: simple',
78+
$message->getBody()->getParts()[0]->getRawContent(),
79+
'Expected text wasn\'t found in message.'
80+
);
81+
}
82+
83+
84+
/**
85+
* Get first order invoice
86+
*
87+
* @param OrderInterface|int $order
88+
* @return InvoiceInterface
89+
*/
90+
protected function getInvoiceByOrder($order): InvoiceInterface
91+
{
92+
$invoiceCollection = $this->invoiceCollectionFactory->create();
93+
94+
return $invoiceCollection->setOrderFilter($order)->setPageSize(1)->getFirstItem();
95+
}
96+
97+
/**
98+
* Gets order entity by increment id.
99+
*
100+
* @param string $incrementId
101+
* @return OrderInterface
102+
*/
103+
private function getOrder(string $incrementId): OrderInterface
104+
{
105+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
106+
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
107+
$searchCriteria = $searchCriteriaBuilder->addFilter('increment_id', $incrementId)
108+
->create();
109+
110+
/** @var OrderRepositoryInterface $repository */
111+
$repository = $this->objectManager->get(OrderRepositoryInterface::class);
112+
$items = $repository->getList($searchCriteria)
113+
->getItems();
114+
115+
return array_pop($items);
116+
}
117+
}

0 commit comments

Comments
 (0)