Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_message\task;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
 
23
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
24
 
25
/**
26
 * Class for testing the migrate message data task.
27
 *
28
 * @package core_message
29
 * @category test
30
 * @copyright 2018 Mark Nelson <markn@moodle.com>
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class migrate_message_data_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * Test set up.
37
     *
38
     * This is executed before running any test in this file.
39
     */
40
    public function setUp(): void {
1441 ariadna 41
        parent::setUp();
1 efrain 42
        $this->resetAfterTest();
43
    }
44
 
45
    /**
46
     * Test migrating legacy messages.
47
     */
11 efrain 48
    public function test_migrating_messages(): void {
1 efrain 49
        global $DB;
50
 
51
        // Create users to test with.
52
        $user1 = $this->getDataGenerator()->create_user();
53
        $user2 = $this->getDataGenerator()->create_user();
54
        $user3 = $this->getDataGenerator()->create_user();
55
 
56
        // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
57
        $now = time();
58
        $timedeleted1 = $now - (2 * DAYSECS);
59
        $timedeleted2 = $now - (2 * DAYSECS) + 1;
60
        $timeread1 = $now - DAYSECS;
61
        $timeread2 = $now - DAYSECS + 1;
62
        $timeread3 = $now - DAYSECS + 2;
63
 
64
        // Send messages from user 1 to user 2.
65
        $m1 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, false, $timeread1);
66
        $m2 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 2);
67
        $m3 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 3);
68
 
69
        // Send messages from user 3 to user 1.
70
        $m4 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, false, $timeread2);
71
        $m5 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 5);
72
        $m6 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 6);
73
 
74
        // Send messages from user 3 to user 2.
75
        $m7 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, false, $timeread3);
76
        $m8 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 8);
77
        $m9 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 9);
78
 
79
        // Let's delete some messages, not using API here as it does not use the legacy tables.
80
        $messageupdate = new \stdClass();
81
        $messageupdate->id = $m1;
82
        $messageupdate->timeusertodeleted = $timedeleted1;
83
        $DB->update_record('message_read', $messageupdate);
84
 
85
        $messageupdate = new \stdClass();
86
        $messageupdate->id = $m5;
87
        $messageupdate->timeuserfromdeleted = $timedeleted2;
88
        $DB->update_record('message', $messageupdate);
89
 
90
        // Now, let's execute the task for user 1.
91
        $task = new migrate_message_data();
92
        $task->set_custom_data(
93
            [
94
                'userid' => $user1->id
95
            ]
96
        );
97
        $task->execute();
98
 
99
        // Ok, now we need to confirm all is good.
100
        // Remember - we are only converting the messages related to user 1.
101
        $this->assertEquals(2, $DB->count_records('message'));
102
        $this->assertEquals(1, $DB->count_records('message_read'));
103
        $this->assertEquals(6, $DB->count_records('messages'));
104
        $this->assertEquals(0, $DB->count_records('notifications'));
105
        $this->assertEquals(0, $DB->count_records('message_popup_notifications'));
106
 
107
        // Get the conversations.
108
        $conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
109
        $conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
110
 
111
        // Confirm what we have in the messages table is correct.
112
        $messages = $DB->get_records('messages', [], 'timecreated ASC');
113
        $i = 1;
114
        foreach ($messages as $message) {
115
            $useridfrom = $user1->id;
116
            $conversationid = $conversation1;
117
            if ($i > 3) {
118
                $useridfrom = $user3->id;
119
                $conversationid = $conversation2;
120
            }
121
 
122
            if ($i == 1) {
123
                $messagereadid1 = $message->id;
124
                $messagedeletedid1 = $message->id;
125
            } else if ($i == 4) {
126
                $messagereadid2 = $message->id;
127
            } else if ($i == 5) {
128
                $messagedeletedid2 = $message->id;
129
            }
130
 
131
            $this->assertEquals($useridfrom, $message->useridfrom);
132
            $this->assertEquals($conversationid, $message->conversationid);
133
            $this->assertEquals('Subject ' . $i, $message->subject);
134
            $this->assertEquals('Full message ' . $i, $message->fullmessage);
135
            $this->assertEquals(FORMAT_PLAIN, $message->fullmessageformat);
136
            $this->assertEquals('Full message HTML '. $i, $message->fullmessagehtml);
137
            $this->assertEquals('Small message ' . $i, $message->smallmessage);
138
            $this->assertEquals($i, $message->timecreated);
139
            $i++;
140
        }
141
 
142
        // Confirm there are 4 actions.
143
        $this->assertEquals(4, $DB->count_records('message_user_actions'));
144
 
145
        // Confirm the messages that were marked as read have actions associated with them.
146
        $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_READ], 'timecreated DESC');
147
        $this->assertCount(2, $muas);
148
 
149
        // Message user action for message read by user 1 (referring to $m4).
150
        $mua1 = array_shift($muas);
151
        // Message user action for message read by user 2 (referring to $m1).
152
        $mua2 = array_shift($muas);
153
 
154
        $this->assertEquals($user1->id, $mua1->userid);
155
        $this->assertEquals($messagereadid2, $mua1->messageid);
156
        $this->assertEquals($timeread2, $mua1->timecreated);
157
 
158
        $this->assertEquals($user2->id, $mua2->userid);
159
        $this->assertEquals($messagereadid1, $mua2->messageid);
160
        $this->assertEquals($timeread1, $mua2->timecreated);
161
 
162
        // Confirm the messages that were deleted have actions associated with them.
163
        $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_DELETED],
164
            'timecreated DESC');
165
        $this->assertCount(2, $muas);
166
 
167
        // Message user action for message deleted by user 3 (referring to $m5).
168
        $mua1 = array_shift($muas);
169
        // Message user action for message deleted by user 2 (referring to $m1).
170
        $mua2 = array_shift($muas);
171
 
172
        $this->assertEquals($user3->id, $mua1->userid);
173
        $this->assertEquals($messagedeletedid2, $mua1->messageid);
174
        $this->assertEquals($timedeleted2, $mua1->timecreated);
175
 
176
        $this->assertEquals($user2->id, $mua2->userid);
177
        $this->assertEquals($messagedeletedid1, $mua2->messageid);
178
        $this->assertEquals($timedeleted1, $mua2->timecreated);
179
    }
180
 
181
    /**
182
     * Test migrating legacy notifications.
183
     */
11 efrain 184
    public function test_migrating_notifications(): void {
1 efrain 185
        global $DB;
186
 
187
        // Create users to test with.
188
        $user1 = $this->getDataGenerator()->create_user();
189
        $user2 = $this->getDataGenerator()->create_user();
190
        $user3 = $this->getDataGenerator()->create_user();
191
 
192
        // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
193
        $timeread = time() - DAYSECS;
194
 
195
        // Send notifications from user 1 to user 2.
196
        $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, true, $timeread);
197
        $this->create_legacy_message_or_notification($user1->id, $user2->id, 2, true);
198
        $this->create_legacy_message_or_notification($user1->id, $user2->id, 3, true);
199
 
200
        // Send notifications from user 3 to user 1.
201
        $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, true, $timeread);
202
        $this->create_legacy_message_or_notification($user3->id, $user1->id, 5, true);
203
        $this->create_legacy_message_or_notification($user3->id, $user1->id, 6, true);
204
 
205
        // Send notifications from user 3 to user 2.
206
        $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, true, $timeread);
207
        $this->create_legacy_message_or_notification($user3->id, $user2->id, 8, true);
208
        $this->create_legacy_message_or_notification($user3->id, $user2->id, 9, true);
209
 
210
        // Now, let's execute the task for user 1.
211
        $task = new migrate_message_data();
212
        $task->set_custom_data(
213
            [
214
                'userid' => $user1->id
215
            ]
216
        );
217
        $task->execute();
218
 
219
        // Ok, now we need to confirm all is good.
220
        // Remember - we are only converting the notifications related to user 1.
221
        $this->assertEquals(2, $DB->count_records('message'));
222
        $this->assertEquals(1, $DB->count_records('message_read'));
223
        $this->assertEquals(3, $DB->count_records('message_popup'));
224
        $this->assertEquals(6, $DB->count_records('notifications'));
225
        $this->assertEquals(6, $DB->count_records('message_popup_notifications'));
226
 
227
        // Confirm what we have in the notifications table is correct.
228
        $notifications = $DB->get_records('notifications', [], 'timecreated ASC');
229
        $popupnotifications = $DB->get_records('message_popup_notifications', [], 'notificationid ASC', 'notificationid');
230
        $i = 1;
231
        foreach ($notifications as $notification) {
232
            // Assert the correct id is stored in the 'message_popup_notifications' table.
233
            $this->assertArrayHasKey($notification->id, $popupnotifications);
234
 
235
            $useridfrom = $user1->id;
236
            $useridto = $user2->id;
237
            if ($i > 3) {
238
                $useridfrom = $user3->id;
239
                $useridto = $user1->id;
240
            }
241
 
242
            $this->assertEquals($useridfrom, $notification->useridfrom);
243
            $this->assertEquals($useridto, $notification->useridto);
244
            $this->assertEquals('Subject ' . $i, $notification->subject);
245
            $this->assertEquals('Full message ' . $i, $notification->fullmessage);
246
            $this->assertEquals(FORMAT_PLAIN, $notification->fullmessageformat);
247
            $this->assertEquals('Full message HTML '. $i, $notification->fullmessagehtml);
248
            $this->assertEquals('Small message ' . $i, $notification->smallmessage);
249
            $this->assertEquals('mod_assign', $notification->component);
250
            $this->assertEquals('assign_notification', $notification->eventtype);
251
            $this->assertEquals('https://www.google.com', $notification->contexturl);
252
            $this->assertEquals('google', $notification->contexturlname);
253
            $this->assertEquals($i, $notification->timecreated);
254
 
255
            if (($i == 1) || ($i == 4)) {
256
                $this->assertEquals($timeread, $notification->timeread);
257
            } else {
258
                $this->assertNull($notification->timeread);
259
            }
260
 
261
            $i++;
262
        }
263
    }
264
 
265
    /**
266
     * Test migrating a legacy message that contains null as the format.
267
     */
11 efrain 268
    public function test_migrating_message_null_format(): void {
1 efrain 269
        global $DB;
270
 
271
        // Create users to test with.
272
        $user1 = $this->getDataGenerator()->create_user();
273
        $user2 = $this->getDataGenerator()->create_user();
274
 
275
        $this->create_legacy_message_or_notification($user1->id, $user2->id, null, false, null, null);
276
 
277
        // Now, let's execute the task for user 1.
278
        $task = new migrate_message_data();
279
        $task->set_custom_data(
280
            [
281
                'userid' => $user1->id
282
            ]
283
        );
284
        $task->execute();
285
 
286
        $messages = $DB->get_records('messages');
287
        $this->assertCount(1, $messages);
288
 
289
        $message = reset($messages);
290
        $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
291
    }
292
 
293
    /**
294
     * Test migrating a legacy notification that contains null as the format.
295
     */
11 efrain 296
    public function test_migrating_notification_null_format(): void {
1 efrain 297
        global $DB;
298
 
299
        // Create users to test with.
300
        $user1 = $this->getDataGenerator()->create_user();
301
        $user2 = $this->getDataGenerator()->create_user();
302
 
303
        $this->create_legacy_message_or_notification($user1->id, $user2->id, null, true, null, null);
304
 
305
        // Now, let's execute the task for user 1.
306
        $task = new migrate_message_data();
307
        $task->set_custom_data(
308
            [
309
                'userid' => $user1->id
310
            ]
311
        );
312
        $task->execute();
313
 
314
        $notifications = $DB->get_records('notifications');
315
        $this->assertCount(1, $notifications);
316
 
317
        $notification = reset($notifications);
318
        $this->assertEquals(FORMAT_MOODLE, $notification->fullmessageformat);
319
    }
320
 
321
    /**
322
     * Test migrating a legacy message that a user sent to themselves then deleted.
323
     */
11 efrain 324
    public function test_migrating_message_deleted_message_sent_to_self(): void {
1 efrain 325
        global $DB;
326
 
327
        // Create user to test with.
328
        $user1 = $this->getDataGenerator()->create_user();
329
 
330
        $m1 = $this->create_legacy_message_or_notification($user1->id, $user1->id, null, false, null, null);
331
 
332
        // Let's delete the message for the 'user to' and 'user from' which in this case is the same user.
333
        $messageupdate = new \stdClass();
334
        $messageupdate->id = $m1;
335
        $messageupdate->timeuserfromdeleted = time();
336
        $messageupdate->timeusertodeleted = time();
337
        $DB->update_record('message', $messageupdate);
338
 
339
        // Now, let's execute the task for the user.
340
        $task = new migrate_message_data();
341
        $task->set_custom_data(
342
            [
343
                'userid' => $user1->id
344
            ]
345
        );
346
        $task->execute();
347
 
348
        $this->assertEquals(0, $DB->count_records('message'));
349
        $this->assertEquals(1, $DB->count_records('message_user_actions'));
350
    }
351
 
352
    /**
353
     * Creates a legacy message or notification to be used for testing.
354
     *
355
     * @param int $useridfrom The user id from
356
     * @param int $useridto The user id to
357
     * @param int $timecreated
358
     * @param bool $notification
359
     * @param int|null $timeread The time the message/notification was read, null if it hasn't been.
360
     * @param string|int|null $format The format of the message.
361
     * @return int The id of the message (in either the message or message_read table)
362
     * @throws dml_exception
363
     */
364
    private function create_legacy_message_or_notification($useridfrom, $useridto, $timecreated = null,
365
            $notification = false, $timeread = null, $format = FORMAT_PLAIN) {
366
        global $DB;
367
 
368
        $tabledata = new \stdClass();
369
 
370
        if (is_null($timecreated)) {
371
            $timecreated = time();
372
        }
373
 
374
        if (!is_null($timeread)) {
375
            $table = 'message_read';
376
            $tabledata->timeread = $timeread;
377
        } else {
378
            $table = 'message';
379
        }
380
 
381
        if ($notification) {
382
            $tabledata->eventtype = 'assign_notification';
383
            $tabledata->component = 'mod_assign';
384
            $tabledata->notification = 1;
385
            $tabledata->contexturl = 'https://www.google.com';
386
            $tabledata->contexturlname = 'google';
387
        } else {
388
            $tabledata->eventtype = 'instantmessage';
389
            $tabledata->component = 'moodle';
390
            $tabledata->notification = 0;
391
        }
392
 
393
        $tabledata->useridfrom = $useridfrom;
394
        $tabledata->useridto = $useridto;
395
        $tabledata->subject = 'Subject ' . $timecreated;
396
        $tabledata->fullmessage = 'Full message ' . $timecreated;
397
        $tabledata->fullmessageformat = $format;
398
        $tabledata->fullmessagehtml = 'Full message HTML ' . $timecreated;
399
        $tabledata->smallmessage = 'Small message ' . $timecreated;
400
        $tabledata->timecreated = $timecreated;
401
 
402
        $id = $DB->insert_record($table, $tabledata);
403
 
404
        // Insert into the legacy 'message_popup' table if it is a notification.
405
        if ($notification) {
406
            $mp = new \stdClass();
407
            $mp->messageid = $id;
408
            $mp->isread = (!is_null($timeread)) ? 1 : 0;
409
 
410
            $DB->insert_record('message_popup', $mp);
411
        }
412
 
413
        return $id;
414
    }
415
}