Skip to content

Commit c46c512

Browse files
authored
ENGCOM-5475: #19230: [Forwardport] Can't Cancel Order #20577
2 parents 9d80960 + 141e90a commit c46c512

File tree

7 files changed

+619
-36
lines changed

7 files changed

+619
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\Event\ManagerInterface;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
14+
15+
/**
16+
* Assign customer to order.
17+
*/
18+
class CustomerAssignment
19+
{
20+
/**
21+
* @var ManagerInterface
22+
*/
23+
private $eventManager;
24+
25+
/**
26+
* @var OrderRepositoryInterface
27+
*/
28+
private $orderRepository;
29+
30+
/**
31+
* CustomerAssignment constructor.
32+
*
33+
* @param ManagerInterface $eventManager
34+
* @param OrderRepositoryInterface $orderRepository
35+
*/
36+
public function __construct(
37+
ManagerInterface $eventManager,
38+
OrderRepositoryInterface $orderRepository
39+
) {
40+
$this->eventManager = $eventManager;
41+
$this->orderRepository = $orderRepository;
42+
}
43+
44+
/**
45+
* Assign customer to order.
46+
*
47+
* @param OrderInterface $order
48+
* @param CustomerInterface $customer
49+
*/
50+
public function execute(OrderInterface $order, CustomerInterface $customer): void
51+
{
52+
$order->setCustomerId($customer->getId())
53+
->setCustomerIsGuest(false)
54+
->setCustomerEmail($customer->getEmail())
55+
->setCustomerFirstname($customer->getFirstname())
56+
->setCustomerLastname($customer->getLastname())
57+
->setCustomerMiddlename($customer->getMiddlename())
58+
->setCustomerPrefix($customer->getPrefix())
59+
->setCustomerSuffix($customer->getSuffix())
60+
->setCustomerGroupId($customer->getGroupId());
61+
62+
$this->orderRepository->save($order);
63+
64+
$this->eventManager->dispatch(
65+
'sales_order_customer_assign_after',
66+
[
67+
'order' => $order,
68+
'customer' => $customer
69+
]
70+
);
71+
}
72+
}

app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Event\Observer;
1212
use Magento\Framework\Event\ObserverInterface;
1313
use Magento\Sales\Api\OrderRepositoryInterface;
14+
use Magento\Sales\Model\Order\CustomerAssignment;
1415

1516
/**
1617
* Assign order to customer created after issuing guest order.
@@ -23,11 +24,22 @@ class AssignOrderToCustomerObserver implements ObserverInterface
2324
private $orderRepository;
2425

2526
/**
27+
* @var CustomerAssignment
28+
*/
29+
private $assignmentService;
30+
31+
/**
32+
* AssignOrderToCustomerObserver constructor.
33+
*
2634
* @param OrderRepositoryInterface $orderRepository
35+
* @param CustomerAssignment $assignmentService
2736
*/
28-
public function __construct(OrderRepositoryInterface $orderRepository)
29-
{
37+
public function __construct(
38+
OrderRepositoryInterface $orderRepository,
39+
CustomerAssignment $assignmentService
40+
) {
3041
$this->orderRepository = $orderRepository;
42+
$this->assignmentService = $assignmentService;
3143
}
3244

3345
/**
@@ -43,18 +55,8 @@ public function execute(Observer $observer)
4355
if (array_key_exists('__sales_assign_order_id', $delegateData)) {
4456
$orderId = $delegateData['__sales_assign_order_id'];
4557
$order = $this->orderRepository->get($orderId);
46-
if (!$order->getCustomerId()) {
47-
//assign customer info to order after customer creation.
48-
$order->setCustomerId($customer->getId())
49-
->setCustomerIsGuest(0)
50-
->setCustomerEmail($customer->getEmail())
51-
->setCustomerFirstname($customer->getFirstname())
52-
->setCustomerLastname($customer->getLastname())
53-
->setCustomerMiddlename($customer->getMiddlename())
54-
->setCustomerPrefix($customer->getPrefix())
55-
->setCustomerSuffix($customer->getSuffix())
56-
->setCustomerGroupId($customer->getGroupId());
57-
$this->orderRepository->save($order);
58+
if (!$order->getCustomerId() && $customer->getId()) {
59+
$this->assignmentService->execute($order, $customer);
5860
}
5961
}
6062
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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\Test\Unit\Model\Order;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\Event\ManagerInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\Order\CustomerAssignment;
16+
17+
/**
18+
* Test for Magento\Sales\Model\Order\CustomerAssignment class.
19+
*/
20+
class CustomerAssigmentTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var CustomerAssignment
24+
*/
25+
private $customerAssignment;
26+
27+
/**
28+
* @var OrderInterface|\PHPUnit\Framework\MockObject\MockObject
29+
*/
30+
private $orderMock;
31+
32+
/**
33+
* @var CustomerInterface|\PHPUnit\Framework\MockObject\MockObject
34+
*/
35+
private $customerMock;
36+
37+
/**
38+
* @var OrderRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
39+
*/
40+
private $orderRepositoryMock;
41+
42+
/**
43+
* @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject
44+
*/
45+
private $eventManagerMock;
46+
47+
/**
48+
* Tests 'execute' method.
49+
*
50+
* @dataProvider executeDataProvider
51+
* @param array $data
52+
*/
53+
public function testExecute(array $data): void
54+
{
55+
$this->configureOrderMock($data);
56+
$this->configureCustomerMock($data);
57+
$this->orderRepositoryMock->expects($this->once())->method('save')->with($this->orderMock);
58+
$this->eventManagerMock->expects($this->once())->method('dispatch')->with(
59+
'sales_order_customer_assign_after',
60+
[
61+
'order' => $this->orderMock,
62+
'customer' => $this->customerMock
63+
]
64+
);
65+
66+
$this->customerAssignment->execute($this->orderMock, $this->customerMock);
67+
}
68+
69+
/**
70+
*
71+
* Data provider for testExecute.
72+
* @return array
73+
*/
74+
public function executeDataProvider(): array
75+
{
76+
return [
77+
[
78+
[
79+
'customerId' => 1,
80+
'customerIsGuest' => false,
81+
'customerEmail' => 'customerEmail',
82+
'customerFirstname' => 'customerFirstname',
83+
'customerLastname' => 'customerLastname',
84+
'customerMiddlename' => 'customerMiddlename',
85+
'customerPrefix' => 'customerPrefix',
86+
'customerSuffix' => 'customerSuffix',
87+
'customerGroupId' => 'customerGroupId',
88+
],
89+
],
90+
];
91+
}
92+
93+
/**
94+
* @return void
95+
*/
96+
protected function setUp()
97+
{
98+
$objectManager = new ObjectManager($this);
99+
$this->orderMock = $this->createMock(OrderInterface::class);
100+
$this->customerMock = $this->createMock(CustomerInterface::class);
101+
$this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class);
102+
$this->eventManagerMock = $this->createMock(ManagerInterface::class);
103+
$this->customerAssignment = $objectManager->getObject(
104+
CustomerAssignment::class,
105+
[
106+
'eventManager' => $this->eventManagerMock,
107+
'orderRepository' => $this->orderRepositoryMock
108+
]
109+
);
110+
}
111+
112+
/**
113+
* Set up order mock.
114+
*
115+
* @param array $data
116+
*/
117+
private function configureOrderMock(array $data): void
118+
{
119+
$this->orderMock->expects($this->once())->method('setCustomerId')->with($data['customerId'])
120+
->willReturn($this->orderMock);
121+
$this->orderMock->expects($this->once())->method('setCustomerIsGuest')->with($data['customerIsGuest'])
122+
->willReturn($this->orderMock);
123+
$this->orderMock->expects($this->once())->method('setCustomerEmail')->with($data['customerEmail'])
124+
->willReturn($this->orderMock);
125+
$this->orderMock->expects($this->once())->method('setCustomerFirstname')->with($data['customerFirstname'])
126+
->willReturn($this->orderMock);
127+
$this->orderMock->expects($this->once())->method('setCustomerLastname')->with($data['customerLastname'])
128+
->willReturn($this->orderMock);
129+
$this->orderMock->expects($this->once())->method('setCustomerMiddlename')->with($data['customerMiddlename'])
130+
->willReturn($this->orderMock);
131+
$this->orderMock->expects($this->once())->method('setCustomerPrefix')->with($data['customerPrefix'])
132+
->willReturn($this->orderMock);
133+
$this->orderMock->expects($this->once())->method('setCustomerSuffix')->with($data['customerSuffix'])
134+
->willReturn($this->orderMock);
135+
$this->orderMock->expects($this->once())->method('setCustomerGroupId')->with($data['customerGroupId'])
136+
->willReturn($this->orderMock);
137+
}
138+
139+
/**
140+
* Set up customer mock.
141+
*
142+
* @param array $data
143+
*/
144+
private function configureCustomerMock(array $data): void
145+
{
146+
$this->customerMock->expects($this->once())->method('getId')->willReturn($data['customerId']);
147+
$this->customerMock->expects($this->once())->method('getEmail')->willReturn($data['customerEmail']);
148+
$this->customerMock->expects($this->once())->method('getFirstname')->willReturn($data['customerFirstname']);
149+
$this->customerMock->expects($this->once())->method('getLastname')->willReturn($data['customerLastname']);
150+
$this->customerMock->expects($this->once())->method('getMiddlename')->willReturn($data['customerMiddlename']);
151+
$this->customerMock->expects($this->once())->method('getPrefix')->willReturn($data['customerPrefix']);
152+
$this->customerMock->expects($this->once())->method('getSuffix')->willReturn($data['customerSuffix']);
153+
$this->customerMock->expects($this->once())->method('getGroupId')->willReturn($data['customerGroupId']);
154+
}
155+
}

app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php

+28-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Event\Observer;
1313
use Magento\Sales\Api\Data\OrderInterface;
1414
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\Order\CustomerAssignment;
1516
use Magento\Sales\Observer\AssignOrderToCustomerObserver;
1617
use PHPUnit\Framework\TestCase;
1718
use PHPUnit_Framework_MockObject_MockObject;
@@ -27,6 +28,9 @@ class AssignOrderToCustomerObserverTest extends TestCase
2728
/** @var OrderRepositoryInterface|PHPUnit_Framework_MockObject_MockObject */
2829
protected $orderRepositoryMock;
2930

31+
/** @var CustomerAssignment | PHPUnit_Framework_MockObject_MockObject */
32+
protected $assignmentMock;
33+
3034
/**
3135
* Set Up
3236
*/
@@ -35,17 +39,23 @@ protected function setUp()
3539
$this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
3640
->disableOriginalConstructor()
3741
->getMock();
38-
$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock);
42+
43+
$this->assignmentMock = $this->getMockBuilder(CustomerAssignment::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
47+
$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->assignmentMock);
3948
}
4049

4150
/**
4251
* Test assigning order to customer after issuing guest order
4352
*
4453
* @dataProvider getCustomerIds
54+
* @param null|int $orderCustomerId
4555
* @param null|int $customerId
4656
* @return void
4757
*/
48-
public function testAssignOrderToCustomerAfterGuestOrder($customerId)
58+
public function testAssignOrderToCustomerAfterGuestOrder($orderCustomerId, $customerId)
4959
{
5060
$orderId = 1;
5161
/** @var Observer|PHPUnit_Framework_MockObject_MockObject $observerMock */
@@ -62,31 +72,24 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
6272
->getMockForAbstractClass();
6373
$observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
6474
$eventMock->expects($this->any())->method('getData')
65-
->willReturnMap([
66-
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
67-
['customer_data_object', null, $customerMock]
68-
]);
69-
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
75+
->willReturnMap(
76+
[
77+
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
78+
['customer_data_object', null, $customerMock]
79+
]
80+
);
81+
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($orderCustomerId);
7082
$this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId)
7183
->willReturn($orderMock);
7284

73-
$orderMock->expects($this->once())->method('setCustomerId')->willReturn($orderMock);
74-
$orderMock->expects($this->once())->method('setCustomerIsGuest')->willReturn($orderMock);
75-
$orderMock->expects($this->once())->method('setCustomerEmail')->willReturn($orderMock);
76-
$orderMock->expects($this->once())->method('setCustomerFirstname')->willReturn($orderMock);
77-
$orderMock->expects($this->once())->method('setCustomerLastname')->willReturn($orderMock);
78-
$orderMock->expects($this->once())->method('setCustomerMiddlename')->willReturn($orderMock);
79-
$orderMock->expects($this->once())->method('setCustomerPrefix')->willReturn($orderMock);
80-
$orderMock->expects($this->once())->method('setCustomerSuffix')->willReturn($orderMock);
81-
$orderMock->expects($this->once())->method('setCustomerGroupId')->willReturn($orderMock);
82-
83-
if (!$customerId) {
84-
$this->orderRepositoryMock->expects($this->once())->method('save')->with($orderMock);
85+
if (!$orderCustomerId) {
86+
$customerMock->expects($this->once())->method('getId')->willReturn($customerId);
87+
$this->assignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock);
8588
$this->sut->execute($observerMock);
86-
return ;
89+
return;
8790
}
8891

89-
$this->orderRepositoryMock->expects($this->never())->method('save')->with($orderMock);
92+
$this->assignmentMock->expects($this->never())->method('execute');
9093
$this->sut->execute($observerMock);
9194
}
9295

@@ -97,6 +100,9 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
97100
*/
98101
public function getCustomerIds()
99102
{
100-
return [[null, 1]];
103+
return [
104+
[null, 1],
105+
[1, 1],
106+
];
101107
}
102108
}

0 commit comments

Comments
 (0)