Skip to content

Commit 9afeea3

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98087' into 2.3-develop-pr17
2 parents 1e7e38a + 6c3686a commit 9afeea3

File tree

4 files changed

+71
-48
lines changed

4 files changed

+71
-48
lines changed

app/code/Magento/Backend/Model/Session/Quote.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @method Quote setOrderId($orderId)
2525
* @method int getOrderId()
2626
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2728
* @since 100.0.2
2829
*/
2930
class Quote extends \Magento\Framework\Session\SessionManager
@@ -149,7 +150,8 @@ public function getQuote()
149150
$this->_quote = $this->quoteFactory->create();
150151
if ($this->getStoreId()) {
151152
if (!$this->getQuoteId()) {
152-
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
153+
$customerGroupId = $this->groupManagement->getDefaultGroup($this->getStoreId())->getId();
154+
$this->_quote->setCustomerGroupId($customerGroupId);
153155
$this->_quote->setIsActive(false);
154156
$this->_quote->setStoreId($this->getStoreId());
155157

app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ public function testGetQuoteWithoutQuoteId()
267267
$cartInterfaceMock->expects($this->atLeastOnce())->method('getId')->willReturn($quoteId);
268268
$defaultGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)->getMock();
269269
$defaultGroup->expects($this->any())->method('getId')->will($this->returnValue($customerGroupId));
270-
$this->groupManagementMock->expects($this->any())->method('getDefaultGroup')->willReturn($defaultGroup);
270+
$this->groupManagementMock
271+
->method('getDefaultGroup')
272+
->with($storeId)
273+
->willReturn($defaultGroup);
271274

272275
$dataCustomerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
273276
->disableOriginalConstructor()

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
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;
1312

1413
/**
1514
* Create order account form
@@ -133,8 +132,9 @@ protected function _prepareForm()
133132
$this->_addAttributesToForm($attributes, $fieldset);
134133

135134
$this->_form->addFieldNameSuffix('order[account]');
136-
$storeId = (int)$this->_sessionQuote->getStoreId();
137-
$this->_form->setValues($this->extractValuesFromAttributes($attributes, $storeId));
135+
136+
$formValues = $this->extractValuesFromAttributes($attributes);
137+
$this->_form->setValues($formValues);
138138

139139
return $this;
140140
}
@@ -192,37 +192,18 @@ public function getFormValues()
192192
* Extract the form values from attributes.
193193
*
194194
* @param array $attributes
195-
* @param int $storeId
196195
* @return array
197196
*/
198-
private function extractValuesFromAttributes(array $attributes, int $storeId): array
197+
private function extractValuesFromAttributes(array $attributes): array
199198
{
200199
$formValues = $this->getFormValues();
201200
foreach ($attributes as $code => $attribute) {
202201
$defaultValue = $attribute->getDefaultValue();
203202
if (isset($defaultValue) && !isset($formValues[$code])) {
204203
$formValues[$code] = $defaultValue;
205204
}
206-
if ($code === 'group_id' && empty($formValues[$code])) {
207-
$formValues[$code] = $this->getDefaultCustomerGroup($storeId);
208-
}
209205
}
210206

211207
return $formValues;
212208
}
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-
}
228209
}

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

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8-
98
declare(strict_types=1);
109

1110
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
@@ -23,7 +22,10 @@
2322
use PHPUnit\Framework\MockObject\MockObject;
2423

2524
/**
25+
* Class for test Account
26+
*
2627
* @magentoAppArea adminhtml
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2729
*/
2830
class AccountTest extends \PHPUnit\Framework\TestCase
2931
{
@@ -43,42 +45,54 @@ class AccountTest extends \PHPUnit\Framework\TestCase
4345
private $session;
4446

4547
/**
46-
* @magentoDataFixture Magento/Sales/_files/quote.php
48+
* @inheritdoc
4749
*/
4850
protected function setUp()
4951
{
5052
$this->objectManager = Bootstrap::getObjectManager();
51-
$quote = $this->objectManager->create(Quote::class)->load(1);
53+
parent::setUp();
54+
}
55+
56+
/**
57+
* Test for get form with existing customer
58+
*
59+
* @magentoDataFixture Magento/Customer/_files/customer.php
60+
*/
61+
public function testGetFormWithCustomer()
62+
{
63+
$customerGroup = 2;
64+
$quote = $this->objectManager->create(Quote::class);
5265

5366
$this->session = $this->getMockBuilder(SessionQuote::class)
5467
->disableOriginalConstructor()
55-
->setMethods(['getCustomerId', 'getStore', 'getStoreId', 'getQuote', 'getQuoteId'])
68+
->setMethods(['getCustomerId','getQuote'])
5669
->getMock();
57-
$this->session->method('getCustomerId')
58-
->willReturn(1);
5970
$this->session->method('getQuote')
6071
->willReturn($quote);
61-
$this->session->method('getQuoteId')
62-
->willReturn($quote->getId());
72+
$this->session->method('getCustomerId')
73+
->willReturn(1);
74+
6375
/** @var LayoutInterface $layout */
6476
$layout = $this->objectManager->get(LayoutInterface::class);
6577
$this->accountBlock = $layout->createBlock(
6678
Account::class,
6779
'address_block' . rand(),
6880
['sessionQuote' => $this->session]
6981
);
70-
parent::setUp();
71-
}
7282

73-
/**
74-
* @magentoDataFixture Magento/Customer/_files/customer.php
75-
*/
76-
public function testGetForm()
77-
{
83+
$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 */
87+
$customer = $customerRepository->getById($fixtureCustomerId);
88+
$customer->setGroupId($customerGroup);
89+
$customerRepository->save($customer);
90+
7891
$expectedFields = ['group_id', 'email'];
7992
$form = $this->accountBlock->getForm();
8093
self::assertEquals(1, $form->getElements()->count(), "Form has invalid number of fieldsets");
8194
$fieldset = $form->getElements()[0];
95+
$content = $form->toHtml();
8296

8397
self::assertEquals(count($expectedFields), $fieldset->getElements()->count());
8498

@@ -88,22 +102,45 @@ public function testGetForm()
88102
sprintf('Unexpected field "%s" in form.', $element->getId())
89103
);
90104
}
105+
106+
self::assertContains(
107+
'<option value="'.$customerGroup.'" selected="selected">Wholesale</option>',
108+
$content,
109+
'The Customer Group specified for the chosen customer should be selected.'
110+
);
111+
112+
self::assertContains(
113+
'value="'.$customer->getEmail().'"',
114+
$content,
115+
'The Customer Email specified for the chosen customer should be input '
116+
);
91117
}
92118

93119
/**
94120
* Tests a case when user defined custom attribute has default value.
95121
*
96-
* @magentoDataFixture Magento/Customer/_files/customer.php
97-
* @magentoConfigFixture current_store customer/create_account/default_group 3
122+
* @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
123+
* @magentoConfigFixture current_store customer/create_account/default_group 2
124+
* @magentoConfigFixture secondstore_store customer/create_account/default_group 3
98125
*/
99126
public function testGetFormWithUserDefinedAttribute()
100127
{
128+
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
129+
$storeManager = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class);
130+
$secondStore = $storeManager->getStore('secondstore');
131+
132+
$quoteSession = $this->objectManager->get(SessionQuote::class);
133+
$quoteSession->setStoreId($secondStore->getId());
134+
101135
$formFactory = $this->getFormFactoryMock();
102136
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
103137

104138
/** @var LayoutInterface $layout */
105139
$layout = $this->objectManager->get(LayoutInterface::class);
106-
$accountBlock = $layout->createBlock(Account::class, 'address_block' . rand());
140+
$accountBlock = $layout->createBlock(
141+
Account::class,
142+
'address_block' . rand()
143+
);
107144

108145
$form = $accountBlock->getForm();
109146
$form->setUseContainer(true);
@@ -116,7 +153,7 @@ public function testGetFormWithUserDefinedAttribute()
116153
);
117154

118155
self::assertContains(
119-
'<option value="3" selected="selected">Customer Group 1</option>',
156+
'<option value="3" selected="selected">Retailer</option>',
120157
$content,
121158
'The Customer Group specified for the chosen store should be selected.'
122159
);
@@ -162,13 +199,13 @@ private function createCustomerGroupAttribute(): AttributeMetadataInterface
162199
{
163200
/** @var Option $option1 */
164201
$option1 = $this->objectManager->create(Option::class);
165-
$option1->setValue(3);
166-
$option1->setLabel('Customer Group 1');
202+
$option1->setValue(2);
203+
$option1->setLabel('Wholesale');
167204

168205
/** @var Option $option2 */
169206
$option2 = $this->objectManager->create(Option::class);
170-
$option2->setValue(4);
171-
$option2->setLabel('Customer Group 2');
207+
$option2->setValue(3);
208+
$option2->setLabel('Retailer');
172209

173210
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
174211
$attributeMetadataFactory = $this->objectManager->create(AttributeMetadataInterfaceFactory::class);

0 commit comments

Comments
 (0)