MDL-61255 core_message: added unit test for task
[moodle.git] / message / tests / migrate_message_data_task_test.php
CommitLineData
5cc6b3ee
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Tests the migrate message data task.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2018 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
31
32/**
33 * Class for testing the migrate message data task.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2018 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_migrate_message_data_task_testcase extends advanced_testcase {
41
42 /**
43 * Test set up.
44 *
45 * This is executed before running any test in this file.
46 */
47 public function setUp() {
48 $this->resetAfterTest();
49 }
50
51 /**
52 * Test migrating legacy messages.
53 */
54 public function test_migrating_messages() {
55 global $DB;
56
57 // Create users to test with.
58 $user1 = $this->getDataGenerator()->create_user();
59 $user2 = $this->getDataGenerator()->create_user();
60 $user3 = $this->getDataGenerator()->create_user();
61
62 // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
63 $now = time();
64 $timedeleted1 = $now - (2 * DAYSECS);
65 $timedeleted2 = $now - (2 * DAYSECS) + 1;
66 $timeread1 = $now - DAYSECS;
67 $timeread2 = $now - DAYSECS + 1;
68 $timeread3 = $now - DAYSECS + 2;
69
70 // Send messages from user 1 to user 2.
71 $m1 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, false, $timeread1);
72 $m2 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 2);
73 $m3 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 3);
74
75 // Send messages from user 3 to user 1.
76 $m4 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, false, $timeread2);
77 $m5 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 5);
78 $m6 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 6);
79
80 // Send messages from user 3 to user 2.
81 $m7 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, false, $timeread3);
82 $m8 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 8);
83 $m9 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 9);
84
85 // Let's delete some messages, not using API here as it does not use the legacy tables.
86 $messageupdate = new stdClass();
87 $messageupdate->id = $m1;
88 $messageupdate->timeusertodeleted = $timedeleted1;
89 $DB->update_record('message_read', $messageupdate);
90
91 $messageupdate = new stdClass();
92 $messageupdate->id = $m5;
93 $messageupdate->timeuserfromdeleted = $timedeleted2;
94 $DB->update_record('message', $messageupdate);
95
96 // Now, let's execute the task for user 1.
97 $task = new \core_message\task\migrate_message_data();
98 $task->set_custom_data(
99 [
100 'userid' => $user1->id
101 ]
102 );
103 $task->execute();
104
105 // Ok, now we need to confirm all is good.
106 // Remember - we are only converting the messages related to user 1.
107 $this->assertEquals(2, $DB->count_records('message'));
108 $this->assertEquals(1, $DB->count_records('message_read'));
109 $this->assertEquals(6, $DB->count_records('messages'));
110
111 // Get the conversations.
112 $conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
113 $conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
114
115 // Confirm what we have in the messages table is correct.
116 $messages = $DB->get_records('messages', [], 'timecreated ASC');
117 $i = 1;
118 foreach ($messages as $message) {
119 $useridfrom = $user1->id;
120 $conversationid = $conversation1;
121 if ($i > 3) {
122 $useridfrom = $user3->id;
123 $conversationid = $conversation2;
124 }
125
126 if ($i == 1) {
127 $messagereadid1 = $message->id;
128 $messagedeletedid1 = $message->id;
129 } else if ($i == 4) {
130 $messagereadid2 = $message->id;
131 } else if ($i == 5) {
132 $messagedeletedid2 = $message->id;
133 }
134
135 $this->assertEquals($useridfrom, $message->useridfrom);
136 $this->assertEquals($conversationid, $message->conversationid);
137 $this->assertEquals('Subject ' . $i, $message->subject);
138 $this->assertEquals('Full message ' . $i, $message->fullmessage);
139 $this->assertEquals(FORMAT_PLAIN, $message->fullmessageformat);
140 $this->assertEquals('Full message HTML '. $i, $message->fullmessagehtml);
141 $this->assertEquals('Small message ' . $i, $message->smallmessage);
142 $this->assertEquals($i, $message->timecreated);
143 $i++;
144 }
145
146 // Confirm there are 4 actions.
147 $this->assertEquals(4, $DB->count_records('message_user_actions'));
148
149 // Confirm the messages that were marked as read have actions associated with them.
150 $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_READ], 'timecreated DESC');
151 $this->assertCount(2, $muas);
152
153 // Message user action for message read by user 1 (referring to $m4).
154 $mua1 = array_shift($muas);
155 // Message user action for message read by user 2 (referring to $m1).
156 $mua2 = array_shift($muas);
157
158 $this->assertEquals($user1->id, $mua1->userid);
159 $this->assertEquals($messagereadid2, $mua1->messageid);
160 $this->assertEquals($timeread2, $mua1->timecreated);
161
162 $this->assertEquals($user2->id, $mua2->userid);
163 $this->assertEquals($messagereadid1, $mua2->messageid);
164 $this->assertEquals($timeread1, $mua2->timecreated);
165
166 // Confirm the messages that were deleted have actions associated with them.
167 $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_DELETED],
168 'timecreated DESC');
169 $this->assertCount(2, $muas);
170
171 // Message user action for message deleted by user 3 (referring to $m5).
172 $mua1 = array_shift($muas);
173 // Message user action for message deleted by user 2 (referring to $m1).
174 $mua2 = array_shift($muas);
175
176 $this->assertEquals($user3->id, $mua1->userid);
177 $this->assertEquals($messagedeletedid2, $mua1->messageid);
178 $this->assertEquals($timedeleted2, $mua1->timecreated);
179
180 $this->assertEquals($user2->id, $mua2->userid);
181 $this->assertEquals($messagedeletedid1, $mua2->messageid);
182 $this->assertEquals($timedeleted1, $mua2->timecreated);
183 }
184
185 /**
186 * Test migrating legacy notifications.
187 */
188 public function test_migrating_notifications() {
189 global $DB;
190
191 // Create users to test with.
192 $user1 = $this->getDataGenerator()->create_user();
193 $user2 = $this->getDataGenerator()->create_user();
194 $user3 = $this->getDataGenerator()->create_user();
195
196 // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
197 $timeread = time() - DAYSECS;
198
199 // Send notifications from user 1 to user 2.
200 $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, true, $timeread);
201 $this->create_legacy_message_or_notification($user1->id, $user2->id, 2, true);
202 $this->create_legacy_message_or_notification($user1->id, $user2->id, 3, true);
203
204 // Send notifications from user 3 to user 1.
205 $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, true, $timeread);
206 $this->create_legacy_message_or_notification($user3->id, $user1->id, 5, true);
207 $this->create_legacy_message_or_notification($user3->id, $user1->id, 6, true);
208
209 // Send notifications from user 3 to user 2.
210 $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, true, $timeread);
211 $this->create_legacy_message_or_notification($user3->id, $user2->id, 8, true);
212 $this->create_legacy_message_or_notification($user3->id, $user2->id, 9, true);
213
214 // Now, let's execute the task for user 1.
215 $task = new \core_message\task\migrate_message_data();
216 $task->set_custom_data(
217 [
218 'userid' => $user1->id
219 ]
220 );
221 $task->execute();
222
223 // Ok, now we need to confirm all is good.
224 // Remember - we are only converting the notifications related to user 1.
225 $this->assertEquals(2, $DB->count_records('message'));
226 $this->assertEquals(1, $DB->count_records('message_read'));
227 $this->assertEquals(6, $DB->count_records('notifications'));
228
229 // Confirm what we have in the notifications table is correct.
230 $notifications = $DB->get_records('notifications', [], 'timecreated ASC');
231 $i = 1;
232 foreach ($notifications as $notification) {
233 $useridfrom = $user1->id;
234 $useridto = $user2->id;
235 if ($i > 3) {
236 $useridfrom = $user3->id;
237 $useridto = $user1->id;
238 }
239
240 $this->assertEquals($useridfrom, $notification->useridfrom);
241 $this->assertEquals($useridto, $notification->useridto);
242 $this->assertEquals('Subject ' . $i, $notification->subject);
243 $this->assertEquals('Full message ' . $i, $notification->fullmessage);
244 $this->assertEquals(FORMAT_PLAIN, $notification->fullmessageformat);
245 $this->assertEquals('Full message HTML '. $i, $notification->fullmessagehtml);
246 $this->assertEquals('Small message ' . $i, $notification->smallmessage);
247 $this->assertEquals('mod_assign', $notification->component);
248 $this->assertEquals('assign_notification', $notification->eventtype);
249 $this->assertEquals('https://www.google.com', $notification->contexturl);
250 $this->assertEquals('google', $notification->contexturlname);
251 $this->assertEquals($i, $notification->timecreated);
252
253 if (($i == 1) || ($i == 4)) {
254 $this->assertEquals($timeread, $notification->timeread);
255 } else {
256 $this->assertNull($notification->timeread);
257 }
258
259 $i++;
260 }
261 }
262
263 /**
264 * Creates a legacy message or notification to be used for testing.
265 *
266 * @param int $useridfrom The user id from
267 * @param int $useridto The user id to
268 * @param int $timecreated
269 * @param bool $notification
270 * @param int|null $timeread The time the message/notification was read, null if it hasn't been.
271 * @return int The id of the message (in either the message or message_read table)
272 * @throws dml_exception
273 */
274 private function create_legacy_message_or_notification($useridfrom, $useridto, $timecreated = null,
275 $notification = false, $timeread = null) {
276 global $DB;
277
278 $tabledata = new \stdClass();
279
280 if (is_null($timecreated)) {
281 $timecreated = time();
282 }
283
284 if (!is_null($timeread)) {
285 $table = 'message_read';
286 $tabledata->timeread = $timeread;
287 } else {
288 $table = 'message';
289 }
290
291 if ($notification) {
292 $tabledata->eventtype = 'assign_notification';
293 $tabledata->component = 'mod_assign';
294 $tabledata->notification = 1;
295 $tabledata->contexturl = 'https://www.google.com';
296 $tabledata->contexturlname = 'google';
297 } else {
298 $tabledata->eventtype = 'instantmessage';
299 $tabledata->component = 'moodle';
300 $tabledata->notification = 0;
301 }
302
303 $tabledata->useridfrom = $useridfrom;
304 $tabledata->useridto = $useridto;
305 $tabledata->subject = 'Subject ' . $timecreated;
306 $tabledata->fullmessage = 'Full message ' . $timecreated;
307 $tabledata->fullmessageformat = FORMAT_PLAIN;
308 $tabledata->fullmessagehtml = 'Full message HTML ' . $timecreated;
309 $tabledata->smallmessage = 'Small message ' . $timecreated;
310 $tabledata->timecreated = $timecreated;
311
312 return $DB->insert_record($table, $tabledata);
313 }
314}