Skip to content
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

Conversation

adrian-martinez-interactiv4
Copy link
Contributor

@adrian-martinez-interactiv4 adrian-martinez-interactiv4 commented Oct 30, 2017

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::isValidationRequiredmethod:

    /**
     * Retrieve current validation state
     *
     * @return boolean
     */
    public function isValidationRequired()
    {
        return $this->_appMode == \Magento\Framework\App\State::MODE_DEVELOPER;
    }

Actual result:
captura de pantalla 2017-10-30 a las 3 46 56
captura de pantalla 2017-10-30 a las 3 47 15

Desired behaviour would be check the layout update xml against the schema, and show error message to the user if it is invalid:
captura de pantalla 2017-10-30 a las 3 58 10

Also, when layout update is validated, validation exceptions are unhandled, showing a report error in production mode, and showing this error in developer mode:
captura de pantalla 2017-10-30 a las 1 03 01

For example, uncomment demo layout handle in home page:
captura de pantalla 2017-10-30 a las 1 02 34

Desired behaviour would be handle the exception properly instead of crash, and show error message to the user:
captura de pantalla 2017-10-30 a las 1 26 21

Description

  • According to doc block, \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

  1. Enable developer mode

  2. Edit or create a CMS page, enter some invalid xml in Layout Update XML textarea

  3. Try to save the page, application crashes

  4. Enable production mode

  5. Edit or create a CMS page, enter some invalid xml in Layout Update XML textarea

  6. Try to save the page, xml does not get validated

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

Copy link
Contributor

@dmanners dmanners left a 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;
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really allowed to do that?
captura de pantalla 2017-10-30 a las 12 38 37

If not, may I use instanceof a generic exception catch?

CMS Page - Force validate layout update xml, even in production mode, when saving CMS Page
@adrian-martinez-interactiv4 adrian-martinez-interactiv4 force-pushed the FR22#CMS-PAGE-HANDLE-LAYOUT-UPDATE-XML-VALIDATION-EXCEPTION branch from b22ec04 to 6f33525 Compare October 30, 2017 13:57
@adrian-martinez-interactiv4 adrian-martinez-interactiv4 changed the title CMS Page - Handle layout update xml validation exceptions CMS Page - CMS Page - Force validate layout update xml in production mode when saving CMS Page - Handle layout update xml validation exceptions Oct 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants