/**
  * Run schema validation against a known bad xml file with a provided schema.
  *
  * This helper expects the validation to fail and will fail a test if no errors are found.
  *
  * @param $xmlFile string a known bad xml file.
  * @param $schemaFile string schema that should find errors in the known bad xml file.
  */
 protected function _validateFileExpectSuccess($xmlFile, $schemaFile)
 {
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaFile);
     if ($errors) {
         $this->fail('There is a problem with the schema.  A known good XML file failed validation: ' . PHP_EOL . implode(PHP_EOL . PHP_EOL, $errors));
     }
 }
Пример #2
0
 /**
  * @param string $xmlFile
  * @param array $expectedErrors
  * @dataProvider invalidXmlFileDataProvider
  */
 public function testInvalidXmlFile($xmlFile, $expectedErrors)
 {
     $dom = new \DOMDocument();
     $dom->load(__DIR__ . "/_files/{$xmlFile}");
     libxml_use_internal_errors(true);
     $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $this->_xsdFile);
     libxml_use_internal_errors(false);
     $this->assertEquals($expectedErrors, $result);
 }
Пример #3
0
 /**
  * @param string $schema
  * @param string $xml
  * @return array
  */
 public function validate($schema, $xml)
 {
     $document = new \DOMDocument();
     $document->loadXML($xml);
     libxml_use_internal_errors(true);
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($document, $schema);
     libxml_use_internal_errors(false);
     return $errors;
 }
Пример #4
0
 /**
  * Constructor
  *
  * @param string $source
  * @throws \Magento\Framework\Exception
  */
 public function __construct($source)
 {
     $config = new \DOMDocument();
     $config->loadXML($source);
     $errors = Dom::validateDomDocument($config, $this->getSchemaFile());
     if (!empty($errors)) {
         throw new \Magento\Framework\Exception("Invalid Document: \n" . implode("\n", $errors));
     }
     $this->_data = $this->_extractData($config);
 }
Пример #5
0
 /**
  * Constructor
  *
  * @param string $source
  * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function __construct($source, \Magento\Framework\Config\Dom\UrnResolver $urnResolver)
 {
     $this->urnResolver = $urnResolver;
     $config = new \DOMDocument();
     $config->loadXML($source);
     $errors = Dom::validateDomDocument($config, $this->getSchemaFile());
     if (!empty($errors)) {
         throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase("Invalid Document: \n%1", [implode("\n", $errors)]));
     }
     $this->_data = $this->_extractData($config);
 }
Пример #6
0
 public function testFileSchemaUsingInvalidXml()
 {
     $xmlFile = __DIR__ . '/_files/invalid_fieldset.xml';
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $schema = \Magento\Framework\App\Utility\Files::init()->getPathToSource() . '/lib/internal/Magento/Framework/Object/etc/fieldset_file.xsd';
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     if (!$errors) {
         $this->fail('There is a problem with the schema.  A known bad XML file passed validation');
     }
 }
 public function testFileSchemaUsingInvalidXml()
 {
     $xmlFile = __DIR__ . '/_files/invalid_fieldset.xml';
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $schema = $this->urnResolver->getRealPath('urn:magento:framework:DataObject/etc/fieldset_file.xsd');
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     if (!$errors) {
         $this->fail('There is a problem with the schema.  A known bad XML file passed validation');
     }
 }
Пример #8
0
 /**
  * @param string $filePath
  * @param string $xml
  * @throws \Exception
  * @dataProvider linearFilesProvider
  */
 public function testDiConfigFileWithoutMerging($filePath, $xml)
 {
     /** @var \Magento\Framework\ObjectManager\Config\SchemaLocator $schemaLocator */
     $schemaLocator = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\ObjectManager\\Config\\SchemaLocator');
     $dom = new \DOMDocument();
     $dom->loadXML($xml);
     libxml_use_internal_errors(true);
     $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaLocator->getSchema());
     libxml_use_internal_errors(false);
     if (!empty($result)) {
         $this->fail('File ' . $filePath . ' has invalid xml structure. ' . implode("\n", $result));
     }
 }
Пример #9
0
 public function testSchema()
 {
     $invoker = new \Magento\TestFramework\Utility\AggregateInvoker($this);
     $invoker(function ($configFile) {
         $dom = new \DOMDocument();
         $dom->loadXML(file_get_contents($configFile));
         $schema = \Magento\TestFramework\Utility\Files::init()->getPathToSource() . '/app/code/Magento/Backend/etc/system_file.xsd';
         $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
         if ($errors) {
             $this->fail('XML-file has validation errors:' . PHP_EOL . implode(PHP_EOL . PHP_EOL, $errors));
         }
     }, \Magento\TestFramework\Utility\Files::init()->getConfigFiles('adminhtml/system.xml', array()));
 }
 public function testFileSchemaUsingInvalidXml()
 {
     if (!function_exists('libxml_set_external_entity_loader')) {
         $this->markTestSkipped('Skipped due to MAGETWO-45033');
     }
     $xmlFile = __DIR__ . '/_files/invalid_fieldset.xml';
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $schema = $this->urnResolver->getRealPath('urn:magento:framework:DataObject/etc/fieldset_file.xsd');
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     if (!$errors) {
         $this->fail('There is a problem with the schema.  A known bad XML file passed validation');
     }
 }
Пример #11
0
 /**
  * @param string $xsdFile
  * @param string $invalidXmlFile
  * @param int $expectedErrorsQty
  * @dataProvider invalidXmlFileDataProvider
  */
 public function testInvalidXmlFile($xsdFile, $invalidXmlFile, $expectedErrorsQty)
 {
     $dom = new \DOMDocument();
     $dom->load(__DIR__ . "/_files/{$invalidXmlFile}");
     $schema = __DIR__ . "/../../etc/{$xsdFile}";
     libxml_use_internal_errors(true);
     $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     $errorsQty = count($result);
     libxml_use_internal_errors(false);
     if ($expectedErrorsQty > 0) {
         $this->assertNotEmpty($result);
     }
     $this->assertEquals($expectedErrorsQty, $errorsQty);
 }
 public function testSchema()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($configFile) {
         $dom = new \DOMDocument();
         $dom->loadXML(file_get_contents($configFile));
         $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
         $schema = $urnResolver->getRealPath('urn:magento:module:Magento_Config:etc/system_file.xsd');
         $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
         if ($errors) {
             $this->fail('XML-file has validation errors:' . PHP_EOL . implode(PHP_EOL . PHP_EOL, $errors));
         }
     }, \Magento\Framework\App\Utility\Files::init()->getConfigFiles('adminhtml/system.xml', []));
 }
Пример #13
0
 /**
  * @param string $xsdFile
  * @param string $invalidXmlFile
  * @param int $expectedErrorsQty
  * @dataProvider invalidXmlFileDataProvider
  */
 public function testInvalidXmlFile($xsdFile, $invalidXmlFile, $expectedErrorsQty)
 {
     if (!function_exists('libxml_set_external_entity_loader')) {
         $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
     }
     $dom = new \DOMDocument();
     $dom->load(__DIR__ . "/_files/{$invalidXmlFile}");
     $schema = __DIR__ . "/../../etc/{$xsdFile}";
     libxml_use_internal_errors(true);
     $result = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     $errorsQty = count($result);
     libxml_use_internal_errors(false);
     if ($expectedErrorsQty > 0) {
         $this->assertNotEmpty($result);
     }
     $this->assertEquals($expectedErrorsQty, $errorsQty);
 }
Пример #14
0
 public function testXmlFiles()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($filename) {
         $dom = new \DOMDocument();
         $xmlFile = file_get_contents($filename);
         $dom->loadXML($xmlFile);
         $errors = libxml_get_errors();
         libxml_clear_errors();
         $this->assertEmpty($errors, print_r($errors, true));
         $schemaLocations = [];
         preg_match('/xsi:noNamespaceSchemaLocation=\\s*"(urn:[^"]+)"/s', $xmlFile, $schemaLocations);
         $this->assertEquals(2, count($schemaLocations), 'The XML file at ' . $filename . ' does not have a schema properly defined.  It should ' . 'have a xsi:noNamespaceSchemaLocation attribute defined with a URN path.  E.g. ' . 'xsi:noNamespaceSchemaLocation="urn:magento:framework:Relative_Path/something.xsd"');
         $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaLocations[1]);
         $this->assertEmpty($errors, "Error validating {$filename} against {$schemaLocations[1]}\n" . print_r($errors, true));
     }, $this->getXmlFiles());
 }
Пример #15
0
 /**
  * Run schema validation against an xml file with a provided schema.
  *
  * This helper expects the validation to pass and will fail a test if any errors are found.
  *
  * @param $xmlFile string a known good xml file.
  * @param $schemaFile string schema that should find no errors in the known good xml file.
  * @param $expectedErrors null|array that may contain a list of expected errors.  Each element can be a substring
  *   of an error, but all errors must be listed.
  */
 protected function _validateFileExpectFailure($xmlFile, $schemaFile, $expectedErrors = null)
 {
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $actualErrors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaFile);
     if (isset($expectedErrors)) {
         $this->assertNotEmpty($actualErrors, 'No schema validation errors found, expected errors: ' . PHP_EOL . implode(PHP_EOL, $expectedErrors));
         foreach ($expectedErrors as $expectedError) {
             $found = false;
             foreach ($actualErrors as $errorKey => $actualError) {
                 if (!(strpos($actualError, $expectedError) === false)) {
                     // found expected string
                     $found = true;
                     // remove found error from list of actual errors
                     unset($actualErrors[$errorKey]);
                     break;
                 }
             }
             $this->assertTrue($found, 'Failed asserting that ' . $expectedError . " is in: \n" . implode(PHP_EOL, $actualErrors));
         }
         // list of actual errors should now be empty
         $this->assertEmpty($actualErrors, "There were unexpected errors: \n" . implode(PHP_EOL, $actualErrors));
     } elseif (!$actualErrors) {
         $this->fail('There is a problem with the schema.  A known bad XML file passed validation');
     }
 }
Пример #16
0
 /**
  * Validate dom document
  *
  * @param \DOMDocument $domDocument
  * @param string|null $schemaFilePath
  * @return array of errors
  * @throws \Exception
  */
 protected function validateDomDocument(\DOMDocument $domDocument, $schema = null)
 {
     $schema = $schema !== null ? $schema : $this->schema;
     libxml_use_internal_errors(true);
     try {
         $errors = \Magento\Framework\Config\Dom::validateDomDocument($domDocument, $schema);
     } catch (\Exception $exception) {
         libxml_use_internal_errors(false);
         throw $exception;
     }
     libxml_use_internal_errors(false);
     return $errors;
 }