1212use Magento \Customer \Api \Data \AddressSearchResultsInterfaceFactory ;
1313use Magento \Customer \Model \AddressFactory ;
1414use Magento \Customer \Model \AddressRegistry ;
15+ use Magento \Customer \Model \Config \Share ;
1516use Magento \Customer \Model \Customer ;
1617use Magento \Customer \Model \CustomerRegistry ;
1718use 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