MDL-62709 core_message: convert NULL format types in task
authorMark Nelson <markn@moodle.com>
Thu, 14 Jun 2018 04:52:30 +0000 (12:52 +0800)
committerMark Nelson <markn@moodle.com>
Thu, 14 Jun 2018 05:18:46 +0000 (13:18 +0800)
message/classes/task/migrate_message_data.php
message/tests/migrate_message_data_task_test.php

index a2a76d1..34d481f 100644 (file)
@@ -172,7 +172,7 @@ class migrate_message_data extends \core\task\adhoc_task {
         $tabledata->useridto = $notification->useridto;
         $tabledata->subject = $notification->subject;
         $tabledata->fullmessage = $notification->fullmessage;
-        $tabledata->fullmessageformat = $notification->fullmessageformat;
+        $tabledata->fullmessageformat = $notification->fullmessageformat ?? FORMAT_MOODLE;
         $tabledata->fullmessagehtml = $notification->fullmessagehtml;
         $tabledata->smallmessage = $notification->smallmessage;
         $tabledata->component = $notification->component;
@@ -210,7 +210,7 @@ class migrate_message_data extends \core\task\adhoc_task {
         $tabledata->conversationid = $conversationid;
         $tabledata->subject = $message->subject;
         $tabledata->fullmessage = $message->fullmessage;
-        $tabledata->fullmessageformat = $message->fullmessageformat;
+        $tabledata->fullmessageformat = $message->fullmessageformat ?? FORMAT_MOODLE;
         $tabledata->fullmessagehtml = $message->fullmessagehtml;
         $tabledata->smallmessage = $message->smallmessage;
         $tabledata->timecreated = $message->timecreated;
index 72c1d0a..3ec3009 100644 (file)
@@ -268,6 +268,62 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
         }
     }
 
+    /**
+     * Test migrating a legacy message that contains null as the format.
+     */
+    public function test_migrating_message_null_format() {
+        global $DB;
+
+        // Create users to test with.
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $this->create_legacy_message_or_notification($user1->id, $user2->id, null, false, null, null);
+
+        // Now, let's execute the task for user 1.
+        $task = new \core_message\task\migrate_message_data();
+        $task->set_custom_data(
+            [
+                'userid' => $user1->id
+            ]
+        );
+        $task->execute();
+
+        $messages = $DB->get_records('messages');
+        $this->assertCount(1, $messages);
+
+        $message = reset($messages);
+        $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
+    }
+
+    /**
+     * Test migrating a legacy notification that contains null as the format.
+     */
+    public function test_migrating_notification_null_format() {
+        global $DB;
+
+        // Create users to test with.
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $this->create_legacy_message_or_notification($user1->id, $user2->id, null, true, null, null);
+
+        // Now, let's execute the task for user 1.
+        $task = new \core_message\task\migrate_message_data();
+        $task->set_custom_data(
+            [
+                'userid' => $user1->id
+            ]
+        );
+        $task->execute();
+
+        $notifications = $DB->get_records('notifications');
+        $this->assertCount(1, $notifications);
+
+        $notification = reset($notifications);
+        $this->assertEquals(FORMAT_MOODLE, $notification->fullmessageformat);
+    }
+
     /**
      * Creates a legacy message or notification to be used for testing.
      *
@@ -276,11 +332,12 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
      * @param int $timecreated
      * @param bool $notification
      * @param int|null $timeread The time the message/notification was read, null if it hasn't been.
+     * @param string|int|null $format The format of the message.
      * @return int The id of the message (in either the message or message_read table)
      * @throws dml_exception
      */
     private function create_legacy_message_or_notification($useridfrom, $useridto, $timecreated = null,
-            $notification = false, $timeread = null) {
+            $notification = false, $timeread = null, $format = FORMAT_PLAIN) {
         global $DB;
 
         $tabledata = new \stdClass();
@@ -312,7 +369,7 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
         $tabledata->useridto = $useridto;
         $tabledata->subject = 'Subject ' . $timecreated;
         $tabledata->fullmessage = 'Full message ' . $timecreated;
-        $tabledata->fullmessageformat = FORMAT_PLAIN;
+        $tabledata->fullmessageformat = $format;
         $tabledata->fullmessagehtml = 'Full message HTML ' . $timecreated;
         $tabledata->smallmessage = 'Small message ' . $timecreated;
         $tabledata->timecreated = $timecreated;