Skip to content

Commit b364ccb

Browse files
committed
ACP2E-3791: [Cloud] Delete operation is forbidden for current area error during customer account creation
1 parent 9a84997 commit b364ccb

File tree

1 file changed

+82
-121
lines changed

1 file changed

+82
-121
lines changed

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

Lines changed: 82 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -276,42 +276,37 @@ class AccountManagementTest extends TestCase
276276
/**
277277
* @var int
278278
*/
279-
private $getStoreIdCounter;
279+
private $getWebsiteIdCounter;
280280

281281
/**
282-
* @var int
282+
* @var int|null
283283
*/
284-
private $getWebsiteIdCounter;
284+
private $customerStoreId;
285285

286286
/**
287287
* @inheritDoc
288288
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
289289
*/
290290
protected function setUp(): void
291291
{
292-
293-
$this->customerFactory = $this->createPartialMock(CustomerFactory::class, ['create']);
294-
$this->manager = $this->getMockForAbstractClass(ManagerInterface::class);
295-
$this->store = $this->getMockBuilder(Store::class)
296-
->disableOriginalConstructor()
297-
->getMock();
298-
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
292+
$this->customerFactory = $this->createMock(CustomerFactory::class);
293+
$this->manager = $this->createMock(ManagerInterface::class);
294+
$this->store = $this->createMock(Store::class);
295+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
299296
$this->random = $this->createMock(Random::class);
300297
$this->validator = $this->createMock(Validator::class);
301298
$this->validationResultsInterfaceFactory = $this->createMock(
302299
ValidationResultsInterfaceFactory::class
303300
);
304-
$this->addressRepository = $this->getMockForAbstractClass(AddressRepositoryInterface::class);
305-
$this->customerMetadata = $this->getMockForAbstractClass(CustomerMetadataInterface::class);
301+
$this->addressRepository = $this->createMock(AddressRepositoryInterface::class);
302+
$this->customerMetadata = $this->createMock(CustomerMetadataInterface::class);
306303
$this->customerRegistry = $this->createMock(CustomerRegistry::class);
307-
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
308-
$this->encryptor = $this->getMockForAbstractClass(EncryptorInterface::class);
304+
$this->logger = $this->createMock(LoggerInterface::class);
305+
$this->encryptor = $this->createMock(EncryptorInterface::class);
309306
$this->share = $this->createMock(Share::class);
310307
$this->string = $this->createMock(StringUtils::class);
311-
$this->customerRepository = $this->getMockForAbstractClass(CustomerRepositoryInterface::class);
312-
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
313-
->disableOriginalConstructor()
314-
->getMockForAbstractClass();
308+
$this->customerRepository = $this->createMock(CustomerRepositoryInterface::class);
309+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
315310
$this->transportBuilder = $this->createMock(TransportBuilder::class);
316311
$this->dataObjectProcessor = $this->createMock(DataObjectProcessor::class);
317312
$this->registry = $this->createMock(Registry::class);
@@ -324,12 +319,8 @@ protected function setUp(): void
324319
ExtensibleDataObjectConverter::class
325320
);
326321
$this->allowedCountriesReader = $this->createMock(AllowedCountries::class);
327-
$this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
328-
->disableOriginalConstructor()
329-
->getMockForAbstractClass();
330-
$this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
331-
->disableOriginalConstructor()
332-
->getMockForAbstractClass();
322+
$this->authenticationMock = $this->createMock(AuthenticationInterface::class);
323+
$this->emailNotificationMock = $this->createMock(EmailNotificationInterface::class);
333324

334325
$this->customerSecure = $this->getMockBuilder(CustomerSecure::class)
335326
->onlyMethods(['addData', 'setData'])
@@ -341,16 +332,9 @@ protected function setUp(): void
341332
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
342333
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
343334

344-
$this->visitorCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
345-
->disableOriginalConstructor()
346-
->onlyMethods(['create'])
347-
->getMock();
348-
$this->sessionManager = $this->getMockBuilder(SessionManagerInterface::class)
349-
->disableOriginalConstructor()
350-
->getMockForAbstractClass();
351-
$this->saveHandler = $this->getMockBuilder(SaveHandlerInterface::class)
352-
->disableOriginalConstructor()
353-
->getMockForAbstractClass();
335+
$this->visitorCollectionFactory = $this->createMock(CollectionFactory::class);
336+
$this->sessionManager = $this->createMock(SessionManagerInterface::class);
337+
$this->saveHandler = $this->createMock(SaveHandlerInterface::class);
354338
$this->addressFactory = $this->createMock(AddressFactory::class);
355339

356340
$this->objectManagerHelper = new ObjectManagerHelper($this);
@@ -448,9 +432,11 @@ protected function setUp(): void
448432
'emailNotification',
449433
$this->emailNotificationMock
450434
);
435+
$this->allowedCountriesReader->method('getAllowedCountries')->willReturn(['US' => 'US']);
436+
451437
$this->getIdCounter = 0;
452-
$this->getStoreIdCounter = 0;
453438
$this->getWebsiteIdCounter = 0;
439+
$this->customerStoreId = null;
454440
}
455441

456442
/**
@@ -545,12 +531,17 @@ public function testCreateAccountWithPasswordHashWithCustomerWithoutStoreId(): v
545531
->method('getWebsiteId')
546532
->willReturn($websiteId);
547533
$customer->method('getStoreId')
548-
->willReturnOnConsecutiveCalls(null, null, 1);
534+
->willReturnCallback(fn () => $this->customerStoreId);
549535
$customer->expects($this->once())
550536
->method('setStoreId')
551-
->with($defaultStoreId);
537+
->with($defaultStoreId)
538+
->willReturnCallback(function ($storeId) use ($customer) {
539+
$this->customerStoreId = $storeId;
540+
return $customer;
541+
});
552542

553543
$address = $this->createMock(AddressInterface::class);
544+
$address->expects($this->atLeastOnce())->method('getCountryId')->willReturn('US');
554545
$customer->expects($this->once())->method('getAddresses')->willReturn([$address]);
555546
$customer->expects($this->once())->method('setAddresses')->with(null);
556547
$addressModel = $this->createMock(Address::class);
@@ -622,12 +613,17 @@ public function testCreateAccountWithPasswordHashWithLocalizedException(): void
622613
->method('getWebsiteId')
623614
->willReturn($websiteId);
624615
$customer->method('getStoreId')
625-
->willReturnOnConsecutiveCalls(null, null, 1);
616+
->willReturnCallback(fn () => $this->customerStoreId);
626617
$customer->expects($this->once())
627618
->method('setStoreId')
628-
->with($defaultStoreId);
619+
->with($defaultStoreId)
620+
->willReturnCallback(function ($storeId) use ($customer) {
621+
$this->customerStoreId = $storeId;
622+
return $customer;
623+
});
629624

630625
$address = $this->createMock(AddressInterface::class);
626+
$address->expects($this->atLeastOnce())->method('getCountryId')->willReturn('US');
631627
$customer->expects($this->once())->method('getAddresses')->willReturn([$address]);
632628
$customer->expects($this->once())->method('setAddresses')->with(null);
633629
$addressModel = $this->createMock(Address::class);
@@ -698,17 +694,18 @@ public function testCreateAccountWithPasswordHashWithAddressException(): void
698694
$customer->expects($this->atLeastOnce())
699695
->method('getWebsiteId')
700696
->willReturn($websiteId);
701-
$callCount = 0;
702-
$customer
703-
->method('getStoreId')
704-
->willReturnCallback(function () use (&$callCount) {
705-
return $callCount++ < 2 ? null : 1;
706-
});
697+
$customer->method('getStoreId')
698+
->willReturnCallback(fn () => $this->customerStoreId);
707699
$customer->expects($this->once())
708700
->method('setStoreId')
709-
->with($defaultStoreId);
701+
->with($defaultStoreId)
702+
->willReturnCallback(function ($storeId) use ($customer) {
703+
$this->customerStoreId = $storeId;
704+
return $customer;
705+
});
710706

711707
$address = $this->createMock(AddressInterface::class);
708+
$address->expects($this->atLeastOnce())->method('getCountryId')->willReturn('US');
712709
$customer->expects($this->once())->method('getAddresses')->willReturn([$address]);
713710
$customer->expects($this->once())->method('setAddresses')->with(null);
714711
$addressModel = $this->createMock(Address::class);
@@ -871,19 +868,15 @@ public function testCreateAccountWithoutPassword(): void
871868
$customer->expects($this->once())
872869
->method('setWebsiteId')
873870
->with($websiteId);
874-
$customer->expects($this->any())
875-
->method('getStoreId')
876-
->will($this->returnCallback(function () use ($testCase, $defaultStoreId) {
877-
if ($testCase->getStoreIdCounter > 0) {
878-
return $defaultStoreId;
879-
} else {
880-
$testCase->getStoreIdCounter += 1;
881-
return null;
882-
}
883-
}));
871+
$customer->method('getStoreId')
872+
->willReturnCallback(fn () => $this->customerStoreId);
884873
$customer->expects($this->once())
885874
->method('setStoreId')
886-
->with($defaultStoreId);
875+
->with($defaultStoreId)
876+
->willReturnCallback(function ($storeId) use ($customer) {
877+
$this->customerStoreId = $storeId;
878+
return $customer;
879+
});
887880

888881
$address = $this->createMock(AddressInterface::class);
889882
$address->expects($this->once())->method('setCustomerId')->with($customerId);
@@ -936,10 +929,6 @@ public function testCreateAccountWithoutPassword(): void
936929
$this->emailNotificationMock->expects($this->once())
937930
->method('newAccount')
938931
->willReturnSelf();
939-
$this->allowedCountriesReader
940-
->expects($this->atLeastOnce())
941-
->method('getAllowedCountries')
942-
->willReturn(['US' => 'US']);
943932
$address
944933
->expects($this->atLeastOnce())
945934
->method('getCountryId')
@@ -1165,19 +1154,15 @@ public function testCreateAccountWithPassword(): void
11651154
$customer->expects($this->once())
11661155
->method('setWebsiteId')
11671156
->with($websiteId);
1168-
$customer->expects($this->any())
1169-
->method('getStoreId')
1170-
->will($this->returnCallback(function () use ($testCase, $defaultStoreId) {
1171-
if ($testCase->getStoreIdCounter > 0) {
1172-
return $defaultStoreId;
1173-
} else {
1174-
$testCase->getStoreIdCounter += 1;
1175-
return null;
1176-
}
1177-
}));
1157+
$customer->method('getStoreId')
1158+
->willReturnCallback(fn () => $this->customerStoreId);
11781159
$customer->expects($this->once())
11791160
->method('setStoreId')
1180-
->with($defaultStoreId);
1161+
->with($defaultStoreId)
1162+
->willReturnCallback(function ($storeId) use ($customer) {
1163+
$this->customerStoreId = $storeId;
1164+
return $customer;
1165+
});
11811166

11821167
$address = $this->createMock(AddressInterface::class);
11831168
$address->expects($this->once())->method('setCustomerId')->with($customerId);
@@ -1230,10 +1215,6 @@ public function testCreateAccountWithPassword(): void
12301215
$this->emailNotificationMock->expects($this->once())
12311216
->method('newAccount')
12321217
->willReturnSelf();
1233-
$this->allowedCountriesReader
1234-
->expects($this->atLeastOnce())
1235-
->method('getAllowedCountries')
1236-
->willReturn(['US' => 'US']);
12371218
$address
12381219
->expects($this->atLeastOnce())
12391220
->method('getCountryId')
@@ -1347,19 +1328,15 @@ public function testCreateAccountWithGroupId(): void
13471328
$customer->expects($this->once())
13481329
->method('setWebsiteId')
13491330
->with($websiteId);
1350-
$customer->expects($this->any())
1351-
->method('getStoreId')
1352-
->will($this->returnCallback(function () use ($testCase, $defaultStoreId) {
1353-
if ($testCase->getStoreIdCounter > 0) {
1354-
return $defaultStoreId;
1355-
} else {
1356-
$testCase->getStoreIdCounter += 1;
1357-
return null;
1358-
}
1359-
}));
1331+
$customer->method('getStoreId')
1332+
->willReturnCallback(fn () => $this->customerStoreId);
13601333
$customer->expects($this->once())
13611334
->method('setStoreId')
1362-
->with($defaultStoreId);
1335+
->with($defaultStoreId)
1336+
->willReturnCallback(function ($storeId) use ($customer) {
1337+
$this->customerStoreId = $storeId;
1338+
return $customer;
1339+
});
13631340

13641341
$address = $this->createMock(AddressInterface::class);
13651342
$address->expects($this->once())->method('setCustomerId')->with($customerId);
@@ -1412,10 +1389,6 @@ public function testCreateAccountWithGroupId(): void
14121389
$this->emailNotificationMock->expects($this->once())
14131390
->method('newAccount')
14141391
->willReturnSelf();
1415-
$this->allowedCountriesReader
1416-
->expects($this->atLeastOnce())
1417-
->method('getAllowedCountries')
1418-
->willReturn(['US' => 'US']);
14191392
$address
14201393
->expects($this->atLeastOnce())
14211394
->method('getCountryId')
@@ -2241,18 +2214,20 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
22412214
$store->expects($this->any())
22422215
->method('getWebsiteId')
22432216
->willReturn($websiteId);
2217+
22442218
//Handle address - existing and non-existing. Non-Existing should return null when call getId method
22452219
$existingAddress = $this->createMock(AddressInterface::class);
2220+
$existingAddress->expects($this->atLeastOnce())
2221+
->method('getCountryId')
2222+
->willReturn('US');
22462223
$nonExistingAddress = $this->createMock(AddressInterface::class);
2224+
$nonExistingAddress->expects($this->atLeastOnce())
2225+
->method('getCountryId')
2226+
->willReturn('US');
22472227
//Ensure that existing address is not in use
2248-
$this->addressRepository
2249-
->expects($this->atLeastOnce())
2250-
->method("save")
2251-
->willReturnCallback(function ($arg1) use ($existingAddress, $nonExistingAddress) {
2252-
if ($arg1 == $existingAddress || $arg1 == $nonExistingAddress) {
2253-
return null;
2254-
}
2255-
});
2228+
$this->addressRepository->expects($this->exactly(2))
2229+
->method('save')
2230+
->willReturnArgument(0);
22562231

22572232
$existingAddress
22582233
->expects($this->any())
@@ -2332,7 +2307,7 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
23322307
->method('getStore')
23332308
->willReturn($store);
23342309
$this->share
2335-
->expects($this->once())
2310+
->expects($this->atLeastOnce())
23362311
->method('isWebsiteScope')
23372312
->willReturn(true);
23382313
$website = $this->getMockBuilder(Website::class)
@@ -2346,14 +2321,6 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
23462321
->method('getWebsite')
23472322
->with($websiteId)
23482323
->willReturn($website);
2349-
$this->allowedCountriesReader
2350-
->expects($this->atLeastOnce())
2351-
->method('getAllowedCountries')
2352-
->willReturn(['US' => 'US']);
2353-
$existingAddress
2354-
->expects($this->atLeastOnce())
2355-
->method('getCountryId')
2356-
->willReturn('US');
23572324

23582325
$this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash));
23592326
}
@@ -2438,19 +2405,15 @@ public function testCreateAccountUnexpectedValueException(): void
24382405
$customer->expects($this->once())
24392406
->method('setWebsiteId')
24402407
->with($websiteId);
2441-
$customer->expects($this->any())
2442-
->method('getStoreId')
2443-
->will($this->returnCallback(function () use ($testCase, $defaultStoreId) {
2444-
if ($testCase->getStoreIdCounter > 0) {
2445-
return $defaultStoreId;
2446-
} else {
2447-
$testCase->getStoreIdCounter += 1;
2448-
return null;
2449-
}
2450-
}));
2408+
$customer->method('getStoreId')
2409+
->willReturnCallback(fn () => $this->customerStoreId);
24512410
$customer->expects($this->once())
24522411
->method('setStoreId')
2453-
->with($defaultStoreId);
2412+
->with($defaultStoreId)
2413+
->willReturnCallback(function ($storeId) use ($customer) {
2414+
$this->customerStoreId = $storeId;
2415+
return $customer;
2416+
});
24542417

24552418
$address = $this->createMock(AddressInterface::class);
24562419
$address->expects($this->once())->method('setCustomerId')->with($customerId);
@@ -2504,8 +2467,6 @@ public function testCreateAccountUnexpectedValueException(): void
25042467
->method('newAccount')
25052468
->willThrowException($exception);
25062469
$this->logger->expects($this->once())->method('error')->with($exception);
2507-
$this->allowedCountriesReader->expects($this->atLeastOnce())
2508-
->method('getAllowedCountries')->willReturn(['US' => 'US']);
25092470
$address->expects($this->atLeastOnce())->method('getCountryId')->willReturn('US');
25102471
$this->accountManagement->createAccount($customer);
25112472
}

0 commit comments

Comments
 (0)