From dbb9487255f2808eb5543cbc2dfa26b8757d3d04 Mon Sep 17 00:00:00 2001
From: Dennis Snell
Date: Mon, 14 Nov 2022 17:32:41 -0700
Subject: [PATCH] Tag Processor: Merge independent tests into single file
---
.../WP_HTML_Tag_Processor_Isolated_Test.php | 145 --
phpunit/html/WP_HTML_Tag_Processor_Test.php | 1187 -----------------
...est.php => wp-html-tag-processor-test.php} | 23 +-
.../html/wp-html-tag-processor-wp-test.php | 91 --
4 files changed, 2 insertions(+), 1444 deletions(-)
delete mode 100644 phpunit/html/WP_HTML_Tag_Processor_Isolated_Test.php
delete mode 100644 phpunit/html/WP_HTML_Tag_Processor_Test.php
rename phpunit/html/{wp-html-tag-processor-standalone-test.php => wp-html-tag-processor-test.php} (98%)
delete mode 100644 phpunit/html/wp-html-tag-processor-wp-test.php
diff --git a/phpunit/html/WP_HTML_Tag_Processor_Isolated_Test.php b/phpunit/html/WP_HTML_Tag_Processor_Isolated_Test.php
deleted file mode 100644
index d593c5d2f4fe92..00000000000000
--- a/phpunit/html/WP_HTML_Tag_Processor_Isolated_Test.php
+++ /dev/null
@@ -1,145 +0,0 @@
-' );
-
- $this->expectException( Exception::class );
-
- $p->next_tag();
- $p->set_attribute( $attribute_name, 'test' );
-
- $this->assertEquals( '', (string) $p );
- }
-
- /**
- * Attribute names with invalid characters should be rejected.
- *
- * When WP_DEBUG isn't set we want to quietly fail to set the
- * invalid attribute to avoid breaking the HTML and to do so
- * without breaking the entire page.
- *
- * @dataProvider data_invalid_attribute_names
- * @covers set_attribute
- */
- public function test_set_attribute_silently_fails_when_given_invalid_attribute_names_outside_of_debug_mode( $attribute_name ) {
- $p = new WP_HTML_Tag_Processor( '' );
-
- $p->next_tag();
- $p->set_attribute( $attribute_name, 'test' );
-
- $this->assertEquals( '', (string) $p );
- }
-
- /**
- * Data provider with invalid HTML attribute names.
- *
- * @return array {
- * @type string $attribute_name Text considered invalid for HTML attribute names.
- * }
- */
- public function data_invalid_attribute_names() {
- return array(
- 'controls_null' => array( "i\x00d" ),
- 'controls_newline' => array( "\nbroken-expectations" ),
- 'space' => array( 'aria label' ),
- 'double-quote' => array( '"id"' ),
- 'single-quote' => array( "'id'" ),
- 'greater-than' => array( 'sneaky>script' ),
- 'solidus' => array( 'data/test-id' ),
- 'equals' => array( 'checked=checked' ),
- 'noncharacters_1' => array( html_entity_decode( 'anything' ) ),
- 'noncharacters_2' => array( html_entity_decode( 'test' ) ),
- 'noncharacters_3' => array( html_entity_decode( 'test' ) ),
- 'noncharacters_4' => array( html_entity_decode( 'test' ) ),
- 'noncharacters_5' => array( html_entity_decode( '' ) ),
- 'wp_no_lt' => array( 'id',
- );
-
- $examples['Simple uppercase script tag'] = array(
- '',
- );
-
- $examples['Script with a comment opener inside should end at the next script tag closer (dash dash escaped state)'] = array(
- '-->',
- );
-
- $examples['Script with a comment opener and a script tag opener inside should end two script tag closer later (double escaped state)'] = array(
- '-->',
- );
-
- $examples['Double escaped script with a tricky opener'] = array(
- '">">',
- );
-
- $examples['Double escaped script with a tricky closer'] = array(
- '">',
- );
-
- $examples['Double escaped, then escaped, then double escaped'] = array(
- '',
- );
-
- $examples['Script with a commented a script tag opener inside should at the next tag closer (dash dash escaped state)'] = array(
- '-->',
- );
-
- $examples['Script closer with another script tag in closer attributes'] = array(
- '',
- );
-
- $examples['Script closer with attributes'] = array(
- '',
- );
-
- $examples['Script opener with title closer inside'] = array(
- '',
- );
-
- $examples['Complex script with many parsing states'] = array(
- '-->-->',
- );
- return $examples;
- }
-
- /**
- * @ticket 56299
- *
- * @covers next_tag
- *
- * @dataProvider data_rcdata_state
- */
- public function test_next_tag_ignores_the_contents_of_a_rcdata_tag( $rcdata_then_div, $rcdata_tag ) {
- $p = new WP_HTML_Tag_Processor( $rcdata_then_div );
- $p->next_tag();
- $this->assertSame( strtoupper( $rcdata_tag ), $p->get_tag(), "The first found tag was not '$rcdata_tag'" );
- $p->next_tag();
- $this->assertSame( 'DIV', $p->get_tag(), "The second found tag was not 'div'" );
- }
-
- /**
- * Data provider for test_ignores_contents_of_a_rcdata_tag().
- *
- * @return array {
- * @type array {
- * @type string $rcdata_then_div The HTML snippet containing RCDATA and div tags.
- * @type string $rcdata_tag The RCDATA tag.
- * }
- * }
- */
- public function data_rcdata_state() {
- $examples = array();
- $examples['Simple textarea'] = array(
- '',
- 'textarea',
- );
-
- $examples['Simple title'] = array(
- 'Back to notifications',
- 'title',
- );
-
- $examples['Comment opener inside a textarea tag should be ignored'] = array(
- '
'
- );
- $p->next_tag( 'span' );
- $p->set_attribute( 'class', 'span-class' );
- $p->next_tag( 'p' );
- $p->set_attribute( 'class', 'p-class' );
- $this->assertSame(
- '123456
789',
- (string) $p
- );
- }
-
- /**
- * @ticket 56299
- *
- * @covers next_tag
- * @covers remove_attribute
- * @covers __toString
- */
- public function test_removing_attributes_works_even_in_malformed_html() {
- $p = new WP_HTML_Tag_Processor( self::HTML_MALFORMED );
- $p->next_tag( 'span' );
- $p->remove_attribute( 'Notifications<' );
- $this->assertSame(
- 'Back to notifications
',
- (string) $p
- );
- }
-
- /**
- * @ticket 56299
- *
- * @covers next_Tag
- * @covers set_attribute
- * @covers __toString
- */
- public function test_updating_attributes_works_even_in_malformed_html_1() {
- $p = new WP_HTML_Tag_Processor( self::HTML_MALFORMED );
- $p->next_tag( 'span' );
- $p->set_attribute( 'id', 'first' );
- $p->next_tag( 'span' );
- $p->set_attribute( 'id', 'second' );
- $this->assertSame(
- 'Back to notifications
',
- (string) $p
- );
- }
-
- /**
- * @ticket 56299
- *
- * @covers next_tag
- * @covers set_attribute
- * @covers add_class
- * @covers __toString
- *
- * @dataProvider data_malformed_tag
- */
- public function test_updating_attributes_works_even_in_malformed_html_2( $html_input, $html_expected ) {
- $p = new WP_HTML_Tag_Processor( $html_input );
- $p->next_tag();
- $p->set_attribute( 'foo', 'bar' );
- $p->add_class( 'firstTag' );
- $p->next_tag();
- $p->add_class( 'secondTag' );
- $this->assertSame(
- $html_expected,
- (string) $p
- );
- }
-
- /**
- * Data provider for test_updates_when_malformed_tag().
- *
- * @return array {
- * @type array {
- * @type string $html_input The input HTML snippet.
- * @type string $html_expected The expected HTML snippet after processing.
- * }
- * }
- */
- public function data_malformed_tag() {
- $null_byte = chr( 0 );
- $examples = array();
- $examples['Invalid entity inside attribute value'] = array(
- '
test',
- '
test',
- );
-
- $examples['HTML tag opening inside attribute value'] = array(
- 'This <is> a <strong is="true">thing.
test',
- 'This <is> a <strong is="true">thing.
test',
- );
-
- $examples['HTML tag brackets in attribute values and data markup'] = array(
- 'This <is> a <strong is="true">thing.
test',
- 'This <is> a <strong is="true">thing.
test',
- );
-
- $examples['Single and double quotes in attribute value'] = array(
- 'test',
- '
test',
- );
-
- $examples['Unquoted attribute values'] = array(
- '
test',
- '
test',
- );
-
- $examples['Double-quotes escaped in double-quote attribute value'] = array(
- '
test',
- '
test',
- );
-
- $examples['Unquoted attribute value'] = array(
- '
test',
- '
test',
- );
-
- $examples['Unquoted attribute value with tag-like value'] = array(
- '
>test',
- '
>test',
- );
-
- $examples['Unquoted attribute value with tag-like value followed by tag-like data'] = array(
- '
>test',
- '
>test',
- );
-
- $examples['1'] = array(
- '
test',
- '
test',
- );
-
- $examples['2'] = array(
- '
test',
- '
test',
- );
-
- $examples['4'] = array(
- '
test',
- '
test',
- );
-
- $examples['5'] = array(
- '
code>test',
- '
code>test',
- );
-
- $examples['6'] = array(
- '
test',
- '
test',
- );
-
- $examples['7'] = array(
- '
test',
- '
test',
- );
-
- $examples['8'] = array(
- '
id="test">test',
- '
id="test">test',
- );
-
- $examples['9'] = array(
- '
test',
- '
test',
- );
-
- $examples['10'] = array(
- '>test',
- '>test',
- );
-
- $examples['11'] = array(
- 'The applicative operator <* works well in Haskell; is what?test',
- 'The applicative operator <* works well in Haskell; is what?test',
- );
-
- $examples['12'] = array(
- '<3 is a heart but is a tag.test',
- '<3 is a heart but is a tag.test',
- );
-
- $examples['13'] = array(
- 'test',
- 'test',
- );
-
- $examples['14'] = array(
- 'test',
- 'test',
- );
-
- $examples['15'] = array(
- ' a HTML Tag]]>test',
- ' a HTML Tag]]>test',
- );
-
- $examples['16'] = array(
- '
test',
- '
test',
- );
-
- $examples['17'] = array(
- '
test',
- '
test',
- );
-
- $examples['18'] = array(
- '
test',
- '
test',
- );
-
- $examples['19'] = array(
- '
test',
- '
test',
- );
-
- $examples['20'] = array(
- '
test',
- '
test',
- );
-
- $examples['21'] = array(
- '
test',
- '
test',
- );
-
- $examples['22'] = array(
- '
test',
- '
test',
- );
-
- $examples['23'] = array(
- '
test',
- '
test',
- );
-
- $examples['24'] = array(
- '
test',
- '
test',
- );
-
- $examples['25'] = array(
- '
test',
- '
test',
- );
-
- $examples['Multiple unclosed tags treated as a single tag'] = array(
- '
-test',
- '
-test',
- );
-
- $examples['27'] = array(
- '
test',
- '
test',
- );
-
- $examples['28'] = array(
- '
test',
- '
test',
- );
-
- return $examples;
- }
-}
diff --git a/phpunit/html/wp-html-tag-processor-standalone-test.php b/phpunit/html/wp-html-tag-processor-test.php
similarity index 98%
rename from phpunit/html/wp-html-tag-processor-standalone-test.php
rename to phpunit/html/wp-html-tag-processor-test.php
index 8079db28f52be7..e66b1d50758d0b 100644
--- a/phpunit/html/wp-html-tag-processor-standalone-test.php
+++ b/phpunit/html/wp-html-tag-processor-test.php
@@ -1,30 +1,11 @@
Text';
const HTML_WITH_CLASSES = 'Text
';
const HTML_MALFORMED = 'Back to notifications
';
@@ -951,7 +932,7 @@ public function data_script_state() {
public function test_next_tag_ignores_the_contents_of_a_rcdata_tag( $rcdata_then_div, $rcdata_tag ) {
$p = new WP_HTML_Tag_Processor( $rcdata_then_div );
$p->next_tag();
- $this->assertSame( $rcdata_tag, $p->get_tag(), "The first found tag was not '$rcdata_tag'" );
+ $this->assertSame( strtoupper( $rcdata_tag ), $p->get_tag(), "The first found tag was not '$rcdata_tag'" );
$p->next_tag();
$this->assertSame( 'DIV', $p->get_tag(), "The second found tag was not 'div'" );
}
diff --git a/phpunit/html/wp-html-tag-processor-wp-test.php b/phpunit/html/wp-html-tag-processor-wp-test.php
deleted file mode 100644
index 41008800d0b751..00000000000000
--- a/phpunit/html/wp-html-tag-processor-wp-test.php
+++ /dev/null
@@ -1,91 +0,0 @@
-
- * $p = new WP_HTML_Tag_Processor( '' );
- * $p->next_tag();
- * $p->set_attribute('class', '" onclick="alert');
- * echo $p;
- * //
- *
- *
- * To prevent it, `set_attribute` calls `esc_attr()` on its given values.
- *
- *
- *
- *
- *
- * @ticket 56299
- *
- * @dataProvider data_set_attribute_escapable_values
- * @covers set_attribute
- */
- public function test_set_attribute_prevents_xss( $value_to_set, $expected_result ) {
- $p = new WP_HTML_Tag_Processor( '' );
- $p->next_tag();
- $p->set_attribute( 'test', $value_to_set );
-
- /*
- * Testing the escaping is hard using tools that properly parse
- * HTML because they might interpret the escaped values. It's hard
- * with tools that don't understand HTML because they might get
- * confused by improperly-escaped values.
- *
- * For this test, since we control the input HTML we're going to
- * do what looks like the opposite of what we want to be doing with
- * this library but are only doing so because we have full control
- * over the content and because we want to look at the raw values.
- */
- $match = null;
- preg_match( '~^$~', $p->get_updated_html(), $match );
- list( , $actual_value ) = $match;
-
- $this->assertEquals( $actual_value, '"' . $expected_result . '"' );
- }
-
- /**
- * Data provider with HTML attribute values that might need escaping.
- */
- public function data_set_attribute_escapable_values() {
- return array(
- array( '"', '"' ),
- array( '"', '"' ),
- array( '&', '&' ),
- array( '&', '&' ),
- array( '€', '€' ),
- array( "'", ''' ),
- array( '<>', '<>' ),
- array( '"";', '"";' ),
- array(
- '" onclick="alert(\'1\');">',
- '" onclick="alert('1');"><span onclick=""></span><script>alert("1")</script>',
- ),
- );
- }
-
-}