forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPostStatus.php
More file actions
225 lines (201 loc) · 7.58 KB
/
getPostStatus.php
File metadata and controls
225 lines (201 loc) · 7.58 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
<?php
/**
* @group post
*/
class Tests_Post_GetPostStatus extends WP_UnitTestCase {
/**
* Array of post IDs.
*
* @var int[]
*/
public static $post_ids;
/**
* Create shared fixtures.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
$post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash', 'private', 'delete' );
foreach ( $post_statuses as $post_status ) {
$date = '';
$actual_status = $post_status;
if ( 'future' === $post_status ) {
$date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
} elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
$actual_status = 'publish';
}
self::$post_ids[ $post_status ] = $factory->post->create(
array(
'post_status' => $actual_status,
'post_date' => $date,
'post_name' => "$post_status-post",
)
);
// Attachments without parent or media.
self::$post_ids[ "$post_status-attachment-no-parent" ] = $factory->attachment->create_object(
array(
'post_status' => $actual_status,
'post_name' => "$post_status-attachment-no-parent",
'post_date' => $date,
)
);
// Attachments without media.
self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
array(
'post_parent' => self::$post_ids[ $post_status ],
'post_status' => 'inherit',
'post_name' => "$post_status-attachment",
'post_date' => $date,
)
);
}
// Attachment with incorrect parent ID.
self::$post_ids['badly-parented-attachment'] = $factory->attachment->create_object(
array(
'post_parent' => PHP_INT_MAX, // Impossibly large number.
'post_status' => 'inherit',
'post_name' => "$post_status-attachment",
'post_date' => $date,
)
);
// Trash the trash post and attachment.
wp_trash_post( self::$post_ids['trash'] );
wp_trash_post( self::$post_ids['trash-attachment-no-parent'] );
// Force delete parent and unattached post objects.
wp_delete_post( self::$post_ids['delete'], true );
wp_delete_post( self::$post_ids['delete-attachment-no-parent'], true );
}
/**
* Ensure `get_post_status()` resolves correctly for posts and attachments.
*
* @ticket 52326
* @dataProvider data_get_post_status_resolves
*
* @param string $post_key The post key in self::$post_ids.
* @param string $expected The expected get_post_status() return value.
*/
public function test_get_post_status_resolves( $post_key, $expected ) {
$this->assertSame( $expected, get_post_status( self::$post_ids[ $post_key ] ) );
}
/**
* Data provider for test_get_post_status_resolves().
*
* @return array[] {
* @type string $post_key The post key in self::$post_ids.
* @type string $expected The expected get_post_status() return value.
* }
*/
public function data_get_post_status_resolves() {
return array(
array( 'publish', 'publish' ),
array( 'future', 'future' ),
array( 'draft', 'draft' ),
array( 'auto-draft', 'auto-draft' ),
array( 'trash', 'trash' ),
array( 'private', 'private' ),
array( 'delete', false ),
// Attachment with `inherit` status from parent.
array( 'publish-attachment', 'publish' ),
array( 'future-attachment', 'future' ),
array( 'draft-attachment', 'draft' ),
array( 'auto-draft-attachment', 'auto-draft' ),
array( 'trash-attachment', 'publish' ),
array( 'private-attachment', 'private' ),
array( 'delete-attachment', 'publish' ),
// Attachment with native status (rather than inheriting from parent).
array( 'publish-attachment-no-parent', 'publish' ),
array( 'future-attachment-no-parent', 'publish' ), // Attachments can't have future status.
array( 'draft-attachment-no-parent', 'publish' ), // Attachments can't have draft status.
array( 'auto-draft-attachment-no-parent', 'auto-draft' ),
array( 'trash-attachment-no-parent', 'trash' ),
array( 'private-attachment-no-parent', 'private' ),
array( 'delete-attachment-no-parent', false ),
// Attachment attempting to inherit from an invalid parent number.
array( 'badly-parented-attachment', 'publish' ),
);
}
/**
* Ensure post status resolves after trashing parent posts.
*
* @ticket 52326
* @dataProvider data_get_post_status_after_trashing
*
* @param string $post_to_test The post key in self::$post_ids.
* @param string $post_to_trash The post key to trash then delete in self::$post_ids.
* @param string $expected The expected result after trashing the post.
*/
public function test_get_post_status_after_trashing( $post_to_test, $post_to_trash, $expected ) {
wp_trash_post( self::$post_ids[ $post_to_trash ] );
$this->assertSame( $expected, get_post_status( self::$post_ids[ $post_to_test ] ) );
// Now delete the post, expect publish.
wp_delete_post( self::$post_ids[ $post_to_trash ], true );
$this->assertSame( 'publish', get_post_status( self::$post_ids[ $post_to_test ] ) );
}
/**
* Data provider for test_get_post_status_after_trashing().
* @return array[] {
* @type string $post_to_test The post key in self::$post_ids.
* @type string $post_to_trash The post key to trash then delete in self::$post_ids.
* @type string $expected The expected result after trashing the post.
* }
*/
public function data_get_post_status_after_trashing() {
return array(
array( 'publish-attachment', 'publish', 'publish' ),
array( 'future-attachment', 'future', 'future' ),
array( 'draft-attachment', 'draft', 'draft' ),
array( 'auto-draft-attachment', 'auto-draft', 'auto-draft' ),
array( 'private-attachment', 'private', 'private' ),
array( 'delete-attachment', 'publish', 'publish' ),
);
}
/**
* Ensure the `post_states_html` filter works to modify post state output.
*
* @ticket 51403
*
* @dataProvider data_filter_post_states_html_should_enable_post_state_html_output_modification
*
* @covers ::_post_states
*
* @param string $post_state The post state to test.
*/
public function test_filter_post_states_html_should_enable_post_state_html_output_modification( $post_state ) {
$post = get_post( self::$post_ids[ $post_state ] );
$original_output = _post_states( $post, false );
if ( count( get_post_states( $post ) ) === 0 ) {
$text_to_append = '— <span class="post-state">Sample state</span>';
} else {
$text_to_append = '<span class="post-state">, Sample state</span>';
}
add_filter(
'post_states_html',
function ( $post_states_html, $post_states, $filtered_post ) use ( $text_to_append, $post ) {
$this->assertIsString( $post_states_html, 'Expected first filter arg to be a string.' );
$this->assertIsArray( $post_states, 'Expected second filter arg to be an array.' );
$this->assertInstanceOf( WP_Post::class, $filtered_post, 'Expected third filter arg to be a WP_Post' );
$this->assertSame( $post->ID, $filtered_post->ID, 'Expected the third filter arg to be the same as the current post.' );
return $post_states_html . $text_to_append;
},
10,
3
);
$output = _post_states( $post, false );
$this->assertSame( $original_output . $text_to_append, $output, 'Expected text to be appended to the original output.' );
}
/**
* Data provider for test_filter_post_states_html_should_enable_post_state_html_output_modification().
*
* @return array[] {
* @type string $post_state The post state to test.
* }
*/
public static function data_filter_post_states_html_should_enable_post_state_html_output_modification() {
return array(
array( 'publish' ),
array( 'future' ),
array( 'draft' ),
array( 'auto-draft' ),
array( 'trash' ),
array( 'private' ),
);
}
}