Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for hide comment bulk action dropdown based on user capability #8264

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ protected function get_views() {
protected function get_bulk_actions() {
global $comment_status;

if ( ! current_user_can( 'moderate_comments' ) ) {
return array(); // Return an empty array if the user doesn't have permission
}

$actions = array();
Comment on lines +360 to 364
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( ! current_user_can( 'moderate_comments' ) ) {
return array(); // Return an empty array if the user doesn't have permission
}
$actions = array();
$actions = array();
if ( ! current_user_can( 'moderate_comments' ) ) {
return $actions;
}

Simplify.


if ( in_array( $comment_status, array( 'all', 'approved' ), true ) ) {
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/tests/admin/wpCommentsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,53 @@ static function () {
$this->assertStringContainsString( $expected, $output );
}

/**
* @ticket 59440
*
* @covers WP_Comments_List_Table::bulk_actions
*/
public function test_bulk_action_menu_should_be_shown_if_user_has_capability() {
$u = self::factory()->user->create_and_get(
array(
'role' => 'administrator',
)
);

wp_set_current_user( $u );

$this->assertTrue( current_user_can( 'moderate_comments' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is probably a bit pointless here.


ob_start();
$this->table->bulk_actions();
$output = ob_get_clean();
Comment on lines +142 to +144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the get_echo helper function in tests.


$this->assertNotEmpty( $output );
$this->assertStringContainsString( '<option value="-1">Bulk actions</option>', $output );
}

/**
* @ticket 59440
*
* @covers WP_Comments_List_Table::bulk_actions
*/
public function test_bulk_action_menu_should_not_be_shown_if_user_has_no_capability() {
$u = self::factory()->user->create_and_get(
array(
'role' => 'subscriber',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe author is a better choice here, a bit more realistic.

)
);

wp_set_current_user( $u );

$this->assertFalse( current_user_can( 'moderate_comments' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is probably a bit pointless here.


ob_start();
$this->table->bulk_actions();
$output = ob_get_clean();
Comment on lines +166 to +168
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here about get_echo


$this->assertEmpty( $output );
}

/**
* @ticket 45089
*
Expand Down
Loading