PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-sales/Test/Unit/Model/Order/Pdf/Config/XsdTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 302 lines | 246 code | 14 blank | 42 comment | 1 complexity | 14755c924e0678f16eab63b2f413c2aa MD5 | raw file
  1. <?php
  2. /**
  3. * Test for validation rules implemented by XSD schema for sales PDF rendering configuration
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Sales\Test\Unit\Model\Order\Pdf\Config;
  9. class XsdTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected static $_schemaPath;
  15. /**
  16. * @var string
  17. */
  18. protected static $_schemaFilePath;
  19. public static function setUpBeforeClass()
  20. {
  21. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  22. self::$_schemaPath = $urnResolver->getRealPath('urn:magento:module:Magento_Sales:etc/pdf.xsd');
  23. self::$_schemaFilePath = $urnResolver->getRealPath('urn:magento:module:Magento_Sales:etc/pdf_file.xsd');
  24. }
  25. public function setUp()
  26. {
  27. if (!function_exists('libxml_set_external_entity_loader')) {
  28. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  29. }
  30. }
  31. /**
  32. * @param string $fixtureXml
  33. * @param array $expectedErrors
  34. * @dataProvider schemaByExemplarDataProvider
  35. */
  36. public function testSchemaByExemplar($fixtureXml, array $expectedErrors)
  37. {
  38. $this->_testSchema(self::$_schemaPath, $fixtureXml, $expectedErrors);
  39. }
  40. /**
  41. * @param string $fixtureXml
  42. * @param array $expectedErrors
  43. * @dataProvider fileSchemaByExemplarDataProvider
  44. */
  45. public function testFileSchemaByExemplar($fixtureXml, array $expectedErrors)
  46. {
  47. $this->_testSchema(self::$_schemaFilePath, $fixtureXml, $expectedErrors);
  48. }
  49. /**
  50. * Test schema against exemplar data
  51. *
  52. * @param string $schema
  53. * @param string $fixtureXml
  54. * @param array $expectedErrors
  55. */
  56. protected function _testSchema($schema, $fixtureXml, array $expectedErrors)
  57. {
  58. $validationStateMock = $this->getMock('\Magento\Framework\Config\ValidationStateInterface', [], [], '', false);
  59. $validationStateMock->method('isValidationRequired')
  60. ->willReturn(true);
  61. $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, '%message%');
  62. $actualResult = $dom->validate($schema, $actualErrors);
  63. $this->assertEquals(empty($expectedErrors), $actualResult);
  64. $this->assertEquals($expectedErrors, $actualErrors);
  65. }
  66. /**
  67. * @return array
  68. */
  69. public function schemaByExemplarDataProvider()
  70. {
  71. $result = $this->_getExemplarTestData();
  72. $result['non-valid totals missing title'] = [
  73. '<config><totals><total name="i1"><source_field>foo</source_field></total></totals></config>',
  74. [
  75. 'Element \'total\': Missing child element(s). Expected is one of ( title, title_source_field, ' .
  76. 'font_size, display_zero, sort_order, model, amount_prefix ).'
  77. ],
  78. ];
  79. $result['non-valid totals missing source_field'] = [
  80. '<config><totals><total name="i1"><title>Title</title></total></totals></config>',
  81. [
  82. 'Element \'total\': Missing child element(s). Expected is one of ( source_field, ' .
  83. 'title_source_field, font_size, display_zero, sort_order, model, amount_prefix ).'
  84. ],
  85. ];
  86. return $result;
  87. }
  88. /**
  89. * @return array
  90. */
  91. public function fileSchemaByExemplarDataProvider()
  92. {
  93. $result = $this->_getExemplarTestData();
  94. $result['valid totals missing title'] = [
  95. '<config><totals><total name="i1"><source_field>foo</source_field></total></totals></config>',
  96. [],
  97. ];
  98. $result['valid totals missing source_field'] = [
  99. '<config><totals><total name="i1"><title>Title</title></total></totals></config>',
  100. [],
  101. ];
  102. return $result;
  103. }
  104. /**
  105. * Return use cases, common for both merged configuration and individual files.
  106. * Reused by appropriate data providers.
  107. *
  108. * @return array
  109. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  110. */
  111. protected function _getExemplarTestData()
  112. {
  113. return [
  114. 'valid empty' => ['<config/>', []],
  115. 'valid empty renderers' => ['<config><renderers/></config>', []],
  116. 'valid empty totals' => ['<config><totals/></config>', []],
  117. 'valid empty renderers and totals' => ['<config><renderers/><totals/></config>', []],
  118. 'non-valid unknown node in <config>' => [
  119. '<config><unknown/></config>',
  120. ['Element \'unknown\': This element is not expected.'],
  121. ],
  122. 'valid pages' => [
  123. '<config><renderers><page type="p1"/><page type="p2"/></renderers></config>',
  124. [],
  125. ],
  126. 'non-valid non-unique pages' => [
  127. '<config><renderers><page type="p1"/><page type="p1"/></renderers></config>',
  128. [
  129. 'Element \'page\': Duplicate key-sequence [\'p1\'] ' .
  130. 'in unique identity-constraint \'uniquePageRenderer\'.'
  131. ],
  132. ],
  133. 'non-valid unknown node in renderers' => [
  134. '<config><renderers><unknown/></renderers></config>',
  135. ['Element \'unknown\': This element is not expected. Expected is ( page ).'],
  136. ],
  137. 'valid page renderers' => [
  138. '<config><renderers><page type="p1"><renderer product_type="prt1">Class\A</renderer>' .
  139. '<renderer product_type="prt2">Class\B</renderer></page></renderers></config>',
  140. [],
  141. ],
  142. 'non-valid non-unique page renderers' => [
  143. '<config><renderers><page type="p1"><renderer product_type="prt1">Class\A</renderer>' .
  144. '<renderer product_type="prt1">Class\B</renderer></page></renderers></config>',
  145. [
  146. 'Element \'renderer\': Duplicate key-sequence [\'prt1\'] ' .
  147. 'in unique identity-constraint \'uniqueProductTypeRenderer\'.'
  148. ],
  149. ],
  150. 'non-valid empty renderer class name' => [
  151. '<config><renderers><page type="p1"><renderer product_type="prt1"/></page></renderers></config>',
  152. [
  153. 'Element \'renderer\': [facet \'pattern\'] The value \'\' is not accepted ' .
  154. 'by the pattern \'[A-Z][a-zA-Z\d]*(\\\\[A-Z][a-zA-Z\d]*)*\'.',
  155. 'Element \'renderer\': \'\' is not a valid value of the atomic type \'classNameType\'.'
  156. ],
  157. ],
  158. 'non-valid unknown node in page' => [
  159. '<config><renderers><page type="p1"><unknown/></page></renderers></config>',
  160. ['Element \'unknown\': This element is not expected. Expected is ( renderer ).'],
  161. ],
  162. 'valid totals' => [
  163. '<config><totals><total name="i1"><title>Title1</title><source_field>src_fld1</source_field></total>' .
  164. '<total name="i2"><title>Title2</title><source_field>src_fld2</source_field></total>' .
  165. '</totals></config>',
  166. [],
  167. ],
  168. 'non-valid non-unique total items' => [
  169. '<config><totals><total name="i1"><title>Title1</title><source_field>src_fld1</source_field></total>' .
  170. '<total name="i1"><title>Title2</title><source_field>src_fld2</source_field></total>' .
  171. '</totals></config>',
  172. [
  173. 'Element \'total\': Duplicate key-sequence [\'i1\'] ' .
  174. 'in unique identity-constraint \'uniqueTotalItem\'.'
  175. ],
  176. ],
  177. 'non-valid unknown node in total items' => [
  178. '<config><totals><unknown/></totals></config>',
  179. ['Element \'unknown\': This element is not expected. Expected is ( total ).'],
  180. ],
  181. 'non-valid totals empty title' => [
  182. '<config><totals><total name="i1"><title/><source_field>foo</source_field></total></totals></config>',
  183. [
  184. 'Element \'title\': [facet \'minLength\'] The value has a length of \'0\'; ' .
  185. 'this underruns the allowed minimum length of \'1\'.',
  186. 'Element \'title\': \'\' is not a valid value of the atomic type \'nonEmptyString\'.'
  187. ],
  188. ],
  189. 'non-valid totals empty source_field' => [
  190. '<config><totals><total name="i1"><title>Title</title><source_field/></total></totals></config>',
  191. [
  192. 'Element \'source_field\': [facet \'pattern\'] The value \'\' is not accepted ' .
  193. 'by the pattern \'[a-z0-9_]+\'.',
  194. 'Element \'source_field\': \'\' is not a valid value of the atomic type \'fieldType\'.'
  195. ],
  196. ],
  197. 'non-valid totals empty title_source_field' => [
  198. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  199. '<title_source_field/></total></totals></config>',
  200. [
  201. 'Element \'title_source_field\': [facet \'pattern\'] The value \'\' is not accepted ' .
  202. 'by the pattern \'[a-z0-9_]+\'.',
  203. 'Element \'title_source_field\': \'\' is not a valid value of the atomic type \'fieldType\'.'
  204. ],
  205. ],
  206. 'non-valid totals bad model' => [
  207. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  208. '<model>a model</model></total></totals></config>',
  209. [
  210. 'Element \'model\': [facet \'pattern\'] The value \'a model\' is not accepted ' .
  211. 'by the pattern \'[A-Z][a-zA-Z\d]*(\\\\[A-Z][a-zA-Z\d]*)*\'.',
  212. 'Element \'model\': \'a model\' is not a valid value of the atomic type \'classNameType\'.'
  213. ],
  214. ],
  215. 'valid totals title_source_field' => [
  216. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  217. '<title_source_field>bar</title_source_field></total></totals></config>',
  218. [],
  219. ],
  220. 'valid totals model' => [
  221. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  222. '<model>Class\A</model></total></totals></config>',
  223. [],
  224. ],
  225. 'valid totals font_size' => [
  226. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  227. '<font_size>9</font_size></total></totals></config>',
  228. [],
  229. ],
  230. 'non-valid totals font_size 0' => [
  231. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  232. '<font_size>0</font_size></total></totals></config>',
  233. ['Element \'font_size\': \'0\' is not a valid value of the atomic type \'xs:positiveInteger\'.'],
  234. ],
  235. 'non-valid totals font_size' => [
  236. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  237. '<font_size>A</font_size></total></totals></config>',
  238. ['Element \'font_size\': \'A\' is not a valid value of the atomic type \'xs:positiveInteger\'.'],
  239. ],
  240. 'valid totals display_zero' => [
  241. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  242. '<display_zero>1</display_zero></total></totals></config>',
  243. [],
  244. ],
  245. 'valid totals display_zero true' => [
  246. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  247. '<display_zero>true</display_zero></total></totals></config>',
  248. [],
  249. ],
  250. 'non-valid totals display_zero' => [
  251. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  252. '<display_zero>A</display_zero></total></totals></config>',
  253. ['Element \'display_zero\': \'A\' is not a valid value of the atomic type \'xs:boolean\'.'],
  254. ],
  255. 'valid totals sort_order' => [
  256. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  257. '<sort_order>100</sort_order></total></totals></config>',
  258. [],
  259. ],
  260. 'valid totals sort_order 0' => [
  261. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  262. '<sort_order>0</sort_order></total></totals></config>',
  263. [],
  264. ],
  265. 'non-valid totals sort_order' => [
  266. '<config><totals><total name="i1"><title>Title</title><source_field>foo</source_field>' .
  267. '<sort_order>A</sort_order></total></totals></config>',
  268. [
  269. 'Element \'sort_order\': \'A\' is not a valid value ' .
  270. 'of the atomic type \'xs:nonNegativeInteger\'.'
  271. ],
  272. ],
  273. 'valid totals title with translate attribute' => [
  274. '<config><totals><total name="i1"><title translate="true">Title</title>' .
  275. '<source_field>foo</source_field></total></totals></config>',
  276. [],
  277. ],
  278. 'non-valid totals title with bad translate attribute' => [
  279. '<config><totals><total name="i1"><title translate="unknown">Title</title>' .
  280. '<source_field>foo</source_field></total></totals></config>',
  281. [
  282. 'Element \'title\', attribute \'translate\': \'unknown\' is not a valid value ' .
  283. 'of the atomic type \'xs:boolean\'.'
  284. ],
  285. ]
  286. ];
  287. }
  288. }