MDL-63168 core_message: added unit test
authorMark Nelson <markn@moodle.com>
Wed, 23 Jan 2019 04:58:35 +0000 (12:58 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 23 Jan 2019 05:23:33 +0000 (13:23 +0800)
message/tests/migrate_message_data_task_test.php

index 3ec3009..a480e70 100644 (file)
@@ -324,6 +324,37 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
         $this->assertEquals(FORMAT_MOODLE, $notification->fullmessageformat);
     }
 
+    /**
+     * Test migrating a legacy message that a user sent to themselves then deleted.
+     */
+    public function test_migrating_message_deleted_message_sent_to_self() {
+        global $DB;
+
+        // Create user to test with.
+        $user1 = $this->getDataGenerator()->create_user();
+
+        $m1 = $this->create_legacy_message_or_notification($user1->id, $user1->id, null, false, null, null);
+
+        // Let's delete the message for the 'user to' and 'user from' which in this case is the same user.
+        $messageupdate = new stdClass();
+        $messageupdate->id = $m1;
+        $messageupdate->timeuserfromdeleted = time();
+        $messageupdate->timeusertodeleted = time();
+        $DB->update_record('message', $messageupdate);
+
+        // Now, let's execute the task for the user.
+        $task = new \core_message\task\migrate_message_data();
+        $task->set_custom_data(
+            [
+                'userid' => $user1->id
+            ]
+        );
+        $task->execute();
+
+        $this->assertEquals(0, $DB->count_records('message'));
+        $this->assertEquals(1, $DB->count_records('message_user_actions'));
+    }
+
     /**
      * Creates a legacy message or notification to be used for testing.
      *