Skip to content

Commit f8e9ddb

Browse files
authored
add unit tests
1 parent fbf18e4 commit f8e9ddb

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Tests for _reset_privacy_policy_page_for_post() and the self-healing
4+
* check in WP_Privacy_Policy_Content::notice().
5+
*
6+
* @package WordPress
7+
* @subpackage UnitTests
8+
* @since 7.1.0
9+
*
10+
* @group privacy
11+
*
12+
* @covers ::_reset_privacy_policy_page_for_post
13+
*/
14+
class Tests_Privacy_WpPrivacyResetPolicyPageForPost extends WP_UnitTestCase {
15+
/**
16+
* ID of the page set as the Privacy Policy page.
17+
*
18+
* @var int
19+
*/
20+
private $policy_page_id;
21+
22+
public function set_up() {
23+
parent::set_up();
24+
25+
$this->policy_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
26+
update_option( 'wp_page_for_privacy_policy', $this->policy_page_id );
27+
}
28+
29+
public function tear_down() {
30+
delete_option( 'wp_page_for_privacy_policy' );
31+
parent::tear_down();
32+
}
33+
34+
/**
35+
* Tests that trashing the Privacy Policy page resets the option to 0.
36+
*
37+
* @ticket 56694
38+
*/
39+
public function test_trashing_privacy_policy_page_resets_option() {
40+
wp_trash_post( $this->policy_page_id );
41+
42+
$this->assertSame( 0, (int) get_option( 'wp_page_for_privacy_policy' ) );
43+
}
44+
45+
/**
46+
* Tests that permanently deleting the Privacy Policy page resets the option to 0.
47+
*
48+
* @ticket 56694
49+
*/
50+
public function test_deleting_privacy_policy_page_resets_option() {
51+
wp_delete_post( $this->policy_page_id, true );
52+
53+
$this->assertSame( 0, (int) get_option( 'wp_page_for_privacy_policy' ) );
54+
}
55+
56+
/**
57+
* Tests that trashing a different page does not change the option.
58+
*
59+
* @ticket 56694
60+
*/
61+
public function test_trashing_a_different_page_does_not_reset_option() {
62+
$other_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
63+
wp_trash_post( $other_page_id );
64+
65+
$this->assertSame(
66+
$this->policy_page_id,
67+
(int) get_option( 'wp_page_for_privacy_policy' ),
68+
'Trashing an unrelated page should not reset wp_page_for_privacy_policy.'
69+
);
70+
}
71+
72+
/**
73+
* Tests that deleting a non-page post type does not change the option.
74+
*
75+
* @ticket 56694
76+
*/
77+
public function test_deleting_non_page_post_type_does_not_reset_option() {
78+
$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
79+
wp_delete_post( $post_id, true );
80+
81+
$this->assertSame(
82+
$this->policy_page_id,
83+
(int) get_option( 'wp_page_for_privacy_policy' ),
84+
'Deleting a non-page post should not reset wp_page_for_privacy_policy.'
85+
);
86+
}
87+
88+
/**
89+
* Tests that WP_Privacy_Policy_Content::notice() resets the option to 0
90+
* when the stored ID points to a page that no longer exists.
91+
*
92+
* @ticket 56694
93+
*
94+
* @covers WP_Privacy_Policy_Content::notice
95+
*/
96+
public function test_notice_self_heals_when_policy_page_does_not_exist() {
97+
update_option( 'wp_page_for_privacy_policy', 99999 );
98+
99+
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
100+
set_current_screen( 'post' );
101+
102+
$post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) );
103+
WP_Privacy_Policy_Content::notice( $post );
104+
105+
$this->assertSame(
106+
0,
107+
(int) get_option( 'wp_page_for_privacy_policy' ),
108+
'notice() should reset the option to 0 when the stored page does not exist.'
109+
);
110+
}
111+
112+
/**
113+
* Tests that _reset_privacy_policy_page_for_post() does not call
114+
* update_option() when wp_page_for_privacy_policy is already 0.
115+
*
116+
* @ticket 56694
117+
*/
118+
public function test_no_update_option_when_policy_page_already_zero() {
119+
update_option( 'wp_page_for_privacy_policy', 0 );
120+
121+
$call_count = 0;
122+
add_filter(
123+
'pre_update_option_wp_page_for_privacy_policy',
124+
static function ( $value ) use ( &$call_count ) {
125+
++$call_count;
126+
return $value;
127+
}
128+
);
129+
130+
$other_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
131+
wp_trash_post( $other_page_id );
132+
133+
$this->assertSame(
134+
0,
135+
$call_count,
136+
'update_option() should not be called when wp_page_for_privacy_policy is already 0.'
137+
);
138+
}
139+
}

0 commit comments

Comments
 (0)