Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a3fb05

Browse files
committed
bug #11908 [Translation] [Config] Clear libxml errors after parsing xliff file (pulzarraider)
This PR was merged into the 2.3 branch. Discussion ---------- [Translation] [Config] Clear libxml errors after parsing xliff file | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - If libxml_use_internal_errors is set to `true` before parsing xliff file, the libxml errors are not cleared correctly. An error `Validation failed: no DTD found !` occurs in libxml errors after parsing and it's available outside the xliff parser (can break other functionality that use `libxml_get_errors` function). Commits ------- fab61ef [Translation] [Config] Clear libxml errors after parsing XML file
2 parents 4ac8add + fab61ef commit 3a3fb05

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public function testLoadFile()
5858
}
5959

6060
$this->assertInstanceOf('DOMDocument', XmlUtils::loadFile($fixtures.'valid.xml', array($mock, 'validate')));
61+
$this->assertSame(array(), libxml_get_errors());
62+
}
63+
64+
public function testLoadFileWithInternalErrorsEnabled()
65+
{
66+
libxml_use_internal_errors(true);
67+
68+
$this->assertSame(array(), libxml_get_errors());
69+
$this->assertInstanceOf('DOMDocument', XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/invalid_schema.xml'));
70+
$this->assertSame(array(), libxml_get_errors());
6171
}
6272

6373
/**

src/Symfony/Component/Config/Util/XmlUtils.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private function __construct()
3131
/**
3232
* Loads an XML file.
3333
*
34-
* @param string $file An XML file path
34+
* @param string $file An XML file path
3535
* @param string|callable $schemaOrCallable An XSD schema file path or callable
3636
*
3737
* @return \DOMDocument
@@ -95,10 +95,11 @@ public static function loadFile($file, $schemaOrCallable = null)
9595
}
9696
throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
9797
}
98-
99-
libxml_use_internal_errors($internalErrors);
10098
}
10199

100+
libxml_clear_errors();
101+
libxml_use_internal_errors($internalErrors);
102+
102103
return $dom;
103104
}
104105

src/Symfony/Component/Translation/Loader/XliffFileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function parseFile($file)
116116

117117
$dom->normalizeDocument();
118118

119+
libxml_clear_errors();
119120
libxml_use_internal_errors($internalErrors);
120121

121122
return array(simplexml_import_dom($dom), strtoupper($dom->encoding));
@@ -124,7 +125,7 @@ private function parseFile($file)
124125
/**
125126
* Returns the XML errors of the internal XML parser
126127
*
127-
* @param bool $internalErrors
128+
* @param bool $internalErrors
128129
*
129130
* @return array An array of errors
130131
*/

src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ public function testLoad()
3131

3232
$this->assertEquals('en', $catalogue->getLocale());
3333
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
34+
$this->assertSame(array(), libxml_get_errors());
35+
}
36+
37+
public function testLoadWithInternalErrorsEnabled()
38+
{
39+
libxml_use_internal_errors(true);
40+
41+
$this->assertSame(array(), libxml_get_errors());
42+
43+
$loader = new XliffFileLoader();
44+
$resource = __DIR__.'/../fixtures/resources.xlf';
45+
$catalogue = $loader->load($resource, 'en', 'domain1');
46+
47+
$this->assertEquals('en', $catalogue->getLocale());
48+
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
49+
$this->assertSame(array(), libxml_get_errors());
3450
}
3551

3652
public function testLoadWithResname()

0 commit comments

Comments
 (0)