Make WordPress Core

Changeset 64987


Ignore:
Timestamp:
10/28/2026 10:55:06 PM (9 days ago)
Author:
lancewillett
Message:

Tests: Simplify content and template helper coverage.

Consolidate repeated post, term, bookmark, rewrite, and block-binding checks while retaining distinct output and escaping behavior.

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

Props huzaifaalmesbah.
See #65819.

Location:
trunk/tests/phpunit/tests
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-bindings/patternOverridesSource.php

    r64324 r64987  
    8383                return array(
    8484                        'empty attributes'     => array( array() ),
    85                         'missing metadata'     => array( array( 'unrelated' => 'value' ) ),
    8685                        'empty metadata array' => array( array( 'metadata' => array() ) ),
    8786                        'empty name string'    => array( array( 'metadata' => array( 'name' => '' ) ) ),
    88                         'null name'            => array( array( 'metadata' => array( 'name' => null ) ) ),
    89                         'false name'           => array( array( 'metadata' => array( 'name' => false ) ) ),
    90                 );
    91         }
    92 
    93         /**
    94          * Tests that _block_bindings_pattern_overrides_get_value() returns null when context is empty or missing.
    95          *
    96          * @ticket 65819
    97          *
    98          * @covers ::_block_bindings_pattern_overrides_get_value
    99          *
    100          * @dataProvider data_empty_context
    101          *
    102          * @param array $context Available block context.
    103          */
    104         public function test_get_value_returns_null_when_context_empty( array $context ) {
     87                );
     88        }
     89
     90        /**
     91         * Tests that _block_bindings_pattern_overrides_get_value() returns null when context is empty.
     92         *
     93         * @ticket 65819
     94         *
     95         * @covers ::_block_bindings_pattern_overrides_get_value
     96         *
     97         */
     98        public function test_get_value_returns_null_when_context_empty() {
    10599                $block = $this->create_block(
    106100                        array(
    … …  
    109103                                ),
    110104                        ),
    111                         $context
     105                        array()
    112106                );
    113107
    114108                $this->assertNull( _block_bindings_pattern_overrides_get_value( array(), $block, 'content' ) );
    115         }
    116 
    117         /**
    118          * Data provider for test_get_value_returns_null_when_context_empty.
    119          *
    120          * @return array[]
    121          */
    122         public function data_empty_context() {
    123                 return array(
    124                         'empty context'                 => array( array() ),
    125                         'missing pattern/overrides key' => array( array( 'postType' => 'post' ) ),
    126                         'empty pattern/overrides array' => array( array( 'pattern/overrides' => array() ) ),
    127                 );
    128109        }
    129110
    … …  
    181162
    182163        /**
    183          * Tests that _block_bindings_pattern_overrides_get_value() returns the matching override value for an attribute.
    184          *
    185          * @ticket 65819
    186          *
    187          * @covers ::_block_bindings_pattern_overrides_get_value
    188          */
    189         public function test_get_value_returns_override_value() {
    190                 $block = $this->create_block(
    191                         array(
    192                                 'metadata' => array(
    193                                         'name' => 'custom-header',
    194                                 ),
    195                         ),
    196                         array(
    197                                 'pattern/overrides' => array(
    198                                         'custom-header' => array(
    199                                                 'content' => 'Overridden Header Text',
    200                                         ),
    201                                 ),
    202                         )
    203                 );
    204 
    205                 $result = _block_bindings_pattern_overrides_get_value( array(), $block, 'content' );
    206 
    207                 $this->assertSame( 'Overridden Header Text', $result );
    208         }
    209 
    210         /**
    211164         * Tests that the registered source get_value() method executes correctly.
    212165         *
    … …  
    274227        public function data_override_values_and_types() {
    275228                return array(
    276                         'string'        => array( 'A customized string' ),
    277229                        'empty string'  => array( '' ),
    278                         'integer'       => array( 12345 ),
    279230                        'zero integer'  => array( 0 ),
    280                         'zero string'   => array( '0' ),
    281                         'boolean true'  => array( true ),
    282231                        'boolean false' => array( false ),
    283232                        'array'         => array( array( 'nested' => 'value' ) ),
  • trunk/tests/phpunit/tests/block-bindings/postDataSource.php

    r64287 r64987  
    179179                        'missing args'           => array( array() ),
    180180                        'empty string field'     => array( array( 'field' => '' ) ),
    181                         'null field'             => array( array( 'field' => null ) ),
    182181                        'empty string key'       => array( array( 'key' => '' ) ),
    183                         'null key'               => array( array( 'key' => null ) ),
    184182                        'both empty field & key' => array(
    185183                                array(
    … …  
    216214                return array(
    217215                        'missing postId context' => array( array() ),
    218                         'empty string postId'    => array( array( 'postId' => '' ) ),
    219                         'null postId'            => array( array( 'postId' => null ) ),
    220216                        'zero postId'            => array( array( 'postId' => 0 ) ),
    221217                );
    … …  
    303299
    304300        /**
    305          * Tests that _block_bindings_post_data_get_value() returns date field formatted in ISO 8601.
    306          *
    307          * @ticket 65819
    308          *
    309          * @covers ::_block_bindings_post_data_get_value
    310          */
    311         public function test_get_value_returns_date_field() {
    312                 $block = $this->create_block(
    313                         'core/paragraph',
    314                         array( 'postId' => self::$post_id )
    315                 );
    316 
    317                 $this->assertSame(
    318                         '2025-01-01T10:00:00+00:00',
    319                         _block_bindings_post_data_get_value( array( 'field' => 'date' ), $block ),
    320                         'Date field should return the post date in ISO 8601 format.'
    321                 );
    322         }
    323 
    324         /**
    325301         * Tests that _block_bindings_post_data_get_value() returns modified date when modified date is later than publish date.
    326302         *
    … …  
    359335                        _block_bindings_post_data_get_value( array( 'field' => 'modified' ), $block ),
    360336                        'Modified date should return empty string when equal to publish date.'
    361                 );
    362         }
    363 
    364         /**
    365          * Tests that _block_bindings_post_data_get_value() returns permalink for link field.
    366          *
    367          * @ticket 65819
    368          *
    369          * @covers ::_block_bindings_post_data_get_value
    370          */
    371         public function test_get_value_returns_link_field() {
    372                 $block = $this->create_block(
    373                         'core/paragraph',
    374                         array( 'postId' => self::$post_id )
    375                 );
    376 
    377                 $expected_link = esc_url( get_permalink( self::$post_id ) );
    378                 $this->assertSame(
    379                         $expected_link,
    380                         _block_bindings_post_data_get_value( array( 'field' => 'link' ), $block ),
    381                         'Link field should return esc_url( get_permalink() ).'
    382337                );
    383338        }
  • trunk/tests/phpunit/tests/block-bindings/termDataSource.php

    r64233 r64987  
    144144                        'missing field' => array( array() ),
    145145                        'empty string'  => array( array( 'field' => '' ) ),
    146                         'null field'    => array( array( 'field' => null ) ),
    147                         'false field'   => array( array( 'field' => false ) ),
    148146                );
    149147        }
    … …  
    171169        public function data_missing_or_empty_context() {
    172170                return array(
    173                         'empty context'    => array( array() ),
    174171                        'missing taxonomy' => array( array( 'termId' => 123 ) ),
    175172                        'missing termId'   => array( array( 'taxonomy' => 'category' ) ),
    … …  
    186183                                ),
    187184                        ),
    188                         'null identifiers' => array(
    189                                 array(
    190                                         'termId'   => null,
    191                                         'taxonomy' => null,
    192                                 ),
    193                         ),
    194185                );
    195186        }
  • trunk/tests/phpunit/tests/bookmark/sanitizeBookmarkField.php

    r64416 r64987  
    7575        public static function data_casts_integer_fields() {
    7676                return array(
    77                         'link_id, raw'              => array( 'link_id', '12abc', 'raw', 12 ),
    78                         'link_id, display'          => array( 'link_id', '5', 'display', 5 ),
    79                         'link_id, edit'             => array( 'link_id', '6', 'edit', 6 ),
    80                         'link_id, attribute'        => array( 'link_id', '7', 'attribute', 7 ),
    81                         'link_rating, db'           => array( 'link_rating', '8', 'db', 8 ),
    82                         'link_rating, js'           => array( 'link_rating', '9', 'js', 9 ),
    83                         'link_rating, empty string' => array( 'link_rating', '', 'display', 0 ),
    84                         'link_rating, zero string'  => array( 'link_rating', '0', 'display', 0 ),
     77                        'link_id, raw'       => array( 'link_id', '12abc', 'raw', 12 ),
     78                        'link_id, attribute' => array( 'link_id', '7', 'attribute', 7 ),
     79                        'link_rating, js'    => array( 'link_rating', '9', 'js', 9 ),
    8580                );
    8681        }
    … …  
    115110                        'array of mixed values, display' => array( array( '1', '-2', 'abc', 3 ), 'display', array( 1, 2, 0, 3 ) ),
    116111                        'single value, edit'             => array( '5', 'edit', array( 5 ) ),
    117                         'empty array, db'                => array( array(), 'db', array() ),
    118112                        'empty string, raw'              => array( '', 'raw', array( 0 ) ),
    119113                );
    … …  
    148142                        'markup'          => array( '<b>N</b>', 'N' ),
    149143                        'other character' => array( 'x', '' ),
    150                         'empty string'    => array( '', '' ),
    151                         'zero string'     => array( '0', '' ),
    152144                );
    153145        }
    … …  
    176168        public static function data_link_target_values() {
    177169                return array(
    178                         '_blank'       => array( '_blank', '_blank' ),
    179                         '_top'         => array( '_top', '_top' ),
    180                         '_self'        => array( '_self', '' ),
    181                         'uppercase'    => array( '_BLANK', '' ),
    182                         'frame name'   => array( 'my-frame', '' ),
    183                         'empty string' => array( '', '' ),
    184                         'zero string'  => array( '0', '' ),
     170                        '_blank'    => array( '_blank', '_blank' ),
     171                        '_top'      => array( '_top', '_top' ),
     172                        '_self'     => array( '_self', '' ),
     173                        'uppercase' => array( '_BLANK', '' ),
    185174                );
    186175        }
    … …  
    277266         * @dataProvider data_edit_escaping_function
    278267         *
    279          * @param string $field  Bookmark field name.
    280          * @param string $filter Filter run by the expected escaping function.
    281          */
    282         public function test_edit_context_escaping_function( $field, $filter ) {
     268         * @param string $field    Bookmark field name.
     269         * @param string $filter   Filter run by the expected escaping function.
     270         * @param string $expected Expected escaped and filtered value.
     271         */
     272        public function test_edit_context_escaping_function( $field, $filter, $expected ) {
    283273                // Isolates the escaping from the default filters on the link_notes hooks.
    284274                remove_all_filters( "edit_{$field}" );
    … …  
    292282                        add_filter( $hook, array( $mock, 'filter' ) );
    293283                }
    294 
    295                 sanitize_bookmark_field( $field, self::MARKUP, self::BOOKMARK_ID, 'edit' );
     284                add_filter(
     285                        'esc_html',
     286                        static function ( $escaped ) {
     287                                return $escaped . '|html';
     288                        },
     289                        20
     290                );
     291                add_filter(
     292                        'attribute_escape',
     293                        static function ( $escaped ) {
     294                                return $escaped . '|attribute';
     295                        },
     296                        20
     297                );
     298
     299                $result = sanitize_bookmark_field( $field, self::MARKUP, self::BOOKMARK_ID, 'edit' );
     300
     301                $this->assertSame( $expected, $result );
    296302
    297303                foreach ( $escaping_filters as $hook => $mock ) {
    … …  
    307313        public static function data_edit_escaping_function() {
    308314                return array(
    309                         'link_notes'  => array( 'link_notes', 'esc_html' ),
    310                         'other field' => array( 'link_foo', 'attribute_escape' ),
     315                        'link_notes'  => array( 'link_notes', 'esc_html', '&lt;b&gt;Bold&lt;/b&gt; &amp; &quot;double&quot; &#039;single&#039;|html' ),
     316                        'other field' => array( 'link_foo', 'attribute_escape', '&lt;b&gt;Bold&lt;/b&gt; &amp; &quot;double&quot; &#039;single&#039;|attribute' ),
    311317                );
    312318        }
  • trunk/tests/phpunit/tests/post/getPostFormatLink.php

    r64546 r64987  
    1717         */
    1818        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    19                 $formats = array( 'aside', 'chat', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio' );
     19                $formats = array( 'aside', 'video' );
    2020
    2121                foreach ( $formats as $format ) {
    … …  
    3535        public function test_get_post_format_link_non_existent_term() {
    3636                $this->assertFalse( get_post_format_link( 'nonexistent' ) );
    37         }
    38 
    39         /**
    40          * Tests that get_post_format_link() returns false for an empty string.
    41          *
    42          * @ticket 65819
    43          */
    44         public function test_get_post_format_link_empty_string() {
    45                 $this->assertFalse( get_post_format_link( '' ) );
    4637        }
    4738
    … …  
    10798
    10899        /**
    109          * Tests that get_post_format_link() respects a custom permastruct base.
    110          *
    111          * @ticket 65819
    112          */
    113         public function test_get_post_format_link_with_custom_permastruct() {
    114                 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    115                 add_permastruct( 'post_format', 'format/%post_format%' );
    116 
    117                 $expected = home_url( '/format/aside/' );
    118                 $actual   = get_post_format_link( 'aside' );
    119 
    120                 remove_permastruct( 'post_format' );
    121 
    122                 $this->assertSame( $expected, $actual );
    123         }
    124 
    125         /**
    126          * Tests that get_post_format_link() strips the 'post-format-' prefix from the query arg in plain permalinks.
    127          *
    128          * @ticket 65819
    129          */
    130         public function test_get_post_format_link_strips_prefix_in_query_args() {
    131                 $link = get_post_format_link( 'video' );
    132 
    133                 $this->assertStringNotContainsString( 'post-format-video', $link );
    134                 $this->assertStringContainsString( 'post_format=video', $link );
    135         }
    136 
    137         /**
    138          * Tests that get_post_format_link() returns false for non-string inputs.
    139          *
    140          * @ticket 65819
    141          */
    142         public function test_get_post_format_link_non_string_input() {
    143                 $this->assertFalse( get_post_format_link( 123 ) );
    144                 $this->assertFalse( get_post_format_link( null ) );
    145         }
    146 
    147         /**
    148100         * Tests that the 'term_link' filter can modify the post format link.
    149101         *
    … …  
    172124        public static function data_standard_post_formats() {
    173125                return array(
    174                         'aside format'   => array( 'aside' ),
    175                         'chat format'    => array( 'chat' ),
    176                         'gallery format' => array( 'gallery' ),
    177                         'link format'    => array( 'link' ),
    178                         'image format'   => array( 'image' ),
    179                         'quote format'   => array( 'quote' ),
    180                         'status format'  => array( 'status' ),
    181                         'video format'   => array( 'video' ),
    182                         'audio format'   => array( 'audio' ),
     126                        'aside format' => array( 'aside' ),
     127                        'video format' => array( 'video' ),
    183128                );
    184129        }
  • trunk/tests/phpunit/tests/post/theGuid.php

    r64535 r64987  
    4646
    4747        /**
    48          * Tests that the_guid() displays the guid when given a post ID.
    49          *
    50          * @ticket 65819
    51          */
    52         public function test_the_guid_with_post_id() {
    53                 $this->expectOutputString( self::$guid );
    54                 the_guid( self::$post_id );
    55         }
    56 
    57         /**
    5848         * Tests that the_guid() displays the guid when given a WP_Post object.
    5949         *
    … …  
    8777                $this->expectOutputString( '' );
    8878                the_guid( 999999 );
    89         }
    90 
    91         /**
    92          * Tests that the_guid() outputs an empty string when called with 0 and no global post exists.
    93          *
    94          * @ticket 65819
    95          */
    96         public function test_the_guid_with_zero_and_no_global_post() {
    97                 unset( $GLOBALS['post'] );
    98 
    99                 $this->expectOutputString( '' );
    100                 the_guid( 0 );
    10179        }
    10280
    … …  
    175153                remove_filter( 'get_the_guid', $custom_callback, 10 );
    176154        }
    177 
    178         /**
    179          * Tests that the_guid() displays the guid for an attachment.
    180          *
    181          * @ticket 65819
    182          */
    183         public function test_the_guid_with_attachment() {
    184                 $attachment_guid = 'https://example.org/wp-content/uploads/2026/10/test.jpg';
    185                 $attachment_id   = self::factory()->attachment->create_object(
    186                         'test.jpg',
    187                         0,
    188                         array(
    189                                 'post_mime_type' => 'image/jpeg',
    190                                 'post_type'      => 'attachment',
    191                                 'guid'           => $attachment_guid,
    192                         )
    193                 );
    194 
    195                 $this->expectOutputString( $attachment_guid );
    196                 the_guid( $attachment_id );
    197         }
    198155}
  • trunk/tests/phpunit/tests/post/theTitleAttribute.php

    r64515 r64987  
    118118                return array(
    119119                        'array with boolean false' => array( array( 'echo' => false ) ),
    120                         'array with integer 0'     => array( array( 'echo' => 0 ) ),
    121120                        'query string with echo=0' => array( 'echo=0' ),
    122121                );
    … …  
    234233
    235234        /**
    236          * Tests that the_title_attribute() accepts both a post ID and a WP_Post object.
    237          *
    238          * @ticket 65819
    239          */
    240         public function test_accepts_post_id_and_post_object() {
    241                 $by_id = the_title_attribute(
    242                         array(
    243                                 'post' => self::$post_id,
    244                                 'echo' => false,
    245                         )
    246                 );
    247 
     235         * Tests that the_title_attribute() accepts a WP_Post object.
     236         *
     237         * @ticket 65819
     238         */
     239        public function test_accepts_post_object() {
    248240                $by_object = the_title_attribute(
    249241                        array(
    … …  
    253245                );
    254246
    255                 $this->assertSame( 'Standard Post Title', $by_id );
    256                 $this->assertSame( $by_id, $by_object );
     247                $this->assertSame( 'Standard Post Title', $by_object );
    257248        }
    258249
  • trunk/tests/phpunit/tests/post/walkerPageDropdown.php

    r64380 r64987  
    6666                                '<option class="level-0" value="7">About</option>',
    6767                        ),
    68                         'first level of depth'                 => array(
    69                                 array(),
    70                                 1,
    71                                 '<option class="level-1" value="7">&nbsp;&nbsp;&nbsp;About</option>',
    72                         ),
    73                         'second level of depth'                => array(
    74                                 array(),
    75                                 2,
    76                                 '<option class="level-2" value="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;About</option>',
    77                         ),
    78                         'selected by integer ID'               => array(
    79                                 array( 'selected' => 7 ),
    80                                 0,
    81                                 '<option class="level-0" value="7" selected="selected">About</option>',
    82                         ),
    8368                        'selected by string ID'                => array(
    8469                                array( 'selected' => '7' ),
    8570                                0,
    8671                                '<option class="level-0" value="7" selected="selected">About</option>',
    87                         ),
    88                         'another page is selected'             => array(
    89                                 array( 'selected' => 70 ),
    90                                 0,
    91                                 '<option class="level-0" value="7">About</option>',
    92                         ),
    93                         'selected value is an empty string'    => array(
    94                                 array( 'selected' => '' ),
    95                                 0,
    96                                 '<option class="level-0" value="7">About</option>',
    97                         ),
    98                         'selected value is false'              => array(
    99                                 array( 'selected' => false ),
    100                                 0,
    101                                 '<option class="level-0" value="7">About</option>',
    10272                        ),
    10373                        'post name as the value field'         => array(
    … …  
    12797                                '<option class="level-0" value="7">About</option>',
    12898                        ),
    129                         'with depth and selection'             => array(
    130                                 array( 'selected' => 7 ),
    131                                 1,
    132                                 '<option class="level-1" value="7" selected="selected">&nbsp;&nbsp;&nbsp;About</option>',
    133                         ),
    13499                );
    135100        }
    … …  
    164129        public static function data_page_titles() {
    165130                return array(
    166                         'plain title'            => array( 'Contact us', 'Contact us' ),
    167                         'title with markup'      => array( 'Tom & <b>Jerry</b> "quoted"', 'Tom &amp; &lt;b&gt;Jerry&lt;/b&gt; &quot;quoted&quot;' ),
    168                         'empty title'            => array( '', '#7 (no title)' ),
    169                         'zero title'             => array( '0', '0' ),
    170                         'title with only spaces' => array( '  ', '  ' ),
     131                        'title with markup' => array( 'Tom & <b>Jerry</b> "quoted"', 'Tom &amp; &lt;b&gt;Jerry&lt;/b&gt; &quot;quoted&quot;' ),
     132                        'empty title'       => array( '', '#7 (no title)' ),
     133                        'zero title'        => array( '0', '0' ),
    171134                );
    172135        }
  • trunk/tests/phpunit/tests/rewrite/wpMatchesMapRegex.php

    r64400 r64987  
    3939
    4040                return array(
    41                         'single placeholder'               => array(
    42                                 'index.php?pagename=$matches[1]',
    43                                 array( 'about/', 'about' ),
    44                                 'index.php?pagename=about',
    45                         ),
    4641                        'several placeholders'             => array(
    4742                                'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
    … …  
    7469                                'index.php?a=first&b=',
    7570                        ),
    76                         'empty match'                      => array(
    77                                 'index.php?a=$matches[1]&b=$matches[2]',
    78                                 array( 'full', '', 'second' ),
    79                                 'index.php?a=&b=second',
    80                         ),
    8171                        'zero string match'                => array(
    8272                                'index.php?page=$matches[1]',
    8373                                array( 'full', '0' ),
    8474                                'index.php?page=0',
    85                         ),
    86                         'no matches'                       => array(
    87                                 'index.php?a=$matches[1]',
    88                                 array(),
    89                                 'index.php?a=',
    9075                        ),
    9176                        'two-digit placeholders'           => array(
    … …  
    117102                                'index.php?a=$match[1]',
    118103                        ),
    119                         'subject without placeholders'     => array(
    120                                 'index.php?feed=rss2',
    121                                 array( 'full', 'first' ),
    122                                 'index.php?feed=rss2',
    123                         ),
    124104                        'empty subject'                    => array(
    125105                                '',
    … …  
    129109                );
    130110        }
    131 
    132         /**
    133          * Tests that a new instance stores the mapped subject in its output property.
    134          *
    135          * @ticket 65819
    136          *
    137          * @covers ::__construct
    138          * @covers ::_map
    139          */
    140         public function test_constructor_sets_output() {
    141                 $map = new WP_MatchesMapRegex( 'index.php?p=$matches[1]', array( 'full', '123' ) );
    142 
    143                 $this->assertSame( 'index.php?p=123', $map->output );
    144         }
    145111}
  • trunk/tests/phpunit/tests/term/defaultTopicCountScale.php

    r64427 r64987  
    3131        public static function data_counts() {
    3232                return array(
    33                         'zero'                => array( 0, 0 ),
    34                         'one'                 => array( 1, 30 ),
    35                         'rounds down'         => array( 2, 48 ),
    36                         'rounds up'           => array( 4, 70 ),
    37                         'nine'                => array( 9, 100 ),
    38                         'ninety-nine'         => array( 99, 200 ),
    39                         'nine hundred ninety' => array( 999, 300 ),
    40                         'numeric string'      => array( '9', 100 ),
     33                        'zero'        => array( 0, 0 ),
     34                        'one'         => array( 1, 30 ),
     35                        'rounds down' => array( 2, 48 ),
     36                        'rounds up'   => array( 4, 70 ),
     37                        'ninety-nine' => array( 99, 200 ),
    4138                );
    4239        }
  • trunk/tests/phpunit/tests/term/getTheTermList.php

    r64473 r64987  
    118118
    119119                $this->assertWPError( $result );
     120                $this->assertSame( 'invalid_term', $result->get_error_code() );
    120121        }
    121122
Note: See TracChangeset for help on using the changeset viewer.