Make WordPress Core

Ticket #31909: wp_dropdown_categories-required.31909-5.diff

File wp_dropdown_categories-required.31909-5.diff, 3.8 KB (added by pcarvalho, 9 years ago)

fixed the inline documentation for the test

  • src/wp-includes/category-template.php

    diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
    index 35d6c82..37ba533 100644
    function category_description( $category = 0 ) { 
    314314 *
    315315 * @since 2.1.0
    316316 * @since 4.2.0 Introduced the `value_field` argument.
     317 * @since 4.6.0 Added required attribute
    317318 *
    318319 * @param string|array $args {
    319320 *     Optional. Array or string of arguments to generate a categories drop-down element.
    function category_description( $category = 0 ) { 
    351352 *     @type string|array $taxonomy          Name of the category or categories to retrieve. Default 'category'.
    352353 *     @type bool         $hide_if_empty     True to skip generating markup if no categories are found.
    353354 *                                           Default false (create select element even if no categories are found).
     355 *     @type bool         $required          Whether the field is required or not using HTML5 'required' attribute.
     356 *                                           Accepts 0, 1, or their boolean. Defaults to false.
    354357 * }
    355358 * @return string HTML content only if 'echo' argument is 0.
    356359 */
    357360function wp_dropdown_categories( $args = '' ) {
    358361        $defaults = array(
    359                 'show_option_all' => '', 'show_option_none' => '',
    360                 'orderby' => 'id', 'order' => 'ASC',
     362                'show_option_all' => '',
     363                'show_option_none' => '',
     364                'option_none_value' => -1,
     365                'orderby' => 'id',
     366                'order' => 'ASC',
    361367                'show_count' => 0,
    362                 'hide_empty' => 1, 'child_of' => 0,
    363                 'exclude' => '', 'echo' => 1,
    364                 'selected' => 0, 'hierarchical' => 0,
    365                 'name' => 'cat', 'id' => '',
    366                 'class' => 'postform', 'depth' => 0,
    367                 'tab_index' => 0, 'taxonomy' => 'category',
    368                 'hide_if_empty' => false, 'option_none_value' => -1,
     368                'hide_empty' => 1,
     369                'child_of' => 0,
     370                'exclude' => '',
     371                'echo' => 1,
     372                'selected' => 0,
     373                'hierarchical' => 0,
     374                'name' => 'cat',
     375                'id' => '',
     376                'class' => 'postform',
     377                'depth' => 0,
     378                'tab_index' => 0,
     379                'taxonomy' => 'category',
     380                'hide_if_empty' => false,
    369381                'value_field' => 'term_id',
     382                'required' => false
    370383        );
    371384
    372385        $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
    function wp_dropdown_categories( $args = '' ) { 
    405418        $name = esc_attr( $r['name'] );
    406419        $class = esc_attr( $r['class'] );
    407420        $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
     421        $required = $r['required'] ? 'required' : '';
    408422
    409423        if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
    410                 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
     424                $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n";
    411425        } else {
    412426                $output = '';
    413427        }
  • tests/phpunit/tests/category.php

    diff --git tests/phpunit/tests/category.php tests/phpunit/tests/category.php
    index 913f4dc..aff9c1d 100644
    class Tests_Category extends WP_UnitTestCase { 
    262262                // Test to see if it returns the default with the category ID.
    263263                $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
    264264        }
     265        /**
     266         * @ticket 31909
     267         */
     268        public function test_wp_dropdown_categories_value_field_should_contain_required() {
     269                // Create a test category.
     270                $cat_id = self::factory()->category->create( array(
     271                        'name' => 'Test Category',
     272                        'slug' => 'test_category',
     273                ) );
     274
     275                // Get the default functionality of wp_dropdown_categories().
     276                $args = array(
     277                        'show_option_none'  => __( 'Select one', 'text-domain' ),
     278                        'option_none_value' => "",
     279                        'required'          => 1,
     280                        'hide_empty'        => 0,
     281                        'echo'              => 0,
     282                );
     283                $dropdown_categories = wp_dropdown_categories( $args );
     284
     285
     286                // Test to see if it contains the "required" attribute.
     287                $this->assertContains( 'required', $dropdown_categories );
     288        }
    265289
    266290        /**
    267291         * @ticket 30306