-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Test Coverage for Speculation Rules Plugin #1845
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -122,4 +122,152 @@ public function test_plsr_add_settings_action_link(): void { | |||||||||
plsr_add_settings_action_link( $default_action_links ) | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* @covers ::plsr_get_mode_labels | ||||||||||
* @covers ::plsr_get_eagerness_labels | ||||||||||
* @covers ::plsr_get_setting_default | ||||||||||
* @covers ::plsr_register_setting | ||||||||||
*/ | ||||||||||
public function test_register_settings(): void { | ||||||||||
plsr_register_setting(); | ||||||||||
$settings = plsr_get_setting_default(); | ||||||||||
$this->assertArrayHasKey( 'mode', $settings ); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we check this, we should probably also check for the
Suggested change
|
||||||||||
// Test default settings applied correctly. | ||||||||||
$default_settings = plsr_get_setting_default(); | ||||||||||
$this->assertEquals( $default_settings, get_option( 'plsr_speculation_rules' ) ); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* @covers ::plsr_get_stored_setting_value | ||||||||||
*/ | ||||||||||
public function test_get_stored_setting_value(): void { | ||||||||||
update_option( | ||||||||||
'plsr_speculation_rules', | ||||||||||
array( | ||||||||||
'mode' => 'prefetch', | ||||||||||
'eagerness' => 'moderate', | ||||||||||
) | ||||||||||
); | ||||||||||
$settings = plsr_get_stored_setting_value(); | ||||||||||
$this->assertEquals( | ||||||||||
array( | ||||||||||
'mode' => 'prefetch', | ||||||||||
'eagerness' => 'moderate', | ||||||||||
), | ||||||||||
$settings | ||||||||||
); | ||||||||||
|
||||||||||
// Test default when no option is set. | ||||||||||
delete_option( 'plsr_speculation_rules' ); | ||||||||||
$settings = plsr_get_stored_setting_value(); | ||||||||||
$this->assertEquals( plsr_get_setting_default(), $settings ); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Function to test sanitize_setting() with various inputs. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I kept the
![]() ![]()
![]() ![]() cc : @westonruter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange. I don't know why that would be. Nevertheless, I see that the |
||||||||||
*/ | ||||||||||
public function test_sanitize_setting(): void { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above:
Suggested change
|
||||||||||
$input = array( | ||||||||||
'mode' => 'prerender', | ||||||||||
'eagerness' => 'eager', | ||||||||||
); | ||||||||||
$sanitized = plsr_sanitize_setting( $input ); | ||||||||||
$this->assertEquals( $input, $sanitized ); | ||||||||||
|
||||||||||
$input = array( | ||||||||||
'mode' => 'invalid_mode', | ||||||||||
'eagerness' => 'conservative', | ||||||||||
); | ||||||||||
$sanitized = plsr_sanitize_setting( $input ); | ||||||||||
$this->assertEquals( 'prerender', $sanitized['mode'] ); | ||||||||||
|
||||||||||
$input = array( | ||||||||||
'mode' => 'prefetch', | ||||||||||
'eagerness' => 'invalid_eagerness', | ||||||||||
); | ||||||||||
$sanitized = plsr_sanitize_setting( $input ); | ||||||||||
$this->assertEquals( 'moderate', $sanitized['eagerness'] ); | ||||||||||
|
||||||||||
$input = 'invalid_input'; | ||||||||||
$sanitized = plsr_sanitize_setting( $input ); | ||||||||||
$this->assertEquals( plsr_get_setting_default(), $sanitized ); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* @covers ::plsr_add_setting_ui | ||||||||||
*/ | ||||||||||
public function test_add_setting_ui(): void { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
do_action( 'load-options-reading.php' );// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores | ||||||||||
|
||||||||||
// Check if the settings section has been added. | ||||||||||
global $wp_settings_sections; | ||||||||||
$this->assertArrayHasKey( 'reading', $wp_settings_sections ); | ||||||||||
$this->assertArrayHasKey( 'plsr_speculation_rules', $wp_settings_sections['reading'] ); | ||||||||||
|
||||||||||
// Check the output of the callback function for the section. | ||||||||||
$output = get_echo( $wp_settings_sections['reading']['plsr_speculation_rules']['callback'] ); | ||||||||||
$this->assertStringContainsString( 'This section allows you to control how URLs that your users navigate to are speculatively loaded to improve performance.', $output ); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Data provider for testing plsr_render_settings_field. | ||||||||||
* | ||||||||||
* @return array<string, array<mixed>> Data for testing settings fields. | ||||||||||
*/ | ||||||||||
public function data_provider_to_test_render_settings_field(): array { | ||||||||||
return array( | ||||||||||
'mode' => array( | ||||||||||
array( | ||||||||||
'field' => 'mode', | ||||||||||
'title' => 'Speculation Mode', | ||||||||||
'description' => 'Prerendering will lead to faster load times than prefetching.', | ||||||||||
), | ||||||||||
array( | ||||||||||
'mode' => 'prefetch', | ||||||||||
'eagerness' => 'moderate', | ||||||||||
), | ||||||||||
'name="plsr_speculation_rules[mode]"', | ||||||||||
'value="prefetch"', | ||||||||||
'Prerendering will lead to faster load times than prefetching.', | ||||||||||
), | ||||||||||
'eagerness' => array( | ||||||||||
array( | ||||||||||
'field' => 'eagerness', | ||||||||||
'title' => 'Eagerness', | ||||||||||
'description' => 'The eagerness setting defines the heuristics based on which the loading is triggered.', | ||||||||||
), | ||||||||||
array( | ||||||||||
'mode' => 'prefetch', | ||||||||||
'eagerness' => 'moderate', | ||||||||||
), | ||||||||||
'name="plsr_speculation_rules[eagerness]"', | ||||||||||
'value="moderate"', | ||||||||||
'The eagerness setting defines the heuristics based on which the loading is triggered.', | ||||||||||
), | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Test rendering of settings fields using data provider. | ||||||||||
* | ||||||||||
* @dataProvider data_provider_to_test_render_settings_field | ||||||||||
* @param array<mixed> $args Arguments for the settings field. | ||||||||||
* @param array<mixed> $stored_settings Stored settings values. | ||||||||||
* @param string $name_check HTML name attribute check. | ||||||||||
* @param string $value_check HTML value attribute check. | ||||||||||
* @param string $description_check Description check. | ||||||||||
*/ | ||||||||||
public function test_render_settings_field( array $args, array $stored_settings, string $name_check, string $value_check, string $description_check ): void { | ||||||||||
// Simulate getting stored settings. | ||||||||||
update_option( 'plsr_speculation_rules', $stored_settings ); | ||||||||||
|
||||||||||
// Capture the output of the settings field rendering. | ||||||||||
$output = get_echo( 'plsr_render_settings_field', array( $args ) ); | ||||||||||
|
||||||||||
// Check for the presence of form elements. | ||||||||||
$this->assertStringContainsString( $name_check, $output ); | ||||||||||
$this->assertStringContainsString( $value_check, $output ); | ||||||||||
$this->assertStringContainsString( $description_check, $output ); | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,6 @@ private function require_uninstall(): void { | |
|
||
/** | ||
* Test option deletion. | ||
* | ||
* @covers ::plsr_delete_plugin_option | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this? The function is located in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned here #1845 (comment) Without adding
|
||
*/ | ||
public function test_delete_plugin_option(): void { | ||
unregister_setting( 'reading', 'plsr_speculation_rules' ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think better name this
test_plsr_register_setting
in accordance with the primary method it tests.