MDL-59834 Global Search: Indexing user messages
authorMatt Porritt <mattp@catalyst-au.net>
Thu, 31 Aug 2017 01:36:18 +0000 (11:36 +1000)
committerMatt Porritt <mattp@catalyst-au.net>
Sun, 3 Sep 2017 23:09:48 +0000 (09:09 +1000)
Make indexing of user messages tolerant to user deletion.
When creating the search document for user messages,
add a check to see if the user has been deleted in the
system. If they have been deleted abort creating the search
document.
This is to avoid rasining an error when trying to get the
user context for a deleted user.

message/classes/search/base_message.php
message/tests/search_test_received.php
message/tests/search_test_sent.php

index 435bc54..f6ff548 100644 (file)
@@ -51,6 +51,14 @@ abstract class base_message extends \core_search\base {
      * @return \core_search\document
      */
     public function get_document($record, $options = array()) {
+
+        // Check if user still exists, before proceeding.
+        $user = \core_user::get_user($options['user1id'], 'deleted');
+        if ($user->deleted == 1) {
+            return false;
+        }
+
+        // Get user context.
         try {
             $usercontext = \context_user::instance($options['user1id']);
         } catch (\moodle_exception $ex) {
index 191850e..7bcde69 100644 (file)
@@ -226,4 +226,49 @@ class message_received_search_testcase extends advanced_testcase {
         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
 
     }
+
+    /**
+     * Test received deleted user.
+     * Tests the case where a received message for a deleted user
+     * is attempted to be added to the index.
+     *
+     * @return void
+     */
+    public function test_message_received_deleted_user() {
+
+        // Returns the instance as long as the area is supported.
+        $searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);
+        $this->assertInstanceOf('\core_message\search\message_received', $searcharea);
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $this->preventResetByRollback();
+        $sink = $this->redirectMessages();
+
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
+        $message->userfrom = $user1;
+        $message->userto = $user2;
+        $message->subject = "Test Subject";
+        $message->smallmessage = "Test small messsage";
+        $message->fullmessage = "Test full messsage";
+        $message->fullmessageformat = 0;
+        $message->fullmessagehtml = null;
+        $message->notification = 0;
+        $message->component = "moodle";
+        $message->name = "instantmessage";
+
+        message_send($message);
+
+        $messages = $sink->get_messages();
+        $message = $messages[0];
+
+        // Delete user.
+        delete_user($user2);
+
+        $doc = $searcharea->get_document($message);
+
+        $this->assertFalse($doc);
+    }
 }
index 7e9c887..75a3735 100644 (file)
@@ -226,4 +226,50 @@ class message_sent_search_testcase extends advanced_testcase {
         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
 
     }
+
+    /**
+     * Test sent deleted user.
+     * Tests the case where a sent message for a deleted user
+     * is attempted to be added to the index.
+     *
+     * @return void
+     */
+    public function test_message_sent_deleted_user() {
+
+        // Returns the instance as long as the area is supported.
+        $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
+        $this->assertInstanceOf('\core_message\search\message_sent', $searcharea);
+
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        $this->preventResetByRollback();
+        $sink = $this->redirectMessages();
+
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
+        $message->userfrom = $user1;
+        $message->userto = $user2;
+        $message->subject = "Test Subject";
+        $message->smallmessage = "Test small messsage";
+        $message->fullmessage = "Test full messsage";
+        $message->fullmessageformat = 0;
+        $message->fullmessagehtml = null;
+        $message->notification = 0;
+        $message->component = "moodle";
+        $message->name = "instantmessage";
+
+        message_send($message);
+
+        $messages = $sink->get_messages();
+        $message = $messages[0];
+
+        // Delete user.
+        delete_user($user1);
+
+        $doc = $searcharea->get_document($message);
+
+        $this->assertFalse($doc);
+
+    }
 }
\ No newline at end of file