Skip to content

magento/magento2#19230: [Forwardport] Can't Cancel Order #20577

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

Merged
merged 6 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 72 additions & 0 deletions app/code/Magento/Sales/Model/Order/CustomerAssignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\Order;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* Assign customer to order.
*/
class CustomerAssignment
{
/**
* @var ManagerInterface
*/
private $eventManager;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* CustomerAssignment constructor.
*
* @param ManagerInterface $eventManager
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
ManagerInterface $eventManager,
OrderRepositoryInterface $orderRepository
) {
$this->eventManager = $eventManager;
$this->orderRepository = $orderRepository;
}

/**
* Assign customer to order.
*
* @param OrderInterface $order
* @param CustomerInterface $customer
*/
public function execute(OrderInterface $order, CustomerInterface $customer): void
{
$order->setCustomerId($customer->getId())
->setCustomerIsGuest(false)
->setCustomerEmail($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerMiddlename($customer->getMiddlename())
->setCustomerPrefix($customer->getPrefix())
->setCustomerSuffix($customer->getSuffix())
->setCustomerGroupId($customer->getGroupId());

$this->orderRepository->save($order);

$this->eventManager->dispatch(
'sales_order_customer_assign_after',
[
'order' => $order,
'customer' => $customer
]
);
}
}
30 changes: 16 additions & 14 deletions app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\CustomerAssignment;

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

/**
* @var CustomerAssignment
*/
private $assignmentService;

/**
* AssignOrderToCustomerObserver constructor.
*
* @param OrderRepositoryInterface $orderRepository
* @param CustomerAssignment $assignmentService
*/
public function __construct(OrderRepositoryInterface $orderRepository)
{
public function __construct(
OrderRepositoryInterface $orderRepository,
CustomerAssignment $assignmentService
) {
$this->orderRepository = $orderRepository;
$this->assignmentService = $assignmentService;
}

/**
Expand All @@ -43,18 +55,8 @@ public function execute(Observer $observer)
if (array_key_exists('__sales_assign_order_id', $delegateData)) {
$orderId = $delegateData['__sales_assign_order_id'];
$order = $this->orderRepository->get($orderId);
if (!$order->getCustomerId()) {
//assign customer info to order after customer creation.
$order->setCustomerId($customer->getId())
->setCustomerIsGuest(0)
->setCustomerEmail($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerMiddlename($customer->getMiddlename())
->setCustomerPrefix($customer->getPrefix())
->setCustomerSuffix($customer->getSuffix())
->setCustomerGroupId($customer->getGroupId());
$this->orderRepository->save($order);
if (!$order->getCustomerId() && $customer->getId()) {
$this->assignmentService->execute($order, $customer);
}
}
}
Expand Down
155 changes: 155 additions & 0 deletions app/code/Magento/Sales/Test/Unit/Model/Order/CustomerAssigmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\CustomerAssignment;

/**
* Test for Magento\Sales\Model\Order\CustomerAssignment class.
*/
class CustomerAssigmentTest extends \PHPUnit\Framework\TestCase
{
/**
* @var CustomerAssignment
*/
private $customerAssignment;

/**
* @var OrderInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $orderMock;

/**
* @var CustomerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $customerMock;

/**
* @var OrderRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $orderRepositoryMock;

/**
* @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $eventManagerMock;

/**
* Tests 'execute' method.
*
* @dataProvider executeDataProvider
* @param array $data
*/
public function testExecute(array $data): void
{
$this->configureOrderMock($data);
$this->configureCustomerMock($data);
$this->orderRepositoryMock->expects($this->once())->method('save')->with($this->orderMock);
$this->eventManagerMock->expects($this->once())->method('dispatch')->with(
'sales_order_customer_assign_after',
[
'order' => $this->orderMock,
'customer' => $this->customerMock
]
);

$this->customerAssignment->execute($this->orderMock, $this->customerMock);
}

/**
*
* Data provider for testExecute.
* @return array
*/
public function executeDataProvider(): array
{
return [
[
[
'customerId' => 1,
'customerIsGuest' => false,
'customerEmail' => 'customerEmail',
'customerFirstname' => 'customerFirstname',
'customerLastname' => 'customerLastname',
'customerMiddlename' => 'customerMiddlename',
'customerPrefix' => 'customerPrefix',
'customerSuffix' => 'customerSuffix',
'customerGroupId' => 'customerGroupId',
],
],
];
}

/**
* @return void
*/
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->orderMock = $this->createMock(OrderInterface::class);
$this->customerMock = $this->createMock(CustomerInterface::class);
$this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class);
$this->eventManagerMock = $this->createMock(ManagerInterface::class);
$this->customerAssignment = $objectManager->getObject(
CustomerAssignment::class,
[
'eventManager' => $this->eventManagerMock,
'orderRepository' => $this->orderRepositoryMock
]
);
}

/**
* Set up order mock.
*
* @param array $data
*/
private function configureOrderMock(array $data): void
{
$this->orderMock->expects($this->once())->method('setCustomerId')->with($data['customerId'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerIsGuest')->with($data['customerIsGuest'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerEmail')->with($data['customerEmail'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerFirstname')->with($data['customerFirstname'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerLastname')->with($data['customerLastname'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerMiddlename')->with($data['customerMiddlename'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerPrefix')->with($data['customerPrefix'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerSuffix')->with($data['customerSuffix'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerGroupId')->with($data['customerGroupId'])
->willReturn($this->orderMock);
}

/**
* Set up customer mock.
*
* @param array $data
*/
private function configureCustomerMock(array $data): void
{
$this->customerMock->expects($this->once())->method('getId')->willReturn($data['customerId']);
$this->customerMock->expects($this->once())->method('getEmail')->willReturn($data['customerEmail']);
$this->customerMock->expects($this->once())->method('getFirstname')->willReturn($data['customerFirstname']);
$this->customerMock->expects($this->once())->method('getLastname')->willReturn($data['customerLastname']);
$this->customerMock->expects($this->once())->method('getMiddlename')->willReturn($data['customerMiddlename']);
$this->customerMock->expects($this->once())->method('getPrefix')->willReturn($data['customerPrefix']);
$this->customerMock->expects($this->once())->method('getSuffix')->willReturn($data['customerSuffix']);
$this->customerMock->expects($this->once())->method('getGroupId')->willReturn($data['customerGroupId']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Event\Observer;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\CustomerAssignment;
use Magento\Sales\Observer\AssignOrderToCustomerObserver;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
Expand All @@ -27,6 +28,9 @@ class AssignOrderToCustomerObserverTest extends TestCase
/** @var OrderRepositoryInterface|PHPUnit_Framework_MockObject_MockObject */
protected $orderRepositoryMock;

/** @var CustomerAssignment | PHPUnit_Framework_MockObject_MockObject */
protected $assignmentMock;

/**
* Set Up
*/
Expand All @@ -35,17 +39,23 @@ protected function setUp()
$this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
->disableOriginalConstructor()
->getMock();
$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock);

$this->assignmentMock = $this->getMockBuilder(CustomerAssignment::class)
->disableOriginalConstructor()
->getMock();

$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->assignmentMock);
}

/**
* Test assigning order to customer after issuing guest order
*
* @dataProvider getCustomerIds
* @param null|int $orderCustomerId
* @param null|int $customerId
* @return void
*/
public function testAssignOrderToCustomerAfterGuestOrder($customerId)
public function testAssignOrderToCustomerAfterGuestOrder($orderCustomerId, $customerId)
{
$orderId = 1;
/** @var Observer|PHPUnit_Framework_MockObject_MockObject $observerMock */
Expand All @@ -62,31 +72,24 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
->getMockForAbstractClass();
$observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
$eventMock->expects($this->any())->method('getData')
->willReturnMap([
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
['customer_data_object', null, $customerMock]
]);
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
->willReturnMap(
[
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
['customer_data_object', null, $customerMock]
]
);
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($orderCustomerId);
$this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId)
->willReturn($orderMock);

$orderMock->expects($this->once())->method('setCustomerId')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerIsGuest')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerEmail')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerFirstname')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerLastname')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerMiddlename')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerPrefix')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerSuffix')->willReturn($orderMock);
$orderMock->expects($this->once())->method('setCustomerGroupId')->willReturn($orderMock);

if (!$customerId) {
$this->orderRepositoryMock->expects($this->once())->method('save')->with($orderMock);
if (!$orderCustomerId) {
$customerMock->expects($this->once())->method('getId')->willReturn($customerId);
$this->assignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock);
$this->sut->execute($observerMock);
return ;
return;
}

$this->orderRepositoryMock->expects($this->never())->method('save')->with($orderMock);
$this->assignmentMock->expects($this->never())->method('execute');
$this->sut->execute($observerMock);
}

Expand All @@ -97,6 +100,9 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
*/
public function getCustomerIds()
{
return [[null, 1]];
return [
[null, 1],
[1, 1],
];
}
}
Loading