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: Test cleanup #5987

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
50 changes: 25 additions & 25 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function test_warns_that_the_static_creator_methods_should_be_called_inst
* @covers WP_HTML_Processor::get_tag
*/
public function test_get_tag_is_null_once_document_is_finished() {
$p = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' );
$p->next_tag();
$this->assertSame( 'DIV', $p->get_tag() );
$processor = WP_HTML_Processor::create_fragment( '<div class="test">Test</div>' );
$processor->next_tag();
$this->assertSame( 'DIV', $processor->get_tag() );

$this->assertFalse( $p->next_tag() );
$this->assertNull( $p->get_tag() );
$this->assertFalse( $processor->next_tag() );
$this->assertNull( $processor->get_tag() );
}

/**
Expand All @@ -77,44 +77,44 @@ public function test_get_tag_is_null_once_document_is_finished() {
* @covers WP_HTML_Processor::seek
*/
public function test_clear_to_navigate_after_seeking() {
$p = WP_HTML_Processor::create_fragment( '<div one><strong></strong></div><p><strong two></strong></p>' );
$processor = WP_HTML_Processor::create_fragment( '<div one><strong></strong></div><p><strong two></strong></p>' );

while ( $p->next_tag() ) {
while ( $processor->next_tag() ) {
// Create a bookmark before entering a stack of elements and formatting elements.
if ( null !== $p->get_attribute( 'one' ) ) {
$this->assertTrue( $p->set_bookmark( 'one' ) );
if ( null !== $processor->get_attribute( 'one' ) ) {
$this->assertTrue( $processor->set_bookmark( 'one' ) );
continue;
}

// Create a bookmark inside of that stack.
if ( null !== $p->get_attribute( 'two' ) ) {
$p->set_bookmark( 'two' );
if ( null !== $processor->get_attribute( 'two' ) ) {
$processor->set_bookmark( 'two' );
break;
}
}

// Ensure that it's possible to seek back to the outside location.
$this->assertTrue( $p->seek( 'one' ), 'Could not seek to earlier-seen location.' );
$this->assertSame( 'DIV', $p->get_tag(), "Should have jumped back to DIV but found {$p->get_tag()} instead." );
$this->assertTrue( $processor->seek( 'one' ), 'Could not seek to earlier-seen location.' );
$this->assertSame( 'DIV', $processor->get_tag(), "Should have jumped back to DIV but found {$processor->get_tag()} instead." );

/*
* Ensure that the P element from the inner location isn't still on the stack of open elements.
* If it were, then the first STRONG element, inside the outer DIV would match the next call.
*/
$this->assertTrue( $p->next_tag( array( 'breadcrumbs' => array( 'P', 'STRONG' ) ) ), 'Failed to find given location after seeking.' );
$this->assertTrue( $processor->next_tag( array( 'breadcrumbs' => array( 'P', 'STRONG' ) ) ), 'Failed to find given location after seeking.' );

// Only if the stack is properly managed will the processor advance to the inner STRONG element.
$this->assertTrue( $p->get_attribute( 'two' ), "Found the wrong location given the breadcrumbs, at {$p->get_tag()}." );
$this->assertTrue( $processor->get_attribute( 'two' ), "Found the wrong location given the breadcrumbs, at {$processor->get_tag()}." );

// Ensure that in seeking backwards the processor reports the correct full set of breadcrumbs.
$this->assertTrue( $p->seek( 'one' ), 'Failed to jump back to first bookmark.' );
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $p->get_breadcrumbs(), 'Found wrong set of breadcrumbs navigating to node "one".' );
$this->assertTrue( $processor->seek( 'one' ), 'Failed to jump back to first bookmark.' );
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs(), 'Found wrong set of breadcrumbs navigating to node "one".' );

// Ensure that in seeking forwards the processor reports the correct full set of breadcrumbs.
$this->assertTrue( $p->seek( 'two' ), 'Failed to jump forward to second bookmark.' );
$this->assertTrue( $p->get_attribute( 'two' ), "Found the wrong location given the bookmark, at {$p->get_tag()}." );
$this->assertTrue( $processor->seek( 'two' ), 'Failed to jump forward to second bookmark.' );
$this->assertTrue( $processor->get_attribute( 'two' ), "Found the wrong location given the bookmark, at {$processor->get_tag()}." );

$this->assertSame( array( 'HTML', 'BODY', 'P', 'STRONG' ), $p->get_breadcrumbs(), 'Found wrong set of bookmarks navigating to node "two".' );
$this->assertSame( array( 'HTML', 'BODY', 'P', 'STRONG' ), $processor->get_breadcrumbs(), 'Found wrong set of bookmarks navigating to node "two".' );
}

/**
Expand All @@ -126,10 +126,10 @@ public function test_clear_to_navigate_after_seeking() {
* @covers WP_HTML_Processor::reconstruct_active_formatting_elements
*/
public function test_fails_to_reconstruct_formatting_elements() {
$p = WP_HTML_Processor::create_fragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' );
$processor = WP_HTML_Processor::create_fragment( '<p><em>One<p><em>Two<p><em>Three<p><em>Four' );

$this->assertTrue( $p->next_tag( 'EM' ), 'Could not find first EM.' );
$this->assertFalse( $p->next_tag( 'EM' ), 'Should have aborted before finding second EM as it required reconstructing the first EM.' );
$this->assertTrue( $processor->next_tag( 'EM' ), 'Could not find first EM.' );
$this->assertFalse( $processor->next_tag( 'EM' ), 'Should have aborted before finding second EM as it required reconstructing the first EM.' );
}

/**
Expand Down Expand Up @@ -246,7 +246,7 @@ public function test_cannot_nest_void_tags_next_token( $tag_name ) {
*
* @return array[]
*/
public function data_void_tags() {
public static function data_void_tags() {
return array(
'AREA' => array( 'AREA' ),
'BASE' => array( 'BASE' ),
Expand Down Expand Up @@ -290,7 +290,7 @@ public function test_step_in_body_fails_on_unsupported_tags( $tag_name ) {
*
* @return array[]
*/
public function data_unsupported_special_in_body_tags() {
public static function data_unsupported_special_in_body_tags() {
return array(
'APPLET' => array( 'APPLET' ),
'BASE' => array( 'BASE' ),
Expand Down
98 changes: 49 additions & 49 deletions tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class Tests_HtmlApi_WpHtmlProcessorBreadcrumbs extends WP_UnitTestCase {
* @param string $tag_name Name of first tag in HTML (because HTML treats IMAGE as IMG this may not match the HTML).
*/
public function test_navigates_into_normative_html_for_supported_elements( $html, $tag_name ) {
$p = WP_HTML_Processor::create_fragment( $html );
$processor = WP_HTML_Processor::create_fragment( $html );

$this->assertTrue( $p->step(), "Failed to step into supported {$tag_name} element." );
$this->assertSame( $tag_name, $p->get_tag(), "Misread {$tag_name} as a {$p->get_tag()} element." );
$this->assertTrue( $processor->step(), "Failed to step into supported {$tag_name} element." );
$this->assertSame( $tag_name, $processor->get_tag(), "Misread {$tag_name} as a {$processor->get_tag()} element." );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_single_tag_of_supported_elements() {
public static function data_single_tag_of_supported_elements() {
$supported_elements = array(
'A',
'ABBR',
Expand Down Expand Up @@ -155,17 +155,17 @@ public function data_single_tag_of_supported_elements() {
* @param string $html HTML string containing unsupported elements.
*/
public function test_fails_when_encountering_unsupported_tag( $html ) {
$p = WP_HTML_Processor::create_fragment( $html );
$processor = WP_HTML_Processor::create_fragment( $html );

$this->assertFalse( $p->step(), "Should not have stepped into unsupported {$p->get_tag()} element." );
$this->assertFalse( $processor->step(), "Should not have stepped into unsupported {$processor->get_tag()} element." );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_unsupported_elements() {
public static function data_unsupported_elements() {
$unsupported_elements = array(
'APPLET', // Deprecated.
'BASE',
Expand Down Expand Up @@ -229,22 +229,22 @@ public function data_unsupported_elements() {
* @param string $html HTML containing unsupported markup.
*/
public function test_fails_when_encountering_unsupported_markup( $html, $description ) {
$p = WP_HTML_Processor::create_fragment( $html );
$processor = WP_HTML_Processor::create_fragment( $html );

while ( $p->step() && null === $p->get_attribute( 'supported' ) ) {
while ( $processor->step() && null === $processor->get_attribute( 'supported' ) ) {
continue;
}

$this->assertTrue( $p->get_attribute( 'supported' ), 'Did not find required supported element.' );
$this->assertFalse( $p->step(), "Didn't properly reject unsupported markup: {$description}" );
$this->assertTrue( $processor->get_attribute( 'supported' ), 'Did not find required supported element.' );
$this->assertFalse( $processor->step(), "Didn't properly reject unsupported markup: {$description}" );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_unsupported_markup() {
public static function data_unsupported_markup() {
return array(
'A with formatting following unclosed A' => array(
'<a><strong>Click <a supported><big unsupported>Here</big></a></strong></a>',
Expand All @@ -270,17 +270,17 @@ public function data_unsupported_markup() {
* @param int $n How many breadcrumb matches to scan through in order to find "target" element.
*/
public function test_finds_correct_tag_given_breadcrumbs( $html, $breadcrumbs, $n ) {
$p = WP_HTML_Processor::create_fragment( $html );
$processor = WP_HTML_Processor::create_fragment( $html );

$p->next_tag(
$processor->next_tag(
array(
'breadcrumbs' => $breadcrumbs,
'match_offset' => $n,
)
);

$this->assertNotNull( $p->get_tag(), 'Failed to find target node.' );
$this->assertTrue( $p->get_attribute( 'target' ), "Found {$p->get_tag()} element didn't contain the necessary 'target' attribute." );
$this->assertNotNull( $processor->get_tag(), 'Failed to find target node.' );
$this->assertTrue( $processor->get_attribute( 'target' ), "Found {$processor->get_tag()} element didn't contain the necessary 'target' attribute." );
}

/**
Expand All @@ -295,22 +295,22 @@ public function test_finds_correct_tag_given_breadcrumbs( $html, $breadcrumbs, $
* @param int $ignored_n Not used in this test but provided in the dataset for other tests.
*/
public function test_reports_correct_breadcrumbs_for_html( $html, $breadcrumbs, $ignored_n ) {
$p = WP_HTML_Processor::create_fragment( $html );
$processor = WP_HTML_Processor::create_fragment( $html );

while ( $p->next_tag() && null === $p->get_attribute( 'target' ) ) {
while ( $processor->next_tag() && null === $processor->get_attribute( 'target' ) ) {
continue;
}

$this->assertNotNull( $p->get_tag(), 'Failed to find the target node.' );
$this->assertSame( $breadcrumbs, $p->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' );
$this->assertNotNull( $processor->get_tag(), 'Failed to find the target node.' );
$this->assertSame( $breadcrumbs, $processor->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_html_target_with_breadcrumbs() {
public static function data_html_target_with_breadcrumbs() {
return array(
'Simple IMG tag' => array( '<img target>', array( 'HTML', 'BODY', 'IMG' ), 1 ),
'Two sibling IMG tags' => array( '<img><img target>', array( 'HTML', 'BODY', 'IMG' ), 2 ),
Expand Down Expand Up @@ -393,7 +393,7 @@ public function test_reports_if_tag_matches_breadcrumbs_of_various_specificity(
*
* @return array[].
*/
public function data_html_with_breadcrumbs_of_various_specificity() {
public static function data_html_with_breadcrumbs_of_various_specificity() {
return array(
// Test with void elements.
'Inner IMG' => array( '<div><span><figure><img target></figure></span></div>', array( 'span', 'figure', 'img' ), true ),
Expand Down Expand Up @@ -433,38 +433,38 @@ public function data_html_with_breadcrumbs_of_various_specificity() {
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_remains_stable_when_editing_attributes() {
$p = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );
$p->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );
$processor = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );
$processor->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );

$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
$processor->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag.'
);

$p->set_attribute( 'a-name', 'a-value' );
$processor->set_attribute( 'a-name', 'a-value' );

$this->assertTrue(
$p->get_attribute( 'here' ),
$processor->get_attribute( 'here' ),
'Should have found the B tag but could not find expected "here" attribute.'
);

$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
$processor->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag.'
);

$p->get_updated_html();
$processor->get_updated_html();

$this->assertTrue(
$p->get_attribute( 'here' ),
$processor->get_attribute( 'here' ),
'Should have stayed at the B tag but could not find expected "here" attribute.'
);

$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
$p->get_breadcrumbs(),
$processor->get_breadcrumbs(),
'Found the wrong nested structure at the matched tag after updating attributes.'
);
}
Expand All @@ -479,12 +479,12 @@ public function test_remains_stable_when_editing_attributes() {
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_can_modify_attributes_after_finding_tag() {
$p = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );
$processor = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );

$this->assertTrue( $p->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );
$this->assertTrue( $processor->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );

$p->set_attribute( 'found-it', true );
$this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $p->get_updated_html() );
$processor->set_attribute( 'found-it', true );
$this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $processor->get_updated_html() );
}

/**
Expand All @@ -497,11 +497,11 @@ public function test_can_modify_attributes_after_finding_tag() {
* @covers WP_HTML_Processor::next_tag
*/
public function test_can_query_an_element_by_tag_name() {
$p = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );
$p->next_tag( 'IMG' );
$p->set_attribute( 'loading', 'lazy' );
$processor = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );
$processor->next_tag( 'IMG' );
$processor->set_attribute( 'loading', 'lazy' );

$this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $p->get_updated_html() );
$this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $processor->get_updated_html() );
}

/**
Expand All @@ -514,35 +514,35 @@ public function test_can_query_an_element_by_tag_name() {
* @covers WP_HTML_Processor::seek
*/
public function test_can_seek_back_and_forth() {
$p = WP_HTML_Processor::create_fragment(
$processor = WP_HTML_Processor::create_fragment(
<<<'HTML'
<div>text<p one>more stuff<div><![CDATA[this is not real CDATA]]><p><!-- hi --><div two><p><div><p>three comes soon<div><p three>' );
HTML
);

// Find first tag of interest.
while ( $p->next_tag() && null === $p->get_attribute( 'one' ) ) {
while ( $processor->next_tag() && null === $processor->get_attribute( 'one' ) ) {
continue;
}
$p->set_bookmark( 'first' );
$processor->set_bookmark( 'first' );

// Find second tag of interest.
while ( $p->next_tag() && null === $p->get_attribute( 'two' ) ) {
while ( $processor->next_tag() && null === $processor->get_attribute( 'two' ) ) {
continue;
}
$p->set_bookmark( 'second' );
$processor->set_bookmark( 'second' );

// Find third tag of interest.
while ( $p->next_tag() && null === $p->get_attribute( 'three' ) ) {
while ( $processor->next_tag() && null === $processor->get_attribute( 'three' ) ) {
continue;
}
$p->set_bookmark( 'third' );
$processor->set_bookmark( 'third' );

// Seek backwards.
$p->seek( 'first' );
$processor->seek( 'first' );

// Seek forwards. If the current token isn't also updated this could appear like a backwards seek.
$p->seek( 'second' );
$this->assertTrue( $p->get_attribute( 'two' ) );
$processor->seek( 'second' );
$this->assertTrue( $processor->get_attribute( 'two' ) );
}
}
Loading
Loading