diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 3fe5c337688..dcc4e014a5b 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -3706,9 +3706,12 @@ class core_message_api_testcase extends core_message_messagelib_testcase { } /** - * Tests the user can post a message. + * Test that calling to can_post_message() now shows debugging. MDL-65093. + * + * @deprecated since 3.8 + * @todo Final deprecation in MDL-66266 */ - public function test_can_post_message() { + public function test_can_post_emits_debugging() { // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3718,201 +3721,8 @@ 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(); - $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_post_message($user2)); - $this->assertDebuggingCalled(); - } - - /** - * Tests the user can't post a message without proper capability. - */ - public function test_can_post_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_post_message($user2)); - $this->assertDebuggingCalled(); - } - - /** - * Tests the user can post a message when they are contact. - */ - public function test_can_post_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_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(); - } - - /** - * Tests the user can't post a message if they are not a contact and the user - * has requested messages only from contacts. - */ - public function test_can_post_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_post_message($user2)); - $this->assertDebuggingCalled(); - } - - /** - * Tests the user can't post a message if they are blocked. - */ - public function test_can_post_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_post_message($user1, $user2)); - $this->assertDebuggingCalled(); - } - - /** - * Tests the user can post 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_post_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_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); - - // 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_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. - 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_post_message($user2)); - $this->assertDebuggingCalled(); - - // 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_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(); - } - - /** - * Tests the user with the messageanyuser capability can post a message. - */ - public function test_can_post_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_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); - $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_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(); + $this->assertDebuggingCalled('\core_message\api::can_post_message is deprecated, please use ' . + '\core_message\api::can_send_message instead.', DEBUG_DEVELOPER); } /** @@ -4068,7 +3878,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase { /** * Test verifying that users cannot send messages to conversations they are not a part of. */ - public function test_can_post_message_to_conversation_non_member() { + public function test_can_send_message_to_conversation_non_member() { // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user();