Make WordPress Core

Changeset 54215


Ignore:
Timestamp:
09/19/2022 09:06:08 PM (9 months ago)
Author:
davidbaumwald
Message:

Administration: Add new get_views_links method to WP_List_Table.

Many WP_List_Table child classes in core use mostly the same code to create their "view" links markup. To DRY-up the code, a new WP_List_Table->get_view_links method is being introduced to consolidate the HTML link generation when provided an array of links.

This change also implements this new method in the relevant WP_List_Table_xxx child classes get_views methods. Finally, unit tests are being added to validate view links markup and test for some "unhappy paths".

Props afercia, costdev, garrett-eclipse, Dharm1025, juhise, peterwilsoncc.
Fixes #42066.

Location:
trunk
Files:
7 added
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r54071 r54215  
    294294
    295295        foreach ( $stati as $status => $label ) {
    296             $current_link_attributes = '';
    297 
    298             if ( $status === $comment_status ) {
    299                 $current_link_attributes = ' class="current" aria-current="page"';
    300             }
    301 
    302296            if ( 'mine' === $status ) {
    303297                $current_user_id    = get_current_user_id();
     
    330324            */
    331325
    332             $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . sprintf(
    333                 translate_nooped_plural( $label, $num_comments->$status ),
    334                 sprintf(
    335                     '<span class="%s-count">%s</span>',
    336                     ( 'moderated' === $status ) ? 'pending' : $status,
    337                     number_format_i18n( $num_comments->$status )
    338                 )
    339             ) . '</a>';
     326            $status_links[ $status ] = array(
     327                'url'     => esc_url( $link ),
     328                'label'   => sprintf(
     329                    translate_nooped_plural( $label, $num_comments->$status ),
     330                    sprintf(
     331                        '<span class="%s-count">%s</span>',
     332                        ( 'moderated' === $status ) ? 'pending' : $status,
     333                        number_format_i18n( $num_comments->$status )
     334                    )
     335                ),
     336                'current' => $status === $comment_status,
     337            );
    340338        }
    341339
     
    349347         *                              'Pending', 'Approved', 'Spam', and 'Trash'.
    350348         */
    351         return apply_filters( 'comment_status_links', $status_links );
     349        return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
    352350    }
    353351
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r54133 r54215  
    15151515
    15161516    /**
     1517     * Generates views links.
     1518     *
     1519     * @since 6.1.0
     1520     *
     1521     * @param array $link_data {
     1522     *     An array of link data.
     1523     *
     1524     *     @type string $url     The link URL.
     1525     *     @type string $label   The link label.
     1526     *     @type bool   $current Optional. Whether this is the currently selected view.
     1527     * }
     1528     * @return array An array of link markup. Keys match the $link_data input array.
     1529     */
     1530    protected function get_views_links( $link_data = array() ) {
     1531        if ( ! is_array( $link_data ) ) {
     1532            _doing_it_wrong(
     1533                __METHOD__,
     1534                sprintf(
     1535                    /* translators: %s: The $link_data argument. */
     1536                    __( 'The <code>%s</code> argument must be an array.' ),
     1537                    '$link_data'
     1538                ),
     1539                '6.1.0'
     1540            );
     1541
     1542            return array( '' );
     1543        }
     1544
     1545        $views_links = array();
     1546        foreach ( $link_data as $view => $link ) {
     1547            if ( empty( $link['url'] ) || ! is_string( $link['url'] ) || '' === trim( $link['url'] ) ) {
     1548                _doing_it_wrong(
     1549                    __METHOD__,
     1550                    sprintf(
     1551                        /* translators: %1$s: The argument name. %2$s: The view name. */
     1552                        __( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
     1553                        'url',
     1554                        esc_html( $view )
     1555                    ),
     1556                    '6.1.0'
     1557                );
     1558
     1559                continue;
     1560            }
     1561
     1562            if ( empty( $link['label'] ) || ! is_string( $link['label'] ) || '' === trim( $link['label'] ) ) {
     1563                _doing_it_wrong(
     1564                    __METHOD__,
     1565                    sprintf(
     1566                        /* translators: %1$s: The argument name. %2$s: The view name. */
     1567                        __( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
     1568                        'label',
     1569                        esc_html( $view )
     1570                    ),
     1571                    '6.1.0'
     1572                );
     1573
     1574                continue;
     1575            }
     1576
     1577            $views_links[ $view ] = sprintf(
     1578                '<a href="%s"%s>%s</a>',
     1579                esc_url( $link['url'] ),
     1580                isset( $link['current'] ) && true === $link['current'] ? ' class="current" aria-current="page"' : '',
     1581                $link['label']
     1582            );
     1583        }
     1584
     1585        return $views_links;
     1586    }
     1587
     1588    /**
    15171589     * Sends required variables to JavaScript land.
    15181590     *
  • trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php

    r54071 r54215  
    263263
    264264        foreach ( $statuses as $status => $label_count ) {
    265             $current_link_attributes = $requested_status === $status || ( '' === $requested_status && 'all' === $status )
    266                 ? ' class="current" aria-current="page"'
    267                 : '';
    268265            if ( (int) $counts[ $status ] > 0 ) {
    269266                $label    = sprintf( translate_nooped_plural( $label_count, $counts[ $status ] ), number_format_i18n( $counts[ $status ] ) );
    270267                $full_url = 'all' === $status ? $url : add_query_arg( 'status', $status, $url );
    271268
    272                 $view_links[ $status ] = sprintf(
    273                     '<a href="%1$s"%2$s>%3$s</a>',
    274                     esc_url( $full_url ),
    275                     $current_link_attributes,
    276                     $label
     269                $view_links[ $status ] = array(
     270                    'url'     => esc_url( $full_url ),
     271                    'label'   => $label,
     272                    'current' => $requested_status === $status || ( '' === $requested_status && 'all' === $status ),
    277273                );
    278274            }
    279275        }
    280276
    281         return $view_links;
     277        return $this->get_views_links( $view_links );
    282278    }
    283279
  • trunk/src/wp-admin/includes/class-wp-ms-themes-list-table.php

    r51795 r54215  
    445445
    446446            if ( 'search' !== $type ) {
    447                 $status_links[ $type ] = sprintf(
    448                     "<a href='%s'%s>%s</a>",
    449                     esc_url( add_query_arg( 'theme_status', $type, $url ) ),
    450                     ( $type === $status ) ? ' class="current" aria-current="page"' : '',
    451                     sprintf( $text, number_format_i18n( $count ) )
     447                $status_links[ $type ] = array(
     448                    'url'     => esc_url( add_query_arg( 'theme_status', $type, $url ) ),
     449                    'label'   => sprintf( $text, number_format_i18n( $count ) ),
     450                    'current' => $type === $status,
    452451                );
    453452            }
    454453        }
    455454
    456         return $status_links;
     455        return $this->get_views_links( $status_links );
    457456    }
    458457
  • trunk/src/wp-admin/includes/class-wp-ms-users-list-table.php

    r54071 r54215  
    138138        $total_admins = count( $super_admins );
    139139
    140         $current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : '';
    141         $role_links              = array();
    142         $role_links['all']       = sprintf(
    143             '<a href="%s"%s>%s</a>',
    144             network_admin_url( 'users.php' ),
    145             $current_link_attributes,
    146             sprintf(
     140        $role_links        = array();
     141        $role_links['all'] = array(
     142            'url'     => network_admin_url( 'users.php' ),
     143            'label'   => sprintf(
    147144                /* translators: Number of users. */
    148145                _nx(
     
    153150                ),
    154151                number_format_i18n( $total_users )
    155             )
    156         );
    157         $current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : '';
    158         $role_links['super']     = sprintf(
    159             '<a href="%s"%s>%s</a>',
    160             network_admin_url( 'users.php?role=super' ),
    161             $current_link_attributes,
    162             sprintf(
     152            ),
     153            'current' => 'super' !== $role,
     154        );
     155
     156        $role_links['super'] = array(
     157            'url'     => network_admin_url( 'users.php?role=super' ),
     158            'label'   => sprintf(
    163159                /* translators: Number of users. */
    164160                _n(
     
    168164                ),
    169165                number_format_i18n( $total_admins )
    170             )
    171         );
    172 
    173         return $role_links;
     166            ),
     167            'current' => 'super' === $role,
     168        );
     169
     170        return $this->get_views_links( $role_links );
    174171    }
    175172
  • trunk/src/wp-admin/includes/class-wp-plugin-install-list-table.php

    r54063 r54215  
    311311        $display_tabs = array();
    312312        foreach ( (array) $tabs as $action => $text ) {
    313             $current_link_attributes                     = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
    314             $href                                        = self_admin_url( 'plugin-install.php?tab=' . $action );
    315             $display_tabs[ 'plugin-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
     313            $display_tabs[ 'plugin-install-' . $action ] = array(
     314                'url'     => self_admin_url( 'plugin-install.php?tab=' . $action ),
     315                'label'   => $text,
     316                'current' => $action === $tab,
     317            );
    316318        }
    317319        // No longer a real tab.
    318320        unset( $display_tabs['plugin-install-upload'] );
    319321
    320         return $display_tabs;
     322        return $this->get_views_links( $display_tabs );
    321323    }
    322324
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r54062 r54215  
    577577
    578578            if ( 'search' !== $type ) {
    579                 $status_links[ $type ] = sprintf(
    580                     "<a href='%s'%s>%s</a>",
    581                     add_query_arg( 'plugin_status', $type, 'plugins.php' ),
    582                     ( $type === $status ) ? ' class="current" aria-current="page"' : '',
    583                     sprintf( $text, number_format_i18n( $count ) )
     579                $status_links[ $type ] = array(
     580                    'url'     => add_query_arg( 'plugin_status', $type, 'plugins.php' ),
     581                    'label'   => sprintf( $text, number_format_i18n( $count ) ),
     582                    'current' => $type === $status,
    584583                );
    585584            }
    586585        }
    587586
    588         return $status_links;
     587        return $this->get_views_links( $status_links );
    589588    }
    590589
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r54099 r54215  
    332332            );
    333333
    334             $mine = $this->get_edit_link( $mine_args, $mine_inner_html, $class );
     334            $mine = array(
     335                'url'     => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
     336                'label'   => $mine_inner_html,
     337                'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
     338            );
    335339
    336340            $all_args['all_posts'] = 1;
    337341            $class                 = '';
    338         }
    339 
    340         if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ) ) {
    341             $class = 'current';
    342342        }
    343343
     
    353353        );
    354354
    355         $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
     355        $status_links['all'] = array(
     356            'url'     => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
     357            'label'   => $all_inner_html,
     358            'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
     359        );
    356360
    357361        if ( $mine ) {
     
    382386            );
    383387
    384             $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class );
     388            $status_links[ $status_name ] = array(
     389                'url'     => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
     390                'label'   => $status_label,
     391                'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
     392            );
    385393        }
    386394
     
    405413
    406414            $sticky_link = array(
    407                 'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class ),
     415                'sticky' => array(
     416                    'url'     => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
     417                    'label'   => $sticky_inner_html,
     418                    'current' => ! empty( $_REQUEST['show_sticky'] ),
     419                ),
    408420            );
    409421
     
    413425        }
    414426
    415         return $status_links;
     427        return $this->get_views_links( $status_links );
    416428    }
    417429
  • trunk/src/wp-admin/includes/class-wp-privacy-requests-table.php

    r52239 r54215  
    154154        $admin_url = $this->get_admin_url();
    155155
    156         $current_link_attributes = empty( $current_status ) ? ' class="current" aria-current="page"' : '';
    157         $status_label            = sprintf(
     156        $status_label = sprintf(
    158157            /* translators: %s: Number of requests. */
    159158            _nx(
     
    166165        );
    167166
    168         $views['all'] = sprintf(
    169             '<a href="%s"%s>%s</a>',
    170             esc_url( $admin_url ),
    171             $current_link_attributes,
    172             $status_label
     167        $views['all'] = array(
     168            'url'     => esc_url( $admin_url ),
     169            'label'   => $status_label,
     170            'current' => empty( $current_status ),
    173171        );
    174172
     
    179177            }
    180178
    181             $current_link_attributes = $status === $current_status ? ' class="current" aria-current="page"' : '';
    182             $total_status_requests   = absint( $counts->{$status} );
     179            $total_status_requests = absint( $counts->{$status} );
    183180
    184181            if ( ! $total_status_requests ) {
     
    193190            $status_link = add_query_arg( 'filter-status', $status, $admin_url );
    194191
    195             $views[ $status ] = sprintf(
    196                 '<a href="%s"%s>%s</a>',
    197                 esc_url( $status_link ),
    198                 $current_link_attributes,
    199                 $status_label
     192            $views[ $status ] = array(
     193                'url'     => esc_url( $status_link ),
     194                'label'   => $status_label,
     195                'current' => $status === $current_status,
    200196            );
    201197        }
    202198
    203         return $views;
     199        return $this->get_views_links( $views );
    204200    }
    205201
  • trunk/src/wp-admin/includes/class-wp-theme-install-list-table.php

    r52947 r54215  
    187187        $display_tabs = array();
    188188        foreach ( (array) $tabs as $action => $text ) {
    189             $current_link_attributes                    = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
    190             $href                                       = self_admin_url( 'theme-install.php?tab=' . $action );
    191             $display_tabs[ 'theme-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
    192         }
    193 
    194         return $display_tabs;
     189            $display_tabs[ 'theme-install-' . $action ] = array(
     190                'url'     => self_admin_url( 'theme-install.php?tab=' . $action ),
     191                'label'   => $text,
     192                'current' => $action === $tab,
     193            );
     194        }
     195
     196        return $this->get_views_links( $display_tabs );
    195197    }
    196198
  • trunk/src/wp-admin/includes/class-wp-users-list-table.php

    r54070 r54215  
    186186        }
    187187
    188         $role_links              = array();
    189         $avail_roles             = array();
    190         $all_text                = __( 'All' );
    191         $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
     188        $role_links  = array();
     189        $avail_roles = array();
     190        $all_text    = __( 'All' );
    192191
    193192        if ( $count_users ) {
     
    216215        }
    217216
    218         $role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text );
     217        $role_links['all'] = array(
     218            'url'     => $url,
     219            'label'   => $all_text,
     220            'current' => empty( $role ),
     221        );
    219222
    220223        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    221224            if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
    222225                continue;
    223             }
    224 
    225             $current_link_attributes = '';
    226 
    227             if ( $this_role === $role ) {
    228                 $current_link_attributes = ' class="current" aria-current="page"';
    229226            }
    230227
     
    239236            }
    240237
    241             $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
     238            $role_links[ $this_role ] = array(
     239                'url'     => esc_url( add_query_arg( 'role', $this_role, $url ) ),
     240                'label'   => $name,
     241                'current' => $this_role === $role,
     242            );
    242243        }
    243244
    244245        if ( ! empty( $avail_roles['none'] ) ) {
    245 
    246             $current_link_attributes = '';
    247 
    248             if ( 'none' === $role ) {
    249                 $current_link_attributes = ' class="current" aria-current="page"';
    250             }
    251246
    252247            $name = __( 'No role' );
     
    258253            );
    259254
    260             $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$current_link_attributes>$name</a>";
    261         }
    262 
    263         return $role_links;
     255            $role_links['none'] = array(
     256                'url'     => esc_url( add_query_arg( 'role', 'none', $url ) ),
     257                'label'   => $name,
     258                'current' => 'none' === $role,
     259            );
     260        }
     261
     262        return $this->get_views_links( $role_links );
    264263    }
    265264
  • trunk/tests/phpunit/tests/admin/wpCommentsListTable.php

    r51998 r54215  
    196196    }
    197197
     198    /**
     199     * @ticket 42066
     200     *
     201     * @covers WP_Comments_List_Table::get_views
     202     */
     203    public function test_get_views_should_return_views_by_default() {
     204        $this->table->prepare_items();
     205
     206        $expected = array(
     207            'all'       => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=all" class="current" aria-current="page">All <span class="count">(<span class="all-count">0</span>)</span></a>',
     208            'mine'      => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=mine&#038;user_id=0">Mine <span class="count">(<span class="mine-count">0</span>)</span></a>',
     209            'moderated' => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=moderated">Pending <span class="count">(<span class="pending-count">0</span>)</span></a>',
     210            'approved'  => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=approved">Approved <span class="count">(<span class="approved-count">0</span>)</span></a>',
     211            'spam'      => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=spam">Spam <span class="count">(<span class="spam-count">0</span>)</span></a>',
     212            'trash'     => '<a href="http://example.org/wp-admin/edit-comments.php?comment_status=trash">Trash <span class="count">(<span class="trash-count">0</span>)</span></a>',
     213        );
     214        $this->assertSame( $expected, $this->table->get_views() );
     215    }
     216
    198217}
  • trunk/tests/phpunit/tests/admin/wpListTable.php

    r53868 r54215  
    77 */
    88class Tests_Admin_WpListTable extends WP_UnitTestCase {
     9
     10    /**
     11     * List table.
     12     *
     13     * @var WP_List_Table $list_table
     14     */
     15    protected static $list_table;
     16
     17    public static function set_up_before_class() {
     18        global $hook_suffix;
     19
     20        parent::set_up_before_class();
     21
     22        require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
     23
     24        $hook_suffix      = '_wp_tests';
     25        self::$list_table = new WP_List_Table();
     26    }
    927
    1028    /**
     
    105123        return $datasets;
    106124    }
     125
     126    /**
     127     * Tests the "get_views_links()" method.
     128     *
     129     * @ticket 42066
     130     *
     131     * @covers WP_List_Table::get_views_links
     132     *
     133     * @dataProvider data_get_views_links
     134     *
     135     * @param array $link_data {
     136     *     An array of link data.
     137     *
     138     *     @type string $url     The link URL.
     139     *     @type string $label   The link label.
     140     *     @type bool   $current Optional. Whether this is the currently selected view.
     141     * }
     142     * @param array $expected
     143     */
     144    public function test_get_views_links( $link_data, $expected ) {
     145        $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' );
     146        $get_views_links->setAccessible( true );
     147
     148        $actual = $get_views_links->invokeArgs( self::$list_table, array( $link_data ) );
     149
     150        $this->assertSameSetsWithIndex( $expected, $actual );
     151    }
     152
     153    /**
     154     * Data provider.
     155     *
     156     * @return array
     157     */
     158    public function data_get_views_links() {
     159        return array(
     160            'one "current" link'                           => array(
     161                'link_data' => array(
     162                    'all'       => array(
     163                        'url'     => 'https://example.org/',
     164                        'label'   => 'All',
     165                        'current' => true,
     166                    ),
     167                    'activated' => array(
     168                        'url'     => add_query_arg( 'status', 'activated', 'https://example.org/' ),
     169                        'label'   => 'Activated',
     170                        'current' => false,
     171                    ),
     172                ),
     173                'expected'  => array(
     174                    'all'       => '<a href="https://example.org/" class="current" aria-current="page">All</a>',
     175                    'activated' => '<a href="https://example.org/?status=activated">Activated</a>',
     176                ),
     177            ),
     178            'two "current" links'                          => array(
     179                'link_data' => array(
     180                    'all'       => array(
     181                        'url'     => 'https://example.org/',
     182                        'label'   => 'All',
     183                        'current' => true,
     184                    ),
     185                    'activated' => array(
     186                        'url'     => add_query_arg( 'status', 'activated', 'https://example.org/' ),
     187                        'label'   => 'Activated',
     188                        'current' => true,
     189                    ),
     190                ),
     191                'expected'  => array(
     192                    'all'       => '<a href="https://example.org/" class="current" aria-current="page">All</a>',
     193                    'activated' => '<a href="https://example.org/?status=activated" class="current" aria-current="page">Activated</a>',
     194                ),
     195            ),
     196            'one "current" link and one without "current" key' => array(
     197                'link_data' => array(
     198                    'all'       => array(
     199                        'url'     => 'https://example.org/',
     200                        'label'   => 'All',
     201                        'current' => true,
     202                    ),
     203                    'activated' => array(
     204                        'url'   => add_query_arg( 'status', 'activated', 'https://example.org/' ),
     205                        'label' => 'Activated',
     206                    ),
     207                ),
     208                'expected'  => array(
     209                    'all'       => '<a href="https://example.org/" class="current" aria-current="page">All</a>',
     210                    'activated' => '<a href="https://example.org/?status=activated">Activated</a>',
     211                ),
     212            ),
     213            'one "current" link with escapable characters' => array(
     214                'link_data' => array(
     215                    'all'       => array(
     216                        'url'     => 'https://example.org/',
     217                        'label'   => 'All',
     218                        'current' => true,
     219                    ),
     220                    'activated' => array(
     221                        'url'     => add_query_arg(
     222                            array(
     223                                'status' => 'activated',
     224                                'sort'   => 'desc',
     225                            ),
     226                            'https://example.org/'
     227                        ),
     228                        'label'   => 'Activated',
     229                        'current' => false,
     230                    ),
     231                ),
     232                'expected'  => array(
     233                    'all'       => '<a href="https://example.org/" class="current" aria-current="page">All</a>',
     234                    'activated' => '<a href="https://example.org/?status=activated&#038;sort=desc">Activated</a>',
     235                ),
     236            ),
     237        );
     238    }
     239
     240    /**
     241     * Tests that "get_views_links()" throws a _doing_it_wrong().
     242     *
     243     * @ticket 42066
     244     *
     245     * @covers WP_List_Table::get_views_links
     246     *
     247     * @expectedIncorrectUsage WP_List_Table::get_views_links
     248     *
     249     * @dataProvider data_get_views_links_doing_it_wrong
     250     *
     251     * @param array $link_data {
     252     *     An array of link data.
     253     *
     254     *     @type string $url     The link URL.
     255     *     @type string $label   The link label.
     256     *     @type bool   $current Optional. Whether this is the currently selected view.
     257     * }
     258     */
     259    public function test_get_views_links_doing_it_wrong( $link_data ) {
     260        $get_views_links = new ReflectionMethod( self::$list_table, 'get_views_links' );
     261        $get_views_links->setAccessible( true );
     262        $get_views_links->invokeArgs( self::$list_table, array( $link_data ) );
     263    }
     264
     265    /**
     266     * Data provider.
     267     *
     268     * @return array
     269     */
     270    public function data_get_views_links_doing_it_wrong() {
     271        return array(
     272            'non-array $link_data'               => array(
     273                'link_data' => 'https://example.org, All, class="current" aria-current="page"',
     274            ),
     275            'a link with no URL'                 => array(
     276                'link_data' => array(
     277                    'all' => array(
     278                        'label'   => 'All',
     279                        'current' => true,
     280                    ),
     281                ),
     282            ),
     283            'a link with an empty URL'           => array(
     284                'link_data' => array(
     285                    'all' => array(
     286                        'url'     => '',
     287                        'label'   => 'All',
     288                        'current' => true,
     289                    ),
     290                ),
     291            ),
     292            'a link with a URL of only spaces'   => array(
     293                'link_data' => array(
     294                    'all' => array(
     295                        'url'     => '  ',
     296                        'label'   => 'All',
     297                        'current' => true,
     298                    ),
     299                ),
     300            ),
     301            'a link with a non-string URL'       => array(
     302                'link_data' => array(
     303                    'all' => array(
     304                        'url'     => array(),
     305                        'label'   => 'All',
     306                        'current' => true,
     307                    ),
     308                ),
     309            ),
     310            'a link with no label'               => array(
     311                'link_data' => array(
     312                    'all' => array(
     313                        'url'     => 'https://example.org/',
     314                        'current' => true,
     315                    ),
     316                ),
     317            ),
     318            'a link with an empty label'         => array(
     319                'link_data' => array(
     320                    'all' => array(
     321                        'url'     => 'https://example.org/',
     322                        'label'   => '',
     323                        'current' => true,
     324                    ),
     325                ),
     326            ),
     327            'a link with a label of only spaces' => array(
     328                'link_data' => array(
     329                    'all' => array(
     330                        'url'     => 'https://example.org/',
     331                        'label'   => '  ',
     332                        'current' => true,
     333                    ),
     334                ),
     335            ),
     336            'a link with a non-string label'     => array(
     337                'link_data' => array(
     338                    'all' => array(
     339                        'url'     => 'https://example.org/',
     340                        'label'   => array(),
     341                        'current' => true,
     342                    ),
     343                ),
     344            ),
     345        );
     346    }
    107347}
  • trunk/tests/phpunit/tests/admin/wpPostsListTable.php

    r51997 r54215  
    320320    }
    321321
     322    /**
     323     * @ticket 42066
     324     *
     325     * @covers WP_Posts_List_Table::get_views
     326     */
     327    public function test_get_views_should_return_views_by_default() {
     328        global $avail_post_stati;
     329
     330        $avail_post_stati_backup = $avail_post_stati;
     331        $avail_post_stati        = get_available_post_statuses();
     332
     333        $actual           = $this->table->get_views();
     334        $avail_post_stati = $avail_post_stati_backup;
     335
     336        $expected = array(
     337            'all'     => '<a href="edit.php?post_type=page">All <span class="count">(38)</span></a>',
     338            'publish' => '<a href="edit.php?post_status=publish&#038;post_type=page">Published <span class="count">(38)</span></a>',
     339        );
     340
     341        $this->assertSame( $expected, $actual );
     342    }
     343
    322344}
  • trunk/tests/phpunit/tests/admin/wpPrivacyRequestsTable.php

    r53942 r54215  
    199199        );
    200200    }
     201
     202    /**
     203     * @ticket 42066
     204     *
     205     * @covers WP_Privacy_Requests_List_Table::get_views
     206     */
     207    public function test_get_views_should_return_views_by_default() {
     208        $expected = array(
     209            'all' => '<a href="http://example.org/wp-admin/export-personal-data.php" class="current" aria-current="page">All <span class="count">(0)</span></a>',
     210        );
     211
     212        $this->assertSame( $expected, $this->get_mocked_class_instance()->get_views() );
     213    }
    201214}
  • trunk/tests/phpunit/tests/multisite/wpMsSitesListTable.php

    r52010 r54215  
    231231            $this->assertSameSets( $expected, $items );
    232232        }
     233
     234        /**
     235         * @ticket 42066
     236         */
     237        public function test_get_views_should_return_views_by_default() {
     238            $expected = array(
     239                'all'    => '<a href="sites.php" class="current" aria-current="page">All <span class="count">(14)</span></a>',
     240                'public' => '<a href="sites.php?status=public">Public <span class="count">(14)</span></a>',
     241            );
     242
     243            $this->assertSame( $expected, $this->table->get_views() );
     244        }
    233245    }
    234246endif;
Note: See TracChangeset for help on using the changeset viewer.