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..ccb5038c65951 --- /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( "(?