File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 2828use PHPUnit \Framework \MockObject \MockObject ;
2929use PHPUnit \Framework \TestCase ;
3030use 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 ())
You can’t perform that action at this time.
0 commit comments