Skip to content

Commit 712a47b

Browse files
committed
Merge remote-tracking branch 'origin/MC-33165' into 2.4-develop-pr23
2 parents 64fc283 + 54cdb3b commit 712a47b

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
use Magento\Sales\Model\Order\Item as OrderItem;
3131
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
3232
use Magento\Store\Api\Data\StoreInterface;
33-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
33+
use PHPUnit\Framework\TestCase;
34+
use PHPUnit\Framework\MockObject\MockObject;
3435

3536
/**
3637
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3738
* @SuppressWarnings(PHPMD.TooManyFields)
3839
*/
39-
class CreateTest extends \PHPUnit\Framework\TestCase
40+
class CreateTest extends TestCase
4041
{
4142
const CUSTOMER_ID = 1;
4243

@@ -46,12 +47,12 @@ class CreateTest extends \PHPUnit\Framework\TestCase
4647
private $adminOrderCreate;
4748

4849
/**
49-
* @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
50+
* @var CartRepositoryInterface|MockObject
5051
*/
5152
private $quoteRepository;
5253

5354
/**
54-
* @var QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
55+
* @var QuoteFactory|MockObject
5556
*/
5657
private $quoteFactory;
5758

@@ -111,7 +112,7 @@ protected function setUp()
111112
->setMethods(['getForCustomer'])
112113
->getMockForAbstractClass();
113114

114-
$this->sessionQuote = $this->getMockBuilder(\Magento\Backend\Model\Session\Quote::class)
115+
$this->sessionQuote = $this->getMockBuilder(SessionQuote::class)
115116
->disableOriginalConstructor()
116117
->setMethods(
117118
[
@@ -227,6 +228,7 @@ public function testSetAccountData()
227228
'customer_tax_class_id' => $taxClassId
228229
]
229230
);
231+
$quote->method('getStoreId')->willReturn(1);
230232
$this->dataObjectHelper->method('populateWithArray')
231233
->with(
232234
$customer,
@@ -245,6 +247,10 @@ public function testSetAccountData()
245247
$this->groupRepository->method('getById')
246248
->willReturn($customerGroup);
247249

250+
$customer->expects($this->once())
251+
->method('setStoreId')
252+
->with(1);
253+
248254
$this->adminOrderCreate->setAccountData(['group_id' => 1]);
249255
}
250256

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)