/** * Getter for node by path * * @param string $nodePath * @return \DOMElement|null * @throws \Magento\Framework\Exception an exception is possible if original document contains multiple fixed nodes */ protected function _getMatchedNode($nodePath) { if (!preg_match('/^\\/config(\\/menu)?$/i', $nodePath)) { return null; } return parent::_getMatchedNode($nodePath); }
/** * @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); }
/** * @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; }
/** * 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)); } }
/** * 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); }
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'); } }
/** * 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); }
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())); }
/** * @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)); } }
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', [])); }
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'); } }
/** * @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 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()); }
/** * @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); }
/** * @override * @see \Magento\Framework\Config\Dom::_initDom() * @param string $xml * @return \DOMDocument * @throws \Magento\Framework\Config\Dom\ValidationException */ protected function _initDom($xml) { /** @var string $defaultSchema */ $defaultSchema = $this->schema; /** @var \DOMDocument $dom */ $dom = new \DOMDocument(); $dom->loadXML($xml); // Возвращает строку вида: «urn:magento:module:Magento_Config:etc/system_file.xsd» /** @var string $schema */ $schema = $dom->documentElement->getAttributeNS($dom->lookupNamespaceUri('xsi'), 'noNamespaceSchemaLocation'); /** * Используем df_starts_with($customSchema, 'urn:') * для совместимости с устаревшим и нигде в ядре не используемым форматом * с обратными файловыми путями: ../ * * 2016-06-07 * Раньше тут стояло: * if ($schema && && df_starts_with($schema, 'urn:') * Однако сторонние модули используют хуёвые невалидные схемы типа * urn:magento:framework:Backend/etc/system_file.xsd * что приводило к сбоям. */ if ($schema && 'urn:magento:module:Df_Config:etc/system_file.xsd' === $schema) { /** * Переводить схему в формат файлового пути необязательно: * https://github.com/magento/magento2/blob/2335247d4ae2dc1e0728ee73022b0a244ccd7f4c/lib/internal/Magento/Framework/Config/Dom/UrnResolver.php#L69-L71 * https://github.com/magento/magento2/blob/2335247d4ae2dc1e0728ee73022b0a244ccd7f4c/lib/internal/Magento/Framework/Config/Dom/UrnResolver.php#L26-L55 */ $this->schema = $schema; } /** @var \DOMDocument $result */ try { $result = parent::_initDom($xml); } finally { $this->schema = $defaultSchema; } return $result; }
/** * 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'); } }
/** * 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; }