Skip to content

Commit

Permalink
MDL-62709 core_message: convert NULL format types in task
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jun 14, 2018
1 parent 8791c50 commit cd9ecbd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions message/classes/task/migrate_message_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function migrate_notification($notification, $isread) {
$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;
Expand Down Expand Up @@ -210,7 +210,7 @@ private function migrate_message($conversationid, $message) {
$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;
Expand Down
61 changes: 59 additions & 2 deletions message/tests/migrate_message_data_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,62 @@ public function test_migrating_notifications() {
}
}

/**
* 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.
*
Expand All @@ -276,11 +332,12 @@ public function test_migrating_notifications() {
* @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();
Expand Down Expand Up @@ -312,7 +369,7 @@ private function create_legacy_message_or_notification($useridfrom, $useridto, $
$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;
Expand Down

0 comments on commit cd9ecbd

Please sign in to comment.