@@ -9,6 +9,9 @@ class Test_Speculation_Rules_Settings extends WP_UnitTestCase {
9
9
10
10
/**
11
11
* @covers ::plsr_register_setting
12
+ * @covers ::plsr_get_mode_labels
13
+ * @covers ::plsr_get_eagerness_labels
14
+ * @covers ::plsr_get_setting_default
12
15
*/
13
16
public function test_plsr_register_setting (): void {
14
17
unregister_setting ( 'reading ' , 'plsr_speculation_rules ' );
@@ -18,6 +21,14 @@ public function test_plsr_register_setting(): void {
18
21
plsr_register_setting ();
19
22
$ settings = get_registered_settings ();
20
23
$ this ->assertArrayHasKey ( 'plsr_speculation_rules ' , $ settings );
24
+
25
+ $ settings = plsr_get_setting_default ();
26
+ $ this ->assertArrayHasKey ( 'mode ' , $ settings );
27
+ $ this ->assertArrayHasKey ( 'eagerness ' , $ settings );
28
+
29
+ // Test default settings applied correctly.
30
+ $ default_settings = plsr_get_setting_default ();
31
+ $ this ->assertEquals ( $ default_settings , get_option ( 'plsr_speculation_rules ' ) );
21
32
}
22
33
23
34
/**
@@ -122,4 +133,131 @@ public function test_plsr_add_settings_action_link(): void {
122
133
plsr_add_settings_action_link ( $ default_action_links )
123
134
);
124
135
}
136
+
137
+ /**
138
+ * @covers ::plsr_get_stored_setting_value
139
+ */
140
+ public function test_get_stored_setting_value (): void {
141
+ update_option (
142
+ 'plsr_speculation_rules ' ,
143
+ array (
144
+ 'mode ' => 'prefetch ' ,
145
+ 'eagerness ' => 'moderate ' ,
146
+ )
147
+ );
148
+ $ settings = plsr_get_stored_setting_value ();
149
+ $ this ->assertEquals (
150
+ array (
151
+ 'mode ' => 'prefetch ' ,
152
+ 'eagerness ' => 'moderate ' ,
153
+ ),
154
+ $ settings
155
+ );
156
+
157
+ // Test default when no option is set.
158
+ delete_option ( 'plsr_speculation_rules ' );
159
+ $ settings = plsr_get_stored_setting_value ();
160
+ $ this ->assertEquals ( plsr_get_setting_default (), $ settings );
161
+ }
162
+
163
+ /**
164
+ * Function to test sanitize_setting() with various inputs.
165
+ */
166
+ public function test_plsr_sanitize_setting_with_invalid_inputs (): void {
167
+
168
+ $ input = array (
169
+ 'mode ' => 'invalid_mode ' ,
170
+ 'eagerness ' => 'conservative ' ,
171
+ );
172
+ $ sanitized = plsr_sanitize_setting ( $ input );
173
+ $ this ->assertEquals ( 'prerender ' , $ sanitized ['mode ' ] );
174
+
175
+ $ input = array (
176
+ 'mode ' => 'prefetch ' ,
177
+ 'eagerness ' => 'invalid_eagerness ' ,
178
+ );
179
+ $ sanitized = plsr_sanitize_setting ( $ input );
180
+ $ this ->assertEquals ( 'moderate ' , $ sanitized ['eagerness ' ] );
181
+
182
+ $ input = 'invalid_input ' ;
183
+ $ sanitized = plsr_sanitize_setting ( $ input );
184
+ $ this ->assertEquals ( plsr_get_setting_default (), $ sanitized );
185
+ }
186
+
187
+ /**
188
+ * @covers ::plsr_add_setting_ui
189
+ */
190
+ public function test_plsr_add_setting_ui (): void {
191
+ do_action ( 'load-options-reading.php ' );// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
192
+
193
+ // Check if the settings section has been added.
194
+ global $ wp_settings_sections ;
195
+ $ this ->assertArrayHasKey ( 'reading ' , $ wp_settings_sections );
196
+ $ this ->assertArrayHasKey ( 'plsr_speculation_rules ' , $ wp_settings_sections ['reading ' ] );
197
+
198
+ // Check the output of the callback function for the section.
199
+ $ output = get_echo ( $ wp_settings_sections ['reading ' ]['plsr_speculation_rules ' ]['callback ' ] );
200
+ $ this ->assertStringContainsString ( 'This section allows you to control how URLs that your users navigate to are speculatively loaded to improve performance. ' , $ output );
201
+ }
202
+
203
+ /**
204
+ * Data provider for testing plsr_render_settings_field.
205
+ *
206
+ * @return array<string, array<mixed>> Data for testing settings fields.
207
+ */
208
+ public function data_provider_to_test_render_settings_field (): array {
209
+ return array (
210
+ 'mode ' => array (
211
+ array (
212
+ 'field ' => 'mode ' ,
213
+ 'title ' => 'Speculation Mode ' ,
214
+ 'description ' => 'Prerendering will lead to faster load times than prefetching. ' ,
215
+ ),
216
+ array (
217
+ 'mode ' => 'prefetch ' ,
218
+ 'eagerness ' => 'moderate ' ,
219
+ ),
220
+ 'name="plsr_speculation_rules[mode]" ' ,
221
+ 'value="prefetch" ' ,
222
+ 'Prerendering will lead to faster load times than prefetching. ' ,
223
+ ),
224
+ 'eagerness ' => array (
225
+ array (
226
+ 'field ' => 'eagerness ' ,
227
+ 'title ' => 'Eagerness ' ,
228
+ 'description ' => 'The eagerness setting defines the heuristics based on which the loading is triggered. ' ,
229
+ ),
230
+ array (
231
+ 'mode ' => 'prefetch ' ,
232
+ 'eagerness ' => 'moderate ' ,
233
+ ),
234
+ 'name="plsr_speculation_rules[eagerness]" ' ,
235
+ 'value="moderate" ' ,
236
+ 'The eagerness setting defines the heuristics based on which the loading is triggered. ' ,
237
+ ),
238
+ );
239
+ }
240
+
241
+ /**
242
+ * Test rendering of settings fields using data provider.
243
+ *
244
+ * @dataProvider data_provider_to_test_render_settings_field
245
+ * @param array<mixed> $args Arguments for the settings field.
246
+ * @param array<mixed> $stored_settings Stored settings values.
247
+ * @param string $name_check HTML name attribute check.
248
+ * @param string $value_check HTML value attribute check.
249
+ * @param string $description_check Description check.
250
+ */
251
+ public function test_plsr_render_settings_field ( array $ args , array $ stored_settings , string $ name_check , string $ value_check , string $ description_check ): void {
252
+ // Simulate getting stored settings.
253
+ update_option ( 'plsr_speculation_rules ' , $ stored_settings );
254
+
255
+ // Capture the output of the settings field rendering.
256
+ $ output = get_echo ( 'plsr_render_settings_field ' , array ( $ args ) );
257
+
258
+ // Check for the presence of form elements.
259
+ $ this ->assertStringContainsString ( $ name_check , $ output );
260
+ $ this ->assertStringContainsString ( $ value_check , $ output );
261
+ $ this ->assertStringContainsString ( $ description_check , $ output );
262
+ }
125
263
}
0 commit comments