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 message_email;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Class for testing the event observers.
|
|
|
21 |
*
|
|
|
22 |
* @package message_email
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2019 Mark Nelson <markn@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
class event_observers_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Test the message viewed event observer.
|
|
|
31 |
*/
|
11 |
efrain |
32 |
public function test_message_viewed_observer(): void {
|
1 |
efrain |
33 |
global $DB;
|
|
|
34 |
|
|
|
35 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
36 |
|
|
|
37 |
$this->resetAfterTest();
|
|
|
38 |
|
|
|
39 |
// Create the test data.
|
|
|
40 |
$course = $this->getDataGenerator()->create_course();
|
|
|
41 |
|
|
|
42 |
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
43 |
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
44 |
|
|
|
45 |
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
46 |
|
|
|
47 |
groups_add_member($group1->id, $user1->id);
|
|
|
48 |
groups_add_member($group1->id, $user2->id);
|
|
|
49 |
|
|
|
50 |
$conversation = \core_message\api::create_conversation(
|
|
|
51 |
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
|
|
52 |
[$user1->id, $user2->id],
|
|
|
53 |
'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
|
|
|
54 |
'core_group',
|
|
|
55 |
'groups',
|
|
|
56 |
$group1->id,
|
|
|
57 |
\context_course::instance($course->id)->id
|
|
|
58 |
);
|
|
|
59 |
|
|
|
60 |
$message = new \core\message\message();
|
|
|
61 |
$message->courseid = 1;
|
|
|
62 |
$message->component = 'moodle';
|
|
|
63 |
$message->name = 'instantmessage';
|
|
|
64 |
$message->userfrom = $user1;
|
|
|
65 |
$message->convid = $conversation->id;
|
|
|
66 |
$message->subject = 'message subject';
|
|
|
67 |
$message->fullmessage = 'message body';
|
|
|
68 |
$message->fullmessageformat = FORMAT_MARKDOWN;
|
|
|
69 |
$message->fullmessagehtml = '<p>message body</p>';
|
|
|
70 |
$message->smallmessage = 'small message';
|
|
|
71 |
$message->notification = '0';
|
|
|
72 |
|
|
|
73 |
// Send the message twice.
|
|
|
74 |
$messageid1 = message_send($message);
|
|
|
75 |
$messageid2 = message_send($message);
|
|
|
76 |
|
|
|
77 |
// Check there are now 2 messages pending to be sent in the digest.
|
|
|
78 |
$this->assertEquals(2, $DB->count_records('message_email_messages'));
|
|
|
79 |
|
|
|
80 |
// Mark one of the messages as read.
|
|
|
81 |
$message1 = $DB->get_record('messages', ['id' => $messageid1]);
|
|
|
82 |
\core_message\api::mark_message_as_read($user2->id, $message1);
|
|
|
83 |
|
|
|
84 |
$emailmessage = $DB->get_records('message_email_messages');
|
|
|
85 |
|
|
|
86 |
// Check there is now only 1 message pending to be sent in the digest and it is the correct message.
|
|
|
87 |
$this->assertEquals(1, count($emailmessage));
|
|
|
88 |
|
|
|
89 |
$emailmessage = reset($emailmessage);
|
|
|
90 |
|
|
|
91 |
$this->assertEquals($user2->id, $emailmessage->useridto);
|
|
|
92 |
$this->assertEquals($conversation->id, $emailmessage->conversationid);
|
|
|
93 |
$this->assertEquals($messageid2, $emailmessage->messageid);
|
|
|
94 |
}
|
|
|
95 |
}
|