Skip to content

Commit 62da920

Browse files
committed
Improve test coverage in OD_HTML_Tag_Processor
1 parent 89c6e76 commit 62da920

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

plugins/optimization-detective/tests/test-class-od-html-tag-processor.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ public function test_append_head_and_body_html(): void {
443443
$this->assertTrue( $did_seek );
444444
$this->assertTrue( $saw_head );
445445
$this->assertTrue( $saw_body );
446+
$this->assertTrue( $processor->has_bookmark( OD_HTML_Tag_Processor::END_OF_HEAD_BOOKMARK ) );
447+
$this->assertTrue( $processor->has_bookmark( OD_HTML_Tag_Processor::END_OF_BODY_BOOKMARK ) );
446448
$this->assertStringContainsString( $head_injected, $processor->get_updated_html(), 'Only expecting end-of-head injection once document was finalized.' );
447449
$this->assertStringContainsString( $body_injected, $processor->get_updated_html(), 'Only expecting end-of-body injection once document was finalized.' );
448450

@@ -465,6 +467,53 @@ public function test_append_head_and_body_html(): void {
465467
$this->assertSame( $expected, $processor->get_updated_html() );
466468
}
467469

470+
/**
471+
* Test get_updated_html() when running out of bookmarks.
472+
*
473+
* @covers ::get_updated_html
474+
* @covers ::warn
475+
*/
476+
public function test_get_updated_html_when_out_of_bookmarks(): void {
477+
$this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::set_bookmark' );
478+
$html = '
479+
<html>
480+
<head>
481+
<meta charset=utf-8>
482+
</head>
483+
<body>
484+
<h1>Hello World</h1>
485+
</body>
486+
</html>
487+
';
488+
$processor = new OD_HTML_Tag_Processor( $html );
489+
$this->assertTrue( $processor->next_tag() );
490+
$this->assertEquals( 'HTML', $processor->get_tag() );
491+
$max_bookmarks = max( WP_HTML_Processor::MAX_BOOKMARKS, WP_HTML_Tag_Processor::MAX_BOOKMARKS );
492+
for ( $i = 0; $i < $max_bookmarks + 1; $i++ ) {
493+
if ( ! $processor->set_bookmark( "bookmark-$i" ) ) {
494+
break;
495+
}
496+
}
497+
$processor->append_head_html( '<!-- Failed to append to HEAD -->' );
498+
$processor->append_body_html( '<!-- Failed to append to BODY -->' );
499+
500+
$saw_head = false;
501+
$saw_body = false;
502+
while ( $processor->next_open_tag() ) {
503+
$tag = $processor->get_tag();
504+
if ( 'HEAD' === $tag ) {
505+
$saw_head = true;
506+
} elseif ( 'BODY' === $tag ) {
507+
$saw_body = true;
508+
}
509+
}
510+
$this->assertTrue( $saw_head );
511+
$this->assertTrue( $saw_body );
512+
$this->assertFalse( $processor->has_bookmark( OD_HTML_Tag_Processor::END_OF_HEAD_BOOKMARK ) );
513+
$this->assertFalse( $processor->has_bookmark( OD_HTML_Tag_Processor::END_OF_BODY_BOOKMARK ) );
514+
$this->assertSame( $html, $processor->get_updated_html() );
515+
}
516+
468517
/**
469518
* Test get_tag(), get_attribute(), set_attribute(), remove_attribute(), and get_updated_html().
470519
*

0 commit comments

Comments
 (0)