-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
CMS Page - CMS Page - Force validate layout update xml in production mode when saving CMS Page - Handle layout update xml validation exceptions #11857
CMS Page - CMS Page - Force validate layout update xml in production mode when saving CMS Page - Handle layout update xml validation exceptions #11857
Conversation
b5d5f06
to
b22ec04
Compare
4926059
to
b22ec04
Compare
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.
Just a few suggestions on the exceptions and validation methods.
{ | ||
$hasError = false; | ||
if (!empty($data['layout_update_xml']) && !$validatorCustomLayout->isValid($data['layout_update_xml'])) { | ||
$hasError = true; |
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.
I would consider just a return true here
if (!empty($data['custom_layout_update_xml']) && | ||
!$validatorCustomLayout->isValid($data['custom_layout_update_xml']) | ||
) { | ||
$hasError = true; |
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.
I would consider just a return true here
) { | ||
$hasError = true; | ||
} | ||
return $hasError; |
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.
I would consider a return false;
and then we could remove the $hasError
variable here.
} catch (ValidationSchemaException $e) { | ||
} catch (\Exception $e) { | ||
$this->messageManager->addExceptionMessage($e); | ||
} finally { |
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.
I understand that you want to catch all other exceptions here but from a validation point I think there is only the ValidationException
and ValidationSchemaException
unless I am mistaken. In that case I would prefer the following.
try {
$hasError = $this->validateData($data, $validatorCustomLayout);
} catch (ValidationException $e || ValidationSchemaException $e) {
$validatorMessages = $validatorCustomLayout->getMessages();
$hasError = $hasError || (bool) count($validatorMessages);
foreach ($validatorMessages as $message) {
$this->messageManager->addErrorMessage($message);
}
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e);
}
What do you think?
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.
I hadn't seen that before, I mean, I didn't know I could catch several exceptions in a single catch block, I used finally to avoid logic duplication, but also that code must be executed if no exception is caught, so it can't be just there in the catch block. We have to find an intermediate solution.
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.
CMS Page - Force validate layout update xml, even in production mode, when saving CMS Page
b22ec04
to
6f33525
Compare
When saving custom layout update xml in CMS page, in production mode, xml is not validated against schema file, due to
\Magento\Framework\App\Arguments\ValidationState::isValidationRequired
method:Actual result:
Desired behaviour would be check the layout update xml against the schema, and show error message to the user if it is invalid:
Also, when layout update is validated, validation exceptions are unhandled, showing a report error in production mode, and showing this error in developer mode:
For example, uncomment demo layout handle in home page:
Desired behaviour would be handle the exception properly instead of crash, and show error message to the user:
Description
\Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::validate
should always return a boolean value. It uses\Magento\Framework\View\Model\Layout\Update\Validator::isValid
method, that may throw an exception if xml is not valid, so\Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::validate
must handle possible exceptions thrown by xml validator, in order to always return a boolean value.\Magento\Cms\Model\Page\DomValidationState
has been added, and it is injected from post data processor to force layout update xml validation, only upon cms page save.This PR include changes of PR #11865
Manual testing scenarios
Enable developer mode
Edit or create a CMS page, enter some invalid xml in Layout Update XML textarea
Try to save the page, application crashes
Enable production mode
Edit or create a CMS page, enter some invalid xml in Layout Update XML textarea
Try to save the page, xml does not get validated
Contribution checklist