From 1746ec47dc685d60ee8781829e80d47d61cf8dfa Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 16 Apr 2019 19:37:07 +0800 Subject: [PATCH] Always send emails from 'noreplyuser' (#165) --- classes/task/email_certificate_task.php | 11 ++++------- tests/email_certificate_task_test.php | 6 ------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/classes/task/email_certificate_task.php b/classes/task/email_certificate_task.php index a7a6641..1cf277d 100644 --- a/classes/task/email_certificate_task.php +++ b/classes/task/email_certificate_task.php @@ -73,13 +73,10 @@ class email_certificate_task extends \core\task\scheduled_task { $context = \context::instance_by_id($customcert->contextid); // Get the person we are going to send this email on behalf of. - // Look through the teachers. - if ($teachers = get_enrolled_users($context, 'moodle/course:update')) { - $teachers = sort_by_roleassignment_authority($teachers, $context); - $userfrom = reset($teachers); - } else { // Ok, no teachers, use administrator name. - $userfrom = get_admin(); - } + $userfrom = \core_user::get_noreply_user(); + + // Store teachers for later. + $teachers = get_enrolled_users($context, 'moodle/course:update'); $courseshortname = format_string($customcert->courseshortname, true, array('context' => $context)); $coursefullname = format_string($customcert->coursefullname, true, array('context' => $context)); diff --git a/tests/email_certificate_task_test.php b/tests/email_certificate_task_test.php index b05f7f1..f4f1e1b 100644 --- a/tests/email_certificate_task_test.php +++ b/tests/email_certificate_task_test.php @@ -95,11 +95,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc // Confirm that we sent out emails to the two users. $this->assertCount(2, $emails); - $this->assertContains(fullname($user3), $emails[0]->header); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals($user1->email, $emails[0]->to); - $this->assertContains(fullname($user3), $emails[1]->header); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals($user2->email, $emails[1]->to); @@ -150,11 +148,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc // Confirm that we only sent out 2 emails, both emails to the teacher for the two students. $this->assertCount(2, $emails); - $this->assertContains(fullname($user3), utf8_encode($emails[0]->header)); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals($user3->email, $emails[0]->to); - $this->assertContains(fullname($user3), utf8_encode($emails[1]->header)); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals($user3->email, $emails[1]->to); } @@ -189,11 +185,9 @@ class mod_customcert_task_email_certificate_task_testcase extends advanced_testc // Confirm that we only sent out 2 emails, both emails to the other address that was valid for the two students. $this->assertCount(2, $emails); - $this->assertContains(fullname(get_admin()), utf8_encode($emails[0]->header)); $this->assertEquals($CFG->noreplyaddress, $emails[0]->from); $this->assertEquals('testcustomcert@example.com', $emails[0]->to); - $this->assertContains(fullname(get_admin()), utf8_encode($emails[1]->header)); $this->assertEquals($CFG->noreplyaddress, $emails[1]->from); $this->assertEquals('testcustomcert@example.com', $emails[1]->to); }