cd59f5dfb1ac129daaf5236a5fc55d9ab4534846
[moodle.git] / message / output / email / tests / send_email_task_test.php
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/>.
17 /**
18  * Tests the send email task.
19  *
20  * @package message_email
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
32 /**
33  * Class for testing the send email task.
34  *
35  * @package message_email
36  * @category test
37  * @copyright 2019 Mark Nelson <markn@moodle.com>
38  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class core_message_send_email_task_testcase extends advanced_testcase {
42     /**
43      * Test sending email task.
44      */
45     public function test_sending_email_task() {
46         global $DB;
48         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
50         $this->resetAfterTest();
52         // Create a course.
53         $course = $this->getDataGenerator()->create_course();
55         $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
56         $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
58         // Create two groups in the course.
59         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
60         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
62         groups_add_member($group1->id, $user1->id);
63         groups_add_member($group2->id, $user1->id);
65         groups_add_member($group1->id, $user2->id);
66         groups_add_member($group2->id, $user2->id);
68         $conversation1 = \core_message\api::create_conversation(
69             \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
70             [$user1->id, $user2->id],
71             'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
72             'core_group',
73             'groups',
74             $group1->id,
75             context_course::instance($course->id)->id
76         );
78         $conversation2 = \core_message\api::create_conversation(
79             \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
80             [$user1->id, $user2->id],
81             'Group 2',
82             \core_message\api::MESSAGE_CONVERSATION_ENABLED,
83             'core_group',
84             'groups',
85             $group2->id,
86             context_course::instance($course->id)->id
87         );
89         // Go through each conversation.
90         if ($conversations = $DB->get_records('message_conversations')) {
91             foreach ($conversations as $conversation) {
92                 $conversationid = $conversation->id;
94                 $message = new \core\message\message();
95                 $message->courseid = 1;
96                 $message->component = 'moodle';
97                 $message->name = 'instantmessage';
98                 $message->userfrom = $user1;
99                 $message->convid = $conversationid;
100                 $message->subject = 'message subject';
101                 $message->fullmessage = 'message body';
102                 $message->fullmessageformat = FORMAT_MARKDOWN;
103                 $message->fullmessagehtml = '<p>message body</p>';
104                 $message->smallmessage = 'small message';
105                 $message->notification = '0';
107                 message_send($message);
108             }
109         }
111         $this->assertEquals(2, $DB->count_records('message_email_messages'));
113         // Only 1 email is sent as the 2 messages are included in it at a digest.
114         $sink = $this->redirectEmails();
115         $task = new \message_email\task\send_email_task();
116         $task->execute();
117         $this->assertEquals(1, $sink->count());
119         // Confirm table was emptied after task was run.
120         $this->assertEquals(0, $DB->count_records('message_email_messages'));
122         // Confirm running it again does not send another.
123         $sink = $this->redirectEmails();
124         $task = new \message_email\task\send_email_task();
125         $task->execute();
126         $this->assertEquals(0, $sink->count());
127     }