From 8d0b7a8674cf0f382b0ad2f92d87da659d80ace5 Mon Sep 17 00:00:00 2001 From: Tasio Date: Fri, 18 Jul 2025 12:17:53 +0200 Subject: [PATCH 1/2] MDL-86043 core: test for the task sending new users an email --- .../send_new_user_passwords_task_test.php | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 public/lib/tests/task/send_new_user_passwords_task_test.php diff --git a/public/lib/tests/task/send_new_user_passwords_task_test.php b/public/lib/tests/task/send_new_user_passwords_task_test.php new file mode 100644 index 00000000000..1fa19b8d0a9 --- /dev/null +++ b/public/lib/tests/task/send_new_user_passwords_task_test.php @@ -0,0 +1,79 @@ +. + +declare(strict_types=1); + +namespace core\task; + +/** + * File containing tests for send_new_user_passwords_task + * + * @package core + * @category test + * @covers \core\task\send_new_user_passwords_task + * @copyright 2025 Moodle Pty Ltd + * @author 2025 Tasio Bertomeu Gomez + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class send_new_user_passwords_task_test extends \advanced_testcase { + /** + * Validate the content of the email sent to new users + */ + public function test_send_new_user_password_task(): void { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + $user1 = self::getDataGenerator()->create_user([ + 'username' => 'student1', + 'firstname' => 'StudentA', + 'lastname' => 'One', + 'email' => 's1@example.com', + ]); + $user2 = self::getDataGenerator()->create_user([ + 'username' => 'student2', + 'firstname' => 'StudentB', + 'lastname' => 'One', + 'email' => 's2@example.com', + ]); + set_user_preference('create_password', 1, $user1); + set_user_preference('create_password', 1, $user2); + + $sink = $this->redirectEmails(); + $task = new \core\task\send_new_user_passwords_task(); + ob_start(); + $task->execute(); + ob_end_clean(); + $emails = $sink->get_messages(); + $sink->close(); + + // Email for the new users. + $emailonebody = quoted_printable_decode($emails[0]->body); + $this->assertStringContainsString("Hi StudentA,", $emailonebody); + $this->assertStringContainsString("An account has been created for you at 'PHPUnit test site'", $emailonebody); + $this->assertStringContainsString("username: student1", $emailonebody); + $this->assertStringContainsString('https://www.example.com/moodle/login/?lang=en', $emailonebody); + $this->assertStringContainsString('https://www.example.com/moodle/user/contactsitesupport.php', $emailonebody); + $this->assertStringContainsString('PHPUnit test site: New user account', $emails[0]->subject); + + $emailtwobody = quoted_printable_decode($emails[1]->body); + $this->assertStringContainsString("Hi StudentB,", $emailtwobody); + $this->assertStringContainsString("An account has been created for you at 'PHPUnit test site'", $emailtwobody); + $this->assertStringContainsString("username: student2", $emailtwobody); + $this->assertStringContainsString('https://www.example.com/moodle/login/?lang=en', $emailtwobody); + $this->assertStringContainsString('https://www.example.com/moodle/user/contactsitesupport.php', $emailtwobody); + $this->assertStringContainsString('PHPUnit test site: New user account', $emails[1]->subject); + } +} From da92cfbeee108999ad9e89e60c4a307c5b108ccb Mon Sep 17 00:00:00 2001 From: Tasio Date: Mon, 21 Jul 2025 10:50:37 +0200 Subject: [PATCH 2/2] MDL-86043 core: keep SQL consistent with other Moodle versions. This change resolves issues with Oracle in Moodle 4.5. It has been ported to newer Moodle versions as it improves the existing SQL and helps maintain consistency across different Moodle versions. --- public/lib/classes/task/send_new_user_passwords_task.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/lib/classes/task/send_new_user_passwords_task.php b/public/lib/classes/task/send_new_user_passwords_task.php index 57002c2c5a8..32ec30cf365 100644 --- a/public/lib/classes/task/send_new_user_passwords_task.php +++ b/public/lib/classes/task/send_new_user_passwords_task.php @@ -55,6 +55,7 @@ class send_new_user_passwords_task extends scheduled_task { mtrace('Creating passwords for new users...'); $userfieldsapi = \core_user\fields::for_name(); $usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; + [$sort, $sortparams] = users_order_by_sql('u'); $newusers = $DB->get_recordset_sql("SELECT u.id as id, u.email, u.auth, u.deleted, u.suspended, u.emailstop, u.mnethostid, u.mailformat, $usernamefields, u.username, u.lang, @@ -66,7 +67,8 @@ class send_new_user_passwords_task extends scheduled_task { AND u.email <> '' AND u.suspended = 0 AND u.auth <> 'nologin' - AND u.deleted = 0"); + AND u.deleted = 0 + ORDER BY $sort ASC", $sortparams); // Note: we can not send emails to suspended accounts. foreach ($newusers as $newuser) {