Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: Add tests for virtual node breadcrumbs and depth #6929

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,66 @@ public function test_retains_proper_bookmarks_after_seeking_back_to_closed_eleme
'Should have retained breadcrumbs from bookmarked location after seeking backwards to it.'
);
}

/**
* Ensures that breadcrumbs are properly reported on virtual nodes.
*
* @dataProvider data_virtual_nodes_breadcrumbs
*
* @ticket 61348
*/
public function test_breadcrumbs_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
$processor = WP_HTML_Processor::create_fragment( $html );

for ( $i = 0; $i < $token_position; $i++ ) {
$processor->next_token();
}

$this->assertSame( $expected_tag_name, $processor->get_tag(), "Found incorrect tag name {$processor->get_tag()}." );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted in #6765 I generally try to avoid duplicating content in the test failure when it's already reported. Either it's simple enough to let the error say "Failed to find expected tag name" or we add some new and useful information in the interpolation. It's never worth repeating the invalid information in my opinion, because that only repeats that the test failed instead of clueing someone in to what it should be or how they can make it right.

if ( 'open' === $expect_open_close ) {
$this->assertFalse( $processor->is_tag_closer(), "Found closer when opener expected at {$processor->get_tag()}." );
} else {
$this->assertTrue( $processor->is_tag_closer(), "Found opener when closer expected at {$processor->get_tag()}." );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tests it's probably helpful to use get_token_name() since if it gets off track on something that isn't a tag this will turn into an empty string when casting from false for the interpolation

}

$this->assertEquals( $expected_breadcrumbs, $processor->get_breadcrumbs(), "Found incorrect breadcrumbs in {$html}." );
}

/**
* Ensures that get_current_depth reports the correct depth on virtual nodes.
*
* @dataProvider data_virtual_nodes_breadcrumbs
*
* @ticket 61348
*/
public function test_depth_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
$processor = WP_HTML_Processor::create_fragment( $html );

for ( $i = 0; $i < $token_position; $i++ ) {
$processor->next_token();
}

$this->assertSame( count( $expected_breadcrumbs ), $processor->get_current_depth(), "Found incorrect depth in {$html}." );
}

/**
* Data provider.
*
* Data arrays will have the following shape:
* 0: string Input html.
* 1: int Token index to seek.
* 2: string Expected tag name.
* 3: string 'open' or 'close' indicating an opener or closer is expected.
* 4: array<string> Expected breadcrumbs.
*
* @return array[]
*/
public static function data_virtual_nodes_breadcrumbs() {
return array(
'Implied P tag opener on unmatched closer' => array( '</p>', 1, 'P', 'open', array( 'HTML', 'BODY', 'P' ) ),
'Implied heading tag closer on heading child' => array( '<h1><h2>', 2, 'H1', 'close', array( 'HTML', 'BODY' ) ),
'Implied A tag closer on A tag child' => array( '<a><a>', 2, 'A', 'close', array( 'HTML', 'BODY' ) ),
'Implied A tag closer on A tag descendent' => array( '<a><span><a>', 4, 'A', 'close', array( 'HTML', 'BODY' ) ),
);
}
}
Loading