Skip to content

Commit 381c6e1

Browse files
authored
Merge pull request #1845 from sarthak-19/add/code_coverage_speculation_rules
Improve Test Coverage for Speculation Rules Plugin
2 parents e7cab2c + 66b31f2 commit 381c6e1

File tree

5 files changed

+142
-5
lines changed

5 files changed

+142
-5
lines changed

plugins/speculation-rules/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
if ( ! defined( 'ABSPATH' ) ) {
2020
exit; // Exit if accessed directly.
2121
}
22-
// @codeCoverageIgnoreEnd
2322

2423
(
2524
/**
@@ -81,3 +80,4 @@ static function ( string $version ): void {
8180
require_once __DIR__ . '/settings.php';
8281
}
8382
);
83+
// @codeCoverageIgnoreEnd

plugins/speculation-rules/settings.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ function plsr_render_settings_field( array $args ): void {
224224
$choices = plsr_get_eagerness_labels();
225225
break;
226226
default:
227-
return; // Invalid (and this case should never occur).
227+
// Invalid (and this case should never occur).
228+
return; // @codeCoverageIgnore
228229
}
229230

230231
$value = $option[ $args['field'] ];

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

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Test_Speculation_Rules_Settings extends WP_UnitTestCase {
99

1010
/**
1111
* @covers ::plsr_register_setting
12+
* @covers ::plsr_get_mode_labels
13+
* @covers ::plsr_get_eagerness_labels
14+
* @covers ::plsr_get_setting_default
1215
*/
1316
public function test_plsr_register_setting(): void {
1417
unregister_setting( 'reading', 'plsr_speculation_rules' );
@@ -18,6 +21,14 @@ public function test_plsr_register_setting(): void {
1821
plsr_register_setting();
1922
$settings = get_registered_settings();
2023
$this->assertArrayHasKey( 'plsr_speculation_rules', $settings );
24+
25+
$settings = plsr_get_setting_default();
26+
$this->assertArrayHasKey( 'mode', $settings );
27+
$this->assertArrayHasKey( 'eagerness', $settings );
28+
29+
// Test default settings applied correctly.
30+
$default_settings = plsr_get_setting_default();
31+
$this->assertEquals( $default_settings, get_option( 'plsr_speculation_rules' ) );
2132
}
2233

2334
/**
@@ -122,4 +133,131 @@ public function test_plsr_add_settings_action_link(): void {
122133
plsr_add_settings_action_link( $default_action_links )
123134
);
124135
}
136+
137+
/**
138+
* @covers ::plsr_get_stored_setting_value
139+
*/
140+
public function test_get_stored_setting_value(): void {
141+
update_option(
142+
'plsr_speculation_rules',
143+
array(
144+
'mode' => 'prefetch',
145+
'eagerness' => 'moderate',
146+
)
147+
);
148+
$settings = plsr_get_stored_setting_value();
149+
$this->assertEquals(
150+
array(
151+
'mode' => 'prefetch',
152+
'eagerness' => 'moderate',
153+
),
154+
$settings
155+
);
156+
157+
// Test default when no option is set.
158+
delete_option( 'plsr_speculation_rules' );
159+
$settings = plsr_get_stored_setting_value();
160+
$this->assertEquals( plsr_get_setting_default(), $settings );
161+
}
162+
163+
/**
164+
* Function to test sanitize_setting() with various inputs.
165+
*/
166+
public function test_plsr_sanitize_setting_with_invalid_inputs(): void {
167+
168+
$input = array(
169+
'mode' => 'invalid_mode',
170+
'eagerness' => 'conservative',
171+
);
172+
$sanitized = plsr_sanitize_setting( $input );
173+
$this->assertEquals( 'prerender', $sanitized['mode'] );
174+
175+
$input = array(
176+
'mode' => 'prefetch',
177+
'eagerness' => 'invalid_eagerness',
178+
);
179+
$sanitized = plsr_sanitize_setting( $input );
180+
$this->assertEquals( 'moderate', $sanitized['eagerness'] );
181+
182+
$input = 'invalid_input';
183+
$sanitized = plsr_sanitize_setting( $input );
184+
$this->assertEquals( plsr_get_setting_default(), $sanitized );
185+
}
186+
187+
/**
188+
* @covers ::plsr_add_setting_ui
189+
*/
190+
public function test_plsr_add_setting_ui(): void {
191+
do_action( 'load-options-reading.php' );// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
192+
193+
// Check if the settings section has been added.
194+
global $wp_settings_sections;
195+
$this->assertArrayHasKey( 'reading', $wp_settings_sections );
196+
$this->assertArrayHasKey( 'plsr_speculation_rules', $wp_settings_sections['reading'] );
197+
198+
// Check the output of the callback function for the section.
199+
$output = get_echo( $wp_settings_sections['reading']['plsr_speculation_rules']['callback'] );
200+
$this->assertStringContainsString( 'This section allows you to control how URLs that your users navigate to are speculatively loaded to improve performance.', $output );
201+
}
202+
203+
/**
204+
* Data provider for testing plsr_render_settings_field.
205+
*
206+
* @return array<string, array<mixed>> Data for testing settings fields.
207+
*/
208+
public function data_provider_to_test_render_settings_field(): array {
209+
return array(
210+
'mode' => array(
211+
array(
212+
'field' => 'mode',
213+
'title' => 'Speculation Mode',
214+
'description' => 'Prerendering will lead to faster load times than prefetching.',
215+
),
216+
array(
217+
'mode' => 'prefetch',
218+
'eagerness' => 'moderate',
219+
),
220+
'name="plsr_speculation_rules[mode]"',
221+
'value="prefetch"',
222+
'Prerendering will lead to faster load times than prefetching.',
223+
),
224+
'eagerness' => array(
225+
array(
226+
'field' => 'eagerness',
227+
'title' => 'Eagerness',
228+
'description' => 'The eagerness setting defines the heuristics based on which the loading is triggered.',
229+
),
230+
array(
231+
'mode' => 'prefetch',
232+
'eagerness' => 'moderate',
233+
),
234+
'name="plsr_speculation_rules[eagerness]"',
235+
'value="moderate"',
236+
'The eagerness setting defines the heuristics based on which the loading is triggered.',
237+
),
238+
);
239+
}
240+
241+
/**
242+
* Test rendering of settings fields using data provider.
243+
*
244+
* @dataProvider data_provider_to_test_render_settings_field
245+
* @param array<mixed> $args Arguments for the settings field.
246+
* @param array<mixed> $stored_settings Stored settings values.
247+
* @param string $name_check HTML name attribute check.
248+
* @param string $value_check HTML value attribute check.
249+
* @param string $description_check Description check.
250+
*/
251+
public function test_plsr_render_settings_field( array $args, array $stored_settings, string $name_check, string $value_check, string $description_check ): void {
252+
// Simulate getting stored settings.
253+
update_option( 'plsr_speculation_rules', $stored_settings );
254+
255+
// Capture the output of the settings field rendering.
256+
$output = get_echo( 'plsr_render_settings_field', array( $args ) );
257+
258+
// Check for the presence of form elements.
259+
$this->assertStringContainsString( $name_check, $output );
260+
$this->assertStringContainsString( $value_check, $output );
261+
$this->assertStringContainsString( $description_check, $output );
262+
}
125263
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ private function require_uninstall(): void {
2929

3030
/**
3131
* Test option deletion.
32-
*
33-
* @covers ::plsr_delete_plugin_option
3432
*/
3533
public function test_delete_plugin_option(): void {
3634
unregister_setting( 'reading', 'plsr_speculation_rules' );

plugins/speculation-rules/uninstall.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// If uninstall.php is not called by WordPress, bail.
1010
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
11-
exit;
11+
exit;// @codeCoverageIgnore
1212
}
1313

1414
// For a multisite, delete the option for all sites (however limited to 100 sites to avoid memory limit or timeout problems in large scale networks).

0 commit comments

Comments
 (0)