LayoutFilesTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity\Modular;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class LayoutFilesTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Layout\Argument\Parser
  14. */
  15. protected $_argParser;
  16. /**
  17. * @var \Magento\Framework\Data\Argument\InterpreterInterface
  18. */
  19. protected $_argInterpreter;
  20. protected function setUp()
  21. {
  22. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  23. $this->_argParser = $objectManager->get(\Magento\Framework\View\Layout\Argument\Parser::class);
  24. $this->_argInterpreter = $objectManager->get('layoutArgumentGeneratorInterpreter');
  25. }
  26. /**
  27. * @param string $area
  28. * @param string $layoutFile
  29. * @dataProvider layoutArgumentsDataProvider
  30. */
  31. public function testLayoutArguments($area, $layoutFile)
  32. {
  33. \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($area);
  34. $dom = new \DOMDocument();
  35. $dom->load($layoutFile);
  36. $xpath = new \DOMXPath($dom);
  37. $argumentNodes = $xpath->query('/layout//arguments/argument | /layout//action/argument');
  38. /** @var \DOMNode $argumentNode */
  39. foreach ($argumentNodes as $argumentNode) {
  40. try {
  41. $argumentData = $this->_argParser->parse($argumentNode);
  42. if ($this->isSkippedArgument($argumentData)) {
  43. continue;
  44. }
  45. $this->_argInterpreter->evaluate($argumentData);
  46. } catch (\Exception $e) {
  47. $this->fail($e->getMessage());
  48. }
  49. }
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function layoutArgumentsDataProvider()
  55. {
  56. $areas = ['adminhtml', 'frontend', 'email'];
  57. $data = [];
  58. foreach ($areas as $area) {
  59. $layoutFiles = \Magento\Framework\App\Utility\Files::init()->getLayoutFiles(['area' => $area], false);
  60. foreach ($layoutFiles as $layoutFile) {
  61. $data[substr($layoutFile, strlen(BP))] = [$area, $layoutFile];
  62. }
  63. }
  64. return $data;
  65. }
  66. /**
  67. * Whether an argument should be skipped, because it cannot be evaluated in the testing environment
  68. *
  69. * @param array $argumentData
  70. * @return bool
  71. */
  72. protected function isSkippedArgument(array $argumentData)
  73. {
  74. // Do not take into account argument name and parameters
  75. unset($argumentData['name']);
  76. unset($argumentData['param']);
  77. $isUpdater = isset($argumentData['updater']);
  78. unset($argumentData['updater']);
  79. // Arguments, evaluation of which causes a run-time error, because of unsafe assumptions to the environment
  80. $typeAttr = \Magento\Framework\View\Model\Layout\Merge::TYPE_ATTRIBUTE;
  81. $prCollection =
  82. \Magento\GroupedProduct\Model\ResourceModel\Product\Type\Grouped\AssociatedProductsCollection::class;
  83. $ignoredArguments = [
  84. [
  85. $typeAttr => 'object',
  86. 'value' => $prCollection,
  87. ],
  88. [$typeAttr => 'object', 'value' => \Magento\Wishlist\Model\ResourceModel\Item\Collection\Grid::class],
  89. [
  90. $typeAttr => 'object',
  91. 'value' => \Magento\CustomerSegment\Model\ResourceModel\Segment\Report\Detail\Collection::class
  92. ],
  93. [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\ActionsGroup::class],
  94. [$typeAttr => 'options', 'model' => \Magento\Logging\Model\ResourceModel\Grid\Actions::class],
  95. ];
  96. $isIgnoredArgument = in_array($argumentData, $ignoredArguments, true);
  97. unset($argumentData[$typeAttr]);
  98. $hasValue = !empty($argumentData);
  99. return $isIgnoredArgument || $isUpdater && !$hasValue;
  100. }
  101. }