Skip to content

Commit 0f45643

Browse files
committed
dailymail_test feature added
1 parent 8d4638d commit 0f45643

File tree

3 files changed

+174
-7
lines changed

3 files changed

+174
-7
lines changed

classes/task/send_daily_mail.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public function get_name() {
4242
*/
4343
public function execute() {
4444
global $DB;
45-
// Call your own api
45+
// Call your own api.
4646
$users = $DB->get_records_sql('SELECT DISTINCT userid FROM {moodleoverflow_mail_info}');
47+
if (empty($users)) {
48+
mtrace('No daily mail to send.');
49+
return;
50+
}
4751
foreach($users as $user) {
4852
$userdata = $DB->get_records('moodleoverflow_mail_info', array('userid' => $user->userid), 'courseid, forumid'); //order by courseid
4953
$mail = array();
@@ -59,8 +63,8 @@ public function execute() {
5963
array_push($mail,$string);
6064
}
6165
$message = implode('<br>', $mail);
62-
mtrace($message);
63-
//send message to user
66+
// mtrace($message);.
67+
// send message to user.
6468
$userto = $DB->get_record('user', array('id' => $user->userid));
6569
$from = \core_user::get_noreply_user();
6670
$subject = get_string('tasksenddailymail', 'mod_moodleoverflow');

lib.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,11 @@ function moodleoverflow_send_mails() {
733733
$errorcount[$postid] = 0;
734734
}
735735
}
736-
736+
737737
// Send mails to the users with information about the posts.
738738
if ($users && $posts) {
739-
740739
// Send one mail to every user.
741740
foreach ($users as $userto) {
742-
743741
// Terminate if the process takes more time then two minutes.
744742
core_php_time_limit::raise(120);
745743

@@ -764,7 +762,6 @@ function moodleoverflow_send_mails() {
764762

765763
// Loop through all posts of this users.
766764
foreach ($posts as $postid => $post) {
767-
768765

769766

770767
// Initiate variables for the post.

tests/dailymail_test.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* The module moodleoverflow tests.
19+
*
20+
* @package mod_moodleoverflow
21+
* @copyright 2023 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
namespace mod_moodleoverflow;
25+
26+
use mod_moodleoverflow\task\send_mails;
27+
use mod_moodleoverflow\task\send_daily_mail;
28+
29+
defined('MOODLE_INTERNAL') || die();
30+
31+
global $CFG;
32+
require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php');
33+
34+
class mod_moodleoverflow_dailymail_test extends \advanced_testcase {
35+
36+
private $sink;
37+
private $messagesink;
38+
private $course;
39+
private $user;
40+
private $moodleoverflow;
41+
private $discussion;
42+
43+
/**
44+
* Test setUp.
45+
*/
46+
public function setUp(): void {
47+
$this->resetAfterTest();
48+
set_config('maxeditingtime', -10, 'moodleoverflow');
49+
50+
unset_config('noemailever');
51+
$this->sink = $this->redirectEmails();
52+
53+
$this->preventResetByRollback();
54+
$this->messagesink = $this->redirectMessages();
55+
56+
// Create a new course with a moodleoverflow forum.
57+
$this->course = $this->getDataGenerator()->create_course();
58+
$location = array('course' => $this->course->id,'forcesubscribe' => MOODLEOVERFLOW_FORCESUBSCRIBE);
59+
$this->moodleoverflow = $this->getDataGenerator()->create_module('moodleoverflow',$location);
60+
61+
}
62+
63+
/**
64+
* Test tearDown.
65+
*/
66+
public function tearDown(): void {
67+
// Clear all caches.
68+
\mod_moodleoverflow\subscriptions::reset_moodleoverflow_cache();
69+
\mod_moodleoverflow\subscriptions::reset_discussion_cache();
70+
}
71+
72+
/**
73+
* Function that creates a new user, which adds a new discussion an post to the moodleoverflow.
74+
*/
75+
public function helper_create_user_and_discussion($maildigest) {
76+
// Create a user enrolled in the course as student.
77+
$this->user = $this->getDataGenerator()->create_user(array('firstname' => 'Tamaro', 'maildigest' => $maildigest));
78+
$this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, 'student');
79+
80+
//Create a new discussion and post within the moodleoverflow.
81+
$generator = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow');
82+
$this->discussion = $generator->post_to_forum($this->moodleoverflow, $this->user);
83+
}
84+
85+
/**
86+
* Run the send daily mail task.
87+
* @return false|string
88+
*/
89+
private function run_send_daily_mail() {
90+
$mailtask = new send_daily_mail();
91+
ob_start();
92+
$mailtask->execute();
93+
$output = ob_get_contents();
94+
ob_end_clean();
95+
return $output;
96+
}
97+
98+
/**
99+
* Run the send mails task.
100+
* @return false|string
101+
*/
102+
private function run_send_mails() {
103+
$mailtask = new send_mails();
104+
ob_start();
105+
$mailtask->execute();
106+
$output = ob_get_contents();
107+
ob_end_clean();
108+
return $output;
109+
}
110+
111+
/**
112+
* Test if the task send_daily_mail sends a mail to the user
113+
*/
114+
public function test_mail_delivery() {
115+
global $DB;
116+
117+
// Create users with maildigest = on
118+
$this->helper_create_user_and_discussion('1');
119+
120+
// Send a mail and test if the mail was sent.
121+
122+
$this->run_send_mails(); //content2
123+
$this->run_send_daily_mail(); //content
124+
$messages = $this->sink->count();
125+
126+
$this->assertEquals(1, $messages);
127+
}
128+
129+
public function test_content_of_mail_delivery() {
130+
global $DB;
131+
132+
// Creat Users with maildigest = on.
133+
$this->helper_create_user_and_discussion('1');
134+
135+
//send the mails and count the messages.
136+
$this->run_send_mails();
137+
$content = $this->run_send_daily_mail();
138+
$messages = $this->sink->count();
139+
140+
//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);
152+
}
153+
154+
public function test_mail_not_send() {
155+
global $DB;
156+
// Creat Users with daily_mail = off.
157+
$this->helper_create_user_and_discussion('0');
158+
159+
// Now send the mails and test if no mail was sent.
160+
$this->run_send_mails();
161+
$this->run_send_daily_mail();
162+
$messages = $this->sink->count();
163+
164+
$this->assertEquals(0,$messages);
165+
}
166+
}

0 commit comments

Comments
 (0)