Make WordPress Core


Ignore:
Timestamp:
09/19/2022 09:06:08 PM (2 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.