From 26f39c88618cecca0107235b27bb74d38bc3fe10 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 15 Oct 2018 17:43:21 +0800 Subject: [PATCH] MDL-63547 core_message: added delete_conversation_by_id web service --- lib/db/services.php | 10 ++ message/externallib.php | 65 +++++++++++ message/tests/externallib_test.php | 167 +++++++++++++++++++++++++++++ version.php | 2 +- 4 files changed, 243 insertions(+), 1 deletion(-) diff --git a/lib/db/services.php b/lib/db/services.php index 36bdc0794f4..033de846610 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -924,6 +924,16 @@ $functions = array( 'ajax' => true, 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_message_delete_conversation_by_id' => array( + 'classname' => 'core_message_external', + 'methodname' => 'delete_conversation_by_id', + 'classpath' => 'message/externallib.php', + 'description' => 'Deletes a conversation.', + 'type' => 'write', + 'capabilities' => 'moodle/site:deleteownmessage', + 'ajax' => true, + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), 'core_message_delete_message' => array( 'classname' => 'core_message_external', 'methodname' => 'delete_message', diff --git a/message/externallib.php b/message/externallib.php index e5073b5296e..a8d0f0c167a 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -2633,6 +2633,71 @@ class core_message_external extends external_api { return true; } + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since 3.6 + */ + public static function delete_conversation_by_id_parameters() { + return new external_function_parameters( + array( + 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'), + 'conversationid' => new external_value(PARAM_INT, 'The id of the conversation'), + ) + ); + } + + /** + * Deletes a conversation. + * + * @param int $userid The user id of who we want to delete the conversation for + * @param int $conversationid The id of the conversation + * @return array + * @throws moodle_exception + * @since 3.6 + */ + public static function delete_conversation_by_id($userid, $conversationid) { + global $CFG; + + // Check if private messaging between users is allowed. + if (empty($CFG->messaging)) { + throw new moodle_exception('disabled', 'message'); + } + + // Validate params. + $params = [ + 'userid' => $userid, + 'conversationid' => $conversationid, + ]; + $params = self::validate_parameters(self::delete_conversation_by_id_parameters(), $params); + + // Validate context. + $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_delete_conversation($user->id, $conversationid)) { + \core_message\api::delete_conversation_by_id($user->id, $conversationid); + } else { + throw new moodle_exception("You do not have permission to delete the conversation '$conversationid'"); + } + + return []; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since 3.6 + */ + public static function delete_conversation_by_id_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 584f15685ca..92dae3f1948 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -3483,6 +3483,173 @@ class core_message_externallib_testcase extends externallib_advanced_testcase { core_message_external::delete_conversation($user1->id, $user2->id); } + /** + * Test deleting conversation. + */ + public function test_delete_conversation_by_id() { + global $DB; + + $this->resetAfterTest(true); + + // Create some users. + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // The person wanting to delete the conversation. + $this->setUser($user1); + + // Send some messages back and forth. + $time = time(); + $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); + $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // Delete the conversation. + core_message_external::delete_conversation_by_id($user1->id, $conversationid); + + $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); + $this->assertCount(4, $muas); + // Sort by id. + ksort($muas); + + $mua1 = array_shift($muas); + $mua2 = array_shift($muas); + $mua3 = array_shift($muas); + $mua4 = array_shift($muas); + + $this->assertEquals($user1->id, $mua1->userid); + $this->assertEquals($m1id, $mua1->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); + + $this->assertEquals($user1->id, $mua2->userid); + $this->assertEquals($m2id, $mua2->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); + + $this->assertEquals($user1->id, $mua3->userid); + $this->assertEquals($m3id, $mua3->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); + + $this->assertEquals($user1->id, $mua4->userid); + $this->assertEquals($m4id, $mua4->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); + } + + /** + * Test deleting conversation as other user. + */ + public function test_delete_conversation_by_id_as_other_user() { + global $DB; + + $this->resetAfterTest(true); + + $this->setAdminUser(); + + // Create some users. + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + // Send some messages back and forth. + $time = time(); + $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); + $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); + $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); + $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); + + $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); + + // Delete the conversation. + core_message_external::delete_conversation_by_id($user1->id, $conversationid); + + $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); + $this->assertCount(4, $muas); + // Sort by id. + ksort($muas); + + $mua1 = array_shift($muas); + $mua2 = array_shift($muas); + $mua3 = array_shift($muas); + $mua4 = array_shift($muas); + + $this->assertEquals($user1->id, $mua1->userid); + $this->assertEquals($m1id, $mua1->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); + + $this->assertEquals($user1->id, $mua2->userid); + $this->assertEquals($m2id, $mua2->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); + + $this->assertEquals($user1->id, $mua3->userid); + $this->assertEquals($m3id, $mua3->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); + + $this->assertEquals($user1->id, $mua4->userid); + $this->assertEquals($m4id, $mua4->messageid); + $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); + } + + /** + * Test deleting conversation as other user without proper capability. + */ + public function test_delete_conversation_by_id_as_other_user_without_cap() { + $this->resetAfterTest(true); + + // Create some users. + $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]); + + // The person wanting to delete the conversation. + $this->setUser($user3); + + // Ensure an exception is thrown. + $this->expectException('moodle_exception'); + core_message_external::delete_conversation_by_id($user1->id, $conversationid); + } + + /** + * Test deleting conversation with messaging disabled. + */ + public function test_delete_conversation_by_id_messaging_disabled() { + global $CFG; + + $this->resetAfterTest(true); + + // Create some users. + $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]); + + // The person wanting to delete the conversation. + $this->setUser($user1); + + // Disable messaging. + $CFG->messaging = 0; + + // Ensure an exception is thrown. + $this->expectException('moodle_exception'); + core_message_external::delete_conversation_by_id($user1->id, $conversationid); + } + /** * Test get message processor. */ diff --git a/version.php b/version.php index 7f3abd4d453..4627b7a75af 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018101100.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018101100.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -- 2.43.0