XsdTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Test\Unit\Model\Config;
  7. class XsdTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $_xsdFile;
  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->_xsdFile = $urnResolver->getRealPath('urn:magento:module:Magento_Cron:etc/crontab.xsd');
  20. }
  21. /**
  22. * @param string $xmlFile
  23. * @dataProvider validXmlFileDataProvider
  24. */
  25. public function testValidXmlFile($xmlFile)
  26. {
  27. $dom = new \DOMDocument();
  28. $dom->load(__DIR__ . "/_files/{$xmlFile}");
  29. libxml_use_internal_errors(true);
  30. $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $this->_xsdFile);
  31. libxml_use_internal_errors(false);
  32. $this->assertEmpty($result, 'Validation failed with errors: ' . join(', ', $result));
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function validXmlFileDataProvider()
  38. {
  39. return [['crontab_valid.xml'], ['crontab_valid_without_schedule.xml']];
  40. }
  41. /**
  42. * @param string $xmlFile
  43. * @param array $expectedErrors
  44. * @dataProvider invalidXmlFileDataProvider
  45. */
  46. public function testInvalidXmlFile($xmlFile, $expectedErrors)
  47. {
  48. $dom = new \DOMDocument();
  49. $dom->load(__DIR__ . "/_files/{$xmlFile}");
  50. libxml_use_internal_errors(true);
  51. $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $this->_xsdFile);
  52. libxml_use_internal_errors(false);
  53. $this->assertEquals($expectedErrors, $result);
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function invalidXmlFileDataProvider()
  59. {
  60. return [
  61. [
  62. 'crontab_invalid.xml',
  63. [
  64. "Element 'job', attribute 'wrongName': The attribute 'wrongName' is not allowed.\nLine: 10\n",
  65. "Element 'job', attribute 'wrongInstance': " .
  66. "The attribute 'wrongInstance' is not allowed.\nLine: 10\n",
  67. "Element 'job', attribute 'wrongMethod': The attribute 'wrongMethod' is not allowed.\nLine: 10\n",
  68. "Element 'job': The attribute 'name' is required but missing.\nLine: 10\n",
  69. "Element 'job': The attribute 'instance' is required but missing.\nLine: 10\n",
  70. "Element 'job': The attribute 'method' is required but missing.\nLine: 10\n",
  71. "Element 'wrongSchedule': This element is not expected." .
  72. " Expected is one of ( schedule, config_path ).\nLine: 11\n"
  73. ],
  74. ],
  75. [
  76. 'crontab_invalid_duplicates.xml',
  77. [
  78. "Element 'job': Duplicate key-sequence ['job1'] in " .
  79. "unique identity-constraint 'uniqueJobName'.\nLine: 13\n"
  80. ]
  81. ],
  82. [
  83. 'crontab_invalid_without_name.xml',
  84. ["Element 'job': The attribute 'name' is required but missing.\nLine: 10\n"]
  85. ],
  86. [
  87. 'crontab_invalid_without_instance.xml',
  88. ["Element 'job': The attribute 'instance' is required but missing.\nLine: 10\n"]
  89. ],
  90. [
  91. 'crontab_invalid_without_method.xml',
  92. ["Element 'job': The attribute 'method' is required but missing.\nLine: 10\n"]
  93. ]
  94. ];
  95. }
  96. }