forked from WordPress/wordpress-develop
/
includesListTable.php
427 lines (357 loc) · 12 KB
/
includesListTable.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
* @group admin
*/
class Tests_Admin_includesListTable extends WP_UnitTestCase {
protected static $top = array();
protected static $children = array();
protected static $grandchildren = array();
protected static $post_ids = array();
/**
* @var WP_Posts_List_Table
*/
protected $table;
function setUp(): void {
parent::setUp();
$this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) );
}
public static function wpSetUpBeforeClass( $factory ) {
// Note that our top/children/grandchildren arrays are 1-indexed.
// Create top-level pages.
$num_posts = 5;
foreach ( range( 1, $num_posts ) as $i ) {
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => sprintf( 'Top Level Page %d', $i ),
)
);
self::$top[ $i ] = $p;
self::$post_ids[] = $p->ID;
}
// Create child pages.
$num_children = 3;
foreach ( self::$top as $top => $top_page ) {
foreach ( range( 1, $num_children ) as $i ) {
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_parent' => $top_page->ID,
'post_title' => sprintf( 'Child %d', $i ),
)
);
self::$children[ $top ][ $i ] = $p;
self::$post_ids[] = $p->ID;
}
}
// Create grand-child pages for the third and fourth top-level pages.
$num_grandchildren = 3;
foreach ( range( 3, 4 ) as $top ) {
foreach ( self::$children[ $top ] as $child => $child_page ) {
foreach ( range( 1, $num_grandchildren ) as $i ) {
$p = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_parent' => $child_page->ID,
'post_title' => sprintf( 'Grandchild %d', $i ),
)
);
self::$grandchildren[ $top ][ $child ][ $i ] = $p;
self::$post_ids[] = $p->ID;
}
}
}
}
/**
* @ticket 15459
*/
function test_list_hierarchical_pages_first_page() {
$this->_test_list_hierarchical_page(
array(
'paged' => 1,
'posts_per_page' => 2,
),
array(
self::$top[1]->ID,
self::$children[1][1]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_list_hierarchical_pages_second_page() {
$this->_test_list_hierarchical_page(
array(
'paged' => 2,
'posts_per_page' => 2,
),
array(
self::$top[1]->ID,
self::$children[1][2]->ID,
self::$children[1][3]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_search_hierarchical_pages_first_page() {
$this->_test_list_hierarchical_page(
array(
'paged' => 1,
'posts_per_page' => 2,
's' => 'Child',
),
array(
self::$children[1][1]->ID,
self::$children[1][2]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_search_hierarchical_pages_second_page() {
$this->_test_list_hierarchical_page(
array(
'paged' => 2,
'posts_per_page' => 2,
's' => 'Top',
),
array(
self::$top[3]->ID,
self::$top[4]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_grandchildren_hierarchical_pages_first_page() {
// Page 6 is the first page with grandchildren.
$this->_test_list_hierarchical_page(
array(
'paged' => 6,
'posts_per_page' => 2,
),
array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][1]->ID,
self::$grandchildren[3][1][2]->ID,
)
);
}
/**
* @ticket 15459
*/
function test_grandchildren_hierarchical_pages_second_page() {
// Page 7 is the second page with grandchildren.
$this->_test_list_hierarchical_page(
array(
'paged' => 7,
'posts_per_page' => 2,
),
array(
self::$top[3]->ID,
self::$children[3][1]->ID,
self::$grandchildren[3][1][3]->ID,
self::$children[3][2]->ID,
)
);
}
/**
* Helper function to test the output of a page which uses `WP_Posts_List_Table`.
*
* @param array $args Query args for the list of pages.
* @param array $expected_ids Expected IDs of pages returned.
*/
protected function _test_list_hierarchical_page( array $args, array $expected_ids ) {
$matches = array();
$_REQUEST['paged'] = $args['paged'];
$GLOBALS['per_page'] = $args['posts_per_page'];
$args = array_merge(
array(
'post_type' => 'page',
),
$args
);
// Mimic the behaviour of `wp_edit_posts_query()`:
if ( ! isset( $args['orderby'] ) ) {
$args['orderby'] = 'menu_order title';
$args['order'] = 'asc';
$args['posts_per_page'] = -1;
$args['posts_per_archive_page'] = -1;
}
$pages = new WP_Query( $args );
ob_start();
$this->table->set_hierarchical_display( true );
$this->table->display_rows( $pages->posts );
$output = ob_get_clean();
// Clean up.
unset( $_REQUEST['paged'] );
unset( $GLOBALS['per_page'] );
preg_match_all( '|<tr[^>]*>|', $output, $matches );
$this->assertCount( count( $expected_ids ), array_keys( $matches[0] ) );
foreach ( $expected_ids as $id ) {
$this->assertStringContainsString( sprintf( 'id="post-%d"', $id ), $output );
}
}
/**
* @ticket 37407
*/
function test_filter_button_should_not_be_shown_if_there_are_no_posts() {
// Set post type to a non-existent one.
$this->table->screen->post_type = 'foo';
ob_start();
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="post-query-submit"', $output );
}
/**
* @ticket 37407
*/
function test_months_dropdown_should_not_be_shown_if_there_are_no_posts() {
// Set post type to a non-existent one.
$this->table->screen->post_type = 'foo';
ob_start();
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="filter-by-date"', $output );
}
/**
* @ticket 37407
*/
function test_category_dropdown_should_not_be_shown_if_there_are_no_posts() {
// Set post type to a non-existent one.
$this->table->screen->post_type = 'foo';
ob_start();
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="cat"', $output );
}
/**
* @ticket 38341
*/
public function test_empty_trash_button_should_not_be_shown_if_there_are_no_posts() {
// Set post type to a non-existent one.
$this->table->screen->post_type = 'foo';
ob_start();
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="delete_all"', $output );
}
/**
* @ticket 40188
*/
public function test_filter_button_should_not_be_shown_if_there_are_no_comments() {
$table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
ob_start();
$table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="post-query-submit"', $output );
}
/**
* @ticket 40188
*/
public function test_filter_button_should_be_shown_if_there_are_comments() {
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);
$table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
$table->prepare_items();
ob_start();
$table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringContainsString( 'id="post-query-submit"', $output );
}
/**
* @ticket 40188
*/
public function test_filter_comment_status_dropdown_should_be_shown_if_there_are_comments() {
$post_id = self::factory()->post->create();
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => '1',
)
);
$table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
$table->prepare_items();
ob_start();
$table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringContainsString( 'id="filter-by-comment-type"', $output );
$this->assertStringContainsString( "<option value='comment'>", $output );
}
/**
* @ticket 38341
*/
public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comments() {
$table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
ob_start();
$table->extra_tablenav( 'top' );
$output = ob_get_clean();
$this->assertStringNotContainsString( 'id="delete_all"', $output );
}
/**
* @ticket 45089
*/
public function test_sortable_columns() {
require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
$override_sortable_columns = array(
'author' => array( 'comment_author', true ),
'response' => 'comment_post_ID',
'date' => array( 'comment_date', 'dEsC' ), // The ordering support should be case-insensitive.
);
// Stub the get_sortable_columns() method.
$object = $this->getMockBuilder( 'WP_Comments_List_Table' )
->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
->setMethods( array( 'get_sortable_columns' ) )
->getMock();
// Change the null return value of the stubbed get_sortable_columns() method.
$object->method( 'get_sortable_columns' )
->willReturn( $override_sortable_columns );
$output = get_echo( array( $object, 'print_column_headers' ) );
$this->assertStringContainsString( '?orderby=comment_author&order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
$this->assertStringContainsString( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
$this->assertStringContainsString( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
$this->assertStringContainsString( '?orderby=comment_date&order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
$this->assertStringContainsString( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
}
/**
* @ticket 45089
*/
public function test_sortable_columns_with_current_ordering() {
require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
$override_sortable_columns = array(
'author' => array( 'comment_author', false ),
'response' => 'comment_post_ID',
'date' => array( 'comment_date', 'asc' ), // We will override this with current ordering.
);
// Current ordering.
$_GET['orderby'] = 'comment_date';
$_GET['order'] = 'desc';
// Stub the get_sortable_columns() method.
$object = $this->getMockBuilder( 'WP_Comments_List_Table' )
->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
->setMethods( array( 'get_sortable_columns' ) )
->getMock();
// Change the null return value of the stubbed get_sortable_columns() method.
$object->method( 'get_sortable_columns' )
->willReturn( $override_sortable_columns );
$output = get_echo( array( $object, 'print_column_headers' ) );
$this->assertStringContainsString( '?orderby=comment_author&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
$this->assertStringContainsString( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
$this->assertStringContainsString( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
$this->assertStringContainsString( '?orderby=comment_date&order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
$this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
}
}