Make WordPress Core

Changeset 51997


Ignore:
Timestamp:
11/03/2021 12:48:42 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Tests: Split WP_Posts_List_Table and WP_Comments_List_Table tests into two separate files for clarity.

These were previously combined in the includesListTable.php file. Since the tests were specific neither to the _get_list_table() function nor the parent WP_List_Table class, the naming was confusing, which should now be resolved.

Follow-up to [31730], [38854], [40297], [48151], [48521], [49190], [51993].

See #53363.

Location:
trunk/tests/phpunit/tests/admin
Files:
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/wpCommentsListTable.php

    r51995 r51997  
    44 * @group admin
    55 */
    6 class Tests_Admin_IncludesListTable extends WP_UnitTestCase {
    7         protected static $top           = array();
    8         protected static $children      = array();
    9         protected static $grandchildren = array();
    10         protected static $post_ids      = array();
     6class Tests_Admin_wpCommentsListTable extends WP_UnitTestCase {
    117
    128        /**
    13          * @var WP_Posts_List_Table
     9         * @var WP_Comments_List_Table
    1410         */
    1511        protected $table;
     
    1713        function set_up() {
    1814                parent::set_up();
    19                 $this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) );
    20         }
    21 
    22         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    23                 // Note that our top/children/grandchildren arrays are 1-indexed.
    24 
    25                 // Create top-level pages.
    26                 $num_posts = 5;
    27                 foreach ( range( 1, $num_posts ) as $i ) {
    28                         $p = $factory->post->create_and_get(
    29                                 array(
    30                                         'post_type'  => 'page',
    31                                         'post_title' => sprintf( 'Top Level Page %d', $i ),
    32                                 )
    33                         );
    34 
    35                         self::$top[ $i ]  = $p;
    36                         self::$post_ids[] = $p->ID;
    37                 }
    38 
    39                 // Create child pages.
    40                 $num_children = 3;
    41                 foreach ( self::$top as $top => $top_page ) {
    42                         foreach ( range( 1, $num_children ) as $i ) {
    43                                 $p = $factory->post->create_and_get(
    44                                         array(
    45                                                 'post_type'   => 'page',
    46                                                 'post_parent' => $top_page->ID,
    47                                                 'post_title'  => sprintf( 'Child %d', $i ),
    48                                         )
    49                                 );
    50 
    51                                 self::$children[ $top ][ $i ] = $p;
    52                                 self::$post_ids[]             = $p->ID;
    53                         }
    54                 }
    55 
    56                 // Create grand-child pages for the third and fourth top-level pages.
    57                 $num_grandchildren = 3;
    58                 foreach ( range( 3, 4 ) as $top ) {
    59                         foreach ( self::$children[ $top ] as $child => $child_page ) {
    60                                 foreach ( range( 1, $num_grandchildren ) as $i ) {
    61                                         $p = $factory->post->create_and_get(
    62                                                 array(
    63                                                         'post_type'   => 'page',
    64                                                         'post_parent' => $child_page->ID,
    65                                                         'post_title'  => sprintf( 'Grandchild %d', $i ),
    66                                                 )
    67                                         );
    68 
    69                                         self::$grandchildren[ $top ][ $child ][ $i ] = $p;
    70                                         self::$post_ids[]                            = $p->ID;
    71                                 }
    72                         }
    73                 }
    74         }
    75 
    76         /**
    77          * @ticket 15459
    78          *
    79          * @covers WP_Posts_List_Table::display_rows
    80          * @covers WP_Posts_List_Table::set_hierarchical_display
    81          */
    82         function test_list_hierarchical_pages_first_page() {
    83                 $this->_test_list_hierarchical_page(
    84                         array(
    85                                 'paged'          => 1,
    86                                 'posts_per_page' => 2,
    87                         ),
    88                         array(
    89                                 self::$top[1]->ID,
    90                                 self::$children[1][1]->ID,
    91                         )
    92                 );
    93         }
    94 
    95         /**
    96          * @ticket 15459
    97          *
    98          * @covers WP_Posts_List_Table::display_rows
    99          * @covers WP_Posts_List_Table::set_hierarchical_display
    100          */
    101         function test_list_hierarchical_pages_second_page() {
    102                 $this->_test_list_hierarchical_page(
    103                         array(
    104                                 'paged'          => 2,
    105                                 'posts_per_page' => 2,
    106                         ),
    107                         array(
    108                                 self::$top[1]->ID,
    109                                 self::$children[1][2]->ID,
    110                                 self::$children[1][3]->ID,
    111                         )
    112                 );
    113         }
    114 
    115         /**
    116          * @ticket 15459
    117          *
    118          * @covers WP_Posts_List_Table::display_rows
    119          * @covers WP_Posts_List_Table::set_hierarchical_display
    120          */
    121         function test_search_hierarchical_pages_first_page() {
    122                 $this->_test_list_hierarchical_page(
    123                         array(
    124                                 'paged'          => 1,
    125                                 'posts_per_page' => 2,
    126                                 's'              => 'Child',
    127                         ),
    128                         array(
    129                                 self::$children[1][1]->ID,
    130                                 self::$children[1][2]->ID,
    131                         )
    132                 );
    133         }
    134 
    135         /**
    136          * @ticket 15459
    137          *
    138          * @covers WP_Posts_List_Table::display_rows
    139          * @covers WP_Posts_List_Table::set_hierarchical_display
    140          */
    141         function test_search_hierarchical_pages_second_page() {
    142                 $this->_test_list_hierarchical_page(
    143                         array(
    144                                 'paged'          => 2,
    145                                 'posts_per_page' => 2,
    146                                 's'              => 'Top',
    147                         ),
    148                         array(
    149                                 self::$top[3]->ID,
    150                                 self::$top[4]->ID,
    151                         )
    152                 );
    153         }
    154 
    155         /**
    156          * @ticket 15459
    157          *
    158          * @covers WP_Posts_List_Table::display_rows
    159          * @covers WP_Posts_List_Table::set_hierarchical_display
    160          */
    161         function test_grandchildren_hierarchical_pages_first_page() {
    162                 // Page 6 is the first page with grandchildren.
    163                 $this->_test_list_hierarchical_page(
    164                         array(
    165                                 'paged'          => 6,
    166                                 'posts_per_page' => 2,
    167                         ),
    168                         array(
    169                                 self::$top[3]->ID,
    170                                 self::$children[3][1]->ID,
    171                                 self::$grandchildren[3][1][1]->ID,
    172                                 self::$grandchildren[3][1][2]->ID,
    173                         )
    174                 );
    175         }
    176 
    177         /**
    178          * @ticket 15459
    179          *
    180          * @covers WP_Posts_List_Table::display_rows
    181          * @covers WP_Posts_List_Table::set_hierarchical_display
    182          */
    183         function test_grandchildren_hierarchical_pages_second_page() {
    184                 // Page 7 is the second page with grandchildren.
    185                 $this->_test_list_hierarchical_page(
    186                         array(
    187                                 'paged'          => 7,
    188                                 'posts_per_page' => 2,
    189                         ),
    190                         array(
    191                                 self::$top[3]->ID,
    192                                 self::$children[3][1]->ID,
    193                                 self::$grandchildren[3][1][3]->ID,
    194                                 self::$children[3][2]->ID,
    195                         )
    196                 );
    197         }
    198 
    199         /**
    200          * Helper function to test the output of a page which uses `WP_Posts_List_Table`.
    201          *
    202          * @param array $args         Query args for the list of pages.
    203          * @param array $expected_ids Expected IDs of pages returned.
    204          */
    205         protected function _test_list_hierarchical_page( array $args, array $expected_ids ) {
    206                 if ( PHP_VERSION_ID >= 80100 ) {
    207                         /*
    208                          * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    209                          * via hooked in filter functions until a more structural solution to the
    210                          * "missing input validation" conundrum has been architected and implemented.
    211                          */
    212                         $this->expectDeprecation();
    213                         $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    214                 }
    215 
    216                 $matches = array();
    217 
    218                 $_REQUEST['paged']   = $args['paged'];
    219                 $GLOBALS['per_page'] = $args['posts_per_page'];
    220 
    221                 $args = array_merge(
    222                         array(
    223                                 'post_type' => 'page',
    224                         ),
    225                         $args
    226                 );
    227 
    228                 // Mimic the behaviour of `wp_edit_posts_query()`:
    229                 if ( ! isset( $args['orderby'] ) ) {
    230                         $args['orderby']                = 'menu_order title';
    231                         $args['order']                  = 'asc';
    232                         $args['posts_per_page']         = -1;
    233                         $args['posts_per_archive_page'] = -1;
    234                 }
    235 
    236                 // Effectively ignore the output until retrieving it later via `getActualOutput()`.
    237                 $this->expectOutputRegex( '`.`' );
    238 
    239                 $pages = new WP_Query( $args );
    240 
    241                 $this->table->set_hierarchical_display( true );
    242                 $this->table->display_rows( $pages->posts );
    243                 $output = $this->getActualOutput();
    244 
    245                 // Clean up.
    246                 unset( $_REQUEST['paged'] );
    247                 unset( $GLOBALS['per_page'] );
    248 
    249                 preg_match_all( '|<tr[^>]*>|', $output, $matches );
    250 
    251                 $this->assertCount( count( $expected_ids ), array_keys( $matches[0] ) );
    252 
    253                 foreach ( $expected_ids as $id ) {
    254                         $this->assertStringContainsString( sprintf( 'id="post-%d"', $id ), $output );
    255                 }
    256         }
    257 
    258         /**
    259          * @ticket 37407
    260          *
    261          * @covers WP_Posts_List_Table::extra_tablenav
    262          */
    263         function test_filter_button_should_not_be_shown_if_there_are_no_posts() {
    264                 // Set post type to a non-existent one.
    265                 $this->table->screen->post_type = 'foo';
    266 
    267                 ob_start();
    268                 $this->table->extra_tablenav( 'top' );
    269                 $output = ob_get_clean();
    270 
    271                 $this->assertStringNotContainsString( 'id="post-query-submit"', $output );
    272         }
    273 
    274         /**
    275          * @ticket 37407
    276          *
    277          * @covers WP_Posts_List_Table::extra_tablenav
    278          */
    279         function test_months_dropdown_should_not_be_shown_if_there_are_no_posts() {
    280                 // Set post type to a non-existent one.
    281                 $this->table->screen->post_type = 'foo';
    282 
    283                 ob_start();
    284                 $this->table->extra_tablenav( 'top' );
    285                 $output = ob_get_clean();
    286 
    287                 $this->assertStringNotContainsString( 'id="filter-by-date"', $output );
    288         }
    289 
    290         /**
    291          * @ticket 37407
    292          *
    293          * @covers WP_Posts_List_Table::extra_tablenav
    294          */
    295         function test_category_dropdown_should_not_be_shown_if_there_are_no_posts() {
    296                 // Set post type to a non-existent one.
    297                 $this->table->screen->post_type = 'foo';
    298 
    299                 ob_start();
    300                 $this->table->extra_tablenav( 'top' );
    301                 $output = ob_get_clean();
    302 
    303                 $this->assertStringNotContainsString( 'id="cat"', $output );
    304         }
    305 
    306         /**
    307          * @ticket 38341
    308          *
    309          * @covers WP_Posts_List_Table::extra_tablenav
    310          */
    311         public function test_empty_trash_button_should_not_be_shown_if_there_are_no_posts() {
    312                 // Set post type to a non-existent one.
    313                 $this->table->screen->post_type = 'foo';
    314 
    315                 ob_start();
    316                 $this->table->extra_tablenav( 'top' );
    317                 $output = ob_get_clean();
    318 
    319                 $this->assertStringNotContainsString( 'id="delete_all"', $output );
     15                $this->table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    32016        }
    32117
     
    32622         */
    32723        public function test_filter_button_should_not_be_shown_if_there_are_no_comments() {
    328                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    329 
    33024                ob_start();
    331                 $table->extra_tablenav( 'top' );
     25                $this->table->extra_tablenav( 'top' );
    33226                $output = ob_get_clean();
    33327
     
    34943                );
    35044
    351                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    352                 $table->prepare_items();
     45                $this->table->prepare_items();
    35346
    35447                ob_start();
    355                 $table->extra_tablenav( 'top' );
     48                $this->table->extra_tablenav( 'top' );
    35649                $output = ob_get_clean();
    35750
     
    37366                );
    37467
    375                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    376                 $table->prepare_items();
     68                $this->table->prepare_items();
    37769
    37870                ob_start();
    379                 $table->extra_tablenav( 'top' );
     71                $this->table->extra_tablenav( 'top' );
    38072                $output = ob_get_clean();
    38173
     
    39082         */
    39183        public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comments() {
    392                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    393 
    39484                ob_start();
    395                 $table->extra_tablenav( 'top' );
     85                $this->table->extra_tablenav( 'top' );
    39686                $output = ob_get_clean();
    39787
     
    40595         */
    40696        public function test_bulk_action_menu_supports_options_and_optgroups() {
    407                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    408 
    40997                add_filter(
    41098                        'bulk_actions-edit-comments',
     
    421109
    422110                ob_start();
    423                 $table->bulk_actions();
     111                $this->table->bulk_actions();
    424112                $output = ob_get_clean();
    425113
     
    442130         */
    443131        public function test_sortable_columns() {
    444                 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
    445 
    446132                $override_sortable_columns = array(
    447133                        'author'   => array( 'comment_author', true ),
     
    478164         */
    479165        public function test_sortable_columns_with_current_ordering() {
    480                 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
    481 
    482166                $override_sortable_columns = array(
    483167                        'author'   => array( 'comment_author', false ),
  • trunk/tests/phpunit/tests/admin/wpPostsListTable.php

    r51996 r51997  
    44 * @group admin
    55 */
    6 class Tests_Admin_IncludesListTable extends WP_UnitTestCase {
     6class Tests_Admin_wpPostsListTable extends WP_UnitTestCase {
    77        protected static $top           = array();
    88        protected static $children      = array();
     
    320320        }
    321321
    322         /**
    323          * @ticket 40188
    324          *
    325          * @covers WP_Posts_List_Table::extra_tablenav
    326          */
    327         public function test_filter_button_should_not_be_shown_if_there_are_no_comments() {
    328                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    329 
    330                 ob_start();
    331                 $table->extra_tablenav( 'top' );
    332                 $output = ob_get_clean();
    333 
    334                 $this->assertStringNotContainsString( 'id="post-query-submit"', $output );
    335         }
    336 
    337         /**
    338          * @ticket 40188
    339          *
    340          * @covers WP_Posts_List_Table::extra_tablenav
    341          */
    342         public function test_filter_button_should_be_shown_if_there_are_comments() {
    343                 $post_id    = self::factory()->post->create();
    344                 $comment_id = self::factory()->comment->create(
    345                         array(
    346                                 'comment_post_ID'  => $post_id,
    347                                 'comment_approved' => '1',
    348                         )
    349                 );
    350 
    351                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    352                 $table->prepare_items();
    353 
    354                 ob_start();
    355                 $table->extra_tablenav( 'top' );
    356                 $output = ob_get_clean();
    357 
    358                 $this->assertStringContainsString( 'id="post-query-submit"', $output );
    359         }
    360 
    361         /**
    362          * @ticket 40188
    363          *
    364          * @covers WP_Posts_List_Table::extra_tablenav
    365          */
    366         public function test_filter_comment_type_dropdown_should_be_shown_if_there_are_comments() {
    367                 $post_id    = self::factory()->post->create();
    368                 $comment_id = self::factory()->comment->create(
    369                         array(
    370                                 'comment_post_ID'  => $post_id,
    371                                 'comment_approved' => '1',
    372                         )
    373                 );
    374 
    375                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    376                 $table->prepare_items();
    377 
    378                 ob_start();
    379                 $table->extra_tablenav( 'top' );
    380                 $output = ob_get_clean();
    381 
    382                 $this->assertStringContainsString( 'id="filter-by-comment-type"', $output );
    383                 $this->assertStringContainsString( "<option value='comment'>", $output );
    384         }
    385 
    386         /**
    387          * @ticket 38341
    388          *
    389          * @covers WP_Posts_List_Table::extra_tablenav
    390          */
    391         public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comments() {
    392                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    393 
    394                 ob_start();
    395                 $table->extra_tablenav( 'top' );
    396                 $output = ob_get_clean();
    397 
    398                 $this->assertStringNotContainsString( 'id="delete_all"', $output );
    399         }
    400 
    401         /**
    402          * @ticket 19278
    403          *
    404          * @covers WP_Posts_List_Table::bulk_actions
    405          */
    406         public function test_bulk_action_menu_supports_options_and_optgroups() {
    407                 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
    408 
    409                 add_filter(
    410                         'bulk_actions-edit-comments',
    411                         static function() {
    412                                 return array(
    413                                         'delete'       => 'Delete',
    414                                         'Change State' => array(
    415                                                 'feature' => 'Featured',
    416                                                 'sale'    => 'On Sale',
    417                                         ),
    418                                 );
    419                         }
    420                 );
    421 
    422                 ob_start();
    423                 $table->bulk_actions();
    424                 $output = ob_get_clean();
    425 
    426                 $expected = <<<'OPTIONS'
    427 <option value="delete">Delete</option>
    428         <optgroup label="Change State">
    429                 <option value="feature">Featured</option>
    430                 <option value="sale">On Sale</option>
    431         </optgroup>
    432 OPTIONS;
    433                 $expected = str_replace( "\r\n", "\n", $expected );
    434 
    435                 $this->assertStringContainsString( $expected, $output );
    436         }
    437 
    438         /**
    439          * @ticket 45089
    440          *
    441          * @covers WP_Posts_List_Table::print_column_headers
    442          */
    443         public function test_sortable_columns() {
    444                 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
    445 
    446                 $override_sortable_columns = array(
    447                         'author'   => array( 'comment_author', true ),
    448                         'response' => 'comment_post_ID',
    449                         'date'     => array( 'comment_date', 'dEsC' ), // The ordering support should be case-insensitive.
    450                 );
    451 
    452                 // Stub the get_sortable_columns() method.
    453                 $object = $this->getMockBuilder( 'WP_Comments_List_Table' )
    454                         ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
    455                         ->setMethods( array( 'get_sortable_columns' ) )
    456                         ->getMock();
    457 
    458                 // Change the null return value of the stubbed get_sortable_columns() method.
    459                 $object->method( 'get_sortable_columns' )
    460                         ->willReturn( $override_sortable_columns );
    461 
    462                 $output = get_echo( array( $object, 'print_column_headers' ) );
    463 
    464                 $this->assertStringContainsString( '?orderby=comment_author&#038;order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
    465                 $this->assertStringContainsString( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
    466 
    467                 $this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
    468                 $this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
    469 
    470                 $this->assertStringContainsString( '?orderby=comment_date&#038;order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
    471                 $this->assertStringContainsString( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
    472         }
    473 
    474         /**
    475          * @ticket 45089
    476          *
    477          * @covers WP_Posts_List_Table::print_column_headers
    478          */
    479         public function test_sortable_columns_with_current_ordering() {
    480                 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
    481 
    482                 $override_sortable_columns = array(
    483                         'author'   => array( 'comment_author', false ),
    484                         'response' => 'comment_post_ID',
    485                         'date'     => array( 'comment_date', 'asc' ), // We will override this with current ordering.
    486                 );
    487 
    488                 // Current ordering.
    489                 $_GET['orderby'] = 'comment_date';
    490                 $_GET['order']   = 'desc';
    491 
    492                 // Stub the get_sortable_columns() method.
    493                 $object = $this->getMockBuilder( 'WP_Comments_List_Table' )
    494                         ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
    495                         ->setMethods( array( 'get_sortable_columns' ) )
    496                         ->getMock();
    497 
    498                 // Change the null return value of the stubbed get_sortable_columns() method.
    499                 $object->method( 'get_sortable_columns' )
    500                         ->willReturn( $override_sortable_columns );
    501 
    502                 $output = get_echo( array( $object, 'print_column_headers' ) );
    503 
    504                 $this->assertStringContainsString( '?orderby=comment_author&#038;order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
    505                 $this->assertStringContainsString( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
    506 
    507                 $this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
    508                 $this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
    509 
    510                 $this->assertStringContainsString( '?orderby=comment_date&#038;order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
    511                 $this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
    512         }
    513 
    514322}
Note: See TracChangeset for help on using the changeset viewer.