diff --git a/lib/tests/message_test.php b/lib/tests/message_test.php index 7377f898be88a..f50276d6644f6 100644 --- a/lib/tests/message_test.php +++ b/lib/tests/message_test.php @@ -131,8 +131,9 @@ public function test_send_message() { $this->preventResetByRollback(); $this->resetAfterTest(); - $user1 = $this->getDataGenerator()->create_user(); + $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); $user2 = $this->getDataGenerator()->create_user(); + set_config('allowedemaildomains', 'example.com'); // Test basic email processor. $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); diff --git a/lib/tests/messagelib_test.php b/lib/tests/messagelib_test.php index f5641dd3e52ee..a9654e40eedf2 100644 --- a/lib/tests/messagelib_test.php +++ b/lib/tests/messagelib_test.php @@ -36,11 +36,11 @@ public function test_message_provider_disabled() { set_config($disableprovidersetting, 1, 'message'); $preferences = get_message_output_default_preferences(); $this->assertTrue($preferences->$disableprovidersetting == 1); - $noreplyuser = core_user::get_noreply_user(); + $message = new stdClass(); $message->component = 'moodle'; $message->name = 'instantmessage'; - $message->userfrom = $noreplyuser->email; + $message->userfrom = get_admin(); $message->userto = $this->getDataGenerator()->create_user();; $message->subject = 'message subject 1'; $message->fullmessage = 'message body'; @@ -379,8 +379,9 @@ public function test_send_message() { $this->preventResetByRollback(); $this->resetAfterTest(); - $user1 = $this->getDataGenerator()->create_user(); + $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); $user2 = $this->getDataGenerator()->create_user(); + set_config('allowedemaildomains', 'example.com'); // Test basic email redirection. $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index e322a9bce21d0..af169a5de5e38 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2790,8 +2790,10 @@ public function test_email_to_user() { $this->resetAfterTest(); - $user1 = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); + $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); + $user2 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); + $user3 = $this->getDataGenerator()->create_user(array('maildisplay' => 0)); + set_config('allowedemaildomains', 'example.com'); $subject = 'subject'; $messagetext = 'message text'; @@ -2833,16 +2835,29 @@ public function test_email_to_user() { email_to_user($user1, $user2, $subject, $messagetext); $this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()'); - // Test $CFG->emailonlyfromnoreplyaddress. - set_config('emailonlyfromnoreplyaddress', 1); - $this->assertNotEmpty($CFG->emailonlyfromnoreplyaddress); + // Test that an empty noreplyaddress will default to a no-reply address. + $sink = $this->redirectEmails(); + email_to_user($user1, $user3, $subject, $messagetext); + $result = $sink->get_messages(); + $this->assertEquals($CFG->noreplyaddress, $result[0]->from); + $sink->close(); + set_config('noreplyaddress', ''); + $sink = $this->redirectEmails(); + email_to_user($user1, $user3, $subject, $messagetext); + $result = $sink->get_messages(); + $this->assertEquals('noreply@www.example.com', $result[0]->from); + $sink->close(); + + // Test $CFG->allowedemaildomains. + set_config('noreplyaddress', 'noreply@www.example.com'); + $this->assertNotEmpty($CFG->allowedemaildomains); $sink = $this->redirectEmails(); email_to_user($user1, $user2, $subject, $messagetext); - unset_config('emailonlyfromnoreplyaddress'); + unset_config('allowedemaildomains'); email_to_user($user1, $user2, $subject, $messagetext); $result = $sink->get_messages(); - $this->assertEquals($CFG->noreplyaddress, $result[0]->from); - $this->assertNotEquals($CFG->noreplyaddress, $result[1]->from); + $this->assertNotEquals($CFG->noreplyaddress, $result[0]->from); + $this->assertEquals($CFG->noreplyaddress, $result[1]->from); $sink->close(); } @@ -3202,4 +3217,69 @@ public function test_ip_is_public_public_ips($ip) { $this->assertTrue(ip_is_public($ip)); } + /** + * Test the function can_send_from_real_email_address + * + * @param string $email Email address for the from user. + * @param int $display The user's email display preference. + * @param bool $samecourse Are the users in the same course? + * @param bool $result The expected result. + * @dataProvider data_can_send_from_real_email_address + */ + public function test_can_send_from_real_email_address($email, $display, $samecourse, $result) { + global $DB; + $this->resetAfterTest(); + + $fromuser = $this->getDataGenerator()->create_user(); + $touser = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(); + $alloweddomains = ['example.com']; + + $fromuser->email = $email; + $fromuser->maildisplay = $display; + if ($samecourse) { + $this->getDataGenerator()->enrol_user($fromuser->id, $course->id, 'student'); + $this->getDataGenerator()->enrol_user($touser->id, $course->id, 'student'); + } else { + $this->getDataGenerator()->enrol_user($fromuser->id, $course->id, 'student'); + } + $this->assertEquals($result, can_send_from_real_email_address($fromuser, $touser, $alloweddomains)); + } + + /** + * Data provider for test_can_send_from_real_email_address. + * + * @return array Returns an array of test data for the above function. + */ + public function data_can_send_from_real_email_address() { + return [ + // Test from email is in allowed domain. + // Test that from display is set to show no one. + ['email' => 'fromuser@example.com', 'display' => core_user::MAILDISPLAY_HIDE, + 'samecourse' => false, 'result' => false], + // Test that from display is set to course members only (course member). + ['email' => 'fromuser@example.com', 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'samecourse' => true, 'result' => true], + // Test that from display is set to course members only (Non course member). + ['email' => 'fromuser@example.com', 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'samecourse' => false, 'result' => false], + // Test that from display is set to show everyone. + ['email' => 'fromuser@example.com', 'display' => core_user::MAILDISPLAY_EVERYONE, + 'samecourse' => false, 'result' => true], + + // Test from email is not in allowed domain. + // Test that from display is set to show no one. + ['email' => 'fromuser@moodle.com', 'display' => core_user::MAILDISPLAY_HIDE, + 'samecourse' => false, 'result' => false], + // Test that from display is set to course members only (course member). + ['email' => 'fromuser@moodle.com', 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'samecourse' => true, 'result' => false], + // Test that from display is set to course members only (Non course member. + ['email' => 'fromuser@moodle.com', 'display' => core_user::MAILDISPLAY_COURSE_MEMBERS_ONLY, + 'samecourse' => false, 'result' => false], + // Test that from display is set to show everyone. + ['email' => 'fromuser@moodle.com', 'display' => core_user::MAILDISPLAY_EVERYONE, + 'samecourse' => false, 'result' => false], + ]; + } }