31
31
global $ CFG ;
32
32
require_once ($ CFG ->dirroot . '/mod/moodleoverflow/lib.php ' );
33
33
34
+
35
+ /**
36
+ * Class mod_moodleoverflow_dailymail_testcase.
37
+ *
38
+ * @package mod_moodleoverflow
39
+ * @copyright 2023 Tamaro Walter
40
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
+ */
34
42
class dailymail_test extends \advanced_testcase {
35
43
36
44
private $ sink ;
37
- private $ messagesink ;
38
45
private $ course ;
39
46
private $ user ;
40
47
private $ moodleoverflow ;
48
+ private $ coursemodule ;
41
49
private $ discussion ;
42
50
43
51
/**
@@ -51,13 +59,12 @@ public function setUp(): void {
51
59
$ this ->sink = $ this ->redirectEmails ();
52
60
53
61
$ this ->preventResetByRollback ();
54
- $ this ->messagesink = $ this ->redirectMessages ();
55
-
62
+ $ this ->redirectMessages ();
56
63
// Create a new course with a moodleoverflow forum.
57
64
$ this ->course = $ this ->getDataGenerator ()->create_course ();
58
65
$ location = array ('course ' => $ this ->course ->id , 'forcesubscribe ' => MOODLEOVERFLOW_FORCESUBSCRIBE );
59
66
$ this ->moodleoverflow = $ this ->getDataGenerator ()->create_module ('moodleoverflow ' , $ location );
60
-
67
+ $ this -> coursemodule = get_coursemodule_from_instance ( ' moodleoverflow ' , $ this -> moodleoverflow -> id );
61
68
}
62
69
63
70
/**
@@ -69,8 +76,13 @@ public function tearDown(): void {
69
76
\mod_moodleoverflow \subscriptions::reset_discussion_cache ();
70
77
}
71
78
79
+
80
+
81
+ // Helper functions.
82
+
72
83
/**
73
84
* Function that creates a new user, which adds a new discussion an post to the moodleoverflow.
85
+ * @param $maildigest The maildigest setting: 0 = off , 1 = on
74
86
*/
75
87
public function helper_create_user_and_discussion ($ maildigest ) {
76
88
// Create a user enrolled in the course as student.
@@ -86,7 +98,7 @@ public function helper_create_user_and_discussion($maildigest) {
86
98
* Run the send daily mail task.
87
99
* @return false|string
88
100
*/
89
- private function run_send_daily_mail () {
101
+ private function helper_run_send_daily_mail () {
90
102
$ mailtask = new send_daily_mail ();
91
103
ob_start ();
92
104
$ mailtask ->execute ();
@@ -99,7 +111,7 @@ private function run_send_daily_mail() {
99
111
* Run the send mails task.
100
112
* @return false|string
101
113
*/
102
- private function run_send_mails () {
114
+ private function helper_run_send_mails () {
103
115
$ mailtask = new send_mails ();
104
116
ob_start ();
105
117
$ mailtask ->execute ();
@@ -108,59 +120,87 @@ private function run_send_mails() {
108
120
return $ output ;
109
121
}
110
122
123
+
124
+
125
+ // Begin of test functions.
126
+
111
127
/**
112
- * Test if the task send_daily_mail sends a mail to the user
128
+ * Test if the task send_daily_mail sends a mail to the user.
113
129
*/
114
130
public function test_mail_delivery () {
115
131
global $ DB ;
116
132
117
- // Create users with maildigest = on
133
+ // Create user with maildigest = on
118
134
$ this ->helper_create_user_and_discussion ('1 ' );
119
135
120
136
// Send a mail and test if the mail was sent.
121
137
122
- $ this ->run_send_mails (); // content2
123
- $ this ->run_send_daily_mail (); // content
138
+ $ this ->helper_run_send_mails (); // content2
139
+ $ this ->helper_run_send_daily_mail (); // content
124
140
$ messages = $ this ->sink ->count ();
125
141
126
142
$ this ->assertEquals (1 , $ messages );
127
143
}
128
144
145
+
146
+ /**
147
+ * Test if the content of the mail matches the supposed content
148
+ */
129
149
public function test_content_of_mail_delivery () {
130
150
global $ DB ;
131
151
132
- // Creat Users with maildigest = on.
152
+ // Creat user with maildigest = on.
133
153
$ this ->helper_create_user_and_discussion ('1 ' );
134
154
135
155
// send the mails and count the messages.
136
- $ this ->run_send_mails ();
137
- $ content = $ this ->run_send_daily_mail ();
156
+ $ this ->helper_run_send_mails ();
157
+ $ content = $ this ->helper_run_send_daily_mail ();
158
+ $ content = str_replace (["\n\r" , "\n" , "\r" ], '' , $ content );
138
159
$ messages = $ this ->sink ->count ();
139
160
140
161
// Build the text that the mail should have.
141
- // Text structure: $string['digestunreadpost'] = 'Course: {$a->currentcourse} -> {$a->currentforum}, Topic: {$a->discussion} has {$a->unreadposts} unread posts.';.
142
- $ currentcourse = $ this ->course ->fullname ;
143
- $ currentforum = $ this ->moodleoverflow ->name ;
144
- $ currentdiscussion = $ this ->discussion [0 ]->name ;
145
- $ text = 'Course: ' . $ currentcourse . ' -> ' . $ currentforum . ', Topic: ' . $ currentdiscussion . ' has ' . $ messages . ' unread posts. ' ;
146
- $ content = str_replace ("\r\n" , "" , $ content );
147
- $ text = str_replace ("\r\n" , "" , $ text );
148
-
149
- // $this->assertisInt(0, strcmp($text, $content)); //strcmp compares 2 strings and retuns 0 if equal
150
- // $this->assertEquals($text, $content);
151
- $ this ->assertStringContainsString ($ text , $ content );
162
+ // Text structure: $string['digestunreadpost'] = 'Course: {$a->linktocourse}-> {$a->linktoforum}, Topic: {$a->linktodiscussion} has {$a->unreadposts} unread posts.';.
163
+ $ linktocourse = '<a href="https://www.example.com/moodle/course/view.php?id= ' . $ this ->course ->id . '"> ' . $ this ->course ->fullname . '</a> ' ;
164
+ $ linktoforum = '<a href="https://www.example.com/moodle/mod/moodleoverflow/view.php?id= ' . $ this ->coursemodule ->id . '"> ' . $ this ->moodleoverflow ->name . '</a> ' ;
165
+ $ linktodiscussion = '<a href="https://www.example.com/moodle/mod/moodleoverflow/discussion.php?d= ' . $ this ->discussion [0 ]->id . '"> ' . $ this ->discussion [0 ]->name . '</a> ' ;
166
+
167
+ // assemble text
168
+ $ text = 'Course: ' . $ linktocourse . ' -> ' . $ linktoforum . ', Topic: ' . $ linktodiscussion . ' has ' . $ messages . ' unread posts. ' ;
169
+
170
+ $ this ->assertEquals ($ text , $ content );
152
171
}
153
172
173
+
174
+ /**
175
+ * Test if the task does not send a mail when maildigest = 0
176
+ */
154
177
public function test_mail_not_send () {
155
178
global $ DB ;
156
- // Creat Users with daily_mail = off.
179
+ // Creat user with daily_mail = off.
157
180
$ this ->helper_create_user_and_discussion ('0 ' );
158
181
159
182
// Now send the mails and test if no mail was sent.
160
- $ this ->run_send_mails ();
161
- $ this ->run_send_daily_mail ();
183
+ $ this ->helper_run_send_mails ();
184
+ $ this ->helper_run_send_daily_mail ();
162
185
$ messages = $ this ->sink ->count ();
163
186
164
187
$ this ->assertEquals (0 , $ messages );
165
188
}
189
+
190
+ /**
191
+ * Test if database is updated after sending a mail
192
+ */
193
+ public function test_records_removed () {
194
+ global $ DB ;
195
+ // create user with maildigest = on.
196
+ $ this ->helper_create_user_and_discussion ('1 ' );
197
+
198
+ // Now send the mails.
199
+ $ this ->helper_run_send_mails ();
200
+ $ this ->helper_run_send_daily_mail ();
201
+
202
+ // Now check the database if the records of the users are deleted.
203
+ $ records = $ DB ->get_records ('moodleoverflow_mail_info ' , array ('userid ' => $ this ->user ->id ));
204
+ $ this ->assertEmpty ($ records );
205
+ }
166
206
}
0 commit comments