@@ -16,6 +16,30 @@ class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
1616 * @var int
1717 */
1818 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+ }
1943
2044 /**
2145 * Setup.
@@ -42,6 +66,18 @@ public function tear_down() {
4266 parent::tear_down();
4367 }
4468
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+
4581 /**
4682 * @ticket 47720
4783 *
@@ -136,23 +172,12 @@ public function data_start_el_with_empty_attributes() {
136172 * @param string $target Optional. The target value. Default empty string.
137173 */
138174 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' );
150175
151176 $output = '';
152177
153178 $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,
156181 'title' => 'Privacy Policy',
157182 'target' => $target,
158183 'xfn' => $xfn,
@@ -217,6 +242,7 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
217242 );
218243
219244 // Do not set the privacy policy page.
245+ update_option( 'wp_page_for_privacy_policy', 0 );
220246
221247 $output = '';
222248
@@ -251,23 +277,12 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
251277 * @covers Walker_Nav_Menu::start_el
252278 */
253279 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' );
265280
266281 $output = '';
267282
268283 $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,
271286 'title' => 'Privacy Policy',
272287 'target' => '',
273288 'xfn' => '',
@@ -296,22 +311,10 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
296311 * @covers Walker_Nav_Menu::start_el
297312 */
298313 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-
311314 $output = '';
312315
313316 // 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;
315318
316319 $item = array(
317320 'ID' => $not_privacy_policy_id,
@@ -414,6 +417,46 @@ public function test_build_atts_should_build_attributes( $atts, $expected ) {
414417 $this->assertSame( $expected, $actual );
415418 }
416419
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+
417460 /**
418461 * Data provider.
419462 *
@@ -447,4 +490,15 @@ public function data_build_atts_should_build_attributes() {
447490 ),
448491 );
449492 }
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+ }
450504}
0 commit comments