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