Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WidgetConfigTest.php
Go to the documentation of this file.
1 <?php
9 
10 class WidgetConfigTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $urnResolver;
14 
15  protected function setUp()
16  {
17  $this->urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
18  }
19 
20  public function testXmlFiles()
21  {
22  $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
23  $invoker(
27  function ($configFile) {
28  $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget.xsd');
29  $this->_validateFileExpectSuccess($configFile, $schema);
30  },
31  array_merge(
32  \Magento\Framework\App\Utility\Files::init()->getConfigFiles('widget.xml'),
33  \Magento\Framework\App\Utility\Files::init()->getLayoutConfigFiles('widget.xml')
34  )
35  );
36  }
37 
38  public function testSchemaUsingValidXml()
39  {
40  $xmlFile = __DIR__ . '/_files/widget.xml';
41  $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget.xsd');
42  $this->_validateFileExpectSuccess($xmlFile, $schema);
43  }
44 
45  public function testSchemaUsingInvalidXml()
46  {
47  if (!function_exists('libxml_set_external_entity_loader')) {
48  $this->markTestSkipped('Skipped due to MAGETWO-45033');
49  }
50  $xmlFile = __DIR__ . '/_files/invalid_widget.xml';
51  $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget.xsd');
52  $this->_validateFileExpectFailure($xmlFile, $schema);
53  }
54 
55  public function testFileSchemaUsingXml()
56  {
57  $xmlFile = __DIR__ . '/_files/widget_file.xml';
58  $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget_file.xsd');
59  $this->_validateFileExpectSuccess($xmlFile, $schema);
60  }
61 
63  {
64  if (!function_exists('libxml_set_external_entity_loader')) {
65  $this->markTestSkipped('Skipped due to MAGETWO-45033');
66  }
67  $xmlFile = __DIR__ . '/_files/invalid_widget.xml';
68  $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget_file.xsd');
69  $this->_validateFileExpectFailure($xmlFile, $schema);
70  }
71 
80  protected function _validateFileExpectFailure($xmlFile, $schemaFile)
81  {
82  $dom = new \DOMDocument();
83  $dom->loadXML(file_get_contents($xmlFile));
85  if (!$errors) {
86  $this->fail('There is a problem with the schema. A known bad XML file passed validation');
87  }
88  }
89 
98  protected function _validateFileExpectSuccess($xmlFile, $schemaFile)
99  {
100  $dom = new \DOMDocument();
101  $dom->loadXML(file_get_contents($xmlFile));
103  if ($errors) {
104  $this->fail(
105  'There is a problem with the schema. A known good XML file failed validation: ' . PHP_EOL . implode(
106  PHP_EOL . PHP_EOL,
107  $errors
108  )
109  );
110  }
111  }
112 }
static validateDomDocument(\DOMDocument $dom, $schema, $errorFormat=self::ERROR_FORMAT_DEFAULT)
Definition: Dom.php:301
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$errors
Definition: overview.phtml:9