Skip to content

Commit 70e85f6

Browse files
committed
MC-33165: Cannot change customer group
1 parent 1e28f35 commit 70e85f6

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
88

9+
use Magento\Customer\Api\GroupManagementInterface;
910
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Framework\App\ObjectManager;
1012
use Magento\Framework\Data\Form\Element\AbstractElement;
1113
use Magento\Framework\Pricing\PriceCurrencyInterface;
1214

@@ -50,6 +52,7 @@ class Account extends AbstractForm
5052
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
5153
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
5254
* @param array $data
55+
* @param GroupManagementInterface|null $groupManagement
5356
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5457
*/
5558
public function __construct(
@@ -62,11 +65,13 @@ public function __construct(
6265
\Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory,
6366
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
6467
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
65-
array $data = []
68+
array $data = [],
69+
?GroupManagementInterface $groupManagement = null
6670
) {
6771
$this->_metadataFormFactory = $metadataFormFactory;
6872
$this->customerRepository = $customerRepository;
6973
$this->_extensibleDataObjectConverter = $extensibleDataObjectConverter;
74+
$this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class);
7075
parent::__construct(
7176
$context,
7277
$sessionQuote,
@@ -78,6 +83,13 @@ public function __construct(
7883
);
7984
}
8085

86+
/**
87+
* Group Management
88+
*
89+
* @var GroupManagementInterface
90+
*/
91+
private $groupManagement;
92+
8193
/**
8294
* Return Header CSS Class
8395
*
@@ -163,6 +175,7 @@ public function getFormValues()
163175
{
164176
try {
165177
$customer = $this->customerRepository->getById($this->getCustomerId());
178+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
166179
} catch (\Exception $e) {
167180
$data = [];
168181
}
@@ -179,6 +192,10 @@ public function getFormValues()
179192
}
180193
}
181194

195+
if (array_key_exists('group_id', $data) && empty($data['group_id'])) {
196+
$data['group_id'] = $this->groupManagement->getDefaultGroup($this->getQuote()->getStoreId())->getId();
197+
}
198+
182199
if ($this->getQuote()->getCustomerEmail()) {
183200
$data['email'] = $this->getQuote()->getCustomerEmail();
184201
}

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ public function setAccountData($accountData)
16451645
$data,
16461646
\Magento\Customer\Api\Data\CustomerInterface::class
16471647
);
1648+
$customer->setStoreId($this->getQuote()->getStoreId());
16481649
$this->getQuote()->updateCustomerData($customer);
16491650
$data = [];
16501651

app/code/Magento/Sales/Test/Unit/Model/AdminOrder/CreateTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function testSetAccountData()
227227
'customer_tax_class_id' => $taxClassId
228228
]
229229
);
230+
$quote->method('getStoreId')->willReturn(1);
230231
$this->dataObjectHelper->method('populateWithArray')
231232
->with(
232233
$customer,
@@ -245,6 +246,10 @@ public function testSetAccountData()
245246
$this->groupRepository->method('getById')
246247
->willReturn($customerGroup);
247248

249+
$customer->expects($this->once())
250+
->method('setStoreId')
251+
->with(1);
252+
248253
$this->adminOrderCreate->setAccountData(['group_id' => 1]);
249254
}
250255

dev/tests/integration/testsuite/Magento/Sales/Block/Adminhtml/Order/Create/Form/AccountTest.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
1111

1212
use Magento\Backend\Model\Session\Quote as SessionQuote;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
1314
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1415
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
16+
use Magento\Customer\Api\Data\CustomerInterface;
1517
use Magento\Customer\Model\Data\Option;
1618
use Magento\Customer\Model\Metadata\Form;
1719
use Magento\Customer\Model\Metadata\FormFactory;
1820
use Magento\Framework\View\LayoutInterface;
1921
use Magento\Quote\Model\Quote;
22+
use Magento\Store\Model\StoreManagerInterface;
2023
use Magento\TestFramework\Helper\Bootstrap;
2124
use Magento\TestFramework\ObjectManager;
2225
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
2327

2428
/**
2529
* Class for test Account
2630
*
2731
* @magentoAppArea adminhtml
2832
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2933
*/
30-
class AccountTest extends \PHPUnit\Framework\TestCase
34+
class AccountTest extends TestCase
3135
{
3236
/**
3337
* @var Account
@@ -81,9 +85,9 @@ public function testGetFormWithCustomer()
8185
);
8286

8387
$fixtureCustomerId = 1;
84-
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
85-
$customerRepository = $this->objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
86-
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
88+
/** @var CustomerRepositoryInterface $customerRepository */
89+
$customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
90+
/** @var CustomerInterface $customer */
8791
$customer = $customerRepository->getById($fixtureCustomerId);
8892
$customer->setGroupId($customerGroup);
8993
$customerRepository->save($customer);
@@ -125,8 +129,8 @@ public function testGetFormWithCustomer()
125129
*/
126130
public function testGetFormWithUserDefinedAttribute()
127131
{
128-
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
129-
$storeManager = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class);
132+
/** @var StoreManagerInterface $storeManager */
133+
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
130134
$secondStore = $storeManager->getStore('secondstore');
131135

132136
$quoteSession = $this->objectManager->get(SessionQuote::class);
@@ -159,6 +163,46 @@ public function testGetFormWithUserDefinedAttribute()
159163
);
160164
}
161165

166+
/**
167+
* Test for get form with default customer group
168+
*
169+
*/
170+
public function testGetFormWithDefaultCustomerGroup()
171+
{
172+
$customerGroup = 0;
173+
$quote = $this->objectManager->create(Quote::class);
174+
$quote->setCustomerGroupId($customerGroup);
175+
176+
$this->session = $this->getMockBuilder(SessionQuote::class)
177+
->disableOriginalConstructor()
178+
->setMethods(['getCustomerId', 'getQuote'])
179+
->getMock();
180+
$this->session->method('getQuote')
181+
->willReturn($quote);
182+
$this->session->method('getCustomerId')
183+
->willReturn(1);
184+
185+
$formFactory = $this->getFormFactoryMock();
186+
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
187+
188+
/** @var LayoutInterface $layout */
189+
$layout = $this->objectManager->get(LayoutInterface::class);
190+
$accountBlock = $layout->createBlock(
191+
Account::class,
192+
'address_block' . rand(),
193+
['sessionQuote' => $this->session]
194+
);
195+
196+
$expectedGroupId = 1;
197+
$form = $accountBlock->getForm();
198+
199+
self::assertEquals(
200+
$expectedGroupId,
201+
$form->getElement('group_id')->getValue(),
202+
'The Customer Group specified for the chosen customer should be selected.'
203+
);
204+
}
205+
162206
/**
163207
* Creates a mock for Form object.
164208
*

0 commit comments

Comments
 (0)