From 3af665099fbc9a5e5db772d1b997a45e3eaf30ec Mon Sep 17 00:00:00 2001 From: Sarthak Jaiswal Date: Tue, 4 Feb 2025 13:40:20 +0530 Subject: [PATCH 1/5] Add missing test case for improving code coverage --- plugins/speculation-rules/load.php | 2 +- .../tests/test-speculation-rules-settings.php | 152 ++++++++++++++++++ .../test-speculation-rules-uninstall.php | 2 - 3 files changed, 153 insertions(+), 3 deletions(-) diff --git a/plugins/speculation-rules/load.php b/plugins/speculation-rules/load.php index 2d04073930..c49dd686e9 100644 --- a/plugins/speculation-rules/load.php +++ b/plugins/speculation-rules/load.php @@ -19,7 +19,6 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } -// @codeCoverageIgnoreEnd ( /** @@ -83,3 +82,4 @@ static function ( string $version ): void { require_once __DIR__ . '/settings.php'; } ); +// @codeCoverageIgnoreEnd diff --git a/plugins/speculation-rules/tests/test-speculation-rules-settings.php b/plugins/speculation-rules/tests/test-speculation-rules-settings.php index 5f10ed11e4..8d6ba882e0 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-settings.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-settings.php @@ -122,4 +122,156 @@ 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 ); + // 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. + */ + public function test_sanitize_setting(): void { + $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 { + 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. + ob_start(); + call_user_func( $wp_settings_sections['reading']['plsr_speculation_rules']['callback'] ); + $output = ob_get_clean(); + $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> Data for testing settings fields. + */ + public function settingsFieldProvider(): 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 settingsFieldProvider + * @param array $args Arguments for the settings field. + * @param array $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. + ob_start(); + plsr_render_settings_field( $args ); + $output = ob_get_clean(); + + // Check for the presence of form elements. + $this->assertStringContainsString( $name_check, $output ); + $this->assertStringContainsString( $value_check, $output ); + $this->assertStringContainsString( $description_check, $output ); + } } diff --git a/plugins/speculation-rules/tests/test-speculation-rules-uninstall.php b/plugins/speculation-rules/tests/test-speculation-rules-uninstall.php index 2628bf3cfa..c06c8b8ec1 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-uninstall.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-uninstall.php @@ -29,8 +29,6 @@ private function require_uninstall(): void { /** * Test option deletion. - * - * @covers ::plsr_delete_plugin_option */ public function test_delete_plugin_option(): void { unregister_setting( 'reading', 'plsr_speculation_rules' ); From 04a08ac78acb4bdab271012fb61770245d4dc96f Mon Sep 17 00:00:00 2001 From: Sarthak Jaiswal Date: Wed, 5 Feb 2025 19:13:43 +0530 Subject: [PATCH 2/5] Used get_echo() function to reduce code --- .../tests/test-speculation-rules-settings.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/plugins/speculation-rules/tests/test-speculation-rules-settings.php b/plugins/speculation-rules/tests/test-speculation-rules-settings.php index 8d6ba882e0..6b8ed8880d 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-settings.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-settings.php @@ -206,9 +206,7 @@ public function test_add_setting_ui(): void { $this->assertArrayHasKey( 'plsr_speculation_rules', $wp_settings_sections['reading'] ); // Check the output of the callback function for the section. - ob_start(); - call_user_func( $wp_settings_sections['reading']['plsr_speculation_rules']['callback'] ); - $output = ob_get_clean(); + $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 ); } @@ -265,9 +263,7 @@ public function test_render_settings_field( array $args, array $stored_settings, update_option( 'plsr_speculation_rules', $stored_settings ); // Capture the output of the settings field rendering. - ob_start(); - plsr_render_settings_field( $args ); - $output = ob_get_clean(); + $output = get_echo( 'plsr_render_settings_field', array( $args ) ); // Check for the presence of form elements. $this->assertStringContainsString( $name_check, $output ); From 66eddb70f71de00363102bd3e9acd19d235f5d8d Mon Sep 17 00:00:00 2001 From: Sarthak Jaiswal Date: Thu, 6 Feb 2025 01:59:40 +0530 Subject: [PATCH 3/5] Add @codeCoverageIgnore in uninstall.php --- .../tests/test-speculation-rules-settings.php | 4 ++-- plugins/speculation-rules/uninstall.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/speculation-rules/tests/test-speculation-rules-settings.php b/plugins/speculation-rules/tests/test-speculation-rules-settings.php index 6b8ed8880d..56798a807b 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-settings.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-settings.php @@ -215,7 +215,7 @@ public function test_add_setting_ui(): void { * * @return array> Data for testing settings fields. */ - public function settingsFieldProvider(): array { + public function data_provider_to_test_render_settings_field(): array { return array( 'mode' => array( array( @@ -251,7 +251,7 @@ public function settingsFieldProvider(): array { /** * Test rendering of settings fields using data provider. * - * @dataProvider settingsFieldProvider + * @dataProvider data_provider_to_test_render_settings_field * @param array $args Arguments for the settings field. * @param array $stored_settings Stored settings values. * @param string $name_check HTML name attribute check. diff --git a/plugins/speculation-rules/uninstall.php b/plugins/speculation-rules/uninstall.php index 6f93f106e7..375a166e9d 100644 --- a/plugins/speculation-rules/uninstall.php +++ b/plugins/speculation-rules/uninstall.php @@ -8,7 +8,7 @@ // If uninstall.php is not called by WordPress, bail. if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { - exit; + exit;// @codeCoverageIgnore } // 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). From 7519de29a49f610c91fd4a92f504dd9d3b2ca1e3 Mon Sep 17 00:00:00 2001 From: Sarthak Jaiswal Date: Tue, 11 Feb 2025 17:33:15 +0530 Subject: [PATCH 4/5] Code refactor and function rename --- plugins/speculation-rules/settings.php | 3 +- .../tests/test-speculation-rules-settings.php | 36 +++++++------------ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/plugins/speculation-rules/settings.php b/plugins/speculation-rules/settings.php index 54b16e0698..1a204cc149 100644 --- a/plugins/speculation-rules/settings.php +++ b/plugins/speculation-rules/settings.php @@ -224,7 +224,8 @@ function plsr_render_settings_field( array $args ): void { $choices = plsr_get_eagerness_labels(); break; default: - return; // Invalid (and this case should never occur). + // Invalid (and this case should never occur). + return; // @codeCoverageIgnore } $value = $option[ $args['field'] ]; diff --git a/plugins/speculation-rules/tests/test-speculation-rules-settings.php b/plugins/speculation-rules/tests/test-speculation-rules-settings.php index 56798a807b..fc48233a15 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-settings.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-settings.php @@ -9,6 +9,9 @@ class Test_Speculation_Rules_Settings extends WP_UnitTestCase { /** * @covers ::plsr_register_setting + * @covers ::plsr_get_mode_labels + * @covers ::plsr_get_eagerness_labels + * @covers ::plsr_get_setting_default */ public function test_plsr_register_setting(): void { unregister_setting( 'reading', 'plsr_speculation_rules' ); @@ -18,6 +21,14 @@ public function test_plsr_register_setting(): void { plsr_register_setting(); $settings = get_registered_settings(); $this->assertArrayHasKey( 'plsr_speculation_rules', $settings ); + + $settings = plsr_get_setting_default(); + $this->assertArrayHasKey( 'mode', $settings ); + $this->assertArrayHasKey( 'eagerness', $settings ); + + // Test default settings applied correctly. + $default_settings = plsr_get_setting_default(); + $this->assertEquals( $default_settings, get_option( 'plsr_speculation_rules' ) ); } /** @@ -123,21 +134,6 @@ public function test_plsr_add_settings_action_link(): void { ); } - /** - * @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 ); - // 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 */ @@ -167,13 +163,7 @@ public function test_get_stored_setting_value(): void { /** * Function to test sanitize_setting() with various inputs. */ - public function test_sanitize_setting(): void { - $input = array( - 'mode' => 'prerender', - 'eagerness' => 'eager', - ); - $sanitized = plsr_sanitize_setting( $input ); - $this->assertEquals( $input, $sanitized ); + public function test_plsr_sanitize_setting_with_invalid_inputs(): void { $input = array( 'mode' => 'invalid_mode', @@ -197,7 +187,7 @@ public function test_sanitize_setting(): void { /** * @covers ::plsr_add_setting_ui */ - public function test_add_setting_ui(): void { + public function test_plsr_add_setting_ui(): void { do_action( 'load-options-reading.php' );// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores // Check if the settings section has been added. From 8fd7597f8eda1e3fd003ac6cc47d859fe7459a06 Mon Sep 17 00:00:00 2001 From: Sarthak Jaiswal Date: Tue, 11 Feb 2025 17:39:19 +0530 Subject: [PATCH 5/5] Modified test function name --- .../speculation-rules/tests/test-speculation-rules-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/speculation-rules/tests/test-speculation-rules-settings.php b/plugins/speculation-rules/tests/test-speculation-rules-settings.php index fc48233a15..7bd603d180 100644 --- a/plugins/speculation-rules/tests/test-speculation-rules-settings.php +++ b/plugins/speculation-rules/tests/test-speculation-rules-settings.php @@ -248,7 +248,7 @@ public function data_provider_to_test_render_settings_field(): array { * @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 { + public function test_plsr_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 );