Skip to content

Commit 832211b

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3130' into Tier4-Kings-PR-07-16-2024
2 parents fc6a3dc + fd879d4 commit 832211b

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

app/code/Magento/Sales/Model/Service/OrderService.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\Payment\Gateway\Command\CommandException;
1010
use Magento\Sales\Api\OrderManagementInterface;
11+
use Magento\Sales\Model\Order\Config;
1112
use Magento\Sales\Model\OrderMutexInterface;
1213
use Psr\Log\LoggerInterface;
1314

@@ -66,6 +67,11 @@ class OrderService implements OrderManagementInterface
6667
*/
6768
private $orderMutex;
6869

70+
/**
71+
* @var Config
72+
*/
73+
private $orderConfig;
74+
6975
/**
7076
* Constructor
7177
*
@@ -79,6 +85,7 @@ class OrderService implements OrderManagementInterface
7985
* @param \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures
8086
* @param LoggerInterface $logger
8187
* @param OrderMutexInterface|null $orderMutex
88+
* @param Config|null $orderConfig
8289
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8390
*/
8491
public function __construct(
@@ -91,7 +98,8 @@ public function __construct(
9198
\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender,
9299
\Magento\Sales\Api\PaymentFailuresInterface $paymentFailures,
93100
LoggerInterface $logger,
94-
?OrderMutexInterface $orderMutex = null
101+
?OrderMutexInterface $orderMutex = null,
102+
?Config $orderConfig = null
95103
) {
96104
$this->orderRepository = $orderRepository;
97105
$this->historyRepository = $historyRepository;
@@ -103,6 +111,7 @@ public function __construct(
103111
$this->paymentFailures = $paymentFailures;
104112
$this->logger = $logger;
105113
$this->orderMutex = $orderMutex ?: ObjectManager::getInstance()->get(OrderMutexInterface::class);
114+
$this->orderConfig = $orderConfig ?: ObjectManager::getInstance()->get(Config::class);
106115
}
107116

108117
/**
@@ -165,16 +174,26 @@ public function getCommentsList($id)
165174
public function addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
166175
{
167176
$order = $this->orderRepository->get($id);
168-
169-
/**
170-
* change order status is not allowed during add comment to the order
171-
*/
172-
if ($statusHistory->getStatus() && $statusHistory->getStatus() != $order->getStatus()) {
173-
throw new \Magento\Framework\Exception\LocalizedException(
174-
__('Unable to add comment: The status "%1" is not part of the order
175-
status history.', $statusHistory->getStatus())
176-
);
177+
$statuses = $this->orderConfig->getStateStatuses($order->getState());
178+
$orderStatus = $order->getStatus();
179+
$orderStatusHistory = $statusHistory->getStatus();
180+
if ($orderStatusHistory) {
181+
/**
182+
* change order status in the scope of different state is not allowed during add comment to the order
183+
*/
184+
if (!array_key_exists($orderStatusHistory, $statuses)) {
185+
throw new \Magento\Framework\Exception\LocalizedException(
186+
__(
187+
'Unable to add comment: The status "%1" is not part of the order status history.',
188+
$orderStatusHistory
189+
)
190+
);
191+
}
192+
$orderStatus = $orderStatusHistory;
177193
}
194+
$statusHistory->setStatus($orderStatus);
195+
$order->setStatus($orderStatus);
196+
178197
$order->addStatusHistory($statusHistory);
179198
$this->orderRepository->save($order);
180199
$notify = $statusHistory['is_customer_notified'] ?? false;

app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
2121
use Magento\Sales\Api\PaymentFailuresInterface;
2222
use Magento\Sales\Model\Order;
23+
use Magento\Sales\Model\Order\Config;
2324
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
2425
use Magento\Sales\Model\Order\Status\History;
2526
use Magento\Sales\Model\OrderMutex;
@@ -28,12 +29,12 @@
2829
use PHPUnit\Framework\MockObject\MockObject;
2930
use PHPUnit\Framework\TestCase;
3031
use Psr\Log\LoggerInterface;
31-
use Magento\Framework\Phrase;
3232
use Magento\Framework\Exception\LocalizedException;
3333

3434
/**
3535
*
3636
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37+
* @SuppressWarnings(PHPMD.TooManyFields)
3738
*/
3839
class OrderServiceTest extends TestCase
3940
{
@@ -112,6 +113,11 @@ class OrderServiceTest extends TestCase
112113
*/
113114
private $resourceConnectionMock;
114115

116+
/**
117+
* @var MockObject|Config
118+
*/
119+
private $orderConfigMock;
120+
115121
protected function setUp(): void
116122
{
117123
$this->orderRepositoryMock = $this->getMockBuilder(
@@ -189,6 +195,10 @@ protected function setUp(): void
189195
->disableOriginalConstructor()
190196
->getMock();
191197

198+
$this->orderConfigMock = $this->getMockBuilder(Config::class)
199+
->disableOriginalConstructor()
200+
->getMock();
201+
192202
$this->orderService = new OrderService(
193203
$this->orderRepositoryMock,
194204
$this->orderStatusHistoryRepositoryMock,
@@ -199,7 +209,8 @@ protected function setUp(): void
199209
$this->orderCommentSender,
200210
$paymentFailures,
201211
$logger,
202-
new OrderMutex($this->resourceConnectionMock)
212+
new OrderMutex($this->resourceConnectionMock),
213+
$this->orderConfigMock
203214
);
204215
}
205216

@@ -276,11 +287,15 @@ public function testGetCommentsList()
276287

277288
public function testAddComment()
278289
{
290+
$orderId = 123;
279291
$clearComment = "Comment text here...";
280-
$this->orderRepositoryMock->expects($this->once())
281-
->method('get')
282-
->with(123)
283-
->willReturn($this->orderMock);
292+
$this->mockCommentStatuses($orderId, Order::STATUS_FRAUD);
293+
$this->orderMock->expects($this->once())
294+
->method('setStatus')
295+
->willReturn(Order::STATUS_FRAUD);
296+
$this->orderStatusHistoryMock->expects($this->once())
297+
->method('setStatus')
298+
->willReturn(Order::STATUS_FRAUD);
284299
$this->orderMock->expects($this->once())
285300
->method('addStatusHistory')
286301
->with($this->orderStatusHistoryMock)
@@ -294,23 +309,52 @@ public function testAddComment()
294309
$this->orderCommentSender->expects($this->once())
295310
->method('send')
296311
->with($this->orderMock, false, $clearComment);
297-
$this->assertTrue($this->orderService->addComment(123, $this->orderStatusHistoryMock));
312+
$this->assertTrue($this->orderService->addComment($orderId, $this->orderStatusHistoryMock));
298313
}
299314

300315
/**
301316
* test for add comment with order status change case
302317
*/
303318
public function testAddCommentWithStatus()
304319
{
305-
$params = ['status' => 'holded'];
306-
$inputException = new LocalizedException(
307-
new Phrase('Unable to add comment: The status "%1" is not part of the order
308-
status history.', $params)
320+
$orderId = 123;
321+
$inputException = __(
322+
'Unable to add comment: The status "%1" is not part of the order status history.',
323+
Order::STATE_NEW
309324
);
310-
$this->orderStatusHistoryMock->method('getStatus')
311-
->willThrowException($inputException);
325+
$this->mockCommentStatuses($orderId, Order::STATE_NEW);
312326
$this->expectException(LocalizedException::class);
313-
$this->orderService->addComment(123, $this->orderStatusHistoryMock);
327+
$this->expectExceptionMessage((string)$inputException);
328+
$this->orderService->addComment($orderId, $this->orderStatusHistoryMock);
329+
}
330+
331+
/**
332+
* @param $orderId
333+
* @param $orderStatusHistory
334+
*/
335+
private function mockCommentStatuses($orderId, $orderStatusHistory): void
336+
{
337+
$this->orderRepositoryMock->expects($this->once())
338+
->method('get')
339+
->with($orderId)
340+
->willReturn($this->orderMock);
341+
$this->orderMock->expects($this->once())
342+
->method('getState')
343+
->willReturn(Order::STATE_PROCESSING);
344+
$this->orderConfigMock->expects($this->once())
345+
->method('getStateStatuses')
346+
->with(Order::STATE_PROCESSING)
347+
->willReturn([
348+
Order::STATE_PROCESSING => 'Processing',
349+
Order::STATUS_FRAUD => 'Suspected Fraud',
350+
'test' => 'Tests'
351+
]);
352+
$this->orderMock->expects($this->once())
353+
->method('getStatus')
354+
->willReturn(Order::STATE_PROCESSING);
355+
$this->orderStatusHistoryMock->expects($this->once())
356+
->method('getStatus')
357+
->willReturn($orderStatusHistory);
314358
}
315359

316360
public function testNotify()

0 commit comments

Comments
 (0)