123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Test\Integrity\Modular;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LayoutFilesTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\View\Layout\Argument\Parser
- */
- protected $_argParser;
- /**
- * @var \Magento\Framework\Data\Argument\InterpreterInterface
- */
- protected $_argInterpreter;
- protected function setUp()
- {
- $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- $this->_argParser = $objectManager->get(\Magento\Framework\View\Layout\Argument\Parser::class);
- $this->_argInterpreter = $objectManager->get('layoutArgumentGeneratorInterpreter');
- }
- /**
- * @param string $area
- * @param string $layoutFile
- * @dataProvider layoutArgumentsDataProvider
- */
- public function testLayoutArguments($area, $layoutFile)
- {
- \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($area);
- $dom = new \DOMDocument();
- $dom->load($layoutFile);
- $xpath = new \DOMXPath($dom);
- $argumentNodes = $xpath->query('/layout//arguments/argument | /layout//action/argument');
- /** @var \DOMNode $argumentNode */
- foreach ($argumentNodes as $argumentNode) {
- try {
- $argumentData = $this->_argParser->parse($argumentNode);
- if ($this->isSkippedArgument($argumentData)) {
- continue;
- }
- $this->_argInterpreter->evaluate($argumentData);
- } catch (\Exception $e) {
- $this->fail($e->getMessage());
- }
- }
- }
- /**
- * @return array
- */
- public function layoutArgumentsDataProvider()
- {
- $areas = ['adminhtml', 'frontend', 'email'];
- $data = [];
- foreach ($areas as $area) {
- $layoutFiles = \Magento\Framework\App\Utility\Files::init()->getLayoutFiles(['area' => $area], false);
- foreach ($layoutFiles as $layoutFile) {
- $data[substr($layoutFile, strlen(BP))] = [$area, $layoutFile];
- }
- }
- return $data;
- }
- /**
- * Whether an argument should be skipped, because it cannot be evaluated in the testing environment
- *
- * @param array $argumentData
- * @return bool
- */
- protected function isSkippedArgument(array $argumentData)
- {
- // Do not take into account argument name and parameters
- unset($argumentData['name']);
- unset($argumentData['param']);
- $isUpdater = isset($argumentData['updater']);
- unset($argumentData['updater']);
- // Arguments, evaluation of which causes a run-time error, because of unsafe assumptions to the environment
- $typeAttr = \Magento\Framework\View\Model\Layout\Merge::TYPE_ATTRIBUTE;
- $prCollection =
- \Magento\GroupedProduct\Model\ResourceModel\Product\Type\Grouped\AssociatedProductsCollection::class;
- $ignoredArguments = [
- [
- $typeAttr => 'object',
- 'value' => $prCollection,
- ],
- [$typeAttr => 'object', 'value' => \Magento\Wishlist\Model\ResourceModel\Item\Collection\Grid::class],
- [
- $typeAttr => 'object',
- 'value' => \Magento\CustomerSegment\Model\ResourceModel\Segment\Report\Detail\Collection::class
- ],
- [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\ActionsGroup::class],
- [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\Actions::class],
- ];
- $isIgnoredArgument = in_array($argumentData, $ignoredArguments, true);
- unset($argumentData[$typeAttr]);
- $hasValue = !empty($argumentData);
- return $isIgnoredArgument || $isUpdater && !$hasValue;
- }
- }
|