HandlesTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Test format of layout files
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Test\Integrity\Layout;
  9. class HandlesTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  13. */
  14. public function testHandleDeclarations()
  15. {
  16. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  17. $invoker(
  18. /**
  19. * Test dependencies between handle attributes that is out of coverage by XSD
  20. *
  21. * @param string $layoutFile
  22. */
  23. function ($layoutFile) {
  24. $issues = [];
  25. $node = simplexml_load_file($layoutFile);
  26. $label = $node['label'];
  27. $designAbstraction = $node['design_abstraction'];
  28. if (!$label) {
  29. if ($designAbstraction) {
  30. $issues[] = 'Attribute "design_abstraction" is defined, but "label" is not';
  31. }
  32. }
  33. if ($issues) {
  34. $this->fail("Issues found in handle declaration:\n" . implode("\n", $issues) . "\n");
  35. }
  36. },
  37. \Magento\Framework\App\Utility\Files::init()->getLayoutFiles()
  38. );
  39. }
  40. public function testContainerDeclarations()
  41. {
  42. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  43. $invoker(
  44. /**
  45. * Test dependencies between container attributes that is out of coverage by XSD
  46. *
  47. * @param string $layoutFile
  48. */
  49. function ($layoutFile) {
  50. $issues = [];
  51. $xml = simplexml_load_file($layoutFile);
  52. $containers = $xml->xpath('/layout//container') ?: [];
  53. /** @var \SimpleXMLElement $node */
  54. foreach ($containers as $node) {
  55. if (!isset($node['htmlTag']) && (isset($node['htmlId']) || isset($node['htmlClass']))) {
  56. $issues[] = $node->asXML();
  57. }
  58. }
  59. if ($issues) {
  60. $this->fail(
  61. 'The following containers declare attribute "htmlId" and/or "htmlClass", but not "htmlTag":' .
  62. "\n" .
  63. implode(
  64. "\n",
  65. $issues
  66. ) . "\n"
  67. );
  68. }
  69. },
  70. \Magento\Framework\App\Utility\Files::init()->getLayoutFiles()
  71. );
  72. }
  73. public function testHeadBlockUsage()
  74. {
  75. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  76. $invoker(
  77. /**
  78. * Test validate that head block doesn't exist in layout
  79. *
  80. * @param string $layoutFile
  81. */
  82. function ($layoutFile) {
  83. $dom = new \DOMDocument();
  84. $dom->load($layoutFile);
  85. $xpath = new \DOMXpath($dom);
  86. if ($xpath->query("//*[@name='head']")->length) {
  87. $this->fail('Following file contains deprecated head block. File Path:' . "\n" . $layoutFile);
  88. }
  89. },
  90. \Magento\Framework\App\Utility\Files::init()->getLayoutFiles()
  91. );
  92. }
  93. }