Changeset 55583 for trunk/tests/phpunit/tests/post/getPages.php
- Timestamp:
- 03/23/2023 11:35:21 AM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/getPages.php
r55569 r55583 3 3 /** 4 4 * @group post 5 * 6 * @covers ::get_pages 5 7 */ 6 7 8 class Tests_Post_GetPages extends WP_UnitTestCase { 8 9 /** … … 101 102 102 103 /** 104 * @ticket 43514 105 */ 106 public function test_get_pages_cache_empty() { 107 global $wpdb; 108 109 wp_cache_delete( 'last_changed', 'posts' ); 110 $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) ); 111 112 $num_queries = $wpdb->num_queries; 113 114 $pages = get_pages(); // Database gets queried. 115 116 $this->assertSame( $num_queries + 1, $wpdb->num_queries ); 117 118 $num_queries = $wpdb->num_queries; 119 120 $pages = get_pages(); // Database should not get queried. 121 122 $this->assertSame( $num_queries, $wpdb->num_queries ); 123 } 124 125 /** 103 126 * @ticket 40669 104 127 */ 105 public function test_ cache_should_be_invalidated_by_add_post_meta() {128 public function test_get_pages_cache_should_be_invalidated_by_add_post_meta() { 106 129 $posts = self::factory()->post->create_many( 107 130 2, … … 139 162 * @ticket 40669 140 163 */ 141 public function test_ cache_should_be_invalidated_by_update_post_meta() {164 public function test_get_pages_cache_should_be_invalidated_by_update_post_meta() { 142 165 $posts = self::factory()->post->create_many( 143 166 2, … … 174 197 175 198 /** 176 * @ticket 12821 177 * @covers ::get_pages 178 */ 179 public function test_include_ignore_meta_key() { 199 * @ticket 40669 200 */ 201 public function test_get_pages_cache_should_be_invalidated_by_delete_post_meta() { 180 202 $posts = self::factory()->post->create_many( 181 203 2, … … 185 207 ); 186 208 187 $pages = get_pages( 188 array( 189 'include' => $posts, 209 add_post_meta( $posts[0], 'foo', 'bar' ); 210 add_post_meta( $posts[1], 'foo', 'bar' ); 211 212 $cached = get_pages( 213 array( 190 214 'meta_key' => 'foo', 191 215 'meta_value' => 'bar', … … 193 217 ); 194 218 195 $page_ids = wp_list_pluck( $pages, 'ID' ); 196 $this->assertSameSets( $posts, $page_ids ); 197 } 198 199 /** 200 * @ticket 12821 201 * @covers ::get_pages 202 */ 203 public function test_include_ignore_exclude() { 204 $includes = self::factory()->post->create_many( 205 2, 206 array( 207 'post_type' => 'page', 208 ) 209 ); 210 211 $excludes = self::factory()->post->create_many( 212 2, 213 array( 214 'post_type' => 'page', 215 ) 216 ); 217 218 $pages = get_pages( 219 array( 220 'include' => $includes, 221 'exclude' => $excludes, 222 ) 223 ); 224 225 $page_ids = wp_list_pluck( $pages, 'ID' ); 226 $this->assertSameSets( $includes, $page_ids ); 227 } 228 229 /** 230 * @ticket 40669 231 */ 232 public function test_cache_should_be_invalidated_by_delete_post_meta() { 233 $posts = self::factory()->post->create_many( 234 2, 235 array( 236 'post_type' => 'page', 237 ) 238 ); 239 240 add_post_meta( $posts[0], 'foo', 'bar' ); 241 add_post_meta( $posts[1], 'foo', 'bar' ); 242 243 $cached = get_pages( 219 $cached_ids = wp_list_pluck( $cached, 'ID' ); 220 $this->assertSameSets( $posts, $cached_ids ); 221 222 delete_post_meta( $posts[1], 'foo' ); 223 224 $found = get_pages( 244 225 array( 245 226 'meta_key' => 'foo', … … 248 229 ); 249 230 250 $cached_ids = wp_list_pluck( $cached, 'ID' );251 $this->assertSameSets( $posts, $cached_ids );252 253 delete_post_meta( $posts[1], 'foo' );254 255 $found = get_pages(256 array(257 'meta_key' => 'foo',258 'meta_value' => 'bar',259 )260 );261 262 231 $found_ids = wp_list_pluck( $found, 'ID' ); 263 232 $this->assertSameSets( array( $posts[0] ), $found_ids ); … … 267 236 * @ticket 40669 268 237 */ 269 public function test_ cache_should_be_invalidated_by_delete_post_meta_by_key() {238 public function test_get_pages_cache_should_be_invalidated_by_delete_post_meta_by_key() { 270 239 $posts = self::factory()->post->create_many( 271 240 2, … … 362 331 363 332 /** 333 * @ticket 12821 334 */ 335 public function test_get_pages_include_ignores_meta_key() { 336 $posts = self::factory()->post->create_many( 337 2, 338 array( 339 'post_type' => 'page', 340 ) 341 ); 342 343 $pages = get_pages( 344 array( 345 'include' => $posts, 346 'meta_key' => 'foo', 347 'meta_value' => 'bar', 348 ) 349 ); 350 351 $page_ids = wp_list_pluck( $pages, 'ID' ); 352 $this->assertSameSets( $posts, $page_ids ); 353 } 354 355 /** 356 * @ticket 12821 357 */ 358 public function test_get_pages_include_ignores_exclude() { 359 $includes = self::factory()->post->create_many( 360 2, 361 array( 362 'post_type' => 'page', 363 ) 364 ); 365 366 $excludes = self::factory()->post->create_many( 367 2, 368 array( 369 'post_type' => 'page', 370 ) 371 ); 372 373 $pages = get_pages( 374 array( 375 'include' => $includes, 376 'exclude' => $excludes, 377 ) 378 ); 379 380 $page_ids = wp_list_pluck( $pages, 'ID' ); 381 $this->assertSameSets( $includes, $page_ids ); 382 } 383 384 public function test_get_pages_exclude_tree() { 385 $post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) ); 386 $post_id2 = self::factory()->post->create( 387 array( 388 'post_type' => 'page', 389 'post_parent' => $post_id1, 390 ) 391 ); 392 $post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) ); 393 $post_id4 = self::factory()->post->create( 394 array( 395 'post_type' => 'page', 396 'post_parent' => $post_id3, 397 ) 398 ); 399 400 $all = get_pages(); 401 402 $this->assertCount( 4, $all ); 403 404 $exclude1 = get_pages( "exclude_tree=$post_id1" ); 405 $this->assertCount( 2, $exclude1 ); 406 407 $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) ); 408 $this->assertCount( 2, $exclude2 ); 409 410 $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) ); 411 $this->assertCount( 2, $exclude3 ); 412 413 $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) ); 414 $this->assertCount( 2, $exclude4 ); 415 416 $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); 417 $this->assertCount( 0, $exclude5 ); 418 419 $post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) ); 420 $post_id6 = self::factory()->post->create( 421 array( 422 'post_type' => 'page', 423 'post_parent' => $post_id5, 424 ) 425 ); 426 427 $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); 428 $this->assertCount( 2, $exclude6 ); 429 } 430 431 /** 364 432 * @ticket 9470 365 433 */ … … 433 501 * @ticket 22208 434 502 */ 435 public function test_get_chi dren_fields_ids() {503 public function test_get_children_fields_ids() { 436 504 $post_id = self::factory()->post->create(); 437 505 $child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) ); … … 706 774 } 707 775 708 public function test_exclude_tree() {709 $post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );710 $post_id2 = self::factory()->post->create(711 array(712 'post_type' => 'page',713 'post_parent' => $post_id1,714 )715 );716 $post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) );717 $post_id4 = self::factory()->post->create(718 array(719 'post_type' => 'page',720 'post_parent' => $post_id3,721 )722 );723 724 $all = get_pages();725 726 $this->assertCount( 4, $all );727 728 $exclude1 = get_pages( "exclude_tree=$post_id1" );729 $this->assertCount( 2, $exclude1 );730 731 $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) );732 $this->assertCount( 2, $exclude2 );733 734 $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) );735 $this->assertCount( 2, $exclude3 );736 737 $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) );738 $this->assertCount( 2, $exclude4 );739 740 $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );741 $this->assertCount( 0, $exclude5 );742 743 $post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) );744 $post_id6 = self::factory()->post->create(745 array(746 'post_type' => 'page',747 'post_parent' => $post_id5,748 )749 );750 751 $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );752 $this->assertCount( 2, $exclude6 );753 }754 755 /**756 * @ticket 43514757 */758 public function test_get_pages_cache_empty() {759 global $wpdb;760 761 wp_cache_delete( 'last_changed', 'posts' );762 $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );763 764 $num_queries = $wpdb->num_queries;765 766 $pages = get_pages(); // Database gets queried.767 768 $this->assertSame( $num_queries + 1, $wpdb->num_queries );769 770 $num_queries = $wpdb->num_queries;771 772 $pages = get_pages(); // Database should not get queried.773 774 $this->assertSame( $num_queries, $wpdb->num_queries );775 }776 777 776 /** 778 777 * @ticket 12821 779 * @covers ::get_pages780 778 */ 781 779 public function test_get_pages_post_type() { … … 790 788 } 791 789 792 793 790 /** 794 791 * @ticket 12821 795 * @covers ::get_pages796 */797 public function test_get_pages_author() {798 $author_1 = self::factory()->user->create(799 array(800 'user_login' => 'author1',801 'role' => 'author',802 )803 );804 $posts = self::factory()->post->create_many(805 2,806 array(807 'post_type' => 'page',808 'post_author' => $author_1,809 )810 );811 $pages = get_pages(812 array(813 'authors' => $author_1,814 )815 );816 817 $this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );818 }819 820 821 /**822 * @ticket 12821823 * @covers ::get_pages824 792 */ 825 793 public function test_get_pages_post_status() { … … 849 817 /** 850 818 * @ticket 12821 851 * @covers ::get_pages852 819 */ 853 820 public function test_get_pages_offset() { … … 863 830 } 864 831 865 866 832 /** 867 833 * @ticket 12821 868 * @covers ::get_pages 869 */ 870 public function test_get_pages_authors() { 834 */ 835 public function test_get_pages_author() { 871 836 $author_1 = self::factory()->user->create( 872 837 array( … … 875 840 ) 876 841 ); 842 $posts = self::factory()->post->create_many( 843 2, 844 array( 845 'post_type' => 'page', 846 'post_author' => $author_1, 847 ) 848 ); 849 $pages = get_pages( 850 array( 851 'authors' => $author_1, 852 ) 853 ); 854 855 $this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) ); 856 } 857 858 /** 859 * @ticket 12821 860 */ 861 public function test_get_pages_multiple_authors() { 862 $author_1 = self::factory()->user->create( 863 array( 864 'user_login' => 'author1', 865 'role' => 'author', 866 ) 867 ); 877 868 $post_1 = self::factory()->post->create( 878 869 array( … … 909 900 /** 910 901 * @ticket 12821 911 * @covers ::get_pages 912 */ 913 public function test_get_pages_authors_names() { 902 */ 903 public function test_get_pages_multiple_authors_by_user_login() { 914 904 $author_1 = self::factory()->user->create( 915 905 array( … … 952 942 /** 953 943 * @ticket 12821 954 * @covers ::get_pages 955 */ 956 public function test_orderby() { 944 */ 945 public function test_get_pages_orderby() { 957 946 global $wpdb; 958 947 // 'rand' is a valid value. 959 948 get_pages( array( 'sort_column' => 'rand' ) ); 960 $this->assertStringContainsString( 'ORDER BY RAND()', $wpdb->last_query, 'Check order is random' ); 949 $this->assertStringContainsString( 950 'ORDER BY RAND()', 951 $wpdb->last_query, 952 'Check that ORDER is random.' 953 ); 961 954 962 955 // This isn't allowed. 963 956 get_pages( array( 'sort_order' => 'rand' ) ); 964 $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present' ); 965 $this->assertStringNotContainsString( 'RAND()', $wpdb->last_query, 'Check order is random is not present' ); 966 $this->assertStringContainsString( 'DESC', $wpdb->last_query, 'Check DESC is random is not present' ); 957 $this->assertStringContainsString( 958 'ORDER BY', 959 $wpdb->last_query, 960 'Check that ORDER BY is present.' 961 ); 962 $this->assertStringNotContainsString( 963 'RAND()', 964 $wpdb->last_query, 965 'Check that ORDER is not random.' 966 ); 967 $this->assertStringContainsString( 968 'DESC', 969 $wpdb->last_query, 970 'Check that DESC is not present.' 971 ); 967 972 968 973 // 'none' is a valid value. 969 974 get_pages( array( 'sort_column' => 'none' ) ); 970 $this->assertStringNotContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is not present' ); 971 $this->assertStringNotContainsString( 'DESC', $wpdb->last_query, 'Check DESC is not present' ); 972 $this->assertStringNotContainsString( 'ASC', $wpdb->last_query, 'Check ASC is not present' ); 975 $this->assertStringNotContainsString( 976 'ORDER BY', 977 $wpdb->last_query, 978 'Check that ORDER BY is not present.' 979 ); 980 $this->assertStringNotContainsString( 981 'DESC', 982 $wpdb->last_query, 983 'Check that DESC is not present.' 984 ); 985 $this->assertStringNotContainsString( 986 'ASC', 987 $wpdb->last_query, 988 'Check that ASC is not present.' 989 ); 973 990 974 991 // False is a valid value. 975 992 get_pages( array( 'sort_column' => false ) ); 976 $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present if sort_column equal false is passed.' ); 993 $this->assertStringContainsString( 994 'ORDER BY', 995 $wpdb->last_query, 996 'Check that ORDER BY is present if sort_column is false.' 997 ); 977 998 978 999 // Empty array() is a valid value. 979 1000 get_pages( array( 'sort_column' => array() ) ); 980 $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present if sort_column equals an empty array is passed.' ); 1001 $this->assertStringContainsString( 1002 'ORDER BY', 1003 $wpdb->last_query, 1004 'Check that ORDER BY is present if sort_column is an empty array.' 1005 ); 981 1006 } 982 1007 983 1008 /** 984 1009 * @ticket 12821 985 * @covers ::get_pages 986 */ 987 public function test_order() { 1010 */ 1011 public function test_get_pages_order() { 988 1012 global $wpdb; 989 1013 … … 996 1020 "ORDER BY $wpdb->posts.post_type ASC", 997 1021 $wpdb->last_query, 998 'Check order is post type'1022 'Check that ORDER is post type.' 999 1023 ); 1000 1024 … … 1008 1032 "ORDER BY $wpdb->posts.post_title DESC", 1009 1033 $wpdb->last_query, 1010 'Check order is default'1034 'Check that ORDER is default.' 1011 1035 ); 1012 1036 … … 1020 1044 "ORDER BY $wpdb->posts.post_date ASC", 1021 1045 $wpdb->last_query, 1022 'Check order is post date'1046 'Check that ORDER is post date.' 1023 1047 ); 1024 1048 }
Note: See TracChangeset
for help on using the changeset viewer.