-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdailymail_test.php
208 lines (170 loc) · 6.92 KB
/
dailymail_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The module moodleoverflow tests.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_moodleoverflow;
use mod_moodleoverflow\task\send_mails;
use mod_moodleoverflow\task\send_daily_mail;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php');
/**
* Class mod_moodleoverflow_dailymail_testcase.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dailymail_test extends \advanced_testcase {
private $sink;
private $course;
private $user;
private $moodleoverflow;
private $coursemodule;
private $discussion;
/**
* Test setUp.
*/
public function setUp(): void {
$this->resetAfterTest();
set_config('maxeditingtime', -10, 'moodleoverflow');
unset_config('noemailever');
$this->sink = $this->redirectEmails();
$this->preventResetByRollback();
$this->redirectMessages();
// Create a new course with a moodleoverflow forum.
$this->course = $this->getDataGenerator()->create_course();
$location = array('course' => $this->course->id, 'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE);
$this->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location);
$this->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->moodleoverflow->id);
}
/**
* Test tearDown.
*/
public function tearDown(): void {
// Clear all caches.
\mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
\mod_moodleoverflow\subscriptions::reset_discussion_cache();
}
// Helper functions.
/**
* Function that creates a new user, which adds a new discussion an post to the moodleoverflow.
* @param $maildigest The maildigest setting: 0 = off , 1 = on
*/
public function helper_create_user_and_discussion($maildigest) {
// Create a user enrolled in the course as student.
$this->user = $this->getDataGenerator()->create_user(array('firstname' => 'Tamaro', 'maildigest' => $maildigest));
$this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, 'student');
// Create a new discussion and post within the moodleoverflow.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
$this->discussion = $generator->post_to_forum($this->moodleoverflow, $this->user);
}
/**
* Run the send daily mail task.
* @return false|string
*/
private function helper_run_send_daily_mail() {
$mailtask = new send_daily_mail();
ob_start();
$mailtask->execute();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Run the send mails task.
* @return false|string
*/
private function helper_run_send_mails() {
$mailtask = new send_mails();
ob_start();
$mailtask->execute();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
// Begin of test functions.
/**
* Test if the task send_daily_mail sends a mail to the user.
*/
public function test_mail_delivery() {
// Create user with maildigest = on.
$this->helper_create_user_and_discussion('1');
// Send a mail and test if the mail was sent.
$this->helper_run_send_mails();
$this->helper_run_send_daily_mail();
$messages = $this->sink->count();
$this->assertEquals(1, $messages);
}
/**
* Test if the content of the mail matches the supposed content.
*/
public function test_content_of_mail_delivery() {
// Create user with maildigest = on.
$this->helper_create_user_and_discussion('1');
// Send the mails and count the messages.
$this->helper_run_send_mails();
$this->helper_run_send_daily_mail();
$content = $this->sink->get_messages();
$message = $content[0]->body;
$message = str_replace(["\n\r", "\n", "\r"], '', $message);
$messagecount = $this->sink->count();
// Build the text that the mail should have.
// Text structure at get_string('digestunreadpost', moodleoverflow).
$linktocourse = '<a href=3D"https://www.example.com/mood=le/course/view.php?id=3D'. $this->course->id;
$linktoforum = '<a href=3D"https://www.=example.com/moodle/mod/moodleoverflow/view.php?id=3D'. $this->coursemodule->id;
$linktodiscussion = '<a href=3D"https://www.example.com/moodle/mod/moodleoverflow/=discussion.php?d=3D'
. $this->discussion[0]->id;
// Assemble text.
$text = 'Course: ' . $linktocourse . ' -> ' . $linktoforum . ', Topic: '
. $linktodiscussion . ' has ' . $messagecount . ' unread posts.';
$this->assertStringContainsString($linktocourse, $message);
$this->assertStringContainsString($linktoforum, $message);
$this->assertStringContainsString($linktodiscussion, $message);
$this->assertStringContainsString($messagecount, $message);
}
/**
* Test if the task does not send a mail when maildigest = 0
*/
public function test_mail_not_send() {
// Creat user with daily_mail = off.
$this->helper_create_user_and_discussion('0');
// Now send the mails and test if no mail was sent.
$this->helper_run_send_mails();
$this->helper_run_send_daily_mail();
$messages = $this->sink->count();
$this->assertEquals(0, $messages);
}
/**
* Test if database is updated after sending a mail
*/
public function test_records_removed() {
global $DB;
// create user with maildigest = on.
$this->helper_create_user_and_discussion('1');
// Now send the mails.
$this->helper_run_send_mails();
$this->helper_run_send_daily_mail();
// Now check the database if the records of the users are deleted.
$records = $DB->get_records('moodleoverflow_mail_info', array('userid' => $this->user->id));
$this->assertEmpty($records);
}
}