MDL-63547 core_message: deprecated api::delete_conversation()
authorMark Nelson <markn@moodle.com>
Sun, 14 Oct 2018 06:03:03 +0000 (14:03 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 17 Oct 2018 04:05:16 +0000 (12:05 +0800)
message/classes/api.php
message/externallib.php
message/tests/api_test.php
message/tests/events_test.php
message/upgrade.txt

index 4320e25..f4e260e 100644 (file)
@@ -650,13 +650,15 @@ class api {
      *
      * This function does not verify any permissions.
      *
+     * @deprecated since 3.6
      * @param int $userid The user id of who we want to delete the messages for (this may be done by the admin
      *  but will still seem as if it was by the user)
      * @param int $otheruserid The id of the other user in the conversation
      * @return bool
      */
     public static function delete_conversation($userid, $otheruserid) {
-        global $DB, $USER;
+        debugging('\core_message\api::delete_conversation() is deprecated, please use ' .
+            '\core_message\api::delete_conversation_by_id() instead.', DEBUG_DEVELOPER);
 
         $conversationid = self::get_conversation_between_users([$userid, $otheruserid]);
 
@@ -665,6 +667,23 @@ class api {
             return true;
         }
 
+        self::delete_conversation_by_id($userid, $conversationid);
+
+        return true;
+    }
+
+    /**
+     * Deletes a conversation for a specified user.
+     *
+     * This function does not verify any permissions.
+     *
+     * @param int $userid The user id of who we want to delete the messages for (this may be done by the admin
+     *  but will still seem as if it was by the user)
+     * @param int $conversationid The id of the other user in the conversation
+     */
+    public static function delete_conversation_by_id(int $userid, int $conversationid) {
+        global $DB, $USER;
+
         // Get all messages belonging to this conversation that have not already been deleted by this user.
         $sql = "SELECT m.*
                  FROM {messages} m
@@ -689,8 +708,6 @@ class api {
             \core\event\message_deleted::create_from_ids($userid, $USER->id,
                 $message->id, $mua->id)->trigger();
         }
-
-        return true;
     }
 
     /**
index f678c2f..30f056b 100644 (file)
@@ -2588,7 +2588,10 @@ class core_message_external extends external_api {
         core_user::require_active_user($user);
 
         if (\core_message\api::can_delete_conversation($user->id)) {
-            $status = \core_message\api::delete_conversation($user->id, $otheruserid);
+            if ($conversationid = \core_message\api::get_conversation_between_users([$userid, $otheruserid])) {
+                \core_message\api::delete_conversation_by_id($user->id, $conversationid);
+            }
+            $status = true;
         } else {
             throw new moodle_exception('You do not have permission to delete messages');
         }
index f9f1cfc..ed4a272 100644 (file)
@@ -1181,6 +1181,58 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
 
         // Delete the conversation as user 1.
         \core_message\api::delete_conversation($user1->id, $user2->id);
+        $this->assertDebuggingCalled();
+
+        $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);
+    }
+
+    /**
+     * Tests deleting a conversation by conversation id.
+     */
+    public function test_delete_conversation_by_id() {
+        global $DB;
+
+        // Create some users.
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // The person doing the search.
+        $this->setUser($user1);
+
+        // Send some messages back and forth.
+        $time = 1;
+        $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
+        $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
+        $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
+        $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
+
+        // Delete the conversation as user 1.
+        $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+        \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
 
         $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC');
         $this->assertCount(4, $muas);
index 973eddf..714461b 100644 (file)
@@ -364,6 +364,7 @@ class core_message_events_testcase extends core_message_messagelib_testcase {
         // Trigger and capture the event.
         $sink = $this->redirectEvents();
         \core_message\api::delete_conversation($user1->id, $user2->id);
+        $this->assertDebuggingCalled();
         $events = $sink->get_events();
 
         // Get the user actions for the messages deleted by that user.
index 2ada652..aad4d00 100644 (file)
@@ -28,6 +28,7 @@ information provided here is intended especially for developers.
   Please see their declaration in lib/deprecatedlib.php to view their alternatives (if applicable).
 * The following methods have been deprecated and should not be used any more:
   - \core_message\api::is_user_blocked()
+  - \core_message\api::delete_conversation()
 * The following web services have been deprecated. Please do not call these any more.
   - core_message_external::block_contacts, please use core_message_external::block_user instead.
   - core_message_external::unblock_contacts, please use core_message_external::unblock_user instead.