diff --git a/backup/util/helper/async_helper.class.php b/backup/util/helper/async_helper.class.php index b57c4b5eb28..9d7ed598dab 100644 --- a/backup/util/helper/async_helper.class.php +++ b/backup/util/helper/async_helper.class.php @@ -96,6 +96,21 @@ class async_helper { 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. @@ -105,7 +120,7 @@ class async_helper { */ 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, diff --git a/backup/util/helper/tests/async_helper_test.php b/backup/util/helper/tests/async_helper_test.php index da485ab8d9a..b076e55abf1 100644 --- a/backup/util/helper/tests/async_helper_test.php +++ b/backup/util/helper/tests/async_helper_test.php @@ -30,6 +30,7 @@ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); * Asyncronhous helper tests. * * @package core_backup + * @covers \async_helper * @copyright 2018 Matt Porritt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -47,7 +48,7 @@ class async_helper_test extends \advanced_testcase { 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},
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'); @@ -76,15 +77,17 @@ class async_helper_test extends \advanced_testcase { $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); } /**