Merge branch 'MDL-57278-master-5' of https://github.com/xow/moodle

This commit is contained in:
Andrew Nicols
2016-12-08 10:44:49 +08:00
2 changed files with 159 additions and 4 deletions
+16 -4
View File
@@ -6161,13 +6161,25 @@ function send_confirmation_email($user, $confirmationurl = null) {
$subject = get_string('emailconfirmationsubject', '', format_string($site->fullname));
$username = urlencode($user->username);
$username = str_replace('.', '%2E', $username); // Prevent problems with trailing dots.
if (empty($confirmationurl)) {
$confirmationurl = '/login/confirm.php';
}
$confirmationurl = new moodle_url($confirmationurl, array('data' => $user->secret .'/'. $username));
$data->link = $confirmationurl->out(false);
$confirmationurl = new moodle_url($confirmationurl);
// Remove data parameter just in case it was included in the confirmation so we can add it manually later.
$confirmationurl->remove_params('data');
$confirmationpath = $confirmationurl->out(false);
// We need to custom encode the username to include trailing dots in the link.
// Because of this custom encoding we can't use moodle_url directly.
// Determine if a query string is present in the confirmation url.
$hasquerystring = strpos($confirmationpath, '?') !== false;
// Perform normal url encoding of the username first.
$username = urlencode($user->username);
// Prevent problems with trailing dots not being included as part of link in some mail clients.
$username = str_replace('.', '%2E', $username);
$data->link = $confirmationpath . ( $hasquerystring ? '&' : '?') . 'data='. $user->secret .'/'. $username;
$message = get_string('emailconfirmation', '', $data);
$messagehtml = text_to_html(get_string('emailconfirmation', '', $data), false, false, true);
+143
View File
@@ -2892,6 +2892,149 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertEventContextNotUsed($event);
}
/**
* Data provider for test_generate_confirmation_link
* @return Array of confirmation urls and expected resultant confirmation links
*/
public function generate_confirmation_link_provider() {
global $CFG;
return [
"Simple name" => [
"username" => "simplename",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/simplename"
],
"Period in between words in username" => [
"username" => "period.inbetween",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/period%2Einbetween"
],
"Trailing periods in username" => [
"username" => "trailingperiods...",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/trailingperiods%2E%2E%2E"
],
"At symbol in username" => [
"username" => "at@symbol",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/at%40symbol"
],
"Dash symbol in username" => [
"username" => "has-dash",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/has-dash"
],
"Underscore in username" => [
"username" => "under_score",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/under_score"
],
"Many different characters in username" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => null,
"expected" => $CFG->wwwroot . "/login/confirm.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom relative confirmation url" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => "/custom/local/url.php",
"expected" => $CFG->wwwroot . "/custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom relative confirmation url with parameters" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => "/custom/local/url.php?with=param",
"expected" => $CFG->wwwroot . "/custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom local confirmation url" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => $CFG->wwwroot . "/custom/local/url.php",
"expected" => $CFG->wwwroot . "/custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom local confirmation url with parameters" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => $CFG->wwwroot . "/custom/local/url.php?with=param",
"expected" => $CFG->wwwroot . "/custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom external confirmation url" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => "http://moodle.org/custom/external/url.php",
"expected" => "http://moodle.org/custom/external/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom external confirmation url with parameters" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => "http://moodle.org/ext.php?with=some&param=eters",
"expected" => "http://moodle.org/ext.php?with=some&param=eters&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
"Custom external confirmation url with parameters" => [
"username" => "many_-.@characters@[email protected]..",
"confirmationurl" => "http://moodle.org/ext.php?with=some&data=test",
"expected" => "http://moodle.org/ext.php?with=some&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
],
];
}
/**
* Test generate_confirmation_link
* @dataProvider generate_confirmation_link_provider
* @param string $username The name of the user
* @param string $confirmationurl The url the user should go to to confirm
* @param string $expected The expected url of the confirmation link
*/
public function test_generate_confirmation_link($username, $confirmationurl, $expected) {
$this->resetAfterTest();
$sink = $this->redirectEmails();
$user = $this->getDataGenerator()->create_user(
[
"username" => $username,
"confirmed" => false,
"email" => '[email protected]',
]
);
send_confirmation_email($user, $confirmationurl);
$sink->close();
$messages = $sink->get_messages();
$message = array_shift($messages);
$messagebody = quoted_printable_decode($message->body);
$this->assertContains($expected, $messagebody);
}
/**
* Test generate_confirmation_link with custom admin link
*/
public function test_generate_confirmation_link_with_custom_admin() {
global $CFG;
$this->resetAfterTest();
$sink = $this->redirectEmails();
$admin = $CFG->admin;
$CFG->admin = 'custom/admin/path';
$user = $this->getDataGenerator()->create_user(
[
"username" => "many_-.@characters@[email protected]..",
"confirmed" => false,
"email" => '[email protected]',
]
);
$confirmationurl = "/admin/test.php?with=params";
$expected = $CFG->wwwroot . "/" . $CFG->admin . "/test.php?with=params&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E";
send_confirmation_email($user, $confirmationurl);
$sink->close();
$messages = $sink->get_messages();
$message = array_shift($messages);
$messagebody = quoted_printable_decode($message->body);
$sink->close();
$this->assertContains($expected, $messagebody);
$CFG->admin = $admin;
}
/**
* Test remove_course_content deletes course contents
* TODO Add asserts to verify other data related to course is deleted as well.