Skip to content

Commit f7cdb4a

Browse files
authored
Merge branch 'master' into bugfix/settingsreset
2 parents e2f8e2c + 0bc711c commit f7cdb4a

File tree

6 files changed

+81
-33
lines changed

6 files changed

+81
-33
lines changed

.github/workflows/moodle-ci.yml

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88

99
strategy:
1010
matrix:
11-
php: ['8.1']
12-
moodle-branch: ['MOODLE_402_STABLE']
11+
php: ['8.2']
12+
moodle-branch: ['MOODLE_403_STABLE']
1313
database: ['pgsql']
1414

1515
steps:
@@ -110,27 +110,21 @@ jobs:
110110
strategy:
111111
fail-fast: false
112112
matrix:
113-
php: ['8.0', '8.1']
114-
moodle-branch: ['MOODLE_401_STABLE', 'MOODLE_402_STABLE']
113+
php: [8.1']
114+
moodle-branch: ['MOODLE_401_STABLE', 'MOODLE_402_STABLE', 'MOODLE_403_STABLE']
115115
database: ['mariadb', 'pgsql']
116116
include:
117-
- php: '7.4'
118-
moodle-branch: 'MOODLE_39_STABLE'
117+
- php: '8.2'
118+
moodle-branch: 'MOODLE_402_STABLE'
119119
database: 'mariadb'
120-
- php: '7.4'
121-
moodle-branch: 'MOODLE_39_STABLE'
120+
- php: '8.2'
121+
moodle-branch: 'MOODLE_402_STABLE'
122122
database: 'pgsql'
123-
- php: '8.0'
124-
moodle-branch: 'MOODLE_311_STABLE'
123+
- php: '8.2'
124+
moodle-branch: 'MOODLE_403_STABLE'
125125
database: 'mariadb'
126-
- php: '8.0'
127-
moodle-branch: 'MOODLE_311_STABLE'
128-
database: 'pgsql'
129-
- php: '8.0'
130-
moodle-branch: 'MOODLE_400_STABLE'
131-
database: 'mariadb'
132-
- php: '8.0'
133-
moodle-branch: 'MOODLE_400_STABLE'
126+
- php: '8.2'
127+
moodle-branch: 'MOODLE_403_STABLE'
134128
database: 'pgsql'
135129

136130
steps:
@@ -193,4 +187,4 @@ jobs:
193187

194188
- name: Behat features
195189
if: ${{ always() }}
196-
run: moodle-plugin-ci behat --auto-rerun 0
190+
run: moodle-plugin-ci behat --auto-rerun 0

classes/task/send_daily_mail.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ public function execute() {
5656
$mail = [];
5757
// Fill the $mail array.
5858
foreach ($userdata as $row) {
59-
$currentcourse = $DB->get_record('course', ['id' => $row->courseid], 'fullname, id');
60-
$currentforum = $DB->get_record('moodleoverflow', ['id' => $row->forumid], 'name, id');
59+
$currentcourse = $DB->get_record('course', array('id' => $row->courseid), 'fullname, id');
60+
// Check if the user is enrolled in the course, if not, go to the next row.
61+
if (!is_enrolled(\context_course::instance($row->courseid), $user->userid, '', true)) {
62+
continue;
63+
}
64+
65+
$currentforum = $DB->get_record('moodleoverflow', array('id' => $row->forumid), 'name, id');
6166
$coursemoduleid = get_coursemodule_from_instance('moodleoverflow', $row->forumid);
6267
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $row->forumdiscussionid], 'name, id');
6368
$unreadposts = $row->numberofposts;
@@ -77,7 +82,7 @@ public function execute() {
7782
'linktoforum' => $linktoforum,
7883
'linktodiscussion' => $linktodiscussion,
7984
'unreadposts' => $unreadposts, ]);
80-
array_push($mail, $string);
85+
$mail[] = $string;
8186
}
8287
// Build the final message and send it to user. Then remove the sent records.
8388
$message = implode('<br>', $mail);

db/install.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@
179179
<TABLE NAME="moodleoverflow_mail_info" COMMENT="represent the content of the digest mail">
180180
<FIELDS>
181181
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
182-
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true"/>
183-
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true"/>
184-
<FIELD NAME="forumid" TYPE="int" LENGTH="10" NOTNULL="true"/>
185-
<FIELD NAME="forumdiscussionid" TYPE="int" LENGTH="10" NOTNULL="true"/>
186-
<FIELD NAME="numberofposts" TYPE="int" LENGTH="10" NOTNULL="true"/>
182+
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
183+
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
184+
<FIELD NAME="forumid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
185+
<FIELD NAME="forumdiscussionid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
186+
<FIELD NAME="numberofposts" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
187187
</FIELDS>
188188
<KEYS>
189189
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

tests/behat/add_moodleoverflow.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Feature: Add moodleoverflow activities and discussions
1919
And I log in as "teacher1"
2020
And I am on "Course 1" course homepage
2121
And I turn editing mode on
22-
And I add a "Moodleoverflow" to section "1" and I fill the form with:
22+
And I add a "moodleoverflow" activity to course "C1" section "1" and I fill the form with:
2323
| Moodleoverflow name | Test moodleoverflow name |
2424
| Description | Test forum description |
2525
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:

tests/behat/delete_file.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Feature: Delete attachments
1717
And I log in as "teacher1"
1818
And I am on "Course 1" course homepage
1919
And I turn editing mode on
20-
And I add a "Moodleoverflow" to section "1" and I fill the form with:
20+
And I add a "moodleoverflow" activity to course "C1" section "1" and I fill the form with:
2121
| Moodleoverflow name | Test moodleoverflow name |
2222
| Description | Test forum description |
2323
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:

tests/dailymail_test.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class dailymail_test extends \advanced_testcase {
5959
/** @var \stdClass discussion instance */
6060
private $discussion;
6161

62+
/** @var moodleoverflow generator */
63+
private $generator;
64+
6265
/**
6366
* Test setUp.
6467
*/
@@ -94,12 +97,13 @@ public function tearDown(): void {
9497
*/
9598
public function helper_create_user_and_discussion($maildigest) {
9699
// 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]);
98102
$this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, 'student');
99103

100104
// 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);
103107
}
104108

105109
/**
@@ -137,7 +141,6 @@ private function helper_run_send_mails() {
137141
* @covers \send_daily_mail::execute
138142
*/
139143
public function test_mail_delivery(): void {
140-
141144
// Create user with maildigest = on.
142145
$this->helper_create_user_and_discussion('1');
143146

@@ -149,6 +152,52 @@ public function test_mail_delivery(): void {
149152
$this->assertEquals(1, $messages);
150153
}
151154

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+
152201

153202
/**
154203
* Test if the content of the mail matches the supposed content.

0 commit comments

Comments
 (0)