diff --git a/message/classes/helper.php b/message/classes/helper.php index 99daf9045ae..69a705426f9 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -583,7 +583,10 @@ class helper { // Set contact and blocked status indicators. $data->iscontact = ($member->contactid) ? true : false; - $data->isblocked = ($member->blockedid) ? true : false; + + // We don't want that a user has been blocked if they can message the user anyways. + $canmessageifblocked = api::can_send_message($referenceuserid, $member->id, true); + $data->isblocked = ($member->blockedid && !$canmessageifblocked) ? true : false; $data->isdeleted = ($member->deleted) ? true : false; @@ -594,9 +597,9 @@ class helper { $privacysetting = api::get_user_privacy_messaging_preference($member->id); $data->requirescontact = $privacysetting == api::MESSAGE_PRIVACY_ONLYCONTACTS; - // Here we check that if the sender wanted to block the recipient, the recipient would - // still be able to message them regardless. - $data->canmessageevenifblocked = api::can_send_message($referenceuserid, $member->id, true); + // Here we check that if the sender wanted to block the recipient, the + // recipient would still be able to message them regardless. + $data->canmessageevenifblocked = !$data->isdeleted && $canmessageifblocked; $data->canmessage = !$data->isdeleted && api::can_send_message($member->id, $referenceuserid); } diff --git a/message/tests/behat/block_user.feature b/message/tests/behat/block_user.feature index 02fd075c433..0de9db972fd 100644 --- a/message/tests/behat/block_user.feature +++ b/message/tests/behat/block_user.feature @@ -41,3 +41,27 @@ Feature: To be able to block users that we are able to or to see a message if we And I open contact menu When I click on "Block" "link" in the "[data-region='header-container']" "css_element" Then I should see "You are unable to block Teacher 1" + + Scenario: Block a user who then gets an elevated role + Given I log in as "student1" + And I select "Student 2" user in messaging + And I open contact menu + And I click on "Block" "link" in the "[data-region='header-container']" "css_element" + And I click on "Block" "button" in the "[data-region='confirm-dialogue']" "css_element" + And I log out + And I log in as "admin" + And I am on "Course 1" course homepage + And I navigate to course participants + And I click on "Unenrol" "icon" in the "student2" "table_row" + And I click on "Unenrol" "button" in the "Unenrol" "dialogue" + And I enrol "Student 2" user as "Teacher" + And I log out + And I log in as "student2" + And I select "Student 1" user in messaging + And I should not see "You are unable to message this user" + And I log out + And I log in as "student1" + And I select "Student 2" user in messaging + And I open contact menu + When I click on "Block" "link" in the "[data-region='header-container']" "css_element" + Then I should see "You are unable to block Student 2"