Skip to content

Commit

Permalink
MDL-64148 oauth2: Make email greetings consistent
Browse files Browse the repository at this point in the history
Some email body strings use first names as greetings,
some use full names, and some do not.

Using the first name for greeting makes it consistent and
a bit more "personal".
  • Loading branch information
meirzamoodle committed Jul 30, 2024
1 parent 020259d commit b4f1704
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
10 changes: 8 additions & 2 deletions auth/oauth2/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ public static function send_confirm_link_login_email($userinfo, $issuer, $userid
$user = get_complete_user_data('id', $userid);

$data = new stdClass();
$data->fullname = fullname($user);
$placeholders = \core_user::get_name_placeholders($user);
foreach ($placeholders as $field => $value) {
$data->{$field} = $value;
}
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$data->issuername = format_string($issuer->get('name'));
Expand Down Expand Up @@ -319,7 +322,10 @@ public static function send_confirm_account_email($userinfo, $issuer) {
$user = get_complete_user_data('id', $user->id);

$data = new stdClass();
$data->fullname = fullname($user);
$placeholders = \core_user::get_name_placeholders($user);
foreach ($placeholders as $field => $value) {
$data->{$field} = $value;
}
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();

Expand Down
4 changes: 2 additions & 2 deletions auth/oauth2/lang/en/auth_oauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
$string['accountexists'] = 'A user already exists on this site with this username. If this is your account, log in by entering your username and password and add it as a linked login via your preferences page.';
$string['auth_oauth2description'] = 'OAuth 2 standards based authentication';
$string['auth_oauth2settings'] = 'OAuth 2 authentication settings.';
$string['confirmaccountemail'] = 'Hi {$a->fullname},
$string['confirmaccountemail'] = 'Hi {$a->firstname},
A new account has been requested at \'{$a->sitename}\'
using your email address.
Expand All @@ -47,7 +47,7 @@
$string['confirmaccountemailsubject'] = '{$a}: account confirmation';
$string['confirmationinvalid'] = 'The confirmation link is either invalid, or has expired. Please start the login process again to generate a new confirmation email.';
$string['confirmationpending'] = 'This account is pending email confirmation.';
$string['confirmlinkedloginemail'] = 'Hi {$a->fullname},
$string['confirmlinkedloginemail'] = 'Hi {$a->firstname},
A request has been made to link the {$a->issuername} login
{$a->linkedemail} to your account at \'{$a->sitename}\'
Expand Down
37 changes: 37 additions & 0 deletions auth/oauth2/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @package auth_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \auth_oauth2\api
*/
class api_test extends \advanced_testcase {

Expand Down Expand Up @@ -227,4 +229,39 @@ public function test_send_confirm_account_email(): void {
// Explicitly test the user is not yet confirmed.
$this->assertEquals(0, $userdata->confirmed);
}

/**
* Test case for checking the email greetings in OAuth2 confirmation emails.
*/
public function test_email_greetings(): void {
$this->resetAfterTest();
$this->setAdminUser();

$issuer = \core\oauth2\api::create_standard_issuer('microsoft');

$userinfo = [];
$userinfo['username'] = 'apple';
$userinfo['email'] = '[email protected]';
$userinfo['firstname'] = 'Apple';
$userinfo['lastname'] = 'Fruit';
$sink = $this->redirectEmails(); // Make sure we are redirecting emails.
\auth_oauth2\api::send_confirm_account_email($userinfo, $issuer);
$result = $sink->get_messages();
$sink->close();
// Test greetings.
$this->assertStringContainsString('Hi ' . $userinfo['firstname'], quoted_printable_decode($result[0]->body));

$userinfo = [];
$userinfo['username'] = 'banana';
$userinfo['email'] = '[email protected]';
$userinfo['firstname'] = 'Banana';
$userinfo['lastname'] = 'Fruit';
$user = $this->getDataGenerator()->create_user();
$sink = $this->redirectEmails(); // Make sure we are redirecting emails.
\auth_oauth2\api::send_confirm_link_login_email($userinfo, $issuer, $user->id);
$result = $sink->get_messages();
$sink->close();
// Test greetings.
$this->assertStringContainsString('Hi ' . $user->firstname, quoted_printable_decode($result[0]->body));
}
}
13 changes: 12 additions & 1 deletion auth/oauth2/tests/auth_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @category test
* @copyright 2019 Shamim Rezaie
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \auth_oauth2\auth
* @covers \auth_oauth2\auth
*/
class auth_test extends \advanced_testcase {

Expand Down Expand Up @@ -89,4 +89,15 @@ public function test_oauth2_complete_login(): void {
$extrauserinfo = $event->other['extrauserinfo'];
$this->assertEquals($info, $extrauserinfo);
}

/**
* Test case for checking the email greetings in the password change information email.
*/
public function test_email_greetings(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['auth' => 'oauth2']);
$auth = get_auth_plugin($user->auth);
$info = $auth->get_password_change_info($user);
$this->assertStringContainsString('Hi ' . $user->firstname, quoted_printable_decode($info['message']));
}
}

0 comments on commit b4f1704

Please sign in to comment.