Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arzola committed Jan 17, 2025
1 parent 6e755eb commit 1db8c0d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 45 deletions.
12 changes: 6 additions & 6 deletions src/wp-includes/class-walker-nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Walker_Nav_Menu extends Walker {
/**
* The URL to the privacy policy page.
*
* @since 6.7.2
* @since 6.2.0
* @var string
*/
private static $privacy_policy_url = null;
Expand All @@ -52,15 +52,15 @@ class Walker_Nav_Menu extends Walker {
* This method caches the URL to avoid multiple database queries and shared the URL between all instances of the class.
* Single query is performed on the first call to this method.
*
* @since 6.7.2
* @since 6.8.0
*
* @return string The URL to the privacy policy page.
*/
public static function check_privacy_policy_url() {
private static function get_privacy_policy_url() {
if ( null === self::$privacy_policy_url ) {
self::$privacy_policy_url = get_privacy_policy_url();
static::$privacy_policy_url = get_privacy_policy_url();
}
return self::$privacy_policy_url;
return static::$privacy_policy_url;
}

/**
Expand Down Expand Up @@ -260,7 +260,7 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur
$atts['rel'] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';

if ( ! empty( $menu_item->url ) ) {
if ( static::check_privacy_policy_url() === $menu_item->url ) {
if ( static::get_privacy_policy_url() === $menu_item->url ) {
$atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
}

Expand Down
132 changes: 93 additions & 39 deletions tests/phpunit/tests/menu/walker-nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
* @var int
*/
private $orig_wp_nav_menu_max_depth;
/**
* The privacy policy page ID.
*
* @var int
*/
protected static $privacy_policy_id;

/**
* Set up before class.
*/
public static function set_up_before_class() {
parent::set_up_before_class();

$post_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Test Privacy Policy',
'post_status' => 'publish',
)
);

update_option( 'wp_page_for_privacy_policy', $post_id );
self::$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
}

/**
* Setup.
Expand All @@ -42,6 +66,18 @@ public function tear_down() {
parent::tear_down();
}

/**
* Tear down after class.
*/
public static function tear_down_after_class() {
if ( self::$privacy_policy_id ) {
wp_delete_post( self::$privacy_policy_id, true );
update_option( 'wp_page_for_privacy_policy', 0 );
}

parent::tear_down_after_class();
}

/**
* @ticket 47720
*
Expand Down Expand Up @@ -136,23 +172,12 @@ public function data_start_el_with_empty_attributes() {
* @param string $target Optional. The target value. Default empty string.
*/
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_to_privacy_policy_url( $expected, $xfn = '', $target = '' ) {
$post_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Test Privacy Policy',
'post_status' => 'publish',
)
);

// Set the privacy policy page.
update_option( 'wp_page_for_privacy_policy', $post_id );
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );

$output = '';

$item = array(
'ID' => $privacy_policy_id,
'object_id' => $privacy_policy_id,
'ID' => self::$privacy_policy_id,
'object_id' => self::$privacy_policy_id,
'title' => 'Privacy Policy',
'target' => $target,
'xfn' => $xfn,
Expand Down Expand Up @@ -217,6 +242,7 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
);

// Do not set the privacy policy page.
update_option( 'wp_page_for_privacy_policy', 0 );

$output = '';

Expand Down Expand Up @@ -251,23 +277,12 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
* @covers Walker_Nav_Menu::start_el
*/
public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_when_no_url_is_passed() {
$post_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Test Privacy Policy',
'post_status' => 'publish',
)
);

// Set the privacy policy page.
update_option( 'wp_page_for_privacy_policy', $post_id );
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );

$output = '';

$item = array(
'ID' => $privacy_policy_id,
'object_id' => $privacy_policy_id,
'ID' => self::$privacy_policy_id,
'object_id' => self::$privacy_policy_id,
'title' => 'Privacy Policy',
'target' => '',
'xfn' => '',
Expand Down Expand Up @@ -296,22 +311,10 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
* @covers Walker_Nav_Menu::start_el
*/
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_when_id_does_not_match_but_url_does() {
$post_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Test Privacy Policy',
'post_status' => 'publish',
)
);

// Set the privacy policy page.
update_option( 'wp_page_for_privacy_policy', $post_id );
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );

$output = '';

// Ensure the ID does not match the privacy policy.
$not_privacy_policy_id = $privacy_policy_id - 1;
$not_privacy_policy_id = self::$privacy_policy_id - 1;

$item = array(
'ID' => $not_privacy_policy_id,
Expand Down Expand Up @@ -414,6 +417,46 @@ public function test_build_atts_should_build_attributes( $atts, $expected ) {
$this->assertSame( $expected, $actual );
}

/**
* Test that get_privacy_policy_url() returns the correct URL.
*/
public function test_get_privacy_policy_url_returns_correct_url() {
$expected_url = get_privacy_policy_url();
$actual_url = $this->invoke_private_method( 'get_privacy_policy_url' );

$this->assertSame( $expected_url, $actual_url, 'The URL should match the privacy policy URL set in the option.' );
}

/**
* Test that get_privacy_policy_url() updates after cache reset.
*/
public function test_get_privacy_policy_url_updates_after_reset() {
// Set initial privacy policy URL.
$first_url = $this->invoke_private_method( 'get_privacy_policy_url' );

// Change the privacy policy option.
$new_policy_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'New Privacy Policy',
'post_status' => 'publish',
)
);
update_option( 'wp_page_for_privacy_policy', $new_policy_id );

// Reset the cache by setting the static property to null.
$reflection = new ReflectionClass( 'Walker_Nav_Menu' );
$property = $reflection->getProperty( 'privacy_policy_url' );
$property->setAccessible( true );
$property->setValue( null );

// Fetch the new URL.
$new_url = $this->invoke_private_method( 'get_privacy_policy_url' );

$this->assertNotSame( $first_url, $new_url, 'The URL should update after the cache is reset.' );
}


/**
* Data provider.
*
Expand Down Expand Up @@ -447,4 +490,15 @@ public function data_build_atts_should_build_attributes() {
),
);
}

/**
* Helper method to call private methods.
*/
private function invoke_private_method( $method_name ) {
$reflection = new ReflectionClass( 'Walker_Nav_Menu' );
$method = $reflection->getMethod( $method_name );
$method->setAccessible( true );

return $method->invoke( null );
}
}

0 comments on commit 1db8c0d

Please sign in to comment.