From dd59b9ad4f81c96f20b6d4cf683730750e091a9c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:30:34 -0400 Subject: [PATCH 1/2] Add unit tests for saveDomDocument() in wp-admin/includes/misc.php --- .../admin/includes/misc/saveDomDocument.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/saveDomDocument.php diff --git a/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php b/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php new file mode 100644 index 0000000000000..74d9103f437ab --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php @@ -0,0 +1,78 @@ +temp_file = wp_tempnam( 'saveDomDocument' ); + } + + /** + * Clean up the test environment. + */ + public function tear_down() { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + parent::tear_down(); + } + + /** + * Tests that saveDomDocument() correctly saves a DOMDocument to a file. + * + * @ticket 65173 + */ + public function test_saveDomDocument_saves_file() { + $doc = new DOMDocument(); + $root = $doc->createElement( 'root' ); + $doc->appendChild( $root ); + + saveDomDocument( $doc, $this->temp_file ); + + $this->assertFileExists( $this->temp_file, 'The file should be saved.' ); + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $doc->saveXML(), 'The saved file content should match the XML.' ); + } + + /** + * Tests that saveDomDocument() converts LF to CRLF. + * + * @ticket 65173 + */ + public function test_saveDomDocument_converts_line_endings() { + $doc = new DOMDocument(); + // Create a multi-line XML. + $root = $doc->createElement( 'root' ); + $child1 = $doc->createElement( 'child', 'one' ); + $child2 = $doc->createElement( 'child', 'two' ); + $root->appendChild( $child1 ); + $root->appendChild( $child2 ); + $doc->appendChild( $root ); + $doc->formatOutput = true; + + saveDomDocument( $doc, $this->temp_file ); + + $content = file_get_contents( $this->temp_file ); + + // The function replaces [^\r]\n with \r\n. + // Note: DOMDocument::saveXML() usually outputs LF on Linux environments where these tests run in Docker. + + $this->assertStringContainsString( "\r\n", $content, 'The output should contain CRLF line endings.' ); + $this->assertStringNotContainsString( "(? Date: Tue, 5 May 2026 18:03:49 -0400 Subject: [PATCH 2/2] Fix formatting of root element creation in test --- tests/phpunit/tests/admin/includes/misc/saveDomDocument.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php b/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php index 74d9103f437ab..ccb5038c65951 100644 --- a/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php +++ b/tests/phpunit/tests/admin/includes/misc/saveDomDocument.php @@ -57,7 +57,7 @@ public function test_saveDomDocument_saves_file() { public function test_saveDomDocument_converts_line_endings() { $doc = new DOMDocument(); // Create a multi-line XML. - $root = $doc->createElement( 'root' ); + $root = $doc->createElement( 'root' ); $child1 = $doc->createElement( 'child', 'one' ); $child2 = $doc->createElement( 'child', 'two' ); $root->appendChild( $child1 );