/lib/internal/Magento/Framework/Api/Test/Unit/ExtensionAttribute/Config/XsdTest.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 163 lines · 141 code · 4 blank · 18 comment · 1 complexity · 5d24aa1ce3af397b5c51be826eb93da6 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api\Test\Unit\ExtensionAttribute\Config;
  7. class XsdTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $_schemaFile;
  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. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  19. $this->_schemaFile = $urnResolver->getRealPath('urn:magento:framework:Api/etc/extension_attributes.xsd');
  20. }
  21. /**
  22. * @param string $fixtureXml
  23. * @param array $expectedErrors
  24. * @dataProvider exemplarXmlDataProvider
  25. */
  26. public function testExemplarXml($fixtureXml, array $expectedErrors)
  27. {
  28. $validationStateMock = $this->getMock(
  29. \Magento\Framework\Config\ValidationStateInterface::class,
  30. [],
  31. [],
  32. '',
  33. false
  34. );
  35. $validationStateMock->method('isValidationRequired')
  36. ->willReturn(true);
  37. $messageFormat = '%message%';
  38. $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, $messageFormat);
  39. $actualResult = $dom->validate($this->_schemaFile, $actualErrors);
  40. $this->assertEquals($expectedErrors, $actualErrors, "Validation errors does not match.");
  41. $this->assertEquals(empty($expectedErrors), $actualResult, "Validation result is invalid.");
  42. }
  43. /**
  44. * @return array
  45. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  46. */
  47. public function exemplarXmlDataProvider()
  48. {
  49. return [
  50. /** Valid configurations */
  51. 'valid with empty extension attributes' => [
  52. '<config>
  53. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  54. </extension_attributes>
  55. </config>',
  56. [],
  57. ],
  58. 'valid with one attribute code' => [
  59. '<config>
  60. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  61. <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface" />
  62. </extension_attributes>
  63. </config>',
  64. [],
  65. ],
  66. 'valid with multiple attribute codes' => [
  67. '<config>
  68. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  69. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom" />
  70. <attribute code="custom_2" type="Magento\CustomerExtra\Api\Data\CustomerCustom2" />
  71. </extension_attributes>
  72. </config>',
  73. [],
  74. ],
  75. 'valid with multiple attribute codes with permissions' => [
  76. '<config>
  77. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  78. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom">
  79. <resources>
  80. <resource ref="Magento_Customer::manage"/>
  81. </resources>
  82. </attribute>
  83. <attribute code="custom_2" type="Magento\CustomerExtra\Api\Data\CustomerCustom2">
  84. <resources>
  85. <resource ref="Magento_Customer::manage"/>
  86. <resource ref="Magento_Catalog::other"/>
  87. </resources>
  88. </attribute>
  89. </extension_attributes>
  90. </config>',
  91. [],
  92. ],
  93. 'valid with attribute code with join' => [
  94. '<config>
  95. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  96. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom">
  97. <join reference_table="library_account"
  98. reference_field="customer_id"
  99. join_on_field="id"
  100. >
  101. <field>library_card_id</field>
  102. </join>
  103. </attribute>
  104. </extension_attributes>
  105. </config>',
  106. [],
  107. ],
  108. 'valid with attribute code with permissions and join' => [
  109. '<config>
  110. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  111. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom">
  112. <resources>
  113. <resource ref="Magento_Customer::manage"/>
  114. </resources>
  115. <join reference_table="library_account"
  116. reference_field="customer_id"
  117. join_on_field="id"
  118. >
  119. <field>library_card_id</field>
  120. </join>
  121. </attribute>
  122. </extension_attributes>
  123. </config>',
  124. [],
  125. ],
  126. /** Invalid configurations */
  127. 'invalid missing extension_attributes' => [
  128. '<config/>',
  129. ["Element 'config': Missing child element(s). Expected is ( extension_attributes )."],
  130. ],
  131. 'invalid with attribute code with resources without single resource' => [
  132. '<config>
  133. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  134. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom">
  135. <resources>
  136. </resources>
  137. </attribute>
  138. </extension_attributes>
  139. </config>',
  140. ["Element 'resources': Missing child element(s). Expected is ( resource )."],
  141. ],
  142. 'invalid with attribute code without join attributes' => [
  143. '<config>
  144. <extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
  145. <attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom">
  146. <join/>
  147. </attribute>
  148. </extension_attributes>
  149. </config>',
  150. [
  151. "Element 'join': The attribute 'reference_table' is required but missing.",
  152. "Element 'join': The attribute 'join_on_field' is required but missing.",
  153. "Element 'join': The attribute 'reference_field' is required but missing.",
  154. "Element 'join': Missing child element(s). Expected is ( field ).",
  155. ],
  156. ],
  157. ];
  158. }
  159. }