Skip to content

Fix bug 27490: Can't change customer group when placing an admin order #27501

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 13 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
*/
namespace Magento\Quote\Observer\Frontend\Quote\Address;

use Magento\Framework\App\Area;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\Event\ObserverInterface;

/**
* Handle customer VAT number on collect_totals_before event of quote address.
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class CollectTotalsObserver implements ObserverInterface
{
Expand Down Expand Up @@ -51,6 +55,11 @@ class CollectTotalsObserver implements ObserverInterface
*/
protected $groupManagement;

/**
* @var State
*/
protected $state;

/**
* Initialize dependencies.
*
Expand All @@ -61,6 +70,7 @@ class CollectTotalsObserver implements ObserverInterface
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param \Magento\Customer\Model\Session $customerSession
* @param State|null $state
*/
public function __construct(
\Magento\Customer\Helper\Address $customerAddressHelper,
Expand All @@ -69,7 +79,8 @@ public function __construct(
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Customer\Model\Session $customerSession
\Magento\Customer\Model\Session $customerSession,
State $state = null
) {
$this->customerVat = $customerVat;
$this->customerAddressHelper = $customerAddressHelper;
Expand All @@ -78,6 +89,7 @@ public function __construct(
$this->groupManagement = $groupManagement;
$this->addressRepository = $addressRepository;
$this->customerSession = $customerSession;
$this->state = $state ?: ObjectManager::getInstance()->get(State::class);
}

/**
Expand Down Expand Up @@ -131,7 +143,12 @@ public function execute(\Magento\Framework\Event\Observer $observer)
);
}

if ($groupId !== null) {
// Do not update customer group as not logged in when doing process in admin area
if ($groupId !== null
&& !(
$this->state->getAreaCode() == Area::AREA_ADMINHTML
&& $groupId == $this->groupManagement->getNotLoggedInGroup()->getId()
)) {
$address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
$quote->setCustomerGroupId($groupId);
$this->customerSession->setCustomerGroupId($groupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Magento\Quote\Test\Unit\Observer\Frontend\Quote\Address;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;

/**
* Class CollectTotalsTest
*
Expand Down Expand Up @@ -88,6 +91,11 @@ class CollectTotalsObserverTest extends \PHPUnit\Framework\TestCase
*/
protected $groupInterfaceMock;

/**
* @var State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stateMock;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -149,6 +157,11 @@ protected function setUp()
['getId']
);

$this->stateMock = $this->createPartialMock(
State::class,
['getAreaCode']
);

$shippingAssignmentMock = $this->createMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
$shippingMock = $this->createMock(\Magento\Quote\Api\Data\ShippingInterface::class);
$shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock);
Expand Down Expand Up @@ -177,7 +190,8 @@ protected function setUp()
$this->customerDataFactoryMock,
$this->groupManagementMock,
$this->addressRepository,
$this->customerSession
$this->customerSession,
$this->stateMock
);
}

Expand Down Expand Up @@ -231,8 +245,53 @@ public function testDispatchWithCustomerCountryNotInEUAndNotLoggedCustomerInGrou
$this->model->execute($this->observerMock);
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function testDispatchWithNotLoggedCustomerInGroupAndInAdminArea()
{
$this->stateMock->expects($this->once())
->method('getAreaCode')->will($this->returnValue(Area::AREA_ADMINHTML));
$this->groupManagementMock->expects($this->exactly(2))
->method('getNotLoggedInGroup')
->will($this->returnValue($this->groupInterfaceMock));
$this->groupInterfaceMock->expects($this->exactly(2))
->method('getId')->will($this->returnValue(0));
$this->vatValidatorMock->expects($this->once())
->method('isEnabled')
->with($this->quoteAddressMock, $this->storeId)
->will($this->returnValue(true));

$this->quoteAddressMock->expects($this->once())
->method('getCountryId')
->will($this->returnValue('customerCountryCode'));
$this->quoteAddressMock->expects($this->once())->method('getVatId')->will($this->returnValue('vatId'));

$this->customerVatMock->expects(
$this->once()
)->method(
'isCountryInEU'
)->with(
'customerCountryCode'
)->will(
$this->returnValue(false)
);

$this->customerMock->expects($this->once())->method('getId')->will($this->returnValue(null));

/** Assertions */
$this->quoteAddressMock->expects($this->never())->method('setPrevQuoteCustomerGroupId');
$this->customerDataFactoryMock->expects($this->never())->method('mergeDataObjectWithArray');
$this->quoteMock->expects($this->never())->method('setCustomerGroupId');

/** SUT execution */
$this->model->execute($this->observerMock);
}

public function testDispatchWithDefaultCustomerGroupId()
{
$this->stateMock->expects($this->once())
->method('getAreaCode')->will($this->returnValue(Area::AREA_FRONTEND));
$this->vatValidatorMock->expects($this->once())
->method('isEnabled')
->with($this->quoteAddressMock, $this->storeId)
Expand Down Expand Up @@ -269,6 +328,8 @@ public function testDispatchWithDefaultCustomerGroupId()

public function testDispatchWithCustomerCountryInEU()
{
$this->stateMock->expects($this->once())
->method('getAreaCode')->will($this->returnValue(Area::AREA_FRONTEND));
$this->vatValidatorMock->expects($this->once())
->method('isEnabled')
->with($this->quoteAddressMock, $this->storeId)
Expand Down Expand Up @@ -353,6 +414,8 @@ public function testDispatchWithAddressCustomerVatIdAndCountryId()

public function testDispatchWithEmptyShippingAddress()
{
$this->stateMock->expects($this->once())
->method('getAreaCode')->will($this->returnValue(Area::AREA_FRONTEND));
$customerCountryCode = "DE";
$customerVat = "123123123";
$defaultShipping = 1;
Expand Down