Skip to content

Commit 1de1cea

Browse files
committed
Merge remote-tracking branch 'local/ACP2E-1200' into PR_combine
2 parents a31a7ea + b75bcfa commit 1de1cea

File tree

5 files changed

+200
-2
lines changed

5 files changed

+200
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Quote\Plugin;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Quote\Observer\SubmitObserver;
14+
use Magento\Sales\Model\Order;
15+
16+
/**
17+
* Send admin order confirmation
18+
*/
19+
class SendOrderNotification
20+
{
21+
/**
22+
* @var RequestInterface $request
23+
*/
24+
private RequestInterface $request;
25+
26+
/**
27+
* @param RequestInterface $request
28+
*/
29+
public function __construct(RequestInterface $request)
30+
{
31+
$this->request = $request;
32+
}
33+
34+
/**
35+
* Adjusts order flag for confirmation email delivery
36+
*
37+
* @param SubmitObserver $subject
38+
* @param Observer $observer
39+
* @return Observer[]
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function beforeExecute(SubmitObserver $subject, Observer $observer): array
43+
{
44+
/** @var Order $order */
45+
$order = $observer->getEvent()->getOrder();
46+
$requestInfo = $this->request->getParam('order');
47+
if (!empty($requestInfo)) {
48+
$order->setCanSendNewEmailFlag((bool)($requestInfo['send_confirmation'] ?? 0));
49+
}
50+
51+
return [$observer];
52+
}
53+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\Quote\Test\Unit\Plugin;
9+
10+
use Magento\Framework\Event\Observer;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Quote\Observer\SubmitObserver;
13+
use Magento\Quote\Plugin\SendOrderNotification;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Magento\Sales\Model\Order;
17+
use Magento\Framework\Event;
18+
19+
/**
20+
* Unit test for SendOrderNotification plugin
21+
*/
22+
class SendOrderNotificationTest extends TestCase
23+
{
24+
/**
25+
* @var RequestInterface|RequestInterface&MockObject|MockObject
26+
*/
27+
private RequestInterface $request;
28+
29+
/**
30+
* @var SubmitObserver|SubmitObserver&MockObject|MockObject
31+
*/
32+
private SubmitObserver $subject;
33+
34+
/**
35+
* @var Observer|Observer&MockObject|MockObject
36+
*/
37+
private Observer $observer;
38+
39+
/**
40+
* @var SendOrderNotification
41+
*/
42+
private SendOrderNotification $notification;
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
protected function setUp(): void
48+
{
49+
$this->request = $this->createMock(RequestInterface::class);
50+
$this->subject = $this->createMock(SubmitObserver::class);
51+
$this->observer = $this->createMock(Observer::class);
52+
$this->notification = new SendOrderNotification($this->request);
53+
}
54+
55+
/**
56+
* @return void
57+
*/
58+
public function testBeforeExecuteWithSendConfirmation()
59+
{
60+
$this->request->expects($this->once())->method('getParam')->with('order')
61+
->willReturn(['send_confirmation' => 1]);
62+
63+
$order = $this->getMockBuilder(Order::class)
64+
->disableOriginalConstructor()
65+
->onlyMethods([])
66+
->getMock();
67+
68+
$event = $this->getMockBuilder(Event::class)
69+
->disableOriginalConstructor()
70+
->addMethods(['getOrder'])
71+
->getMock();
72+
$event->expects($this->exactly(2))->method('getOrder')->willReturn($order);
73+
74+
$this->observer->expects($this->exactly(2))->method('getEvent')->willReturn($event);
75+
76+
$result = $this->notification->beforeExecute($this->subject, $this->observer);
77+
$this->assertIsArray($result);
78+
$this->assertContains($this->observer, $result);
79+
80+
$observerCheck = $result[0];
81+
/** @var Order $orderCheck */
82+
$orderCheck = $observerCheck->getEvent()->getOrder();
83+
$this->assertTrue($orderCheck->getCanSendNewEmailFlag());
84+
}
85+
86+
/**
87+
* @return void
88+
*/
89+
public function testBeforeExecuteWithoutSendConfirmation()
90+
{
91+
$this->request->expects($this->once())->method('getParam')->with('order')
92+
->willReturn(['order' => []]);
93+
94+
$order = $this->getMockBuilder(Order::class)
95+
->disableOriginalConstructor()
96+
->onlyMethods([])
97+
->getMock();
98+
99+
$event = $this->getMockBuilder(Event::class)
100+
->disableOriginalConstructor()
101+
->addMethods(['getOrder'])
102+
->getMock();
103+
$event->expects($this->exactly(2))->method('getOrder')->willReturn($order);
104+
105+
$this->observer->expects($this->exactly(2))->method('getEvent')->willReturn($event);
106+
107+
$result = $this->notification->beforeExecute($this->subject, $this->observer);
108+
$this->assertIsArray($result);
109+
$this->assertContains($this->observer, $result);
110+
111+
$observerCheck = $result[0];
112+
/** @var Order $orderCheck */
113+
$orderCheck = $observerCheck->getEvent()->getOrder();
114+
$this->assertFalse($orderCheck->getCanSendNewEmailFlag());
115+
}
116+
}

app/code/Magento/Quote/etc/adminhtml/di.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
</argument>
1818
</arguments>
1919
</type>
20-
</config>
20+
<type name="Magento\Quote\Observer\SubmitObserver">
21+
<plugin name="beforeSendOrderNotification" type="Magento\Quote\Plugin\SendOrderNotification" sortOrder="1" disabled="false" />
22+
</type>
23+
</config>

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,7 @@ public function createOrder()
19901990
$this->_prepareCustomer();
19911991
$this->_validate();
19921992
$quote = $this->getQuote();
1993+
19931994
$this->_prepareQuoteItems();
19941995

19951996
$orderData = [];
@@ -2009,7 +2010,6 @@ public function createOrder()
20092010
$quote->setReservedOrderId($orderData['increment_id']);
20102011
}
20112012
$order = $this->quoteManagement->submit($quote, $orderData);
2012-
20132013
if ($this->getSession()->getOrder()->getId()) {
20142014
$oldOrder = $this->getSession()->getOrder();
20152015
$oldOrder->setRelationChildId($order->getId());
@@ -2018,6 +2018,7 @@ public function createOrder()
20182018
$this->orderManagement->cancel($oldOrder->getEntityId());
20192019
$order->save();
20202020
}
2021+
20212022
if ($this->getSendConfirmation() && !$order->getEmailSent()) {
20222023
$this->emailSender->send($order);
20232024
}

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/Create/SaveTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ public function testSendEmailOnOrderSave(): void
145145
$this->assertThat($message->getBody()->getParts()[0]->getRawContent(), $assert);
146146
}
147147

148+
/**
149+
* @magentoDbIsolation enabled
150+
* @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
151+
*
152+
* @return void
153+
*/
154+
public function testNotSendEmailOnOrderSave(): void
155+
{
156+
$this->prepareRequest();
157+
$this->dispatch('backend/sales/order_create/save');
158+
$this->assertSessionMessages(
159+
$this->equalTo([(string)__('You created the order.')]),
160+
MessageInterface::TYPE_SUCCESS
161+
);
162+
163+
$this->assertRedirect($this->stringContains('sales/order/view/'));
164+
165+
$orderId = $this->getOrderId();
166+
if ($orderId === false) {
167+
$this->fail('Order is not created.');
168+
}
169+
170+
$this->assertNull($this->transportBuilder->getSentMessage());
171+
}
172+
148173
/**
149174
* Gets quote by reserved order id.
150175
*

0 commit comments

Comments
 (0)