From 2482bb7f03179939ba325988a45f12a9585464dc Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:18:54 -0400 Subject: [PATCH 1/2] Add unit tests for iis7_add_rewrite_rule() --- .../includes/misc/iis7AddRewriteRule.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php diff --git a/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php new file mode 100644 index 0000000000000..d8f440386d35b --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php @@ -0,0 +1,130 @@ +temp_file = wp_tempnam( 'web.config' ); + } + + /** + * Clean up the test environment. + */ + public function tear_down() { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + parent::tear_down(); + } + + /** + * Tests that iis7_add_rewrite_rule() correctly adds WordPress rules. + * + * @ticket 65172 + * + * @dataProvider data_iis7_add_rewrite_rule + * + * @param string $initial_content The initial XML content of the file. + * @param string $rewrite_rule The rule to add. + * @param string $expected_content The expected XML content after adding. + */ + public function test_iis7_add_rewrite_rule( $initial_content, $rewrite_rule, $expected_content ) { + if ( null !== $initial_content ) { + file_put_contents( $this->temp_file, $initial_content ); + } else { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + } + + $result = iis7_add_rewrite_rule( $this->temp_file, $rewrite_rule ); + + $this->assertTrue( $result, 'iis7_add_rewrite_rule() should return true on success.' ); + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $expected_content, 'The XML content should match the expected output.' ); + } + + /** + * Data provider for test_iis7_add_rewrite_rule. + * + * @return array[] + */ + public function data_iis7_add_rewrite_rule(): array { + $rule = ''; + + return array( + 'File does not exist' => array( + 'initial_content' => null, + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Empty configuration' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing system.webServer' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing rewrite' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing rules' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing other rule' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Rule already exists (lowercase)' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + 'Rule already exists (CamelCase)' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + 'Rule with same prefix already exists' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + ); + } + + /** + * Tests that iis7_add_rewrite_rule() returns false for invalid XML. + * + * @ticket 65172 + */ + public function test_iis7_add_rewrite_rule_invalid_xml() { + file_put_contents( $this->temp_file, 'Not XML' ); + $rule = ''; + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $this->assertFalse( @iis7_add_rewrite_rule( $this->temp_file, $rule ), 'Should return false for invalid XML.' ); + } +} From da69e95ee9348dbe1c10edd41089604ce06c7d01 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:18:54 -0400 Subject: [PATCH 2/2] Add unit tests for iis7_add_rewrite_rule() --- .../includes/misc/iis7AddRewriteRule.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php diff --git a/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php new file mode 100644 index 0000000000000..52b4f7c61fd57 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7AddRewriteRule.php @@ -0,0 +1,134 @@ +temp_file = wp_tempnam( 'web.config' ); + } + + /** + * Clean up the test environment. + */ + public function tear_down() { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + parent::tear_down(); + } + + /** + * Tests that iis7_add_rewrite_rule() correctly adds WordPress rules. + * + * @ticket 65172 + * + * @dataProvider data_iis7_add_rewrite_rule + * + * @param string $initial_content The initial XML content of the file. + * @param string $rewrite_rule The rule to add. + * @param string $expected_content The expected XML content after adding. + */ + public function test_iis7_add_rewrite_rule( $initial_content, $rewrite_rule, $expected_content ) { + if ( null !== $initial_content ) { + file_put_contents( $this->temp_file, $initial_content ); + } else { + if ( file_exists( $this->temp_file ) ) { + unlink( $this->temp_file ); + } + } + + $result = iis7_add_rewrite_rule( $this->temp_file, $rewrite_rule ); + + $this->assertTrue( $result, 'iis7_add_rewrite_rule() should return true on success.' ); + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $expected_content, 'The XML content should match the expected output.' ); + } + + /** + * Data provider for test_iis7_add_rewrite_rule. + * + * @return array + */ + public function data_iis7_add_rewrite_rule(): array { + $rule = ''; + + return array( + 'File does not exist' => array( + 'initial_content' => null, + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Empty configuration' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing system.webServer' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing rewrite' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing rules' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Existing other rule' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '' . $rule . '', + ), + 'Rule already exists (lowercase)' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + 'Rule already exists (CamelCase)' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + 'Rule with same prefix already exists' => array( + 'initial_content' => '', + 'rewrite_rule' => $rule, + 'expected_content' => '', + ), + ); + } + + /** + * Tests that iis7_add_rewrite_rule() returns false for invalid XML. + * + * @ticket 65172 + */ + public function test_iis7_add_rewrite_rule_invalid_xml() { + file_put_contents( $this->temp_file, 'Not XML' ); + $rule = ''; + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $this->assertFalse( @iis7_add_rewrite_rule( $this->temp_file, $rule ), 'Should return false for invalid XML.' ); + } +}