Skip to content

Commit bbb6750

Browse files
committed
Removed LoggerSettingService as not needed. Updated tests for Admin Settings.
1 parent 3781589 commit bbb6750

File tree

9 files changed

+324
-114
lines changed

9 files changed

+324
-114
lines changed

plugins/wpgraphql-logging/src/Admin/Settings/ConfigurationHelper.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,25 @@ public function get_config(): array {
7575
* Get configuration for a specific section (tab).
7676
*
7777
* @param string $section The configuration section key.
78-
* @param array<string, mixed> $default_value Default value if section not found.
78+
* @param array<string, mixed> $default_value_value Default value if section not found.
7979
*
8080
* @return array<string, mixed>
8181
*/
82-
public function get_section_config( string $section, array $default_value = [] ): array {
82+
public function get_section_config( string $section, array $default_value_value = [] ): array {
8383
$config = $this->get_config();
84-
return $config[ $section ] ?? $default_value;
84+
return $config[ $section ] ?? $default_value_value;
8585
}
8686

8787
/**
8888
* Get a specific setting value from a configuration section.
8989
*
9090
* @param string $section The configuration section key.
9191
* @param string $setting_key The setting key within the section.
92-
* @param mixed $default_value Default value if setting not found.
92+
* @param mixed $default_value_value Default value if setting not found.
9393
*/
94-
public function get_setting( string $section, string $setting_key, $default_value = null ): mixed {
94+
public function get_setting( string $section, string $setting_key, $default_value_value = null ): mixed {
9595
$section_config = $this->get_section_config( $section );
96-
return $section_config[ $setting_key ] ?? $default_value;
96+
return $section_config[ $setting_key ] ?? $default_value_value;
9797
}
9898

9999
/**
@@ -135,15 +135,6 @@ public function clear_cache(): void {
135135
wp_cache_delete( $option_key, $this->get_settings_group() );
136136
}
137137

138-
/**
139-
* Reload the configuration from the database.
140-
* This bypasses any cache and forces a fresh load.
141-
*/
142-
public function reload_config(): void {
143-
$this->clear_cache();
144-
$this->load_config();
145-
}
146-
147138
/**
148139
* Get the option key for the settings.
149140
*/
@@ -158,6 +149,18 @@ public function get_settings_group(): string {
158149
return (string) apply_filters( 'wpgraphql_logging_settings_group_settings_group', WPGRAPHQL_LOGGING_SETTINGS_GROUP );
159150
}
160151

152+
/**
153+
* Get the raw option value from the database.
154+
*
155+
* @param string $option_key The option key to retrieve.
156+
* @param mixed $default_value Default value if option not found.
157+
*
158+
* @return array<string, mixed> The option value as an array.
159+
*/
160+
public function get_option_value( string $option_key, mixed $default_value = null ): array {
161+
return (array) get_option( $option_key, $default_value );
162+
}
163+
161164
/**
162165
* Hook into WordPress to clear cache when settings are updated.
163166
* This should be called during plugin initialization.
@@ -201,7 +204,7 @@ protected function load_config(): void {
201204
}
202205

203206
// Load from database.
204-
$this->config = (array) get_option( $option_key, [] );
207+
$this->config = $this->get_option_value( $option_key, [] );
205208

206209
// Cache the result in both cache groups.
207210
wp_cache_set( $option_key, $this->config, self::CACHE_GROUP, $cache_duration );

plugins/wpgraphql-logging/src/Admin/Settings/LoggingSettingsService.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

plugins/wpgraphql-logging/src/Admin/Settings/SettingsFormManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WPGraphQL\Logging\Admin\Settings;
66

7+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
78
use WPGraphQL\Logging\Admin\Settings\Fields\SettingsFieldCollection;
89

910
/**
@@ -18,8 +19,9 @@
1819
class SettingsFormManager {
1920
/**
2021
* @param \WPGraphQL\Logging\Admin\Settings\Fields\SettingsFieldCollection $field_collection Collection of fields to be registered in the settings sections.
22+
* @param \WPGraphQL\Logging\Admin\Settings\ConfigurationHelper $configuration_helper The configuration helper instance to access settings.
2123
*/
22-
public function __construct(readonly SettingsFieldCollection $field_collection ) {
24+
public function __construct(readonly SettingsFieldCollection $field_collection, readonly ConfigurationHelper $configuration_helper) {
2325
/**
2426
* Fire off init action.
2527
*
@@ -110,14 +112,14 @@ public function sanitize_settings( ?array $new_input ): array {
110112
* Get the option key for the settings group.
111113
*/
112114
public function get_option_key(): string {
113-
return LoggingSettingsService::get_option_key();
115+
return $this->configuration_helper->get_option_key();
114116
}
115117

116118
/**
117119
* Get the settings group for the options.
118120
*/
119121
public function get_settings_group(): string {
120-
return LoggingSettingsService::get_settings_group();
122+
return $this->configuration_helper->get_settings_group();
121123
}
122124

123125
/**

plugins/wpgraphql-logging/src/Admin/Settings/Templates/admin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
declare(strict_types=1);
1111

12+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
1213
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
1314
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
14-
use WPGraphQL\Logging\Admin\Settings\LoggingSettingsService;
1515

16-
$wpgraphql_logging_tabs_config = (array) get_query_var( 'wpgraphql_logging_main_page_config' );
17-
$wpgraphql_logging_current_tab = (string) ( $wpgraphql_logging_tabs_config['current_tab'] ?? '' );
18-
$wpgraphql_logging_tabs = (array) ( $wpgraphql_logging_tabs_config['tabs'] ?? [] );
16+
$wpgraphql_logging_configuration_helper = ConfigurationHelper::get_instance();
17+
$wpgraphql_logging_tabs_config = (array) get_query_var( 'wpgraphql_logging_main_page_config' );
18+
$wpgraphql_logging_current_tab = (string) ( $wpgraphql_logging_tabs_config['current_tab'] ?? '' );
19+
$wpgraphql_logging_tabs = (array) ( $wpgraphql_logging_tabs_config['tabs'] ?? [] );
1920
?>
2021

2122
<div class="wrap">
@@ -39,7 +40,7 @@
3940
<div class="inside">
4041
<?php
4142
// Settings sections.
42-
settings_fields( LoggingSettingsService::get_settings_group() );
43+
settings_fields( $wpgraphql_logging_configuration_helper->get_settings_group() );
4344
do_settings_sections( 'wpgraphql-logging-' . $wpgraphql_logging_current_tab );
4445
submit_button();
4546
?>

plugins/wpgraphql-logging/src/Admin/SettingsPage.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WPGraphQL\Logging\Admin;
66

7+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
78
use WPGraphQL\Logging\Admin\Settings\Fields\SettingsFieldCollection;
89
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
910
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\SettingsTabInterface;
@@ -127,7 +128,7 @@ public function register_settings_page(): void {
127128
*
128129
* @return string The path to the admin template file.
129130
*/
130-
public function get_admin_template() : string {
131+
public function get_admin_template(): string {
131132
$template_path = trailingslashit( WPGRAPHQL_LOGGING_PLUGIN_DIR ) . 'src/Admin/Settings/Templates/admin.php';
132133
return (string) apply_filters( 'wpgraphql_logging_admin_template_path', $template_path );
133134
}
@@ -140,7 +141,8 @@ public function register_settings_fields(): void {
140141
if ( ! isset( $collection ) ) {
141142
return;
142143
}
143-
$settings_manager = new SettingsFormManager( $collection );
144+
$configuration_helper = ConfigurationHelper::get_instance();
145+
$settings_manager = new SettingsFormManager( $collection, $configuration_helper );
144146
$settings_manager->render_form();
145147
}
146148

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\WPUnit\Admin\Settings;
6+
7+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
8+
use Codeception\TestCase\WPTestCase;
9+
10+
/**
11+
* Test class for ConfigurationHelper.
12+
*
13+
* @package WPGraphQL\Logging
14+
*
15+
* @since 0.0.1
16+
*/
17+
class ConfigurationHelperTest extends WPTestCase {
18+
19+
protected $config = [
20+
'basic_configuration' => [
21+
'enabled' => true,
22+
'log_level' => 'info',
23+
'max_log_entries' => 1000,
24+
'enable_debug_mode' => false
25+
],
26+
'data_management' => [
27+
'enabled' => false,
28+
'auto_cleanup' => true,
29+
'cleanup_interval' => 'weekly',
30+
'retention_period' => 30,
31+
'export_format' => 'json'
32+
]
33+
];
34+
35+
protected $default = [];
36+
37+
38+
public function setUp(): void {
39+
$configuration_helper = ConfigurationHelper::get_instance();
40+
$option_key = $configuration_helper->get_option_key();
41+
$this->default = get_option($option_key, []);
42+
update_option($option_key, $this->config);
43+
parent::setUp();
44+
}
45+
46+
public function tearDown(): void {
47+
parent::tearDown();
48+
$configuration_helper = ConfigurationHelper::get_instance();
49+
$option_key = $configuration_helper->get_option_key();
50+
update_option($option_key, $this->default);
51+
}
52+
53+
54+
/**
55+
* Test that instance is initially null and gets created.
56+
*/
57+
public function test_instance_initially_null_then_created(): void {
58+
// Use reflection to access the private static property
59+
$reflection = new \ReflectionClass(ConfigurationHelper::class);
60+
$instanceProperty = $reflection->getProperty('instance');
61+
$instanceProperty->setAccessible(true);
62+
63+
// Reset the instance to null
64+
$instanceProperty->setValue(null, null);
65+
66+
// Verify instance is null initially
67+
$this->assertNull($instanceProperty->getValue());
68+
69+
// Get instance should create it
70+
$instance = ConfigurationHelper::get_instance();
71+
72+
// Verify instance is no longer null and is correct type
73+
$this->assertNotNull($instanceProperty->getValue());
74+
$this->assertInstanceOf(ConfigurationHelper::class, $instance);
75+
$this->assertSame($instance, $instanceProperty->getValue());
76+
}
77+
78+
/**
79+
* Test get_config returns empty array when no config is set.
80+
*/
81+
public function test_get_config_returns_empty_array_when_no_config(): void {
82+
$instance = ConfigurationHelper::get_instance();
83+
$config = $instance->get_config();
84+
85+
$this->assertIsArray($config);
86+
}
87+
88+
public function test_get_setting(): void {
89+
$instance = ConfigurationHelper::get_instance();
90+
91+
$default_value = ['test_key' => 'default_value'];
92+
$section = 'nonexistent_section';
93+
$setting_key = 'test_key';
94+
$result = $instance->get_setting($section, $setting_key, $default_value);
95+
$this->assertEquals($default_value, $result);
96+
}
97+
98+
public function test_get_basic_config_returns_array(): void {
99+
$instance = ConfigurationHelper::get_instance();
100+
$basic_config = $instance->get_basic_config();
101+
102+
$this->assertIsArray($basic_config);
103+
$this->assertSame($this->config['basic_configuration'], $basic_config);
104+
}
105+
106+
public function test_get_data_management_returns_array(): void {
107+
$instance = ConfigurationHelper::get_instance();
108+
$data_management = $instance->get_data_management_config();
109+
110+
$this->assertIsArray($data_management);
111+
$this->assertSame($this->config['data_management'], $data_management);
112+
}
113+
114+
public function test_get_is_enabled(): void {
115+
$instance = ConfigurationHelper::get_instance();
116+
$this->assertTrue($instance->is_enabled('basic_configuration', 'enabled'));
117+
$this->assertFalse($instance->is_enabled('data_management', 'enabled'));
118+
}
119+
120+
}

plugins/wpgraphql-logging/tests/wpunit/Admin/Settings/Fields/Field/CheckboxFieldTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ public function test_render_field(): void {
6161
$rendered_output = ob_get_contents();
6262
ob_end_clean();
6363

64-
$expected_output = <<<HTML
65-
<span class="wpgraphql-logging-tooltip">
66-
<span class="dashicons dashicons-editor-help"></span>
67-
<span id="wpgraphql_logging_settings[basic_configuration][enable_logging]-tooltip" class="tooltip-text description">Enable or disable query logging for WPGraphQL requests.</span>
68-
</span><input type="checkbox" name="wpgraphql_logging_settings[basic_configuration][enable_logging]" aria-labelledby="wpgraphql_logging_settings[basic_configuration][enable_logging]-tooltip" value="1" class="custom-css-class" />
69-
HTML;
70-
$this->assertEquals(
71-
preg_replace('/[\s\t\r\n]+/', '', $expected_output),
72-
preg_replace('/[\s\t\r\n]+/', '', $rendered_output)
73-
);
74-
64+
$this->assertStringContainsString( 'type="checkbox"', $rendered_output );
7565
}
7666
}

0 commit comments

Comments
 (0)