Make WordPress Core

Ticket #31251: 31251.patch

File 31251.patch, 6.2 KB (added by krogsgard, 10 years ago)

Add unit tests, inline docs. Huge thanks to @boone for the walkthrough.

  • src/wp-includes/user.php

     
    13551355 * The available arguments are as follows:
    13561356 *
    13571357 * @since 2.3.0
     1358 * @since 4.2.0 The default value for $show was changed to empty
    13581359 *
    13591360 * @global wpdb $wpdb WordPress database object for queries.
    13601361 *
     
    13821383 *     @type bool|int     $multi                   Whether to skip the ID attribute on the 'select' element.
    13831384 *                                                 Accepts 1|true or 0|false. Default 0|false.
    13841385 *     @type string       $show                    User table column to display. If the selected item is empty
    1385  *                                                 then the 'user_login' will be displayed in parentheses.
    1386  *                                                 Accepts user fields. Default 'display_name'.
     1386 *                                                 then the 'display_name' will be displayed with the 'user_login' in parenthesis.
     1387 *                                                 Accepts user fields. Default 'display_name' with 'user_login' in parenthesis.
    13871388 *     @type int|bool     $echo                    Whether to echo or return the drop-down. Accepts 1|true (echo)
    13881389 *                                                 or 0|false (return). Default 1|true.
    13891390 *     @type int          $selected                Which user ID should be selected. Default 0.
     
    14031404                'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
    14041405                'orderby' => 'display_name', 'order' => 'ASC',
    14051406                'include' => '', 'exclude' => '', 'multi' => 0,
    1406                 'show' => 'display_name', 'echo' => 1,
     1407                'show' => '', 'echo' => 1,
    14071408                'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
    14081409                'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false,
    14091410                'option_none_value' => -1
     
    14181419        $option_none_value = $r['option_none_value'];
    14191420
    14201421        $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
    1421         $query_args['fields'] = array( 'ID', 'user_login', $show );
     1422        $query_args['fields'] = array( 'ID', 'user_login', 'display_name' );
    14221423        $users = get_users( $query_args );
    14231424
    14241425        $output = '';
     
    14471448                        if ( $_selected ) {
    14481449                                $found_selected = true;
    14491450                        }
    1450                         $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
     1451                        $display = ! empty( $user->$show ) ? $user->$show : $user->display_name . ' ('. $user->user_login . ')';
    14511452                        $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
    14521453                }
    14531454
    14541455                if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) {
    14551456                        $user = get_userdata( $r['selected'] );
    14561457                        $_selected = selected( $user->ID, $r['selected'], false );
    1457                         $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
     1458                        $display = ! empty( $user->$show ) ? $user->$show : $user->display_name . ' ('. $user->user_login . ')';
    14581459                        $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
    14591460                }
    14601461
  • tests/phpunit/tests/user/wpDropdownUsers.php

     
     1<?php
     2
     3/**
     4 * Test functions in wp-includes/user.php
     5 *
     6 * @group user
     7 */
     8class Tests_User_WpDropdownUsers extends WP_UnitTestCase {
     9
     10        /**
     11         * @ticket 31251
     12         */
     13        public function test_show_should_display_display_name_and_user_in_parenthesis_when_show_is_not_specified() {
     14
     15                // create a user with a different display_name
     16                $u = $this->factory->user->create( array(
     17                        'user_login'   => 'foo',
     18                        'display_name' => 'Foo Person'
     19                ) );
     20
     21                // Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
     22                $found = wp_dropdown_users( array(
     23                        'echo' => false
     24                ) );
     25
     26                $expected = "<option value='$u'>Foo Person (foo)</option>";
     27
     28                $this->assertContains( $expected, $found );
     29        }
     30
     31        /**
     32         * @ticket 31251
     33         */
     34        public function test_show_should_display_display_name_and_user_in_parenthesis_when_show_is_specified_as_empty() {
     35
     36                // create a user with a different display_name
     37                $u = $this->factory->user->create( array(
     38                        'user_login'   => 'foo',
     39                        'display_name' => 'Foo Person'
     40                ) );
     41
     42                // Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
     43                $found = wp_dropdown_users( array(
     44                        'echo' => false,
     45                        'show' => ''
     46                ) );
     47
     48                $expected = "<option value='$u'>Foo Person (foo)</option>";
     49
     50                $this->assertContains( $expected, $found );
     51        }
     52
     53        /**
     54         * @ticket 31251
     55         */
     56        public function test_show_should_display_user_property_when_the_value_of_show_is_a_valid_user_property() {
     57
     58                // create a user with a different display_name
     59                $u = $this->factory->user->create( array(
     60                        'user_login'   => 'foo',
     61                        'display_name' => 'Foo Person'
     62                ) );
     63
     64                // Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
     65                $found = wp_dropdown_users( array(
     66                        'echo' => false,
     67                        'show' => 'user_login'
     68                ) );
     69
     70                $expected = "<option value='$u'>foo</option>";
     71
     72                $this->assertContains( $expected, $found );
     73        }
     74
     75        /**
     76         * @ticket 31251
     77         */
     78        public function test_show_should_display_display_name_and_user_in_parenthesis_when_show_an_invalid_user_property() {
     79
     80                // create a user with a different display_name
     81                $u = $this->factory->user->create( array(
     82                        'user_login'   => 'foo',
     83                        'display_name' => 'Foo Person'
     84                ) );
     85
     86                // Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
     87                $found = wp_dropdown_users( array(
     88                        'echo' => false,
     89                        'show' => 'foobar'
     90                ) );
     91
     92                $expected = "<option value='$u'>Foo Person (foo)</option>";
     93
     94                $this->assertContains( $expected, $found );
     95        }
     96
     97}
     98 No newline at end of file