XsdTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Config;
  7. class XsdTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $_xsdFile;
  13. protected function setUp()
  14. {
  15. if (!function_exists('libxml_set_external_entity_loader')) {
  16. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  17. }
  18. $this->_xsdFile = "urn:magento:module:Magento_Sales:etc/sales.xsd";
  19. }
  20. /**
  21. * @param string $xmlFile
  22. * @dataProvider validXmlFileDataProvider
  23. */
  24. public function testValidXmlFile($xmlFile)
  25. {
  26. $dom = new \DOMDocument();
  27. $dom->load(__DIR__ . "/_files/{$xmlFile}");
  28. libxml_use_internal_errors(true);
  29. $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $this->_xsdFile);
  30. libxml_use_internal_errors(false);
  31. $this->assertEmpty($result, 'Validation failed with errors: ' . join(', ', $result));
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function validXmlFileDataProvider()
  37. {
  38. return [['sales_valid.xml']];
  39. }
  40. /**
  41. * @param string $xmlFile
  42. * @param array $expectedErrors
  43. * @dataProvider invalidXmlFileDataProvider
  44. */
  45. public function testInvalidXmlFile($xmlFile, $expectedErrors)
  46. {
  47. $dom = new \DOMDocument();
  48. $dom->load(__DIR__ . "/_files/{$xmlFile}");
  49. libxml_use_internal_errors(true);
  50. $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $this->_xsdFile);
  51. libxml_use_internal_errors(false);
  52. $this->assertEquals($expectedErrors, $result);
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function invalidXmlFileDataProvider()
  58. {
  59. return [
  60. [
  61. 'sales_invalid.xml',
  62. [
  63. "Element 'section', attribute 'wrongName': The attribute 'wrongName' is not allowed.\nLine: 9\n",
  64. "Element 'section': The attribute 'name' is required but missing.\nLine: 9\n",
  65. "Element 'wrongGroup': This element is not expected. Expected is ( group ).\nLine: 10\n"
  66. ],
  67. ],
  68. [
  69. 'sales_invalid_duplicates.xml',
  70. [
  71. "Element 'renderer': Duplicate key-sequence ['r1']" .
  72. " in unique identity-constraint 'uniqueRendererName'.\nLine: 13\n",
  73. "Element 'item': Duplicate key-sequence ['i1']" .
  74. " in unique identity-constraint 'uniqueItemName'.\nLine: 15\n",
  75. "Element 'group': Duplicate key-sequence ['g1']" .
  76. " in unique identity-constraint 'uniqueGroupName'.\nLine: 17\n",
  77. "Element 'section': Duplicate key-sequence ['s1']" .
  78. " in unique identity-constraint 'uniqueSectionName'.\nLine: 21\n",
  79. "Element 'available_product_type': Duplicate key-sequence ['a1']" .
  80. " in unique identity-constraint 'uniqueProductTypeName'.\nLine: 28\n"
  81. ]
  82. ],
  83. [
  84. 'sales_invalid_without_attributes.xml',
  85. [
  86. "Element 'section': The attribute 'name' is required but missing.\nLine: 9\n",
  87. "Element 'group': The attribute 'name' is required but missing.\nLine: 10\n",
  88. "Element 'item': The attribute 'name' is required but missing.\nLine: 11\n",
  89. "Element 'renderer': The attribute 'name' is required but missing.\nLine: 12\n",
  90. "Element 'renderer': The attribute 'instance' is required but missing.\nLine: 12\n",
  91. "Element 'available_product_type': The attribute 'name' is required but missing.\nLine: 17\n"
  92. ]
  93. ],
  94. [
  95. 'sales_invalid_root_node.xml',
  96. ["Element 'wrong': This element is not expected. Expected is one of ( section, order ).\nLine: 9\n"]
  97. ]
  98. ];
  99. }
  100. }