Skip to content

Commit

Permalink
add phpunit test for bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Mar 20, 2024
1 parent f459edc commit 47c336c
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions tests/dailymail_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class dailymail_test extends \advanced_testcase {
/** @var \stdClass discussion instance */
private $discussion;

/** @var moodleoverflow generator */
private $generator;

/**
* Test setUp.
*/
Expand Down Expand Up @@ -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' => '[email protected]',
'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);
}

/**
Expand Down Expand Up @@ -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');

Expand All @@ -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' => '[email protected]',
'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 == "[email protected]") {
$this->assertStringContainsString($this->discussion[0]->name, $firstmail->body);
$this->assertStringNotContainsString($discussion[0]->name, $firstmail->body);
} else {
$this->assertEquals('[email protected]', $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.
Expand Down

0 comments on commit 47c336c

Please sign in to comment.