Make WordPress Core


Ignore:
Timestamp:
02/01/2024 01:00:56 AM (2 years ago)
Author:
dmsnell
Message:

HTML API: Test cleanup

Rename $p variable to $processor in tests for clarity.

Use static data providers. A mix of static and non-static data providers were
used in HTML API tests. Data providers are required to be static in the next
PHPUnit version and there's no harm in using them consistently now.

Follow-up to [57507]

Props jonsurrell
See #59647

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php

    r57316 r57508  
    8080     * @return array[].
    8181     */
    82     public function data_article_container_group() {
     82    public static function data_article_container_group() {
    8383        $group = array();
    8484
     
    123123     */
    124124    public function test_in_body_skips_unexpected_button_closer() {
    125         $p = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' );
    126 
    127         $p->step();
    128         $this->assertSame( 'DIV', $p->get_tag(), 'Did not stop at initial DIV tag.' );
    129         $this->assertFalse( $p->is_tag_closer(), 'Did not find that initial DIV tag is an opener.' );
     125        $processor = WP_HTML_Processor::create_fragment( '<div>Test</button></div>' );
     126
     127        $processor->step();
     128        $this->assertSame( 'DIV', $processor->get_tag(), 'Did not stop at initial DIV tag.' );
     129        $this->assertFalse( $processor->is_tag_closer(), 'Did not find that initial DIV tag is an opener.' );
    130130
    131131        /*
     
    133133         * It should be ignored as there's no BUTTON to close.
    134134         */
    135         $this->assertTrue( $p->step(), 'Found no further tags when it should have found the closing DIV' );
    136         $this->assertSame( 'DIV', $p->get_tag(), "Did not skip unexpected BUTTON; stopped at {$p->get_tag()}." );
    137         $this->assertTrue( $p->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' );
     135        $this->assertTrue( $processor->step(), 'Found no further tags when it should have found the closing DIV' );
     136        $this->assertSame( 'DIV', $processor->get_tag(), "Did not skip unexpected BUTTON; stopped at {$processor->get_tag()}." );
     137        $this->assertTrue( $processor->is_tag_closer(), 'Did not find that the terminal DIV tag is a closer.' );
    138138    }
    139139
     
    144144     */
    145145    public function test_in_body_button_with_no_button_in_scope() {
    146         $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );
    147 
    148         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
    149         $this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
    150         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
     146        $processor = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>here</button>!</p></div><button two>not here</button>' );
     147
     148        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
     149        $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
     150        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
    151151
    152152        /*
     
    155155         * malformed and unexpected ones.
    156156         */
    157         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
    158         $this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
    159         $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
     157        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
     158        $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
     159        $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
    160160    }
    161161
     
    169169     */
    170170    public function test_in_body_button_with_button_in_scope_as_parent() {
    171         $p = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );
    172 
    173         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
    174         $this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
    175         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
     171        $processor = WP_HTML_Processor::create_fragment( '<div><p>Click the button <button one>almost<button two>here</button>!</p></div><button three>not here</button>' );
     172
     173        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
     174        $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
     175        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
    176176
    177177        /*
     
    179179         * or it may place it as a child of the first one, but it implicitly closes the open BUTTON.
    180180         */
    181         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
    182         $this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
    183         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
     181        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
     182        $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
     183        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'P', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
    184184
    185185        /*
     
    187187         * looking for proper handling of the open and close sequence for the BUTTON tags.
    188188         */
    189         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
    190         $this->assertTrue( $p->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
    191         $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
     189        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
     190        $this->assertTrue( $processor->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
     191        $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
    192192    }
    193193
     
    202202     */
    203203    public function test_in_body_button_with_button_in_scope_as_ancestor() {
    204         $p = WP_HTML_Processor::create_fragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );
     204        $processor = WP_HTML_Processor::create_fragment( '<div><button one><p>Click the button <span><button two>here</button>!</span></p></div><button three>not here</button>' );
    205205
    206206        // This button finds itself normally nesting inside the DIV.
    207         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
    208         $this->assertTrue( $p->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
    209         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
     207        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected first button.' );
     208        $this->assertTrue( $processor->get_attribute( 'one' ), 'Failed to match expected attribute on first button.' );
     209        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for first button.' );
    210210
    211211        /*
     
    215215         * itself a child of the most-recent open element above the most-recent BUTTON, or the DIV.
    216216         */
    217         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
    218         $this->assertTrue( $p->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
    219         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
     217        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected second button.' );
     218        $this->assertTrue( $processor->get_attribute( 'two' ), 'Failed to match expected attribute on second button.' );
     219        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for second button.' );
    220220
    221221        // The third button is back to normal, because everything has been implicitly or explicitly closed by now.
    222         $this->assertTrue( $p->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
    223         $this->assertTrue( $p->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
    224         $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
     222        $this->assertTrue( $processor->next_tag( 'BUTTON' ), 'Could not find expected third button.' );
     223        $this->assertTrue( $processor->get_attribute( 'three' ), 'Failed to match expected attribute on third button.' );
     224        $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' );
    225225    }
    226226
     
    275275     * @return array[].
    276276     */
    277     public function data_heading_elements() {
     277    public static function data_heading_elements() {
    278278        return array(
    279279            'H1' => array( 'H1' ),
     
    329329     * @return array[]
    330330     */
    331     public function data_heading_combinations() {
     331    public static function data_heading_combinations() {
    332332        $headings = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' );
    333333
     
    356356     */
    357357    public function test_in_body_any_other_end_tag_with_unclosed_special_element() {
    358         $p = WP_HTML_Processor::create_fragment( '<div><span><p></span><div>' );
    359 
    360         $p->next_tag( 'P' );
    361         $this->assertSame( 'P', $p->get_tag(), "Expected to start test on P element but found {$p->get_tag()} instead." );
    362         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'P' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
    363 
    364         $this->assertTrue( $p->next_tag(), 'Failed to advance past P tag to expected DIV opener.' );
    365         $this->assertSame( 'DIV', $p->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );
    366         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'DIV' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should still be open and DIV should be its child.' );
     358        $processor = WP_HTML_Processor::create_fragment( '<div><span><p></span><div>' );
     359
     360        $processor->next_tag( 'P' );
     361        $this->assertSame( 'P', $processor->get_tag(), "Expected to start test on P element but found {$processor->get_tag()} instead." );
     362        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'P' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
     363
     364        $this->assertTrue( $processor->next_tag(), 'Failed to advance past P tag to expected DIV opener.' );
     365        $this->assertSame( 'DIV', $processor->get_tag(), "Expected to find DIV element, but found {$processor->get_tag()} instead." );
     366        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should still be open and DIV should be its child.' );
    367367    }
    368368
     
    379379     */
    380380    public function test_in_body_any_other_end_tag_with_unclosed_non_special_element() {
    381         $p = WP_HTML_Processor::create_fragment( '<div><span><code></span><div>' );
    382 
    383         $p->next_tag( 'CODE' );
    384         $this->assertSame( 'CODE', $p->get_tag(), "Expected to start test on CODE element but found {$p->get_tag()} instead." );
    385         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'CODE' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
    386 
    387         $this->assertTrue( $p->step(), 'Failed to advance past CODE tag to expected SPAN closer.' );
    388         $this->assertTrue( $p->is_tag_closer(), 'Expected to find closing SPAN, but found opener instead.' );
    389         $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $p->get_breadcrumbs(), 'Failed to advance past CODE tag to expected DIV opener.' );
    390 
    391         $this->assertTrue( $p->next_tag(), 'Failed to advance past SPAN closer to expected DIV opener.' );
    392         $this->assertSame( 'DIV', $p->get_tag(), "Expected to find DIV element, but found {$p->get_tag()} instead." );
    393         $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'DIV' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should be closed and DIV should be its sibling.' );
     381        $processor = WP_HTML_Processor::create_fragment( '<div><span><code></span><div>' );
     382
     383        $processor->next_tag( 'CODE' );
     384        $this->assertSame( 'CODE', $processor->get_tag(), "Expected to start test on CODE element but found {$processor->get_tag()} instead." );
     385        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'SPAN', 'CODE' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting.' );
     386
     387        $this->assertTrue( $processor->step(), 'Failed to advance past CODE tag to expected SPAN closer.' );
     388        $this->assertTrue( $processor->is_tag_closer(), 'Expected to find closing SPAN, but found opener instead.' );
     389        $this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to advance past CODE tag to expected DIV opener.' );
     390
     391        $this->assertTrue( $processor->next_tag(), 'Failed to advance past SPAN closer to expected DIV opener.' );
     392        $this->assertSame( 'DIV', $processor->get_tag(), "Expected to find DIV element, but found {$processor->get_tag()} instead." );
     393        $this->assertSame( array( 'HTML', 'BODY', 'DIV', 'DIV' ), $processor->get_breadcrumbs(), 'Failed to produce expected DOM nesting: SPAN should be closed and DIV should be its sibling.' );
    394394    }
    395395
     
    413413     */
    414414    public function test_br_end_tag_unsupported() {
    415         $p = WP_HTML_Processor::create_fragment( '</br>' );
    416 
    417         $this->assertFalse( $p->next_tag(), 'Found a BR tag that should not be handled.' );
    418         $this->assertSame( WP_HTML_Processor::ERROR_UNSUPPORTED, $p->get_last_error() );
     415        $processor = WP_HTML_Processor::create_fragment( '</br>' );
     416
     417        $this->assertFalse( $processor->next_tag(), 'Found a BR tag that should not be handled.' );
     418        $this->assertSame( WP_HTML_Processor::ERROR_UNSUPPORTED, $processor->get_last_error() );
    419419    }
    420420}
Note: See TracChangeset for help on using the changeset viewer.