Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutFilesTest.php
Go to the documentation of this file.
1 <?php
7 
11 class LayoutFilesTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_argParser;
17 
21  protected $_argInterpreter;
22 
23  protected function setUp()
24  {
26  $this->_argParser = $objectManager->get(\Magento\Framework\View\Layout\Argument\Parser::class);
27  $this->_argInterpreter = $objectManager->get('layoutArgumentGeneratorInterpreter');
28  }
29 
35  public function testLayoutArguments($area, $layoutFile)
36  {
38  $dom = new \DOMDocument();
39  $dom->load($layoutFile);
40  $xpath = new \DOMXPath($dom);
41  $argumentNodes = $xpath->query('/layout//arguments/argument | /layout//action/argument');
43  foreach ($argumentNodes as $argumentNode) {
44  try {
45  $argumentData = $this->_argParser->parse($argumentNode);
46  if ($this->isSkippedArgument($argumentData)) {
47  continue;
48  }
49  $this->_argInterpreter->evaluate($argumentData);
50  } catch (\Exception $e) {
51  $this->fail($e->getMessage());
52  }
53  }
54  }
55 
59  public function layoutArgumentsDataProvider()
60  {
61  $areas = ['adminhtml', 'frontend', 'email'];
62  $data = [];
63  foreach ($areas as $area) {
64  $layoutFiles = \Magento\Framework\App\Utility\Files::init()->getLayoutFiles(['area' => $area], false);
65  foreach ($layoutFiles as $layoutFile) {
66  $data[substr($layoutFile, strlen(BP))] = [$area, $layoutFile];
67  }
68  }
69  return $data;
70  }
71 
78  protected function isSkippedArgument(array $argumentData)
79  {
80  // Do not take into account argument name and parameters
81  unset($argumentData['name']);
82  unset($argumentData['param']);
83 
84  $isUpdater = isset($argumentData['updater']);
85  unset($argumentData['updater']);
86 
87  // Arguments, evaluation of which causes a run-time error, because of unsafe assumptions to the environment
89  $prCollection =
90  \Magento\GroupedProduct\Model\ResourceModel\Product\Type\Grouped\AssociatedProductsCollection::class;
91  $ignoredArguments = [
92  [
93  $typeAttr => 'object',
94  'value' => $prCollection,
95  ],
96  [$typeAttr => 'object', 'value' => \Magento\Wishlist\Model\ResourceModel\Item\Collection\Grid::class],
97  [
98  $typeAttr => 'object',
99  'value' => \Magento\CustomerSegment\Model\ResourceModel\Segment\Report\Detail\Collection::class
100  ],
101  [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\ActionsGroup::class],
102  [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\Actions::class],
103  ];
104  $isIgnoredArgument = in_array($argumentData, $ignoredArguments, true);
105 
106  unset($argumentData[$typeAttr]);
107  $hasValue = !empty($argumentData);
108 
109  return $isIgnoredArgument || $isUpdater && !$hasValue;
110  }
111 }
$objectManager
Definition: bootstrap.php:17
const BP
Definition: autoload.php:14