Make WordPress Core

Changeset 60972


Ignore:
Timestamp:
10/19/2025 12:17:57 AM (13 months ago)
Author:
dmsnell
Message:

HTML API: Rely on assertEqualHTML in oEmbed filtering tests.

As part of ongoing work to improve the reliability of HTML parsing code in WordPress, this patch replaces the use of PCRE matches in oEmbed filtering tests with semantic assertions via the HTML API and assertEqualHTML().

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9259
Discussed in https://core.trac.wordpress.org/ticket/63694

Props dmsnell, jonsurrell.
See #63694

Location:
trunk/tests/phpunit/tests/oembed
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/filterResult.php

    r58143 r60972  
    1010                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'https://www.youtube.com/watch?v=72xdCU__XCk' );
    1111
    12                 $this->assertSame( $html, $actual );
     12                $this->assertEqualHTML( $html, $actual );
    1313        }
    1414
    … …  
    1717                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'http://example.com/sample-page/' );
    1818
    19                 $matches = array();
    20                 preg_match( '|src=".*#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
    21 
    22                 $this->assertArrayHasKey( 1, $matches );
    23                 $this->assertArrayHasKey( 2, $matches );
    24                 $this->assertSame( $matches[1], $matches[2] );
     19                $processor = new WP_HTML_Tag_Processor( $actual );
     20
     21                $this->assertTrue(
     22                        $processor->next_tag( 'IFRAME' ),
     23                        'Failed to find expected IFRAME element in filtered output.'
     24                );
     25
     26                $src = $processor->get_attribute( 'src' );
     27                $this->assertIsString(
     28                        $src,
     29                        isset( $src )
     30                                ? 'Expected "src" attribute on IFRAME with string value but found boolean attribute instead.'
     31                                : 'Failed to find expected "src" attribute on IFRAME element.'
     32                );
     33
     34                $query_string = parse_url( $src, PHP_URL_FRAGMENT );
     35                $this->assertStringStartsWith(
     36                        '?',
     37                        $query_string,
     38                        'Should have found URL fragment in "src" attribute resembling a query string.'
     39                );
     40
     41                $query_string = substr( $query_string, 1 );
     42                $query_args   = array();
     43                parse_str( $query_string, $query_args );
     44
     45                $this->assertArrayHasKey(
     46                        'secret',
     47                        $query_args,
     48                        'Failed to find expected query arg "secret" in IFRAME "src" attribute.'
     49                );
     50
     51                $this->assertSame(
     52                        $query_args['secret'],
     53                        $processor->get_attribute( 'data-secret' ),
     54                        'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute.'
     55                );
    2556        }
    2657
    … …  
    2960                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
    3061
    31                 $this->assertSame( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
     62                $this->assertEqualHTML( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
    3263        }
    3364
    … …  
    4273                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
    4374
    44                 $this->assertSame( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
     75                $this->assertEqualHTML( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
    4576        }
    4677
    … …  
    6192                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
    6293
    63                 $matches = array();
    64                 preg_match( '|src="https://wordpress.org#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
    65 
    66                 $this->assertArrayHasKey( 1, $matches );
    67                 $this->assertArrayHasKey( 2, $matches );
    68                 $this->assertSame( $matches[1], $matches[2] );
     94                $processor = new WP_HTML_Tag_Processor( $actual );
     95
     96                $this->assertTrue(
     97                        $processor->next_tag( 'IFRAME' ),
     98                        'Failed to find expected IFRAME element in filtered output.'
     99                );
     100
     101                $src = $processor->get_attribute( 'src' );
     102                $this->assertMatchesRegularExpression(
     103                        '~^https://wordpress.org~',
     104                        $src,
     105                        'Failed to find expected "src" attribute on IFRAME element.'
     106                );
     107
     108                $query_string = parse_url( $src, PHP_URL_FRAGMENT );
     109                $this->assertStringStartsWith(
     110                        '?',
     111                        $query_string,
     112                        'Should have found URL fragment in "src" attribute resembling a query string.'
     113                );
     114
     115                $query_string = substr( $query_string, 1 );
     116                $query_args   = array();
     117                parse_str( $query_string, $query_args );
     118
     119                $this->assertArrayHasKey(
     120                        'secret',
     121                        $query_args,
     122                        'Failed to find expected query arg "secret" in IFRAME "src" attribute.'
     123                );
     124
     125                $this->assertSame(
     126                        $query_args['secret'],
     127                        $processor->get_attribute( 'data-secret' ),
     128                        'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute.'
     129                );
    69130        }
    70131
    … …  
    72133                $actual = wp_filter_oembed_result( 'some string', (object) array( 'type' => 'link' ), '' );
    73134
    74                 $this->assertSame( 'some string', $actual );
     135                $this->assertEqualHTML( 'some string', $actual );
    75136        }
    76137
    … …  
    84145                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
    85146
    86                 $this->assertSame( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe>', $actual );
     147                $this->assertEqualHTML( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe>', $actual );
    87148        }
    88149
    … …  
    91152                $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
    92153
    93                 $this->assertSame( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe>', $actual );
     154                $this->assertEqualHTML( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe>', $actual );
    94155        }
    95156
    … …  
    125186                );
    126187                $actual = _wp_oembed_get_object()->data2html( $data, 'https://untrusted.localhost' );
    127                 $this->assertSame( $expected, $actual );
     188                $this->assertEqualHTML( $expected, $actual );
    128189        }
    129190
    … …  
    135196                $actual = _oembed_filter_feed_content( wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' ) );
    136197
    137                 $this->assertSame( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe>', $actual );
     198                $this->assertEqualHTML( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe>', $actual );
    138199        }
    139200}
  • trunk/tests/phpunit/tests/oembed/filterTitleAttributes.php

    r49117 r60972  
    6868                $actual = wp_filter_oembed_iframe_title_attribute( $html, (object) $oembed_data, $url );
    6969
    70                 $this->assertSame( $expected, $actual );
     70                $this->assertEqualHTML( $expected, $actual );
    7171        }
    7272
    … …  
    8585                remove_filter( 'oembed_iframe_title_attribute', array( $this, '_filter_oembed_iframe_title_attribute' ) );
    8686
    87                 $this->assertSame( '<iframe title="Baz" src=""></iframe>', $actual );
     87                $this->assertEqualHTML( '<iframe title="Baz" src=""></iframe>', $actual );
    8888        }
    8989
    … …  
    102102                remove_filter( 'oembed_iframe_title_attribute', array( $this, '_filter_oembed_iframe_title_attribute' ) );
    103103
    104                 $this->assertSame( '<p title="Bar">Baz</p><iframe title="Baz" src=""></iframe>', $actual );
     104                $this->assertEqualHTML( '<p title="Bar">Baz</p><iframe title="Baz" src=""></iframe>', $actual );
    105105        }
    106106
Note: See TracChangeset for help on using the changeset viewer.