XsdTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Test for validation rules implemented by XSD schemas for email templates configuration
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Email\Test\Unit\Model\Template\Config;
  9. class XsdTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Test validation rules implemented by XSD schema for merged configs
  13. *
  14. * @param string $fixtureXml
  15. * @param array $expectedErrors
  16. * @dataProvider mergedXmlDataProvider
  17. */
  18. public function testMergedXml($fixtureXml, array $expectedErrors)
  19. {
  20. if (!function_exists('libxml_set_external_entity_loader')) {
  21. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  22. }
  23. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  24. $schemaFile = $urnResolver->getRealPath('urn:magento:module:Magento_Email:etc/email_templates.xsd');
  25. $this->_testXmlAgainstXsd($fixtureXml, $schemaFile, $expectedErrors);
  26. }
  27. /**
  28. * @return array
  29. */
  30. public function mergedXmlDataProvider()
  31. {
  32. // @codingStandardsIgnoreStart
  33. return [
  34. 'valid' => [
  35. '<config><template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend"/></config>',
  36. [],
  37. ],
  38. 'empty root node' => [
  39. '<config/>',
  40. ["Element 'config': Missing child element(s). Expected is ( template )."],
  41. ],
  42. 'irrelevant root node' => [
  43. '<template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend"/>',
  44. ["Element 'template': No matching global declaration available for the validation root."],
  45. ],
  46. 'invalid node' => [
  47. '<config><invalid/></config>',
  48. ["Element 'invalid': This element is not expected. Expected is ( template )."],
  49. ],
  50. 'node "template" with value' => [
  51. '<config>
  52. <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend">invalid</template>
  53. </config>',
  54. ["Element 'template': Character content is not allowed, because the content type is empty."],
  55. ],
  56. 'node "template" with children' => [
  57. '<config>
  58. <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend"><invalid/></template>
  59. </config>',
  60. ["Element 'template': Element content is not allowed, because the content type is empty."],
  61. ],
  62. 'node "template" without attribute "id"' => [
  63. '<config><template label="Test" file="test.txt" type="text" module="Module" area="frontend"/></config>',
  64. ["Element 'template': The attribute 'id' is required but missing."],
  65. ],
  66. 'node "template" without attribute "label"' => [
  67. '<config><template id="test" file="test.txt" type="text" module="Module" area="frontend"/></config>',
  68. ["Element 'template': The attribute 'label' is required but missing."],
  69. ],
  70. 'node "template" without attribute "file"' => [
  71. '<config><template id="test" label="Test" type="text" module="Module" area="frontend"/></config>',
  72. ["Element 'template': The attribute 'file' is required but missing."],
  73. ],
  74. 'node "template" without attribute "type"' => [
  75. '<config><template id="test" label="Test" file="test.txt" module="Module" area="frontend"/></config>',
  76. ["Element 'template': The attribute 'type' is required but missing."],
  77. ],
  78. 'node "template" with invalid attribute "type"' => [
  79. '<config><template id="test" label="Test" file="test.txt" type="invalid" module="Module" area="frontend"/></config>',
  80. [
  81. "Element 'template', attribute 'type': " .
  82. "[facet 'enumeration'] The value 'invalid' is not an element of the set {'html', 'text'}.",
  83. "Element 'template', attribute 'type': " .
  84. "'invalid' is not a valid value of the atomic type 'emailTemplateFormatType'."
  85. ],
  86. ],
  87. 'node "template" without attribute "area"' => [
  88. '<config><template id="test" label="Test" file="test.txt" type="text" module="Module"/></config>',
  89. ["Element 'template': The attribute 'area' is required but missing."],
  90. ],
  91. 'node "template" with invalid attribute "area"' => [
  92. '<config><template id="test" label="Test" file="test.txt" type="text" module="Module" area="invalid"/></config>',
  93. [
  94. "Element 'template', attribute 'area': " .
  95. "[facet 'enumeration'] The value 'invalid' is not an element of the set {'frontend', 'adminhtml'}.",
  96. "Element 'template', attribute 'area': " .
  97. "'invalid' is not a valid value of the atomic type 'areaType'."
  98. ],
  99. ],
  100. 'node "template" with unknown attribute' => [
  101. '<config>
  102. <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend" unknown="true"/>
  103. </config>',
  104. ["Element 'template', attribute 'unknown': The attribute 'unknown' is not allowed."],
  105. ]
  106. ];
  107. // @codingStandardsIgnoreEnd
  108. }
  109. /**
  110. * Test that XSD schema validates fixture XML contents producing expected results
  111. *
  112. * @param string $fixtureXml
  113. * @param string $schemaFile
  114. * @param array $expectedErrors
  115. */
  116. protected function _testXmlAgainstXsd($fixtureXml, $schemaFile, array $expectedErrors)
  117. {
  118. $validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  119. $validationStateMock->method('isValidationRequired')
  120. ->willReturn(true);
  121. $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, '%message%');
  122. $actualResult = $dom->validate($schemaFile, $actualErrors);
  123. $this->assertEquals(empty($expectedErrors), $actualResult);
  124. $this->assertEquals($expectedErrors, $actualErrors);
  125. }
  126. }