Make WordPress Core

Changeset 64396


Ignore:
Timestamp:
10/16/2026 04:43:54 PM (7 hours ago)
Author:
lancewillett
Message:

Tests: Cover zero-slug category dropdown selection.

Verify that Walker_CategoryDropdown::start_el() leaves a category with slug '0' unselected when selected is false, and selects it when selected is '0'. Guard against loose comparisons selecting the wrong option.

Follow-up to [64379].

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14313

Props huzaifaalmesbah.
See #65819.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/category/walkerCategoryDropdown.php

    r64379 r64396  
    154154
    155155        /**
     156         * Tests that the selected value is compared as a string with a category slug of '0'.
     157         *
     158         * A loose comparison would treat the slug '0' as equal to false.
     159         *
     160         * @ticket 65819
     161         *
     162         * @covers ::start_el
     163         *
     164         * @dataProvider data_start_el_with_zero_slug
     165         *
     166         * @param mixed  $selected Selected value.
     167         * @param string $expected Expected option element, without the leading tab and trailing newline.
     168         */
     169        public function test_start_el_compares_zero_slug_as_string( $selected, $expected ) {
     170                $walker = new Walker_CategoryDropdown();
     171                $output = '';
     172
     173                $walker->start_el(
     174                        $output,
     175                        $this->get_category(
     176                                array(
     177                                        'name' => 'Zero',
     178                                        'slug' => '0',
     179                                )
     180                        ),
     181                        0,
     182                        array(
     183                                'value_field' => 'slug',
     184                                'selected'    => $selected,
     185                                'show_count'  => 0,
     186                        )
     187                );
     188
     189                $this->assertSame( "\t" . $expected . "\n", $output );
     190        }
     191
     192        /**
     193         * Data provider.
     194         *
     195         * @return array[]
     196         */
     197        public static function data_start_el_with_zero_slug() {
     198                return array(
     199                        'selected value is false' => array( false, '<option class="level-0" value="0">Zero</option>' ),
     200                        'selected value is \'0\'' => array( '0', '<option class="level-0" value="0" selected="selected">Zero</option>' ),
     201                );
     202        }
     203
     204        /**
    156205         * Tests that the option value is escaped.
    157206         *
Note: See TracChangeset for help on using the changeset viewer.