Skip to content

Commit 935aafa

Browse files
committed
Improve selector integration tests
1 parent 24a5bb8 commit 935aafa

File tree

2 files changed

+72
-73
lines changed

2 files changed

+72
-73
lines changed

tests/phpunit/tests/html-api/wpHtmlProcessor-select.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,60 @@ public function test_select_miss() {
2626
*
2727
* @dataProvider data_selectors
2828
*/
29-
public function test_select( string $html, string $selector ) {
29+
public function test_select_all( string $html, string $selector, int $match_count ) {
3030
$processor = WP_HTML_Processor::create_full_parser( $html );
31-
$this->assertTrue( $processor->select( $selector ) );
32-
$this->assertTrue( $processor->get_attribute( 'match' ) );
31+
$count = 0;
32+
foreach ( $processor->select_all( $selector ) as $_ ) {
33+
$breadcrumb_string = implode( ', ', $processor->get_breadcrumbs() );
34+
$this->assertTrue(
35+
$processor->get_attribute( 'match' ),
36+
"Matched unexpected tag {$processor->get_tag()} @ {$breadcrumb_string}"
37+
);
38+
++$count;
39+
}
40+
$this->assertSame( $match_count, $count, 'Did not match expected number of tags.' );
3341
}
3442

3543
/**
3644
* Data provider.
3745
*
46+
* Most selectors are covered by the tag processor selector tests.
47+
* This suite should focus on complex selectors.
48+
*
3849
* @return array
3950
*/
4051
public static function data_selectors(): array {
4152
return array(
42-
'simple type' => array( '<div match>', 'div' ),
43-
'any type' => array( '<html match>', '*' ),
44-
'simple class' => array( '<div class="x" match>', '.x' ),
45-
'simple id' => array( '<div id="x" match>', '#x' ),
46-
'simple attribute' => array( '<div att match>', '[att]' ),
47-
'attribute value' => array( '<div att="val" match>', '[att=val]' ),
48-
'attribute quoted value' => array( '<div att="::" match>', '[att="::"]' ),
49-
'complex any descendant' => array( '<section><div match>', 'section *' ),
50-
'complex any child' => array( '<section><div match>', 'section > *' ),
51-
52-
'list' => array( '<div><p match>', 'a, p' ),
53-
'compound' => array( '<div att><section att="foo bar" match>', 'section[att~="bar"]' ),
53+
'any descendant' => array( '<section><p match><i match><em match><p match>', 'section *', 4 ),
54+
'any child 1' => array( '<section><p match><i><em><p match>', 'section > *', 2 ),
55+
'any child 2' => array( '<div><section match><div>', 'div > *', 1 ),
5456
);
5557
}
5658

5759
/**
5860
* @ticket TBD
61+
*
62+
* @expectedIncorrectUsage WP_HTML_Processor::select_all
63+
*
64+
* @dataProvider data_invalid_selectors
5965
*/
60-
public function test_select_all() {
61-
$processor = WP_HTML_Processor::create_full_parser( '<div match><p class="x" match><svg><rect match/></svg><i id="y" match></i>' );
62-
$count = 0;
63-
foreach ( $processor->select_all( 'div, .x, svg>rect, #y' ) as $_ ) {
64-
++$count;
65-
$this->assertTrue( $processor->get_attribute( 'match' ) );
66-
}
67-
$this->assertSame( 4, $count );
66+
public function test_invalid_selector( string $selector ) {
67+
$processor = WP_HTML_Processor::create_fragment( 'irrelevant' );
68+
$this->assertFalse( $processor->select( $selector ) );
6869
}
6970

7071
/**
71-
* @ticket TBD
72+
* Data provider.
7273
*
73-
* @expectedIncorrectUsage WP_HTML_Processor::select_all
74+
* @return array
7475
*/
75-
public function test_invalid_selector() {
76-
$processor = WP_HTML_Processor::create_fragment( 'irrelevant' );
77-
$this->assertFalse( $processor->select( '[invalid!selector]' ) );
76+
public static function data_invalid_selectors(): array {
77+
return array(
78+
'invalid selector' => array( '[invalid!selector]' ),
79+
80+
// The class selectors below are not allowed in non-final position.
81+
'unsupported child selector' => array( '.parent > .child' ),
82+
'unsupported descendant selector' => array( '.ancestor .descendant' ),
83+
);
7884
}
7985
}

tests/phpunit/tests/html-api/wpHtmlTagProcessor-select.php

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ public function test_select_miss() {
2626
*
2727
* @dataProvider data_selectors
2828
*/
29-
public function test_select( string $html, string $selector ) {
29+
public function test_select( string $html, string $selector, int $match_count ) {
3030
$processor = new WP_HTML_Tag_Processor( $html );
31-
$this->assertTrue( $processor->select( $selector ) );
32-
$this->assertTrue( $processor->get_attribute( 'match' ) );
31+
$count = 0;
32+
foreach ( $processor->select_all( $selector ) as $_ ) {
33+
$this->assertTrue(
34+
$processor->get_attribute( 'match' ),
35+
"Matched unexpected tag {$processor->get_tag()}"
36+
);
37+
++$count;
38+
}
39+
$this->assertSame( $match_count, $count, 'Did not match expected number of tags.' );
3340
}
3441

3542
/**
@@ -39,58 +46,44 @@ public function test_select( string $html, string $selector ) {
3946
*/
4047
public static function data_selectors(): array {
4148
return array(
42-
'simple type' => array( '<p><div match>', 'div' ),
43-
'any type' => array( '<div match>', '*' ),
44-
'simple class' => array( '<div><div class="x" match>', '.x' ),
45-
'simple id' => array( '<div><div id="x" match>', '#x' ),
46-
'boolean attribute' => array( '<div tta><div att match>', '[att]' ),
47-
'boolean attribute with string match' => array( '<div tta><div att match>', '[att=""]' ),
49+
'simple type' => array( '<div match><div match><span>', 'div', 2 ),
50+
'any type' => array( '<div match><span match>', '*', 2 ),
51+
'simple class' => array( '<div><div class="x" match><span><span class="x" match>', '.x', 2 ),
52+
'simple id' => array( '<div><div id="x" match><span id="x" match>', '#x', 2 ),
4853

49-
'attribute value' => array( '<div att="v"><div att="val" match>', '[att=val]' ),
50-
'attribute quoted value' => array( '<div att><div att="::" match>', '[att="::"]' ),
51-
'attribute case insensitive' => array( '<div att="value"><div att="val" match>', '[att="VAL"i]' ),
52-
'attribute case sensitive mod' => array( '<div att="VAL"><div att="val" match>', '[att="val"s]' ),
54+
'attribute presence' => array( '<div tta><div att match><span att="foo" match>', '[att]', 2 ),
55+
'attribute empty string match' => array( '<div tta><div att match><span match att=>', '[att=""]', 2 ),
56+
'attribute value' => array( '<div att="v"><div att="val" match><p match att=val>', '[att=val]', 2 ),
57+
'attribute quoted value' => array( '<div att><div att="::" match><p att=\'::\' match>', '[att="::"]', 2 ),
58+
'attribute case insensitive' => array( '<div att="value"><div att="val" match><p match att=VaL>', '[att="VAL"i]', 2 ),
59+
'attribute case sensitive mod' => array( '<div att="VAL"><div att="val" match><p att=val match>', '[att="val"s]', 2 ),
5360

54-
'attribute one of' => array( '<div att="a B c"><div att="a b c" match>', '[att~="b"]' ),
55-
'attribute one of insensitive' => array( '<div att="a c"><div att="a B c" match>', '[att~="b"i]' ),
56-
'attribute one of mod sensitive' => array( '<div att="a B c"><div att="a b c" match>', '[att~="b"s]' ),
57-
'attribute one of whitespace cases' => array( "<div att=' a'><div att='\na\ta b ' match>", '[att~="b"]' ),
61+
'attribute one of' => array( '<div att="a B c"><div att="a b c" match><p att="b" match><p att="abc"><p att="abcdef b " match>', '[att~="b"]', 3 ),
62+
'attribute one of insensitive' => array( '<div att="a c"><div att="a B c" match>', '[att~="b"i]', 1 ),
63+
'attribute one of mod sensitive' => array( '<div att="a B c"><div att="a b c" match>', '[att~="b"s]', 1 ),
64+
'attribute one of whitespace cases' => array( "<div att=' a'><div att='\na\ta b ' match>", '[att~="b"]', 1 ),
5865

59-
'attribute with-hyphen (no hyphen)' => array( '<p att="special_not"><p att="special" match>', '[att|="special"]' ),
60-
'attribute with-hyphen (hyphen prefix)' => array( '<p att="special_not"><p att="special-yeah" match>', '[att|="special"]' ),
61-
'attribute with-hyphen insensitive' => array( '<p att="special_not"><p att="SPECIAL" match>', '[att|="special"i]' ),
62-
'attribute with-hyphen sensitive mod' => array( '<p att="spec"><p att="special" match>', '[att|="special"s]' ),
66+
'attribute with-hyphen' => array( '<p att="special_not"><p att="special" match><p att="special-great" match>', '[att|="special"]', 2 ),
67+
'attribute with-hyphen insensitive' => array( '<p att="special_not"><p att="SPECIAL" match><p att="SPECIAL-great" match>', '[att|="special" i]', 2 ),
68+
'attribute with-hyphen sensitive mod' => array( '<p att="SPECIAL"><p att="special" match>', '[att|="special"s]', 1 ),
6369

64-
'attribute prefixed' => array( '<p att="fix"><p att="prefix" match>', '[att^="p"]' ),
65-
'attribute prefixed insensitive' => array( '<p att="fix"><p att="Prefix" match>', '[att^="p"i]' ),
66-
'attribute prefixed sensitive mod' => array( '<p att="Prefix"><p att="prefix" match>', '[att^="p"s]' ),
70+
'attribute prefixed' => array( '<p att="notprefix"><p att="prefix" match><p att="perfect" match>', '[att^="p"]', 2 ),
71+
'attribute prefixed insensitive' => array( '<p att="notprefix"><p att="Prefix" match>', '[att^="p"i]', 1 ),
72+
'attribute prefixed sensitive mod' => array( '<p att="Prefix"><p att="prefix" match>', '[att^="p"s]', 1 ),
6773

68-
'attribute suffixed' => array( '<p att="suffix_"><p att="suffix" match>', '[att$="x"]' ),
69-
'attribute suffixed insensitive' => array( '<p att="suffix_"><p att="suffiX" match>', '[att$="x"i]' ),
70-
'attribute suffixed sensitive mod' => array( '<p att="suffiX"><p att="suffix" match>', '[att$="x"s]' ),
74+
'attribute suffixed' => array( '<p att="suffix_"><p att="suffix" match><p att="brilliant…x" match>', '[att$="x"]', 2 ),
75+
'attribute suffixed insensitive' => array( '<p att="suffix_"><p att="suffiX" match>', '[att$="x"i]', 1 ),
76+
'attribute suffixed sensitive mod' => array( '<p att="suffiX"><p att="suffix" match>', '[att$="x"s]', 1 ),
7177

72-
'attribute contains' => array( '<p att="abcyz"><p att="abcxyz" match>', '[att*="x"]' ),
73-
'attribute contains insensitive' => array( '<p att="abcyz"><p att="abcXyz" match>', '[att*="x"i]' ),
74-
'attribute contains sensitive mod' => array( '<p att="abcXyz"><p att="abcxyz" match>', '[att*="x"s]' ),
78+
'attribute contains' => array( '<p att="abcyz"><p att="abcxyz" match><p att="x" match>', '[att*="x"]', 2 ),
79+
'attribute contains insensitive' => array( '<p att="abcyz"><p att="abcXyz" match>', '[att*="x"i]', 1 ),
80+
'attribute contains sensitive mod' => array( '<p att="abcXyz"><p att="abcxyz" match>', '[att*="x"s]', 1 ),
7581

76-
'list' => array( '<div><p match>', 'a, p' ),
77-
'compound' => array( '<div att><section att="bar" match>', 'section[att="bar"]' ),
82+
'list' => array( '<div><p match><a match><span>', 'a, p, .class, #id, [att]', 2 ),
83+
'compound' => array( '<div att><custom-el att="bar" fruit="APPLE BANANA" match>', 'custom-el[att="bar"][ fruit ~= "banana" i]', 1 ),
7884
);
7985
}
8086

81-
/**
82-
* @ticket TBD
83-
*/
84-
public function test_select_all() {
85-
$processor = new WP_HTML_Tag_Processor( '<div match><p class="x" match><svg><rect match/></svg><i id="y" match></i>' );
86-
$count = 0;
87-
foreach ( $processor->select_all( 'div, .x, rect, #y' ) as $_ ) {
88-
++$count;
89-
$this->assertTrue( $processor->get_attribute( 'match' ) );
90-
}
91-
$this->assertSame( 4, $count );
92-
}
93-
9487
/**
9588
* @ticket TBD
9689
*

0 commit comments

Comments
 (0)