Skip to content

magento/magento2#38659: Change order status issue #38923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions app/code/Magento/Sales/Controller/Adminhtml/Order/AddComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function execute()
throw new \Magento\Framework\Exception\LocalizedException(__($error));
}

$orderStatus = $this->getOrderStatus($order->getDataByKey('status'), $data['status']);
$order->setStatus($orderStatus);
$order->setStatus($data['status']);
$notify = $data['is_customer_notified'] ?? false;
$visible = $data['is_visible_on_front'] ?? false;

Expand All @@ -55,7 +54,7 @@ public function execute()
}

$comment = trim(strip_tags($data['comment']));
$history = $order->addStatusHistoryComment($comment, $orderStatus);
$history = $order->addStatusHistoryComment($comment, $order->getStatus());
$history->setIsVisibleOnFront($visible);
$history->setIsCustomerNotified($notify);
$history->save();
Expand All @@ -81,17 +80,4 @@ public function execute()
}
return $this->resultRedirectFactory->create()->setPath('sales/*/');
}

/**
* Get order status to set
*
* @param string $orderStatus
* @param string $historyStatus
* @return string
*/
private function getOrderStatus(string $orderStatus, string $historyStatus): string
{
return ($orderStatus === Order::STATE_PROCESSING || $orderStatus === Order::STATUS_FRAUD) ? $historyStatus
: $orderStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
use Magento\Sales\Model\Order\Status\History;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -126,20 +127,21 @@ protected function setUp(): void
* @param array $historyData
* @param string $orderStatus
* @param bool $userHasResource
* @param bool $visibleOnFront
* @param bool $expectedNotify
*
* @throws Exception
* @dataProvider executeWillNotifyCustomerDataProvider
*/
public function testExecuteWillNotifyCustomer(
array $historyData,
string $orderStatus,
bool $userHasResource,
bool $visibleOnFront,
bool $expectedNotify
) {
$orderId = 30;
$this->requestMock->expects($this->once())->method('getParam')->with('order_id')->willReturn($orderId);
$this->orderMock->expects($this->atLeastOnce())->method('getDataByKey')
->with('status')->willReturn($orderStatus);
$this->orderRepositoryMock->expects($this->once())
->method('get')
->willReturn($this->orderMock);
Expand All @@ -148,76 +150,92 @@ public function testExecuteWillNotifyCustomer(
$this->orderMock->expects($this->once())
->method('addStatusHistoryComment')
->willReturn($this->statusHistoryCommentMock);
$this->statusHistoryCommentMock->expects($this->once())->method('setIsVisibleOnFront')->with($visibleOnFront);
$this->statusHistoryCommentMock->expects($this->once())->method('setIsCustomerNotified')->with($expectedNotify);
$this->objectManagerMock->expects($this->once())->method('create')->willReturn(
$this->createMock(OrderCommentSender::class)
);

$this->addCommentController->execute();
}

/**
* @return array
*/
public function executeWillNotifyCustomerDataProvider()
public static function executeWillNotifyCustomerDataProvider(): array
{
return [
'User Has Access - Notify True' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => true,
'is_visible_on_front' => true,
'status' => 'processing'
],
'orderStatus' =>'processing',
'userHasResource' => true,
'expectedNotify' => true
'expectedNotify' => true,
'visibleOnFront' => true
],
'User Has Access - Notify False' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => false,
'is_visible_on_front' => false,
'status' => 'processing'
],
'orderStatus' =>'processing',
'userHasResource' => true,
'expectedNotify' => false
'expectedNotify' => false,
'visibleOnFront' => false
],
'User Has Access - Notify Unset' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => false,
'is_visible_on_front' => false,
'status' => 'processing'
],
'orderStatus' =>'fraud',
'userHasResource' => true,
'expectedNotify' => false
'expectedNotify' => false,
'visibleOnFront' => false
],
'User No Access - Notify True' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => true,
'is_visible_on_front' => false,
'status' => 'fraud'
],
'orderStatus' =>'processing',
'userHasResource' => false,
'expectedNotify' => false
'expectedNotify' => false,
'visibleOnFront' => false
],
'User No Access - Notify False' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => false,
'is_visible_on_front' => false,
'status' => 'processing'
],
'orderStatus' =>'complete',
'userHasResource' => false,
'expectedNotify' => false
'expectedNotify' => false,
'visibleOnFront' => false
],
'User No Access - Notify Unset' => [
'postData' => [
[
'comment' => 'Great Product!',
'is_customer_notified' => false,
'is_visible_on_front' => false,
'status' => 'processing'
],
'orderStatus' =>'complete',
'userHasResource' => false,
'expectedNotify' => false
'expectedNotify' => false,
'visibleOnFront' => false
],
];
}
Expand All @@ -230,21 +248,17 @@ public function executeWillNotifyCustomerDataProvider()
public function testExecuteForEmptyCommentMessage(): void
{
$orderId = 30;
$orderStatus = 'processing';
$historyData = [
'comment' => '',
'is_customer_notified' => false,
'status' => 'processing'
'is_visible_on_front' => true
];

$this->requestMock->expects($this->once())->method('getParam')->with('order_id')->willReturn($orderId);
$this->orderMock->expects($this->atLeastOnce())->method('getDataByKey')
->with('status')->willReturn($orderStatus);
$this->orderRepositoryMock->expects($this->once())
->method('get')
->willReturn($this->orderMock);
$this->requestMock->expects($this->once())->method('getPost')->with('history')->willReturn($historyData);

$this->requestMock->expects($this->once())->method('getParam')
->with('order_id')->willReturn($orderId);
$this->orderRepositoryMock->expects($this->once())->method('get')->willReturn($this->orderMock);
$this->requestMock->expects($this->once())->method('getPost')
->with('history')->willReturn($historyData);
$this->resultJson->expects($this->once())
->method('setData')
->with(
Expand All @@ -258,7 +272,7 @@ public function testExecuteForEmptyCommentMessage(): void
$this->jsonFactory->expects($this->once())
->method('create')
->willReturn($this->resultJson);

$this->assertSame($this->resultJson, $this->addCommentController->execute());
$result = $this->addCommentController->execute();
$this->assertSame($this->resultJson, $result);
}
}