Skip to content

Commit 9fe7ddf

Browse files
committed
Merge branch 'ACP2E-873' of https://github.com/magento-l3/magento2ce into ACP2E-873
2 parents 28c1f28 + 6872f62 commit 9fe7ddf

File tree

2 files changed

+170
-10
lines changed

2 files changed

+170
-10
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Customer address entity resource model
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
@@ -17,15 +15,11 @@
1715
use Magento\Framework\Exception\InputException;
1816

1917
/**
20-
* Address repository.
21-
*
2218
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2319
*/
2420
class AddressRepository implements \Magento\Customer\Api\AddressRepositoryInterface
2521
{
2622
/**
27-
* Directory data
28-
*
2923
* @var \Magento\Directory\Helper\Data
3024
*/
3125
protected $directoryData;
@@ -126,7 +120,11 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address)
126120
} else {
127121
$addressModel->updateData($address);
128122
}
129-
$addressModel->setStoreId($customerModel->getStoreId());
123+
if ($customerModel->getSharingConfig() !== null &&
124+
$customerModel->getSharingConfig()->isWebsiteScope()
125+
) {
126+
$addressModel->setStoreId($customerModel->getStoreId());
127+
}
130128

131129
$errors = $addressModel->validate();
132130
if ($errors !== true) {
@@ -275,7 +273,7 @@ private function getCollectionProcessor()
275273
{
276274
if (!$this->collectionProcessor) {
277275
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
278-
'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
276+
CollectionProcessorInterface::class
279277
);
280278
}
281279
return $this->collectionProcessor;

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

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory;
1313
use Magento\Customer\Model\AddressFactory;
1414
use Magento\Customer\Model\AddressRegistry;
15+
use Magento\Customer\Model\Config\Share;
1516
use Magento\Customer\Model\Customer;
1617
use Magento\Customer\Model\CustomerRegistry;
1718
use Magento\Customer\Model\ResourceModel\Address;
@@ -91,6 +92,11 @@ class AddressRepositoryTest extends TestCase
9192
*/
9293
private $collectionProcessor;
9394

95+
/**
96+
* @var Share|MockObject
97+
*/
98+
private $configShare;
99+
94100
protected function setUp(): void
95101
{
96102
$this->addressFactory = $this->createPartialMock(AddressFactory::class, ['create']);
@@ -112,9 +118,20 @@ protected function setUp(): void
112118
'',
113119
false
114120
);
115-
$this->customer = $this->createMock(Customer::class);
121+
$this->customer = $this->createPartialMock(
122+
Customer::class,
123+
['getSharingConfig','getAddressesCollection']
124+
);
116125
$this->address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->addMethods(
117-
['getCountryId', 'getFirstname', 'getLastname', 'getCity', 'getTelephone', 'getShouldIgnoreValidation']
126+
[
127+
'getCountryId',
128+
'getFirstname',
129+
'getLastname',
130+
'getCity',
131+
'getTelephone',
132+
'getShouldIgnoreValidation',
133+
'setStoreId'
134+
]
118135
)
119136
->onlyMethods(
120137
[
@@ -148,6 +165,11 @@ protected function setUp(): void
148165
$this->extensionAttributesJoinProcessor,
149166
$this->collectionProcessor
150167
);
168+
$this->configShare = $this->getMockBuilder(Share::class)->onlyMethods(
169+
['isWebsiteScope']
170+
)
171+
->disableOriginalConstructor()
172+
->getMock();
151173
}
152174

153175
public function testSave()
@@ -212,6 +234,146 @@ public function testSave()
212234
$this->repository->save($customerAddress);
213235
}
214236

237+
public function testSaveWithConfigCustomerAccountShareScopeWebsite()
238+
{
239+
$customerId = 34;
240+
$addressId = 53;
241+
$customerAddress = $this->getMockForAbstractClass(
242+
AddressInterface::class,
243+
[],
244+
'',
245+
false
246+
);
247+
$addressCollection =
248+
$this->createMock(Collection::class);
249+
$customerAddress->expects($this->atLeastOnce())
250+
->method('getCustomerId')
251+
->willReturn($customerId);
252+
$customerAddress->expects($this->atLeastOnce())
253+
->method('getId')
254+
->willReturn($addressId);
255+
$this->customerRegistry->expects($this->once())
256+
->method('retrieve')
257+
->with($customerId)
258+
->willReturn($this->customer);
259+
$this->address->expects($this->atLeastOnce())
260+
->method("getId")
261+
->willReturn($addressId);
262+
$this->addressRegistry->expects($this->once())
263+
->method('retrieve')
264+
->with($addressId)
265+
->willReturn(null);
266+
$this->addressFactory->expects($this->once())
267+
->method('create')
268+
->willReturn($this->address);
269+
$this->address->expects($this->once())
270+
->method('updateData')
271+
->with($customerAddress);
272+
$this->address->expects($this->once())
273+
->method('setCustomer')
274+
->with($this->customer);
275+
$this->customer->expects($this->exactly(2))
276+
->method('getSharingConfig')
277+
->willReturn($this->configShare);
278+
$this->configShare->expects($this->once())
279+
->method('isWebsiteScope')
280+
->willReturn(true);
281+
$this->address->expects($this->once())
282+
->method('setStoreId');
283+
$this->address->expects($this->once())
284+
->method('validate')
285+
->willReturn(true);
286+
$this->address->expects($this->once())
287+
->method('save');
288+
$this->addressRegistry->expects($this->once())
289+
->method('push')
290+
->with($this->address);
291+
$this->customer->expects($this->exactly(2))
292+
->method('getAddressesCollection')
293+
->willReturn($addressCollection);
294+
$addressCollection->expects($this->once())
295+
->method("removeItemByKey")
296+
->with($addressId);
297+
$addressCollection->expects($this->once())
298+
->method("addItem")
299+
->with($this->address);
300+
$this->address->expects($this->once())
301+
->method('getDataModel')
302+
->willReturn($customerAddress);
303+
304+
$this->repository->save($customerAddress);
305+
}
306+
307+
public function testSaveWithConfigCustomerAccountShareScopeGlobal()
308+
{
309+
$customerId = 34;
310+
$addressId = 53;
311+
$customerAddress = $this->getMockForAbstractClass(
312+
AddressInterface::class,
313+
[],
314+
'',
315+
false
316+
);
317+
$addressCollection =
318+
$this->createMock(Collection::class);
319+
$customerAddress->expects($this->atLeastOnce())
320+
->method('getCustomerId')
321+
->willReturn($customerId);
322+
$customerAddress->expects($this->atLeastOnce())
323+
->method('getId')
324+
->willReturn($addressId);
325+
$this->customerRegistry->expects($this->once())
326+
->method('retrieve')
327+
->with($customerId)
328+
->willReturn($this->customer);
329+
$this->address->expects($this->atLeastOnce())
330+
->method("getId")
331+
->willReturn($addressId);
332+
$this->addressRegistry->expects($this->once())
333+
->method('retrieve')
334+
->with($addressId)
335+
->willReturn(null);
336+
$this->addressFactory->expects($this->once())
337+
->method('create')
338+
->willReturn($this->address);
339+
$this->address->expects($this->once())
340+
->method('updateData')
341+
->with($customerAddress);
342+
$this->address->expects($this->once())
343+
->method('setCustomer')
344+
->with($this->customer);
345+
$this->customer->expects($this->exactly(2))
346+
->method('getSharingConfig')
347+
->willReturn($this->configShare);
348+
$this->configShare->expects($this->once())
349+
->method('isWebsiteScope')
350+
->willReturn(false);
351+
$this->address->expects($this->never())
352+
->method('setStoreId');
353+
$this->address->expects($this->once())
354+
->method('validate')
355+
->willReturn(true);
356+
$this->address->expects($this->once())
357+
->method('save');
358+
$this->addressRegistry->expects($this->once())
359+
->method('push')
360+
->with($this->address);
361+
$this->customer->expects($this->exactly(2))
362+
->method('getAddressesCollection')
363+
->willReturn($addressCollection);
364+
$addressCollection->expects($this->once())
365+
->method("removeItemByKey")
366+
->with($addressId);
367+
$addressCollection->expects($this->once())
368+
->method("addItem")
369+
->with($this->address);
370+
$this->address->expects($this->once())
371+
->method('getDataModel')
372+
->willReturn($customerAddress);
373+
374+
$this->repository->save($customerAddress);
375+
}
376+
215377
public function testSaveWithException()
216378
{
217379
$this->expectException(InputException::class);

0 commit comments

Comments
 (0)