Skip to content

Commit f0e02b1

Browse files
author
Matthias Herold
committed
[FEATURE] \Magento\Framework\Config\Dom -> added ability to create default/fixed value nodes during XSD Schema Validation with flag 'LIBXML_SCHEMA_CREATE'
1 parent 14eeea2 commit f0e02b1

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

lib/internal/Magento/Framework/Config/Dom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public static function validateDomDocument(
313313
libxml_set_external_entity_loader([self::$urnResolver, 'registerEntityLoader']);
314314
$errors = [];
315315
try {
316-
$result = $dom->schemaValidate($schema);
316+
$result = $dom->schemaValidate($schema, LIBXML_SCHEMA_CREATE);
317317
if (!$result) {
318318
$errors = self::getXmlErrors($errorFormat);
319319
}

lib/internal/Magento/Framework/Config/Test/Unit/DomTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,48 @@ public function validateDataProvider()
135135
];
136136
}
137137

138+
/**
139+
* @param string $xml
140+
* @param string $expectedValue
141+
* @dataProvider validateWithDefaultValueDataProvider
142+
*/
143+
public function testValidateWithDefaultValue($xml, $expectedValue)
144+
{
145+
if (!function_exists('libxml_set_external_entity_loader')) {
146+
$this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
147+
}
148+
149+
$actualErrors = [];
150+
151+
$dom = new \Magento\Framework\Config\Dom($xml, $this->validationStateMock);
152+
$dom->validate(__DIR__ . '/_files/sample.xsd', $actualErrors);
153+
154+
$actualValue = $dom->getDom()
155+
->getElementsByTagName('root')->item(0)
156+
->getElementsByTagName('node')->item(0)
157+
->getAttribute('attribute_with_default_value');
158+
159+
$this->assertEmpty($actualErrors);
160+
$this->assertEquals($expectedValue, $actualValue);
161+
}
162+
163+
/**
164+
* @return array
165+
*/
166+
public function validateWithDefaultValueDataProvider()
167+
{
168+
return [
169+
'default_value' => [
170+
'<root><node id="id1"/></root>',
171+
'default_value'
172+
],
173+
'custom_value' => [
174+
'<root><node id="id1" attribute_with_default_value="non_default_value"/></root>',
175+
'non_default_value'
176+
],
177+
];
178+
}
179+
138180
public function testValidateCustomErrorFormat()
139181
{
140182
$xml = '<root><unknown_node/></root>';

lib/internal/Magento/Framework/Config/Test/Unit/_files/sample.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<xs:simpleContent>
2222
<xs:extension base="xs:string">
2323
<xs:attribute name="id" type="xs:string" use="required"/>
24+
<xs:attribute name="attribute_with_default_value" type="xs:string" default="default_value"/>
2425
</xs:extension>
2526
</xs:simpleContent>
2627
</xs:complexType>

0 commit comments

Comments
 (0)