From 15bbfce1f06c1c345785e5b0b05da952b3d4f974 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 17:56:43 -0400 Subject: [PATCH 1/2] Add unit tests for iis7_save_url_rewrite_rules() in src/wp-admin/includes/misc.php --- .../includes/misc/iis7SaveUrlRewriteRules.php | 209 ++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php diff --git a/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php b/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php new file mode 100644 index 0000000000000..4889eb45b8fc2 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php @@ -0,0 +1,209 @@ +orig_home = get_option( 'home' ); + $this->orig_siteurl = get_option( 'siteurl' ); + $this->orig_script_filename = isset( $_SERVER['SCRIPT_FILENAME'] ) ? $_SERVER['SCRIPT_FILENAME'] : null; + $this->orig_iis_rewritten = isset( $_SERVER['IIS_WasUrlRewritten'] ) ? $_SERVER['IIS_WasUrlRewritten'] : null; + $this->orig_iis_url_rewrite = isset( $_SERVER['IIS_UrlRewriteModule'] ) ? $_SERVER['IIS_UrlRewriteModule'] : null; + $this->orig_is_iis7 = isset( $is_iis7 ) ? $is_iis7 : null; + + $this->temp_home = get_temp_dir() . 'wp_iis_test_home/'; + if ( ! is_dir( $this->temp_home ) ) { + mkdir( $this->temp_home ); + } + } + + public function tear_down() { + if ( $this->temp_home && is_dir( $this->temp_home ) ) { + $this->recursive_delete( $this->temp_home ); + } + + global $is_iis7; + + update_option( 'home', $this->orig_home ); + update_option( 'siteurl', $this->orig_siteurl ); + if ( null !== $this->orig_script_filename ) { + $_SERVER['SCRIPT_FILENAME'] = $this->orig_script_filename; + } else { + unset( $_SERVER['SCRIPT_FILENAME'] ); + } + if ( null !== $this->orig_iis_rewritten ) { + $_SERVER['IIS_WasUrlRewritten'] = $this->orig_iis_rewritten; + } else { + unset( $_SERVER['IIS_WasUrlRewritten'] ); + } + if ( null !== $this->orig_iis_url_rewrite ) { + $_SERVER['IIS_UrlRewriteModule'] = $this->orig_iis_url_rewrite; + } else { + unset( $_SERVER['IIS_UrlRewriteModule'] ); + } + if ( null !== $this->orig_is_iis7 ) { + $is_iis7 = $this->orig_is_iis7; + } + + parent::tear_down(); + } + + /** + * Helper to delete a directory and its contents. + * + * @param string $dir Path to the directory. + */ + private function recursive_delete( $dir ) { + foreach ( scandir( $dir ) as $file ) { + if ( '.' === $file || '..' === $file ) { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $file; + if ( is_dir( $path ) ) { + $this->recursive_delete( $path ); + } else { + unlink( $path ); + } + } + rmdir( $dir ); + } + + /** + * Tests that iis7_save_url_rewrite_rules() correctly writes rules to web.config. + * + * @ticket 65147 + */ + public function test_iis7_save_url_rewrite_rules_success() { + global $wp_rewrite, $is_iis7; + + // Simulate IIS7 with permalink support. + $is_iis7 = true; + $_SERVER['IIS_UrlRewriteModule'] = '1'; + $_SERVER['SCRIPT_FILENAME'] = $this->temp_home . 'index.php'; + + // Update home/siteurl to match the temp directory. + update_option( 'home', 'http://localhost' ); + update_option( 'siteurl', 'http://localhost' ); + + // We MUST define ABSPATH to be consistent with our temp home if possible, + // but ABSPATH is a constant. get_home_path() uses ABSPATH if home/siteurl are same. + // So we make sure they are NOT same so it uses SCRIPT_FILENAME logic. + update_option( 'siteurl', 'http://localhost/wp' ); + $_SERVER['SCRIPT_FILENAME'] = $this->temp_home . 'wp/index.php'; + if ( ! is_dir( $this->temp_home . 'wp' ) ) { + mkdir( $this->temp_home . 'wp' ); + } + + // Mock iis7_supports_permalinks() because we are not on cgi-fcgi SAPI. + add_filter( 'iis7_supports_permalinks', '__return_true' ); + + // Setup permalinks. + $orig_structure = $wp_rewrite->permalink_structure; + $wp_rewrite->set_permalink_structure( '/%postname%/' ); + + $web_config_file = $this->temp_home . 'web.config'; + // We don't touch the file here, let the function create it or handle it. + + $result = iis7_save_url_rewrite_rules(); + + $wp_rewrite->set_permalink_structure( $orig_structure ); + remove_filter( 'iis7_supports_permalinks', '__return_true' ); + + $this->assertTrue( $result, 'iis7_save_url_rewrite_rules() should return true on success.' ); + $this->assertFileExists( $web_config_file ); + + $content = file_get_contents( $web_config_file ); + // The rule name generated by $wp_rewrite->iis7_url_rewrite_rules() typically starts with "WordPress". + $this->assertStringContainsString( 'name="WordPress', $content ); + $this->assertStringContainsString( 'assertFalse( $result, 'iis7_save_url_rewrite_rules() should return false when not on IIS.' ); + } + + /** + * Tests that iis7_save_url_rewrite_rules() returns null in multisite. + * + * @ticket 65147 + */ + public function test_iis7_save_url_rewrite_rules_multisite() { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'This test requires multisite.' ); + } + + $this->assertNull( iis7_save_url_rewrite_rules() ); + } +} From 6ec66d6188afafc5175e34f4dc470b24268f6b0c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 28 Apr 2026 18:11:40 -0400 Subject: [PATCH 2/2] Fix formatting in iis7SaveUrlRewriteRules.php test --- .../tests/admin/includes/misc/iis7SaveUrlRewriteRules.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php b/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php index 4889eb45b8fc2..2c2cfea3aad79 100644 --- a/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php +++ b/tests/phpunit/tests/admin/includes/misc/iis7SaveUrlRewriteRules.php @@ -138,9 +138,9 @@ public function test_iis7_save_url_rewrite_rules_success() { global $wp_rewrite, $is_iis7; // Simulate IIS7 with permalink support. - $is_iis7 = true; + $is_iis7 = true; $_SERVER['IIS_UrlRewriteModule'] = '1'; - $_SERVER['SCRIPT_FILENAME'] = $this->temp_home . 'index.php'; + $_SERVER['SCRIPT_FILENAME'] = $this->temp_home . 'index.php'; // Update home/siteurl to match the temp directory. update_option( 'home', 'http://localhost' );