diff --git a/tests/phpunit/tests/load/wpDoingSitemap.php b/tests/phpunit/tests/load/wpDoingSitemap.php new file mode 100644 index 0000000000000..a1ce10496575c --- /dev/null +++ b/tests/phpunit/tests/load/wpDoingSitemap.php @@ -0,0 +1,67 @@ +assertFalse( wp_doing_sitemap() ); + } + + /** + * The 'wp_doing_sitemap' filter should be able to force a true result. + * + * @ticket 56954 + */ + public function test_filter_can_force_true() { + add_filter( 'wp_doing_sitemap', '__return_true' ); + + $this->assertTrue( wp_doing_sitemap() ); + } + + /** + * The DOING_SITEMAP constant should make the function return true. + * + * Runs in a separate process because the constant cannot be undefined + * once it has been set. + * + * @ticket 56954 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_constant_defined_returns_true() { + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false before the constant is defined.' ); + + define( 'DOING_SITEMAP', true ); + + $this->assertTrue( wp_doing_sitemap(), 'wp_doing_sitemap() should be true once DOING_SITEMAP is defined.' ); + } + + /** + * The 'wp_doing_sitemap' filter should be able to override the constant. + * + * @ticket 56954 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_filter_overrides_constant() { + define( 'DOING_SITEMAP', true ); + + add_filter( 'wp_doing_sitemap', '__return_false' ); + + $this->assertFalse( wp_doing_sitemap() ); + } +} diff --git a/tests/phpunit/tests/sitemaps/sitemaps.php b/tests/phpunit/tests/sitemaps/sitemaps.php index 85f9965245842..e24c2d9bc7282 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps.php +++ b/tests/phpunit/tests/sitemaps/sitemaps.php @@ -493,4 +493,52 @@ public function test_empty_url_list_should_return_404() { $this->assertTrue( is_404() ); } + + /** + * Ensures rendering a sitemap defines the DOING_SITEMAP constant. + * + * @ticket 56954 + * + * @covers WP_Sitemaps::render_sitemaps + * @covers ::wp_doing_sitemap + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_render_sitemaps_defines_doing_sitemap() { + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false before a sitemap is rendered.' ); + + wp_register_sitemap_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) ); + + $this->go_to( home_url( '/?sitemap=foo' ) ); + + wp_sitemaps_get_server()->render_sitemaps(); + + $this->assertTrue( defined( 'DOING_SITEMAP' ), 'The DOING_SITEMAP constant should be defined.' ); + $this->assertTrue( wp_doing_sitemap(), 'wp_doing_sitemap() should be true while rendering a sitemap.' ); + } + + /** + * Ensures the DOING_SITEMAP constant is not defined when sitemaps are disabled. + * + * The constant is only defined once the request has passed the preliminary + * checks, so a disabled sitemap request should never reach that point. + * + * @ticket 56954 + * + * @covers WP_Sitemaps::render_sitemaps + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_render_sitemaps_does_not_define_doing_sitemap_when_disabled() { + add_filter( 'wp_sitemaps_enabled', '__return_false' ); + + $this->go_to( home_url( '/?sitemap=index' ) ); + + wp_sitemaps_get_server()->render_sitemaps(); + + $this->assertFalse( defined( 'DOING_SITEMAP' ), 'The DOING_SITEMAP constant should not be defined when sitemaps are disabled.' ); + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false when sitemaps are disabled.' ); + } }