Skip to content

Commit f34e607

Browse files
author
Oleksandr Dubovyk
committed
Merge remote-tracking branch 'mpi/MC-38915' into PR17november
2 parents 66d3829 + f6bb022 commit f34e607

File tree

4 files changed

+202
-32
lines changed

4 files changed

+202
-32
lines changed

app/code/Magento/Customer/Observer/AfterAddressSaveObserver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public function execute(Observer $observer)
141141
if ($customerAddress->getVatId() == ''
142142
|| !$this->_customerVat->isCountryInEU($customerAddress->getCountry())
143143
) {
144-
$defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId();
144+
$defaultGroupId = $customer->getGroupId() ? $customer->getGroupId() :
145+
$this->_groupManagement->getDefaultGroup($customer->getStore())->getId();
145146
if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
146147
$customer->setGroupId($defaultGroupId);
147148
$customer->save();
@@ -216,8 +217,8 @@ protected function _canProcessAddress($address)
216217
protected function _isDefaultBilling($address)
217218
{
218219
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
219-
|| $address->getIsPrimaryBilling()
220-
|| $address->getIsDefaultBilling();
220+
|| $address->getIsPrimaryBilling()
221+
|| $address->getIsDefaultBilling();
221222
}
222223

223224
/**
@@ -229,8 +230,8 @@ protected function _isDefaultBilling($address)
229230
protected function _isDefaultShipping($address)
230231
{
231232
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
232-
|| $address->getIsPrimaryShipping()
233-
|| $address->getIsDefaultShipping();
233+
|| $address->getIsPrimaryShipping()
234+
|| $address->getIsDefaultShipping();
234235
}
235236

236237
/**

app/code/Magento/Customer/Test/Unit/Observer/AfterAddressSaveObserverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public function testAfterAddressSaveDefaultGroup(
341341
$customer->expects($this->once())
342342
->method('getDisableAutoGroupChange')
343343
->willReturn(false);
344-
$customer->expects($this->once())
344+
$customer->expects($this->exactly(2))
345345
->method('getGroupId')
346346
->willReturn(null);
347347
$customer->expects($this->once())

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

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@
66

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

9+
use Magento\Backend\Block\Template\Context;
10+
use Magento\Backend\Model\Session\Quote;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
913
use Magento\Customer\Api\GroupManagementInterface;
14+
use Magento\Customer\Model\Metadata\Form;
1015
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1116
use Magento\Framework\App\ObjectManager;
1217
use Magento\Framework\Data\Form\Element\AbstractElement;
18+
use Magento\Framework\Data\FormFactory;
19+
use Magento\Customer\Model\Metadata\FormFactory as MetadataFormFactory;
20+
use Magento\Framework\Exception\LocalizedException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
use Magento\Framework\Phrase;
1323
use Magento\Framework\Pricing\PriceCurrencyInterface;
24+
use Magento\Framework\Reflection\DataObjectProcessor;
25+
use Magento\Sales\Model\AdminOrder\Create;
26+
use Magento\Store\Model\ScopeInterface;
1427

1528
/**
1629
* Create order account form
@@ -25,46 +38,48 @@ class Account extends AbstractForm
2538
/**
2639
* Metadata form factory
2740
*
28-
* @var \Magento\Customer\Model\Metadata\FormFactory
41+
* @var MetadataFormFactory
2942
*/
3043
protected $_metadataFormFactory;
3144

3245
/**
3346
* Customer repository
3447
*
35-
* @var \Magento\Customer\Api\CustomerRepositoryInterface
48+
* @var CustomerRepositoryInterface
3649
*/
3750
protected $customerRepository;
3851

3952
/**
40-
* @var \Magento\Framework\Api\ExtensibleDataObjectConverter
53+
* @var ExtensibleDataObjectConverter
4154
*/
4255
protected $_extensibleDataObjectConverter;
56+
4357
private const XML_PATH_EMAIL_REQUIRED_CREATE_ORDER = 'customer/create_account/email_required_create_order';
58+
4459
/**
45-
* @param \Magento\Backend\Block\Template\Context $context
46-
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
47-
* @param \Magento\Sales\Model\AdminOrder\Create $orderCreate
60+
* @param Context $context
61+
* @param Quote $sessionQuote
62+
* @param Create $orderCreate
4863
* @param PriceCurrencyInterface $priceCurrency
49-
* @param \Magento\Framework\Data\FormFactory $formFactory
50-
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
51-
* @param \Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory
52-
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
64+
* @param FormFactory $formFactory
65+
* @param DataObjectProcessor $dataObjectProcessor
66+
* @param MetadataFormFactory $metadataFormFactory
67+
* @param CustomerRepositoryInterface $customerRepository
5368
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
5469
* @param array $data
5570
* @param GroupManagementInterface|null $groupManagement
5671
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5772
*/
5873
public function __construct(
59-
\Magento\Backend\Block\Template\Context $context,
60-
\Magento\Backend\Model\Session\Quote $sessionQuote,
61-
\Magento\Sales\Model\AdminOrder\Create $orderCreate,
74+
Context $context,
75+
Quote $sessionQuote,
76+
Create $orderCreate,
6277
PriceCurrencyInterface $priceCurrency,
63-
\Magento\Framework\Data\FormFactory $formFactory,
64-
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
65-
\Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory,
66-
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
67-
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
78+
FormFactory $formFactory,
79+
DataObjectProcessor $dataObjectProcessor,
80+
MetadataFormFactory $metadataFormFactory,
81+
CustomerRepositoryInterface $customerRepository,
82+
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
6883
array $data = [],
6984
?GroupManagementInterface $groupManagement = null
7085
) {
@@ -103,7 +118,7 @@ public function getHeaderCssClass()
103118
/**
104119
* Return header text
105120
*
106-
* @return \Magento\Framework\Phrase
121+
* @return Phrase
107122
*/
108123
public function getHeaderText()
109124
{
@@ -114,10 +129,12 @@ public function getHeaderText()
114129
* Prepare Form and add elements to form
115130
*
116131
* @return $this
132+
* @throws LocalizedException
133+
* @throws NoSuchEntityException
117134
*/
118135
protected function _prepareForm()
119136
{
120-
/** @var \Magento\Customer\Model\Metadata\Form $customerForm */
137+
/** @var Form $customerForm */
121138
$customerForm = $this->_metadataFormFactory->create('customer', 'adminhtml_checkout');
122139

123140
// prepare customer attributes to show
@@ -170,6 +187,8 @@ protected function _addAdditionalFormElementData(AbstractElement $element)
170187
* Return Form Elements values
171188
*
172189
* @return array
190+
* @throws LocalizedException
191+
* @throws NoSuchEntityException
173192
*/
174193
public function getFormValues()
175194
{
@@ -183,7 +202,7 @@ public function getFormValues()
183202
? $this->_extensibleDataObjectConverter->toFlatArray(
184203
$customer,
185204
[],
186-
\Magento\Customer\Api\Data\CustomerInterface::class
205+
CustomerInterface::class
187206
)
188207
: [];
189208
foreach ($this->getQuote()->getData() as $key => $value) {
@@ -193,7 +212,7 @@ public function getFormValues()
193212
}
194213

195214
if (array_key_exists('group_id', $data) && empty($data['group_id'])) {
196-
$data['group_id'] = $this->groupManagement->getDefaultGroup($this->getQuote()->getStoreId())->getId();
215+
$data['group_id'] = $this->getSelectedGroupId();
197216
}
198217

199218
if ($this->getQuote()->getCustomerEmail()) {
@@ -208,6 +227,8 @@ public function getFormValues()
208227
*
209228
* @param array $attributes
210229
* @return array
230+
* @throws LocalizedException
231+
* @throws NoSuchEntityException
211232
*/
212233
private function extractValuesFromAttributes(array $attributes): array
213234
{
@@ -231,7 +252,24 @@ private function isEmailRequiredToCreateOrder()
231252
{
232253
return $this->_scopeConfig->getValue(
233254
self::XML_PATH_EMAIL_REQUIRED_CREATE_ORDER,
234-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
255+
ScopeInterface::SCOPE_STORE
235256
);
236257
}
258+
259+
/**
260+
* Retrieve selected group id
261+
*
262+
* @return string
263+
* @throws LocalizedException
264+
* @throws NoSuchEntityException
265+
*/
266+
private function getSelectedGroupId(): string
267+
{
268+
$selectedGroupId = $this->groupManagement->getDefaultGroup($this->getQuote()->getStoreId())->getId();
269+
$orderDetails = $this->getRequest()->getParam('order');
270+
if (!empty($orderDetails) && !empty($orderDetails['account']['group_id'])) {
271+
$selectedGroupId = $orderDetails['account']['group_id'];
272+
}
273+
return $selectedGroupId;
274+
}
237275
}

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

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
1111

12+
use Magento\Backend\Block\Template\Context;
1213
use Magento\Backend\Model\Session\Quote as SessionQuote;
1314
use Magento\Customer\Api\CustomerRepositoryInterface;
1415
use Magento\Customer\Api\Data\AttributeMetadataInterface;
@@ -17,6 +18,7 @@
1718
use Magento\Customer\Model\Data\Option;
1819
use Magento\Customer\Model\Metadata\Form;
1920
use Magento\Customer\Model\Metadata\FormFactory;
21+
use Magento\Framework\App\RequestInterface as Request;
2022
use Magento\Framework\View\LayoutInterface;
2123
use Magento\Quote\Model\Quote;
2224
use Magento\Store\Model\StoreManagerInterface;
@@ -107,7 +109,7 @@ public function testGetFormWithCustomer()
107109
);
108110
}
109111

110-
self::assertRegExp(
112+
self::assertMatchesRegularExpression(
111113
'/<option value="'.$customerGroup.'".*?selected="selected"\>Wholesale\<\/option\>/is',
112114
$content,
113115
'The Customer Group specified for the chosen customer should be selected.'
@@ -150,13 +152,13 @@ public function testGetFormWithUserDefinedAttribute()
150152
$form->setUseContainer(true);
151153
$content = $form->toHtml();
152154

153-
self::assertRegExp(
155+
self::assertMatchesRegularExpression(
154156
'/\<option value="1".*?selected="selected"\>Yes\<\/option\>/is',
155157
$content,
156158
'Default value for user defined custom attribute should be selected.'
157159
);
158160

159-
self::assertRegExp(
161+
self::assertMatchesRegularExpression(
160162
'/<option value="3".*?selected="selected"\>Retailer\<\/option\>/is',
161163
$content,
162164
'The Customer Group specified for the chosen store should be selected.'
@@ -203,6 +205,135 @@ public function testGetFormWithDefaultCustomerGroup()
203205
);
204206
}
205207

208+
/**
209+
* Test for get form with customer group based on vat id validation
210+
*
211+
* @dataProvider getDataForVatValidatedCustomer
212+
* @param int $defaultCustomerGroupId
213+
* @param int $vatValidatedCustomerGroupId
214+
* @param array $customerDetails
215+
* @param array $orderDetails
216+
* @return void
217+
*/
218+
public function testGetFormWithVatValidatedCustomerGroup(
219+
int $defaultCustomerGroupId,
220+
int $vatValidatedCustomerGroupId,
221+
array $customerDetails,
222+
array $orderDetails
223+
): void {
224+
$contextMock = $this->getMockBuilder(Context::class)
225+
->disableOriginalConstructor()
226+
->disableOriginalClone()
227+
->getMock();
228+
$requestMock = $this->getMockBuilder(Request::class)
229+
->getMockForAbstractClass();
230+
$contextMock->expects($this->once())
231+
->method('getRequest')
232+
->willReturn($requestMock);
233+
$requestMock->expects($this->any())
234+
->method('getParam')
235+
->willReturn($orderDetails);
236+
237+
$quote = $this->objectManager->create(Quote::class);
238+
$quote->setCustomerGroupId($defaultCustomerGroupId);
239+
$quote->setData($customerDetails);
240+
241+
$this->session = $this->getMockBuilder(SessionQuote::class)
242+
->disableOriginalConstructor()
243+
->setMethods(['getCustomerId', 'getQuote'])
244+
->getMock();
245+
$this->session->method('getQuote')
246+
->willReturn($quote);
247+
$this->session->method('getCustomerId')
248+
->willReturn($customerDetails['customer_id']);
249+
250+
$formFactory = $this->getFormFactoryMock();
251+
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
252+
253+
/** @var LayoutInterface $layout */
254+
$layout = $this->objectManager->get(LayoutInterface::class);
255+
$accountBlock = $layout->createBlock(
256+
Account::class,
257+
'address_block' . rand(),
258+
[
259+
'context' => $contextMock,
260+
'sessionQuote' => $this->session
261+
]
262+
);
263+
264+
$form = $accountBlock->getForm();
265+
266+
self::assertEquals(
267+
$vatValidatedCustomerGroupId,
268+
$form->getElement('group_id')->getValue(),
269+
'The Customer Group specified for the chosen customer should be selected.'
270+
);
271+
}
272+
273+
/**
274+
* Data provider for vat validated customer group id
275+
*
276+
* @return array
277+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
278+
*/
279+
public function getDataForVatValidatedCustomer(): array
280+
{
281+
return [
282+
'Validated customer group id when its set in quote' => [
283+
'defaultCustomerGroupId' => 0,
284+
'vatValidatedCustomerGroupId' => 3,
285+
'customerDetails' => [
286+
'entity_id' => '35',
287+
'store_id' => 1,
288+
'created_at' => '2020-11-09 01:03:35',
289+
'updated_at' => '2020-11-09 05:44:07',
290+
'customer_id' => 1,
291+
'customer_tax_class_id' => '3',
292+
'customer_group_id' => 3,
293+
'customer_email' => 'test@test.com',
294+
'customer_prefix' => null,
295+
'customer_firstname' => null,
296+
'customer_middlename' => null,
297+
'customer_lastname' => null,
298+
'customer_suffix' => null,
299+
'customer_dob' => null,
300+
],
301+
'orderDetails' => [
302+
'account' => [
303+
'group_id' => 3,
304+
'email' => 'test@test.com'
305+
]
306+
]
307+
],
308+
'Validated customer group id when its set in request' => [
309+
'defaultCustomerGroupId' => 0,
310+
'vatValidatedCustomerGroupId' => 3,
311+
'customerDetails' => [
312+
'entity_id' => '35',
313+
'store_id' => 1,
314+
'created_at' => '2020-11-09 01:03:35',
315+
'updated_at' => '2020-11-09 05:44:07',
316+
'customer_id' => 1,
317+
'customer_tax_class_id' => '3',
318+
'customer_group_id' => null,
319+
'customer_email' => 'test@test.com',
320+
'customer_prefix' => null,
321+
'customer_firstname' => null,
322+
'customer_middlename' => null,
323+
'customer_lastname' => null,
324+
'customer_suffix' => null,
325+
'customer_dob' => null,
326+
],
327+
'orderDetails' => [
328+
'account' => [
329+
'group_id' => 3,
330+
'email' => 'test@test.com'
331+
]
332+
]
333+
]
334+
];
335+
}
336+
206337
/**
207338
* Creates a mock for Form object.
208339
*

0 commit comments

Comments
 (0)