Changeset 51997 for trunk/tests/phpunit/tests/admin/wpCommentsListTable.php
- Timestamp:
- 11/03/2021 12:48:42 AM (3 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/admin/wpCommentsListTable.php
r51995 r51997 4 4 * @group admin 5 5 */ 6 class Tests_Admin_IncludesListTable extends WP_UnitTestCase { 7 protected static $top = array(); 8 protected static $children = array(); 9 protected static $grandchildren = array(); 10 protected static $post_ids = array(); 6 class Tests_Admin_wpCommentsListTable extends WP_UnitTestCase { 11 7 12 8 /** 13 * @var WP_ Posts_List_Table9 * @var WP_Comments_List_Table 14 10 */ 15 11 protected $table; … … 17 13 function set_up() { 18 14 parent::set_up(); 19 $this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) ); 20 } 21 22 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 23 // Note that our top/children/grandchildren arrays are 1-indexed. 24 25 // Create top-level pages. 26 $num_posts = 5; 27 foreach ( range( 1, $num_posts ) as $i ) { 28 $p = $factory->post->create_and_get( 29 array( 30 'post_type' => 'page', 31 'post_title' => sprintf( 'Top Level Page %d', $i ), 32 ) 33 ); 34 35 self::$top[ $i ] = $p; 36 self::$post_ids[] = $p->ID; 37 } 38 39 // Create child pages. 40 $num_children = 3; 41 foreach ( self::$top as $top => $top_page ) { 42 foreach ( range( 1, $num_children ) as $i ) { 43 $p = $factory->post->create_and_get( 44 array( 45 'post_type' => 'page', 46 'post_parent' => $top_page->ID, 47 'post_title' => sprintf( 'Child %d', $i ), 48 ) 49 ); 50 51 self::$children[ $top ][ $i ] = $p; 52 self::$post_ids[] = $p->ID; 53 } 54 } 55 56 // Create grand-child pages for the third and fourth top-level pages. 57 $num_grandchildren = 3; 58 foreach ( range( 3, 4 ) as $top ) { 59 foreach ( self::$children[ $top ] as $child => $child_page ) { 60 foreach ( range( 1, $num_grandchildren ) as $i ) { 61 $p = $factory->post->create_and_get( 62 array( 63 'post_type' => 'page', 64 'post_parent' => $child_page->ID, 65 'post_title' => sprintf( 'Grandchild %d', $i ), 66 ) 67 ); 68 69 self::$grandchildren[ $top ][ $child ][ $i ] = $p; 70 self::$post_ids[] = $p->ID; 71 } 72 } 73 } 74 } 75 76 /** 77 * @ticket 15459 78 * 79 * @covers WP_Posts_List_Table::display_rows 80 * @covers WP_Posts_List_Table::set_hierarchical_display 81 */ 82 function test_list_hierarchical_pages_first_page() { 83 $this->_test_list_hierarchical_page( 84 array( 85 'paged' => 1, 86 'posts_per_page' => 2, 87 ), 88 array( 89 self::$top[1]->ID, 90 self::$children[1][1]->ID, 91 ) 92 ); 93 } 94 95 /** 96 * @ticket 15459 97 * 98 * @covers WP_Posts_List_Table::display_rows 99 * @covers WP_Posts_List_Table::set_hierarchical_display 100 */ 101 function test_list_hierarchical_pages_second_page() { 102 $this->_test_list_hierarchical_page( 103 array( 104 'paged' => 2, 105 'posts_per_page' => 2, 106 ), 107 array( 108 self::$top[1]->ID, 109 self::$children[1][2]->ID, 110 self::$children[1][3]->ID, 111 ) 112 ); 113 } 114 115 /** 116 * @ticket 15459 117 * 118 * @covers WP_Posts_List_Table::display_rows 119 * @covers WP_Posts_List_Table::set_hierarchical_display 120 */ 121 function test_search_hierarchical_pages_first_page() { 122 $this->_test_list_hierarchical_page( 123 array( 124 'paged' => 1, 125 'posts_per_page' => 2, 126 's' => 'Child', 127 ), 128 array( 129 self::$children[1][1]->ID, 130 self::$children[1][2]->ID, 131 ) 132 ); 133 } 134 135 /** 136 * @ticket 15459 137 * 138 * @covers WP_Posts_List_Table::display_rows 139 * @covers WP_Posts_List_Table::set_hierarchical_display 140 */ 141 function test_search_hierarchical_pages_second_page() { 142 $this->_test_list_hierarchical_page( 143 array( 144 'paged' => 2, 145 'posts_per_page' => 2, 146 's' => 'Top', 147 ), 148 array( 149 self::$top[3]->ID, 150 self::$top[4]->ID, 151 ) 152 ); 153 } 154 155 /** 156 * @ticket 15459 157 * 158 * @covers WP_Posts_List_Table::display_rows 159 * @covers WP_Posts_List_Table::set_hierarchical_display 160 */ 161 function test_grandchildren_hierarchical_pages_first_page() { 162 // Page 6 is the first page with grandchildren. 163 $this->_test_list_hierarchical_page( 164 array( 165 'paged' => 6, 166 'posts_per_page' => 2, 167 ), 168 array( 169 self::$top[3]->ID, 170 self::$children[3][1]->ID, 171 self::$grandchildren[3][1][1]->ID, 172 self::$grandchildren[3][1][2]->ID, 173 ) 174 ); 175 } 176 177 /** 178 * @ticket 15459 179 * 180 * @covers WP_Posts_List_Table::display_rows 181 * @covers WP_Posts_List_Table::set_hierarchical_display 182 */ 183 function test_grandchildren_hierarchical_pages_second_page() { 184 // Page 7 is the second page with grandchildren. 185 $this->_test_list_hierarchical_page( 186 array( 187 'paged' => 7, 188 'posts_per_page' => 2, 189 ), 190 array( 191 self::$top[3]->ID, 192 self::$children[3][1]->ID, 193 self::$grandchildren[3][1][3]->ID, 194 self::$children[3][2]->ID, 195 ) 196 ); 197 } 198 199 /** 200 * Helper function to test the output of a page which uses `WP_Posts_List_Table`. 201 * 202 * @param array $args Query args for the list of pages. 203 * @param array $expected_ids Expected IDs of pages returned. 204 */ 205 protected function _test_list_hierarchical_page( array $args, array $expected_ids ) { 206 if ( PHP_VERSION_ID >= 80100 ) { 207 /* 208 * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in 209 * via hooked in filter functions until a more structural solution to the 210 * "missing input validation" conundrum has been architected and implemented. 211 */ 212 $this->expectDeprecation(); 213 $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' ); 214 } 215 216 $matches = array(); 217 218 $_REQUEST['paged'] = $args['paged']; 219 $GLOBALS['per_page'] = $args['posts_per_page']; 220 221 $args = array_merge( 222 array( 223 'post_type' => 'page', 224 ), 225 $args 226 ); 227 228 // Mimic the behaviour of `wp_edit_posts_query()`: 229 if ( ! isset( $args['orderby'] ) ) { 230 $args['orderby'] = 'menu_order title'; 231 $args['order'] = 'asc'; 232 $args['posts_per_page'] = -1; 233 $args['posts_per_archive_page'] = -1; 234 } 235 236 // Effectively ignore the output until retrieving it later via `getActualOutput()`. 237 $this->expectOutputRegex( '`.`' ); 238 239 $pages = new WP_Query( $args ); 240 241 $this->table->set_hierarchical_display( true ); 242 $this->table->display_rows( $pages->posts ); 243 $output = $this->getActualOutput(); 244 245 // Clean up. 246 unset( $_REQUEST['paged'] ); 247 unset( $GLOBALS['per_page'] ); 248 249 preg_match_all( '|<tr[^>]*>|', $output, $matches ); 250 251 $this->assertCount( count( $expected_ids ), array_keys( $matches[0] ) ); 252 253 foreach ( $expected_ids as $id ) { 254 $this->assertStringContainsString( sprintf( 'id="post-%d"', $id ), $output ); 255 } 256 } 257 258 /** 259 * @ticket 37407 260 * 261 * @covers WP_Posts_List_Table::extra_tablenav 262 */ 263 function test_filter_button_should_not_be_shown_if_there_are_no_posts() { 264 // Set post type to a non-existent one. 265 $this->table->screen->post_type = 'foo'; 266 267 ob_start(); 268 $this->table->extra_tablenav( 'top' ); 269 $output = ob_get_clean(); 270 271 $this->assertStringNotContainsString( 'id="post-query-submit"', $output ); 272 } 273 274 /** 275 * @ticket 37407 276 * 277 * @covers WP_Posts_List_Table::extra_tablenav 278 */ 279 function test_months_dropdown_should_not_be_shown_if_there_are_no_posts() { 280 // Set post type to a non-existent one. 281 $this->table->screen->post_type = 'foo'; 282 283 ob_start(); 284 $this->table->extra_tablenav( 'top' ); 285 $output = ob_get_clean(); 286 287 $this->assertStringNotContainsString( 'id="filter-by-date"', $output ); 288 } 289 290 /** 291 * @ticket 37407 292 * 293 * @covers WP_Posts_List_Table::extra_tablenav 294 */ 295 function test_category_dropdown_should_not_be_shown_if_there_are_no_posts() { 296 // Set post type to a non-existent one. 297 $this->table->screen->post_type = 'foo'; 298 299 ob_start(); 300 $this->table->extra_tablenav( 'top' ); 301 $output = ob_get_clean(); 302 303 $this->assertStringNotContainsString( 'id="cat"', $output ); 304 } 305 306 /** 307 * @ticket 38341 308 * 309 * @covers WP_Posts_List_Table::extra_tablenav 310 */ 311 public function test_empty_trash_button_should_not_be_shown_if_there_are_no_posts() { 312 // Set post type to a non-existent one. 313 $this->table->screen->post_type = 'foo'; 314 315 ob_start(); 316 $this->table->extra_tablenav( 'top' ); 317 $output = ob_get_clean(); 318 319 $this->assertStringNotContainsString( 'id="delete_all"', $output ); 15 $this->table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 320 16 } 321 17 … … 326 22 */ 327 23 public function test_filter_button_should_not_be_shown_if_there_are_no_comments() { 328 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );329 330 24 ob_start(); 331 $t able->extra_tablenav( 'top' );25 $this->table->extra_tablenav( 'top' ); 332 26 $output = ob_get_clean(); 333 27 … … 349 43 ); 350 44 351 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 352 $table->prepare_items(); 45 $this->table->prepare_items(); 353 46 354 47 ob_start(); 355 $t able->extra_tablenav( 'top' );48 $this->table->extra_tablenav( 'top' ); 356 49 $output = ob_get_clean(); 357 50 … … 373 66 ); 374 67 375 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 376 $table->prepare_items(); 68 $this->table->prepare_items(); 377 69 378 70 ob_start(); 379 $t able->extra_tablenav( 'top' );71 $this->table->extra_tablenav( 'top' ); 380 72 $output = ob_get_clean(); 381 73 … … 390 82 */ 391 83 public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comments() { 392 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );393 394 84 ob_start(); 395 $t able->extra_tablenav( 'top' );85 $this->table->extra_tablenav( 'top' ); 396 86 $output = ob_get_clean(); 397 87 … … 405 95 */ 406 96 public function test_bulk_action_menu_supports_options_and_optgroups() { 407 $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );408 409 97 add_filter( 410 98 'bulk_actions-edit-comments', … … 421 109 422 110 ob_start(); 423 $t able->bulk_actions();111 $this->table->bulk_actions(); 424 112 $output = ob_get_clean(); 425 113 … … 442 130 */ 443 131 public function test_sortable_columns() { 444 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';445 446 132 $override_sortable_columns = array( 447 133 'author' => array( 'comment_author', true ), … … 478 164 */ 479 165 public function test_sortable_columns_with_current_ordering() { 480 require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';481 482 166 $override_sortable_columns = array( 483 167 'author' => array( 'comment_author', false ),
Note: See TracChangeset
for help on using the changeset viewer.