123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?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() {
- parent::setUp();
- $this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) );
- }
- public static function wpSetUpBeforeClass( WP_UnitTest_Factory $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->assertContains( 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->assertNotContains( '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->assertNotContains( '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->assertNotContains( '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->assertNotContains( '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->assertNotContains( '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->assertContains( 'id="post-query-submit"', $output );
- }
- /**
- * @ticket 40188
- */
- public function test_filter_comment_type_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->assertContains( 'id="filter-by-comment-type"', $output );
- $this->assertContains( "<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->assertNotContains( 'id="delete_all"', $output );
- }
- /**
- * @ticket 19278
- */
- public function test_bulk_action_menu_supports_options_and_optgroups() {
- $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
- add_filter(
- 'bulk_actions-edit-comments',
- function() {
- return array(
- 'delete' => 'Delete',
- 'Change State' => array(
- 'feature' => 'Featured',
- 'sale' => 'On Sale',
- ),
- );
- }
- );
- ob_start();
- $table->bulk_actions();
- $output = ob_get_clean();
- $expected = <<<'OPTIONS'
- <option value="delete">Delete</option>
- <optgroup label="Change State">
- <option value="feature">Featured</option>
- <option value="sale">On Sale</option>
- </optgroup>
- OPTIONS;
- $expected = str_replace( "\r\n", "\n", $expected );
- $this->assertContains( $expected, $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->assertContains( '?orderby=comment_author&order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
- $this->assertContains( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
- $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
- $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
- $this->assertContains( '?orderby=comment_date&order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
- $this->assertContains( '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->assertContains( '?orderby=comment_author&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
- $this->assertContains( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
- $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
- $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
- $this->assertContains( '?orderby=comment_date&order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
- $this->assertContains( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
- }
- }
|