Skip to content

Commit 3959e2e

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

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use Magento\Framework\Session\SessionManagerInterface;
5656
use Magento\Framework\Stdlib\DateTime;
5757
use Magento\Framework\Stdlib\StringUtils as StringHelper;
58+
use Magento\Framework\Validator\Factory as ValidatorFactory;
5859
use Magento\Store\Model\ScopeInterface;
5960
use Magento\Store\Model\StoreManagerInterface;
6061
use Psr\Log\LoggerInterface as PsrLogger;
@@ -405,11 +406,6 @@ class AccountManagement implements AccountManagementInterface
405406
*/
406407
private Authenticate $authenticate;
407408

408-
/**
409-
* @var AddressFactory
410-
*/
411-
private AddressFactory $addressFactory;
412-
413409
/**
414410
* @param CustomerFactory $customerFactory
415411
* @param ManagerInterface $eventManager
@@ -451,6 +447,7 @@ class AccountManagement implements AccountManagementInterface
451447
* @param CustomerLogger|null $customerLogger
452448
* @param Authenticate|null $authenticate
453449
* @param AddressFactory|null $addressFactory
450+
* @param ValidatorFactory|null $validatorFactory
454451
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
455452
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
456453
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -497,7 +494,8 @@ public function __construct(
497494
?Backend $eavValidator = null,
498495
?CustomerLogger $customerLogger = null,
499496
?Authenticate $authenticate = null,
500-
?AddressFactory $addressFactory = null,
497+
private ?AddressFactory $addressFactory = null,
498+
private ?ValidatorFactory $validatorFactory = null,
501499
) {
502500
$this->customerFactory = $customerFactory;
503501
$this->eventManager = $eventManager;
@@ -543,6 +541,7 @@ public function __construct(
543541
$this->customerLogger = $customerLogger ?? $objectManager->get(CustomerLogger::class);
544542
$this->authenticate = $authenticate ?? $objectManager->get(Authenticate::class);
545543
$this->addressFactory = $addressFactory ?? $objectManager->get(AddressFactory::class);
544+
$this->validatorFactory = $validatorFactory ?? $objectManager->get(ValidatorFactory::class);
546545
}
547546

548547
/**
@@ -932,17 +931,16 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
932931
$customerAddresses,
933932
fn ($address) => $this->isAddressAllowedForWebsite($address, $customer->getStoreId())
934933
);
934+
$addressValidator = $this->validatorFactory->createValidator('customer_address', 'save');
935935
foreach ($customerAddresses as $address) {
936936
$addressModel = $this->addressFactory->create()->updateData($address);
937937
if ($this->configShare->isWebsiteScope()) {
938938
$addressModel->setStoreId($customer->getStoreId());
939939
}
940-
$errors = $addressModel->validate();
941-
if ($errors !== true) {
940+
if (!$addressValidator->isValid($addressModel)) {
942941
$exception = new InputException();
943-
foreach ($errors as $error) {
944-
$exception->addError($error);
945-
}
942+
$messages = array_merge(...array_values($addressValidator->getMessages()));
943+
array_walk($messages, [$exception, 'addError']);
946944
throw $exception;
947945
}
948946
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Magento\Framework\Session\SessionManagerInterface;
5353
use Magento\Framework\Stdlib\StringUtils;
5454
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
55+
use Magento\Framework\Validator\Factory as ValidatorFactory;
5556
use Magento\Store\Api\Data\StoreInterface;
5657
use Magento\Store\Model\StoreManagerInterface;
5758
use PHPUnit\Framework\MockObject\MockObject;
@@ -371,6 +372,10 @@ protected function setUp(): void
371372
AddressFactory::class,
372373
$this->createMock(AddressFactory::class)
373374
],
375+
[
376+
ValidatorFactory::class,
377+
$this->createMock(ValidatorFactory::class)
378+
],
374379
];
375380
$this->objectManagerHelper->prepareObjectManager($objects);
376381
$this->accountManagement = $this->objectManagerHelper->getObject(

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

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use Magento\Framework\Session\SessionManagerInterface;
6060
use Magento\Framework\Stdlib\StringUtils;
6161
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
62+
use Magento\Framework\Validator\Factory as ValidatorFactory;
6263
use Magento\Store\Model\ScopeInterface;
6364
use Magento\Store\Model\Store;
6465
use Magento\Store\Model\StoreManagerInterface;
@@ -248,6 +249,11 @@ class AccountManagementTest extends TestCase
248249
*/
249250
private $addressFactory;
250251

252+
/**
253+
* @var MockObject|ValidatorFactory
254+
*/
255+
private $validatorFactory;
256+
251257
/**
252258
* @var MockObject|AddressRegistry
253259
*/
@@ -336,6 +342,7 @@ protected function setUp(): void
336342
$this->sessionManager = $this->createMock(SessionManagerInterface::class);
337343
$this->saveHandler = $this->createMock(SaveHandlerInterface::class);
338344
$this->addressFactory = $this->createMock(AddressFactory::class);
345+
$this->validatorFactory = $this->createMock(ValidatorFactory::class);
339346

340347
$this->objectManagerHelper = new ObjectManagerHelper($this);
341348
$objects = [
@@ -420,6 +427,7 @@ protected function setUp(): void
420427
'addressRegistry' => $this->addressRegistryMock,
421428
'allowedCountriesReader' => $this->allowedCountriesReader,
422429
'addressFactory' => $this->addressFactory,
430+
'validatorFactory' => $this->validatorFactory,
423431
]
424432
);
425433
$this->objectManagerHelper->setBackwardCompatibleProperty(
@@ -547,7 +555,12 @@ public function testCreateAccountWithPasswordHashWithCustomerWithoutStoreId(): v
547555
$addressModel = $this->createMock(Address::class);
548556
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
549557
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
550-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
558+
$validator = $this->createMock(\Magento\Framework\Validator::class);
559+
$this->validatorFactory->expects($this->once())
560+
->method('createValidator')
561+
->with('customer_address', 'save')
562+
->willReturn($validator);
563+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
551564

552565
$this->customerRepository
553566
->expects($this->once())
@@ -629,7 +642,12 @@ public function testCreateAccountWithPasswordHashWithLocalizedException(): void
629642
$addressModel = $this->createMock(Address::class);
630643
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
631644
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
632-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
645+
$validator = $this->createMock(\Magento\Framework\Validator::class);
646+
$this->validatorFactory->expects($this->once())
647+
->method('createValidator')
648+
->with('customer_address', 'save')
649+
->willReturn($validator);
650+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
633651

634652
$this->customerRepository
635653
->expects($this->once())
@@ -711,7 +729,15 @@ public function testCreateAccountWithPasswordHashWithAddressException(): void
711729
$addressModel = $this->createMock(Address::class);
712730
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
713731
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
714-
$addressModel->expects($this->once())->method('validate')->willReturn([new Phrase('Exception message')]);
732+
$validator = $this->createMock(\Magento\Framework\Validator::class);
733+
$this->validatorFactory->expects($this->once())
734+
->method('createValidator')
735+
->with('customer_address', 'save')
736+
->willReturn($validator);
737+
$validator->expects($this->once())->method('isValid')->willReturn(false);
738+
$validator->expects($this->atLeastOnce())
739+
->method('getMessages')
740+
->willReturn([[new Phrase('Exception message')]]);
715741

716742
$this->customerRepository
717743
->expects($this->once())
@@ -885,7 +911,12 @@ public function testCreateAccountWithoutPassword(): void
885911
$addressModel = $this->createMock(Address::class);
886912
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
887913
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
888-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
914+
$validator = $this->createMock(\Magento\Framework\Validator::class);
915+
$this->validatorFactory->expects($this->once())
916+
->method('createValidator')
917+
->with('customer_address', 'save')
918+
->willReturn($validator);
919+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
889920

890921
$this->share->method('isWebsiteScope')
891922
->willReturn(true);
@@ -1171,7 +1202,12 @@ public function testCreateAccountWithPassword(): void
11711202
$addressModel = $this->createMock(Address::class);
11721203
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
11731204
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
1174-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
1205+
$validator = $this->createMock(\Magento\Framework\Validator::class);
1206+
$this->validatorFactory->expects($this->once())
1207+
->method('createValidator')
1208+
->with('customer_address', 'save')
1209+
->willReturn($validator);
1210+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
11751211

11761212
$this->share->method('isWebsiteScope')
11771213
->willReturn(true);
@@ -1345,7 +1381,12 @@ public function testCreateAccountWithGroupId(): void
13451381
$addressModel = $this->createMock(Address::class);
13461382
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
13471383
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
1348-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
1384+
$validator = $this->createMock(\Magento\Framework\Validator::class);
1385+
$this->validatorFactory->expects($this->once())
1386+
->method('createValidator')
1387+
->with('customer_address', 'save')
1388+
->willReturn($validator);
1389+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
13491390

13501391
$this->share->method('isWebsiteScope')
13511392
->willReturn(true);
@@ -2294,13 +2335,20 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
22942335
$this->addressFactory->expects($this->exactly(2))
22952336
->method('create')
22962337
->willReturnOnConsecutiveCalls($existingAddressModel, $nonExistingAddressModel);
2297-
$existingAddressModel->expects($this->once())->method('updateData')->with($existingAddress)->willReturnSelf();
2298-
$existingAddressModel->expects($this->once())->method('validate')->willReturn(true);
2338+
$existingAddressModel->expects($this->once())
2339+
->method('updateData')
2340+
->with($existingAddress)
2341+
->willReturnSelf();
22992342
$nonExistingAddressModel->expects($this->once())
23002343
->method('updateData')
23012344
->with($nonExistingAddress)
23022345
->willReturnSelf();
2303-
$nonExistingAddressModel->expects($this->once())->method('validate')->willReturn(true);
2346+
$validator = $this->createMock(\Magento\Framework\Validator::class);
2347+
$this->validatorFactory->expects($this->once())
2348+
->method('createValidator')
2349+
->with('customer_address', 'save')
2350+
->willReturn($validator);
2351+
$validator->expects($this->exactly(2))->method('isValid')->willReturn(true);
23042352

23052353
$this->storeManager
23062354
->expects($this->atLeastOnce())
@@ -2422,7 +2470,12 @@ public function testCreateAccountUnexpectedValueException(): void
24222470
$addressModel = $this->createMock(Address::class);
24232471
$this->addressFactory->expects($this->once())->method('create')->willReturn($addressModel);
24242472
$addressModel->expects($this->once())->method('updateData')->with($address)->willReturnSelf();
2425-
$addressModel->expects($this->once())->method('validate')->willReturn(true);
2473+
$validator = $this->createMock(\Magento\Framework\Validator::class);
2474+
$this->validatorFactory->expects($this->once())
2475+
->method('createValidator')
2476+
->with('customer_address', 'save')
2477+
->willReturn($validator);
2478+
$validator->expects($this->once())->method('isValid')->with($addressModel)->willReturn(true);
24262479

24272480
$this->share->method('isWebsiteScope')
24282481
->willReturn(true);

0 commit comments

Comments
 (0)