diff --git a/tests/dailymail_test.php b/tests/dailymail_test.php index 922050e033..9be1c336e6 100644 --- a/tests/dailymail_test.php +++ b/tests/dailymail_test.php @@ -59,6 +59,9 @@ class dailymail_test extends \advanced_testcase { /** @var \stdClass discussion instance */ private $discussion; + /** @var moodleoverflow generator */ + private $generator; + /** * Test setUp. */ @@ -94,12 +97,13 @@ public function tearDown(): void { */ public function helper_create_user_and_discussion($maildigest) { // Create a user enrolled in the course as student. - $this->user = $this->getDataGenerator()->create_user(['firstname' => 'Tamaro', 'maildigest' => $maildigest]); + $this->user = $this->getDataGenerator()->create_user(['firstname' => 'Tamaro', 'email' => 'tamaromail@example.com', + '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); + $this->generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $this->discussion = $this->generator->post_to_forum($this->moodleoverflow, $this->user); } /** @@ -136,8 +140,7 @@ private function helper_run_send_mails() { * Test if the task send_daily_mail sends a mail to the user. * @covers \send_daily_mail::execute */ - public function test_mail_delivery() { - + public function test_mail_delivery(): void { // Create user with maildigest = on. $this->helper_create_user_and_discussion('1'); @@ -149,6 +152,46 @@ public function test_mail_delivery() { $this->assertEquals(1, $messages); } + /** + * Test if the task send_daily_mail does not sends email from posts that are not in the course of the user. + * @return void + */ + public function test_delivery_not_enrolled(): void { + // Create user with maildigest = on. + $this->helper_create_user_and_discussion('1'); + + // Create another user, course and a moodleoverflow post. + $course = $this->getDataGenerator()->create_course(); + $location = ['course' => $course->id, 'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE]; + $moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location); + $coursemodule = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id); + $student = $this->getDataGenerator()->create_user(['firstname' => 'Ethan', 'email' => 'ethanmail@example.com', + 'maildigest' => '1']); + $this->getDataGenerator()->enrol_user($student->id, $course->id, 'teacher'); + $discussion = $this->generator->post_to_forum($moodleoverflow, $student); + + // Send the mails. + $this->helper_run_send_mails(); + $this->helper_run_send_daily_mail(); + $messages = $this->sink->count(); + $content = $this->sink->get_messages(); + var_dump($content); + // There should be 2 mails. + $this->assertEquals(2, $messages); + + // Check the recipient of the mails and the discussion that is addressed. There should be false addressed mails. + $firstmail = $content[0]; + $secondmail = $content[1]; + if ($firstmail->to == "tamaromail@example.com") { + $this->assertStringContainsString($this->discussion[0]->name, $firstmail->body); + $this->assertStringNotContainsString($discussion[0]->name, $firstmail->body); + } else { + $this->assertEquals('petermail@example.com', $secondmail->to); + $this->assertStringContainsString($discussion[0]->name, $secondmail->body); + $this->assertStringNotContainsString($this->discussion[0]->name, $secondmail->body); + } + } + /** * Test if the content of the mail matches the supposed content.