Make WordPress Core


Ignore:
Timestamp:
07/21/2020 12:55:20 AM (4 years ago)
Author:
whyisjake
Message:

Sitemaps: Ensure correct HTTP status when sitemaps are disabled

If sitemaps are disabled, previously there would be a rewrite rule for the sitemap endpoint. This endpoint would display the homepage since there was a rewrite rule. Now, Sitemaps are loaded, and the proper HTTP headers are returned.

Fixes #50643.
Props swissspidy, kraftbj, donmhico.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/sitemaps/sitemaps.php

    r48474 r48523  
    445445        $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not prefixed with "\n".' );
    446446    }
     447
     448    /**
     449     * @ticket 50643
     450     */
     451    public function test_sitemaps_enabled() {
     452        $before = wp_sitemaps_get_server()->sitemaps_enabled();
     453        add_filter( 'wp_sitemaps_enabled', '__return_false' );
     454        $after = wp_sitemaps_get_server()->sitemaps_enabled();
     455        remove_filter( 'wp_sitemaps_enabled', '__return_false' );
     456
     457        $this->assertTrue( $before );
     458        $this->assertFalse( $after );
     459    }
     460
     461    /**
     462     * @ticket 50643
     463     * @runInSeparateProcess
     464     * @preserveGlobalState disabled
     465     */
     466    public function test_disable_sitemap_should_return_404() {
     467        add_filter( 'wp_sitemaps_enabled', '__return_false' );
     468
     469        $this->go_to( home_url( '/?sitemap=index' ) );
     470
     471        wp_sitemaps_get_server()->render_sitemaps();
     472
     473        remove_filter( 'wp_sitemaps_enabled', '__return_false' );
     474
     475        $this->assertTrue( is_404() );
     476    }
     477
     478    /**
     479     * @ticket 50643
     480     * @runInSeparateProcess
     481     * @preserveGlobalState disabled
     482     */
     483    public function test_empty_url_list_should_return_404() {
     484        wp_register_sitemap( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
     485
     486        $this->go_to( home_url( '/?sitemap=foo' ) );
     487
     488        wp_sitemaps_get_server()->render_sitemaps();
     489
     490        $this->assertTrue( is_404() );
     491    }
    447492}
Note: See TracChangeset for help on using the changeset viewer.