From 5dad256f880af84227ed5588527e037f0ff74228 Mon Sep 17 00:00:00 2001 From: Colin Stewart <79332690+costdev@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:07:23 +0000 Subject: [PATCH] Add tests for `Admin_Settings::register_settings()`. (#241) --- .../AdminSettings_RegisterSettingsTest.php | 196 ++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 tests/phpunit/tests/AdminSettings/AdminSettings_RegisterSettingsTest.php diff --git a/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterSettingsTest.php b/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterSettingsTest.php new file mode 100644 index 0000000..7902a00 --- /dev/null +++ b/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterSettingsTest.php @@ -0,0 +1,196 @@ +register_settings(); + + $this->assertIsArray( $wp_registered_settings ); + $this->assertArrayHasKey( self::$option_name, $wp_registered_settings ); + } + + /** + * Test that the expected settings section is registered. + * + * @dataProvider data_settings_sections + * + * @param string $section_id The ID of the settings section. + */ + public function test_should_register_settings_section( $section_id ) { + global $wp_settings_sections; + + $admin_settings = new AspireUpdate\Admin_Settings(); + $admin_settings->register_settings(); + + $this->assertIsArray( + $wp_settings_sections, + 'There are no settings sections.' + ); + + $this->assertArrayHasKey( + self::$options_page, + $wp_settings_sections, + 'There is no entry for the plugin in settings sections.' + ); + + $this->assertIsArray( + $wp_settings_sections[ self::$options_page ], + 'There are no settings sections registered for the plugin.' + ); + + $this->assertArrayHasKey( + $section_id, + $wp_settings_sections[ self::$options_page ], + "Section {$section_id} is not registered." + ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_settings_sections() { + return self::text_array_to_dataprovider( + [ + 'aspireupdate_settings_section', + 'aspireupdate_debug_settings_section', + ] + ); + } + + /** + * Test that the expected settings field is registered. + * + * @dataProvider data_settings_fields + * + * @param string $section_id The ID of the settings section. + * @param string $field_id The ID of the settings field. + */ + public function test_should_register_settings_field( $section_id, $field_id ) { + global $wp_settings_fields; + + $admin_settings = new AspireUpdate\Admin_Settings(); + $admin_settings->register_settings(); + + $this->assertIsArray( + $wp_settings_fields, + 'There are no settings sections.' + ); + + $this->assertArrayHasKey( + self::$options_page, + $wp_settings_fields, + 'There is no entry for the plugin in settings sections.' + ); + + $this->assertIsArray( + $wp_settings_fields[ self::$options_page ], + 'There are no settings sections registered for the plugin.' + ); + + $this->assertArrayHasKey( + $section_id, + $wp_settings_fields[ self::$options_page ], + "Section {$section_id} is not registered." + ); + + $this->assertIsArray( + $wp_settings_fields[ self::$options_page ], + "There are no fields registered for Section {$section_id}." + ); + + $this->assertArrayHasKey( + $field_id, + $wp_settings_fields[ self::$options_page ][ $section_id ], + "Field {$field_id} is not registered for Section {$section_id}." + ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_settings_fields() { + return [ + 'aspireupdate_settings_section -> enable' => [ + 'section_id' => 'aspireupdate_settings_section', + 'field_id' => 'enable', + ], + 'aspireupdate_settings_section -> api_host' => [ + 'section_id' => 'aspireupdate_settings_section', + 'field_id' => 'api_host', + ], + 'aspireupdate_settings_section -> api_key' => [ + 'section_id' => 'aspireupdate_settings_section', + 'field_id' => 'api_key', + ], + 'aspireupdate_debug_settings_section -> enable_debug' => [ + 'section_id' => 'aspireupdate_debug_settings_section', + 'field_id' => 'enable_debug', + ], + 'aspireupdate_debug_settings_section -> enable_debug_type' => [ + 'section_id' => 'aspireupdate_debug_settings_section', + 'field_id' => 'enable_debug_type', + ], + 'aspireupdate_debug_settings_section -> disable_ssl_verification' => [ + 'section_id' => 'aspireupdate_debug_settings_section', + 'field_id' => 'disable_ssl_verification', + ], + ]; + } +}