Skip to content

Commit bf67206

Browse files
ENGCOM-5188: [Backport] #22869 - defaulting customer storeId fix #22895
- Merge Pull Request #22895 from Wirson/magento2:22869-rest-customer-update-store-id-fix-2.2 - Merged commits: 1. 9fd7722 2. f84f0db
2 parents 0d4a9c6 + f84f0db commit bf67206

File tree

2 files changed

+32
-177
lines changed

2 files changed

+32
-177
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
215215
$storeId = $customerModel->getStoreId();
216216
if ($storeId === null) {
217217
$customerModel->setStoreId(
218-
$prevCustomerData->getStoreId() ?? $this->storeManager->getStore()->getId()
218+
$prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
219219
);
220220
}
221221

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/CustomerRepositoryTest.php

Lines changed: 31 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -19,95 +19,95 @@ class CustomerRepositoryTest extends \PHPUnit\Framework\TestCase
1919
/**
2020
* @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
2121
*/
22-
protected $customerFactory;
22+
private $customerFactory;
2323

2424
/**
2525
* @var \Magento\Customer\Model\Data\CustomerSecureFactory|\PHPUnit_Framework_MockObject_MockObject
2626
*/
27-
protected $customerSecureFactory;
27+
private $customerSecureFactory;
2828

2929
/**
3030
* @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
3131
*/
32-
protected $customerRegistry;
32+
private $customerRegistry;
3333

3434
/**
3535
* @var \Magento\Customer\Model\ResourceModel\AddressRepository|\PHPUnit_Framework_MockObject_MockObject
3636
*/
37-
protected $addressRepository;
37+
private $addressRepository;
3838

3939
/**
4040
* @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject
4141
*/
42-
protected $customerResourceModel;
42+
private $customerResourceModel;
4343

4444
/**
4545
* @var \Magento\Customer\Api\CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
4646
*/
47-
protected $customerMetadata;
47+
private $customerMetadata;
4848

4949
/**
5050
* @var \Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
5151
*/
52-
protected $searchResultsFactory;
52+
private $searchResultsFactory;
5353

5454
/**
5555
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
5656
*/
57-
protected $eventManager;
57+
private $eventManager;
5858

5959
/**
6060
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
6161
*/
62-
protected $storeManager;
62+
private $storeManager;
6363

6464
/**
6565
* @var \Magento\Framework\Api\ExtensibleDataObjectConverter|\PHPUnit_Framework_MockObject_MockObject
6666
*/
67-
protected $extensibleDataObjectConverter;
67+
private $extensibleDataObjectConverter;
6868

6969
/**
7070
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
7171
*/
72-
protected $dataObjectHelper;
72+
private $dataObjectHelper;
7373

7474
/**
7575
* @var \Magento\Framework\Api\ImageProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
7676
*/
77-
protected $imageProcessor;
77+
private $imageProcessor;
7878

7979
/**
8080
* @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
8181
*/
82-
protected $extensionAttributesJoinProcessor;
82+
private $extensionAttributesJoinProcessor;
8383

8484
/**
8585
* @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
8686
*/
87-
protected $customer;
87+
private $customer;
8888

8989
/**
9090
* @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
9191
*/
9292
private $collectionProcessorMock;
9393

9494
/**
95-
* @var \Magento\Customer\Model\ResourceModel\CustomerRepository
95+
* @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject
9696
*/
97-
protected $model;
97+
private $notificationStorage;
9898

9999
/**
100-
* @var NotificationStorage
100+
* @var \Magento\Customer\Model\ResourceModel\CustomerRepository
101101
*/
102-
private $notificationStorage;
102+
private $model;
103103

104104
protected function setUp()
105105
{
106106
$this->customerResourceModel =
107107
$this->createMock(\Magento\Customer\Model\ResourceModel\Customer::class);
108108
$this->customerRegistry = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
109109
$this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
110-
$this->customerFactory =
110+
$this->customerFactory =
111111
$this->createPartialMock(\Magento\Customer\Model\CustomerFactory::class, ['create']);
112112
$this->customerSecureFactory = $this->createPartialMock(
113113
\Magento\Customer\Model\Data\CustomerSecureFactory::class,
@@ -193,37 +193,7 @@ protected function setUp()
193193
public function testSave()
194194
{
195195
$customerId = 1;
196-
$storeId = 2;
197196

198-
$region = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\RegionInterface::class, [], '', false);
199-
$address = $this->getMockForAbstractClass(
200-
\Magento\Customer\Api\Data\AddressInterface::class,
201-
[],
202-
'',
203-
false,
204-
false,
205-
true,
206-
[
207-
'setCustomerId',
208-
'setRegion',
209-
'getRegion',
210-
'getId'
211-
]
212-
);
213-
$address2 = $this->getMockForAbstractClass(
214-
\Magento\Customer\Api\Data\AddressInterface::class,
215-
[],
216-
'',
217-
false,
218-
false,
219-
true,
220-
[
221-
'setCustomerId',
222-
'setRegion',
223-
'getRegion',
224-
'getId'
225-
]
226-
);
227197
$customerModel = $this->createPartialMock(
228198
\Magento\Customer\Model\Customer::class,
229199
[
@@ -246,10 +216,6 @@ public function testSave()
246216

247217
$origCustomer = $this->customer;
248218

249-
$this->customer->expects($this->atLeastOnce())
250-
->method('__toArray')
251-
->willReturn(['default_billing', 'default_shipping']);
252-
253219
$customerAttributesMetaData = $this->getMockForAbstractClass(
254220
\Magento\Framework\Api\CustomAttributesDataInterface::class,
255221
[],
@@ -265,17 +231,23 @@ public function testSave()
265231
'setAddresses'
266232
]
267233
);
268-
$customerSecureData = $this->createPartialMock(\Magento\Customer\Model\Data\CustomerSecure::class, [
234+
$customerSecureData = $this->createPartialMock(
235+
\Magento\Customer\Model\Data\CustomerSecure::class,
236+
[
269237
'getRpToken',
270238
'getRpTokenCreatedAt',
271239
'getPasswordHash',
272240
'getFailuresNum',
273241
'getFirstFailure',
274242
'getLockExpires',
275-
]);
243+
]
244+
);
276245
$this->customer->expects($this->atLeastOnce())
277246
->method('getId')
278247
->willReturn($customerId);
248+
$this->customer->expects($this->atLeastOnce())
249+
->method('__toArray')
250+
->willReturn([]);
279251
$this->customerRegistry->expects($this->atLeastOnce())
280252
->method('retrieve')
281253
->with($customerId)
@@ -290,28 +262,6 @@ public function testSave()
290262
$this->customerRegistry->expects($this->atLeastOnce())
291263
->method("remove")
292264
->with($customerId);
293-
$address->expects($this->once())
294-
->method('setCustomerId')
295-
->with($customerId)
296-
->willReturnSelf();
297-
$address->expects($this->once())
298-
->method('getRegion')
299-
->willReturn($region);
300-
$address->expects($this->atLeastOnce())
301-
->method('getId')
302-
->willReturn(7);
303-
$address->expects($this->once())
304-
->method('setRegion')
305-
->with($region);
306-
$customerAttributesMetaData->expects($this->atLeastOnce())
307-
->method('getAddresses')
308-
->willReturn([$address]);
309-
$customerAttributesMetaData->expects($this->at(1))
310-
->method('setAddresses')
311-
->with([]);
312-
$customerAttributesMetaData->expects($this->at(2))
313-
->method('setAddresses')
314-
->with([$address]);
315265
$this->extensibleDataObjectConverter->expects($this->once())
316266
->method('toNestedArray')
317267
->with($customerAttributesMetaData, [], \Magento\Customer\Api\Data\CustomerInterface::class)
@@ -323,17 +273,6 @@ public function testSave()
323273
$customerModel->expects($this->once())
324274
->method('getStoreId')
325275
->willReturn(null);
326-
$store = $this->createMock(\Magento\Store\Model\Store::class);
327-
$store->expects($this->once())
328-
->method('getId')
329-
->willReturn($storeId);
330-
$this->storeManager
331-
->expects($this->once())
332-
->method('getStore')
333-
->willReturn($store);
334-
$customerModel->expects($this->once())
335-
->method('setStoreId')
336-
->with($storeId);
337276
$customerModel->expects($this->once())
338277
->method('setId')
339278
->with($customerId);
@@ -400,12 +339,6 @@ public function testSave()
400339
$this->customerRegistry->expects($this->once())
401340
->method('push')
402341
->with($customerModel);
403-
$this->customer->expects($this->once())
404-
->method('getAddresses')
405-
->willReturn([$address, $address2]);
406-
$this->addressRepository->expects($this->once())
407-
->method('save')
408-
->with($address);
409342
$customerAttributesMetaData->expects($this->once())
410343
->method('getEmail')
411344
->willReturn('example@example.com');
@@ -436,7 +369,6 @@ public function testSave()
436369
public function testSaveWithPasswordHash()
437370
{
438371
$customerId = 1;
439-
$storeId = 2;
440372
$passwordHash = 'ukfa4sdfa56s5df02asdf4rt';
441373

442374
$customerSecureData = $this->createPartialMock(
@@ -450,47 +382,8 @@ public function testSaveWithPasswordHash()
450382
'getLockExpires',
451383
]
452384
);
453-
$region = $this->getMockForAbstractClass(
454-
\Magento\Customer\Api\Data\RegionInterface::class,
455-
[],
456-
'',
457-
false
458-
);
459-
$address = $this->getMockForAbstractClass(
460-
\Magento\Customer\Api\Data\AddressInterface::class,
461-
[],
462-
'',
463-
false,
464-
false,
465-
true,
466-
[
467-
'setCustomerId',
468-
'setRegion',
469-
'getRegion',
470-
'getId'
471-
]
472-
);
473-
$address2 = $this->getMockForAbstractClass(
474-
\Magento\Customer\Api\Data\AddressInterface::class,
475-
[],
476-
'',
477-
false,
478-
false,
479-
true,
480-
[
481-
'setCustomerId',
482-
'setRegion',
483-
'getRegion',
484-
'getId'
485-
]
486-
);
487-
488385
$origCustomer = $this->customer;
489386

490-
$this->customer->expects($this->atLeastOnce())
491-
->method('__toArray')
492-
->willReturn(['default_billing', 'default_shipping']);
493-
494387
$customerModel = $this->createPartialMock(
495388
\Magento\Customer\Model\Customer::class,
496389
[
@@ -534,6 +427,7 @@ public function testSaveWithPasswordHash()
534427
$this->customerRegistry->expects($this->atLeastOnce())
535428
->method('remove')
536429
->with($customerId);
430+
537431
$this->customerRegistry->expects($this->once())
538432
->method('retrieveSecureData')
539433
->with($customerId)
@@ -559,6 +453,9 @@ public function testSaveWithPasswordHash()
559453
$this->customer->expects($this->atLeastOnce())
560454
->method('getId')
561455
->willReturn($customerId);
456+
$this->customer->expects($this->atLeastOnce())
457+
->method('__toArray')
458+
->willReturn([]);
562459
$this->customerRegistry->expects($this->atLeastOnce())
563460
->method('retrieve')
564461
->with($customerId)
@@ -570,28 +467,6 @@ public function testSaveWithPasswordHash()
570467
->method('save')
571468
->with($this->customer, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $this->customer)
572469
->willReturn($customerAttributesMetaData);
573-
$address->expects($this->once())
574-
->method('setCustomerId')
575-
->with($customerId)
576-
->willReturnSelf();
577-
$address->expects($this->once())
578-
->method('getRegion')
579-
->willReturn($region);
580-
$address->expects($this->atLeastOnce())
581-
->method('getId')
582-
->willReturn(7);
583-
$address->expects($this->once())
584-
->method('setRegion')
585-
->with($region);
586-
$customerAttributesMetaData->expects($this->any())
587-
->method('getAddresses')
588-
->willReturn([$address]);
589-
$customerAttributesMetaData->expects($this->at(1))
590-
->method('setAddresses')
591-
->with([]);
592-
$customerAttributesMetaData->expects($this->at(2))
593-
->method('setAddresses')
594-
->with([$address]);
595470
$customerAttributesMetaData
596471
->expects($this->atLeastOnce())
597472
->method('getId')
@@ -604,20 +479,6 @@ public function testSaveWithPasswordHash()
604479
->method('create')
605480
->with(['data' => ['customerData']])
606481
->willReturn($customerModel);
607-
$customerModel->expects($this->once())
608-
->method('getStoreId')
609-
->willReturn(null);
610-
$store = $this->createMock(\Magento\Store\Model\Store::class);
611-
$store->expects($this->once())
612-
->method('getId')
613-
->willReturn($storeId);
614-
$this->storeManager
615-
->expects($this->once())
616-
->method('getStore')
617-
->willReturn($store);
618-
$customerModel->expects($this->once())
619-
->method('setStoreId')
620-
->with($storeId);
621482
$customerModel->expects($this->once())
622483
->method('setId')
623484
->with($customerId);
@@ -629,12 +490,6 @@ public function testSaveWithPasswordHash()
629490
$this->customerRegistry->expects($this->once())
630491
->method('push')
631492
->with($customerModel);
632-
$this->customer->expects($this->any())
633-
->method('getAddresses')
634-
->willReturn([$address, $address2]);
635-
$this->addressRepository->expects($this->once())
636-
->method('save')
637-
->with($address);
638493
$customerAttributesMetaData->expects($this->once())
639494
->method('getEmail')
640495
->willReturn('example@example.com');

0 commit comments

Comments
 (0)