Make WordPress Core

Ticket #15145: 15145.3.diff

File 15145.3.diff, 10.9 KB (added by costdev, 4 years ago)

Refresh + Unit tests added.

  • src/wp-includes/user.php

    diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
    index 018c960c03..62348031a1 100644
    a b function get_users( $args = array() ) { 
    765765        return (array) $user_search->get_results();
    766766}
    767767
     768/**
     769 * List all the users of the site, with several options available.
     770 *
     771 * @since 5.9.0
     772 *
     773 * @param string|array $args {
     774 *     Optional. Array or string of default arguments.
     775 *
     776 *     @type string $orderby       How to sort the users. Accepts 'nicename', 'email', 'url', 'registered',
     777 *                                 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
     778 *                                 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
     779 *     @type string $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
     780 *     @type int    $number        Maximum users to return or display. Default empty (all users).
     781 *     @type bool   $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
     782 *     @type bool   $show_fullname Whether to show the user's full name. Default false.
     783 *     @type string $feed          If not empty, show a link to the user's feed and use this text as the alt
     784 *                                 parameter of the link. Default empty.
     785 *     @type string $feed_image    If not empty, show a link to the user's feed and use this image URL as
     786 *                                 clickable anchor. Default empty.
     787 *     @type string $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
     788 *     @type bool   $echo          Whether to output the result or instead return it. Default true.
     789 *     @type string $style         If 'list', each user is wrapped in an `<li>` element, otherwise the users
     790 *                                 will be separated by commas.
     791 *     @type bool   $html          Whether to list the items in HTML form or plaintext. Default true.
     792 *     @type string $exclude       An array, comma-, or space-separated list of user IDs to exclude. Default empty.
     793 *     @type string $include       An array, comma-, or space-separated list of user IDs to include. Default empty.
     794 * }
     795 * @return null|string The output, if echo is set to false. Otherwise null.
     796 */
     797function wp_list_users( $args = array() ) {
     798
     799        $defaults = array(
     800                'orderby'       => 'name',
     801                'order'         => 'ASC',
     802                'number'        => '',
     803                'exclude_admin' => true,
     804                'show_fullname' => false,
     805                'feed'          => '',
     806                'feed_image'    => '',
     807                'feed_type'     => '',
     808                'echo'          => true,
     809                'style'         => 'list',
     810                'html'          => true,
     811                'exclude'       => '',
     812                'include'       => '',
     813        );
     814
     815        $args = wp_parse_args( $args, $defaults );
     816
     817        $return = '';
     818
     819        $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
     820        $query_args['fields'] = 'ids';
     821        $users                = get_users( $query_args );
     822
     823        foreach ( $users as $user_id ) {
     824                $user = get_userdata( $user_id );
     825
     826                if ( $args['exclude_admin'] && 'admin' === $user->display_name ) {
     827                        continue;
     828                }
     829
     830                if ( $args['show_fullname'] && $user->first_name && $user->last_name ) {
     831                        $name = "$user->first_name $user->last_name";
     832                } else {
     833                        $name = $user->display_name;
     834                }
     835
     836                if ( ! $args['html'] ) {
     837                        $return .= $name . ', ';
     838
     839                        continue; // No need to go further to process HTML.
     840                }
     841
     842                if ( 'list' === $args['style'] ) {
     843                        $return .= '<li>';
     844                }
     845
     846                $row = $name;
     847
     848                if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
     849                        $row .= ' ';
     850                        if ( empty( $args['feed_image'] ) ) {
     851                                $row .= '(';
     852                        }
     853
     854                        $row .= '<a href="' . get_author_feed_link( $user->ID, $args['feed_type'] ) . '"';
     855
     856                        $alt = '';
     857                        if ( ! empty( $args['feed'] ) ) {
     858                                $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
     859                                $name = $args['feed'];
     860                        }
     861
     862                        $row .= '>';
     863
     864                        if ( ! empty( $args['feed_image'] ) ) {
     865                                $row .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
     866                        } else {
     867                                $row .= $name;
     868                        }
     869
     870                        $row .= '</a>';
     871
     872                        if ( empty( $args['feed_image'] ) ) {
     873                                $row .= ')';
     874                        }
     875                }
     876
     877                $return .= $row;
     878                $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
     879        }
     880
     881        $return = rtrim( $return, ', ' );
     882
     883        if ( ! $args['echo'] ) {
     884                return $return;
     885        }
     886        echo $return;
     887}
     888
    768889/**
    769890 * Get the sites a user belongs to.
    770891 *
  • new file tests/phpunit/tests/user/listUsers.php

    diff --git a/tests/phpunit/tests/user/listUsers.php b/tests/phpunit/tests/user/listUsers.php
    new file mode 100644
    index 0000000000..3166f07a08
    - +  
     1<?php
     2/**
     3 * @group user
     4 * @ticket 15145
     5 */
     6class Tests_User_ListUsers extends WP_UnitTestCase {
     7        private static $user_ids = array();
     8
     9        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     10                self::$user_ids[] = $factory->user->create(
     11                        array(
     12                                'user_login'   => 'zack',
     13                                'display_name' => 'zack',
     14                                'role'         => 'subscriber',
     15                                'first_name'   => 'zack',
     16                                'last_name'    => 'moon',
     17                                'user_email'   => 'm.zack@example.com',
     18                                'user_url'     => 'http://moonzack.fake',
     19                        )
     20                );
     21
     22                self::$user_ids[] = $factory->user->create(
     23                        array(
     24                                'user_login'   => 'jane',
     25                                'display_name' => 'jane',
     26                                'role'         => 'contributor',
     27                                'first_name'   => 'jane',
     28                                'last_name'    => 'reno',
     29                                'user_email'   => 'r.jane@example.com',
     30                                'user_url'     => 'http://janereno.fake',
     31                        )
     32                );
     33
     34                self::$user_ids[] = $factory->user->create(
     35                        array(
     36                                'user_login'   => 'michelle',
     37                                'display_name' => 'michelle',
     38                                'role'         => 'subscriber',
     39                                'first_name'   => 'michelle',
     40                                'last_name'    => 'jones',
     41                                'user_email'   => 'j.michelle@example.com',
     42                                'user_url'     => 'http://lemichellejones.fake',
     43                        )
     44                );
     45
     46                self::$user_ids[] = $factory->user->create(
     47                        array(
     48                                'user_login'   => 'paul',
     49                                'display_name' => 'paul',
     50                                'role'         => 'subscriber',
     51                                'first_name'   => 'paul',
     52                                'last_name'    => 'norris',
     53                                'user_email'   => 'n.paul@example.com',
     54                                'user_url'     => 'http://awildpaulappeared.fake',
     55                        )
     56                );
     57
     58                foreach ( self::$user_ids as $user ) {
     59                        $factory->post->create(
     60                                array(
     61                                        'post_type'   => 'post',
     62                                        'post_author' => $user,
     63                                )
     64                        );
     65                }
     66        }
     67
     68        /**
     69         * Test that wp_list_users() creates the expected list of users.
     70         *
     71         * @dataProvider data_should_create_a_user_list
     72         * @covers ::wp_list_users
     73         *
     74         * @param array  $args     The arguments to create a list of users.
     75         * @param string $expected The expected result.
     76         */
     77        public function test_should_create_a_user_list( $args, $expected ) {
     78                $actual = wp_list_users( $args );
     79
     80                if ( null === $actual ) {
     81                        $this->expectOutputString( $expected );
     82                } else {
     83                        $this->assertSame( $expected, $actual );
     84                }
     85        }
     86
     87        /**
     88         * Data provider.
     89         *
     90         * @return array
     91         */
     92        public function data_should_create_a_user_list() {
     93                return array(
     94                        'defaults when no args are supplied'      => array(
     95                                'args'     => array(),
     96                                'expected' => '<li>jane</li><li>michelle</li><li>paul</li><li>zack</li>',
     97                        ),
     98                        'the admin account included'              => array(
     99                                'args'     => array(
     100                                        'exclude_admin' => false,
     101                                ),
     102                                'expected' => '<li>admin</li><li>jane</li><li>michelle</li><li>paul</li><li>zack</li>',
     103                        ),
     104                        'the full name of each user'              => array(
     105                                'args'     => array(
     106                                        'show_fullname' => true,
     107                                ),
     108                                'expected' => '<li>jane reno</li><li>michelle jones</li><li>paul norris</li><li>zack moon</li>',
     109                        ),
     110                        'the feed of each user'                   => array(
     111                                'args'     => array(
     112                                        'feed' => 'User feed',
     113                                ),
     114                                'expected' => '<li>jane (<a href="http://example.org/?feed=rss2&amp;author=3">User feed</a>)</li>' .
     115                                                        '<li>michelle (<a href="http://example.org/?feed=rss2&amp;author=4">User feed</a>)</li>' .
     116                                                        '<li>paul (<a href="http://example.org/?feed=rss2&amp;author=5">User feed</a>)</li>' .
     117                                                        '<li>zack (<a href="http://example.org/?feed=rss2&amp;author=2">User feed</a>)</li>',
     118                        ),
     119                        'the feed of each user and an image'      => array(
     120                                'args'     => array(
     121                                        'feed'       => 'User feed with image',
     122                                        'feed_image' => 'http://example.org/image.jpg',
     123                                ),
     124                                'expected' => '<li>jane <a href="http://example.org/?feed=rss2&amp;author=3"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' .
     125                                                        '<li>michelle <a href="http://example.org/?feed=rss2&amp;author=4"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' .
     126                                                        '<li>paul <a href="http://example.org/?feed=rss2&amp;author=5"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' .
     127                                                        '<li>zack <a href="http://example.org/?feed=rss2&amp;author=2"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>',
     128                        ),
     129                        'a feed of the specified type'            => array(
     130                                'args'     => array(
     131                                        'feed'      => 'User feed as atom',
     132                                        'feed_type' => 'atom',
     133                                ),
     134                                'expected' => '<li>jane (<a href="http://example.org/?feed=atom&amp;author=3">User feed as atom</a>)</li>' .
     135                                                        '<li>michelle (<a href="http://example.org/?feed=atom&amp;author=4">User feed as atom</a>)</li>' .
     136                                                        '<li>paul (<a href="http://example.org/?feed=atom&amp;author=5">User feed as atom</a>)</li>' .
     137                                                        '<li>zack (<a href="http://example.org/?feed=atom&amp;author=2">User feed as atom</a>)</li>',
     138                        ),
     139                        'no output via echo'                      => array(
     140                                'args'     => array(
     141                                        'echo' => false,
     142                                ),
     143                                'expected' => '<li>jane</li><li>michelle</li><li>paul</li><li>zack</li>',
     144                        ),
     145                        'commas separating each user'             => array(
     146                                'args'     => array(
     147                                        'style' => '',
     148                                ),
     149                                'expected' => 'jane, michelle, paul, zack',
     150                        ),
     151                        'plain text format'                       => array(
     152                                'args'     => array(
     153                                        'html' => false,
     154                                ),
     155                                'expected' => 'jane, michelle, paul, zack',
     156                        ),
     157                );
     158        }
     159
     160        /**
     161         * Tests that wp_list_users() does not create a user list.
     162         *
     163         * @dataProvider data_should_not_create_a_user_list
     164         * @covers ::wp_list_users
     165         *
     166         * @param array  $args     The arguments to create a list of users.
     167         */
     168        public function test_should_not_create_a_user_list( $args ) {
     169                $actual = wp_list_users( $args );
     170
     171                if ( null === $actual ) {
     172                        $this->expectOutputString( '', 'wp_list_users() did not output an empty string.' );
     173                } else {
     174                        $this->assertSame( $actual, 'wp_list_users() did not return an empty string.' );
     175                }
     176        }
     177
     178        /**
     179         * Data provider.
     180         *
     181         * @return array
     182         */
     183        public function data_should_not_create_a_user_list() {
     184                return array(
     185                        'an empty user query result' => array(
     186                                'args'     => array(
     187                                        'include' => array( 9999 ),
     188                                ),
     189                                'expected' => '',
     190                        ),
     191                );
     192        }
     193}