forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpDeletePost.php
More file actions
241 lines (212 loc) · 7.09 KB
/
wpDeletePost.php
File metadata and controls
241 lines (212 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Test wp_delete_post() function
*
* @package WordPress
* @subpackage Post
*
* @since 6.9.0
*/
/**
* Class to Test wp_delete_post() function
*
* @group post
* @covers ::wp_delete_post
*/
class Tests_Post_WpDeletePost extends WP_UnitTestCase {
/**
* User IDs for the test.
*
* @var array{administrator: int, editor: int, contributor: int}
*/
protected static $user_ids;
/**
* Set up before class.
*
* @param WP_UnitTest_Factory $factory The Unit Test Factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_ids = array(
'administrator' => $factory->user->create(
array(
'role' => 'administrator',
)
),
'editor' => $factory->user->create(
array(
'role' => 'editor',
)
),
'contributor' => $factory->user->create(
array(
'role' => 'contributor',
)
),
);
}
/**
* Tests wp_delete_post reassign hierarchical post type.
*/
public function test_wp_delete_post_reassign_hierarchical_post_type() {
$grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$parent_page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $grandparent_page_id,
)
);
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $parent_page_id,
)
);
$this->assertSame( $parent_page_id, get_post( $page_id )->post_parent );
$this->assertInstanceOf( WP_Post::class, wp_delete_post( $parent_page_id, true ) );
$this->assertSame( $grandparent_page_id, get_post( $page_id )->post_parent );
$this->assertInstanceOf( WP_Post::class, wp_delete_post( $grandparent_page_id, true ) );
$this->assertSame( 0, get_post( $page_id )->post_parent );
}
/**
* Tests: "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly."
*
* @ticket 5364
*/
public function test_delete_future_post_cron() {
$future_date = strtotime( '+1 day' );
$data = array(
'post_status' => 'publish',
'post_content' => 'content',
'post_title' => 'title',
'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
);
// Insert a post and make sure the ID is OK.
$post_id = wp_insert_post( $data );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
// Now delete the post and make sure the cron entry is removed.
$this->assertInstanceOf( WP_Post::class, wp_delete_post( $post_id ) );
$this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
}
/**
* Helper function: return the timestamp(s) of cron jobs for the specified hook and post.
*/
private function next_schedule_for_post( $hook, int $post_id ) {
return wp_next_scheduled( $hook, array( 0 => $post_id ) );
}
/**
* Tests that if the post_id is 0, wp_delete_post should return false.
*
* @ticket 63975
*/
public function test_wp_delete_post_short_circuit_on_post_id_zero() {
$this->setExpectedIncorrectUsage( 'wp_delete_post' );
$this->assertFalse( wp_delete_post( 0, true ) );
}
/**
* Tests wp_delete_post() when the post for the post_id has been already deleted.
*/
public function test_wp_delete_post_returns_false_for_invalid_post() {
$post_id = self::factory()->post->create();
$deleted_post = wp_delete_post( $post_id, true );
$this->assertInstanceOf( WP_Post::class, $deleted_post );
$this->assertSame( $post_id, $deleted_post->ID );
$this->assertNull( wp_delete_post( $post_id, true ) );
}
/**
* Tests actions triggered when deleting a post, even when a string ID is supplied.
*
* @ticket 63975
*/
public function test_wp_delete_post_actions() {
$actions = array(
'before_delete_post',
'delete_post_post',
'delete_post',
'deleted_post_post',
'deleted_post',
'after_delete_post',
);
$captured_action_args = array();
$initial_action_counts = array();
foreach ( $actions as $action ) {
$initial_action_counts[ $action ] = did_action( $action );
add_action(
$action,
static function () use ( $action, &$captured_action_args ) {
$captured_action_args[ $action ] = func_get_args();
},
10,
PHP_INT_MAX
);
}
$post_id = self::factory()->post->create();
$deleted_post = wp_delete_post( (string) $post_id, true );
$this->assertInstanceOf( WP_Post::class, $deleted_post );
foreach ( array( 'before_delete_post', 'delete_post_post', 'delete_post', 'deleted_post_post', 'deleted_post', 'after_delete_post' ) as $action ) {
$this->assertSame( $initial_action_counts[ $action ] + 1, did_action( $action ), "Expected $action action count to increment by 1." );
$this->assertCount( 2, $captured_action_args[ $action ], "Expected count for $action action" );
$this->assertSame( $post_id, $captured_action_args[ $action ][0], "Expected post ID for $action action" );
$this->assertInstanceOf( WP_Post::class, $captured_action_args[ $action ][1], "Expected class for $action action" );
$this->assertSame( $post_id, $captured_action_args[ $action ][1]->ID, "Expected post ID for $action action" );
}
}
/**
* Tests short-circuiting wp_delete_post() with pre_delete_post filter.
*
* @ticket @63975
*/
public function test_wp_delete_post_can_be_short_circuited() {
$post_id = self::factory()->post->create();
$filter = function ( $check, WP_Post $post, bool $force_delete ) use ( $post_id ) {
$this->assertNull( $check );
$this->assertSame( $post_id, $post->ID );
$this->assertTrue( $force_delete );
return false;
};
add_filter( 'pre_delete_post', $filter, 10, 3 );
$this->assertFalse( wp_delete_post( $post_id, true ) );
$post = get_post( $post_id );
$this->assertInstanceOf( WP_Post::class, $post );
$this->assertSame( $post_id, $post->ID );
}
/**
* Tests that wp_delete_post() deletes associated comments.
*/
public function test_wp_delete_post_deletes_associated_comments() {
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_type' => 'comment',
)
);
$this->assertInstanceOf( WP_Post::class, wp_delete_post( $post_id, true ) );
$this->assertNull( get_comment( $comment_id ) );
}
/**
* Tests that upon deletion of a post, attachments should be reattached to the parent post.
*/
public function test_wp_delete_post_reassigns_attachments_to_parent() {
$parent_post_id = self::factory()->post->create(
array(
'post_type' => 'page',
)
);
$post_id = self::factory()->post->create(
array(
'post_parent' => $parent_post_id,
'post_type' => 'page',
)
);
$attachment_id = self::factory()->attachment->create(
array(
'post_parent' => $post_id,
'post_type' => 'attachment',
)
);
$this->assertInstanceOf( WP_Post::class, wp_delete_post( $post_id, true ) );
clean_post_cache( $attachment_id );
$this->assertSame( $parent_post_id, get_post( $attachment_id )->post_parent );
}
}