From 2b057787a3b0fa905b215e019bbf279f6eae9ec6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:08:48 -0400 Subject: [PATCH 1/4] Add unit tests for iis7_delete_rewrite_rule() --- .../includes/misc/iis7DeleteRewriteRule.php | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php diff --git a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php new file mode 100644 index 0000000000000..d374aa106dc49 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php @@ -0,0 +1,127 @@ +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_delete_rewrite_rule() correctly deletes WordPress rules. + * + * @ticket 65170 + * + * @dataProvider data_iis7_delete_rewrite_rule + * + * @param string $initial_content The initial XML content of the file. + * @param string $expected_content The expected XML content after deletion. + */ + public function test_iis7_delete_rewrite_rule( $initial_content, $expected_content ) { + file_put_contents( $this->temp_file, $initial_content ); + + $result = iis7_delete_rewrite_rule( $this->temp_file ); + + $this->assertTrue( $result, 'iis7_delete_rewrite_rule() should return true on success.' ); + + if ( '' === $expected_content ) { + $this->assertStringEqualsFile( $this->temp_file, '', 'The file should be empty.' ); + } else { + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $expected_content, 'The XML content should match the expected output.' ); + } + } + + /** + * Data provider for test_iis7_delete_rewrite_rule. + * + * @return array[] + */ + public function data_iis7_delete_rewrite_rule(): array { + return array( + 'Rule with name "wordpress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Rule with name starting with "wordpress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Rule with name "WordPress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Multiple rules, only WordPress rule is deleted' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'WordPress rule is first among multiple rules' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'No WordPress rule exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Empty rules tag' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + ); + } + + /** + * Tests that iis7_delete_rewrite_rule() returns true if the file does not exist. + * + * @ticket 65170 + */ + public function test_iis7_delete_rewrite_rule_non_existent_file() { + $non_existent_file = dirname( $this->temp_file ) . '/non-existent-web.config'; + if ( file_exists( $non_existent_file ) ) { + unlink( $non_existent_file ); + } + + $this->assertTrue( iis7_delete_rewrite_rule( $non_existent_file ), 'Should return true if the file does not exist.' ); + } + + /** + * Tests that iis7_delete_rewrite_rule() returns false for invalid XML. + * + * @ticket 65170 + */ + public function test_iis7_delete_rewrite_rule_invalid_xml() { + file_put_contents( $this->temp_file, 'Not XML' ); + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $this->assertFalse( @iis7_delete_rewrite_rule( $this->temp_file ), 'Should return false for invalid XML.' ); + } +} From 55e5a48f5f1ab15a57af9b860ff481ae023f33b6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:24:55 -0400 Subject: [PATCH 2/4] Fix spacing in test case names for IIS7 rules white space --- .../admin/includes/misc/iis7DeleteRewriteRule.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php index d374aa106dc49..e948b9a747c9d 100644 --- a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php +++ b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php @@ -69,7 +69,7 @@ public function test_iis7_delete_rewrite_rule( $initial_content, $expected_conte */ public function data_iis7_delete_rewrite_rule(): array { return array( - 'Rule with name "wordpress" exists' => array( + 'Rule with name "wordpress" exists' => array( 'initial_content' => '', 'expected_content' => '', ), @@ -77,7 +77,7 @@ public function data_iis7_delete_rewrite_rule(): array { 'initial_content' => '', 'expected_content' => '', ), - 'Rule with name "WordPress" exists' => array( + 'Rule with name "WordPress" exists' => array( 'initial_content' => '', 'expected_content' => '', ), @@ -85,15 +85,15 @@ public function data_iis7_delete_rewrite_rule(): array { 'initial_content' => '', 'expected_content' => '', ), - 'WordPress rule is first among multiple rules' => array( + 'WordPress rule is first among multiple rules' => array( 'initial_content' => '', 'expected_content' => '', ), - 'No WordPress rule exists' => array( + 'No WordPress rule exists' => array( 'initial_content' => '', 'expected_content' => '', ), - 'Empty rules tag' => array( + 'Empty rules tag' => array( 'initial_content' => '', 'expected_content' => '', ), From de10b0d18335ee47c90e91af6ee85dce31c8a672 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:08:48 -0400 Subject: [PATCH 3/4] Add unit tests for iis7_delete_rewrite_rule() --- .../includes/misc/iis7DeleteRewriteRule.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php diff --git a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php new file mode 100644 index 0000000000000..c85c941baa002 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.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_delete_rewrite_rule() correctly deletes WordPress rules. + * + * @ticket 65170 + * + * @dataProvider data_iis7_delete_rewrite_rule + * + * @param string $initial_content The initial XML content of the file. + * @param string $expected_content The expected XML content after deletion. + */ + public function test_iis7_delete_rewrite_rule( $initial_content, $expected_content ) { + file_put_contents( $this->temp_file, $initial_content ); + + $result = iis7_delete_rewrite_rule( $this->temp_file ); + + $this->assertTrue( $result, 'iis7_delete_rewrite_rule() should return true on success.' ); + + if ( '' === $expected_content ) { + $this->assertStringEqualsFile( $this->temp_file, '', 'The file should be empty.' ); + } else { + $this->assertXmlStringEqualsXmlFile( $this->temp_file, $expected_content, 'The XML content should match the expected output.' ); + } + } + + /** + * Data provider for test_iis7_delete_rewrite_rule. + * + * @return array + */ + public function data_iis7_delete_rewrite_rule(): array { + return array( + 'Rule with name "wordpress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Rule with name starting with "wordpress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Rule with name "WordPress" exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Multiple rules, only WordPress rule is deleted' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'WordPress rule is first among multiple rules' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'No WordPress rule exists' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + 'Empty rules tag' => array( + 'initial_content' => '', + 'expected_content' => '', + ), + ); + } + + /** + * Tests that iis7_delete_rewrite_rule() returns true if the file does not exist. + * + * @ticket 65170 + */ + public function test_iis7_delete_rewrite_rule_non_existent_file() { + $non_existent_file = dirname( $this->temp_file ) . '/non-existent-web.config'; + if ( file_exists( $non_existent_file ) ) { + unlink( $non_existent_file ); + } + + $this->assertTrue( iis7_delete_rewrite_rule( $non_existent_file ), 'Should return true if the file does not exist.' ); + } + + /** + * Tests that iis7_delete_rewrite_rule() returns false for invalid XML. + * + * @ticket 65170 + */ + public function test_iis7_delete_rewrite_rule_invalid_xml() { + file_put_contents( $this->temp_file, 'Not XML' ); + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged + $this->assertFalse( @iis7_delete_rewrite_rule( $this->temp_file ), 'Should return false for invalid XML.' ); + } +} From 69d47d03a310f5ec2a49a6d6eb5519731049be7e Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 18:05:19 -0400 Subject: [PATCH 4/4] Fix spacing in test data for IIS7 delete rule --- .../admin/includes/misc/iis7DeleteRewriteRule.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php index c85c941baa002..3954001cef4f5 100644 --- a/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php +++ b/tests/phpunit/tests/admin/includes/misc/iis7DeleteRewriteRule.php @@ -72,7 +72,7 @@ public function test_iis7_delete_rewrite_rule( $initial_content, $expected_conte */ public function data_iis7_delete_rewrite_rule(): array { return array( - 'Rule with name "wordpress" exists' => array( + 'Rule with name "wordpress" exists' => array( 'initial_content' => '', 'expected_content' => '', ), @@ -80,7 +80,7 @@ public function data_iis7_delete_rewrite_rule(): array { 'initial_content' => '', 'expected_content' => '', ), - 'Rule with name "WordPress" exists' => array( + 'Rule with name "WordPress" exists' => array( 'initial_content' => '', 'expected_content' => '', ), @@ -88,15 +88,15 @@ public function data_iis7_delete_rewrite_rule(): array { 'initial_content' => '', 'expected_content' => '', ), - 'WordPress rule is first among multiple rules' => array( + 'WordPress rule is first among multiple rules' => array( 'initial_content' => '', 'expected_content' => '', ), - 'No WordPress rule exists' => array( + 'No WordPress rule exists' => array( 'initial_content' => '', 'expected_content' => '', ), - 'Empty rules tag' => array( + 'Empty rules tag' => array( 'initial_content' => '', 'expected_content' => '', ),