From 5d883a10d23abf9583ae3806bc705a37e2c4522c Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 7 May 2026 11:56:00 -0400 Subject: [PATCH] Add unit tests for update_home_siteurl() in wp-admin/includes/misc.php --- .../admin/includes/misc/updateHomeSiteurl.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/updateHomeSiteurl.php diff --git a/tests/phpunit/tests/admin/includes/misc/updateHomeSiteurl.php b/tests/phpunit/tests/admin/includes/misc/updateHomeSiteurl.php new file mode 100644 index 0000000000000..a6d3d9dae8318 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/updateHomeSiteurl.php @@ -0,0 +1,112 @@ +assertFalse( $option_updated, 'Rewrite rules should not be flushed when installing.' ); + + wp_installing( false ); // Reset. + } + + /** + * Tests update_home_siteurl() in single site mode. + * + * @ticket 65194 + */ + public function test_update_home_siteurl_should_flush_rules_on_single_site() { + if ( is_multisite() ) { + $this->markTestSkipped( 'This test is for single site only.' ); + } + + $option_updated = false; + add_filter( 'pre_update_option_rewrite_rules', function( $value ) use ( &$option_updated ) { + $option_updated = true; + return $value; + } ); + + // flush_rewrite_rules() might not update the option if it's already empty or similar. + // Let's ensure there are rules to flush. + update_option( 'rewrite_rules', array( 'foo' => 'bar' ) ); + $option_updated = false; + + update_home_siteurl( 'http://old.example.com', 'http://new.example.com' ); + + $this->assertTrue( $option_updated, 'Rewrite rules should be flushed on single site.' ); + } + + /** + * Tests update_home_siteurl() in multisite mode when NOT switched. + * + * @group multisite + * @ticket 65194 + */ + public function test_update_home_siteurl_multisite_not_switched() { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'This test is for multisite only.' ); + } + + $option_updated = false; + add_filter( 'pre_update_option_rewrite_rules', function( $value ) use ( &$option_updated ) { + $option_updated = true; + return $value; + } ); + + update_option( 'rewrite_rules', array( 'foo' => 'bar' ) ); + $option_updated = false; + + update_home_siteurl( 'http://old.example.com', 'http://new.example.com' ); + + $this->assertTrue( $option_updated, 'Rewrite rules should be flushed in multisite when not switched.' ); + } + + /** + * Tests update_home_siteurl() in multisite mode when switched. + * + * @group multisite + * @ticket 65194 + */ + public function test_update_home_siteurl_multisite_switched() { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'This test is for multisite only.' ); + } + + $blog_id = $this->factory->blog->create(); + switch_to_blog( $blog_id ); + + update_option( 'rewrite_rules', array( 'foo' => 'bar' ) ); + + update_home_siteurl( 'http://old.example.com', 'http://new.example.com' ); + + $this->assertFalse( get_option( 'rewrite_rules' ), 'Rewrite rules option should be deleted when switched in multisite.' ); + + restore_current_blog(); + } +}