/vendor/magento/module-integration/Test/Unit/Model/Config/Integration/XsdTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 277 lines · 248 code · 4 blank · 25 comment · 2 complexity · d66697be20d023e8bf1a7b7f92be119e MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model\Config\Integration;
  7. /**
  8. * Test for validation rules implemented by XSD schema for API integration configuration.
  9. */
  10. class XsdTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $_schemaFile;
  16. protected function setUp()
  17. {
  18. if (!function_exists('libxml_set_external_entity_loader')) {
  19. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  20. }
  21. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  22. $this->_schemaFile = $urnResolver->getRealPath(
  23. 'urn:magento:module:Magento_Integration:etc/integration/api.xsd'
  24. );
  25. }
  26. /**
  27. * @param string $fixtureXml
  28. * @param array $expectedErrors
  29. * @dataProvider exemplarXmlDataProvider
  30. */
  31. public function testExemplarXml($fixtureXml, array $expectedErrors)
  32. {
  33. $validationStateMock = $this->getMock('\Magento\Framework\Config\ValidationStateInterface', [], [], '', false);
  34. $validationStateMock->method('isValidationRequired')
  35. ->willReturn(true);
  36. $messageFormat = '%message%';
  37. $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, $messageFormat);
  38. $actualResult = $dom->validate($this->_schemaFile, $actualErrors);
  39. $this->assertEquals(empty($expectedErrors), $actualResult, "Validation result is invalid.");
  40. $this->assertEquals($expectedErrors, $actualErrors, "Validation errors does not match.");
  41. }
  42. /**
  43. * @return array
  44. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  45. */
  46. public function exemplarXmlDataProvider()
  47. {
  48. return [
  49. /** Valid configurations */
  50. 'valid' => [
  51. '<integrations>
  52. <integration name="TestIntegration1">
  53. <resources>
  54. <resource name="Magento_Customer::manage" />
  55. <resource name="Magento_Customer::online" />
  56. </resources>
  57. </integration>
  58. </integrations>',
  59. [],
  60. ],
  61. 'valid with several entities' => [
  62. '<integrations>
  63. <integration name="TestIntegration1">
  64. <resources>
  65. <resource name="Magento_Customer::manage" />
  66. <resource name="Magento_Customer::online" />
  67. </resources>
  68. </integration>
  69. <integration name="TestIntegration2">
  70. <resources>
  71. <resource name="Magento_Catalog::product_read" />
  72. </resources>
  73. </integration>
  74. </integrations>',
  75. [],
  76. ],
  77. /** Missing required nodes */
  78. 'empty root node' => [
  79. '<integrations/>',
  80. ["Element 'integrations': Missing child element(s). Expected is ( integration )."],
  81. ],
  82. 'empty integration' => [
  83. '<integrations>
  84. <integration name="TestIntegration" />
  85. </integrations>',
  86. ["Element 'integration': Missing child element(s). Expected is ( resources )."],
  87. ],
  88. 'empty resources' => [
  89. '<integrations>
  90. <integration name="TestIntegration1">
  91. <resources>
  92. </resources>
  93. </integration>
  94. </integrations>',
  95. ["Element 'resources': Missing child element(s). Expected is ( resource )."],
  96. ],
  97. 'irrelevant root node' => [
  98. '<integration name="TestIntegration"/>',
  99. ["Element 'integration': No matching global declaration available for the validation root."],
  100. ],
  101. /** Excessive nodes */
  102. 'irrelevant node in root' => [
  103. '<integrations>
  104. <integration name="TestIntegration1">
  105. <resources>
  106. <resource name="Magento_Customer::manage" />
  107. <resource name="Magento_Customer::online" />
  108. </resources>
  109. </integration>
  110. <invalid/>
  111. </integrations>',
  112. ["Element 'invalid': This element is not expected. Expected is ( integration )."],
  113. ],
  114. 'irrelevant node in integration' => [
  115. '<integrations>
  116. <integration name="TestIntegration1">
  117. <resources>
  118. <resource name="Magento_Customer::manage" />
  119. <resource name="Magento_Customer::online" />
  120. </resources>
  121. <invalid/>
  122. </integration>
  123. </integrations>',
  124. ["Element 'invalid': This element is not expected."],
  125. ],
  126. 'irrelevant node in resources' => [
  127. '<integrations>
  128. <integration name="TestIntegration1">
  129. <resources>
  130. <resource name="Magento_Customer::manage" />
  131. <resource name="Magento_Customer::online" />
  132. <invalid/>
  133. </resources>
  134. </integration>
  135. </integrations>',
  136. ["Element 'invalid': This element is not expected. Expected is ( resource )."],
  137. ],
  138. 'irrelevant node in resource' => [
  139. '<integrations>
  140. <integration name="TestIntegration1">
  141. <resources>
  142. <resource name="Magento_Customer::manage" />
  143. <resource name="Magento_Customer::online">
  144. <invalid/>
  145. </resource>
  146. </resources>
  147. </integration>
  148. </integrations>',
  149. [
  150. "Element 'resource': Element content is not allowed, " .
  151. "because the content type is a simple type definition."
  152. ],
  153. ],
  154. /** Excessive attributes */
  155. 'invalid attribute in root' => [
  156. '<integrations invalid="invalid">
  157. <integration name="TestIntegration1">
  158. <resources>
  159. <resource name="Magento_Customer::manage" />
  160. <resource name="Magento_Customer::online" />
  161. </resources>
  162. </integration>
  163. </integrations>',
  164. ["Element 'integrations', attribute 'invalid': The attribute 'invalid' is not allowed."],
  165. ],
  166. 'invalid attribute in integration' => [
  167. '<integrations>
  168. <integration name="TestIntegration1" invalid="invalid">
  169. <resources>
  170. <resource name="Magento_Customer::manage" />
  171. <resource name="Magento_Customer::online" />
  172. </resources>
  173. </integration>
  174. </integrations>',
  175. ["Element 'integration', attribute 'invalid': The attribute 'invalid' is not allowed."],
  176. ],
  177. 'invalid attribute in resources' => [
  178. '<integrations>
  179. <integration name="TestIntegration1">
  180. <resources invalid="invalid">
  181. <resource name="Magento_Customer::manage" />
  182. <resource name="Magento_Customer::online" />
  183. </resources>
  184. </integration>
  185. </integrations>',
  186. ["Element 'resources', attribute 'invalid': The attribute 'invalid' is not allowed."],
  187. ],
  188. 'invalid attribute in resource' => [
  189. '<integrations>
  190. <integration name="TestIntegration1">
  191. <resources>
  192. <resource name="Magento_Customer::manage" invalid="invalid" />
  193. <resource name="Magento_Customer::online" />
  194. </resources>
  195. </integration>
  196. </integrations>',
  197. ["Element 'resource', attribute 'invalid': The attribute 'invalid' is not allowed."],
  198. ],
  199. /** Missing or empty required attributes */
  200. 'integration without name' => [
  201. '<integrations>
  202. <integration>
  203. <resources>
  204. <resource name="Magento_Customer::manage" />
  205. <resource name="Magento_Customer::online" />
  206. </resources>
  207. </integration>
  208. </integrations>',
  209. ["Element 'integration': The attribute 'name' is required but missing."],
  210. ],
  211. 'integration with empty name' => [
  212. '<integrations>
  213. <integration name="">
  214. <resources>
  215. <resource name="Magento_Customer::manage" />
  216. <resource name="Magento_Customer::online" />
  217. </resources>
  218. </integration>
  219. </integrations>',
  220. [
  221. "Element 'integration', attribute 'name': [facet 'minLength'] The value '' has a length of '0'; " .
  222. "this underruns the allowed minimum length of '2'.",
  223. "Element 'integration', attribute 'name': " .
  224. "'' is not a valid value of the atomic type 'integrationNameType'."
  225. ],
  226. ],
  227. 'resource without name' => [
  228. '<integrations>
  229. <integration name="TestIntegration1">
  230. <resources>
  231. <resource name="Magento_Customer::manage" />
  232. <resource />
  233. </resources>
  234. </integration>
  235. </integrations>',
  236. ["Element 'resource': The attribute 'name' is required but missing."],
  237. ],
  238. 'resource with empty name' => [
  239. '<integrations>
  240. <integration name="TestIntegration1">
  241. <resources>
  242. <resource name="Magento_Customer::manage" />
  243. <resource name="" />
  244. </resources>
  245. </integration>
  246. </integrations>',
  247. [
  248. "Element 'resource', attribute 'name': [facet 'pattern'] " .
  249. "The value '' is not accepted by the pattern '.+_.+::.+'.",
  250. "Element 'resource', attribute 'name': '' " .
  251. "is not a valid value of the atomic type 'resourceNameType'."
  252. ],
  253. ],
  254. /** Invalid values */
  255. 'resource with invalid name' => [
  256. '<integrations>
  257. <integration name="TestIntegration1">
  258. <resources>
  259. <resource name="Magento_Customer::online" />
  260. <resource name="customer_manage" />
  261. </resources>
  262. </integration>
  263. </integrations>',
  264. [
  265. "Element 'resource', attribute 'name': [facet 'pattern'] " .
  266. "The value 'customer_manage' is not accepted by the pattern '.+_.+::.+'.",
  267. "Element 'resource', attribute 'name': 'customer_manage' " .
  268. "is not a valid value of the atomic type 'resourceNameType'."
  269. ],
  270. ]
  271. ];
  272. }
  273. }