-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Fixed 'Delete operation is forbidden for current area' error. #31318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed 'Delete operation is forbidden for current area' error. #31318
Conversation
This error happens if customer address doesn't pass validation during account creation.
Hi @vovayatsyuk. Thank you for your contribution
❗ Automated tests can be triggered manually with an appropriate comment:
You can find more information about the builds here ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review. For more details, please, review the Magento Contributor Guide documentation. 🕙 You can find the schedule on the Magento Community Calendar page. 📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket. 🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel ✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @vovayatsyuk. The fix totally makes sense for me. Thank you.
Could I ask you to cover this case with an integration test that will trigger the InputException
, please?
Thanks
Hi @vovayatsyuk, could you please cover your changes by integration test? |
…dation-was-failed
There is a test that already checks account creation with an exception during address save. The only part that is missing in the test is to check if I don't know how to do that since My zero testing knowledge doesn't allow me to understand how to do this properly. Can you help? |
I think I got it. I should add a test to |
The test checks if email is still available after failed try with incorrect address.
@magento run all tests |
Hello, @vovayatsyuk looks like the introduced test falls. |
Can you please advise? When I'm running it as a single test is works fine:
But when I run whole class:
It fails. Do you have any idea? |
@vovayatsyuk you can check the previous test cases in the test class looks like they affect your test case. |
Okay. When I run all tests my fix is not working and I still get a I've updated the test. It still doesn't work, but at least it shows the real issue of why it's failed. |
It is necessary to make that all tests passed successfully. |
I found the reason for the failure (https://github.com/magento/magento2/blob/2.4-develop/dev/tests/integration/testsuite/Magento/Customer/_files/customer_rollback.php#L24):
p.s. There are 398 other test files that use this code too. I believe all of these lines should be removed if test doesn't change it back to unregistered state. |
@magento run Integration Tests |
I didn't fix it yet. The easiest way to fix the test is to unregister 'isSecureArea' flag before setting it. But I think it's not correct because the real problem is registered flags from previous tests. However, if you want me to add |
Could you try to fix it? Maybe create the right fixture and use it instead of the old one whenever it needs. |
@magento run all tests |
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time. |
Hi @vovayatsyuk, |
Yes, the problem is still here. |
…dation-was-failed
@magento run all tests |
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @vovayatsyuk,
Please provide more context on when this issue might happen. I see all linked issues already closed.
In the past, I had a similar issue on frontend when the customer was automatically created during the checkout process (using a custom checkout).
But then I I analyzed deeper and it seems like address validation can be simply skipped by adding the following flag ignore_validation_flag
to the customer model:
magento2/app/code/Magento/Customer/Model/AccountManagement.php
Lines 1649 to 1658 in 5676386
/** | |
* Set ignore_validation_flag for reset password flow to skip unnecessary address and customer validation | |
* | |
* @param Customer $customer | |
* @return void | |
*/ | |
private function setIgnoreValidationFlag($customer) | |
{ | |
$customer->setData('ignore_validation_flag', true); | |
} |
magento2/app/code/Magento/Customer/Model/ResourceModel/Customer/Relation.php
Lines 26 to 65 in 5676386
if (!$customer->getData('ignore_validation_flag')) { | |
/** @var \Magento\Customer\Model\Address $address */ | |
foreach ($customer->getAddresses() as $address) { | |
if ($address->getData('_deleted')) { | |
if ($address->getId() == $defaultBillingId) { | |
$customer->setData('default_billing', null); | |
} | |
if ($address->getId() == $defaultShippingId) { | |
$customer->setData('default_shipping', null); | |
} | |
$removedAddressId = $address->getId(); | |
$address->delete(); | |
// Remove deleted address from customer address collection | |
$customer->getAddressesCollection()->removeItemByKey($removedAddressId); | |
} else { | |
$address->setParentId( | |
$customer->getId() | |
)->setStoreId( | |
$customer->getStoreId() | |
)->setIsCustomerSaveTransaction( | |
true | |
)->save(); | |
if (($address->getIsPrimaryBilling() || | |
$address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId | |
) { | |
$customer->setData('default_billing', $address->getId()); | |
} | |
if (($address->getIsPrimaryShipping() || | |
$address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId | |
) { | |
$customer->setData('default_shipping', $address->getId()); | |
} | |
} | |
} | |
} |
magento2/app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php
Lines 265 to 285 in 5676386
if ($customer->getAddresses() !== null && !$customerModel->getData('ignore_validation_flag')) { | |
if ($customer->getId()) { | |
$existingAddresses = $this->getById($customer->getId())->getAddresses(); | |
$getIdFunc = function ($address) { | |
return $address->getId(); | |
}; | |
$existingAddressIds = array_map($getIdFunc, $existingAddresses); | |
} else { | |
$existingAddressIds = []; | |
} | |
$savedAddressIds = []; | |
foreach ($customer->getAddresses() as $address) { | |
$address->setCustomerId($customerId) | |
->setRegion($address->getRegion()); | |
$this->addressRepository->save($address); | |
if ($address->getId()) { | |
$savedAddressIds[] = $address->getId(); | |
} | |
} | |
$this->deleteAddressesByIds(array_diff($existingAddressIds, $savedAddressIds)); | |
} |
So having that in mind, it looks like we shouldn't fix this issue because we already have the mechanism for skipping address validation.
The problem appears when the exception is thrown which leads to the So, yes, if we can guarantee that exception will not be thrown we will never see this error. Unfortunately, I don't remember why it happened in the first place, maybe it was a custom theme that removed the address form from multishipping registration page. |
@vovayatsyuk, so please double-check it from your side, if adding such a flag fixes the issue for you - I think we can close this Pull Request as fixed. If it's not - please provide some steps to reproduce. |
Ok, let's close this PR |
@vovayatsyuk, thank you so much for contributing and trying to improve Magento! I'm very sorry to not process your pull request for three years 🤦♂️ |
Description
This error happens if the customer address doesn't pass validation during account creation.
Fixed Issues (if relevant)
Manual testing scenarios
Expected Behavior
The client should see the "Test" error message every time he submits the form.
Actual Behavior
The client sees the "Delete operation is forbidden for current area" error message for first time. Next submits tells that his email is already in use.
Contribution checklist (*)
Resolved issues: