Skip to content

Commit

Permalink
Merge branch 'MDL-75573' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Dec 6, 2022
2 parents 3365e5a + be83010 commit 425d4f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
17 changes: 16 additions & 1 deletion backup/util/helper/async_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ private function get_user() {
return $user;
}

/**
* Return appropriate description for current async operation {@see async_helper::type}
*
* @return string
*/
private function get_operation_description(): string {
$operations = [
'backup' => new lang_string('backup'),
'copy' => new lang_string('copycourse'),
'restore' => new lang_string('restore'),
];

return (string) ($operations[$this->type] ?? $this->type);
}

/**
* Callback for preg_replace_callback.
* Replaces message placeholders with real values.
Expand All @@ -105,7 +120,7 @@ private function get_user() {
*/
private function lookup_message_variables($matches) {
$options = array(
'operation' => $this->type,
'operation' => $this->get_operation_description(),
'backupid' => $this->backupid,
'user_username' => $this->user->username,
'user_email' => $this->user->email,
Expand Down
19 changes: 11 additions & 8 deletions backup/util/helper/tests/async_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Asyncronhous helper tests.
*
* @package core_backup
* @covers \async_helper
* @copyright 2018 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand All @@ -47,7 +48,7 @@ public function test_send_message() {
set_config('backup_async_message_users', '1', 'backup');
set_config('backup_async_message_subject', 'Moodle {operation} completed sucessfully', 'backup');
set_config('backup_async_message',
'Dear {user_firstname} {user_lastname}, <br/> Your {operation} (ID: {backupid}) has completed successfully!',
'Dear {user_firstname} {user_lastname}, your {operation} (ID: {backupid}) has completed successfully!',
'backup');
set_config('allowedemaildomains', 'example.com');

Expand Down Expand Up @@ -76,15 +77,17 @@ public function test_send_message() {
$this->assertCount(1, $emails);
$email = reset($emails);

$this->assertSame($USER->email, $email->from);
$this->assertSame($user2->email, $email->to);
$this->assertSame('Moodle backup completed sucessfully', $email->subject);
$this->assertNotEmpty($email->header);
$this->assertNotEmpty($email->body);
$this->assertMatchesRegularExpression("/$backupid/", $email->body);
$this->assertThat($email->body, $this->logicalNot($this->stringContains('{')));
$this->assertGreaterThan(0, $messageid);
$sink->clear();

$this->assertSame($USER->email, $email->from);
$this->assertSame($user2->email, $email->to);
$this->assertSame('Moodle Backup completed sucessfully', $email->subject);

// Assert body placeholders have all been replaced.
$this->assertStringContainsString('Dear test human, your Backup', $email->body);
$this->assertStringContainsString("(ID: {$backupid})", $email->body);
$this->assertStringNotContainsString('{', $email->body);
}

/**
Expand Down

0 comments on commit 425d4f2

Please sign in to comment.