Skip to content

Commit d47f683

Browse files
MC-33554: B2B - Customer Group Resets to Default (General) whenever the customer saves their account
1 parent 90334f8 commit d47f683

File tree

2 files changed

+97
-35
lines changed

2 files changed

+97
-35
lines changed

app/code/Magento/Customer/Model/CustomerExtractor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ public function extract(
9393
$customerData,
9494
\Magento\Customer\Api\Data\CustomerInterface::class
9595
);
96-
96+
9797
$store = $this->storeManager->getStore();
9898
$storeId = $store->getId();
99-
99+
100100
if ($isGroupIdEmpty) {
101+
$groupId = isset($customerData['group_id']) ? $customerData['group_id']
102+
: $this->customerGroupManagement->getDefaultGroup($storeId)->getId();
101103
$customerDataObject->setGroupId(
102-
$this->customerGroupManagement->getDefaultGroup($storeId)->getId()
104+
$groupId
103105
);
104106
}
105107

app/code/Magento/Customer/Test/Unit/Model/CustomerExtractorTest.php

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66

77
namespace Magento\Customer\Test\Unit\Model;
88

9+
use Magento\Customer\Api\Data\CustomerInterface;
10+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
11+
use Magento\Customer\Api\Data\GroupInterface;
12+
use Magento\Customer\Api\GroupManagementInterface;
913
use Magento\Customer\Model\CustomerExtractor;
14+
use Magento\Customer\Model\Metadata\Form;
15+
use Magento\Customer\Model\Metadata\FormFactory;
16+
use Magento\Framework\Api\DataObjectHelper;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Store\Api\Data\StoreInterface;
19+
use Magento\Store\Model\StoreManagerInterface;
1020

1121
/**
1222
* Unit test CustomerExtractorTest
@@ -16,40 +26,40 @@ class CustomerExtractorTest extends \PHPUnit\Framework\TestCase
1626
/** @var CustomerExtractor */
1727
protected $customerExtractor;
1828

19-
/** @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject */
29+
/** @var FormFactory|\PHPUnit_Framework_MockObject_MockObject */
2030
protected $formFactory;
2131

22-
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
32+
/** @var CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
2333
protected $customerFactory;
2434

25-
/** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
/** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
2636
protected $storeManager;
2737

28-
/** @var \Magento\Customer\Api\GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
38+
/** @var GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
2939
protected $customerGroupManagement;
3040

31-
/** @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject */
41+
/** @var DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject */
3242
protected $dataObjectHelper;
3343

34-
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
44+
/** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
3545
protected $request;
3646

37-
/** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject */
47+
/** @var Form|\PHPUnit_Framework_MockObject_MockObject */
3848
protected $customerForm;
3949

40-
/** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject */
50+
/** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject */
4151
protected $customerData;
4252

43-
/** @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject */
53+
/** @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject */
4454
protected $store;
4555

46-
/** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */
56+
/** @var GroupInterface|\PHPUnit_Framework_MockObject_MockObject */
4757
protected $customerGroup;
4858

4959
protected function setUp()
5060
{
5161
$this->formFactory = $this->getMockForAbstractClass(
52-
\Magento\Customer\Model\Metadata\FormFactory::class,
62+
FormFactory::class,
5363
[],
5464
'',
5565
false,
@@ -58,7 +68,7 @@ protected function setUp()
5868
['create']
5969
);
6070
$this->customerFactory = $this->getMockForAbstractClass(
61-
\Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
71+
CustomerInterfaceFactory::class,
6272
[],
6373
'',
6474
false,
@@ -67,34 +77,34 @@ protected function setUp()
6777
['create']
6878
);
6979
$this->storeManager = $this->getMockForAbstractClass(
70-
\Magento\Store\Model\StoreManagerInterface::class,
80+
StoreManagerInterface::class,
7181
[],
7282
'',
7383
false
7484
);
7585
$this->customerGroupManagement = $this->getMockForAbstractClass(
76-
\Magento\Customer\Api\GroupManagementInterface::class,
86+
GroupManagementInterface::class,
7787
[],
7888
'',
7989
false
8090
);
81-
$this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
82-
$this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class, [], '', false);
83-
$this->customerForm = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
91+
$this->dataObjectHelper = $this->createMock(DataObjectHelper::class);
92+
$this->request = $this->getMockForAbstractClass(RequestInterface::class, [], '', false);
93+
$this->customerForm = $this->createMock(Form::class);
8494
$this->customerData = $this->getMockForAbstractClass(
85-
\Magento\Customer\Api\Data\CustomerInterface::class,
95+
CustomerInterface::class,
8696
[],
8797
'',
8898
false
8999
);
90100
$this->store = $this->getMockForAbstractClass(
91-
\Magento\Store\Api\Data\StoreInterface::class,
101+
StoreInterface::class,
92102
[],
93103
'',
94104
false
95105
);
96106
$this->customerGroup = $this->getMockForAbstractClass(
97-
\Magento\Customer\Api\Data\GroupInterface::class,
107+
GroupInterface::class,
98108
[],
99109
'',
100110
false
@@ -108,14 +118,27 @@ protected function setUp()
108118
);
109119
}
110120

111-
public function testExtract()
121+
/**
122+
* @param int $storeId
123+
* @param int $websiteId
124+
* @param array $customerData
125+
* @dataProvider getDataProvider
126+
* @return void
127+
*/
128+
public function testExtract(int $storeId, int $websiteId, array $customerData)
112129
{
113-
$customerData = [
114-
'firstname' => 'firstname',
115-
'lastname' => 'firstname',
116-
'email' => 'email.example.com',
117-
];
130+
$this->initializeExpectation($storeId, $websiteId, $customerData);
118131

132+
$this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request));
133+
}
134+
135+
/**
136+
* @param int $storeId
137+
* @param int $websiteId
138+
* @param array $customerData
139+
*/
140+
private function initializeExpectation(int $storeId, int $websiteId, array $customerData): void
141+
{
119142
$this->formFactory->expects($this->once())
120143
->method('create')
121144
->with('customer', 'form-code')
@@ -136,24 +159,61 @@ public function testExtract()
136159
->willReturn($this->customerData);
137160
$this->dataObjectHelper->expects($this->once())
138161
->method('populateWithArray')
139-
->with($this->customerData, $customerData, \Magento\Customer\Api\Data\CustomerInterface::class)
162+
->with($this->customerData, $customerData, CustomerInterface::class)
140163
->willReturn($this->customerData);
141164
$this->storeManager->expects($this->once())
142165
->method('getStore')
143166
->willReturn($this->store);
144167
$this->store->expects($this->once())
145168
->method('getId')
146-
->willReturn(1);
169+
->willReturn($storeId);
147170
$this->store->expects($this->once())
148171
->method('getWebsiteId')
149-
->willReturn(1);
172+
->willReturn($websiteId);
150173
$this->customerData->expects($this->once())
151174
->method('setWebsiteId')
152-
->with(1);
175+
->with($websiteId);
153176
$this->customerData->expects($this->once())
154177
->method('setStoreId')
155-
->with(1);
178+
->with($storeId);
179+
}
156180

157-
$this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request));
181+
/**
182+
* @return array
183+
*/
184+
public function getDataProvider()
185+
{
186+
return [
187+
'extract data when group id is null' => [
188+
1,
189+
1,
190+
[
191+
'firstname' => 'firstname-1',
192+
'lastname' => 'firstname-1',
193+
'email' => 'email-1.example.com',
194+
'group_id' => null
195+
]
196+
],
197+
'extract data when group id is not null and default' => [
198+
1,
199+
2,
200+
[
201+
'firstname' => 'firstname-2',
202+
'lastname' => 'firstname-3',
203+
'email' => 'email-2.example.com',
204+
'group_id' => 1
205+
]
206+
],
207+
'extract data when group id is different from default' => [
208+
1,
209+
1,
210+
[
211+
'firstname' => 'firstname-3',
212+
'lastname' => 'firstname-3',
213+
'email' => 'email-3.example.com',
214+
'group_id' => 2
215+
]
216+
],
217+
];
158218
}
159219
}

0 commit comments

Comments
 (0)