Make WordPress Core


Ignore:
Timestamp:
06/28/2024 09:20:10 AM (5 months ago)
Author:
gziolo
Message:

HTML API: Add tests for virtual node breadcrumbs and depth

Follow-up [58590].
See #61348.
Props jonsurrell, gziolo.

File:
1 edited

Legend:

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

    r58304 r58592  
    574574        );
    575575    }
     576
     577    /**
     578     * Ensures that breadcrumbs are properly reported on virtual nodes.
     579     *
     580     * @ticket 61348
     581     *
     582     * @dataProvider data_virtual_nodes_breadcrumbs
     583     *
     584     * @covers WP_HTML_Processor::get_breadcrumbs
     585     */
     586    public function test_breadcrumbs_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
     587        $processor = WP_HTML_Processor::create_fragment( $html );
     588
     589        for ( $i = 0; $i < $token_position; $i++ ) {
     590            $processor->next_token();
     591        }
     592
     593        $this->assertSame( $expected_tag_name, $processor->get_tag(), "Found incorrect tag name {$processor->get_tag()}." );
     594        if ( 'open' === $expect_open_close ) {
     595            $this->assertFalse( $processor->is_tag_closer(), "Found closer when opener expected at {$processor->get_tag()}." );
     596        } else {
     597            $this->assertTrue( $processor->is_tag_closer(), "Found opener when closer expected at {$processor->get_tag()}." );
     598        }
     599
     600        $this->assertEquals( $expected_breadcrumbs, $processor->get_breadcrumbs(), "Found incorrect breadcrumbs in {$html}." );
     601    }
     602
     603    /**
     604     * Ensures that get_current_depth reports the correct depth on virtual nodes.
     605     *
     606     * @ticket 61348
     607     *
     608     * @dataProvider data_virtual_nodes_breadcrumbs
     609     *
     610     * @covers WP_HTML_Processor::get_current_depth
     611     */
     612    public function test_depth_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
     613        $processor = WP_HTML_Processor::create_fragment( $html );
     614
     615        for ( $i = 0; $i < $token_position; $i++ ) {
     616            $processor->next_token();
     617        }
     618
     619        $this->assertSame( count( $expected_breadcrumbs ), $processor->get_current_depth(), "Found incorrect depth in {$html}." );
     620    }
     621
     622    /**
     623     * Data provider for virtual nodes breadcrumbs with the following shape of arrays:
     624     *     0: string        Input html.
     625     *     1: int           Token index to seek.
     626     *     2: string        Expected tag name.
     627     *     3: string        'open' or 'close' indicating an opener or closer is expected.
     628     *     4: array<string> Expected breadcrumbs.
     629     *
     630     * @return array[]
     631     */
     632    public static function data_virtual_nodes_breadcrumbs() {
     633        return array(
     634            'Implied P tag opener on unmatched closer'    => array( '</p>', 1, 'P', 'open', array( 'HTML', 'BODY', 'P' ) ),
     635            'Implied heading tag closer on heading child' => array( '<h1><h2>', 2, 'H1', 'close', array( 'HTML', 'BODY' ) ),
     636            'Implied A tag closer on A tag child'         => array( '<a><a>', 2, 'A', 'close', array( 'HTML', 'BODY' ) ),
     637            'Implied A tag closer on A tag descendent'    => array( '<a><span><a>', 4, 'A', 'close', array( 'HTML', 'BODY' ) ),
     638        );
     639    }
    576640}
Note: See TracChangeset for help on using the changeset viewer.