From 09ec50178161574928d89f3074e53a5740a052d5 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 15 Oct 2018 17:28:52 +0800 Subject: [PATCH] MDL-63548 core_message: added mark_all_conversation_messages_as_read WS --- lib/db/services.php | 9 +++ message/externallib.php | 60 ++++++++++++++ message/tests/externallib_test.php | 122 +++++++++++++++++++++++++++++ version.php | 2 +- 4 files changed, 192 insertions(+), 1 deletion(-) diff --git a/lib/db/services.php b/lib/db/services.php index 4265c1413fe..3b40f3efa28 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1065,6 +1065,15 @@ $functions = array( 'ajax' => true, 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_message_mark_all_conversation_messages_as_read' => array( + 'classname' => 'core_message_external', + 'methodname' => 'mark_all_conversation_messages_as_read', + 'classpath' => 'message/externallib.php', + 'description' => 'Mark all conversation messages as read for a given user', + 'type' => 'write', + 'ajax' => true, + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), 'core_message_mark_message_read' => array( 'classname' => 'core_message_external', 'methodname' => 'mark_message_read', diff --git a/message/externallib.php b/message/externallib.php index 1dba7ed5b18..f201e836faf 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -2513,6 +2513,66 @@ class core_message_external extends external_api { return true; } + /** + * Mark all conversation messages as read parameters description. + * + * @return external_function_parameters + * @since 3.6 + */ + public static function mark_all_conversation_messages_as_read_parameters() { + return new external_function_parameters( + array( + 'userid' => new external_value(PARAM_INT, 'The user id who who we are marking the messages as read for'), + 'conversationid' => + new external_value(PARAM_INT, 'The conversation id who who we are marking the messages as read for') + ) + ); + } + + /** + * Mark all conversation messages as read function. + * + * @param int $userid The user id of who we want to delete the conversation for + * @param int $conversationid The id of the conversations + * @since 3.6 + */ + public static function mark_all_conversation_messages_as_read(int $userid, int $conversationid) { + global $CFG; + + // Check if messaging is enabled. + if (empty($CFG->messaging)) { + throw new moodle_exception('disabled', 'message'); + } + + $params = array( + 'userid' => $userid, + 'conversationid' => $conversationid, + ); + $params = self::validate_parameters(self::mark_all_conversation_messages_as_read_parameters(), $params); + + $context = context_system::instance(); + self::validate_context($context); + + $user = core_user::get_user($params['userid'], '*', MUST_EXIST); + core_user::require_active_user($user); + + if (\core_message\api::can_mark_all_messages_as_read($userid, $conversationid)) { + \core_message\api::mark_all_messages_as_read($userid, $conversationid); + } else { + throw new moodle_exception('accessdenied', 'admin'); + } + } + + /** + * Mark all conversation messages as read return description. + * + * @return external_warnings + * @since 3.6 + */ + public static function mark_all_conversation_messages_as_read_returns() { + return new external_warnings(); + } + /** * Returns description of method parameters. * diff --git a/message/tests/externallib_test.php b/message/tests/externallib_test.php index aa7f61f8d52..505d7553230 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -3245,6 +3245,128 @@ class core_message_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals(6, $DB->count_records('message_user_actions')); } + /** + * Test marking all conversation messages as read with an invalid user. + */ + public function test_mark_all_conversation_messages_as_read_invalid_user_exception() { + $this->resetAfterTest(true); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $this->send_message($user1, $user2, 'Yo!', 0, $time); + $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + $this->expectException('moodle_exception'); + core_message_external::mark_all_conversation_messages_as_read(-2132131, $conversationid); + } + + /** + * Test marking all conversation messages as read without proper access. + */ + public function test_mark_all_conversation_messages_as_read_access_denied_exception() { + $this->resetAfterTest(true); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + $user3 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $this->send_message($user1, $user2, 'Yo!', 0, $time); + $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // User 3 is not in the conversation. + $this->expectException('moodle_exception'); + core_message_external::mark_all_conversation_messages_as_read($user3->id, $conversationid); + } + + /** + * Test marking all conversation messages as read for another user. + */ + public function test_mark_all_conversation_messages_as_read_wrong_user() { + $this->resetAfterTest(true); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $this->send_message($user1, $user2, 'Yo!', 0, $time); + $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // Can't mark the messages as read for user 2. + $this->setUser($user1); + $this->expectException('moodle_exception'); + core_message_external::mark_all_conversation_messages_as_read($user2->id, $conversationid); + } + + /** + * Test marking all conversation messages as admin. + */ + public function test_mark_all_conversation_messages_as_admin() { + global $DB; + + $this->resetAfterTest(true); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $this->send_message($user1, $user2, 'Yo!', 0, $time); + $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // Admin can do anything. + $this->setAdminUser(); + core_message_external::mark_all_conversation_messages_as_read($user2->id, $conversationid); + $this->assertEquals(2, $DB->count_records('message_user_actions')); + } + + /** + * Test marking all conversation messages. + */ + public function test_mark_all_conversation_messages_as_read() { + global $DB; + + $this->resetAfterTest(true); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $this->send_message($user1, $user2, 'Yo!', 0, $time); + $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // We are the user we want to mark the messages for and we are in the conversation, all good. + $this->setUser($user1); + core_message_external::mark_all_conversation_messages_as_read($user1->id, $conversationid); + $this->assertEquals(2, $DB->count_records('message_user_actions')); + } + /** * Test getting unread conversation count. */ diff --git a/version.php b/version.php index d10fd0eef92..82f0af37444 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018101900.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018101900.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.