Skip to content

Commit 651a526

Browse files
committed
MAGETWO-97397: A new customer created via an admin order has their Customer Group set to the Default customer group on the default website scope
- Set default customer group as selected in the dropdown
1 parent e62d140 commit 651a526

File tree

2 files changed

+115
-28
lines changed
  • app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form
  • dev/tests/integration/testsuite/Magento/Sales/Block/Adminhtml/Order/Create/Form

2 files changed

+115
-28
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1010
use Magento\Framework\Data\Form\Element\AbstractElement;
1111
use Magento\Framework\Pricing\PriceCurrencyInterface;
12+
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
1415
* Create order account form
@@ -132,15 +133,8 @@ protected function _prepareForm()
132133
$this->_addAttributesToForm($attributes, $fieldset);
133134

134135
$this->_form->addFieldNameSuffix('order[account]');
135-
136-
$formValues = $this->getFormValues();
137-
foreach ($attributes as $code => $attribute) {
138-
$defaultValue = $attribute->getDefaultValue();
139-
if (isset($defaultValue) && !isset($formValues[$code])) {
140-
$formValues[$code] = $defaultValue;
141-
}
142-
}
143-
$this->_form->setValues($formValues);
136+
$storeId = (int)$this->_sessionQuote->getStoreId();
137+
$this->_form->setValues($this->extractValuesFromAttributes($attributes, $storeId));
144138

145139
return $this;
146140
}
@@ -193,4 +187,42 @@ public function getFormValues()
193187

194188
return $data;
195189
}
190+
191+
/**
192+
* Extract the form values from attributes.
193+
*
194+
* @param array $attributes
195+
* @param int $storeId
196+
* @return array
197+
*/
198+
private function extractValuesFromAttributes(array $attributes, int $storeId): array
199+
{
200+
$formValues = $this->getFormValues();
201+
foreach ($attributes as $code => $attribute) {
202+
$defaultValue = $attribute->getDefaultValue();
203+
if (isset($defaultValue) && !isset($formValues[$code])) {
204+
$formValues[$code] = $defaultValue;
205+
}
206+
if ($code === 'group_id' && empty($defaultValue)) {
207+
$formValues[$code] = $this->getDefaultCustomerGroup($storeId);
208+
}
209+
}
210+
211+
return $formValues;
212+
}
213+
214+
/**
215+
* Gets default customer group.
216+
*
217+
* @param int $storeId
218+
* @return string|null
219+
*/
220+
private function getDefaultCustomerGroup(int $storeId): ?string
221+
{
222+
return $this->_scopeConfig->getValue(
223+
'customer/create_account/default_group',
224+
ScopeInterface::SCOPE_STORE,
225+
$storeId
226+
);
227+
}
196228
}

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

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,61 @@
1111
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
1212

1313
use Magento\Backend\Model\Session\Quote as SessionQuote;
14+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1415
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
16+
use Magento\Customer\Model\Data\Option;
1517
use Magento\Customer\Model\Metadata\Form;
1618
use Magento\Customer\Model\Metadata\FormFactory;
1719
use Magento\Framework\View\LayoutInterface;
1820
use Magento\Quote\Model\Quote;
1921
use Magento\TestFramework\Helper\Bootstrap;
22+
use Magento\TestFramework\ObjectManager;
23+
use PHPUnit\Framework\MockObject\MockObject;
2024

2125
/**
2226
* @magentoAppArea adminhtml
2327
*/
2428
class AccountTest extends \PHPUnit\Framework\TestCase
2529
{
26-
/** @var Account */
30+
/**
31+
* @var Account
32+
*/
2733
private $accountBlock;
2834

2935
/**
30-
* @var Bootstrap
36+
* @var ObjectManager
3137
*/
3238
private $objectManager;
3339

40+
/**
41+
* @var SessionQuote|MockObject
42+
*/
43+
private $session;
44+
3445
/**
3546
* @magentoDataFixture Magento/Sales/_files/quote.php
3647
*/
3748
protected function setUp()
3849
{
3950
$this->objectManager = Bootstrap::getObjectManager();
4051
$quote = $this->objectManager->create(Quote::class)->load(1);
41-
$sessionQuoteMock = $this->getMockBuilder(
42-
SessionQuote::class
43-
)->disableOriginalConstructor()->setMethods(
44-
['getCustomerId', 'getStore', 'getStoreId', 'getQuote']
45-
)->getMock();
46-
$sessionQuoteMock->expects($this->any())->method('getCustomerId')->will($this->returnValue(1));
47-
$sessionQuoteMock->expects($this->any())->method('getQuote')->will($this->returnValue($quote));
52+
53+
$this->session = $this->getMockBuilder(SessionQuote::class)
54+
->disableOriginalConstructor()
55+
->setMethods(['getCustomerId', 'getStore', 'getStoreId', 'getQuote', 'getQuoteId'])
56+
->getMock();
57+
$this->session->method('getCustomerId')
58+
->willReturn(1);
59+
$this->session->method('getQuote')
60+
->willReturn($quote);
61+
$this->session->method('getQuoteId')
62+
->willReturn($quote->getId());
4863
/** @var LayoutInterface $layout */
4964
$layout = $this->objectManager->get(LayoutInterface::class);
5065
$this->accountBlock = $layout->createBlock(
5166
Account::class,
5267
'address_block' . rand(),
53-
['sessionQuote' => $sessionQuoteMock]
68+
['sessionQuote' => $this->session]
5469
);
5570
parent::setUp();
5671
}
@@ -62,13 +77,13 @@ public function testGetForm()
6277
{
6378
$expectedFields = ['group_id', 'email'];
6479
$form = $this->accountBlock->getForm();
65-
$this->assertEquals(1, $form->getElements()->count(), "Form has invalid number of fieldsets");
80+
self::assertEquals(1, $form->getElements()->count(), "Form has invalid number of fieldsets");
6681
$fieldset = $form->getElements()[0];
6782

68-
$this->assertEquals(count($expectedFields), $fieldset->getElements()->count());
83+
self::assertEquals(count($expectedFields), $fieldset->getElements()->count());
6984

7085
foreach ($fieldset->getElements() as $element) {
71-
$this->assertTrue(
86+
self::assertTrue(
7287
in_array($element->getId(), $expectedFields),
7388
sprintf('Unexpected field "%s" in form.', $element->getId())
7489
);
@@ -79,6 +94,7 @@ public function testGetForm()
7994
* Tests a case when user defined custom attribute has default value.
8095
*
8196
* @magentoDataFixture Magento/Customer/_files/customer.php
97+
* @magentoConfigFixture current_store customer/create_account/default_group 3
8298
*/
8399
public function testGetFormWithUserDefinedAttribute()
84100
{
@@ -91,18 +107,27 @@ public function testGetFormWithUserDefinedAttribute()
91107

92108
$form = $accountBlock->getForm();
93109
$form->setUseContainer(true);
110+
$content = $form->toHtml();
94111

95-
$this->assertContains(
112+
self::assertContains(
96113
'<option value="1" selected="selected">Yes</option>',
97-
$form->toHtml(),
98-
'Default value for user defined custom attribute should be selected'
114+
$content,
115+
'Default value for user defined custom attribute should be selected.'
116+
);
117+
118+
self::assertContains(
119+
'<option value="3" selected="selected">Customer Group 1</option>',
120+
$content,
121+
'The Customer Group specified for the chosen store should be selected.'
99122
);
100123
}
101124

102125
/**
103-
* @return \PHPUnit_Framework_MockObject_MockObject
126+
* Creates a mock for Form object.
127+
*
128+
* @return MockObject
104129
*/
105-
private function getFormFactoryMock(): \PHPUnit_Framework_MockObject_MockObject
130+
private function getFormFactoryMock(): MockObject
106131
{
107132
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
108133
$attributeMetadataFactory = $this->objectManager->create(AttributeMetadataInterfaceFactory::class);
@@ -113,11 +138,12 @@ private function getFormFactoryMock(): \PHPUnit_Framework_MockObject_MockObject
113138
->setDefaultValue('1')
114139
->setFrontendLabel('Yes/No');
115140

141+
/** @var Form|MockObject $form */
116142
$form = $this->getMockBuilder(Form::class)
117143
->disableOriginalConstructor()
118144
->getMock();
119145
$form->method('getUserAttributes')->willReturn([$booleanAttribute]);
120-
$form->method('getSystemAttributes')->willReturn([]);
146+
$form->method('getSystemAttributes')->willReturn([$this->createCustomerGroupAttribute()]);
121147

122148
$formFactory = $this->getMockBuilder(FormFactory::class)
123149
->disableOriginalConstructor()
@@ -126,4 +152,33 @@ private function getFormFactoryMock(): \PHPUnit_Framework_MockObject_MockObject
126152

127153
return $formFactory;
128154
}
155+
156+
/**
157+
* Creates a customer group attribute object.
158+
*
159+
* @return AttributeMetadataInterface
160+
*/
161+
private function createCustomerGroupAttribute(): AttributeMetadataInterface
162+
{
163+
/** @var Option $option1 */
164+
$option1 = $this->objectManager->create(Option::class);
165+
$option1->setValue(3);
166+
$option1->setLabel('Customer Group 1');
167+
168+
/** @var Option $option2 */
169+
$option2 = $this->objectManager->create(Option::class);
170+
$option2->setValue(4);
171+
$option2->setLabel('Customer Group 2');
172+
173+
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
174+
$attributeMetadataFactory = $this->objectManager->create(AttributeMetadataInterfaceFactory::class);
175+
$attribute = $attributeMetadataFactory->create()
176+
->setAttributeCode('group_id')
177+
->setBackendType('static')
178+
->setFrontendInput('select')
179+
->setOptions([$option1, $option2])
180+
->setIsRequired(true);
181+
182+
return $attribute;
183+
}
129184
}

0 commit comments

Comments
 (0)