Skip to content

Commit d2a32e3

Browse files
committed
Add failing test for mutating DOMDocument
A DOMDocument instance being passed to the parser should not have changed after parsing. This could potentially trip-up further use of the same DOMDocument instance. See microformats#174.
1 parent ae49928 commit d2a32e3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/Mf2/ParserTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -790,5 +790,27 @@ public function testHtml5OptionalPEndTag() {
790790
$this->assertEquals('Name', $output['items'][0]['properties']['name'][0]);
791791
}
792792

793+
/**
794+
* Make sure the parser does not mutate any DOMDocument instances passed to the constructor.
795+
* @see https://github.com/indieweb/php-mf2/issues/174
796+
* @see https://github.com/microformats/mf2py/issues/104
797+
*/
798+
public function testNotMutatingPassedInDOM() {
799+
$input = file_get_contents(__DIR__ . '/snarfed.org.html');
800+
801+
// Use same parsing as Parser::__construct(), twice to have a comparison object.
802+
libxml_use_internal_errors(true);
803+
$refDoc = new \DOMDocument();
804+
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
805+
$inputDoc = new \DOMDocument();
806+
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
807+
808+
// For completion sake, test PHP itself.
809+
$this->assertEquals($refDoc, $inputDoc, 'PHP could not create identical DOMDocument instances.');
810+
811+
// Parse one DOMDocument instance, and test if it is still the same as the other.
812+
Mf2\parse($inputDoc, 'http://snarfed.org/2013-10-23_oauth-dropins');
813+
$this->assertEquals($refDoc, $inputDoc, 'Parsing mutated the DOMDocument.');
814+
}
793815
}
794816

0 commit comments

Comments
 (0)