MDL-62709 core_message: convert NULL format types in task

This commit is contained in:
Mark Nelson 2018-06-14 12:52:30 +08:00
parent 8791c50d64
commit cd9ecbd81c
2 changed files with 61 additions and 4 deletions

View 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;

View 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;