Repository navigation
Rewrite Rules: Send no-cache and noindex headers for random content that is not redirected #15825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rene-hermenau
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
rene-hermenau:66625-random-content-nocache
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−82
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
tests/phpunit/tests/query/randomContentRedirectHeaders.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for the HTTP headers sent on random content requests. | ||
| * | ||
| * @runTestsInSeparateProcesses | ||
| * @preserveGlobalState disabled | ||
| * | ||
| * @group query | ||
| * @group rewrite | ||
| * @group xdebug | ||
| * | ||
| * @covers ::wp_random_content_redirect | ||
| */ | ||
| class Tests_Query_RandomContentRedirectHeaders extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Message of the exception thrown in place of the `exit` following a redirect. | ||
| * | ||
| * @var string | ||
| */ | ||
| const REDIRECT_EXCEPTION_MESSAGE = 'Random content redirect.'; | ||
|
|
||
| /** | ||
| * Sets up pretty permalinks and stops redirects from ending the test run. | ||
| */ | ||
| public function set_up(): void { | ||
| parent::set_up(); | ||
|
|
||
| $this->set_permalink_structure( '/%postname%/' ); | ||
| self::factory()->post->create( array( 'post_status' => 'publish' ) ); | ||
| add_filter( | ||
| 'wp_redirect', | ||
| static function (): void { | ||
| throw new WP_Tests_Exception( self::REDIRECT_EXCEPTION_MESSAGE ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Requests a URL, stopping where a random content redirect ends the request. | ||
| * | ||
| * @param string $url The URL for the request. | ||
| */ | ||
| public function go_to( $url ) { | ||
| try { | ||
| parent::go_to( $url ); | ||
| } catch ( WP_Tests_Exception $e ) { | ||
| if ( self::REDIRECT_EXCEPTION_MESSAGE !== $e->getMessage() ) { | ||
| throw $e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensure random content redirects are neither cached nor indexed, whatever the permalink. | ||
| * | ||
| * @ticket 66625 | ||
| * | ||
| * @requires function xdebug_get_headers | ||
| * | ||
| * @dataProvider data_permalinks | ||
| * | ||
| * @param string|null $permalink Permalink of the selected post, or null to keep the real one. | ||
| */ | ||
| public function test_random_content_response_is_not_cached_or_indexed( ?string $permalink ): void { | ||
| if ( null !== $permalink ) { | ||
| add_filter( | ||
| 'post_link', | ||
| static function () use ( $permalink ): string { | ||
| return $permalink; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| $this->go_to( home_url( '/random/' ) ); | ||
|
|
||
| $headers = xdebug_get_headers(); | ||
|
|
||
| $this->assertContains( 'Cache-Control: ' . wp_get_nocache_headers()['Cache-Control'], $headers, 'The random content response should not be cached.' ); | ||
| $this->assertContains( 'X-Robots-Tag: noindex, follow', $headers, 'The random content response should not be indexed.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider. | ||
| * | ||
| * @return array<string, array{0: string|null}> | ||
| */ | ||
| public static function data_permalinks(): array { | ||
| return array( | ||
| 'permalink on this site' => array( null ), | ||
| 'permalink on another site' => array( 'https://elsewhere.example/post/' ), | ||
| 'empty permalink' => array( '' ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.