From b849ae26853b74883da82d48b21b082586b42d56 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 3 Nov 2021 00:48:42 +0000 Subject: [PATCH] Tests: Split `WP_Posts_List_Table` and `WP_Comments_List_Table` tests into two separate files for clarity. These were previously combined in the `includesListTable.php` file. Since the tests were specific neither to the `_get_list_table()` function nor the parent `WP_List_Table` class, the naming was confusing, which should now be resolved. Follow-up to [31730], [38854], [40297], [48151], [48521], [49190], [51993]. See #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@51997 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/admin/wpCommentsListTable.php | 198 ++++++++++++++++++ ...udesListTable.php => wpPostsListTable.php} | 194 +---------------- 2 files changed, 199 insertions(+), 193 deletions(-) create mode 100644 tests/phpunit/tests/admin/wpCommentsListTable.php rename tests/phpunit/tests/admin/{includesListTable.php => wpPostsListTable.php} (53%) diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php new file mode 100644 index 0000000000..917a49c107 --- /dev/null +++ b/tests/phpunit/tests/admin/wpCommentsListTable.php @@ -0,0 +1,198 @@ +table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); + } + + /** + * @ticket 40188 + * + * @covers WP_Posts_List_Table::extra_tablenav + */ + public function test_filter_button_should_not_be_shown_if_there_are_no_comments() { + ob_start(); + $this->table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertStringNotContainsString( 'id="post-query-submit"', $output ); + } + + /** + * @ticket 40188 + * + * @covers WP_Posts_List_Table::extra_tablenav + */ + 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', + ) + ); + + $this->table->prepare_items(); + + ob_start(); + $this->table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertStringContainsString( 'id="post-query-submit"', $output ); + } + + /** + * @ticket 40188 + * + * @covers WP_Posts_List_Table::extra_tablenav + */ + 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', + ) + ); + + $this->table->prepare_items(); + + ob_start(); + $this->table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertStringContainsString( 'id="filter-by-comment-type"', $output ); + $this->assertStringContainsString( " + + + + +OPTIONS; + $expected = str_replace( "\r\n", "\n", $expected ); + + $this->assertStringContainsString( $expected, $output ); + } + + /** + * @ticket 45089 + * + * @covers WP_Posts_List_Table::print_column_headers + */ + public function test_sortable_columns() { + $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 + * + * @covers WP_Posts_List_Table::print_column_headers + */ + public function test_sortable_columns_with_current_ordering() { + $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.' ); + } + +} diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php similarity index 53% rename from tests/phpunit/tests/admin/includesListTable.php rename to tests/phpunit/tests/admin/wpPostsListTable.php index 035d645764..306fddb65d 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -3,7 +3,7 @@ /** * @group admin */ -class Tests_Admin_IncludesListTable extends WP_UnitTestCase { +class Tests_Admin_wpPostsListTable extends WP_UnitTestCase { protected static $top = array(); protected static $children = array(); protected static $grandchildren = array(); @@ -319,196 +319,4 @@ class Tests_Admin_IncludesListTable extends WP_UnitTestCase { $this->assertStringNotContainsString( 'id="delete_all"', $output ); } - /** - * @ticket 40188 - * - * @covers WP_Posts_List_Table::extra_tablenav - */ - 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 - * - * @covers WP_Posts_List_Table::extra_tablenav - */ - 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 - * - * @covers WP_Posts_List_Table::extra_tablenav - */ - 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->assertStringContainsString( 'id="filter-by-comment-type"', $output ); - $this->assertStringContainsString( " - - - - -OPTIONS; - $expected = str_replace( "\r\n", "\n", $expected ); - - $this->assertStringContainsString( $expected, $output ); - } - - /** - * @ticket 45089 - * - * @covers WP_Posts_List_Table::print_column_headers - */ - 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 - * - * @covers WP_Posts_List_Table::print_column_headers - */ - 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.' ); - } - }