From 05074c1ca36808bccc0e8342a1641693a2932ae5 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 7 Feb 2023 09:31:03 +0000 Subject: [PATCH] MDL-74824 user: fix custom change password URL in login notification. --- lib/classes/task/send_login_notifications.php | 2 +- .../task/send_login_notifications_test.php | 46 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) rename login/tests/login_notifications_test.php => lib/tests/task/send_login_notifications_test.php (86%) diff --git a/lib/classes/task/send_login_notifications.php b/lib/classes/task/send_login_notifications.php index e32cf28d06a..245a518fb5a 100644 --- a/lib/classes/task/send_login_notifications.php +++ b/lib/classes/task/send_login_notifications.php @@ -61,7 +61,7 @@ class send_login_notifications extends adhoc_task { $userauth = get_auth_plugin($USER->auth); if ($userauth->can_change_password()) { if ($changepwurl = $userauth->change_password_url()) { - $changepasswordlink = $changepwurl; + $changepasswordlink = (string) $changepwurl; } else { $changepasswordlink = (new \moodle_url('/login/change_password.php'))->out(false); } diff --git a/login/tests/login_notifications_test.php b/lib/tests/task/send_login_notifications_test.php similarity index 86% rename from login/tests/login_notifications_test.php rename to lib/tests/task/send_login_notifications_test.php index eecd58f52e2..89d89553f86 100644 --- a/login/tests/login_notifications_test.php +++ b/lib/tests/task/send_login_notifications_test.php @@ -14,21 +14,19 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -namespace core; +namespace core\task; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/lib/externallib.php'); +use moodle_url; /** - * Contains tests for course related notifications. + * Contains tests for login related notifications. * * @package core * @copyright 2021 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \core\task\send_login_notifications */ -class login_notifications_test extends \advanced_testcase { +class send_login_notifications_test extends \advanced_testcase { /** * Load required classes @@ -156,6 +154,40 @@ class login_notifications_test extends \advanced_testcase { $this->assertCount(0, $messages); } + /** + * Test new login notification where the user auth method provides a custom change password URL + */ + public function test_login_notification_custom_change_password_url(): void { + global $SESSION; + + $this->resetAfterTest(); + $this->setUser(0); + + // Set LDAP auth change password URL. + $changepasswordurl = (new moodle_url('/changepassword.php'))->out(false); + set_config('changepasswordurl', $changepasswordurl, 'auth_ldap'); + + $ldapuser = $this->getDataGenerator()->create_user(['auth' => 'ldap']); + + // Mock data for test. + $ldapuser->lastip = '1.2.3.4'; + $SESSION->isnewsessioncookie = true; + @complete_user_login($ldapuser); + + // Redirect messages to sink and stop buffer output from CLI task. + $sink = $this->redirectMessages(); + ob_start(); + $this->runAdhocTasks(send_login_notifications::class); + ob_end_clean(); + $messages = $sink->get_messages(); + $sink->close(); + + // Send notification, assert custom change password URL is present. + $this->assertCount(1, $messages); + $this->assertStringContainsString("If you don't recognise this activity, please " . + "change your password.", $messages[0]->fullmessagehtml); + } + /** * Test new mobile app login notification. */