Skip to content

Commit 9649da1

Browse files
committed
Refactor test_plsr_render_settings_field()
1 parent ed6ea73 commit 9649da1

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

plugins/speculation-rules/tests/test-speculation-rules-settings.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -269,33 +269,23 @@ public function test_plsr_add_setting_ui(): void {
269269
*/
270270
public function data_provider_to_test_render_settings_field(): array {
271271
return array(
272-
'mode' => array(
273-
array(
274-
'field' => 'mode',
275-
'title' => 'Speculation Mode',
276-
'description' => 'Prerendering will lead to faster load times than prefetching.',
277-
),
278-
array(
279-
'mode' => 'prefetch',
280-
'eagerness' => 'moderate',
281-
),
282-
'name="plsr_speculation_rules[mode]"',
283-
'value="prefetch"',
284-
'Prerendering will lead to faster load times than prefetching.',
272+
'mode' => array(
273+
'field' => 'mode',
274+
'value' => 'prefetch',
275+
'title' => 'Speculation Mode',
276+
'description' => 'The mode description',
285277
),
286-
'eagerness' => array(
287-
array(
288-
'field' => 'eagerness',
289-
'title' => 'Eagerness',
290-
'description' => 'The eagerness setting defines the heuristics based on which the loading is triggered.',
291-
),
292-
array(
293-
'mode' => 'prefetch',
294-
'eagerness' => 'moderate',
295-
),
296-
'name="plsr_speculation_rules[eagerness]"',
297-
'value="moderate"',
298-
'The eagerness setting defines the heuristics based on which the loading is triggered.',
278+
'eagerness' => array(
279+
'field' => 'eagerness',
280+
'value' => 'moderate',
281+
'title' => 'Eagerness',
282+
'description' => 'The eagerness description',
283+
),
284+
'authentication' => array(
285+
'field' => 'authentication',
286+
'value' => 'any',
287+
'title' => 'Authentication',
288+
'description' => 'The authentication description.',
299289
),
300290
);
301291
}
@@ -304,22 +294,34 @@ public function data_provider_to_test_render_settings_field(): array {
304294
* Test rendering of settings fields using data provider.
305295
*
306296
* @dataProvider data_provider_to_test_render_settings_field
307-
* @param array<mixed> $args Arguments for the settings field.
308-
* @param array<mixed> $stored_settings Stored settings values.
309-
* @param string $name_check HTML name attribute check.
310-
* @param string $value_check HTML value attribute check.
311-
* @param string $description_check Description check.
297+
*
298+
* @param string $field Field.
299+
* @param string $value Value.
300+
* @param string $title Title.
301+
* @param string $description Description.
312302
*/
313-
public function test_plsr_render_settings_field( array $args, array $stored_settings, string $name_check, string $value_check, string $description_check ): void {
303+
public function test_plsr_render_settings_field( string $field, string $value, string $title, string $description ): void {
314304
// Simulate getting stored settings.
315-
update_option( 'plsr_speculation_rules', $stored_settings );
305+
update_option( 'plsr_speculation_rules', array( $field => $value ) );
316306

317307
// Capture the output of the settings field rendering.
318-
$output = get_echo( 'plsr_render_settings_field', array( $args ) );
308+
$output = get_echo( 'plsr_render_settings_field', array( compact( 'field', 'title', 'description' ) ) );
319309

320310
// Check for the presence of form elements.
321-
$this->assertStringContainsString( $name_check, $output );
322-
$this->assertStringContainsString( $value_check, $output );
323-
$this->assertStringContainsString( $description_check, $output );
311+
$this->assertStringContainsString( $description, $output );
312+
313+
$p = new WP_HTML_Tag_Processor( $output );
314+
$found = false;
315+
while ( $p->next_tag( array( 'tag_name' => 'INPUT' ) ) ) {
316+
if (
317+
$p->get_attribute( 'name' ) === sprintf( 'plsr_speculation_rules[%s]', $field )
318+
&&
319+
$p->get_attribute( 'value' ) === $value
320+
) {
321+
$found = null !== $p->get_attribute( 'checked' );
322+
break;
323+
}
324+
}
325+
$this->assertTrue( $found, $output );
324326
}
325327
}

0 commit comments

Comments
 (0)