Make WordPress Core

Changeset 65042


Ignore:
Timestamp:
10/30/2026 12:59:06 AM (11 days ago)
Author:
peterwilsoncc
Message:

Rewrites: Reinstate split the query for random redirects.

Removes the filtering of split_the_query to false on random post redirects. As sorting queries randomly generates a temporary table, selecting * places all data in the temporary table, whereas selecting ID only places the post ID in the temporary table. The unsplit query can become inefficient on sites with a relatively few posts (ie, thousands not millions).

This removes the function wp_random_content_split_the_query() with a hard deprecation as it was not released to a production version of WordPress.

Additionally, the following changes have been made to the test suite:

  • Tests ensuring random redirects are not cached are refactored to avoid the $this->go_to() method in the test suite as the helper method flushes the cache, causing the tests to be invalid
  • Introduces tests covering WP::handle_random() specifically
  • Adds docblocks to each and every test in Tests_Query_RandomContentRedirect

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14809.

Follow-up to r63767, r63906, r64402.

Props aryanjasala, peterwilsoncc, dhrupo, westonruter.
Fixes #64498.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r64691 r65042  
    497497
    498498// Random content redirects, see WP::handle_random().
    499 add_filter( 'split_the_query', 'wp_random_content_split_the_query', 10, 2 );
    500499add_action( 'wp_after_insert_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 4 );
    501500add_action( 'after_delete_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 2 );
  • trunk/src/wp-includes/query.php

    r64402 r65042  
    14631463
    14641464/**
    1465  * Prevents the random content query from being split into multiple database queries.
    1466  *
    1467  * Randomly ordered queries are not cached, so there is no benefit to querying the post
    1468  * IDs separately to the post objects. Disabling the split allows the random post to be
    1469  * retrieved in a single database query.
    1470  *
    1471  * @since 7.2.0
    1472  *
    1473  * @param bool     $split_the_query Whether or not to split the query.
    1474  * @param WP_Query $query           The WP_Query instance.
    1475  * @return bool Whether or not to split the query.
    1476  */
    1477 function wp_random_content_split_the_query( $split_the_query, WP_Query $query ): bool {
    1478         if ( wp_is_random_content_query( $query ) ) {
    1479                 return false;
    1480         }
    1481 
    1482         return (bool) $split_the_query;
    1483 }
    1484 
    1485 /**
    14861465 * Redirects random content requests to the randomly selected post.
    14871466 *
  • trunk/tests/phpunit/tests/query/randomContentRedirect.php

    r65030 r65042  
    1616 * @covers ::wp_get_random_content_post_types
    1717 * @covers ::wp_random_content_flush_rewrite_rules_for_post
    18  * @covers ::wp_random_content_split_the_query
    1918 * @covers ::wp_is_random_content_redirect_enabled
    2019 */
    … …  
    9897        protected $recording_queries = false;
    9998
     99        /**
     100         * Creates shared user, category and post fixtures.
     101         *
     102         * @param WP_UnitTest_Factory $factory Test factory.
     103         */
    100104        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
    101105                self::$editor = $factory->user->create(
    … …  
    133137        }
    134138
     139        /**
     140         * Sets up pretty permalinks and redirect capturing before each test.
     141         */
    135142        public function set_up(): void {
    136143                parent::set_up();
    … …  
    150157        }
    151158
     159        /**
     160         * Unregisters the test post types and taxonomy after each test.
     161         */
    152162        public function tear_down(): void {
    153163                unregister_post_type( 'wptests_book' );
    … …  
    166176         * @return false
    167177         */
    168         public function filter_wp_redirect( $location, $status ) {
     178        public function filter_wp_redirect( string $location, int $status ) {
    169179                $this->redirect_location = $location;
    170180                $this->redirect_status   = $status;
    … …  
    187197         * @return string Unmodified database query.
    188198         */
    189         public function record_query( $query ) {
     199        public function record_query( string $query ) {
    190200                if ( $this->recording_queries ) {
    191201                        $this->queries[] = $query;
    … …  
    200210         * @param string $url URL to request.
    201211         */
    202         protected function go_to_and_record_queries( $url ): void {
     212        protected function go_to_and_record_queries( string $url ): void {
    203213                add_action( 'parse_request', array( $this, 'start_recording_queries' ), PHP_INT_MAX );
    204214                add_filter( 'query', array( $this, 'record_query' ) );
    … …  
    247257         * @param int[]  $post_ids Allowed post IDs.
    248258         * @param string $message  Message to display on failure.
    249 
    250259         *
    251260         * @phpstan-assert !null $this->redirect_location
    … …  
    283292
    284293        /**
     294         * Ensure random content URLs redirect to a published post.
     295         *
    285296         * @dataProvider data_random_urls
    286297         *
    287298         * @param string $path Random content request path.
    288299         */
    289         public function test_random_url_redirects_to_a_published_post( $path ): void {
     300        public function test_random_url_redirects_to_a_published_post( string $path ): void {
    290301                $this->go_to( home_url( $path ) );
    291302
    … …  
    294305
    295306        /**
    296          * Data provider.
    297          *
    298          * @return array<string, string[]>
     307         * Data provider for {@see self::test_random_url_redirects_to_a_published_post()}.
     308         *
     309         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    299310         */
    300311        public static function data_random_urls(): array {
    … …  
    308319        }
    309320
     321        /**
     322         * Ensure redirect uses HTTP 302 Temporary Redirect status.
     323         */
    310324        public function test_random_redirect_uses_a_temporary_redirect(): void {
    311325                $this->go_to( home_url( '/random/' ) );
    … …  
    314328        }
    315329
     330        /**
     331         * Ensure redirect location header matches the permalink of the selected post.
     332         */
    316333        public function test_random_redirect_location_is_the_permalink(): void {
    317334                $this->go_to( home_url( '/random/category/arts/' ) );
    … …  
    320337        }
    321338
     339        /**
     340         * Ensure random redirects work with plain permalinks.
     341         */
    322342        public function test_random_query_string_redirects_with_plain_permalinks(): void {
    323343                $this->set_permalink_structure( '' );
    … …  
    329349        }
    330350
     351        /**
     352         * Ensure random redirects work with pathinfo (/index.php/...) permalinks.
     353         */
    331354        public function test_random_redirect_with_pathinfo_permalinks(): void {
    332355                $this->set_permalink_structure( '/index.php/%postname%/' );
    … …  
    337360        }
    338361
     362        /**
     363         * Ensure random redirects respect the selected category.
     364         */
    339365        public function test_random_category_url_only_selects_posts_in_the_category(): void {
    340366                $this->go_to( home_url( '/random/category/sport/' ) );
    … …  
    343369        }
    344370
     371        /**
     372         * Ensure random redirects respect the selected nested category.
     373         */
    345374        public function test_random_child_category_url_selects_posts_in_the_child_category(): void {
    346375                $child   = self::factory()->category->create(
    … …  
    357386        }
    358387
     388        /**
     389         * Ensure random redirects include posts in the selected category's sub-categories.
     390         */
    359391        public function test_random_category_url_includes_posts_in_child_categories(): void {
    360392                $child      = self::factory()->category->create(
    … …  
    371403        }
    372404
     405        /**
     406         * Ensure random redirects respect the `?cat` URL parameter.
     407         */
    373408        public function test_random_cat_query_string_only_selects_posts_in_the_category(): void {
    374409                $this->go_to( add_query_arg( 'cat', self::$arts_category, home_url( '/?random' ) ) );
    … …  
    377412        }
    378413
     414        /**
     415         * Ensure random redirects respect the selected tag.
     416         */
    379417        public function test_random_tag_url_only_selects_posts_with_the_tag(): void {
    380418                $this->go_to( home_url( '/random/tag/impressionism/' ) );
    … …  
    383421        }
    384422
     423        /**
     424         * Ensure random redirects respect URLs specifying terms from multiple taxonomies.
     425         */
    385426        public function test_random_combined_taxonomy_query_selects_posts_matching_all_terms(): void {
    386427                $impressionist_sport = $this->create_post(
    … …  
    397438        }
    398439
     440        /**
     441         * Ensure random redirects respect the selected author.
     442         */
    399443        public function test_random_author_url_only_selects_posts_by_the_author(): void {
    400444                $this->go_to( home_url( '/random/author/random-editor/' ) );
    … …  
    403447        }
    404448
     449        /**
     450         * Ensure random redirects respect the selected date.
     451         */
    405452        public function test_random_date_archive_only_selects_posts_from_the_date(): void {
    406453                $this->go_to( home_url( '/1844/?random' ) );
    … …  
    409456        }
    410457
     458        /**
     459         * Ensure random redirects respect the selected search query.
     460         */
    411461        public function test_random_search_only_selects_matching_posts(): void {
    412462                $this->go_to( home_url( '/?s=Baseball&random' ) );
    … …  
    415465        }
    416466
     467        /**
     468         * Ensure random redirects respect the selected post type.
     469         */
    417470        public function test_random_post_type_archive_url_only_selects_posts_of_the_type(): void {
    418471                register_post_type(
    … …  
    432485        }
    433486
     487        /**
     488         * Ensure random redirects respect the randomable post type parameter regardless of archive setting.
     489         */
    434490        public function test_random_post_type_rewrite_slug_url_selects_posts_of_a_randomable_type_without_an_archive(): void {
    435491                register_post_type(
    … …  
    451507        }
    452508
     509        /**
     510         * Ensure random redirects respect the selected post type using plain permalinks.
     511         */
    453512        public function test_random_post_type_query_string_selects_posts_of_the_type(): void {
    454513                register_post_type(
    … …  
    471530         */
    472531
     532        /**
     533         * Ensure pages are not randomable.
     534         */
    473535        public function test_no_redirect_for_pages(): void {
    474536                $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    479541        }
    480542
     543        /**
     544         * Ensure main query for pages is not modified by random query string parameter.
     545         */
    481546        public function test_main_query_is_unchanged_for_pages(): void {
    482547                $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    487552        }
    488553
     554        /**
     555         * Ensure pages are randomable if registration arguments enable the feature.
     556         */
    489557        public function test_pages_redirect_when_made_randomable(): void {
    490558                add_filter( 'register_post_type_args', array( $this, 'filter_make_pages_randomable' ), 10, 2 );
    … …  
    513581        }
    514582
     583        /**
     584         * Ensure post types registered as not randomable do not redirect.
     585         */
    515586        public function test_no_redirect_for_post_type_that_is_not_randomable(): void {
    516587                register_post_type(
    … …  
    529600        }
    530601
     602        /**
     603         * Ensure archives of post types registered as not randomable do not redirect.
     604         */
    531605        public function test_no_redirect_for_post_type_archive_that_is_not_randomable(): void {
    532606                register_post_type(
    … …  
    548622        }
    549623
     624        /**
     625         * Ensure only randomable post types are selected when multiple post types are requested.
     626         */
    550627        public function test_random_redirect_with_multiple_post_types_only_selects_randomable_types(): void {
    551628                register_post_type(
    … …  
    565642        }
    566643
     644        /**
     645         * Ensure random searches do not select pages.
     646         */
    567647        public function test_random_search_only_selects_randomable_post_types(): void {
    568648                $this->create_post(
    … …  
    578658        }
    579659
     660        /**
     661         * Ensure random searches include randomable custom post types.
     662         */
    580663        public function test_random_search_includes_randomable_custom_post_types(): void {
    581664                register_post_type(
    … …  
    599682        }
    600683
     684        /**
     685         * Ensure random custom taxonomy archives only select randomable post types.
     686         */
    601687        public function test_random_custom_taxonomy_archive_only_selects_randomable_post_types(): void {
    602688                register_post_type(
    … …  
    627713        }
    628714
     715        /**
     716         * Ensure archives of taxonomies used only by pages do not redirect.
     717         */
    629718        public function test_no_redirect_for_taxonomy_used_only_by_pages(): void {
    630719                register_taxonomy(
    … …  
    648737
    649738        /**
     739         * Ensure the eligible post types match the request.
     740         *
    650741         * @dataProvider data_wp_get_random_content_post_types
    651742         *
    … …  
    653744         * @param string[] $expected Expected post types.
    654745         */
    655         public function test_wp_get_random_content_post_types( $url, $expected ): void {
     746        public function test_wp_get_random_content_post_types( string $url, array $expected ): void {
    656747                register_post_type(
    657748                        'wptests_book',
    … …  
    682773
    683774        /**
    684          * Data provider.
    685          *
    686          * @return array<string, array{0: string, 1: string[]}>
     775         * Data provider for {@see self::test_wp_get_random_content_post_types()}.
     776         *
     777         * @return array<non-falsy-string, array{ 0: non-falsy-string, 1: list<non-falsy-string> }>
    687778         */
    688779        public static function data_wp_get_random_content_post_types(): array {
    … …  
    701792        }
    702793
     794        /**
     795         * Ensure the random URL only selects posts by default.
     796         */
    703797        public function test_random_url_defaults_to_posts(): void {
    704798                $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    709803        }
    710804
     805        /**
     806         * Ensure the random URL redirects to a post when a static front page is set.
     807         */
    711808        public function test_random_url_with_static_front_page_redirects_to_a_post(): void {
    712809                $front = $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    719816        }
    720817
     818        /**
     819         * Ensure pagination does not affect the random selection.
     820         */
    721821        public function test_random_redirect_ignores_pagination(): void {
    722822                $this->go_to( home_url( '/random/category/arts/?paged=5' ) );
    … …  
    730830
    731831        /**
     832         * Ensure unpublished and password protected posts are not selected, even for administrators.
     833         *
    732834         * @dataProvider data_ineligible_posts
    733835         *
    734836         * @param array<string, string> $args Post arguments.
    735837         */
    736         public function test_random_redirect_excludes_ineligible_posts( $args ): void {
     838        public function test_random_redirect_excludes_ineligible_posts( array $args ): void {
    737839                $category = self::factory()->category->create();
    738840                $this->create_post( array_merge( array( 'post_category' => array( $category ) ), $args ) );
    … …  
    746848
    747849        /**
    748          * Data provider.
    749          *
    750          * @return array<string, array{0: array<string, string>}>
     850         * Data provider for {@see self::test_random_redirect_excludes_ineligible_posts()}.
     851         *
     852         * @return array<non-falsy-string, array{ 0: array<non-falsy-string, string> }>
    751853         */
    752854        public static function data_ineligible_posts(): array {
    … …  
    767869
    768870        /**
     871         * Ensure sticky posts are not prepended to the random selection.
     872         *
    769873         * @global wpdb $wpdb WordPress database abstraction object.
    770874         */
    … …  
    806910         */
    807911
     912        /**
     913         * Ensure the home page does not redirect without the random query variable.
     914         */
    808915        public function test_no_redirect_without_random_query_var(): void {
    809916                $this->go_to( home_url( '/' ) );
    … …  
    813920        }
    814921
     922        /**
     923         * Ensure archives are not randomized without the random query variable.
     924         */
    815925        public function test_archives_are_unchanged_without_random_query_var(): void {
    816926                $this->go_to( home_url( '/category/arts/' ) );
    … …  
    820930        }
    821931
     932        /**
     933         * Ensure singular posts do not redirect with the random query variable.
     934         */
    822935        public function test_no_redirect_for_singular_post_with_random_query_var(): void {
    823936                $this->go_to( add_query_arg( 'random', '', get_permalink( self::$arts_post ) ) );
    … …  
    828941        }
    829942
     943        /**
     944         * Ensure requests for a post ID do not redirect with the random query variable.
     945         */
    830946        public function test_no_redirect_for_post_id_with_random_query_var(): void {
    831947                $this->go_to( home_url( '/?random&p=' . self::$sport_post ) );
    … …  
    835951        }
    836952
     953        /**
     954         * Ensure pages do not redirect with the random query variable.
     955         */
    837956        public function test_no_redirect_for_page_with_random_query_var(): void {
    838957                $page = $this->create_post(
    … …  
    849968        }
    850969
     970        /**
     971         * Ensure the static front page does not redirect with the random query variable.
     972         */
    851973        public function test_no_redirect_for_static_front_page_with_random_query_var_from_extra_vars(): void {
    852974                $front = $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    861983
    862984        /**
     985         * Ensure feeds do not redirect with the random query variable.
     986         *
    863987         * @dataProvider data_feed_urls
    864988         *
    865989         * @param string $path Feed path.
    866990         */
    867         public function test_no_redirect_for_feeds( $path ): void {
     991        public function test_no_redirect_for_feeds( string $path ): void {
    868992                $this->go_to( home_url( $path ) );
    869993
    … …  
    873997
    874998        /**
    875          * Data provider.
    876          *
    877          * @return array<string, string[]>
     999         * Data provider for {@see self::test_no_redirect_for_feeds()}.
     1000         *
     1001         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    8781002         */
    8791003        public static function data_feed_urls(): array {
    … …  
    8851009        }
    8861010
     1011        /**
     1012         * Ensure embeds do not redirect with the random query variable.
     1013         */
    8871014        public function test_no_redirect_for_embeds(): void {
    8881015                $this->go_to( home_url( '/cassatt/embed/?random' ) );
    … …  
    8921019        }
    8931020
     1021        /**
     1022         * Ensure random requests for a nonexistent category return a 404 rather than redirecting.
     1023         */
    8941024        public function test_no_redirect_for_nonexistent_category(): void {
    8951025                $this->go_to( home_url( '/random/category/does-not-exist/' ) );
    … …  
    8991029        }
    9001030
     1031        /**
     1032         * Ensure random requests for an empty category return a 404 rather than redirecting.
     1033         */
    9011034        public function test_no_redirect_for_empty_category(): void {
    9021035                $this->go_to( home_url( '/random/category/empty/' ) );
    … …  
    9061039        }
    9071040
     1041        /**
     1042         * Ensure random requests without eligible posts return a 404 rather than redirecting.
     1043         */
    9081044        public function test_no_redirect_when_no_posts_are_eligible(): void {
    9091045                add_filter(
    … …  
    9231059
    9241060        /**
     1061         * Ensure requests using HTTP methods other than GET or HEAD do not redirect.
     1062         *
    9251063         * @dataProvider data_non_redirecting_http_methods
    9261064         *
    9271065         * @param string $method HTTP request method.
    9281066         */
    929         public function test_no_redirect_for_non_get_requests( $method ): void {
     1067        public function test_no_redirect_for_non_get_requests( string $method ): void {
    9301068                $_SERVER['REQUEST_METHOD'] = $method;
    9311069
    … …  
    9361074
    9371075        /**
    938          * Data provider.
    939          *
    940          * @return array<string, string[]>
     1076         * Data provider for {@see self::test_no_redirect_for_non_get_requests()}.
     1077         *
     1078         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    9411079         */
    9421080        public static function data_non_redirecting_http_methods(): array {
    … …  
    9491087        }
    9501088
     1089        /**
     1090         * Ensure requests without an HTTP method do not redirect.
     1091         */
    9511092        public function test_no_redirect_without_request_method(): void {
    9521093                unset( $_SERVER['REQUEST_METHOD'] );
    … …  
    9581099
    9591100        /**
     1101         * Ensure GET and HEAD requests redirect, regardless of case.
     1102         *
    9601103         * @dataProvider data_redirecting_http_methods
    9611104         *
    9621105         * @param string $method HTTP request method.
    9631106         */
    964         public function test_redirect_for_get_and_head_requests( $method ): void {
     1107        public function test_redirect_for_get_and_head_requests( string $method ): void {
    9651108                $_SERVER['REQUEST_METHOD'] = $method;
    9661109
    … …  
    9711114
    9721115        /**
    973          * Data provider.
    974          *
    975          * @return array<string, string[]>
     1116         * Data provider for {@see self::test_redirect_for_get_and_head_requests()}.
     1117         *
     1118         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    9761119         */
    9771120        public static function data_redirecting_http_methods(): array {
    … …  
    9971140         * @param string $structure Permalink structure.
    9981141         */
    999         public function test_page_with_slug_beginning_with_random_base_does_not_conflict( $structure ): void {
     1142        public function test_page_with_slug_beginning_with_random_base_does_not_conflict( string $structure ): void {
    10001143                global $wp;
    10011144
    … …  
    10231166
    10241167        /**
     1168         * Ensure the random URL redirects when a page slug begins with the random base.
     1169         *
    10251170         * @dataProvider data_permalink_structures
    10261171         *
    … …  
    10291174         * @param string $structure Permalink structure.
    10301175         */
    1031         public function test_random_url_redirects_when_a_page_slug_begins_with_random_base( $structure ): void {
     1176        public function test_random_url_redirects_when_a_page_slug_begins_with_random_base( string $structure ): void {
    10321177                global $wp_rewrite;
    10331178
    … …  
    10571202         * @param string $structure Permalink structure.
    10581203         */
    1059         public function test_page_named_random_takes_precedence_over_random_content_redirects( $structure ): void {
     1204        public function test_page_named_random_takes_precedence_over_random_content_redirects( string $structure ): void {
    10601205                global $wp_rewrite;
    10611206
    … …  
    10801225
    10811226        /**
    1082          * Data provider.
    1083          *
    1084          * @return array<string, string[]>
     1227         * Data provider for {@see self::test_page_with_slug_beginning_with_random_base_does_not_conflict()}, {@see self::test_random_url_redirects_when_a_page_slug_begins_with_random_base()},
     1228         * and {@see self::test_page_named_random_takes_precedence_over_random_content_redirects()}.
     1229         *
     1230         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    10851231         */
    10861232        public static function data_permalink_structures(): array {
    … …  
    10931239        }
    10941240
     1241        /**
     1242         * Ensure random archives redirect when a page named "random" exists.
     1243         */
    10951244        public function test_random_archives_redirect_when_a_page_named_random_exists(): void {
    10961245                $this->create_post(
    … …  
    11061255        }
    11071256
     1257        /**
     1258         * Ensure the random query string redirects when a page named "random" exists.
     1259         */
    11081260        public function test_random_query_string_redirects_when_a_page_named_random_exists(): void {
    11091261                $this->create_post(
    … …  
    11191271        }
    11201272
     1273        /**
     1274         * Ensure a draft page named "random" does not take precedence over random content redirects.
     1275         */
    11211276        public function test_draft_page_named_random_does_not_take_precedence(): void {
    11221277                $this->create_post(
    … …  
    11341289
    11351290        /**
     1291         * Ensure random content redirects resume once a page named "random" no longer conflicts.
     1292         *
    11361293         * @dataProvider data_page_named_random_changes
    11371294         *
    11381295         * @param callable $change Callback to change the page so it no longer conflicts.
    11391296         */
    1140         public function test_random_content_redirects_resume_when_the_page_no_longer_conflicts( $change ): void {
     1297        public function test_random_content_redirects_resume_when_the_page_no_longer_conflicts( callable $change ): void {
    11411298                $page = $this->create_post(
    11421299                        array(
    … …  
    11561313
    11571314        /**
    1158          * Data provider.
    1159          *
    1160          * @return array<string, callable[]>
     1315         * Data provider for {@see self::test_random_content_redirects_resume_when_the_page_no_longer_conflicts()}.
     1316         *
     1317         * @return array<non-falsy-string, array{ 0: Closure(int): void }>
    11611318         */
    11621319        public static function data_page_named_random_changes(): array {
    … …  
    12051362        }
    12061363
     1364        /**
     1365         * Ensure publishing a draft page named "random" takes precedence over random content redirects.
     1366         */
    12071367        public function test_publishing_a_draft_page_named_random_takes_precedence(): void {
    12081368                $page = $this->create_post(
    … …  
    12221382        }
    12231383
     1384        /**
     1385         * Ensure saving pages and posts not at the random base does not flush the rewrite rules.
     1386         */
    12241387        public function test_saving_unrelated_pages_does_not_flush_rewrite_rules(): void {
    12251388                $flushes = new MockAction();
    … …  
    12481411         */
    12491412
     1413        /**
     1414         * Ensure random content requests do not redirect when the feature is disabled.
     1415         */
    12501416        public function test_no_redirect_when_disabled(): void {
    12511417                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    12571423        }
    12581424
     1425        /**
     1426         * Ensure the main query is not randomized or limited when the feature is disabled.
     1427         */
    12591428        public function test_main_query_is_unchanged_when_disabled(): void {
    12601429                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    12661435        }
    12671436
     1437        /**
     1438         * Ensure the random URL is not routed when the feature is disabled.
     1439         */
    12681440        public function test_random_url_is_not_routed_when_disabled(): void {
    12691441                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    12801452         */
    12811453
     1454        /**
     1455         * Ensure the `pre_get_posts` action can change the eligible post type.
     1456         */
    12821457        public function test_pre_get_posts_can_change_the_eligible_post_type(): void {
    12831458                register_post_type(
    … …  
    13041479        }
    13051480
     1481        /**
     1482         * Ensure the `pre_get_posts` action cannot add non-randomable post types.
     1483         */
    13061484        public function test_pre_get_posts_cannot_add_non_randomable_post_types(): void {
    13071485                $this->create_post( array( 'post_type' => 'page' ) );
    … …  
    13211499        }
    13221500
     1501        /**
     1502         * Ensure the `pre_get_posts` action can exclude posts from the random selection.
     1503         */
    13231504        public function test_pre_get_posts_can_exclude_posts(): void {
    13241505                add_action(
    … …  
    13361517        }
    13371518
     1519        /**
     1520         * Ensure theme customizations of the archive order and limit do not override the random query.
     1521         */
    13381522        public function test_pre_get_posts_archive_customizations_do_not_override_the_random_order(): void {
    13391523                // Typical theme customization of the main query for archives.
    … …  
    13571541        }
    13581542
     1543        /**
     1544         * Ensure secondary queries are not random content queries.
     1545         */
    13591546        public function test_secondary_queries_are_not_random_content_queries(): void {
    13601547                $this->go_to( home_url( '/random/' ) );
    … …  
    13661553        }
    13671554
     1555        /**
     1556         * Ensure the main query for the random URL is a random content query.
     1557         */
    13681558        public function test_wp_is_random_content_query_for_main_query(): void {
    13691559                $this->go_to( home_url( '/random/' ) );
    … …  
    13731563
    13741564        /**
     1565         * Ensure the `is_random()` conditional tag matches the request.
     1566         *
    13751567         * @dataProvider data_is_random
    13761568         *
    … …  
    13811573         * @param bool   $expected Expected result.
    13821574         */
    1383         public function test_is_random( $path, $expected ): void {
     1575        public function test_is_random( string $path, bool $expected ): void {
    13841576                $this->go_to( home_url( $path ) );
    13851577
    … …  
    13891581
    13901582        /**
    1391          * Data provider.
    1392          *
    1393          * @return array<string, array{0: string, 1: bool}>
     1583         * Data provider for {@see self::test_is_random()}.
     1584         *
     1585         * @return array<non-falsy-string, array{ 0: non-falsy-string, 1: bool }>
    13941586         */
    13951587        public static function data_is_random(): array {
    … …  
    14101602
    14111603        /**
     1604         * Ensure secondary queries with the random query variable are for random content.
     1605         *
    14121606         * @covers WP_Query::is_random
    14131607         */
    … …  
    14201614
    14211615        /**
     1616         * Ensure the query is not for random content once it is set to a 404.
     1617         *
    14221618         * @covers WP_Query::is_random
    14231619         */
    … …  
    14311627
    14321628        /**
     1629         * Ensure the `is_random()` conditional tag returns false before the query is run.
     1630         *
    14331631         * @covers ::is_random
    14341632         *
    … …  
    14411639        }
    14421640
     1641        /**
     1642         * Ensure the main query is not randomized in the admin.
     1643         */
    14431644        public function test_main_query_is_not_randomized_in_the_admin(): void {
    14441645                set_current_screen( 'edit.php' );
    … …  
    14551656        }
    14561657
     1658        /**
     1659         * Ensure the request redirects when the `the_posts` filter returns a non-list array.
     1660         */
    14571661        public function test_redirect_when_the_posts_filter_returns_a_non_list_array(): void {
    14581662                add_filter(
    … …  
    14681672        }
    14691673
     1674        /**
     1675         * Ensure the request does not redirect when the permalink is empty.
     1676         */
    14701677        public function test_no_redirect_when_the_permalink_is_empty(): void {
    14711678                add_filter( 'post_link', '__return_empty_string' );
    … …  
    14761683        }
    14771684
     1685        /**
     1686         * Ensure the redirect happens before the `wp` action fires.
     1687         */
    14781688        public function test_redirect_happens_before_the_wp_action(): void {
    14791689                $wp = new MockAction();
    … …  
    14991709         */
    15001710
    1501         public function test_random_post_is_selected_with_a_single_database_query(): void {
     1711        /**
     1712         * The random order is applied to post IDs only, so the database sorts IDs rather than
     1713         * full rows, including post content, of every eligible post.
     1714         *
     1715         * @global wpdb $wpdb WordPress database abstraction object.
     1716         */
     1717        public function test_random_post_is_selected_by_a_single_random_id_query(): void {
     1718                global $wpdb;
     1719
    15021720                $this->go_to_and_record_queries( home_url( '/random/' ) );
    15031721
    1504                 $this->assertNotNull( $this->redirect_location, 'The single database query request did not redirect.' );
    1505                 $this->assertCount( 1, $this->queries, "Only one database query should be made to select the random post:\n" . implode( "\n", $this->queries ) );
    1506                 $this->assertStringContainsString( 'RAND()', $this->queries[0], 'The random query should be the only query made.' );
    1507         }
    1508 
    1509         /**
     1722                $this->assertNotNull( $this->redirect_location, 'The request did not redirect.' );
     1723
     1724                $random_queries = array_values( preg_grep( '/RAND\(\)/', $this->queries ) );
     1725                $this->assertCount( 1, $random_queries, "Exactly one random query should be made:\n" . implode( "\n", $this->queries ) );
     1726                $this->assertMatchesRegularExpression( "/^\s*SELECT\s+{$wpdb->posts}\.ID\s+FROM/", $random_queries[0], 'The random query should select post IDs only.' );
     1727                $this->assertMatchesRegularExpression( '/ORDER BY RAND\(\)/', $random_queries[0], 'The query should use a random order.' );
     1728                $this->assertMatchesRegularExpression( '/LIMIT 0, 1\s*$/', $random_queries[0], 'The query should select a single post.' );
     1729                $this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $random_queries[0], 'The query should not count found rows.' );
     1730        }
     1731
     1732        /**
     1733         * At most one more query is made, to load the selected post by its ID.
     1734         *
    15101735         * @global wpdb $wpdb WordPress database abstraction object.
    15111736         */
    1512         public function test_random_query_selects_a_single_full_post_row(): void {
     1737        public function test_random_post_is_loaded_by_id_after_the_random_query(): void {
    15131738                global $wpdb;
    15141739
    15151740                $this->go_to_and_record_queries( home_url( '/random/' ) );
    15161741
    1517                 $this->assertMatchesRegularExpression( "/SELECT\s+{$wpdb->posts}\.\*/", $this->queries[0], 'The query should not be split into an ID query and a post query.' );
    1518                 $this->assertMatchesRegularExpression( '/ORDER BY RAND\(\)/', $this->queries[0], 'The query should use a random order.' );
    1519                 $this->assertMatchesRegularExpression( '/LIMIT 0, 1\s*$/', $this->queries[0], 'The query should select a single post.' );
    1520                 $this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $this->queries[0], 'The query should not count found rows.' );
    1521         }
    1522 
    1523         /**
     1742                $this->assertLessThanOrEqual( 2, count( $this->queries ), "No more than two database queries should be made to select the random post:\n" . implode( "\n", $this->queries ) );
     1743                $this->assertStringContainsString( 'RAND()', $this->queries[0], 'The random query should be made first.' );
     1744
     1745                if ( isset( $this->queries[1] ) ) {
     1746                        $this->assertMatchesRegularExpression( "/^\s*SELECT\s+{$wpdb->posts}\.\*\s+FROM\s+{$wpdb->posts}\s+WHERE\s+ID\s+IN\s+\(\d+\)/", $this->queries[1], 'The second query should load the selected post by its ID.' );
     1747                }
     1748        }
     1749
     1750        /**
     1751         * Ensure the random query does not prime the post meta or term caches.
     1752         *
    15241753         * @global wpdb $wpdb WordPress database abstraction object.
    15251754         */
    … …  
    15361765        }
    15371766
    1538         public function test_random_query_is_not_split_with_persistent_object_cache(): void {
    1539                 $using_ext_cache = wp_using_ext_object_cache( true );
    1540 
    1541                 $this->go_to_and_record_queries( home_url( '/random/' ) );
    1542 
    1543                 wp_using_ext_object_cache( $using_ext_cache );
    1544 
    1545                 $this->assertCount( 1, $this->queries, "Only one database query should be made with a persistent object cache:\n" . implode( "\n", $this->queries ) );
    1546         }
    1547 
    1548         public function test_split_the_query_is_unchanged_for_other_queries(): void {
    1549                 $this->assertTrue( wp_random_content_split_the_query( true, new WP_Query() ), 'Splitting should remain enabled for non-random queries.' );
    1550                 $this->assertFalse( wp_random_content_split_the_query( false, new WP_Query() ), 'Splitting should remain disabled for non-random queries.' );
    1551         }
    1552 
     1767        /**
     1768         * Ensure different posts are selected across random requests.
     1769         */
    15531770        public function test_random_selection_varies_between_requests(): void {
    15541771                for ( $i = 0; $i < 8; $i++ ) {
    … …  
    15721789
    15731790        /**
     1791         * Ensure the random query selects a single post when a theme overrides the limit.
     1792         *
    15741793         * @dataProvider data_query_vars_overriding_the_limit
    15751794         *
    … …  
    15781797         * @param mixed  $value     Query variable value.
    15791798         */
    1580         public function test_random_query_is_limited_to_one_post_when_a_theme_overrides_the_limit( $path, $query_var, $value ): void {
     1799        public function test_random_query_is_limited_to_one_post_when_a_theme_overrides_the_limit( string $path, string $query_var, $value ): void {
    15811800                add_action(
    15821801                        'pre_get_posts',
    … …  
    15971816
    15981817        /**
    1599          * Data provider.
     1818         * Data provider for {@see self::test_random_query_is_limited_to_one_post_when_a_theme_overrides_the_limit()}.
    16001819         *
    16011820         * The limit is applied differently to the home page and archives, see WP_Query::get_posts().
    16021821         *
    1603          * @return array<string, array{0: string, 1: string, 2: bool|int}>
     1822         * @return array<non-falsy-string, array{ path: non-falsy-string, query_var: non-falsy-string, value: bool|int }>
    16041823         */
    16051824        public static function data_query_vars_overriding_the_limit(): array {
    … …  
    16171836                ) as $context => $path ) {
    16181837                        foreach ( $overrides as $name => $override ) {
    1619                                 $data[ "{$context}: {$name}" ] = array( $path, $override[0], $override[1] );
     1838                                $data[ "{$context}: {$name}" ] = array(
     1839                                        'path'      => $path,
     1840                                        'query_var' => $override[0],
     1841                                        'value'     => $override[1],
     1842                                );
    16201843                        }
    16211844                }
    … …  
    16241847        }
    16251848
     1849        /**
     1850         * Ensure an offset set by a theme does not prevent the redirect.
     1851         */
    16261852        public function test_random_redirect_ignores_an_offset_set_by_a_theme(): void {
    16271853                add_action(
    … …  
    16431869         */
    16441870
     1871        /**
     1872         * Ensure the request does not redirect to a permalink on another site.
     1873         */
    16451874        public function test_no_redirect_when_the_permalink_is_on_another_site(): void {
    16461875                add_filter(
    … …  
    16561885        }
    16571886
     1887        /**
     1888         * Ensure the request redirects to a permalink on an allowed redirect host.
     1889         */
    16581890        public function test_redirect_to_a_permalink_on_an_allowed_host(): void {
    16591891                add_filter(
    … …  
    16761908        }
    16771909
     1910        /**
     1911         * Ensure the request does not redirect to a global post when the main query has no post.
     1912         */
    16781913        public function test_no_redirect_when_the_main_query_has_no_post(): void {
    16791914                add_filter( 'the_posts', '__return_empty_array' );
    … …  
    16961931         */
    16971932
     1933        /**
     1934         * Ensure random searches do not select randomable post types excluded from search.
     1935         */
    16981936        public function test_random_search_excludes_randomable_post_types_excluded_from_search(): void {
    16991937                register_post_type(
    … …  
    17171955        }
    17181956
     1957        /**
     1958         * Ensure randomable post types excluded from search can be requested directly.
     1959         */
    17191960        public function test_randomable_post_type_excluded_from_search_can_be_requested_directly(): void {
    17201961                register_post_type(
    … …  
    17331974        }
    17341975
     1976        /**
     1977         * Ensure randomable attachments are eligible for taxonomies registered for attachment MIME types.
     1978         */
    17351979        public function test_random_post_types_include_attachments_for_attachment_mime_type_taxonomies(): void {
    17361980                $filter = static function ( array $args, string $post_type ): array {
    … …  
    17592003        }
    17602004
     2005        /**
     2006         * Ensure non-string post types set by plugins are ignored.
     2007         */
    17612008        public function test_non_string_post_types_set_by_plugins_are_ignored(): void {
    17622009                add_action(
    … …  
    17792026
    17802027        /**
     2028         * Ensure the enabled filter value is cast to a boolean.
     2029         *
    17812030         * @dataProvider data_enabled_filter_values
    17822031         *
    … …  
    17842033         * @param bool  $expected Expected result.
    17852034         */
    1786         public function test_wp_is_random_content_redirect_enabled_returns_a_boolean( $value, $expected ): void {
     2035        public function test_wp_is_random_content_redirect_enabled_returns_a_boolean( $value, bool $expected ): void {
    17872036                add_filter(
    17882037                        'wp_enable_random_content_redirect',
    … …  
    17962045
    17972046        /**
    1798          * Data provider.
    1799          *
    1800          * @return array<string, array{0: mixed, 1: bool}>
     2047         * Data provider for {@see self::test_wp_is_random_content_redirect_enabled_returns_a_boolean()}.
     2048         *
     2049         * @return array<non-falsy-string, array{ 0: mixed, 1: bool }>
    18012050         */
    18022051        public static function data_enabled_filter_values(): array {
    … …  
    18112060        }
    18122061
     2062        /**
     2063         * Ensure stale rewrite rules do not redirect or randomize the query when the feature is disabled.
     2064         */
    18132065        public function test_no_redirect_when_disabled_without_flushing_the_rewrite_rules(): void {
    18142066                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    18242076         */
    18252077
     2078        /**
     2079         * Ensure creating a page named "random" flushes the rewrite rules once.
     2080         */
    18262081        public function test_creating_a_page_named_random_flushes_the_rewrite_rules(): void {
    18272082                $flushes = new MockAction();
    … …  
    18382093        }
    18392094
     2095        /**
     2096         * Ensure a page named "random" does not flush the rewrite rules with plain permalinks.
     2097         */
    18402098        public function test_page_named_random_does_not_flush_rewrite_rules_with_plain_permalinks(): void {
    18412099                $this->set_permalink_structure( '' );
    … …  
    18532111        }
    18542112
     2113        /**
     2114         * Ensure a page named "random" does not flush the rewrite rules when the feature is disabled.
     2115         */
    18552116        public function test_page_named_random_does_not_flush_rewrite_rules_when_disabled(): void {
    18562117                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    18692130
    18702131        /**
     2132         * Ensure a page at a custom random base takes precedence over random content redirects.
     2133         *
    18712134         * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    18722135         */
    … …  
    18912154
    18922155        /**
     2156         * Ensure a page named "random" does not conflict with a custom random base.
     2157         *
    18932158         * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    18942159         */
    … …  
    19142179        }
    19152180
     2181        /**
     2182         * Ensure a private page named "random" does not take precedence over random content redirects.
     2183         */
    19162184        public function test_private_page_named_random_does_not_take_precedence(): void {
    19172185                $this->create_post(
    … …  
    19282196        }
    19292197
     2198        /**
     2199         * Ensure a password protected page named "random" takes precedence over random content redirects.
     2200         */
    19302201        public function test_password_protected_page_named_random_takes_precedence(): void {
    19312202                $page = $this->create_post(
    … …  
    19482219
    19492220        /**
     2221         * Ensure paths beneath the random base without random rules do not redirect.
     2222         *
    19502223         * @dataProvider data_unsupported_random_paths
    19512224         *
    … …  
    19542227         * @param string $path Request path.
    19552228         */
    1956         public function test_no_redirect_for_paths_without_random_rules( $path ): void {
     2229        public function test_no_redirect_for_paths_without_random_rules( string $path ): void {
    19572230                global $wp;
    19582231
    … …  
    19642237
    19652238        /**
    1966          * Data provider.
    1967          *
    1968          * @return array<string, string[]>
     2239         * Data provider for {@see self::test_no_redirect_for_paths_without_random_rules()}.
     2240         *
     2241         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    19692242         */
    19702243        public static function data_unsupported_random_paths(): array {
    … …  
    19752248        }
    19762249
     2250        /**
     2251         * Ensure random content requests without a post send a 404 status.
     2252         */
    19772253        public function test_random_requests_without_a_post_send_a_404_status(): void {
    19782254                $status = new MockAction();
    … …  
    19922268         */
    19932269
     2270        /**
     2271         * Ensure `WP::handle_random()` ignores requests without the random query variable.
     2272         */
    19942273        public function test_handle_random_ignores_requests_without_the_random_query_var(): void {
    19952274                $wp             = new WP();
    … …  
    19992278        }
    20002279
     2280        /**
     2281         * Ensure `WP::handle_random()` ignores random content requests when the feature is disabled.
     2282         */
    20012283        public function test_handle_random_ignores_random_requests_when_disabled(): void {
    20022284                add_filter( 'wp_enable_random_content_redirect', '__return_false' );
    … …  
    20082290        }
    20092291
     2292        /**
     2293         * Ensure `WP::handle_random()` runs the main query for random content requests.
     2294         */
    20102295        public function test_handle_random_runs_the_main_query_for_random_requests(): void {
    20112296                $wp             = new WP();
    … …  
    20192304
    20202305        /**
     2306         * Ensure the main query is only run once for random content requests.
     2307         *
    20212308         * @dataProvider data_random_requests
    20222309         *
    … …  
    20332320
    20342321        /**
    2035          * Data provider for test_main_query_is_run_once_for_random_requests().
    2036          *
    2037          * @return array<string, array{0: string}>
     2322         * Data provider for {@see self::test_main_query_is_run_once_for_random_requests()}.
     2323         *
     2324         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    20382325         */
    20392326        public static function data_random_requests(): array {
    … …  
    20442331        }
    20452332
     2333        /**
     2334         * Ensure the random content query callbacks are removed after the main query.
     2335         */
    20462336        public function test_random_content_callbacks_are_removed_after_the_main_query(): void {
    20472337                $this->go_to( home_url( '/random/' ) );
    … …  
    20522342        }
    20532343
     2344        /**
     2345         * Ensure the `posts_orderby` and `post_limits` filters do not override the random order or limit.
     2346         */
    20542347        public function test_sql_customizations_do_not_override_the_random_order_or_limit(): void {
    20552348                add_filter(
    … …  
    20742367        }
    20752368
     2369        /**
     2370         * Ensure suppressing filters does not bypass the random order or limit.
     2371         */
    20762372        public function test_suppressed_filters_do_not_bypass_the_random_order_or_limit(): void {
    20772373                add_action(
    … …  
    20972393
    20982394        /**
     2395         * Ensure a post named "random" takes precedence over random content redirects.
     2396         *
    20992397         * @dataProvider data_post_name_at_the_root_structures
    21002398         *
    … …  
    21212419
    21222420        /**
    2123          * Data provider.
    2124          *
    2125          * @return array<string, string[]>
     2421         * Data provider for {@see self::test_post_named_random_takes_precedence_over_random_content_redirects()}.
     2422         *
     2423         * @return array<non-falsy-string, array{ 0: non-falsy-string }>
    21262424         */
    21272425        public static function data_post_name_at_the_root_structures(): array {
    … …  
    21322430        }
    21332431
     2432        /**
     2433         * Ensure random archives redirect when a post named "random" exists.
     2434         */
    21342435        public function test_random_archives_redirect_when_a_post_named_random_exists(): void {
    21352436                $this->create_post( array( 'post_name' => 'random' ) );
    … …  
    21402441        }
    21412442
     2443        /**
     2444         * Ensure a post named "random" does not conflict when posts have a blog prefix.
     2445         */
    21422446        public function test_post_named_random_does_not_conflict_with_a_blog_prefix(): void {
    21432447                $this->set_permalink_structure( '/blog/%postname%/' );
    … …  
    21522456        }
    21532457
     2458        /**
     2459         * Ensure creating a post named "random" flushes the rewrite rules once.
     2460         */
    21542461        public function test_creating_a_post_named_random_flushes_the_rewrite_rules(): void {
    21552462                $flushes = new MockAction();
    … …  
    21612468        }
    21622469
     2470        /**
     2471         * Ensure a post named "random" does not flush the rewrite rules when posts have a blog prefix.
     2472         */
    21632473        public function test_post_named_random_does_not_flush_rewrite_rules_with_a_blog_prefix(): void {
    21642474                $this->set_permalink_structure( '/blog/%postname%/' );
    … …  
    21712481        }
    21722482
     2483        /**
     2484         * Ensure random content redirects resume when a post named "random" is unpublished.
     2485         */
    21732486        public function test_unpublishing_a_post_named_random_resumes_random_content_redirects(): void {
    21742487                $post = $this->create_post( array( 'post_name' => 'random' ) );
Note: See TracChangeset for help on using the changeset viewer.