Differences Between: [Versions 39 and 310]
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 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 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 30 require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); 31 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 { 41 42 /** 43 * Test sending email task. 44 */ 45 public function test_sending_email_task() { 46 global $DB, $SITE; 47 48 $this->preventResetByRollback(); // Messaging is not compatible with transactions. 49 50 $this->resetAfterTest(); 51 52 // Create a course. 53 $course = $this->getDataGenerator()->create_course(); 54 55 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); 56 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); 57 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)); 61 62 groups_add_member($group1->id, $user1->id); 63 groups_add_member($group2->id, $user1->id); 64 65 groups_add_member($group1->id, $user2->id); 66 groups_add_member($group2->id, $user2->id); 67 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 ); 77 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 ); 88 89 // Go through each conversation. 90 if ($conversations = $DB->get_records('message_conversations')) { 91 foreach ($conversations as $conversation) { 92 $conversationid = $conversation->id; 93 94 // Let's send 5 messages. 95 for ($i = 1; $i <= 5; $i++) { 96 $message = new \core\message\message(); 97 $message->courseid = 1; 98 $message->component = 'moodle'; 99 $message->name = 'instantmessage'; 100 $message->userfrom = $user1; 101 $message->convid = $conversationid; 102 $message->subject = 'message subject'; 103 $message->fullmessage = 'message body'; 104 $message->fullmessageformat = FORMAT_MARKDOWN; 105 $message->fullmessagehtml = '<p>message body</p>'; 106 $message->smallmessage = 'small message'; 107 $message->notification = '0'; 108 109 message_send($message); 110 } 111 } 112 } 113 114 $this->assertEquals(10, $DB->count_records('message_email_messages')); 115 116 // Only 1 email is sent as the messages are included in it at a digest. 117 $sink = $this->redirectEmails(); 118 $task = new \message_email\task\send_email_task(); 119 $task->execute(); 120 $this->assertEquals(1, $sink->count()); 121 122 // Confirm it contains the correct data. 123 $emails = $sink->get_messages(); 124 $email = reset($emails); 125 $sitename = format_string($SITE->fullname); 126 $this->assertSame(get_string('messagedigestemailsubject', 'message_email', $sitename), $email->subject); 127 $this->assertSame($user2->email, $email->to); 128 $this->assertNotEmpty($email->header); 129 $emailbody = quoted_printable_decode($email->body); 130 $this->assertStringContainsString('Group 1', $emailbody); 131 $this->assertStringContainsString('Group 2', $emailbody); 132 // 5 unread messages per conversation, this will be listed twice. 133 $this->assertRegExp("/<span\b[^>]*>5<\/span> <span\b[^>]*>Unread message\w+/", $emailbody); 134 135 // Confirm table was emptied after task was run. 136 $this->assertEquals(0, $DB->count_records('message_email_messages')); 137 138 // Confirm running it again does not send another. 139 $sink = $this->redirectEmails(); 140 $task = new \message_email\task\send_email_task(); 141 $task->execute(); 142 $this->assertEquals(0, $sink->count()); 143 } 144 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body