XsdTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model\Config\Consolidated;
  7. /**
  8. * Test for validation rules implemented by XSD schema for 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/integration.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->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  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. '<config>
  52. <integration name="TestIntegration">
  53. <email>test-integration@magento.com</email>
  54. <endpoint_url>https://endpoint.url</endpoint_url>
  55. <identity_link_url>http://www.example.com/identity</identity_link_url>
  56. <resources>
  57. <resource name="Magento_Customer::manage" />
  58. <resource name="Magento_Customer::online" />
  59. </resources>
  60. </integration>
  61. </config>',
  62. [],
  63. ],
  64. 'valid with several entities' => [
  65. '<config>
  66. <integration name="TestIntegration1">
  67. <email>test-integration1@magento.com</email>
  68. <endpoint_url>http://endpoint.url</endpoint_url>
  69. <identity_link_url>http://www.example.com/identity</identity_link_url>
  70. <resources>
  71. <resource name="Magento_Customer::manage" />
  72. <resource name="Magento_Customer::online" />
  73. </resources>
  74. </integration>
  75. <integration name="TestIntegration2">
  76. <email>test-integration2@magento.com</email>
  77. <resources>
  78. <resource name="Magento_Catalog::product_read" />
  79. </resources>
  80. </integration>
  81. </config>',
  82. [],
  83. ],
  84. /** Missing required elements */
  85. 'empty root node' => [
  86. '<config/>',
  87. ["Element 'config': Missing child element(s). Expected is ( integration )."],
  88. ],
  89. 'empty integration' => [
  90. '<config>
  91. <integration name="TestIntegration" />
  92. </config>',
  93. ["Element 'integration': Missing child element(s)." .
  94. " Expected is one of ( email, endpoint_url, identity_link_url, resources )."],
  95. ],
  96. 'integration without email' => [
  97. '<config>
  98. <integration name="TestIntegration1">
  99. <endpoint_url>http://endpoint.url</endpoint_url>
  100. <identity_link_url>http://www.example.com/identity</identity_link_url>
  101. <resources>
  102. <resource name="Magento_Customer::manage" />
  103. <resource name="Magento_Customer::online" />
  104. </resources>
  105. </integration>
  106. </config>',
  107. ["Element 'integration': Missing child element(s). Expected is ( email )."],
  108. ],
  109. 'empty resources' => [
  110. '<config>
  111. <integration name="TestIntegration1">
  112. <email>test-integration1@magento.com</email>
  113. <endpoint_url>http://endpoint.url</endpoint_url>
  114. <identity_link_url>http://www.example.com/identity</identity_link_url>
  115. <resources>
  116. </resources>
  117. </integration>
  118. </config>',
  119. ["Element 'resources': Missing child element(s). Expected is ( resource )."],
  120. ],
  121. /** Empty nodes */
  122. 'empty email' => [
  123. '<config>
  124. <integration name="TestIntegration1">
  125. <email></email>
  126. <endpoint_url>http://endpoint.url</endpoint_url>
  127. <identity_link_url>http://www.example.com/identity</identity_link_url>
  128. <resources>
  129. <resource name="Magento_Customer::manage" />
  130. <resource name="Magento_Customer::online" />
  131. </resources>
  132. </integration>
  133. </config>',
  134. [
  135. "Element 'email': [facet 'pattern'] The value '' is not " .
  136. "accepted by the pattern '[^@]+@[^\.]+\..+'.",
  137. "Element 'email': '' is not a valid value of the atomic type 'emailType'."
  138. ],
  139. ],
  140. 'endpoint_url is empty' => [
  141. '<config>
  142. <integration name="TestIntegration1">
  143. <email>test-integration1@magento.com</email>
  144. <endpoint_url></endpoint_url>
  145. <resources>
  146. <resource name="Magento_Customer::manage" />
  147. <resource name="Magento_Customer::online" />
  148. </resources>
  149. </integration>
  150. </config>',
  151. [
  152. "Element 'endpoint_url': [facet 'minLength'] The value has a length of '0'; this underruns" .
  153. " the allowed minimum length of '4'.",
  154. "Element 'endpoint_url': '' is not a valid value of the atomic type 'urlType'."
  155. ],
  156. ],
  157. 'identity_link_url is empty' => [
  158. '<config>
  159. <integration name="TestIntegration1">
  160. <email>test-integration1@magento.com</email>
  161. <endpoint_url>http://endpoint.url</endpoint_url>
  162. <identity_link_url></identity_link_url>
  163. <resources>
  164. <resource name="Magento_Customer::manage" />
  165. <resource name="Magento_Customer::online" />
  166. </resources>
  167. </integration>
  168. </config>',
  169. [
  170. "Element 'identity_link_url': [facet 'minLength'] The value has a length of '0'; this underruns" .
  171. " the allowed minimum length of '4'.",
  172. "Element 'identity_link_url': '' is not a valid value of the atomic type 'urlType'."
  173. ],
  174. ],
  175. /** Invalid structure */
  176. 'irrelevant root node' => [
  177. '<integration name="TestIntegration"/>',
  178. ["Element 'integration': No matching global declaration available for the validation root."],
  179. ],
  180. 'irrelevant node in root' => [
  181. '<config>
  182. <integration name="TestIntegration1">
  183. <email>test-integration1@magento.com</email>
  184. <endpoint_url>http://endpoint.url</endpoint_url>
  185. <identity_link_url>http://www.example.com/identity</identity_link_url>
  186. <resources>
  187. <resource name="Magento_Customer::manage" />
  188. <resource name="Magento_Customer::online" />
  189. </resources>
  190. </integration>
  191. <invalid/>
  192. </config>',
  193. ["Element 'invalid': This element is not expected. Expected is ( integration )."],
  194. ],
  195. 'irrelevant node in integration' => [
  196. '<config>
  197. <integration name="TestIntegration1">
  198. <email>test-integration1@magento.com</email>
  199. <endpoint_url>http://endpoint.url</endpoint_url>
  200. <identity_link_url>http://www.example.com/identity</identity_link_url>
  201. <resources>
  202. <resource name="Magento_Customer::manage" />
  203. <resource name="Magento_Customer::online" />
  204. </resources>
  205. <invalid/>
  206. </integration>
  207. </config>',
  208. ["Element 'invalid': This element is not expected."],
  209. ],
  210. 'irrelevant node in resources' => [
  211. '<config>
  212. <integration name="TestIntegration1">
  213. <email>test-integration1@magento.com</email>
  214. <endpoint_url>http://endpoint.url</endpoint_url>
  215. <identity_link_url>http://www.example.com/identity</identity_link_url>
  216. <resources>
  217. <resource name="Magento_Customer::manage" />
  218. <resource name="Magento_Customer::online" />
  219. <invalid/>
  220. </resources>
  221. </integration>
  222. </config>',
  223. ["Element 'invalid': This element is not expected. Expected is ( resource )."],
  224. ],
  225. 'irrelevant node in resource' => [
  226. '<config>
  227. <integration name="TestIntegration1">
  228. <email>test-integration1@magento.com</email>
  229. <endpoint_url>http://endpoint.url</endpoint_url>
  230. <identity_link_url>http://www.example.com/identity</identity_link_url>
  231. <resources>
  232. <resource name="Magento_Customer::manage" />
  233. <resource name="Magento_Customer::online">
  234. <invalid/>
  235. </resource>
  236. </resources>
  237. </integration>
  238. </config>',
  239. [
  240. "Element 'resource': Element content is not allowed, " .
  241. "because the content type is a simple type definition."
  242. ],
  243. ],
  244. /** Excessive attributes */
  245. 'invalid attribute in root' => [
  246. '<config invalid="invalid">
  247. <integration name="TestIntegration1">
  248. <email>test-integration1@magento.com</email>
  249. <endpoint_url>http://endpoint.url</endpoint_url>
  250. <identity_link_url>http://www.example.com/identity</identity_link_url>
  251. <resources>
  252. <resource name="Magento_Customer::manage" />
  253. <resource name="Magento_Customer::online" />
  254. </resources>
  255. </integration>
  256. </config>',
  257. ["Element 'config', attribute 'invalid': The attribute 'invalid' is not allowed."],
  258. ],
  259. 'invalid attribute in integration' => [
  260. '<config>
  261. <integration name="TestIntegration1" invalid="invalid">
  262. <email>test-integration1@magento.com</email>
  263. <endpoint_url>http://endpoint.url</endpoint_url>
  264. <identity_link_url>http://www.example.com/identity</identity_link_url>
  265. <resources>
  266. <resource name="Magento_Customer::manage" />
  267. <resource name="Magento_Customer::online" />
  268. </resources>
  269. </integration>
  270. </config>',
  271. ["Element 'integration', attribute 'invalid': The attribute 'invalid' is not allowed."],
  272. ],
  273. 'invalid attribute in email' => [
  274. '<config>
  275. <integration name="TestIntegration1">
  276. <email invalid="invalid">test-integration1@magento.com</email>
  277. <endpoint_url>http://endpoint.url</endpoint_url>
  278. <identity_link_url>http://www.example.com/identity</identity_link_url>
  279. <resources>
  280. <resource name="Magento_Customer::manage"/>
  281. <resource name="Magento_Customer::online" />
  282. </resources>
  283. </integration>
  284. </config>',
  285. ["Element 'email', attribute 'invalid': The attribute 'invalid' is not allowed."],
  286. ],
  287. 'invalid attribute in resources' => [
  288. '<config>
  289. <integration name="TestIntegration1">
  290. <email>test-integration1@magento.com</email>
  291. <endpoint_url>http://endpoint.url</endpoint_url>
  292. <identity_link_url>http://www.example.com/identity</identity_link_url>
  293. <resources invalid="invalid">
  294. <resource name="Magento_Customer::manage" />
  295. <resource name="Magento_Customer::online" />
  296. </resources>
  297. </integration>
  298. </config>',
  299. ["Element 'resources', attribute 'invalid': The attribute 'invalid' is not allowed."],
  300. ],
  301. 'invalid attribute in resource' => [
  302. '<config>
  303. <integration name="TestIntegration1">
  304. <email>test-integration1@magento.com</email>
  305. <endpoint_url>http://endpoint.url</endpoint_url>
  306. <identity_link_url>http://www.example.com/identity</identity_link_url>
  307. <resources>
  308. <resource name="Magento_Customer::manage" invalid="invalid" />
  309. <resource name="Magento_Customer::online" />
  310. </resources>
  311. </integration>
  312. </config>',
  313. ["Element 'resource', attribute 'invalid': The attribute 'invalid' is not allowed."],
  314. ],
  315. 'invalid attribute in endpoint_url' => [
  316. '<config>
  317. <integration name="TestIntegration1">
  318. <email>test-integration1@magento.com</email>
  319. <endpoint_url invalid="invalid">http://endpoint.url</endpoint_url>
  320. <identity_link_url>http://www.example.com/identity</identity_link_url>
  321. <resources>
  322. <resource name="Magento_Customer::manage"/>
  323. <resource name="Magento_Customer::online" />
  324. </resources>
  325. </integration>
  326. </config>',
  327. ["Element 'endpoint_url', attribute 'invalid': The attribute 'invalid' is not allowed."],
  328. ],
  329. 'invalid attribute in identity_link_url' => [
  330. '<config>
  331. <integration name="TestIntegration1">
  332. <email>test-integration1@magento.com</email>
  333. <endpoint_url>http://endpoint.url</endpoint_url>
  334. <identity_link_url invalid="invalid">http://endpoint.url</identity_link_url>
  335. <resources>
  336. <resource name="Magento_Customer::manage"/>
  337. <resource name="Magento_Customer::online" />
  338. </resources>
  339. </integration>
  340. </config>',
  341. ["Element 'identity_link_url', attribute 'invalid': The attribute 'invalid' is not allowed."],
  342. ],
  343. /** Missing or empty required attributes */
  344. 'integration without name' => [
  345. '<config>
  346. <integration>
  347. <email>test-integration1@magento.com</email>
  348. <endpoint_url>http://endpoint.url</endpoint_url>
  349. <identity_link_url>http://www.example.com/identity</identity_link_url>
  350. <resources>
  351. <resource name="Magento_Customer::manage" />
  352. <resource name="Magento_Customer::online" />
  353. </resources>
  354. </integration>
  355. </config>',
  356. ["Element 'integration': The attribute 'name' is required but missing."],
  357. ],
  358. 'integration with empty name' => [
  359. '<config>
  360. <integration name="">
  361. <email>test-integration1@magento.com</email>
  362. <endpoint_url>http://endpoint.url</endpoint_url>
  363. <identity_link_url>http://www.example.com/identity</identity_link_url>
  364. <resources>
  365. <resource name="Magento_Customer::manage" />
  366. <resource name="Magento_Customer::online" />
  367. </resources>
  368. </integration>
  369. </config>',
  370. [
  371. "Element 'integration', attribute 'name': [facet 'minLength'] The value '' has a length of '0'; " .
  372. "this underruns the allowed minimum length of '2'.",
  373. "Element 'integration', attribute 'name': " .
  374. "'' is not a valid value of the atomic type 'integrationNameType'."
  375. ],
  376. ],
  377. 'resource without name' => [
  378. '<config>
  379. <integration name="TestIntegration1">
  380. <email>test-integration1@magento.com</email>
  381. <endpoint_url>http://endpoint.url</endpoint_url>
  382. <identity_link_url>http://www.example.com/identity</identity_link_url>
  383. <resources>
  384. <resource name="Magento_Customer::manage" />
  385. <resource />
  386. </resources>
  387. </integration>
  388. </config>',
  389. ["Element 'resource': The attribute 'name' is required but missing."],
  390. ],
  391. 'resource with empty name' => [
  392. '<config>
  393. <integration name="TestIntegration1">
  394. <email>test-integration1@magento.com</email>
  395. <endpoint_url>http://endpoint.url</endpoint_url>
  396. <identity_link_url>http://www.example.com/identity</identity_link_url>
  397. <resources>
  398. <resource name="Magento_Customer::manage" />
  399. <resource name="" />
  400. </resources>
  401. </integration>
  402. </config>',
  403. [
  404. "Element 'resource', attribute 'name': [facet 'pattern'] " .
  405. "The value '' is not accepted by the pattern '.+_.+::.+'.",
  406. "Element 'resource', attribute 'name': '' " .
  407. "is not a valid value of the atomic type 'resourceNameType'."
  408. ],
  409. ],
  410. /** Invalid values */
  411. 'invalid email' => [
  412. '<config>
  413. <integration name="TestIntegration1">
  414. <email>invalid</email>
  415. <endpoint_url>http://endpoint.url</endpoint_url>
  416. <identity_link_url>http://www.example.com/identity</identity_link_url>
  417. <resources>
  418. <resource name="Magento_Customer::manage" />
  419. <resource name="Magento_Customer::online" />
  420. </resources>
  421. </integration>
  422. </config>',
  423. [
  424. "Element 'email': [facet 'pattern'] The value 'invalid' " .
  425. "is not accepted by the pattern '[^@]+@[^\.]+\..+'.",
  426. "Element 'email': 'invalid' is not a valid value of the atomic type 'emailType'."
  427. ],
  428. ],
  429. /** Invalid values */
  430. 'resource with invalid name' => [
  431. '<config>
  432. <integration name="TestIntegration1">
  433. <email>test-integration1@magento.com</email>
  434. <endpoint_url>http://endpoint.url</endpoint_url>
  435. <identity_link_url>http://www.example.com/identity</identity_link_url>
  436. <resources>
  437. <resource name="Magento_Customer::online" />
  438. <resource name="customer_manage" />
  439. </resources>
  440. </integration>
  441. </config>',
  442. [
  443. "Element 'resource', attribute 'name': [facet 'pattern'] " .
  444. "The value 'customer_manage' is not accepted by the pattern '.+_.+::.+'.",
  445. "Element 'resource', attribute 'name': 'customer_manage' " .
  446. "is not a valid value of the atomic type 'resourceNameType'."
  447. ],
  448. ]
  449. ];
  450. }
  451. }