Skip to content

Commit ce4dbc2

Browse files
committed
basic mail tests functions
1 parent f7685af commit ce4dbc2

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

classes/manager/mail_manager.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function moodleoverflow_send_mails(): bool {
137137
$discussions, $posts, $authors, $recipients);
138138

139139
// Set up the user that receives the mail.
140-
$CFG->branch >= 402 ? cron::setup_user($recipients[$record->usertoid]): cron_setup_user($recipients[$record->usertoid]);
140+
$CFG->branch >= 402 ? cron::setup_user($recipients[$record->usertoid]) : cron_setup_user($recipients[$record->usertoid]);
141141

142142
// Check if the user can see the post.
143143
if (!moodleoverflow_user_can_see_post($moodleoverflows[$record->moodleoverflowid], $discussions[$record->discussionid],
@@ -147,14 +147,14 @@ public static function moodleoverflow_send_mails(): bool {
147147
}
148148

149149
// Determine if the author should be anonymous.
150-
$authoranonymous = match ($record->moodleoverflowanonymous) {
150+
$authoranonymous = match ((int)$record->moodleoverflowanonymous) {
151151
anonymous::NOT_ANONYMOUS => false,
152152
anonymous::EVERYTHING_ANONYMOUS => true,
153153
anonymous::QUESTION_ANONYMOUS => ($record->discussionuserid == $record->authorid)
154154
};
155155

156156
// Set the userfrom variable, that is anonymous or the post author.
157-
$authoranonymous ? $userfrom = core_user::get_noreply_user(): $userfrom = clone($authors[$record->authorid]);
157+
$authoranonymous ? $userfrom = core_user::get_noreply_user() : $userfrom = clone($authors[$record->authorid]);
158158
$userfrom->anonymous = $authoranonymous;
159159

160160
// Cache the recipients capabilities to view full names for the moodleoverflow instance.
@@ -200,7 +200,7 @@ public static function moodleoverflow_send_mails(): bool {
200200
// Build the mail object.
201201
$email = new moodleoverflow_email(
202202
$courses[$record->courseid],
203-
$coursemodules[$record->moodleoverflowid],
203+
$coursemodules[$record->cmid],
204204
$moodleoverflows[$record->moodleoverflowid],
205205
$discussions[$record->discussionid],
206206
$posts[$record->postid],
@@ -262,13 +262,11 @@ public static function moodleoverflow_send_mails(): bool {
262262
$smallmessage, $record->usertolang);
263263

264264
// Finally: send the notification mail.
265-
var_dump("Finally");
266265
$mailsent = message_send($emailmessage);
267-
268266
// Check if an error occurred and mark the post as mailed_error.
269267
if (!$mailsent) {
270268
mtrace('Error: mod/moodleoverflow/classes/manager/mail_manager.php moodleoverflow_send_mails(): ' .
271-
'Could not send out mail for id $record->postid to user $record->usertoid ($record->usertoemail).' .
269+
'Could not send out mail for id $record->postid to user $record->usertoid ($record->usertoemail).' .
272270
' ... not trying again.');
273271
$DB->set_field('moodleoverflow_posts', 'mailed', MOODLEOVERFLOW_MAILED_ERROR, ['id' => $record->postid]);
274272
} else {
@@ -299,7 +297,7 @@ public static function moodleoverflow_send_mails(): bool {
299297
public static function moodleoverflow_get_unmailed_posts($starttime, $endtime): array {
300298
global $DB;
301299

302-
// Define fields that will be get from the database.
300+
// Define fields that will be retrieved from the database.
303301
$postfields = "p.id AS postid, p.message AS postmessage, p.messageformat as postmessageformat, p.modified as postmodified,
304302
p.parent AS postparent, p.userid AS postuserid, p.reviewed AS postreviewed";
305303
$discussionfields = "d.id AS discussionid, d.name AS discussionname, d.userid AS discussionuserid,
@@ -315,7 +313,8 @@ public static function moodleoverflow_get_unmailed_posts($starttime, $endtime):
315313
author.picture AS authorpicture, author.imagealt AS authorimagealt, author.email AS authoremail";
316314
$usertofields = "userto.id AS usertoid, userto.maildigest AS usertomaildigest, userto.description AS usertodescription,
317315
userto.password AS usertopassword, userto.lang AS usertolang, userto.auth AS usertoauth,
318-
userto.suspended AS usertosuspended, userto.deleted AS usertodeleted, userto.emailstop AS usertoemailstop";
316+
userto.suspended AS usertosuspended, userto.deleted AS usertodeleted, userto.emailstop AS usertoemailstop,
317+
userto.email AS usertoemail, userto.username AS usertousername";
319318

320319
$fields = "(ROW_NUMBER() OVER (ORDER BY p.modified)) AS row_num, " . $postfields . ", " . $discussionfields . ", "
321320
. $moodleoverflowfields . ", " . $coursefields . ", " . $cmfields . ", " . $authorfields . ", " . $usertofields;
@@ -402,15 +401,24 @@ public static function moodleoverflow_update_mail_caches(object $record, array &
402401
'picture' => 'authorpicture', 'imagealt' => 'authorimagealt', 'email' => 'authoremail'],
403402
'recipients' => ['id' => 'usertoid', 'description' => 'usertodescription', 'password' => 'usertopassword',
404403
'lang' => 'usertolang', 'auth' => 'usertoauth', 'suspended' => 'usertosuspended',
405-
'deleted' => 'usertodeleted', 'emailstop' => 'usertoemailstop', 'viewfullnames' => [],
406-
'canpost' => []],
404+
'deleted' => 'usertodeleted', 'emailstop' => 'usertoemailstop', 'email' => 'usertoemail',
405+
'username' => 'usertousername'],
407406
];
408407

409408
// Iterate over cache types and update caches if not already set.
410409
foreach ($cachetypes as $cachename => $properties) {
411410
$cachekey = $record->{$properties['id']};
412411
if (!isset(${$cachename}[$cachekey])) {
413-
${$cachename}[$cachekey] = (object) array_map(fn($prop) => $record->{$prop}, $properties);
412+
$obj = new stdClass();
413+
foreach ($properties as $propname => $recordkey) {
414+
$obj->$propname = $record->$recordkey;
415+
}
416+
// Only for recipients, add empty arrays for viewfullnames and canpost.
417+
if ($cachename === 'recipients') {
418+
$obj->viewfullnames = [];
419+
$obj->canpost = [];
420+
}
421+
${$cachename}[$cachekey] = $obj;
414422
}
415423
}
416424
}

classes/output/moodleoverflow_email.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace mod_moodleoverflow\output;
2626

2727
use mod_moodleoverflow\anonymous;
28+
use mod_moodleoverflow\subscriptions;
2829

2930
/**
3031
* Moodleoverflow email renderable for use in e-mail.
@@ -251,8 +252,7 @@ public function __set($name, $value) {
251252
public function get_unsubscribediscussionlink() {
252253

253254
// Check whether the moodleoverflow is subscribable.
254-
$subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow,
255-
\context_module::instance($this->cm->id));
255+
$subscribable = subscriptions::is_subscribable($this->moodleoverflow, \context_module::instance($this->cm->id));
256256
if (!$subscribable) {
257257
return null;
258258
}
@@ -432,7 +432,7 @@ public function get_replylink() {
432432
* @return string
433433
*/
434434
public function get_unsubscribemoodleoverflowlink() {
435-
if (!\mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow,
435+
if (!subscriptions::is_subscribable($this->moodleoverflow,
436436
\context_module::instance($this->cm->id))) {
437437
return null;
438438
}

tests/notification_mail_test.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
namespace block_townsquare;
17+
namespace mod_moodleoverflow;
1818

1919
defined('MOODLE_INTERNAL') || die();
2020

@@ -23,6 +23,7 @@
2323

2424
use mod_moodleoverflow\manager\mail_manager;
2525
use mod_moodleoverflow\subscriptions;
26+
use PHPUnit\Exception;
2627
use stdClass;
2728

2829
/**
@@ -40,8 +41,8 @@
4041
* @copyright 2025 Tamaro Walter
4142
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4243
*
43-
* @covers \mod_moodleoverflow\manager\mail_manager::moodleoverflow_send_mails
4444
*/
45+
#[CoversClass(mail_manager::class)]
4546
final class notification_mail_test extends \advanced_testcase {
4647

4748
// Attributes.
@@ -75,8 +76,12 @@ public function tearDown(): void {
7576
*/
7677
public function test_sortorder(): void {
7778
global $DB;
78-
//var_export($DB->get_records('moodleoverflow_posts'));
79-
mail_manager::moodleoverflow_send_mails();
79+
try {
80+
$result = mail_manager::moodleoverflow_send_mails();
81+
} catch (\dml_exception | \moodle_exception $e) {
82+
$this->fail('Exception thrown during mail sending: ' . $e->getMessage());
83+
}
84+
$this->assertTrue($result);
8085
}
8186

8287
// Helper functions.
@@ -115,11 +120,14 @@ private function helper_course_set_up(): void {
115120
$this->testdata->coursemodule = get_coursemodule_from_instance('moodleoverflow', $this->testdata->moodleoverflow->id);
116121
$this->testdata->discussion = $plugingenerator->post_to_forum($this->testdata->moodleoverflow, $this->testdata->teacher);
117122

118-
// TODO: THIS need to be done with the plugin generator, only temporary solution
119-
// subscribe users to moodleoverflow
120-
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->teacher->id, 'moodleoverflow' => $this->testdata->moodleoverflow->id]);
121-
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->student1->id, 'moodleoverflow' => $this->testdata->moodleoverflow->id]);
122-
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->student2->id, 'moodleoverflow' => $this->testdata->moodleoverflow->id]);
123+
// TODO: THIS need to be done with the plugin generator, only temporary solution.
124+
// Subscribe users to moodleoverflow.
125+
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->teacher->id,
126+
'moodleoverflow' => $this->testdata->moodleoverflow->id, ]);
127+
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->student1->id,
128+
'moodleoverflow' => $this->testdata->moodleoverflow->id, ]);
129+
$DB->insert_record('moodleoverflow_subscriptions', ['userid' => $this->testdata->student2->id,
130+
'moodleoverflow' => $this->testdata->moodleoverflow->id, ]);
123131
}
124132

125133
}

0 commit comments

Comments
 (0)