@@ -16,6 +16,30 @@ class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
16
16
* @var int
17
17
*/
18
18
private $ orig_wp_nav_menu_max_depth ;
19
+ /**
20
+ * The privacy policy page ID.
21
+ *
22
+ * @var int
23
+ */
24
+ protected static $ privacy_policy_id ;
25
+
26
+ /**
27
+ * Set up before class.
28
+ */
29
+ public static function set_up_before_class () {
30
+ parent ::set_up_before_class ();
31
+
32
+ $ post_id = self ::factory ()->post ->create (
33
+ array (
34
+ 'post_type ' => 'page ' ,
35
+ 'post_title ' => 'Test Privacy Policy ' ,
36
+ 'post_status ' => 'publish ' ,
37
+ )
38
+ );
39
+
40
+ update_option ( 'wp_page_for_privacy_policy ' , $ post_id );
41
+ self ::$ privacy_policy_id = (int ) get_option ( 'wp_page_for_privacy_policy ' );
42
+ }
19
43
20
44
/**
21
45
* Setup.
@@ -42,6 +66,18 @@ public function tear_down() {
42
66
parent ::tear_down ();
43
67
}
44
68
69
+ /**
70
+ * Tear down after class.
71
+ */
72
+ public static function tear_down_after_class () {
73
+ if ( self ::$ privacy_policy_id ) {
74
+ wp_delete_post ( self ::$ privacy_policy_id , true );
75
+ update_option ( 'wp_page_for_privacy_policy ' , 0 );
76
+ }
77
+
78
+ parent ::tear_down_after_class ();
79
+ }
80
+
45
81
/**
46
82
* @ticket 47720
47
83
*
@@ -136,23 +172,12 @@ public function data_start_el_with_empty_attributes() {
136
172
* @param string $target Optional. The target value. Default empty string.
137
173
*/
138
174
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_to_privacy_policy_url ( $ expected , $ xfn = '' , $ target = '' ) {
139
- $ post_id = self ::factory ()->post ->create (
140
- array (
141
- 'post_type ' => 'page ' ,
142
- 'post_title ' => 'Test Privacy Policy ' ,
143
- 'post_status ' => 'publish ' ,
144
- )
145
- );
146
-
147
- // Set the privacy policy page.
148
- update_option ( 'wp_page_for_privacy_policy ' , $ post_id );
149
- $ privacy_policy_id = (int ) get_option ( 'wp_page_for_privacy_policy ' );
150
175
151
176
$ output = '' ;
152
177
153
178
$ item = array (
154
- 'ID ' => $ privacy_policy_id ,
155
- 'object_id ' => $ privacy_policy_id ,
179
+ 'ID ' => self :: $ privacy_policy_id ,
180
+ 'object_id ' => self :: $ privacy_policy_id ,
156
181
'title ' => 'Privacy Policy ' ,
157
182
'target ' => $ target ,
158
183
'xfn ' => $ xfn ,
@@ -217,6 +242,7 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
217
242
);
218
243
219
244
// Do not set the privacy policy page.
245
+ update_option ( 'wp_page_for_privacy_policy ' , 0 );
220
246
221
247
$ output = '' ;
222
248
@@ -251,23 +277,12 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
251
277
* @covers Walker_Nav_Menu::start_el
252
278
*/
253
279
public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_when_no_url_is_passed () {
254
- $ post_id = self ::factory ()->post ->create (
255
- array (
256
- 'post_type ' => 'page ' ,
257
- 'post_title ' => 'Test Privacy Policy ' ,
258
- 'post_status ' => 'publish ' ,
259
- )
260
- );
261
-
262
- // Set the privacy policy page.
263
- update_option ( 'wp_page_for_privacy_policy ' , $ post_id );
264
- $ privacy_policy_id = (int ) get_option ( 'wp_page_for_privacy_policy ' );
265
280
266
281
$ output = '' ;
267
282
268
283
$ item = array (
269
- 'ID ' => $ privacy_policy_id ,
270
- 'object_id ' => $ privacy_policy_id ,
284
+ 'ID ' => self :: $ privacy_policy_id ,
285
+ 'object_id ' => self :: $ privacy_policy_id ,
271
286
'title ' => 'Privacy Policy ' ,
272
287
'target ' => '' ,
273
288
'xfn ' => '' ,
@@ -296,22 +311,10 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
296
311
* @covers Walker_Nav_Menu::start_el
297
312
*/
298
313
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_when_id_does_not_match_but_url_does () {
299
- $ post_id = self ::factory ()->post ->create (
300
- array (
301
- 'post_type ' => 'page ' ,
302
- 'post_title ' => 'Test Privacy Policy ' ,
303
- 'post_status ' => 'publish ' ,
304
- )
305
- );
306
-
307
- // Set the privacy policy page.
308
- update_option ( 'wp_page_for_privacy_policy ' , $ post_id );
309
- $ privacy_policy_id = (int ) get_option ( 'wp_page_for_privacy_policy ' );
310
-
311
314
$ output = '' ;
312
315
313
316
// Ensure the ID does not match the privacy policy.
314
- $ not_privacy_policy_id = $ privacy_policy_id - 1 ;
317
+ $ not_privacy_policy_id = self :: $ privacy_policy_id - 1 ;
315
318
316
319
$ item = array (
317
320
'ID ' => $ not_privacy_policy_id ,
@@ -414,6 +417,46 @@ public function test_build_atts_should_build_attributes( $atts, $expected ) {
414
417
$ this ->assertSame ( $ expected , $ actual );
415
418
}
416
419
420
+ /**
421
+ * Test that get_privacy_policy_url() returns the correct URL.
422
+ */
423
+ public function test_get_privacy_policy_url_returns_correct_url () {
424
+ $ expected_url = get_privacy_policy_url ();
425
+ $ actual_url = $ this ->invoke_private_method ( 'get_privacy_policy_url ' );
426
+
427
+ $ this ->assertSame ( $ expected_url , $ actual_url , 'The URL should match the privacy policy URL set in the option. ' );
428
+ }
429
+
430
+ /**
431
+ * Test that get_privacy_policy_url() updates after cache reset.
432
+ */
433
+ public function test_get_privacy_policy_url_updates_after_reset () {
434
+ // Set initial privacy policy URL.
435
+ $ first_url = $ this ->invoke_private_method ( 'get_privacy_policy_url ' );
436
+
437
+ // Change the privacy policy option.
438
+ $ new_policy_id = self ::factory ()->post ->create (
439
+ array (
440
+ 'post_type ' => 'page ' ,
441
+ 'post_title ' => 'New Privacy Policy ' ,
442
+ 'post_status ' => 'publish ' ,
443
+ )
444
+ );
445
+ update_option ( 'wp_page_for_privacy_policy ' , $ new_policy_id );
446
+
447
+ // Reset the cache by setting the static property to null.
448
+ $ reflection = new ReflectionClass ( 'Walker_Nav_Menu ' );
449
+ $ property = $ reflection ->getProperty ( 'privacy_policy_url ' );
450
+ $ property ->setAccessible ( true );
451
+ $ property ->setValue ( null );
452
+
453
+ // Fetch the new URL.
454
+ $ new_url = $ this ->invoke_private_method ( 'get_privacy_policy_url ' );
455
+
456
+ $ this ->assertNotSame ( $ first_url , $ new_url , 'The URL should update after the cache is reset. ' );
457
+ }
458
+
459
+
417
460
/**
418
461
* Data provider.
419
462
*
@@ -447,4 +490,15 @@ public function data_build_atts_should_build_attributes() {
447
490
),
448
491
);
449
492
}
493
+
494
+ /**
495
+ * Helper method to call private methods.
496
+ */
497
+ private function invoke_private_method ( $ method_name ) {
498
+ $ reflection = new ReflectionClass ( 'Walker_Nav_Menu ' );
499
+ $ method = $ reflection ->getMethod ( $ method_name );
500
+ $ method ->setAccessible ( true );
501
+
502
+ return $ method ->invoke ( null );
503
+ }
450
504
}
0 commit comments