diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php index 3724684ffd428..61e9a09e2b799 100644 --- a/src/wp-admin/includes/misc.php +++ b/src/wp-admin/includes/misc.php @@ -833,7 +833,6 @@ function iis7_rewrite_rule_exists( $filename ) { } $doc = new DOMDocument(); - if ( $doc->load( $filename ) === false ) { return false; } diff --git a/tests/phpunit/tests/admin/includes/misc/iis7RewriteRuleExists.php b/tests/phpunit/tests/admin/includes/misc/iis7RewriteRuleExists.php new file mode 100644 index 0000000000000..83532b1f51983 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7RewriteRuleExists.php @@ -0,0 +1,100 @@ +temp_file = wp_tempnam( 'web.config' ); + } + + public function tear_down() { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + + parent::tear_down(); + } + + /** + * Tests that iis7_rewrite_rule_exists() correctly identifies the existence of rules. + * + * @ticket 65148 + * + * @dataProvider data_iis7_rewrite_rule_exists + * + * @phpstan-return array + * + * @param bool $expected Whether the rule is expected to exist. + * @param string $content The XML content of the file. + */ + public function test_iis7_rewrite_rule_exists( $expected, $content ) { + if ( 'Not XML' === $content ) { + file_put_contents( $this->temp_file, $content ); + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $this->assertSame( $expected, @iis7_rewrite_rule_exists( $this->temp_file ) ); + } else { + file_put_contents( $this->temp_file, $content ); + $this->assertSame( $expected, iis7_rewrite_rule_exists( $this->temp_file ) ); + } + } + + /** + * Data provider for test_iis7_rewrite_rule_exists. + * + * @return array + */ + public function data_iis7_rewrite_rule_exists(): array { + return array( + 'Rule with name starting with "wordpress" exists' => array( + 'expected' => true, + 'content' => '', + ), + 'Rule with name "wordpress" exists' => array( + 'expected' => true, + 'content' => '', + ), + 'Rule with name "WordPress" exists' => array( + 'expected' => true, + 'content' => '', + ), + 'Rule does not exist' => array( + 'expected' => false, + 'content' => '', + ), + 'Empty configuration' => array( + 'expected' => false, + 'content' => '', + ), + 'Invalid XML' => array( + 'expected' => false, + 'content' => 'Not XML', + ), + ); + } + + /** + * Tests that iis7_rewrite_rule_exists() returns false if the file does not exist. + * + * @ticket 65148 + */ + public function test_iis7_rewrite_rule_exists_non_existent_file() { + $this->assertFalse( iis7_rewrite_rule_exists( '/non/existent/file' ) ); + } +}