Skip to content

Commit 632f253

Browse files
Banvari Lalglo60612
authored andcommitted
AC-8998::Adobe Commerce Admin REST endpoint "orders/{id}/comments" is changing the status of the order when adding comment to the status
1 parent c22a9db commit 632f253

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,21 @@ public function getCommentsList($id)
160160
* @param int $id
161161
* @param \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory
162162
* @return bool
163+
* @throws \Magento\Framework\Exception\LocalizedException
163164
*/
164165
public function addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
165166
{
166167
$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+
}
167178
$order->addStatusHistory($statusHistory);
168179
$this->orderRepository->save($order);
169180
$notify = $statusHistory['is_customer_notified'] ?? false;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use PHPUnit\Framework\MockObject\MockObject;
2929
use PHPUnit\Framework\TestCase;
3030
use Psr\Log\LoggerInterface;
31+
use Magento\Framework\Phrase;
32+
use Magento\Framework\Exception\LocalizedException;
3133

3234
/**
3335
*
@@ -295,6 +297,22 @@ public function testAddComment()
295297
$this->assertTrue($this->orderService->addComment(123, $this->orderStatusHistoryMock));
296298
}
297299

300+
/**
301+
* test for add comment with order status change case
302+
*/
303+
public function testAddCommentWithStatus()
304+
{
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)
309+
);
310+
$this->orderStatusHistoryMock->method('getStatus')
311+
->willThrowException($inputException);
312+
$this->expectException(LocalizedException::class);
313+
$this->orderService->addComment(123, $this->orderStatusHistoryMock);
314+
}
315+
298316
public function testNotify()
299317
{
300318
$this->orderRepositoryMock->expects($this->once())

0 commit comments

Comments
 (0)