Skip to content

Commit eb604fa

Browse files
committed
Fixed TODOs. Added tests for Settings form manager for sanitizing fields.
1 parent e0ab68b commit eb604fa

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

plugins/hwp-previews/src/Admin/Settings/Settings_Form_Manager.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace HWP\Previews\Admin\Settings;
66

77
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
8+
use HWP\Previews\Preview\Post\Post_Settings_Service;
89

910
class Settings_Form_Manager {
1011
/**
@@ -52,8 +53,7 @@ public function render_form(): void {
5253
*/
5354
public function sanitize_settings( array $new_input ): array {
5455

55-
// @TODO - Refactor with Settings Group.
56-
$option_name = apply_filters( 'hwp_previews_settings_key', HWP_PREVIEWS_SETTINGS_KEY );
56+
$option_name = $this->get_option_key();
5757

5858
$old_input = (array) get_option( $option_name, [] );
5959

@@ -88,16 +88,29 @@ public function sanitize_settings( array $new_input ): array {
8888
return $old_input;
8989
}
9090

91+
/**
92+
* Get the option key for the settings group.
93+
*/
94+
public function get_option_key(): string {
95+
return Post_Settings_Service::get_option_key();
96+
}
97+
98+
/**
99+
* Get the settings group for the options.
100+
*/
101+
public function get_settings_group(): string {
102+
return Post_Settings_Service::get_settings_group();
103+
}
104+
91105
/**
92106
* Create the settings tabs for the post-types.
93107
*
94108
* This method registers the settings group and the settings key for the post-types.
95109
*/
96110
protected function create_tabs(): void {
97111

98-
// @TODO - Refactor with Settings Group.
99-
$option_group = apply_filters( 'hwp_previews_settings_group', HWP_PREVIEWS_SETTINGS_GROUP );
100-
$option_name = apply_filters( 'hwp_previews_settings_key', HWP_PREVIEWS_SETTINGS_KEY );
112+
$option_group = $this->get_settings_group();
113+
$option_name = $this->get_option_key();
101114

102115
register_setting(
103116
$option_group,

plugins/hwp-previews/src/Preview/Post/Post_Settings_Service.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public function get_post_type_config( string $post_type ): ?array {
4040
/**
4141
* The option key for the settings group.
4242
*/
43-
public function get_option_key(): string {
43+
public static function get_option_key(): string {
4444
return (string) apply_filters( 'hwp_previews_settings_group_option_key', HWP_PREVIEWS_SETTINGS_KEY );
4545
}
4646

4747
/**
4848
* The settings group for the options.
4949
*/
50-
public function get_settings_group(): string {
50+
public static function get_settings_group(): string {
5151
return (string) apply_filters( 'hwp_previews_settings_group_settings_group', HWP_PREVIEWS_SETTINGS_GROUP );
5252
}
5353

@@ -56,8 +56,8 @@ public function get_settings_group(): string {
5656
* This method is called in the constructor to ensure settings are available.
5757
*/
5858
protected function setup(): void {
59-
$option_key = $this->get_option_key();
60-
$settings_group = $this->get_settings_group();
59+
$option_key = self::get_option_key();
60+
$settings_group = self::get_settings_group();
6161

6262
$value = wp_cache_get( $option_key, $settings_group );
6363
if ( is_array( $value ) ) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace HWP\Previews\wpunit\Admin\Settings;
4+
5+
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
6+
use HWP\Previews\Admin\Settings\Settings_Form_Manager;
7+
use HWP\Previews\Preview\Post\Post_Preview_Service;
8+
use lucatume\WPBrowser\TestCase\WPTestCase;
9+
10+
class SettingsFormManagerTest extends WPTestCase {
11+
12+
public function test_sanitize_settings() {
13+
14+
$post_preview_service = new Post_Preview_Service();
15+
$form_manager = new Settings_Form_Manager(
16+
$post_preview_service->get_post_types(),
17+
new Settings_Field_Collection()
18+
);
19+
20+
$data = [
21+
"page" => [
22+
"enabled" => "1",
23+
"post_statuses_as_parent" => "1",
24+
"in_iframe" => "1",
25+
"preview_url" => "https://localhost:3000/page?preview=true&post_id={ID}&name={slug}"
26+
]
27+
];
28+
29+
$sanitized_data = $form_manager->sanitize_settings( $data );
30+
$this->assertEquals( $sanitized_data, $data );
31+
}
32+
33+
public function test_sanitize_settings_invalid_field() {
34+
35+
$post_preview_service = new Post_Preview_Service();
36+
$form_manager = new Settings_Form_Manager(
37+
$post_preview_service->get_post_types(),
38+
new Settings_Field_Collection()
39+
);
40+
41+
$data = [
42+
"page" => [
43+
"enabled" => "1",
44+
'not_registered_field' => "This field is not registered in the field collection so it should be removed",
45+
"post_statuses_as_parent" => "1",
46+
"in_iframe" => "1",
47+
"preview_url" => "https://localhost:3000/page?preview=true&post_id={ID}&name={slug}"
48+
]
49+
];
50+
51+
$sanitized_data = $form_manager->sanitize_settings( $data );
52+
53+
// The 'not_registered_field' should be removed from the sanitized data.
54+
unset( $data["page"]["not_registered_field"] );
55+
56+
$this->assertEquals( $sanitized_data, $data );
57+
}
58+
59+
public function test_sanitize_settings_invalid_format() {
60+
61+
$post_preview_service = new Post_Preview_Service();
62+
$form_manager = new Settings_Form_Manager(
63+
$post_preview_service->get_post_types(),
64+
new Settings_Field_Collection()
65+
);
66+
67+
$data = [
68+
"enabled" => "1",
69+
"post_statuses_as_parent" => "1",
70+
"in_iframe" => "1",
71+
"preview_url" => "https://localhost:3000/page?preview=true&post_id={ID}&name={slug}"
72+
];
73+
74+
$this->assertEmpty( $form_manager->sanitize_settings( $data ) );
75+
$this->assertEmpty( $form_manager->sanitize_settings([]) );
76+
}
77+
}

plugins/hwp-previews/tests/wpunit/Preview/Post/PostSettingsServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public function test_get_post_type_config_returns_null_when_no_settings(): void
9393

9494
public function test_get_option_key_returns_filtered_value(): void {
9595
$this->service = new Post_Settings_Service();
96-
$result = $this->service->get_option_key();
96+
$result = Post_Settings_Service::get_option_key();
9797

9898
$this->assertEquals( $this->test_option_key, $result );
9999
}
100100

101101
public function test_get_settings_group_returns_filtered_value(): void {
102102
$this->service = new Post_Settings_Service();
103-
$result = $this->service->get_settings_group();
103+
$result = Post_Settings_Service::get_settings_group();
104104

105105
$this->assertEquals( $this->test_settings_group, $result );
106106
}

0 commit comments

Comments
 (0)