XsdTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Test for validation rules implemented by XSD schema for customer address format configuration
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Test\Unit\Model\Address\Config;
  9. class XsdTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $_schemaFile;
  15. protected function setUp()
  16. {
  17. if (!function_exists('libxml_set_external_entity_loader')) {
  18. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  19. }
  20. $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
  21. $this->_schemaFile = $urnResolver->getRealPath('urn:magento:module:Magento_Customer:etc/address_formats.xsd');
  22. }
  23. /**
  24. * @param string $fixtureXml
  25. * @param array $expectedErrors
  26. * @dataProvider exemplarXmlDataProvider
  27. */
  28. public function testExemplarXml($fixtureXml, array $expectedErrors)
  29. {
  30. $validationStateMock = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
  31. $validationStateMock->method('isValidationRequired')
  32. ->willReturn(true);
  33. $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationStateMock, [], null, null, '%message%');
  34. $actualResult = $dom->validate($this->_schemaFile, $actualErrors);
  35. $this->assertEquals(empty($expectedErrors), $actualResult);
  36. $this->assertEquals($expectedErrors, $actualErrors);
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function exemplarXmlDataProvider()
  42. {
  43. return [
  44. 'valid' => ['<config><format code="code" title="title" /></config>', []],
  45. 'valid with optional attributes' => [
  46. '<config><format code="code" title="title" renderer="Some_Renderer" escapeHtml="false" /></config>',
  47. [],
  48. ],
  49. 'empty root node' => [
  50. '<config/>',
  51. ["Element 'config': Missing child element(s). Expected is ( format )."],
  52. ],
  53. 'irrelevant root node' => [
  54. '<attribute name="attr"/>',
  55. ["Element 'attribute': No matching global declaration available for the validation root."],
  56. ],
  57. 'irrelevant node' => [
  58. '<config><format code="code" title="title" /><invalid /></config>',
  59. ["Element 'invalid': This element is not expected. Expected is ( format )."],
  60. ],
  61. 'non empty node "format"' => [
  62. '<config><format code="code" title="title"><invalid /></format></config>',
  63. ["Element 'format': Element content is not allowed, because the content type is empty."],
  64. ],
  65. 'node "format" without attribute "code"' => [
  66. '<config><format title="title" /></config>',
  67. ["Element 'format': The attribute 'code' is required but missing."],
  68. ],
  69. 'node "format" without attribute "title"' => [
  70. '<config><format code="code" /></config>',
  71. ["Element 'format': The attribute 'title' is required but missing."],
  72. ],
  73. 'node "format" with invalid attribute' => [
  74. '<config><format code="code" title="title" invalid="invalid" /></config>',
  75. ["Element 'format', attribute 'invalid': The attribute 'invalid' is not allowed."],
  76. ],
  77. 'attribute "escapeHtml" with invalid type' => [
  78. '<config><format code="code" title="title" escapeHtml="invalid" /></config>',
  79. [
  80. "Element 'format', attribute 'escapeHtml': 'invalid' is not a valid value of the atomic type" .
  81. " 'xs:boolean'."
  82. ],
  83. ]
  84. ];
  85. }
  86. }