Changeset 65042
- Timestamp:
- 10/30/2026 12:59:06 AM (11 days ago)
- Location:
- trunk
- Files:
-
- 1 added
- 3 edited
-
src/wp-includes/default-filters.php (modified) (1 diff)
-
src/wp-includes/query.php (modified) (1 diff)
-
tests/phpunit/tests/query/randomContentRedirect.php (modified) (137 diffs)
-
tests/phpunit/tests/wp/handleRandom.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/default-filters.php
r64691 r65042 497 497 498 498 // Random content redirects, see WP::handle_random(). 499 add_filter( 'split_the_query', 'wp_random_content_split_the_query', 10, 2 );500 499 add_action( 'wp_after_insert_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 4 ); 501 500 add_action( 'after_delete_post', 'wp_random_content_flush_rewrite_rules_for_post', 10, 2 ); -
trunk/src/wp-includes/query.php
r64402 r65042 1463 1463 1464 1464 /** 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 post1468 * IDs separately to the post objects. Disabling the split allows the random post to be1469 * retrieved in a single database query.1470 *1471 * @since 7.2.01472 *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 /**1486 1465 * Redirects random content requests to the randomly selected post. 1487 1466 * -
trunk/tests/phpunit/tests/query/randomContentRedirect.php
r65030 r65042 16 16 * @covers ::wp_get_random_content_post_types 17 17 * @covers ::wp_random_content_flush_rewrite_rules_for_post 18 * @covers ::wp_random_content_split_the_query19 18 * @covers ::wp_is_random_content_redirect_enabled 20 19 */ … … 98 97 protected $recording_queries = false; 99 98 99 /** 100 * Creates shared user, category and post fixtures. 101 * 102 * @param WP_UnitTest_Factory $factory Test factory. 103 */ 100 104 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { 101 105 self::$editor = $factory->user->create( … … 133 137 } 134 138 139 /** 140 * Sets up pretty permalinks and redirect capturing before each test. 141 */ 135 142 public function set_up(): void { 136 143 parent::set_up(); … … 150 157 } 151 158 159 /** 160 * Unregisters the test post types and taxonomy after each test. 161 */ 152 162 public function tear_down(): void { 153 163 unregister_post_type( 'wptests_book' ); … … 166 176 * @return false 167 177 */ 168 public function filter_wp_redirect( $location,$status ) {178 public function filter_wp_redirect( string $location, int $status ) { 169 179 $this->redirect_location = $location; 170 180 $this->redirect_status = $status; … … 187 197 * @return string Unmodified database query. 188 198 */ 189 public function record_query( $query ) {199 public function record_query( string $query ) { 190 200 if ( $this->recording_queries ) { 191 201 $this->queries[] = $query; … … 200 210 * @param string $url URL to request. 201 211 */ 202 protected function go_to_and_record_queries( $url ): void {212 protected function go_to_and_record_queries( string $url ): void { 203 213 add_action( 'parse_request', array( $this, 'start_recording_queries' ), PHP_INT_MAX ); 204 214 add_filter( 'query', array( $this, 'record_query' ) ); … … 247 257 * @param int[] $post_ids Allowed post IDs. 248 258 * @param string $message Message to display on failure. 249 250 259 * 251 260 * @phpstan-assert !null $this->redirect_location … … 283 292 284 293 /** 294 * Ensure random content URLs redirect to a published post. 295 * 285 296 * @dataProvider data_random_urls 286 297 * 287 298 * @param string $path Random content request path. 288 299 */ 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 { 290 301 $this->go_to( home_url( $path ) ); 291 302 … … 294 305 295 306 /** 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 }> 299 310 */ 300 311 public static function data_random_urls(): array { … … 308 319 } 309 320 321 /** 322 * Ensure redirect uses HTTP 302 Temporary Redirect status. 323 */ 310 324 public function test_random_redirect_uses_a_temporary_redirect(): void { 311 325 $this->go_to( home_url( '/random/' ) ); … … 314 328 } 315 329 330 /** 331 * Ensure redirect location header matches the permalink of the selected post. 332 */ 316 333 public function test_random_redirect_location_is_the_permalink(): void { 317 334 $this->go_to( home_url( '/random/category/arts/' ) ); … … 320 337 } 321 338 339 /** 340 * Ensure random redirects work with plain permalinks. 341 */ 322 342 public function test_random_query_string_redirects_with_plain_permalinks(): void { 323 343 $this->set_permalink_structure( '' ); … … 329 349 } 330 350 351 /** 352 * Ensure random redirects work with pathinfo (/index.php/...) permalinks. 353 */ 331 354 public function test_random_redirect_with_pathinfo_permalinks(): void { 332 355 $this->set_permalink_structure( '/index.php/%postname%/' ); … … 337 360 } 338 361 362 /** 363 * Ensure random redirects respect the selected category. 364 */ 339 365 public function test_random_category_url_only_selects_posts_in_the_category(): void { 340 366 $this->go_to( home_url( '/random/category/sport/' ) ); … … 343 369 } 344 370 371 /** 372 * Ensure random redirects respect the selected nested category. 373 */ 345 374 public function test_random_child_category_url_selects_posts_in_the_child_category(): void { 346 375 $child = self::factory()->category->create( … … 357 386 } 358 387 388 /** 389 * Ensure random redirects include posts in the selected category's sub-categories. 390 */ 359 391 public function test_random_category_url_includes_posts_in_child_categories(): void { 360 392 $child = self::factory()->category->create( … … 371 403 } 372 404 405 /** 406 * Ensure random redirects respect the `?cat` URL parameter. 407 */ 373 408 public function test_random_cat_query_string_only_selects_posts_in_the_category(): void { 374 409 $this->go_to( add_query_arg( 'cat', self::$arts_category, home_url( '/?random' ) ) ); … … 377 412 } 378 413 414 /** 415 * Ensure random redirects respect the selected tag. 416 */ 379 417 public function test_random_tag_url_only_selects_posts_with_the_tag(): void { 380 418 $this->go_to( home_url( '/random/tag/impressionism/' ) ); … … 383 421 } 384 422 423 /** 424 * Ensure random redirects respect URLs specifying terms from multiple taxonomies. 425 */ 385 426 public function test_random_combined_taxonomy_query_selects_posts_matching_all_terms(): void { 386 427 $impressionist_sport = $this->create_post( … … 397 438 } 398 439 440 /** 441 * Ensure random redirects respect the selected author. 442 */ 399 443 public function test_random_author_url_only_selects_posts_by_the_author(): void { 400 444 $this->go_to( home_url( '/random/author/random-editor/' ) ); … … 403 447 } 404 448 449 /** 450 * Ensure random redirects respect the selected date. 451 */ 405 452 public function test_random_date_archive_only_selects_posts_from_the_date(): void { 406 453 $this->go_to( home_url( '/1844/?random' ) ); … … 409 456 } 410 457 458 /** 459 * Ensure random redirects respect the selected search query. 460 */ 411 461 public function test_random_search_only_selects_matching_posts(): void { 412 462 $this->go_to( home_url( '/?s=Baseball&random' ) ); … … 415 465 } 416 466 467 /** 468 * Ensure random redirects respect the selected post type. 469 */ 417 470 public function test_random_post_type_archive_url_only_selects_posts_of_the_type(): void { 418 471 register_post_type( … … 432 485 } 433 486 487 /** 488 * Ensure random redirects respect the randomable post type parameter regardless of archive setting. 489 */ 434 490 public function test_random_post_type_rewrite_slug_url_selects_posts_of_a_randomable_type_without_an_archive(): void { 435 491 register_post_type( … … 451 507 } 452 508 509 /** 510 * Ensure random redirects respect the selected post type using plain permalinks. 511 */ 453 512 public function test_random_post_type_query_string_selects_posts_of_the_type(): void { 454 513 register_post_type( … … 471 530 */ 472 531 532 /** 533 * Ensure pages are not randomable. 534 */ 473 535 public function test_no_redirect_for_pages(): void { 474 536 $this->create_post( array( 'post_type' => 'page' ) ); … … 479 541 } 480 542 543 /** 544 * Ensure main query for pages is not modified by random query string parameter. 545 */ 481 546 public function test_main_query_is_unchanged_for_pages(): void { 482 547 $this->create_post( array( 'post_type' => 'page' ) ); … … 487 552 } 488 553 554 /** 555 * Ensure pages are randomable if registration arguments enable the feature. 556 */ 489 557 public function test_pages_redirect_when_made_randomable(): void { 490 558 add_filter( 'register_post_type_args', array( $this, 'filter_make_pages_randomable' ), 10, 2 ); … … 513 581 } 514 582 583 /** 584 * Ensure post types registered as not randomable do not redirect. 585 */ 515 586 public function test_no_redirect_for_post_type_that_is_not_randomable(): void { 516 587 register_post_type( … … 529 600 } 530 601 602 /** 603 * Ensure archives of post types registered as not randomable do not redirect. 604 */ 531 605 public function test_no_redirect_for_post_type_archive_that_is_not_randomable(): void { 532 606 register_post_type( … … 548 622 } 549 623 624 /** 625 * Ensure only randomable post types are selected when multiple post types are requested. 626 */ 550 627 public function test_random_redirect_with_multiple_post_types_only_selects_randomable_types(): void { 551 628 register_post_type( … … 565 642 } 566 643 644 /** 645 * Ensure random searches do not select pages. 646 */ 567 647 public function test_random_search_only_selects_randomable_post_types(): void { 568 648 $this->create_post( … … 578 658 } 579 659 660 /** 661 * Ensure random searches include randomable custom post types. 662 */ 580 663 public function test_random_search_includes_randomable_custom_post_types(): void { 581 664 register_post_type( … … 599 682 } 600 683 684 /** 685 * Ensure random custom taxonomy archives only select randomable post types. 686 */ 601 687 public function test_random_custom_taxonomy_archive_only_selects_randomable_post_types(): void { 602 688 register_post_type( … … 627 713 } 628 714 715 /** 716 * Ensure archives of taxonomies used only by pages do not redirect. 717 */ 629 718 public function test_no_redirect_for_taxonomy_used_only_by_pages(): void { 630 719 register_taxonomy( … … 648 737 649 738 /** 739 * Ensure the eligible post types match the request. 740 * 650 741 * @dataProvider data_wp_get_random_content_post_types 651 742 * … … 653 744 * @param string[] $expected Expected post types. 654 745 */ 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 { 656 747 register_post_type( 657 748 'wptests_book', … … 682 773 683 774 /** 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> }> 687 778 */ 688 779 public static function data_wp_get_random_content_post_types(): array { … … 701 792 } 702 793 794 /** 795 * Ensure the random URL only selects posts by default. 796 */ 703 797 public function test_random_url_defaults_to_posts(): void { 704 798 $this->create_post( array( 'post_type' => 'page' ) ); … … 709 803 } 710 804 805 /** 806 * Ensure the random URL redirects to a post when a static front page is set. 807 */ 711 808 public function test_random_url_with_static_front_page_redirects_to_a_post(): void { 712 809 $front = $this->create_post( array( 'post_type' => 'page' ) ); … … 719 816 } 720 817 818 /** 819 * Ensure pagination does not affect the random selection. 820 */ 721 821 public function test_random_redirect_ignores_pagination(): void { 722 822 $this->go_to( home_url( '/random/category/arts/?paged=5' ) ); … … 730 830 731 831 /** 832 * Ensure unpublished and password protected posts are not selected, even for administrators. 833 * 732 834 * @dataProvider data_ineligible_posts 733 835 * 734 836 * @param array<string, string> $args Post arguments. 735 837 */ 736 public function test_random_redirect_excludes_ineligible_posts( $args ): void {838 public function test_random_redirect_excludes_ineligible_posts( array $args ): void { 737 839 $category = self::factory()->category->create(); 738 840 $this->create_post( array_merge( array( 'post_category' => array( $category ) ), $args ) ); … … 746 848 747 849 /** 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> }> 751 853 */ 752 854 public static function data_ineligible_posts(): array { … … 767 869 768 870 /** 871 * Ensure sticky posts are not prepended to the random selection. 872 * 769 873 * @global wpdb $wpdb WordPress database abstraction object. 770 874 */ … … 806 910 */ 807 911 912 /** 913 * Ensure the home page does not redirect without the random query variable. 914 */ 808 915 public function test_no_redirect_without_random_query_var(): void { 809 916 $this->go_to( home_url( '/' ) ); … … 813 920 } 814 921 922 /** 923 * Ensure archives are not randomized without the random query variable. 924 */ 815 925 public function test_archives_are_unchanged_without_random_query_var(): void { 816 926 $this->go_to( home_url( '/category/arts/' ) ); … … 820 930 } 821 931 932 /** 933 * Ensure singular posts do not redirect with the random query variable. 934 */ 822 935 public function test_no_redirect_for_singular_post_with_random_query_var(): void { 823 936 $this->go_to( add_query_arg( 'random', '', get_permalink( self::$arts_post ) ) ); … … 828 941 } 829 942 943 /** 944 * Ensure requests for a post ID do not redirect with the random query variable. 945 */ 830 946 public function test_no_redirect_for_post_id_with_random_query_var(): void { 831 947 $this->go_to( home_url( '/?random&p=' . self::$sport_post ) ); … … 835 951 } 836 952 953 /** 954 * Ensure pages do not redirect with the random query variable. 955 */ 837 956 public function test_no_redirect_for_page_with_random_query_var(): void { 838 957 $page = $this->create_post( … … 849 968 } 850 969 970 /** 971 * Ensure the static front page does not redirect with the random query variable. 972 */ 851 973 public function test_no_redirect_for_static_front_page_with_random_query_var_from_extra_vars(): void { 852 974 $front = $this->create_post( array( 'post_type' => 'page' ) ); … … 861 983 862 984 /** 985 * Ensure feeds do not redirect with the random query variable. 986 * 863 987 * @dataProvider data_feed_urls 864 988 * 865 989 * @param string $path Feed path. 866 990 */ 867 public function test_no_redirect_for_feeds( $path ): void {991 public function test_no_redirect_for_feeds( string $path ): void { 868 992 $this->go_to( home_url( $path ) ); 869 993 … … 873 997 874 998 /** 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 }> 878 1002 */ 879 1003 public static function data_feed_urls(): array { … … 885 1009 } 886 1010 1011 /** 1012 * Ensure embeds do not redirect with the random query variable. 1013 */ 887 1014 public function test_no_redirect_for_embeds(): void { 888 1015 $this->go_to( home_url( '/cassatt/embed/?random' ) ); … … 892 1019 } 893 1020 1021 /** 1022 * Ensure random requests for a nonexistent category return a 404 rather than redirecting. 1023 */ 894 1024 public function test_no_redirect_for_nonexistent_category(): void { 895 1025 $this->go_to( home_url( '/random/category/does-not-exist/' ) ); … … 899 1029 } 900 1030 1031 /** 1032 * Ensure random requests for an empty category return a 404 rather than redirecting. 1033 */ 901 1034 public function test_no_redirect_for_empty_category(): void { 902 1035 $this->go_to( home_url( '/random/category/empty/' ) ); … … 906 1039 } 907 1040 1041 /** 1042 * Ensure random requests without eligible posts return a 404 rather than redirecting. 1043 */ 908 1044 public function test_no_redirect_when_no_posts_are_eligible(): void { 909 1045 add_filter( … … 923 1059 924 1060 /** 1061 * Ensure requests using HTTP methods other than GET or HEAD do not redirect. 1062 * 925 1063 * @dataProvider data_non_redirecting_http_methods 926 1064 * 927 1065 * @param string $method HTTP request method. 928 1066 */ 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 { 930 1068 $_SERVER['REQUEST_METHOD'] = $method; 931 1069 … … 936 1074 937 1075 /** 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 }> 941 1079 */ 942 1080 public static function data_non_redirecting_http_methods(): array { … … 949 1087 } 950 1088 1089 /** 1090 * Ensure requests without an HTTP method do not redirect. 1091 */ 951 1092 public function test_no_redirect_without_request_method(): void { 952 1093 unset( $_SERVER['REQUEST_METHOD'] ); … … 958 1099 959 1100 /** 1101 * Ensure GET and HEAD requests redirect, regardless of case. 1102 * 960 1103 * @dataProvider data_redirecting_http_methods 961 1104 * 962 1105 * @param string $method HTTP request method. 963 1106 */ 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 { 965 1108 $_SERVER['REQUEST_METHOD'] = $method; 966 1109 … … 971 1114 972 1115 /** 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 }> 976 1119 */ 977 1120 public static function data_redirecting_http_methods(): array { … … 997 1140 * @param string $structure Permalink structure. 998 1141 */ 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 { 1000 1143 global $wp; 1001 1144 … … 1023 1166 1024 1167 /** 1168 * Ensure the random URL redirects when a page slug begins with the random base. 1169 * 1025 1170 * @dataProvider data_permalink_structures 1026 1171 * … … 1029 1174 * @param string $structure Permalink structure. 1030 1175 */ 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 { 1032 1177 global $wp_rewrite; 1033 1178 … … 1057 1202 * @param string $structure Permalink structure. 1058 1203 */ 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 { 1060 1205 global $wp_rewrite; 1061 1206 … … 1080 1225 1081 1226 /** 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 }> 1085 1231 */ 1086 1232 public static function data_permalink_structures(): array { … … 1093 1239 } 1094 1240 1241 /** 1242 * Ensure random archives redirect when a page named "random" exists. 1243 */ 1095 1244 public function test_random_archives_redirect_when_a_page_named_random_exists(): void { 1096 1245 $this->create_post( … … 1106 1255 } 1107 1256 1257 /** 1258 * Ensure the random query string redirects when a page named "random" exists. 1259 */ 1108 1260 public function test_random_query_string_redirects_when_a_page_named_random_exists(): void { 1109 1261 $this->create_post( … … 1119 1271 } 1120 1272 1273 /** 1274 * Ensure a draft page named "random" does not take precedence over random content redirects. 1275 */ 1121 1276 public function test_draft_page_named_random_does_not_take_precedence(): void { 1122 1277 $this->create_post( … … 1134 1289 1135 1290 /** 1291 * Ensure random content redirects resume once a page named "random" no longer conflicts. 1292 * 1136 1293 * @dataProvider data_page_named_random_changes 1137 1294 * 1138 1295 * @param callable $change Callback to change the page so it no longer conflicts. 1139 1296 */ 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 { 1141 1298 $page = $this->create_post( 1142 1299 array( … … 1156 1313 1157 1314 /** 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 }> 1161 1318 */ 1162 1319 public static function data_page_named_random_changes(): array { … … 1205 1362 } 1206 1363 1364 /** 1365 * Ensure publishing a draft page named "random" takes precedence over random content redirects. 1366 */ 1207 1367 public function test_publishing_a_draft_page_named_random_takes_precedence(): void { 1208 1368 $page = $this->create_post( … … 1222 1382 } 1223 1383 1384 /** 1385 * Ensure saving pages and posts not at the random base does not flush the rewrite rules. 1386 */ 1224 1387 public function test_saving_unrelated_pages_does_not_flush_rewrite_rules(): void { 1225 1388 $flushes = new MockAction(); … … 1248 1411 */ 1249 1412 1413 /** 1414 * Ensure random content requests do not redirect when the feature is disabled. 1415 */ 1250 1416 public function test_no_redirect_when_disabled(): void { 1251 1417 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 1257 1423 } 1258 1424 1425 /** 1426 * Ensure the main query is not randomized or limited when the feature is disabled. 1427 */ 1259 1428 public function test_main_query_is_unchanged_when_disabled(): void { 1260 1429 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 1266 1435 } 1267 1436 1437 /** 1438 * Ensure the random URL is not routed when the feature is disabled. 1439 */ 1268 1440 public function test_random_url_is_not_routed_when_disabled(): void { 1269 1441 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 1280 1452 */ 1281 1453 1454 /** 1455 * Ensure the `pre_get_posts` action can change the eligible post type. 1456 */ 1282 1457 public function test_pre_get_posts_can_change_the_eligible_post_type(): void { 1283 1458 register_post_type( … … 1304 1479 } 1305 1480 1481 /** 1482 * Ensure the `pre_get_posts` action cannot add non-randomable post types. 1483 */ 1306 1484 public function test_pre_get_posts_cannot_add_non_randomable_post_types(): void { 1307 1485 $this->create_post( array( 'post_type' => 'page' ) ); … … 1321 1499 } 1322 1500 1501 /** 1502 * Ensure the `pre_get_posts` action can exclude posts from the random selection. 1503 */ 1323 1504 public function test_pre_get_posts_can_exclude_posts(): void { 1324 1505 add_action( … … 1336 1517 } 1337 1518 1519 /** 1520 * Ensure theme customizations of the archive order and limit do not override the random query. 1521 */ 1338 1522 public function test_pre_get_posts_archive_customizations_do_not_override_the_random_order(): void { 1339 1523 // Typical theme customization of the main query for archives. … … 1357 1541 } 1358 1542 1543 /** 1544 * Ensure secondary queries are not random content queries. 1545 */ 1359 1546 public function test_secondary_queries_are_not_random_content_queries(): void { 1360 1547 $this->go_to( home_url( '/random/' ) ); … … 1366 1553 } 1367 1554 1555 /** 1556 * Ensure the main query for the random URL is a random content query. 1557 */ 1368 1558 public function test_wp_is_random_content_query_for_main_query(): void { 1369 1559 $this->go_to( home_url( '/random/' ) ); … … 1373 1563 1374 1564 /** 1565 * Ensure the `is_random()` conditional tag matches the request. 1566 * 1375 1567 * @dataProvider data_is_random 1376 1568 * … … 1381 1573 * @param bool $expected Expected result. 1382 1574 */ 1383 public function test_is_random( $path,$expected ): void {1575 public function test_is_random( string $path, bool $expected ): void { 1384 1576 $this->go_to( home_url( $path ) ); 1385 1577 … … 1389 1581 1390 1582 /** 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 }> 1394 1586 */ 1395 1587 public static function data_is_random(): array { … … 1410 1602 1411 1603 /** 1604 * Ensure secondary queries with the random query variable are for random content. 1605 * 1412 1606 * @covers WP_Query::is_random 1413 1607 */ … … 1420 1614 1421 1615 /** 1616 * Ensure the query is not for random content once it is set to a 404. 1617 * 1422 1618 * @covers WP_Query::is_random 1423 1619 */ … … 1431 1627 1432 1628 /** 1629 * Ensure the `is_random()` conditional tag returns false before the query is run. 1630 * 1433 1631 * @covers ::is_random 1434 1632 * … … 1441 1639 } 1442 1640 1641 /** 1642 * Ensure the main query is not randomized in the admin. 1643 */ 1443 1644 public function test_main_query_is_not_randomized_in_the_admin(): void { 1444 1645 set_current_screen( 'edit.php' ); … … 1455 1656 } 1456 1657 1658 /** 1659 * Ensure the request redirects when the `the_posts` filter returns a non-list array. 1660 */ 1457 1661 public function test_redirect_when_the_posts_filter_returns_a_non_list_array(): void { 1458 1662 add_filter( … … 1468 1672 } 1469 1673 1674 /** 1675 * Ensure the request does not redirect when the permalink is empty. 1676 */ 1470 1677 public function test_no_redirect_when_the_permalink_is_empty(): void { 1471 1678 add_filter( 'post_link', '__return_empty_string' ); … … 1476 1683 } 1477 1684 1685 /** 1686 * Ensure the redirect happens before the `wp` action fires. 1687 */ 1478 1688 public function test_redirect_happens_before_the_wp_action(): void { 1479 1689 $wp = new MockAction(); … … 1499 1709 */ 1500 1710 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 1502 1720 $this->go_to_and_record_queries( home_url( '/random/' ) ); 1503 1721 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 * 1510 1735 * @global wpdb $wpdb WordPress database abstraction object. 1511 1736 */ 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 { 1513 1738 global $wpdb; 1514 1739 1515 1740 $this->go_to_and_record_queries( home_url( '/random/' ) ); 1516 1741 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 * 1524 1753 * @global wpdb $wpdb WordPress database abstraction object. 1525 1754 */ … … 1536 1765 } 1537 1766 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 */ 1553 1770 public function test_random_selection_varies_between_requests(): void { 1554 1771 for ( $i = 0; $i < 8; $i++ ) { … … 1572 1789 1573 1790 /** 1791 * Ensure the random query selects a single post when a theme overrides the limit. 1792 * 1574 1793 * @dataProvider data_query_vars_overriding_the_limit 1575 1794 * … … 1578 1797 * @param mixed $value Query variable value. 1579 1798 */ 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 { 1581 1800 add_action( 1582 1801 'pre_get_posts', … … 1597 1816 1598 1817 /** 1599 * Data provider .1818 * Data provider for {@see self::test_random_query_is_limited_to_one_post_when_a_theme_overrides_the_limit()}. 1600 1819 * 1601 1820 * The limit is applied differently to the home page and archives, see WP_Query::get_posts(). 1602 1821 * 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 }> 1604 1823 */ 1605 1824 public static function data_query_vars_overriding_the_limit(): array { … … 1617 1836 ) as $context => $path ) { 1618 1837 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 ); 1620 1843 } 1621 1844 } … … 1624 1847 } 1625 1848 1849 /** 1850 * Ensure an offset set by a theme does not prevent the redirect. 1851 */ 1626 1852 public function test_random_redirect_ignores_an_offset_set_by_a_theme(): void { 1627 1853 add_action( … … 1643 1869 */ 1644 1870 1871 /** 1872 * Ensure the request does not redirect to a permalink on another site. 1873 */ 1645 1874 public function test_no_redirect_when_the_permalink_is_on_another_site(): void { 1646 1875 add_filter( … … 1656 1885 } 1657 1886 1887 /** 1888 * Ensure the request redirects to a permalink on an allowed redirect host. 1889 */ 1658 1890 public function test_redirect_to_a_permalink_on_an_allowed_host(): void { 1659 1891 add_filter( … … 1676 1908 } 1677 1909 1910 /** 1911 * Ensure the request does not redirect to a global post when the main query has no post. 1912 */ 1678 1913 public function test_no_redirect_when_the_main_query_has_no_post(): void { 1679 1914 add_filter( 'the_posts', '__return_empty_array' ); … … 1696 1931 */ 1697 1932 1933 /** 1934 * Ensure random searches do not select randomable post types excluded from search. 1935 */ 1698 1936 public function test_random_search_excludes_randomable_post_types_excluded_from_search(): void { 1699 1937 register_post_type( … … 1717 1955 } 1718 1956 1957 /** 1958 * Ensure randomable post types excluded from search can be requested directly. 1959 */ 1719 1960 public function test_randomable_post_type_excluded_from_search_can_be_requested_directly(): void { 1720 1961 register_post_type( … … 1733 1974 } 1734 1975 1976 /** 1977 * Ensure randomable attachments are eligible for taxonomies registered for attachment MIME types. 1978 */ 1735 1979 public function test_random_post_types_include_attachments_for_attachment_mime_type_taxonomies(): void { 1736 1980 $filter = static function ( array $args, string $post_type ): array { … … 1759 2003 } 1760 2004 2005 /** 2006 * Ensure non-string post types set by plugins are ignored. 2007 */ 1761 2008 public function test_non_string_post_types_set_by_plugins_are_ignored(): void { 1762 2009 add_action( … … 1779 2026 1780 2027 /** 2028 * Ensure the enabled filter value is cast to a boolean. 2029 * 1781 2030 * @dataProvider data_enabled_filter_values 1782 2031 * … … 1784 2033 * @param bool $expected Expected result. 1785 2034 */ 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 { 1787 2036 add_filter( 1788 2037 'wp_enable_random_content_redirect', … … 1796 2045 1797 2046 /** 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 }> 1801 2050 */ 1802 2051 public static function data_enabled_filter_values(): array { … … 1811 2060 } 1812 2061 2062 /** 2063 * Ensure stale rewrite rules do not redirect or randomize the query when the feature is disabled. 2064 */ 1813 2065 public function test_no_redirect_when_disabled_without_flushing_the_rewrite_rules(): void { 1814 2066 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 1824 2076 */ 1825 2077 2078 /** 2079 * Ensure creating a page named "random" flushes the rewrite rules once. 2080 */ 1826 2081 public function test_creating_a_page_named_random_flushes_the_rewrite_rules(): void { 1827 2082 $flushes = new MockAction(); … … 1838 2093 } 1839 2094 2095 /** 2096 * Ensure a page named "random" does not flush the rewrite rules with plain permalinks. 2097 */ 1840 2098 public function test_page_named_random_does_not_flush_rewrite_rules_with_plain_permalinks(): void { 1841 2099 $this->set_permalink_structure( '' ); … … 1853 2111 } 1854 2112 2113 /** 2114 * Ensure a page named "random" does not flush the rewrite rules when the feature is disabled. 2115 */ 1855 2116 public function test_page_named_random_does_not_flush_rewrite_rules_when_disabled(): void { 1856 2117 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 1869 2130 1870 2131 /** 2132 * Ensure a page at a custom random base takes precedence over random content redirects. 2133 * 1871 2134 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1872 2135 */ … … 1891 2154 1892 2155 /** 2156 * Ensure a page named "random" does not conflict with a custom random base. 2157 * 1893 2158 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1894 2159 */ … … 1914 2179 } 1915 2180 2181 /** 2182 * Ensure a private page named "random" does not take precedence over random content redirects. 2183 */ 1916 2184 public function test_private_page_named_random_does_not_take_precedence(): void { 1917 2185 $this->create_post( … … 1928 2196 } 1929 2197 2198 /** 2199 * Ensure a password protected page named "random" takes precedence over random content redirects. 2200 */ 1930 2201 public function test_password_protected_page_named_random_takes_precedence(): void { 1931 2202 $page = $this->create_post( … … 1948 2219 1949 2220 /** 2221 * Ensure paths beneath the random base without random rules do not redirect. 2222 * 1950 2223 * @dataProvider data_unsupported_random_paths 1951 2224 * … … 1954 2227 * @param string $path Request path. 1955 2228 */ 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 { 1957 2230 global $wp; 1958 2231 … … 1964 2237 1965 2238 /** 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 }> 1969 2242 */ 1970 2243 public static function data_unsupported_random_paths(): array { … … 1975 2248 } 1976 2249 2250 /** 2251 * Ensure random content requests without a post send a 404 status. 2252 */ 1977 2253 public function test_random_requests_without_a_post_send_a_404_status(): void { 1978 2254 $status = new MockAction(); … … 1992 2268 */ 1993 2269 2270 /** 2271 * Ensure `WP::handle_random()` ignores requests without the random query variable. 2272 */ 1994 2273 public function test_handle_random_ignores_requests_without_the_random_query_var(): void { 1995 2274 $wp = new WP(); … … 1999 2278 } 2000 2279 2280 /** 2281 * Ensure `WP::handle_random()` ignores random content requests when the feature is disabled. 2282 */ 2001 2283 public function test_handle_random_ignores_random_requests_when_disabled(): void { 2002 2284 add_filter( 'wp_enable_random_content_redirect', '__return_false' ); … … 2008 2290 } 2009 2291 2292 /** 2293 * Ensure `WP::handle_random()` runs the main query for random content requests. 2294 */ 2010 2295 public function test_handle_random_runs_the_main_query_for_random_requests(): void { 2011 2296 $wp = new WP(); … … 2019 2304 2020 2305 /** 2306 * Ensure the main query is only run once for random content requests. 2307 * 2021 2308 * @dataProvider data_random_requests 2022 2309 * … … 2033 2320 2034 2321 /** 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 }> 2038 2325 */ 2039 2326 public static function data_random_requests(): array { … … 2044 2331 } 2045 2332 2333 /** 2334 * Ensure the random content query callbacks are removed after the main query. 2335 */ 2046 2336 public function test_random_content_callbacks_are_removed_after_the_main_query(): void { 2047 2337 $this->go_to( home_url( '/random/' ) ); … … 2052 2342 } 2053 2343 2344 /** 2345 * Ensure the `posts_orderby` and `post_limits` filters do not override the random order or limit. 2346 */ 2054 2347 public function test_sql_customizations_do_not_override_the_random_order_or_limit(): void { 2055 2348 add_filter( … … 2074 2367 } 2075 2368 2369 /** 2370 * Ensure suppressing filters does not bypass the random order or limit. 2371 */ 2076 2372 public function test_suppressed_filters_do_not_bypass_the_random_order_or_limit(): void { 2077 2373 add_action( … … 2097 2393 2098 2394 /** 2395 * Ensure a post named "random" takes precedence over random content redirects. 2396 * 2099 2397 * @dataProvider data_post_name_at_the_root_structures 2100 2398 * … … 2121 2419 2122 2420 /** 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 }> 2126 2424 */ 2127 2425 public static function data_post_name_at_the_root_structures(): array { … … 2132 2430 } 2133 2431 2432 /** 2433 * Ensure random archives redirect when a post named "random" exists. 2434 */ 2134 2435 public function test_random_archives_redirect_when_a_post_named_random_exists(): void { 2135 2436 $this->create_post( array( 'post_name' => 'random' ) ); … … 2140 2441 } 2141 2442 2443 /** 2444 * Ensure a post named "random" does not conflict when posts have a blog prefix. 2445 */ 2142 2446 public function test_post_named_random_does_not_conflict_with_a_blog_prefix(): void { 2143 2447 $this->set_permalink_structure( '/blog/%postname%/' ); … … 2152 2456 } 2153 2457 2458 /** 2459 * Ensure creating a post named "random" flushes the rewrite rules once. 2460 */ 2154 2461 public function test_creating_a_post_named_random_flushes_the_rewrite_rules(): void { 2155 2462 $flushes = new MockAction(); … … 2161 2468 } 2162 2469 2470 /** 2471 * Ensure a post named "random" does not flush the rewrite rules when posts have a blog prefix. 2472 */ 2163 2473 public function test_post_named_random_does_not_flush_rewrite_rules_with_a_blog_prefix(): void { 2164 2474 $this->set_permalink_structure( '/blog/%postname%/' ); … … 2171 2481 } 2172 2482 2483 /** 2484 * Ensure random content redirects resume when a post named "random" is unpublished. 2485 */ 2173 2486 public function test_unpublishing_a_post_named_random_resumes_random_content_redirects(): void { 2174 2487 $post = $this->create_post( array( 'post_name' => 'random' ) );
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)