Make WordPress Core

Changeset 55583


Ignore:
Timestamp:
03/23/2023 11:35:21 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Improve get_pages() tests organization.

Includes:

  • Renaming some tests for clarity.
  • Moving some tests to a more appropriate place.
  • Moving the @covers tag to the top of the class.
  • Using consistent formatting for assertion messages.

Follow-up to [27767], [41849], [44587], [55569].

See #57841.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/getPages.php

    r55569 r55583  
    33/**
    44 * @group post
     5 *
     6 * @covers ::get_pages
    57 */
    6 
    78class Tests_Post_GetPages extends WP_UnitTestCase {
    89        /**
     
    101102
    102103        /**
     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        /**
    103126         * @ticket 40669
    104127         */
    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() {
    106129                $posts = self::factory()->post->create_many(
    107130                        2,
     
    139162         * @ticket 40669
    140163         */
    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() {
    142165                $posts = self::factory()->post->create_many(
    143166                        2,
     
    174197
    175198        /**
    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() {
    180202                $posts = self::factory()->post->create_many(
    181203                        2,
     
    185207                );
    186208
    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(
    190214                                'meta_key'   => 'foo',
    191215                                'meta_value' => 'bar',
     
    193217                );
    194218
    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(
    244225                        array(
    245226                                'meta_key'   => 'foo',
     
    248229                );
    249230
    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 
    262231                $found_ids = wp_list_pluck( $found, 'ID' );
    263232                $this->assertSameSets( array( $posts[0] ), $found_ids );
     
    267236         * @ticket 40669
    268237         */
    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() {
    270239                $posts = self::factory()->post->create_many(
    271240                        2,
     
    362331
    363332        /**
     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        /**
    364432         * @ticket 9470
    365433         */
     
    433501         * @ticket 22208
    434502         */
    435         public function test_get_chidren_fields_ids() {
     503        public function test_get_children_fields_ids() {
    436504                $post_id   = self::factory()->post->create();
    437505                $child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) );
     
    706774        }
    707775
    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 43514
    757          */
    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 
    777776        /**
    778777         * @ticket 12821
    779          * @covers ::get_pages
    780778         */
    781779        public function test_get_pages_post_type() {
     
    790788        }
    791789
    792 
    793790        /**
    794791         * @ticket 12821
    795          * @covers ::get_pages
    796          */
    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 12821
    823          * @covers ::get_pages
    824792         */
    825793        public function test_get_pages_post_status() {
     
    849817        /**
    850818         * @ticket 12821
    851          * @covers ::get_pages
    852819         */
    853820        public function test_get_pages_offset() {
     
    863830        }
    864831
    865 
    866832        /**
    867833         * @ticket 12821
    868          * @covers ::get_pages
    869          */
    870         public function test_get_pages_authors() {
     834         */
     835        public function test_get_pages_author() {
    871836                $author_1 = self::factory()->user->create(
    872837                        array(
     
    875840                        )
    876841                );
     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                );
    877868                $post_1   = self::factory()->post->create(
    878869                        array(
     
    909900        /**
    910901         * @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() {
    914904                $author_1 = self::factory()->user->create(
    915905                        array(
     
    952942        /**
    953943         * @ticket 12821
    954          * @covers ::get_pages
    955          */
    956         public function test_orderby() {
     944         */
     945        public function test_get_pages_orderby() {
    957946                global $wpdb;
    958947                // 'rand' is a valid value.
    959948                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                );
    961954
    962955                // This isn't allowed.
    963956                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                );
    967972
    968973                // 'none' is a valid value.
    969974                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                );
    973990
    974991                // False is a valid value.
    975992                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                );
    977998
    978999                // Empty array() is a valid value.
    9791000                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                );
    9811006        }
    9821007
    9831008        /**
    9841009         * @ticket 12821
    985          * @covers ::get_pages
    986          */
    987         public function test_order() {
     1010         */
     1011        public function test_get_pages_order() {
    9881012                global $wpdb;
    9891013
     
    9961020                        "ORDER BY $wpdb->posts.post_type ASC",
    9971021                        $wpdb->last_query,
    998                         'Check order is post type'
     1022                        'Check that ORDER is post type.'
    9991023                );
    10001024
     
    10081032                        "ORDER BY $wpdb->posts.post_title DESC",
    10091033                        $wpdb->last_query,
    1010                         'Check order is default'
     1034                        'Check that ORDER is default.'
    10111035                );
    10121036
     
    10201044                        "ORDER BY $wpdb->posts.post_date ASC",
    10211045                        $wpdb->last_query,
    1022                         'Check order is post date'
     1046                        'Check that ORDER is post date.'
    10231047                );
    10241048        }
Note: See TracChangeset for help on using the changeset viewer.