From 7bed7ef0277a82552051c769afe054708c56bde7 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 20 Jul 2016 17:02:06 +0300 Subject: [PATCH 01/14] MAGETWO-55684: Fix XSD schema --- .../View/Layout/etc/layout_merged.xsd | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd index 88db4682622ff..4b2abef7e836b 100644 --- a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd +++ b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd @@ -20,8 +20,8 @@ - - + + @@ -44,6 +44,27 @@ + + + + + + + + + + Layout Type definition + + + + + + + + + + + From ab4bfaef4ff14140776e9e408fdbcae44c0f2f81 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Thu, 21 Jul 2016 16:03:32 +0300 Subject: [PATCH 02/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception --- lib/internal/Magento/Framework/Config/Dom.php | 4 +++- .../Config/Dom/ValidationSchemaException.php | 14 ++++++++++++ .../Framework/View/Model/Layout/Merge.php | 22 +++++++++++-------- .../View/Model/Layout/Update/Validator.php | 16 +++++++++++--- 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php diff --git a/lib/internal/Magento/Framework/Config/Dom.php b/lib/internal/Magento/Framework/Config/Dom.php index eacd5ba80525a..e24d89f1e625b 100644 --- a/lib/internal/Magento/Framework/Config/Dom.php +++ b/lib/internal/Magento/Framework/Config/Dom.php @@ -12,6 +12,7 @@ namespace Magento\Framework\Config; use Magento\Framework\Config\Dom\UrnResolver; +use Magento\Framework\Config\Dom\ValidationSchemaException; /** * Class Dom @@ -313,8 +314,9 @@ public static function validateDomDocument( $errors = self::getXmlErrors($errorFormat); } } catch (\Exception $exception) { + $errors = self::getXmlErrors($errorFormat); libxml_use_internal_errors(false); - throw $exception; + throw new ValidationSchemaException(implode("\n", $errors)); } libxml_set_external_entity_loader(null); libxml_use_internal_errors(false); diff --git a/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php b/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php new file mode 100644 index 0000000000000..832ace9963e02 --- /dev/null +++ b/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php @@ -0,0 +1,14 @@ +' . $layout . ''; - if ($this->appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) { - if (!$this->layoutValidator->isValid($layoutStr, Validator::LAYOUT_SCHEMA_MERGED, false)) { - $messages = $this->layoutValidator->getMessages(); - //Add first message to exception - $message = reset($messages); - $this->logger->info( - 'Cache file with merged layout: ' . $cacheId - . ' and handles ' . implode(', ', (array)$this->getHandles()) . ': ' . $message - ); + try { + $this->layoutValidator->isValid($layoutStr, Validator::LAYOUT_SCHEMA_MERGED, false); + } catch (\Exception $e) { + $messages = $this->layoutValidator->getMessages(); + //Add first message to exception + $message = reset($messages); + $this->logger->info( + 'Cache file with merged layout: ' . $cacheId + . ' and handles ' . implode(', ', (array)$this->getHandles()) . ': ' . $message + ); + if ($this->appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) { + throw $e; } } diff --git a/lib/internal/Magento/Framework/View/Model/Layout/Update/Validator.php b/lib/internal/Magento/Framework/View/Model/Layout/Update/Validator.php index b3bf3de34079d..97149cbb827a0 100644 --- a/lib/internal/Magento/Framework/View/Model/Layout/Update/Validator.php +++ b/lib/internal/Magento/Framework/View/Model/Layout/Update/Validator.php @@ -6,6 +6,7 @@ namespace Magento\Framework\View\Model\Layout\Update; use Magento\Framework\Config\Dom\UrnResolver; +use Magento\Framework\Config\Dom\ValidationSchemaException; /** * Validator for custom layout update @@ -16,6 +17,8 @@ class Validator extends \Zend_Validate_Abstract { const XML_INVALID = 'invalidXml'; + const XSD_INVALID = 'invalidXsd'; + const HELPER_ARGUMENT_TYPE = 'helperArgumentType'; const UPDATER_MODEL = 'updaterModel'; @@ -93,6 +96,9 @@ protected function _initMessageTemplates() self::XML_INVALID => (string)new \Magento\Framework\Phrase( 'Please correct the XML data and try again. %value%' ), + self::XSD_INVALID => (string)new \Magento\Framework\Phrase( + 'Please correct the XSD data and try again. %value%' + ), ]; } return $this; @@ -101,7 +107,7 @@ protected function _initMessageTemplates() /** * Returns true if and only if $value meets the validation requirements * - * If $value fails validation, then this method returns false, and + * If $value fails validation, then this method throws exception, and * getMessages() will return an array of messages that explain why the * validation failed. * @@ -109,6 +115,7 @@ protected function _initMessageTemplates() * @param string $schema * @param bool $isSecurityCheck * @return bool + * @throws \Exception */ public function isValid($value, $schema = self::LAYOUT_SCHEMA_PAGE_HANDLE, $isSecurityCheck = true) { @@ -132,10 +139,13 @@ public function isValid($value, $schema = self::LAYOUT_SCHEMA_PAGE_HANDLE, $isSe } } catch (\Magento\Framework\Config\Dom\ValidationException $e) { $this->_error(self::XML_INVALID, $e->getMessage()); - return false; + throw $e; + } catch (ValidationSchemaException $e) { + $this->_error(self::XSD_INVALID, $e->getMessage()); + throw $e; } catch (\Exception $e) { $this->_error(self::XML_INVALID); - return false; + throw $e; } return true; } From e23f01b54984a73277b29085f9ad493350bfca30 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Thu, 21 Jul 2016 19:51:18 +0300 Subject: [PATCH 03/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - ValidationSchemaException extends LocalizedException; --- lib/internal/Magento/Framework/Config/Dom.php | 2 +- .../Framework/Config/Dom/ValidationSchemaException.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Config/Dom.php b/lib/internal/Magento/Framework/Config/Dom.php index e24d89f1e625b..90e26230dc2ea 100644 --- a/lib/internal/Magento/Framework/Config/Dom.php +++ b/lib/internal/Magento/Framework/Config/Dom.php @@ -316,7 +316,7 @@ public static function validateDomDocument( } catch (\Exception $exception) { $errors = self::getXmlErrors($errorFormat); libxml_use_internal_errors(false); - throw new ValidationSchemaException(implode("\n", $errors)); + throw new ValidationSchemaException(__(implode("\n", $errors))); } libxml_set_external_entity_loader(null); libxml_use_internal_errors(false); diff --git a/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php b/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php index 832ace9963e02..555c57202afaf 100644 --- a/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php +++ b/lib/internal/Magento/Framework/Config/Dom/ValidationSchemaException.php @@ -5,10 +5,12 @@ */ /** - * \Exception that should be thrown by DOM model when incoming xsd is not valid. + * Exception that should be thrown by DOM model when incoming xsd is not valid. */ namespace Magento\Framework\Config\Dom; -class ValidationSchemaException extends \InvalidArgumentException +use Magento\Framework\Exception\LocalizedException; + +class ValidationSchemaException extends LocalizedException { } From 397fa26f775ebe93c742fe3e4937d68571abf4b5 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Fri, 22 Jul 2016 10:10:17 +0300 Subject: [PATCH 04/14] MAGETWO-55689: Stabilize branch and create PR --- lib/internal/Magento/Framework/Config/Dom.php | 3 ++- .../Magento/Framework/View/Layout/etc/layout_merged.xsd | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Config/Dom.php b/lib/internal/Magento/Framework/Config/Dom.php index 90e26230dc2ea..cbe3c9c6bcf35 100644 --- a/lib/internal/Magento/Framework/Config/Dom.php +++ b/lib/internal/Magento/Framework/Config/Dom.php @@ -316,7 +316,8 @@ public static function validateDomDocument( } catch (\Exception $exception) { $errors = self::getXmlErrors($errorFormat); libxml_use_internal_errors(false); - throw new ValidationSchemaException(__(implode("\n", $errors))); + array_unshift($errors, __('Processed schema file: %1', $schema)); + throw new ValidationSchemaException(__(implode("; ", $errors))); } libxml_set_external_entity_loader(null); libxml_use_internal_errors(false); diff --git a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd index 4b2abef7e836b..a316d01ce8226 100644 --- a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd +++ b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd @@ -44,12 +44,6 @@ - - - - - - From 77caa4a5d1f83d0187238b59630fa4d065ca6353 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Fri, 22 Jul 2016 10:56:26 +0300 Subject: [PATCH 05/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Used Magento\Framework\Phrase inside library; --- lib/internal/Magento/Framework/Config/Dom.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Config/Dom.php b/lib/internal/Magento/Framework/Config/Dom.php index cbe3c9c6bcf35..a5b92f9d57091 100644 --- a/lib/internal/Magento/Framework/Config/Dom.php +++ b/lib/internal/Magento/Framework/Config/Dom.php @@ -13,6 +13,7 @@ use Magento\Framework\Config\Dom\UrnResolver; use Magento\Framework\Config\Dom\ValidationSchemaException; +use Magento\Framework\Phrase; /** * Class Dom @@ -316,8 +317,8 @@ public static function validateDomDocument( } catch (\Exception $exception) { $errors = self::getXmlErrors($errorFormat); libxml_use_internal_errors(false); - array_unshift($errors, __('Processed schema file: %1', $schema)); - throw new ValidationSchemaException(__(implode("; ", $errors))); + array_unshift($errors, new Phrase('Processed schema file: %1', [$schema])); + throw new ValidationSchemaException(new Phrase(implode("\n", $errors))); } libxml_set_external_entity_loader(null); libxml_use_internal_errors(false); From e076a354343e339d0b1806fd40bd6ef563ffb1db Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Fri, 22 Jul 2016 17:26:29 +0300 Subject: [PATCH 06/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Added unit tests; --- .../Framework/Config/Test/Unit/DomTest.php | 36 +++++-- .../View/Test/Unit/Model/Layout/MergeTest.php | 95 +++++++++++++++++ .../Model/Layout/Update/ValidatorTest.php | 100 +++++++++++++----- 3 files changed, 193 insertions(+), 38 deletions(-) create mode 100644 lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DomTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DomTest.php index 411f84b64ac3a..9d204a2ebe1d0 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DomTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DomTest.php @@ -8,18 +8,14 @@ class DomTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Config\ValidationStateInterface + * @var \Magento\Framework\Config\ValidationStateInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $validationStateMock; protected function setUp() { - $this->validationStateMock = $this->getMock( - '\Magento\Framework\Config\ValidationStateInterface', - [], - [], - '', - false + $this->validationStateMock = $this->getMockForAbstractClass( + \Magento\Framework\Config\ValidationStateInterface::class ); $this->validationStateMock->method('isValidationRequired') ->willReturn(true); @@ -176,7 +172,29 @@ public function testValidateUnknownError() $domMock->expects($this->once()) ->method('schemaValidate') ->with($schemaFile) - ->will($this->returnValue(false)); - $this->assertEquals(['Unknown validation error'], $dom->validateDomDocument($domMock, $schemaFile)); + ->willReturn(false); + $this->assertEquals( + ["Element 'unknown_node': This element is not expected. Expected is ( node ).\nLine: 1\n"], + $dom->validateDomDocument($domMock, $schemaFile) + ); + } + + /** + * @expectedException \Magento\Framework\Config\Dom\ValidationSchemaException + */ + public function testValidateDomDocumentThrowsException() + { + if (!function_exists('libxml_set_external_entity_loader')) { + $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033'); + } + $xml = ''; + $schemaFile = __DIR__ . '/_files/sample.xsd'; + $dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock); + $domMock = $this->getMock('DOMDocument', ['schemaValidate'], []); + $domMock->expects($this->once()) + ->method('schemaValidate') + ->with($schemaFile) + ->willThrowException(new \Exception()); + $dom->validateDomDocument($domMock, $schemaFile); } } diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php new file mode 100644 index 0000000000000..7767fcccf1f2a --- /dev/null +++ b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php @@ -0,0 +1,95 @@ +objectManagerHelper = new ObjectManager($this); + + $this->scope = $this->getMockForAbstractClass(\Magento\Framework\Url\ScopeInterface::class); + $this->layoutValidator = $this->getMockBuilder(\Magento\Framework\View\Model\Layout\Update\Validator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class); + $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = $this->objectManagerHelper->getObject( + \Magento\Framework\View\Model\Layout\Merge::class, + [ + 'scope' => $this->scope, + 'layoutValidator' => $this->layoutValidator, + 'logger' => $this->logger, + 'appState' => $this->appState + ] + ); + } + + /** + * @expectedException \Magento\Framework\Config\Dom\ValidationSchemaException + * @expectedExceptionMessage Processed schema file is not valid. + */ + public function testValidateMergedLayoutThrowsException() + { + $messages = [ + 'Please correct the XSD data and try again.' + ]; + $this->scope->expects($this->once())->method('getId')->willReturn(1); + $this->layoutValidator->expects($this->once()) + ->method('isValid') + ->willThrowException( + new ValidationSchemaException( + new Phrase('Processed schema file is not valid.') + ) + ); + $this->layoutValidator->expects($this->once()) + ->method('getMessages') + ->willReturn($messages); + $this->appState->expects($this->once()) + ->method('getMode') + ->willReturn(State::MODE_DEVELOPER); + + $this->model->load(); + } +} diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php index 50a314426523f..cb91bb3ca1cc0 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\View\Test\Unit\Model\Layout\Update; +use Magento\Framework\Phrase; use \Magento\Framework\View\Model\Layout\Update\Validator; class ValidatorTest extends \PHPUnit_Framework_TestCase @@ -12,11 +13,37 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ - protected $_objectHelper; + private $_objectHelper; + + /** + * @var \Magento\Framework\Config\DomFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $domConfigFactory; + + /** + * @var \Magento\Framework\View\Model\Layout\Update\Validator|\PHPUnit_Framework_MockObject_MockObject + */ + private $model; + + /** + * @var \Magento\Framework\Config\Dom\UrnResolver|\PHPUnit_Framework_MockObject_MockObject + */ + private $urnResolver; protected function setUp() { $this->_objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->domConfigFactory = $this->getMockBuilder( + \Magento\Framework\Config\DomFactory::class + )->disableOriginalConstructor()->getMock(); + $this->urnResolver = $this->getMockBuilder( + \Magento\Framework\Config\Dom\UrnResolver::class + )->disableOriginalConstructor()->getMock(); + + $this->model = $this->_objectHelper->getObject( + \Magento\Framework\View\Model\Layout\Update\Validator::class, + ['domConfigFactory' => $this->domConfigFactory, 'urnResolver' => $this->urnResolver] + ); } /** @@ -26,36 +53,21 @@ protected function setUp() */ protected function _createValidator($layoutUpdate, $isSchemaValid = true) { - $domConfigFactory = $this->getMockBuilder( - 'Magento\Framework\Config\DomFactory' - )->disableOriginalConstructor()->getMock(); - - $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver(); $params = [ 'xml' => '' . trim($layoutUpdate) . '', - 'schemaFile' => $urnResolver->getRealPath('urn:magento:framework:View/Layout/etc/page_layout.xsd'), + 'schemaFile' => $this->urnResolver->getRealPath('urn:magento:framework:View/Layout/etc/page_layout.xsd'), ]; - $exceptionMessage = 'validation exception'; - $domConfigFactory->expects( + $this->domConfigFactory->expects( $this->once() )->method( 'createDom' )->with( $this->equalTo($params) - )->will( - $isSchemaValid ? $this->returnSelf() : $this->throwException( - new \Magento\Framework\Config\Dom\ValidationException($exceptionMessage) - ) - ); - $urnResolver = $this->_objectHelper->getObject('Magento\Framework\Config\Dom\UrnResolver'); - $model = $this->_objectHelper->getObject( - 'Magento\Framework\View\Model\Layout\Update\Validator', - ['domConfigFactory' => $domConfigFactory, 'urnResolver' => $urnResolver] - ); + )->willReturnSelf(); - return $model; + return $this->model; } /** @@ -85,15 +97,7 @@ public function testIsValidNotSecurityCheck($layoutUpdate, $isValid, $expectedRe public function testIsValidNotSecurityCheckDataProvider() { return [ - ['test', true, true, []], - [ - 'test', - false, - false, - [ - Validator::XML_INVALID => 'Please correct the XML data and try again. validation exception' - ] - ] + ['test', true, true, []] ]; } @@ -176,4 +180,42 @@ public function testIsValidSecurityCheckDataProvider() [$secureLayout, true, []] ]; } + + /** + * @expectedException \Magento\Framework\Config\Dom\ValidationException + * @expectedExceptionMessage Please correct the XML data and try again. + */ + public function testIsValidThrowsValidationException() + { + $this->domConfigFactory->expects($this->once())->method('createDom')->willThrowException( + new \Magento\Framework\Config\Dom\ValidationException('Please correct the XML data and try again.') + ); + $this->model->isValid('test'); + } + + /** + * @expectedException \Magento\Framework\Config\Dom\ValidationSchemaException + * @expectedExceptionMessage Please correct the XSD data and try again. + */ + public function testIsValidThrowsValidationSchemaException() + { + $this->domConfigFactory->expects($this->once())->method('createDom')->willThrowException( + new \Magento\Framework\Config\Dom\ValidationSchemaException( + new Phrase('Please correct the XSD data and try again.') + ) + ); + $this->model->isValid('test'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Exception. + */ + public function testIsValidThrowsException() + { + $this->domConfigFactory->expects($this->once())->method('createDom')->willThrowException( + new \Exception('Exception.') + ); + $this->model->isValid('test'); + } } From 9005d5831022b76e352a71be1295137115e4d206 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Fri, 22 Jul 2016 17:49:40 +0300 Subject: [PATCH 07/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Removed unused parameter; --- .../Test/Unit/Model/Layout/Update/ValidatorTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php index cb91bb3ca1cc0..d63ab14f29ee6 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/Update/ValidatorTest.php @@ -48,10 +48,9 @@ protected function setUp() /** * @param string $layoutUpdate - * @param boolean $isSchemaValid * @return Validator */ - protected function _createValidator($layoutUpdate, $isSchemaValid = true) + protected function _createValidator($layoutUpdate) { $params = [ 'xml' => '' . @@ -73,13 +72,12 @@ protected function _createValidator($layoutUpdate, $isSchemaValid = true) /** * @dataProvider testIsValidNotSecurityCheckDataProvider * @param string $layoutUpdate - * @param boolean $isValid * @param boolean $expectedResult * @param array $messages */ - public function testIsValidNotSecurityCheck($layoutUpdate, $isValid, $expectedResult, $messages) + public function testIsValidNotSecurityCheck($layoutUpdate, $expectedResult, $messages) { - $model = $this->_createValidator($layoutUpdate, $isValid); + $model = $this->_createValidator($layoutUpdate); $this->assertEquals( $expectedResult, $model->isValid( @@ -97,7 +95,7 @@ public function testIsValidNotSecurityCheck($layoutUpdate, $isValid, $expectedRe public function testIsValidNotSecurityCheckDataProvider() { return [ - ['test', true, true, []] + ['test', true, []] ]; } From 237e54d79c70d8954f16556a3a8195a8b7b6d942 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 25 Jul 2016 12:46:54 +0300 Subject: [PATCH 08/14] MAGETWO-55684: Fix XSD schema --- .../View/Layout/etc/layout_merged.xsd | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd index a316d01ce8226..d6faf66fdc094 100644 --- a/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd +++ b/lib/internal/Magento/Framework/View/Layout/etc/layout_merged.xsd @@ -7,6 +7,8 @@ --> + + @@ -25,24 +27,7 @@ - - - - - - - - - - - - - - - - - - + @@ -50,13 +35,17 @@ Layout Type definition - + - + + + + + From e5e46be1fd26c54821ebf908c1d28353d4e3750e Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Mon, 25 Jul 2016 19:10:30 +0300 Subject: [PATCH 09/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Integration test updated; --- .../Magento/Framework/View/Layout/MergeTest.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php index a3075cc158312..565b582b58272 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\View\Layout; +use Magento\Framework\App\State; use Magento\Framework\Phrase; class MergeTest extends \PHPUnit_Framework_TestCase @@ -393,17 +394,23 @@ public function testLoadWithInvalidArgumentThrowsException() /** * Test loading invalid layout + * + * @expectedException \Exception + * @expectedExceptionMessage Layout is invalid. */ public function testLoadWithInvalidLayout() { $this->_model->addPageHandles(['default']); - $this->_appState->expects($this->any())->method('getMode')->will($this->returnValue('developer')); + $this->_appState->expects($this->once())->method('getMode')->willReturn(State::MODE_DEVELOPER); - $this->_layoutValidator->expects($this->any())->method('getMessages') - ->will($this->returnValue(['testMessage1', 'testMessage2'])); + $this->_layoutValidator->expects($this->once()) + ->method('getMessages') + ->willReturn(['testMessage1', 'testMessage2']); - $this->_layoutValidator->expects($this->any())->method('isValid')->will($this->returnValue(false)); + $this->_layoutValidator->expects($this->any()) + ->method('isValid') + ->willThrowException(new \Exception('Layout is invalid.')); $suffix = md5(implode('|', $this->_model->getHandles())); $cacheId = "LAYOUT_{$this->_theme->getArea()}_STORE{$this->scope->getId()}_{$this->_theme->getId()}{$suffix}"; From 394df7eec3edb28879a67d2006669adb6bde5c20 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Tue, 26 Jul 2016 10:28:41 +0300 Subject: [PATCH 10/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Test LoadWithInvalidLayout updated; --- .../testsuite/Magento/Framework/View/Layout/MergeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php index 565b582b58272..70cf0516390f2 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php @@ -404,7 +404,7 @@ public function testLoadWithInvalidLayout() $this->_appState->expects($this->once())->method('getMode')->willReturn(State::MODE_DEVELOPER); - $this->_layoutValidator->expects($this->once()) + $this->_layoutValidator->expects($this->any()) ->method('getMessages') ->willReturn(['testMessage1', 'testMessage2']); From 2f69d44d1b5b4ffce49d1659389b98e63699a6f6 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Tue, 26 Jul 2016 13:51:27 +0300 Subject: [PATCH 11/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Method addUpdate updated; --- lib/internal/Magento/Framework/View/Model/Layout/Merge.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php index 0d79b12b5687c..b4215abb096d9 100644 --- a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php +++ b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php @@ -201,7 +201,9 @@ public function __construct( */ public function addUpdate($update) { - $this->updates[] = $update; + if (!in_array($update, $this->updates)) { + $this->updates[] = $update; + } return $this; } From 34dcda0b9b36758fdd3377de435d69684aaa9fdd Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Tue, 26 Jul 2016 18:01:50 +0300 Subject: [PATCH 12/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Throw ValidationException if layout update file is not valid; --- .../Magento/Framework/View/Layout/MergeTest.php | 10 ++++++++++ .../Magento/Framework/View/Model/Layout/Merge.php | 11 +++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php index 70cf0516390f2..8e936ee251b6e 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php @@ -424,4 +424,14 @@ public function testLoadWithInvalidLayout() $this->_model->load(); } + + /** + * @expectedException \Magento\Framework\Config\Dom\ValidationException + */ + public function testLayoutUpdateFileIsNotValid() + { + $this->_appState->expects($this->once())->method('getMode')->willReturn(State::MODE_DEVELOPER); + + $this->_model->addPageHandles(['default']); + } } diff --git a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php index b4215abb096d9..88690ae20a671 100644 --- a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php +++ b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php @@ -5,9 +5,11 @@ */ namespace Magento\Framework\View\Model\Layout; +use Magento\Framework\App\State; use Magento\Framework\Filesystem\DriverPool; use Magento\Framework\Filesystem\File\ReadFactory; use Magento\Framework\View\Model\Layout\Update\Validator; +use Magento\Framework\Config\Dom\ValidationException; /** * Layout merge model @@ -737,10 +739,11 @@ protected function _logXmlErrors($fileName, $libXmlErrors) foreach ($libXmlErrors as $error) { $errors[] = "{$error->message} Line: {$error->line}"; } - - $this->logger->info( - sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $errors)) - ); + $error = sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $errors)); + $this->logger->info($error); + if ($this->appState->getMode() === State::MODE_DEVELOPER) { + throw new ValidationException($error); + } } } From a5cccc3a0726946fd894f0605583ec84669c2a13 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Thu, 28 Jul 2016 12:48:02 +0300 Subject: [PATCH 13/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Added getXmlErrors method; --- .../Framework/View/Layout/MergeTest.php | 1 + .../Framework/View/Model/Layout/Merge.php | 37 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php index 8e936ee251b6e..d8a0b349b017b 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php @@ -427,6 +427,7 @@ public function testLoadWithInvalidLayout() /** * @expectedException \Magento\Framework\Config\Dom\ValidationException + * @expectedExceptionMessageRegExp /_mergeFiles\/layout\/file_wrong\.xml\' is not valid/ */ public function testLayoutUpdateFileIsNotValid() { diff --git a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php index 88690ae20a671..e15ec76f710d2 100644 --- a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php +++ b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php @@ -701,7 +701,19 @@ protected function _loadFileLayoutUpdatesXml() /** @var $fileXml \Magento\Framework\View\Layout\Element */ $fileXml = $this->_loadXmlString($fileStr); if (!$fileXml instanceof \Magento\Framework\View\Layout\Element) { - $this->_logXmlErrors($file->getFilename(), libxml_get_errors()); + $xmlErrors = $this->getXmlErrors(libxml_get_errors()); + $this->logXmlErrors($file->getFilename(), $xmlErrors); + if ($this->appState->getMode() === State::MODE_DEVELOPER) { + throw new ValidationException( + new \Magento\Framework\Phrase( + "Theme layout update file '%1' is not valid.\n%2", + [ + $file->getFilename(), + implode("\n", $xmlErrors) + ] + ) + ); + } libxml_clear_errors(); continue; } @@ -729,22 +741,31 @@ protected function _loadFileLayoutUpdatesXml() * Log xml errors to system log * * @param string $fileName - * @param array $libXmlErrors + * @param array $xmlErrors * @return void */ - protected function _logXmlErrors($fileName, $libXmlErrors) + private function logXmlErrors($fileName, $xmlErrors) + { + $this->logger->info( + sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $xmlErrors)) + ); + } + + /** + * Get formatted xml errors + * + * @param array $libXmlErrors + * @return array + */ + private function getXmlErrors($libXmlErrors) { $errors = []; if (count($libXmlErrors)) { foreach ($libXmlErrors as $error) { $errors[] = "{$error->message} Line: {$error->line}"; } - $error = sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $errors)); - $this->logger->info($error); - if ($this->appState->getMode() === State::MODE_DEVELOPER) { - throw new ValidationException($error); - } } + return $errors; } /** From 0bd47f0a86a90a558a3a243cb97a3ca9e1c962bd Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Thu, 28 Jul 2016 13:46:17 +0300 Subject: [PATCH 14/14] MAGETWO-55685: Introduce new ValidationSchemaException and implement processing this exception - Fixed backward incompatible changes; --- lib/internal/Magento/Framework/View/Model/Layout/Merge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php index e15ec76f710d2..7c833acdc050e 100644 --- a/lib/internal/Magento/Framework/View/Model/Layout/Merge.php +++ b/lib/internal/Magento/Framework/View/Model/Layout/Merge.php @@ -702,7 +702,7 @@ protected function _loadFileLayoutUpdatesXml() $fileXml = $this->_loadXmlString($fileStr); if (!$fileXml instanceof \Magento\Framework\View\Layout\Element) { $xmlErrors = $this->getXmlErrors(libxml_get_errors()); - $this->logXmlErrors($file->getFilename(), $xmlErrors); + $this->_logXmlErrors($file->getFilename(), $xmlErrors); if ($this->appState->getMode() === State::MODE_DEVELOPER) { throw new ValidationException( new \Magento\Framework\Phrase( @@ -744,7 +744,7 @@ protected function _loadFileLayoutUpdatesXml() * @param array $xmlErrors * @return void */ - private function logXmlErrors($fileName, $xmlErrors) + protected function _logXmlErrors($fileName, $xmlErrors) { $this->logger->info( sprintf("Theme layout update file '%s' is not valid.\n%s", $fileName, implode("\n", $xmlErrors))