Skip to content

Commit

Permalink
MDL-44467 lib: Unit test updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve committed Oct 28, 2016
1 parent 9715f61 commit e078933
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/tests/message_test.php
Expand Up @@ -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");
Expand Down
7 changes: 4 additions & 3 deletions lib/tests/messagelib_test.php
Expand Up @@ -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';
Expand Down Expand Up @@ -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");
Expand Down
96 changes: 88 additions & 8 deletions lib/tests/moodlelib_test.php
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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],
];
}
}

0 comments on commit e078933

Please sign in to comment.