Skip to content

Commit 54a09a7

Browse files
committed
Build/Test: Add Tests for wp_scheduled_delete.
Props pbearne. Fixes #59938. git-svn-id: https://develop.svn.wordpress.org/trunk@57224 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e3731cc commit 54a09a7

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/**
4+
* Tests for the wp_scheduled_delete function.
5+
*
6+
* @group Functions.php
7+
*
8+
* @covers ::wp_scheduled_delete
9+
*/
10+
class Tests_Functions_wpScheduledDelete extends WP_UnitTestCase {
11+
12+
protected static $comment_id;
13+
protected static $page_id;
14+
15+
public function tear_down() {
16+
// Remove comment.
17+
if ( self::$comment_id ) {
18+
wp_delete_comment( self::$comment_id );
19+
}
20+
// Remove page.
21+
if ( self::$page_id ) {
22+
wp_delete_post( self::$page_id );
23+
}
24+
parent::tear_down();
25+
}
26+
27+
/**
28+
* Delete old trashed post/pages.
29+
*
30+
* @ticket 59938
31+
*
32+
*/
33+
public function test_wp_scheduled_delete() {
34+
self::$page_id = self::factory()->post->create(
35+
array(
36+
'post_type' => 'page',
37+
'post_status' => 'trash',
38+
)
39+
);
40+
add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) );
41+
add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' );
42+
43+
$this->assertNotEmpty( get_post( self::$page_id ) );
44+
45+
wp_scheduled_delete();
46+
47+
$this->assertEmpty( get_post( self::$page_id ) );
48+
}
49+
50+
/**
51+
* Don't delete old trashed post/pages if status not trash.
52+
* Remove the trash meta status.
53+
*
54+
* @ticket 59938
55+
*
56+
*/
57+
public function test_wp_scheduled_delete_not_trash() {
58+
self::$page_id = self::factory()->post->create(
59+
array(
60+
'post_type' => 'page',
61+
'post_status' => 'published',
62+
)
63+
);
64+
add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) );
65+
add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' );
66+
67+
$this->assertNotEmpty( get_post( self::$page_id ) );
68+
69+
wp_scheduled_delete();
70+
71+
$this->assertNotEmpty( get_post( self::$page_id ) );
72+
$this->assertEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_time', true ) );
73+
$this->assertEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_status', true ) );
74+
}
75+
76+
77+
/**
78+
* Don't delete old trashed post/pages if old enough.
79+
*
80+
* @ticket 59938
81+
*
82+
*/
83+
public function test_wp_scheduled_delete_not_old() {
84+
self::$page_id = self::factory()->post->create(
85+
array(
86+
'post_type' => 'page',
87+
'post_status' => 'trash',
88+
)
89+
);
90+
add_post_meta( self::$page_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ) );
91+
add_post_meta( self::$page_id, '_wp_trash_meta_status', 'published' );
92+
93+
$this->assertNotEmpty( get_post( self::$page_id ) );
94+
95+
wp_scheduled_delete();
96+
97+
$this->assertNotEmpty( get_post( self::$page_id ) );
98+
$this->assertNotEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_time', true ) );
99+
$this->assertNotEmpty( get_post_meta( self::$page_id, '_wp_trash_meta_status', true ) );
100+
}
101+
102+
/**
103+
* Delete old trashed comments.
104+
*
105+
* @ticket 59938
106+
*
107+
*/
108+
public function test_wp_scheduled_delete_comment() {
109+
self::$comment_id = self::factory()->comment->create(
110+
array(
111+
'comment_approved' => 'trash',
112+
)
113+
);
114+
add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) );
115+
add_post_meta( self::$comment_id, '_wp_trash_meta_status', 'published' );
116+
117+
$this->assertNotEmpty( get_comment( self::$comment_id ) );
118+
119+
wp_scheduled_delete();
120+
121+
$this->assertEmpty( get_comment( self::$comment_id ) );
122+
}
123+
124+
/**
125+
* Don't delete old trashed comments if status not trash.
126+
* Remove the trash meta status.
127+
*
128+
* @ticket 59938
129+
*
130+
*/
131+
public function test_wp_scheduled_delete_not_trash_comment() {
132+
self::$comment_id = self::factory()->comment->create(
133+
array(
134+
'comment_approved' => '1',
135+
)
136+
);
137+
add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS + 1 ) );
138+
add_comment_meta( self::$comment_id, '_wp_trash_meta_status', 'published' );
139+
140+
$this->assertNotEmpty( get_comment( self::$comment_id ) );
141+
142+
wp_scheduled_delete();
143+
144+
$this->assertNotEmpty( get_comment( self::$comment_id ) );
145+
$this->assertEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_time', true ) );
146+
$this->assertEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_status', true ) );
147+
}
148+
149+
150+
/**
151+
* Don't delete old trashed comments if old enough.
152+
*
153+
* @ticket 59938
154+
*
155+
*/
156+
public function test_wp_scheduled_delete_not_old_comment() {
157+
self::$comment_id = self::factory()->comment->create(
158+
array(
159+
'comment_approved' => 'trash',
160+
)
161+
);
162+
add_comment_meta( self::$comment_id, '_wp_trash_meta_time', time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ) );
163+
add_comment_meta( self::$comment_id, '_wp_trash_meta_status', 'published' );
164+
165+
$this->assertNotEmpty( get_comment( self::$comment_id ) );
166+
167+
wp_scheduled_delete();
168+
169+
$this->assertNotEmpty( get_comment( self::$comment_id ) );
170+
$this->assertNotEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_time', true ) );
171+
$this->assertNotEmpty( get_comment_meta( self::$comment_id, '_wp_trash_meta_status', true ) );
172+
}
173+
}

0 commit comments

Comments
 (0)