Skip to content

Commit 3af6650

Browse files
Sarthak JaiswalSarthak Jaiswal
authored andcommitted
Add missing test case for improving code coverage
1 parent d94977f commit 3af6650

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
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
/**
@@ -83,3 +82,4 @@ static function ( string $version ): void {
8382
require_once __DIR__ . '/settings.php';
8483
}
8584
);
85+
// @codeCoverageIgnoreEnd

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

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

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' );

0 commit comments

Comments
 (0)