Skip to content

Commit ff4dfd5

Browse files
committed
phpunit test update, add links to digest mail
1 parent af53432 commit ff4dfd5

File tree

4 files changed

+95
-38
lines changed

4 files changed

+95
-38
lines changed

Diff for: classes/task/send_daily_mail.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424
namespace mod_moodleoverflow\task;
25+
2526
defined('MOODLE_INTERNAL') || die();
2627
/**
2728
* This task sends a daily mail of unread posts
@@ -42,34 +43,50 @@ public function get_name() {
4243
*/
4344
public function execute() {
4445
global $DB;
46+
4547
// Call your own api.
4648
$users = $DB->get_records_sql('SELECT DISTINCT userid FROM {moodleoverflow_mail_info}');
4749
if (empty($users)) {
4850
mtrace('No daily mail to send.');
4951
return;
5052
}
53+
// Go through each user that has unread posts.
5154
foreach ($users as $user) {
5255
$userdata = $DB->get_records('moodleoverflow_mail_info', array('userid' => $user->userid), 'courseid, forumid'); // order by courseid
5356
$mail = array();
57+
// fill the $mail array.
5458
foreach ($userdata as $row) {
55-
$currentcourse = $DB->get_record('course', array('id' => $row->courseid), 'fullname');
56-
$currentforum = $DB->get_record('moodleoverflow', array('id' => $row->forumid), 'name');
57-
$discussion = $DB->get_record('moodleoverflow_discussions', array('id' => $row->forumdiscussionid), 'name');
59+
$currentcourse = $DB->get_record('course', array('id' => $row->courseid), 'fullname, id');
60+
$currentforum = $DB->get_record('moodleoverflow', array('id' => $row->forumid), 'name, id');
61+
$coursemoduleid = get_coursemodule_from_instance('moodleoverflow', $row->forumid);
62+
$discussion = $DB->get_record('moodleoverflow_discussions', array('id' => $row->forumdiscussionid), 'name, id');
5863
$unreadposts = $row->numberofposts;
59-
$string = get_string('digestunreadpost', 'mod_moodleoverflow', array('currentcourse' => $currentcourse->fullname,
60-
'currentforum' => $currentforum->name,
61-
'discussion' => $discussion->name,
64+
65+
// build url to the course, forum, and discussion.
66+
$linktocourse = new \moodle_url('/course/view.php', array('id' => $currentcourse->id));
67+
$linktoforum = new \moodle_url('/mod/moodleoverflow/view.php', array('id' => $coursemoduleid->id));
68+
$linktodiscussion = new \moodle_url('/mod/moodleoverflow/discussion.php', array('d' => $discussion->id));
69+
70+
// now change the url to a clickable html link.
71+
$linktocourse = \html_writer::link($linktocourse->out(), $currentcourse->fullname);
72+
$linktoforum = \html_writer::link($linktoforum->out(), $currentforum->name);
73+
$linktodiscussion = \html_writer::link($linktodiscussion->out(), $discussion->name);
74+
75+
// build a single line string with the digest information and add it to the mailarray.
76+
$string = get_string('digestunreadpost', 'mod_moodleoverflow', array('linktocourse' => $linktocourse,
77+
'linktoforum' => $linktoforum,
78+
'linktodiscussion' => $linktodiscussion,
6279
'unreadposts' => $unreadposts));
6380
array_push($mail, $string);
6481
}
82+
// build the final message and send it to user. Then remove the sent records.
6583
$message = implode('<br>', $mail);
66-
// mtrace($message);.
67-
// send message to user.
6884
$userto = $DB->get_record('user', array('id' => $user->userid));
6985
$from = \core_user::get_noreply_user();
7086
$subject = get_string('tasksenddailymail', 'mod_moodleoverflow');
71-
87+
mtrace($message);
7288
email_to_user($userto, $from, $subject, $message);
89+
$DB->delete_records('moodleoverflow_mail_info', array('userid' => $user->userid));
7390
}
7491
}
7592
}

Diff for: lang/en/moodleoverflow.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,5 +457,5 @@
457457

458458

459459
// Daily mail message.
460-
$string['digestunreadpost'] = 'Course: {$a->currentcourse} -> {$a->currentforum}, Topic: {$a->discussion} has {$a->unreadposts} unread posts.';
460+
$string['digestunreadpost'] = 'Course: {$a->linktocourse} -> {$a->linktoforum}, Topic: {$a->linktodiscussion} has {$a->unreadposts} unread posts.';
461461

Diff for: tests/dailymail_test.php

+67-27
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@
3131
global $CFG;
3232
require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php');
3333

34+
35+
/**
36+
* Class mod_moodleoverflow_dailymail_testcase.
37+
*
38+
* @package mod_moodleoverflow
39+
* @copyright 2023 Tamaro Walter
40+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41+
*/
3442
class dailymail_test extends \advanced_testcase {
3543

3644
private $sink;
37-
private $messagesink;
3845
private $course;
3946
private $user;
4047
private $moodleoverflow;
48+
private $coursemodule;
4149
private $discussion;
4250

4351
/**
@@ -51,13 +59,12 @@ public function setUp(): void {
5159
$this->sink = $this->redirectEmails();
5260

5361
$this->preventResetByRollback();
54-
$this->messagesink = $this->redirectMessages();
55-
62+
$this->redirectMessages();
5663
// Create a new course with a moodleoverflow forum.
5764
$this->course = $this->getDataGenerator()->create_course();
5865
$location = array('course' => $this->course->id, 'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE);
5966
$this->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow', $location);
60-
67+
$this->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->moodleoverflow->id);
6168
}
6269

6370
/**
@@ -69,8 +76,13 @@ public function tearDown(): void {
6976
\mod_moodleoverflow\subscriptions::reset_discussion_cache();
7077
}
7178

79+
80+
81+
// Helper functions.
82+
7283
/**
7384
* Function that creates a new user, which adds a new discussion an post to the moodleoverflow.
85+
* @param $maildigest The maildigest setting: 0 = off , 1 = on
7486
*/
7587
public function helper_create_user_and_discussion($maildigest) {
7688
// Create a user enrolled in the course as student.
@@ -86,7 +98,7 @@ public function helper_create_user_and_discussion($maildigest) {
8698
* Run the send daily mail task.
8799
* @return false|string
88100
*/
89-
private function run_send_daily_mail() {
101+
private function helper_run_send_daily_mail() {
90102
$mailtask = new send_daily_mail();
91103
ob_start();
92104
$mailtask->execute();
@@ -99,7 +111,7 @@ private function run_send_daily_mail() {
99111
* Run the send mails task.
100112
* @return false|string
101113
*/
102-
private function run_send_mails() {
114+
private function helper_run_send_mails() {
103115
$mailtask = new send_mails();
104116
ob_start();
105117
$mailtask->execute();
@@ -108,59 +120,87 @@ private function run_send_mails() {
108120
return $output;
109121
}
110122

123+
124+
125+
// Begin of test functions.
126+
111127
/**
112-
* Test if the task send_daily_mail sends a mail to the user
128+
* Test if the task send_daily_mail sends a mail to the user.
113129
*/
114130
public function test_mail_delivery() {
115131
global $DB;
116132

117-
// Create users with maildigest = on
133+
// Create user with maildigest = on
118134
$this->helper_create_user_and_discussion('1');
119135

120136
// Send a mail and test if the mail was sent.
121137

122-
$this->run_send_mails(); // content2
123-
$this->run_send_daily_mail(); // content
138+
$this->helper_run_send_mails(); // content2
139+
$this->helper_run_send_daily_mail(); // content
124140
$messages = $this->sink->count();
125141

126142
$this->assertEquals(1, $messages);
127143
}
128144

145+
146+
/**
147+
* Test if the content of the mail matches the supposed content
148+
*/
129149
public function test_content_of_mail_delivery() {
130150
global $DB;
131151

132-
// Creat Users with maildigest = on.
152+
// Creat user with maildigest = on.
133153
$this->helper_create_user_and_discussion('1');
134154

135155
// send the mails and count the messages.
136-
$this->run_send_mails();
137-
$content = $this->run_send_daily_mail();
156+
$this->helper_run_send_mails();
157+
$content = $this->helper_run_send_daily_mail();
158+
$content = str_replace(["\n\r", "\n", "\r"], '', $content);
138159
$messages = $this->sink->count();
139160

140161
// Build the text that the mail should have.
141-
// Text structure: $string['digestunreadpost'] = 'Course: {$a->currentcourse} -> {$a->currentforum}, Topic: {$a->discussion} has {$a->unreadposts} unread posts.';.
142-
$currentcourse = $this->course->fullname;
143-
$currentforum = $this->moodleoverflow->name;
144-
$currentdiscussion = $this->discussion[0]->name;
145-
$text = 'Course: ' . $currentcourse . ' -> ' . $currentforum . ', Topic: ' . $currentdiscussion . ' has ' . $messages . ' unread posts.';
146-
$content = str_replace("\r\n", "", $content);
147-
$text = str_replace("\r\n", "", $text);
148-
149-
// $this->assertisInt(0, strcmp($text, $content)); //strcmp compares 2 strings and retuns 0 if equal
150-
// $this->assertEquals($text, $content);
151-
$this->assertStringContainsString($text, $content);
162+
// Text structure: $string['digestunreadpost'] = 'Course: {$a->linktocourse}-> {$a->linktoforum}, Topic: {$a->linktodiscussion} has {$a->unreadposts} unread posts.';.
163+
$linktocourse = '<a href="https://www.example.com/moodle/course/view.php?id=' . $this->course->id . '">' . $this->course->fullname . '</a>';
164+
$linktoforum = '<a href="https://www.example.com/moodle/mod/moodleoverflow/view.php?id=' . $this->coursemodule->id . '">' . $this->moodleoverflow->name . '</a>';
165+
$linktodiscussion = '<a href="https://www.example.com/moodle/mod/moodleoverflow/discussion.php?d=' . $this->discussion[0]->id . '">' . $this->discussion[0]->name . '</a>';
166+
167+
// assemble text
168+
$text = 'Course: ' . $linktocourse . ' -> ' . $linktoforum . ', Topic: ' . $linktodiscussion . ' has ' . $messages . ' unread posts.';
169+
170+
$this->assertEquals($text, $content);
152171
}
153172

173+
174+
/**
175+
* Test if the task does not send a mail when maildigest = 0
176+
*/
154177
public function test_mail_not_send() {
155178
global $DB;
156-
// Creat Users with daily_mail = off.
179+
// Creat user with daily_mail = off.
157180
$this->helper_create_user_and_discussion('0');
158181

159182
// Now send the mails and test if no mail was sent.
160-
$this->run_send_mails();
161-
$this->run_send_daily_mail();
183+
$this->helper_run_send_mails();
184+
$this->helper_run_send_daily_mail();
162185
$messages = $this->sink->count();
163186

164187
$this->assertEquals(0, $messages);
165188
}
189+
190+
/**
191+
* Test if database is updated after sending a mail
192+
*/
193+
public function test_records_removed() {
194+
global $DB;
195+
// create user with maildigest = on.
196+
$this->helper_create_user_and_discussion('1');
197+
198+
// Now send the mails.
199+
$this->helper_run_send_mails();
200+
$this->helper_run_send_daily_mail();
201+
202+
// Now check the database if the records of the users are deleted.
203+
$records = $DB->get_records('moodleoverflow_mail_info', array('userid' => $this->user->id));
204+
$this->assertEmpty($records);
205+
}
166206
}

Diff for: version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
defined('MOODLE_INTERNAL') || die();
2929

3030
$plugin->component = 'mod_moodleoverflow';
31-
$plugin->version = 2023022404;
31+
$plugin->version = 2023022405;
3232
$plugin->release = 'v4.1-r1';
3333
$plugin->requires = 2020061500; // Requires Moodle 3.9+.
3434
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)