3131global $ CFG ;
3232require_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+ */
3442class 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}
0 commit comments