Merge branch 'MDL-65093_master-deprecate' of git://github.com/markn86/moodle

This commit is contained in:
Adrian Greeve 2019-07-29 10:43:52 +08:00
commit 9f236d63e6
23 changed files with 481 additions and 33 deletions

View File

@ -39,6 +39,7 @@ $string['blockuserconfirm'] = 'Are you sure you want to block {$a}?';
$string['blockuserconfirmbutton'] = 'Block';
$string['blocknoncontacts'] = 'Prevent non-contacts from messaging me';
$string['cancelselection'] = 'Cancel message selection';
$string['cantblockuser'] = 'You are unable to block {$a} because they have a role with permission to message all users';
$string['contactableprivacy'] = 'Accept messages from:';
$string['contactableprivacy_onlycontacts'] = 'My contacts only';
$string['contactableprivacy_coursemember'] = 'My contacts and anyone in my courses';

View File

@ -2412,7 +2412,7 @@ function message_page_type_list() {
*/
function message_can_post_message() {
throw new coding_exception('message_can_post_message() can not be used anymore. Please use ' .
'\core_message\api::can_post_message() instead.');
'\core_message\api::can_send_message() instead.');
}
/**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -182,7 +182,8 @@ function(
isblocked: null,
iscontact: null,
isdeleted: null,
canmessage: null,
canmessage: null,
canmessageevenifblocked: null,
requirescontact: null,
contactrequests: []
};
@ -1544,6 +1545,7 @@ function(
[SELECTORS.ACTION_CONFIRM_REMOVE_CONTACT, generateConfirmActionHandler(removeContact)],
[SELECTORS.ACTION_CONFIRM_DELETE_SELECTED_MESSAGES, generateConfirmActionHandler(deleteSelectedMessages)],
[SELECTORS.ACTION_CONFIRM_DELETE_CONVERSATION, generateConfirmActionHandler(deleteConversation)],
[SELECTORS.ACTION_OKAY_CONFIRM, generateConfirmActionHandler(cancelRequest)],
[SELECTORS.ACTION_REQUEST_ADD_CONTACT, generateConfirmActionHandler(requestAddContact)],
[SELECTORS.ACTION_ACCEPT_CONTACT_REQUEST, generateConfirmActionHandler(acceptContactRequest)],
[SELECTORS.ACTION_DECLINE_CONTACT_REQUEST, generateConfirmActionHandler(declineContactRequest)],

View File

@ -25,6 +25,7 @@ define([], function() {
var SELECTORS = {
ACTION_ACCEPT_CONTACT_REQUEST: '[data-action="accept-contact-request"]',
ACTION_CANCEL_CONFIRM: '[data-action="cancel-confirm"]',
ACTION_OKAY_CONFIRM: '[data-action="okay-confirm"]',
ACTION_CANCEL_EDIT_MODE: '[data-action="cancel-edit-mode"]',
ACTION_CONFIRM_ADD_CONTACT: '[data-action="confirm-add-contact"]',
ACTION_CONFIRM_BLOCK: '[data-action="confirm-block"]',
@ -50,6 +51,7 @@ define([], function() {
CONFIRM_DIALOGUE_CANCEL_BUTTON: '[data-action="cancel-confirm"]',
CONFIRM_DIALOGUE_CONTAINER: '[data-region="confirm-dialogue-container"]',
CONFIRM_DIALOGUE_HEADER: '[data-region="dialogue-header"]',
CONFIRM_DIALOGUE_OKAY_BUTTON: '[data-action="okay-confirm"]',
CONFIRM_DIALOGUE_TEXT: '[data-region="dialogue-text"]',
CONTACT_REQUEST_SENT_MESSAGE_CONTAINER: '[data-region="contact-request-sent-message-container"]',
CONTENT_PLACEHOLDER_CONTAINER: '[data-region="content-placeholder"]',

View File

@ -981,6 +981,7 @@ function(
* @param {String} headerText Text to show in dialogue header.
* @param {Bool} canCancel Can this dialogue be cancelled.
* @param {Bool} skipHeader Skip blanking out the header
* @param {Bool} showOk Show an 'Okay' button for a dialogue which will close it
*/
var showConfirmDialogue = function(
header,
@ -990,13 +991,15 @@ function(
bodyText,
headerText,
canCancel,
skipHeader
skipHeader,
showOk
) {
var dialogue = getConfirmDialogueContainer(body);
var buttons = buttonSelectors.map(function(selector) {
return dialogue.find(selector);
});
var cancelButton = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_CANCEL_BUTTON);
var okayButton = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_OKAY_BUTTON);
var text = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_TEXT);
var dialogueHeader = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_HEADER);
@ -1008,6 +1011,12 @@ function(
cancelButton.addClass('hidden');
}
if (showOk) {
okayButton.removeClass('hidden');
} else {
okayButton.addClass('hidden');
}
if (headerText) {
dialogueHeader.removeClass('hidden');
dialogueHeader.text(headerText);
@ -1041,6 +1050,7 @@ function(
var hideConfirmDialogue = function(header, body, footer) {
var dialogue = getConfirmDialogueContainer(body);
var cancelButton = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_CANCEL_BUTTON);
var okayButton = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_OKAY_BUTTON);
var text = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_TEXT);
var dialogueHeader = dialogue.find(SELECTORS.CONFIRM_DIALOGUE_HEADER);
@ -1050,6 +1060,7 @@ function(
hideConfirmDialogueContainer(header);
dialogue.find('button').addClass('hidden');
cancelButton.removeClass('hidden');
okayButton.removeClass('hidden');
text.text('');
dialogueHeader.addClass('hidden');
dialogueHeader.text('');
@ -1069,10 +1080,17 @@ function(
*/
var renderConfirmBlockUser = function(header, body, footer, user) {
if (user) {
return Str.get_string('blockuserconfirm', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [SELECTORS.ACTION_CONFIRM_BLOCK], string, '', true, false);
});
if (user.canmessageevenifblocked) {
return Str.get_string('cantblockuser', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [], string, '', false, false, true);
});
} else {
return Str.get_string('blockuserconfirm', 'core_message', user.fullname)
.then(function(string) {
return showConfirmDialogue(header, body, footer, [SELECTORS.ACTION_CONFIRM_BLOCK], string, '', true, false);
});
}
} else {
return hideConfirmDialogue(header, body, footer);
}

View File

@ -88,7 +88,8 @@ define(['jquery'], function($) {
isblocked: member.isblocked,
iscontact: member.iscontact,
isdeleted: member.isdeleted,
canmessage: member.canmessage,
canmessage: member.canmessage,
canmessageevenifblocked: member.canmessageevenifblocked,
requirescontact: member.requirescontact,
contactrequests: member.contactrequests || []
};

View File

@ -1844,6 +1844,8 @@ class api {
* Determines if a user is permitted to send another user a private message.
* If no sender is provided then it defaults to the logged in user.
*
* @deprecated since 3.8
* @todo Final deprecation in MDL-66266
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if user is permitted, false otherwise.
@ -1851,22 +1853,39 @@ class api {
public static function can_post_message($recipient, $sender = null) {
global $USER;
debugging('\core_message\api::can_post_message is deprecated, please use ' .
'\core_message\api::can_send_message instead.', DEBUG_DEVELOPER);
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
return self::can_send_message($recipient->id, $sender->id);
}
/**
* Determines if a user is permitted to send another user a private message.
*
* @param int $recipientid The recipient user id.
* @param int $senderid The sender user id.
* @param bool $evenifblocked This lets the user know, that even if the recipient has blocked the user
* the user is still able to send a message.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_send_message(int $recipientid, int $senderid, bool $evenifblocked = false) : bool {
$systemcontext = \context_system::instance();
if (!has_capability('moodle/site:sendmessage', $systemcontext, $sender)) {
if (!has_capability('moodle/site:sendmessage', $systemcontext, $senderid)) {
return false;
}
if (has_capability('moodle/site:readallmessages', $systemcontext, $sender->id)) {
if (has_capability('moodle/site:readallmessages', $systemcontext, $senderid)) {
return true;
}
// Check if the recipient can be messaged by the sender.
return (self::can_contact_user($recipient->id, $sender->id));
return self::can_contact_user($recipientid, $senderid, $evenifblocked);
}
/**
@ -2949,9 +2968,11 @@ class api {
*
* @param int $recipientid
* @param int $senderid
* @param bool $evenifblocked This lets the user know, that even if the recipient has blocked the user
* the user is still able to send a message.
* @return bool true if recipient hasn't blocked sender and sender can contact to recipient, false otherwise.
*/
protected static function can_contact_user(int $recipientid, int $senderid) : bool {
protected static function can_contact_user(int $recipientid, int $senderid, bool $evenifblocked = false) : bool {
if (has_capability('moodle/site:messageanyuser', \context_system::instance(), $senderid) ||
$recipientid == $senderid) {
// The sender has the ability to contact any user across the entire site or themselves.
@ -2961,7 +2982,7 @@ class api {
// The initial value of $cancontact is null to indicate that a value has not been determined.
$cancontact = null;
if (self::is_blocked($recipientid, $senderid)) {
if (self::is_blocked($recipientid, $senderid) || $evenifblocked) {
// The recipient has specifically blocked this sender.
$cancontact = false;
}

View File

@ -583,23 +583,24 @@ 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;
$data->requirescontact = null;
$data->canmessage = null;
$data->canmessageevenifblocked = null;
if ($includeprivacyinfo) {
$privacysetting = api::get_user_privacy_messaging_preference($member->id);
$data->requirescontact = $privacysetting == api::MESSAGE_PRIVACY_ONLYCONTACTS;
$recipient = new \stdClass();
$recipient->id = $member->id;
$sender = new \stdClass();
$sender->id = $referenceuserid;
$data->canmessage = !$data->isdeleted && api::can_post_message($recipient, $sender);
// 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);
}
// Populate the contact requests, even if we don't need them.

View File

@ -189,7 +189,7 @@ class core_message_external extends external_api {
// TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
// Check if the recipient can be messaged by the sender.
if ($success && !\core_message\api::can_post_message($tousers[$message['touserid']], $USER)) {
if ($success && !\core_message\api::can_send_message($tousers[$message['touserid']]->id, $USER->id)) {
$success = false;
$errormessage = get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message['touserid'])));
}
@ -583,6 +583,11 @@ class core_message_external extends external_api {
throw new required_capability_exception($context, $capability, 'nopermissions', '');
}
// If the blocking is going to be useless then don't do it.
if (\core_message\api::can_send_message($userid, $blockeduserid, true)) {
return [];
}
if (!\core_message\api::is_blocked($params['userid'], $params['blockeduserid'])) {
\core_message\api::block_user($params['userid'], $params['blockeduserid']);
}
@ -1290,6 +1295,8 @@ class core_message_external extends external_api {
'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?'),
'isdeleted' => new external_value(PARAM_BOOL, 'Is the user deleted?'),
'canmessageevenifblocked' => new external_value(PARAM_BOOL,
'If the user can still message even if they get blocked'),
'canmessage' => new external_value(PARAM_BOOL, 'If the user can be messaged'),
'requirescontact' => new external_value(PARAM_BOOL, 'If the user requires to be contacts'),
];

View File

@ -57,9 +57,7 @@ if ($userid) {
}
if ($userid) {
$recipient = new stdClass();
$recipient->id = $userid;
if (!\core_message\api::can_post_message($recipient)) {
if (!\core_message\api::can_send_message($userid, $USER->id)) {
throw new moodle_exception('Can not contact user');
}
}

View File

@ -80,5 +80,6 @@
<span data-region="dialogue-button-text">{{#str}} decline, core_message {{/str}}</span>
<span class="hidden" data-region="loading-icon-container">{{> core/loading }}</span>
</button>
<button type="button" class="btn btn-primary btn-block" data-action="okay-confirm">{{#str}} ok, core {{/str}}</button>
<button type="button" class="btn btn-secondary btn-block" data-action="cancel-confirm">{{#str}} cancel, core {{/str}}</button>
</div>

View File

@ -3418,6 +3418,293 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']);
}
/**
* Tests the user can send a message.
*/
public function test_can_send_message() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the first user.
$this->setUser($user1);
// With the default privacy setting, users can't message them.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
// Enrol users to the same course.
$course = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// After enrolling users to the course, they should be able to message them with the default privacy setting.
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id));
}
/**
* Tests the user can't send a message without proper capability.
*/
public function test_can_send_message_without_sendmessage_cap() {
global $DB;
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the user 1.
$this->setUser($user1);
// Remove the capability to send a message.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
unassign_capability('moodle/site:sendmessage', $roleids['user'],
context_system::instance());
// Check that we can not post a message without the capability.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
}
/**
* Tests the user can send a message when they are contact.
*/
public function test_can_send_message_when_contact() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the first user.
$this->setUser($user1);
// Check that we can not send user2 a message.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
// Add users as contacts.
\core_message\api::add_contact($user1->id, $user2->id);
// Check that the return result is now true.
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id));
}
/**
* Tests the user can't send a message if they are not a contact and the user
* has requested messages only from contacts.
*/
public function test_can_send_message_when_not_contact() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the first user.
$this->setUser($user1);
// Set the second user's preference to not receive messages from non-contacts.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
// Check that we can not send user 2 a message.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
}
/**
* Tests the user can't send a message if they are blocked.
*/
public function test_can_send_message_when_blocked() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set the user.
$this->setUser($user1);
// Block the second user.
\core_message\api::block_user($user1->id, $user2->id);
// Check that the second user can no longer send the first user a message.
$this->assertFalse(\core_message\api::can_send_message($user1->id, $user2->id));
}
/**
* Tests the user can send a message when site-wide messaging setting is enabled,
* even if they are not a contact and are not members of the same course.
*/
public function test_can_send_message_site_messaging_setting() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set as the first user.
$this->setUser($user1);
// By default, user only can be messaged by contacts and members of any of his/her courses.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
// Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
set_config('messagingallusers', true);
// Set the second user's preference to receive messages from everybody.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
// Check that we can send user2 a message.
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id));
// Disable site-wide messagging privacy setting. The user will be able to receive messages from contacts
// and members sharing a course with her.
set_config('messagingallusers', false);
// As site-wide messaging setting is disabled, the value for user2 will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
// Enrol users to the same course.
$course = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// Check that we can send user2 a message because they are sharing a course.
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id));
// Set the second user's preference to receive messages only from contacts.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
// Check that now the user2 can't be contacted because user1 is not their contact.
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id));
// Make contacts user1 and user2.
\core_message\api::add_contact($user2->id, $user1->id);
// Check that we can send user2 a message because they are contacts.
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id));
}
/**
* Tests the user with the messageanyuser capability can send a message.
*/
public function test_can_send_message_with_messageanyuser_cap() {
global $DB;
// Create some users.
$teacher1 = self::getDataGenerator()->create_user();
$student1 = self::getDataGenerator()->create_user();
$student2 = self::getDataGenerator()->create_user();
// Create users not enrolled in any course.
$user1 = self::getDataGenerator()->create_user();
// Create a course.
$course1 = $this->getDataGenerator()->create_course();
// Enrol the users in the course.
$this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, 'editingteacher');
$this->getDataGenerator()->enrol_user($student1->id, $course1->id, 'student');
$this->getDataGenerator()->enrol_user($student2->id, $course1->id, 'student');
// Set some student preferences to not receive messages from non-contacts.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $student1->id);
// Check that we can send student1 a message because teacher has the messageanyuser cap by default.
$this->assertTrue(\core_message\api::can_send_message($student1->id, $teacher1->id));
// Check that the teacher can't contact user1 because it's not his teacher.
$this->assertFalse(\core_message\api::can_send_message($user1->id, $teacher1->id));
// Remove the messageanyuser capability from the course1 for teachers.
$coursecontext = context_course::instance($course1->id);
$teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
assign_capability('moodle/site:messageanyuser', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id);
$coursecontext->mark_dirty();
// Check that we can't send user1 a message because they are not contacts.
$this->assertFalse(\core_message\api::can_send_message($student1->id, $teacher1->id));
// However, teacher can message student2 because they are sharing a course.
$this->assertTrue(\core_message\api::can_send_message($student2->id, $teacher1->id));
}
/**
* Tests the user when blocked will not be able to send messages if they are blocked.
*/
public function test_can_send_message_even_if_blocked() {
$this->resetAfterTest();
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id, true));
}
/**
* Tests the user will be able to send a message even if they are blocked as the user
* has the capability 'moodle/site:messageanyuser'.
*/
public function test_can_send_message_even_if_blocked_with_message_any_user_cap() {
global $DB;
$this->resetAfterTest();
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$authenticateduserrole = $DB->get_record('role', array('shortname' => 'user'));
assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduserrole->id, context_system::instance(), true);
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id, true));
}
/**
* Tests the user will be able to send a message even if they are blocked as the user
* has the capability 'moodle/site:readallmessages'.
*/
public function test_can_send_message_even_if_blocked_with_read_all_message_cap() {
global $DB;
$this->resetAfterTest();
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$authenticateduserrole = $DB->get_record('role', array('shortname' => 'user'));
assign_capability('moodle/site:readallmessages', CAP_ALLOW, $authenticateduserrole->id, context_system::instance(), true);
$this->assertTrue(\core_message\api::can_send_message($user2->id, $user1->id, true));
}
/**
* Tests the user can not always send a message if they are blocked just because they share a course.
*/
public function test_can_send_message_even_if_blocked_shared_course() {
$this->resetAfterTest();
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$course = self::getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$this->assertFalse(\core_message\api::can_send_message($user2->id, $user1->id, true));
}
/**
* Tests the user can always send a message even if they are blocked because they share a course and
* have the capability 'moodle/site:messageanyuser' at the course context.
*/
public function test_can_send_message_even_if_blocked_shared_course_with_message_any_user_cap() {
global $DB;
$this->resetAfterTest();
$editingteacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$teacher = self::getDataGenerator()->create_user();
$student = self::getDataGenerator()->create_user();
$course = self::getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $editingteacherrole->id);
$this->getDataGenerator()->enrol_user($student->id, $course->id);
assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $editingteacherrole->id,
context_course::instance($course->id), true);
// Check that the second user can no longer send the first user a message.
$this->assertTrue(\core_message\api::can_send_message($student->id, $teacher->id, true));
}
/**
* Tests the user can post a message.
*/
@ -3431,6 +3718,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// With the default privacy setting, users can't message them.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Enrol users to the same course.
$course = $this->getDataGenerator()->create_course();
@ -3438,6 +3726,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// After enrolling users to the course, they should be able to message them with the default privacy setting.
$this->assertTrue(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
}
/**
@ -3460,6 +3749,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can not post a message without the capability.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
}
/**
@ -3475,12 +3765,14 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can not send user2 a message.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Add users as contacts.
\core_message\api::add_contact($user1->id, $user2->id);
// Check that the return result is now true.
$this->assertTrue(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
}
/**
@ -3500,6 +3792,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can not send user 2 a message.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
}
/**
@ -3518,6 +3811,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that the second user can no longer send the first user a message.
$this->assertFalse(\core_message\api::can_post_message($user1, $user2));
$this->assertDebuggingCalled();
}
/**
@ -3534,6 +3828,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// By default, user only can be messaged by contacts and members of any of his/her courses.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
set_config('messagingallusers', true);
@ -3543,6 +3838,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can send user2 a message.
$this->assertTrue(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Disable site-wide messagging privacy setting. The user will be able to receive messages from contacts
// and members sharing a course with her.
@ -3550,6 +3846,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// As site-wide messaging setting is disabled, the value for user2 will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Enrol users to the same course.
$course = $this->getDataGenerator()->create_course();
@ -3557,16 +3854,19 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// Check that we can send user2 a message because they are sharing a course.
$this->assertTrue(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Set the second user's preference to receive messages only from contacts.
set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
// Check that now the user2 can't be contacted because user1 is not their contact.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
// Make contacts user1 and user2.
\core_message\api::add_contact($user2->id, $user1->id);
// Check that we can send user2 a message because they are contacts.
$this->assertTrue(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled();
}
/**
@ -3596,8 +3896,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can send student1 a message because teacher has the messageanyuser cap by default.
$this->assertTrue(\core_message\api::can_post_message($student1, $teacher1));
$this->assertDebuggingCalled();
// Check that the teacher can't contact user1 because it's not his teacher.
$this->assertFalse(\core_message\api::can_post_message($user1, $teacher1));
$this->assertDebuggingCalled();
// Remove the messageanyuser capability from the course1 for teachers.
$coursecontext = context_course::instance($course1->id);
@ -3607,8 +3909,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
// Check that we can't send user1 a message because they are not contacts.
$this->assertFalse(\core_message\api::can_post_message($student1, $teacher1));
$this->assertDebuggingCalled();
// However, teacher can message student2 because they are sharing a course.
$this->assertTrue(\core_message\api::can_post_message($student2, $teacher1));
$this->assertDebuggingCalled();
}
/**

View File

@ -0,0 +1,67 @@
@core @core_message @javascript
Feature: To be able to block users that we are able to or to see a message if we can not
In order to attempt to block a user
As a user
I need to be able to block a user or to see a message if we can not
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@emample.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | teacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following config values are set as admin:
| messaging | 1 |
Scenario: Block a user
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 should see "Are you sure you want to block Student 2?"
And I click on "Block" "button" in the "[data-region='confirm-dialogue']" "css_element"
And I should see "You have blocked this user."
And I log out
When I log in as "student2"
And I open messaging
And I select "Student 1" user in messaging
Then I should see "You are unable to message this user"
Scenario: Unable to block a user
Given I log in as "student1"
And I select "Teacher 1" 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 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"

View File

@ -1216,6 +1216,30 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals(1, $DB->count_records('message_users_blocked'));
}
/**
* Test blocking a user.
*/
public function test_block_user_when_ineffective() {
global $DB;
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$this->setUser($user1);
$authenticateduser = $DB->get_record('role', array('shortname' => 'user'));
assign_capability('moodle/site:messageanyuser', CAP_ALLOW, $authenticateduser->id, context_system::instance(), true);
// Blocking a user.
$return = core_message_external::block_user($user1->id, $user2->id);
$return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return);
$this->assertEquals(array(), $return);
$this->assertEquals(0, $DB->count_records('message_users_blocked'));
}
/**
* Test blocking a user with messaging disabled.
*/

View File

@ -5,6 +5,7 @@ information provided here is intended especially for developers.
* The following methods have been deprecated and should not be used any more:
- \core_message\api::get_individual_conversations_between_users()
- \core_message\api::can_post_message()
=== 3.7 ===