Skip to content

Commit 15f675d

Browse files
committed
Use a data provider over of repeat tests and steps
1 parent 886ad10 commit 15f675d

1 file changed

Lines changed: 56 additions & 96 deletions

File tree

tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php

Lines changed: 56 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -28,137 +28,97 @@ public function test_modifiable_text_special_textarea() {
2828
}
2929

3030
/**
31-
* PRE elements ignore the first newline in their content.
31+
* PRE and LISTING elements ignore the first newline in their content.
3232
* Setting the modifiable text with a leading newline should ensure that the leading newline
33-
* is present in the resulting TEXTAREA.
33+
* is present in the resulting element.
3434
*
3535
* @ticket 64607
36+
*
37+
* @dataProvider data_modifiable_text_special_pre_tags
38+
*
39+
* @param string $tag_name The tag name to test (e.g. 'pre', 'listing').
3640
*/
37-
public function test_modifiable_text_special_pre() {
41+
public function test_modifiable_text_special_pre_tags( string $tag_name ) {
3842
$set_text = "\nAFTER NEWLINE";
39-
$processor = WP_HTML_Processor::create_fragment( '<pre>REPLACEME<!--x--></pre>' );
43+
$processor = WP_HTML_Processor::create_fragment( "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>" );
4044
$processor->next_tag();
4145
$processor->next_token();
4246
$this->assertSame( '#text', $processor->get_token_type() );
4347
$processor->set_modifiable_text( $set_text );
4448
$this->assertSame( $set_text, $processor->get_modifiable_text() );
4549
$this->assertEqualHTML(
4650
<<<HTML
47-
<pre>
48-
{$set_text}<!--x--></pre>
51+
<{$tag_name}>
52+
{$set_text}<!--x--></{$tag_name}>
4953
HTML,
5054
$processor->get_updated_html(),
5155
'<body>',
52-
'Should have preserved the leading newline in the TEXTAREA content.'
56+
"Should have preserved the leading newline in the {$tag_name} content."
5357
);
5458
}
5559

5660
/**
61+
* Data provider.
5762
*
58-
* @ticket 64607
63+
* @return array[]
5964
*/
60-
public function test_modifiable_text_special_pre_leading_whitespace() {
61-
$set_text = "\nAFTER NEWLINE.";
62-
$processor = WP_HTML_Processor::create_fragment( "<pre>\nREPLACEME<!--x--></pre>" );
63-
$processor->next_tag();
64-
$processor->next_token();
65-
$this->assertSame( '#text', $processor->get_token_type() );
66-
// This is an empty text node because of how the HTML Processor works.
67-
$this->assertSame( '', $processor->get_modifiable_text() );
68-
$processor->set_modifiable_text( $set_text );
69-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
70-
$this->assertEqualHTML(
71-
<<<HTML
72-
<pre>
73-
{$set_text}REPLACEME<!--x--></pre>
74-
HTML,
75-
$processor->get_updated_html(),
76-
'<body>',
77-
'Should have preserved the leading newline in the TEXTAREA content.'
78-
);
79-
80-
$processor = WP_HTML_Processor::create_fragment( "<pre>\nREPLACEME<!--x--></pre>" );
81-
$processor->next_tag();
82-
$processor->next_token();
83-
$processor->next_token();
84-
$this->assertSame( '#text', $processor->get_token_type() );
85-
// This is an empty text node because of how the HTML Processor works.
86-
$this->assertSame( 'REPLACEME', $processor->get_modifiable_text() );
87-
$processor->set_modifiable_text( $set_text );
88-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
89-
$this->assertEqualHTML(
90-
<<<HTML
91-
<pre>
92-
{$set_text}<!--x--></pre>
93-
HTML,
94-
$processor->get_updated_html(),
95-
'<body>',
96-
'Should have preserved the leading newline in the TEXTAREA content.'
97-
);
98-
99-
$processor = WP_HTML_Processor::create_fragment( '<pre> REPLACEME<!--x--></pre>' );
100-
$processor->next_tag();
101-
$processor->next_token();
102-
$this->assertSame( '#text', $processor->get_token_type() );
103-
// This is an empty text node because of how the HTML Processor works.
104-
$this->assertSame( ' ', $processor->get_modifiable_text() );
105-
$processor->set_modifiable_text( $set_text );
106-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
107-
$this->assertEqualHTML(
108-
<<<HTML
109-
<pre>
110-
{$set_text}REPLACEME<!--x--></pre>
111-
HTML,
112-
$processor->get_updated_html(),
113-
'<body>',
114-
'Should have preserved the leading newline in the TEXTAREA content.'
115-
);
116-
117-
$processor = WP_HTML_Processor::create_fragment( '<pre> REPLACEME<!--x--></pre>' );
118-
$processor->next_tag();
119-
$processor->next_token();
120-
$processor->next_token();
121-
$this->assertSame( '#text', $processor->get_token_type() );
122-
// This is an empty text node because of how the HTML Processor works.
123-
$this->assertSame( 'REPLACEME', $processor->get_modifiable_text() );
124-
$processor->set_modifiable_text( $set_text );
125-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
126-
$this->assertEqualHTML(
127-
<<<HTML
128-
<pre>
129-
{$set_text}<!--x--></pre>
130-
HTML,
131-
$processor->get_updated_html(),
132-
'<body>',
133-
'Should have preserved the leading newline in the TEXTAREA content.'
65+
public static function data_modifiable_text_special_pre_tags() {
66+
return array(
67+
'PRE' => array( 'pre' ),
68+
'LISTING' => array( 'listing' ),
13469
);
13570
}
13671

13772
/**
138-
* LISTING elements ignore the first newline in their content.
139-
* Setting the modifiable text with a leading newline should ensure that the leading newline
140-
* is present in the resulting TEXTAREA.
141-
*
142-
* @todo Leading whitespace mage split into multiple text nodes. Add appropriate tests.
73+
* PRE and LISTING elements ignore the first newline in their content.
74+
* Leading whitespace may split into multiple text nodes in the HTML Processor.
75+
* Setting the modifiable text with a leading newline should ensure that the
76+
* leading newline is present in the resulting element.
14377
*
14478
* @ticket 64607
79+
*
80+
* @dataProvider data_modifiable_text_special_leading_whitespace
81+
*
82+
* @param string $html HTML containing the element to test.
83+
* @param int $advance_n_tokens Count of times to run `next_token()` after `next_tag()`.
84+
* @param string $initial_text Expected modifiable text before the update.
85+
* @param string $expected_html Expected HTML output after setting modifiable text.
14586
*/
146-
public function test_modifiable_text_special_listing() {
147-
$set_text = "\nAFTER NEWLINE";
148-
$processor = WP_HTML_Processor::create_fragment( '<listing>REPLACEME<!--x--></listing>' );
87+
public function test_modifiable_text_special_leading_whitespace( string $html, int $advance_n_tokens, string $initial_text, string $expected_html ) {
88+
$set_text = "\nAFTER NEWLINE.";
89+
$processor = WP_HTML_Processor::create_fragment( $html );
14990
$processor->next_tag();
150-
$processor->next_token();
91+
while ( --$advance_n_tokens >= 0 ) {
92+
$processor->next_token();
93+
}
15194
$this->assertSame( '#text', $processor->get_token_type() );
95+
$this->assertSame( $initial_text, $processor->get_modifiable_text() );
15296
$processor->set_modifiable_text( $set_text );
15397
$this->assertSame( $set_text, $processor->get_modifiable_text() );
15498
$this->assertEqualHTML(
155-
<<<HTML
156-
<listing>
157-
{$set_text}<!--x--></listing>
158-
HTML,
99+
$expected_html,
159100
$processor->get_updated_html(),
160101
'<body>',
161-
'Should have preserved the leading newline in the TEXTAREA content.'
102+
'Should have preserved the leading newline in the element content.'
162103
);
163104
}
105+
106+
/**
107+
* Data provider.
108+
*
109+
* @return Generator
110+
*/
111+
public static function data_modifiable_text_special_leading_whitespace() {
112+
$set_text = "\nAFTER NEWLINE.";
113+
114+
foreach ( self::data_modifiable_text_special_pre_tags() as $tag_data ) {
115+
$tag_name = $tag_data[0];
116+
$TAG = strtoupper( $tag_name );
117+
118+
yield "{$TAG} with leading newline, first text node" => array( "<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>", 1, '', "<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>" );
119+
yield "{$TAG} with leading newline, second text node" => array( "<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>", 2, 'REPLACEME', "<{$tag_name}>\n{$set_text}<!--x--></{$tag_name}>" );
120+
yield "{$TAG} with leading space, first text node" => array( "<{$tag_name}> REPLACEME<!--x--></{$tag_name}>", 1, ' ', "<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>" );
121+
yield "{$TAG} with leading space, second text node" => array( "<{$tag_name}> REPLACEME<!--x--></{$tag_name}>", 2, 'REPLACEME', "<{$tag_name}>\n {$set_text}<!--x--></{$tag_name}>" );
122+
}
123+
}
164124
}

0 commit comments

Comments
 (0)