Skip to content

Commit

Permalink
Merge branch 'master' into update/M4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter authored Apr 23, 2024
2 parents fb8ec84 + a385554 commit 318ac17
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ jobs:

- name: Behat features
if: ${{ always() }}
run: moodle-plugin-ci behat --auto-rerun 0
run: moodle-plugin-ci behat --auto-rerun 0
11 changes: 8 additions & 3 deletions classes/task/send_daily_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ public function execute() {
$mail = [];
// Fill the $mail array.
foreach ($userdata as $row) {
$currentcourse = $DB->get_record('course', ['id' => $row->courseid], 'fullname, id');
$currentforum = $DB->get_record('moodleoverflow', ['id' => $row->forumid], 'name, id');
$currentcourse = $DB->get_record('course', array('id' => $row->courseid), 'fullname, id');
// Check if the user is enrolled in the course, if not, go to the next row.
if (!is_enrolled(\context_course::instance($row->courseid), $user->userid, '', true)) {
continue;
}

$currentforum = $DB->get_record('moodleoverflow', array('id' => $row->forumid), 'name, id');
$coursemoduleid = get_coursemodule_from_instance('moodleoverflow', $row->forumid);
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $row->forumdiscussionid], 'name, id');
$unreadposts = $row->numberofposts;
Expand All @@ -77,7 +82,7 @@ public function execute() {
'linktoforum' => $linktoforum,
'linktodiscussion' => $linktodiscussion,
'unreadposts' => $unreadposts, ]);
array_push($mail, $string);
$mail[] = $string;
}
// Build the final message and send it to user. Then remove the sent records.
$message = implode('<br>', $mail);
Expand Down
10 changes: 5 additions & 5 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@
<TABLE NAME="moodleoverflow_mail_info" COMMENT="represent the content of the digest mail">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="forumid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="forumdiscussionid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="numberofposts" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="forumid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="forumdiscussionid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="numberofposts" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
57 changes: 53 additions & 4 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 @@ -137,7 +141,6 @@ private function helper_run_send_mails() {
* @covers \send_daily_mail::execute
*/
public function test_mail_delivery(): void {

// Create user with maildigest = on.
$this->helper_create_user_and_discussion('1');

Expand All @@ -149,6 +152,52 @@ public function test_mail_delivery(): void {
$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);
$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();

// There should be 2 mails.
$this->assertEquals(2, $messages);

// Check the recipient of the mails and the discussion that is addressed. There should be no false addressed discussions.
$firstmail = $content[0];
$secondmail = $content[1];
// Depending on the order of the mails, check the recipient and the discussion that is addressed.
if ($firstmail->to == "[email protected]") {
$this->assertStringContainsString($this->discussion[0]->name, $firstmail->body);
$this->assertStringNotContainsString($discussion[0]->name, $firstmail->body);
$this->assertEquals('[email protected]', $secondmail->to);
$this->assertStringContainsString($discussion[0]->name, $secondmail->body);
$this->assertStringNotContainsString($this->discussion[0]->name, $secondmail->body);
} else {
$this->assertEquals('[email protected]', $firstmail->to);
$this->assertStringContainsString($discussion[0]->name, $firstmail->body);
$this->assertStringNotContainsString($this->discussion[0]->name, $firstmail->body);
$this->assertEquals('[email protected]', $secondmail->to);
$this->assertStringContainsString($this->discussion[0]->name, $secondmail->body);
$this->assertStringNotContainsString($discussion[0]->name, $secondmail->body);
}
}


/**
* Test if the content of the mail matches the supposed content.
Expand Down

0 comments on commit 318ac17

Please sign in to comment.