Skip to content

Commit a82bbb6

Browse files
committed
ACP2E-1200: fixed bug using plugin
1 parent 3a2018c commit a82bbb6

File tree

7 files changed

+127
-25
lines changed

7 files changed

+127
-25
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,6 @@ protected function resolveItems(QuoteEntity $quote)
525525
* @throws ValidatorException
526526
* @throws LocalizedException
527527
* @throws \Exception
528-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
529-
* @SuppressWarnings(PHPMD.NPathComplexity)
530528
*/
531529
protected function submitQuote(QuoteEntity $quote, $orderData = [])
532530
{
@@ -595,10 +593,6 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
595593
$order->setIncrementId($quote->getReservedOrderId());
596594
}
597595

598-
if (array_key_exists('send_confirmation', $orderData) && false === $orderData['send_confirmation']) {
599-
$order->setCanSendNewEmailFlag(false);
600-
}
601-
602596
$this->submitQuoteValidator->validateOrder($order);
603597

604598
$this->eventManager->dispatch(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Magento\Quote\Plugin;
4+
5+
use Magento\Framework\Event\Observer;
6+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
7+
use Magento\Quote\Observer\SubmitObserver;
8+
use Magento\Sales\Model\Order;
9+
10+
class SendOrderNotification
11+
{
12+
private RestRequest $request;
13+
14+
public function __construct(RestRequest $request)
15+
{
16+
$this->request = $request;
17+
}
18+
19+
/**
20+
* Adjusts order flag for confirmation email delivery
21+
* @param SubmitObserver $subject
22+
* @param Observer $observer
23+
* @return Observer[]
24+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
25+
*/
26+
public function beforeExecute(SubmitObserver $subject, Observer $observer): array
27+
{
28+
/** @var Order $order */
29+
$order = $observer->getEvent()->getOrder();
30+
$requestInfo = $this->request->getPostValue('order');
31+
$order->setCanSendNewEmailFlag((bool)($requestInfo['send_confirmation'] ?? 0));
32+
33+
return [$observer];
34+
}
35+
}

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ public function testAssignCustomer(): void
732732
*/
733733
public function testSubmit(): void
734734
{
735-
$orderData = ['send_confirmation' => false];
735+
$orderData = [];
736736
$isGuest = true;
737737
$isVirtual = false;
738738
$customerId = 1;
@@ -827,8 +827,7 @@ public function testSubmit(): void
827827
]
828828
);
829829
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
830-
$test = $this->model->submit($quote, $orderData);
831-
$this->assertEquals($order, $test);
830+
$this->assertEquals($order, $this->model->submit($quote, $orderData));
832831
}
833832

834833
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Magento\Quote\Test\Unit\Plugin;
4+
5+
use Magento\Framework\Event\Observer;
6+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
7+
use Magento\Quote\Observer\SubmitObserver;
8+
use Magento\Quote\Plugin\SendOrderNotification;
9+
use PHPUnit\Framework\TestCase;
10+
use Magento\Sales\Model\Order;
11+
use Magento\Framework\Event;
12+
13+
class SendOrderNotificationTest extends TestCase
14+
{
15+
private RestRequest $request;
16+
private SubmitObserver $subject;
17+
private Observer $observer;
18+
private SendOrderNotification $notification;
19+
20+
protected function setUp(): void
21+
{
22+
$this->request = $this->createMock(RestRequest::class);
23+
$this->subject = $this->createMock(SubmitObserver::class);
24+
$this->observer = $this->createMock(Observer::class);
25+
$this->notification = new SendOrderNotification($this->request);
26+
}
27+
28+
public function testBeforeExecuteWithSendConfirmation()
29+
{
30+
$this->request->expects($this->once())->method('getPostValue')->with('order')
31+
->willReturn(['send_confirmation' => 1]);
32+
33+
$order = $this->getMockBuilder(Order::class)
34+
->disableOriginalConstructor()
35+
->onlyMethods([])
36+
->getMock();
37+
38+
$event = $this->getMockBuilder(Event::class)
39+
->disableOriginalConstructor()
40+
->addMethods(['getOrder'])
41+
->getMock();
42+
$event->expects($this->exactly(2))->method('getOrder')->willReturn($order);
43+
44+
$this->observer->expects($this->exactly(2))->method('getEvent')->willReturn($event);
45+
46+
$result = $this->notification->beforeExecute($this->subject, $this->observer);
47+
$this->assertIsArray($result);
48+
$this->assertContains($this->observer, $result);
49+
50+
$observerCheck = $result[0];
51+
/** @var Order $orderCheck */
52+
$orderCheck = $observerCheck->getEvent()->getOrder();
53+
$this->assertTrue($orderCheck->getCanSendNewEmailFlag());
54+
}
55+
56+
public function testBeforeExecuteWithoutSendConfirmation()
57+
{
58+
$this->request->expects($this->once())->method('getPostValue')->with('order')
59+
->willReturn(['order' => []]);
60+
61+
$order = $this->getMockBuilder(Order::class)
62+
->disableOriginalConstructor()
63+
->onlyMethods([])
64+
->getMock();
65+
66+
$event = $this->getMockBuilder(Event::class)
67+
->disableOriginalConstructor()
68+
->addMethods(['getOrder'])
69+
->getMock();
70+
$event->expects($this->exactly(2))->method('getOrder')->willReturn($order);
71+
72+
$this->observer->expects($this->exactly(2))->method('getEvent')->willReturn($event);
73+
74+
$result = $this->notification->beforeExecute($this->subject, $this->observer);
75+
$this->assertIsArray($result);
76+
$this->assertContains($this->observer, $result);
77+
78+
$observerCheck = $result[0];
79+
/** @var Order $orderCheck */
80+
$orderCheck = $observerCheck->getEvent()->getOrder();
81+
$this->assertFalse($orderCheck->getCanSendNewEmailFlag());
82+
}
83+
}

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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ public function createOrder()
19851985

19861986
$this->_prepareQuoteItems();
19871987

1988-
$orderData = ['send_confirmation' => (bool)$this->getSendConfirmation()];
1988+
$orderData = [];
19891989
if ($this->getSession()->getOrder()->getId()) {
19901990
$oldOrder = $this->getSession()->getOrder();
19911991
$originalId = $oldOrder->getOriginalIncrementId();
@@ -1997,13 +1997,11 @@ public function createOrder()
19971997
'relation_parent_id' => $oldOrder->getId(),
19981998
'relation_parent_real_id' => $oldOrder->getIncrementId(),
19991999
'edit_increment' => $oldOrder->getEditIncrement() + 1,
2000-
'increment_id' => $originalId . '-' . ($oldOrder->getEditIncrement() + 1),
2001-
'send_confirmation' => (bool)$this->getSendConfirmation()
2000+
'increment_id' => $originalId . '-' . ($oldOrder->getEditIncrement() + 1)
20022001
];
20032002
$quote->setReservedOrderId($orderData['increment_id']);
20042003
}
20052004
$order = $this->quoteManagement->submit($quote, $orderData);
2006-
20072005
if ($this->getSession()->getOrder()->getId()) {
20082006
$oldOrder = $this->getSession()->getOrder();
20092007
$oldOrder->setRelationChildId($order->getId());
@@ -2013,10 +2011,6 @@ public function createOrder()
20132011
$order->save();
20142012
}
20152013

2016-
if ($this->getSendConfirmation() && !$order->getEmailSent()) {
2017-
$this->emailSender->send($order);
2018-
}
2019-
20202014
$this->_eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]);
20212015

20222016
return $order;

dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public function testCreateOrderNewCustomerDifferentAddresses()
400400
),
401401
'shipping_method' => $shippingMethod,
402402
'comment' => ['customer_note' => ''],
403-
'send_confirmation' => true
403+
'send_confirmation' => true,
404404
];
405405
$paymentData = ['method' => $paymentMethod];
406406

@@ -419,7 +419,6 @@ public function testCreateOrderNewCustomerDifferentAddresses()
419419
self::assertEquals($this->model->getQuote()->getCustomerLastname(), $newBillAddress->getLastname());
420420
self::assertEquals($order->getCustomerFirstname(), $newBillAddress->getFirstname());
421421
self::assertEquals($order->getCustomerLastname(), $newBillAddress->getLastname());
422-
self::assertTrue($order->getEmailSent());
423422
$this->verifyCreatedOrder($order, $shippingMethod);
424423
/** @var Customer $customer */
425424
$customer = $this->objectManager->create(Customer::class);
@@ -463,7 +462,6 @@ public function testCreateOrderNewCustomer()
463462
$paymentMethod
464463
);
465464
$order = $this->model->createOrder();
466-
self::assertNull($order->getEmailSent());
467465
//Check, order considering decimal qty in product.
468466
foreach ($order->getItems() as $orderItem) {
469467
self::assertTrue($orderItem->getIsQtyDecimal());
@@ -535,7 +533,6 @@ public function testCreateOrderNewCustomerWithFailedFirstPlaceOrderAction(
535533
);
536534

537535
$order = $this->model->createOrder();
538-
self::assertNull($order->getEmailSent());
539536
$this->verifyCreatedOrder($order, $shippingMethod);
540537
}
541538

@@ -596,7 +593,6 @@ public function testCreateOrderExistingCustomerDifferentAddresses()
596593

597594
$this->model->getQuote()->setCustomer($customerMock);
598595
$order = $this->model->createOrder();
599-
self::assertNull($order->getEmailSent());
600596
$this->verifyCreatedOrder($order, $shippingMethod);
601597
$this->objectManager->get(CustomerRegistry::class)
602598
->remove($order->getCustomerId());
@@ -648,7 +644,6 @@ public function testCreateOrderExistingCustomer()
648644

649645
$this->model->getQuote()->setCustomer($customerMock);
650646
$order = $this->model->createOrder();
651-
self::assertNull($order->getEmailSent());
652647
if ($this->model->getSendConfirmation() && !$order->getEmailSent()) {
653648
$this->emailSenderMock->expects($this->once())
654649
->method('send')
@@ -971,7 +966,6 @@ public function testCreateOrderExistingCustomerWhenDefaultAddressDiffersWithNew(
971966
$this->model->setBillingAddress($orderData['billing_address']);
972967
try {
973968
$order =$this->model->createOrder();
974-
self::assertNull($order->getEmailSent());
975969
$orderData = $order->getData();
976970
self::assertNotEmpty($orderData['increment_id'], 'Order increment ID is empty.');
977971
} catch (\Magento\Framework\Exception\LocalizedException $e) {

0 commit comments

Comments
 (0)