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