@@ -59,6 +59,9 @@ class dailymail_test extends \advanced_testcase {
59
59
/** @var \stdClass discussion instance */
60
60
private $ discussion ;
61
61
62
+ /** @var moodleoverflow generator */
63
+ private $ generator ;
64
+
62
65
/**
63
66
* Test setUp.
64
67
*/
@@ -94,12 +97,13 @@ public function tearDown(): void {
94
97
*/
95
98
public function helper_create_user_and_discussion ($ maildigest ) {
96
99
// Create a user enrolled in the course as student.
97
- $ this ->user = $ this ->getDataGenerator ()->create_user (['firstname ' => 'Tamaro ' , 'maildigest ' => $ maildigest ]);
100
+ $ this ->
user =
$ this ->
getDataGenerator ()->
create_user ([
'firstname ' =>
'Tamaro ' ,
'email ' =>
'[email protected] ' ,
101
+ 'maildigest ' => $ maildigest ]);
98
102
$ this ->getDataGenerator ()->enrol_user ($ this ->user ->id , $ this ->course ->id , 'student ' );
99
103
100
104
// Create a new discussion and post within the moodleoverflow.
101
- $ generator = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
102
- $ this ->discussion = $ generator ->post_to_forum ($ this ->moodleoverflow , $ this ->user );
105
+ $ this -> generator = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
106
+ $ this ->discussion = $ this -> generator ->post_to_forum ($ this ->moodleoverflow , $ this ->user );
103
107
}
104
108
105
109
/**
@@ -137,7 +141,6 @@ private function helper_run_send_mails() {
137
141
* @covers \send_daily_mail::execute
138
142
*/
139
143
public function test_mail_delivery (): void {
140
-
141
144
// Create user with maildigest = on.
142
145
$ this ->helper_create_user_and_discussion ('1 ' );
143
146
@@ -149,6 +152,52 @@ public function test_mail_delivery(): void {
149
152
$ this ->assertEquals (1 , $ messages );
150
153
}
151
154
155
+ /**
156
+ * Test if the task send_daily_mail does not sends email from posts that are not in the course of the user.
157
+ * @return void
158
+ */
159
+ public function test_delivery_not_enrolled (): void {
160
+ // Create user with maildigest = on.
161
+ $ this ->helper_create_user_and_discussion ('1 ' );
162
+
163
+ // Create another user, course and a moodleoverflow post.
164
+ $ course = $ this ->getDataGenerator ()->create_course ();
165
+ $ location = ['course ' => $ course ->id , 'forcesubscribe ' => MOODLEOVERFLOW_FORCESUBSCRIBE ];
166
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ('moodleoverflow ' , $ location );
167
+ $ student =
$ this ->
getDataGenerator ()->
create_user ([
'firstname ' =>
'Ethan ' ,
'email ' =>
'[email protected] ' ,
168
+ 'maildigest ' => '1 ' ]);
169
+ $ this ->getDataGenerator ()->enrol_user ($ student ->id , $ course ->id , 'teacher ' );
170
+ $ discussion = $ this ->generator ->post_to_forum ($ moodleoverflow , $ student );
171
+
172
+ // Send the mails.
173
+ $ this ->helper_run_send_mails ();
174
+ $ this ->helper_run_send_daily_mail ();
175
+ $ messages = $ this ->sink ->count ();
176
+ $ content = $ this ->sink ->get_messages ();
177
+
178
+ // There should be 2 mails.
179
+ $ this ->assertEquals (2 , $ messages );
180
+
181
+ // Check the recipient of the mails and the discussion that is addressed. There should be no false addressed discussions.
182
+ $ firstmail = $ content [0 ];
183
+ $ secondmail = $ content [1 ];
184
+ // Depending on the order of the mails, check the recipient and the discussion that is addressed.
185
+ if (
$ firstmail->
to ==
"[email protected] " ) {
186
+ $ this ->assertStringContainsString ($ this ->discussion [0 ]->name , $ firstmail ->body );
187
+ $ this ->assertStringNotContainsString ($ discussion [0 ]->name , $ firstmail ->body );
188
+ $ this ->
assertEquals (
'[email protected] ' ,
$ secondmail->
to );
189
+ $ this ->assertStringContainsString ($ discussion [0 ]->name , $ secondmail ->body );
190
+ $ this ->assertStringNotContainsString ($ this ->discussion [0 ]->name , $ secondmail ->body );
191
+ } else {
192
+ $ this ->
assertEquals (
'[email protected] ' ,
$ firstmail->
to );
193
+ $ this ->assertStringContainsString ($ discussion [0 ]->name , $ firstmail ->body );
194
+ $ this ->assertStringNotContainsString ($ this ->discussion [0 ]->name , $ firstmail ->body );
195
+ $ this ->
assertEquals (
'[email protected] ' ,
$ secondmail->
to );
196
+ $ this ->assertStringContainsString ($ this ->discussion [0 ]->name , $ secondmail ->body );
197
+ $ this ->assertStringNotContainsString ($ discussion [0 ]->name , $ secondmail ->body );
198
+ }
199
+ }
200
+
152
201
153
202
/**
154
203
* Test if the content of the mail matches the supposed content.
0 commit comments