PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/static/testsuite/Magento/Test/Integrity/Xml/SchemaTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 129 lines | 105 code | 12 blank | 12 comment | 3 complexity | 1e11b4b9d595bf5eb70693d0bf7733fd MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity\Xml;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. class SchemaTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function testXmlFiles()
  11. {
  12. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  13. $invoker(
  14. /**
  15. * @param string $filename
  16. */
  17. function ($filename) {
  18. $dom = new \DOMDocument();
  19. $xmlFile = file_get_contents($filename);
  20. $dom->loadXML($xmlFile);
  21. $errors = libxml_get_errors();
  22. libxml_clear_errors();
  23. $this->assertEmpty($errors, print_r($errors, true));
  24. $schemaLocations = [];
  25. preg_match('/xsi:noNamespaceSchemaLocation=\s*"(urn:[^"]+)"/s', $xmlFile, $schemaLocations);
  26. $this->assertEquals(
  27. 2,
  28. count($schemaLocations),
  29. 'The XML file at ' . $filename . ' does not have a schema properly defined. It should '
  30. . 'have a xsi:noNamespaceSchemaLocation attribute defined with a URN path. E.g. '
  31. . 'xsi:noNamespaceSchemaLocation="urn:magento:framework:Relative_Path/something.xsd"'
  32. );
  33. try {
  34. $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaLocations[1]);
  35. } catch (\Exception $exception) {
  36. $errors = [$exception->__toString()];
  37. }
  38. $this->assertEmpty(
  39. $errors,
  40. "Error validating $filename against {$schemaLocations[1]}\n" . print_r($errors, true)
  41. );
  42. },
  43. $this->getXmlFiles()
  44. );
  45. }
  46. public function getSchemas()
  47. {
  48. $componentRegistrar = new ComponentRegistrar();
  49. $codeSchemas = [];
  50. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
  51. $codeSchemas = array_merge($codeSchemas, $this->_getFiles($modulePath, '*.xsd'));
  52. }
  53. $libSchemas = [];
  54. foreach ($componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryPath) {
  55. $libSchemas = array_merge($libSchemas, $this->_getFiles($libraryPath, '*.xsd'));
  56. }
  57. return $this->_dataSet(array_merge($codeSchemas, $libSchemas));
  58. }
  59. public function getXmlFiles()
  60. {
  61. $componentRegistrar = new ComponentRegistrar();
  62. $codeXml = [];
  63. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
  64. $codeXml = array_merge($codeXml, $this->_getFiles($modulePath, '*.xml', '/.\/Test\/Unit\/./'));
  65. }
  66. $this->_filterSpecialCases($codeXml);
  67. $designXml = [];
  68. foreach ($componentRegistrar->getPaths(ComponentRegistrar::THEME) as $themePath) {
  69. $designXml = array_merge($designXml, $this->_getFiles($themePath, '*.xml'));
  70. }
  71. $libXml = [];
  72. foreach ($componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryPath) {
  73. $libXml = array_merge($libXml, $this->_getFiles($libraryPath, '*.xml', '/.\/Test\/./'));
  74. }
  75. return $this->_dataSet(array_merge($codeXml, $designXml, $libXml));
  76. }
  77. protected function _getFiles($dir, $pattern, $skipDirPattern = '')
  78. {
  79. $files = glob($dir . '/' . $pattern, GLOB_NOSORT);
  80. if (empty($skipDirPattern) || !preg_match($skipDirPattern, $dir)) {
  81. foreach (glob($dir . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $newDir) {
  82. $files = array_merge($files, $this->_getFiles($newDir, $pattern, $skipDirPattern));
  83. }
  84. }
  85. return $files;
  86. }
  87. /**
  88. * Files that are exempt from validation
  89. *
  90. * @param array &$files
  91. */
  92. private function _filterSpecialCases(&$files)
  93. {
  94. $list = [
  95. '#etc/countries.xml$#',
  96. '#conf/schema.xml$#',
  97. '#conf/solrconfig.xml$#',
  98. '#layout/swagger_index_index.xml$#',
  99. '#Doc/etc/doc/vars.xml$#'
  100. ];
  101. foreach ($list as $pattern) {
  102. foreach ($files as $key => $value) {
  103. if (preg_match($pattern, $value)) {
  104. unset($files[$key]);
  105. }
  106. }
  107. }
  108. }
  109. protected function _dataSet($files)
  110. {
  111. $data = [];
  112. foreach ($files as $file) {
  113. $data[substr($file, strlen(BP))] = [$file];
  114. }
  115. return $data;
  116. }
  117. }