MDL-50426 message: post_message capability tests

Behat and unit tests to confirm the behaviour of the
messaging lib after the capability checks were added
to the post message function.
This commit is contained in:
Ryan Wyllie 2015-10-14 05:47:36 +00:00 committed by Dan Poltawski
parent cd0c9ac87d
commit ceb689b9fc
4 changed files with 232 additions and 6 deletions

View File

@ -0,0 +1,45 @@
@core @core_message
Feature: Block users from contacting me
In order to block other users
As a user
I need to prevent specific users to sending me messages
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | One | one@example.com |
| user2 | User | Two | two@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | student |
| user2 | C1 | student |
And I log in as "user1"
And I follow "Preferences" in the user menu
And I follow "Messaging"
And I set the field "blocknoncontacts" to "1"
And I press "Save changes"
And I log out
@javascript
Scenario: Block non-contacts warning on messages page
Given I log in as "user1"
And I follow "Messages" in the user menu
And I set the field "Search people and messages" to "User Two"
And I press "Search people and messages"
When I follow "Send message to User Two"
Then I should see "User Two will not be able to reply as you have blocked non-contacts"
@javascript
Scenario: Non-contact can't send message
Given I log in as "user2"
And I follow "Messages" in the user menu
And I set the field "Search people and messages" to "User One"
And I press "Search people and messages"
When I follow "Send message to User One"
Then I should see "User One only accepts messages from their contacts."
And I follow "Picture of User One"
And I press "Message"
And I should see "User One only accepts messages from their contacts."

View File

@ -4,24 +4,41 @@ Feature: Block users from contacting me
As a user
I need to prevent specific users to sending me messages
@javascript
Scenario: Block users from contacting me with Javascript enabled
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | One | one@example.com |
| user2 | User | Two | two@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | student |
| user2 | C1 | student |
And I log in as "user1"
And I follow "Messages" in the user menu
And I set the field "Search people and messages" to "User Two"
And I press "Search people and messages"
When I click on "Block contact" "link" in the "User Two" "table_row"
And I click on "Block contact" "link" in the "User Two" "table_row"
And I log out
@javascript
Scenario: Block users display in message navigation
Given I log in as "user1"
When I follow "Messages" in the user menu
Then the "Message navigation:" select box should contain "Blocked users (1)"
And I set the field "Message navigation:" to "Blocked users (1)"
And I should see "User Two"
And I log out
And I log in as "user2"
@javascript
Scenario: Block users from contacting me
Given I log in as "user2"
And I follow "Messages" in the user menu
And I set the field "Search people and messages" to "User One"
And I press "Search people and messages"
And I follow "Send message to User One"
When I follow "Send message to User One"
Then I should see "This user has blocked you from sending messages to them"
And I follow "Picture of User One"
And I press "Message"
And I should see "This user has blocked you from sending messages to them"

View File

@ -51,3 +51,21 @@ Feature: Users can send messages to each other
And I press "Search people and messages"
And I follow "Send message to User Two"
Then I should see "Lorem ipsum sa messagus textus"
@javascript
Scenario: Using the 'Messages' page
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | student |
| user2 | C1 | student |
And I log in as "user1"
And I follow "Messages" in the user menu
And I set the field "Search people and messages" to "User Two"
And I press "Search people and messages"
And I follow "Send message to User Two"
When I set the field "message" to "Lorem ipsum sa messagus textus"
And I press "Send message"
Then I should see "Lorem ipsum sa messagus textus"

View File

@ -671,4 +671,150 @@ class core_message_messagelib_testcase extends advanced_testcase {
$this->assertEquals('Message 2', $firstmessage->smallmessage);
$this->assertEquals('Message 1', $secondmessage->smallmessage);
}
/**
* Test that message_can_post_message returns false if the sender does not have the
* moode/site:sendmessage capability.
*/
public function test_message_can_post_message_returns_false_without_capability() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:sendmessage', CAP_PROHIBIT, $roleid, $context);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver only accepts
* messages from contacts and the sender isn't a contact.
*/
public function test_message_can_post_message_returns_false_non_contact_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', true, $recipient);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver has blocked the
* sender from messaging them.
*/
public function test_message_can_post_message_returns_false_if_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$this->assertFalse(message_can_post_message($recipient, $sender));
}
/**
* Test that message_can_post_message returns false if the receiver has blocked the
* sender from messaging them.
*/
public function test_message_can_post_message_returns_true() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->assertTrue(message_can_post_message($recipient, $sender));
}
/**
* Test that message_is_user_non_contact_blocked returns false if the recipient allows
* messages from non-contacts.
*/
public function test_message_is_user_non_contact_blocked_false_without_preference() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', false, $recipient);
$this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
}
/**
* Test that message_is_user_non_contact_blocked returns true if the recipient doesn't
* allow messages from non-contacts and the sender isn't a contact.
*/
public function test_message_is_user_non_contact_blocked_true_with_preference() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
set_user_preference('message_blocknoncontacts', true, $recipient);
$this->assertTrue(message_is_user_non_contact_blocked($recipient, $sender));
}
/**
* Test that message_is_user_non_contact_blocked returns false if the recipient doesn't
* allow messages from non-contacts but the sender is a contact.
*/
public function test_message_is_user_non_contact_blocked_false_with_if_contact() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
set_user_preference('message_blocknoncontacts', true, $recipient);
message_add_contact($sender->id);
$this->assertFalse(message_is_user_non_contact_blocked($recipient, $sender));
}
/**
* Test that message_is_user_blocked returns false if the sender is not a contact of
* the recipient.
*/
public function test_message_is_user_blocked_false_no_contact() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->assertFalse(message_is_user_blocked($recipient, $sender));
}
/**
* Test that message_is_user_blocked returns false if the sender is a contact that is
* blocked by the recipient but has the moodle/site:readallmessages capability.
*/
public function test_message_is_user_blocked_false_if_readallmessages() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:readallmessages', CAP_ALLOW, $roleid, $context);
$this->assertFalse(message_is_user_blocked($recipient, $sender));
}
/**
* Test that message_is_user_blocked returns true if the sender is a contact that is
* blocked by the recipient and does not have the moodle/site:readallmessages capability.
*/
public function test_message_is_user_blocked_true_if_blocked() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->setUser($recipient);
message_block_contact($sender->id);
$context = context_system::instance();
$roleid = $this->getDataGenerator()->create_role();
$this->getDataGenerator()->role_assign($roleid, $sender->id, $context->id);
assign_capability('moodle/site:readallmessages', CAP_PROHIBIT, $roleid, $context);
$this->assertTrue(message_is_user_blocked($recipient, $sender));
}
}