From 3413c90bd2cec3bc49146ecfd8b39e56d93f9e05 Mon Sep 17 00:00:00 2001 From: Stevani Andolo Date: Tue, 21 May 2024 16:04:13 +0800 Subject: [PATCH] MDL-80947 communication_customlink: Check before customlink update --- .../customlink/classes/communication_feature.php | 8 ++++++-- .../tests/communication_feature_test.php | 16 ++++++++++++++++ communication/tests/api_test.php | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/communication/provider/customlink/classes/communication_feature.php b/communication/provider/customlink/classes/communication_feature.php index e2b4c760a05..51062176ff3 100644 --- a/communication/provider/customlink/classes/communication_feature.php +++ b/communication/provider/customlink/classes/communication_feature.php @@ -113,13 +113,17 @@ class communication_feature implements } public function save_form_data(\stdClass $instance): void { + if (empty($instance->customlinkurl)) { + return; + } + global $DB; $commid = $this->communication->get_id(); $cachekey = "link_url_{$commid}"; $newrecord = new \stdClass(); - $newrecord->url = $instance->customlinkurl ?? null; + $newrecord->url = $instance->customlinkurl; $existingrecord = $DB->get_record( self::CUSTOMLINK_TABLE, @@ -131,7 +135,7 @@ class communication_feature implements // Create the record if it does not exist. $newrecord->commid = $commid; $DB->insert_record(self::CUSTOMLINK_TABLE, $newrecord); - } else if ($newrecord->url !== $existingrecord->url) { + } else if ($instance->customlinkurl !== $existingrecord->url) { // Update record if the URL has changed. $newrecord->id = $existingrecord->id; $DB->update_record(self::CUSTOMLINK_TABLE, $newrecord); diff --git a/communication/provider/customlink/tests/communication_feature_test.php b/communication/provider/customlink/tests/communication_feature_test.php index d68141f7b40..6c338306089 100644 --- a/communication/provider/customlink/tests/communication_feature_test.php +++ b/communication/provider/customlink/tests/communication_feature_test.php @@ -88,6 +88,22 @@ class communication_feature_test extends \advanced_testcase { $communicationprocessor->get_form_provider()->save_form_data($formdatainstance); $fetchedurl = $communicationprocessor->get_room_provider()->get_chat_room_url(); $this->assertEquals($customlinkurl, $fetchedurl); + + // Test with empty customlinkurl. + $customlinkurlempty = ''; + $formdatainstance = (object) ['customlinkurl' => $customlinkurlempty]; + $communicationprocessor->get_form_provider()->save_form_data($formdatainstance); + $fetchedurl = $communicationprocessor->get_room_provider()->get_chat_room_url(); + // It should not update the url to an empty one. + $this->assertEquals($customlinkurl, $fetchedurl); + + // Test with null customlinkurl. + $customlinkurlempty = null; + $formdatainstance = (object) ['customlinkurl' => $customlinkurlempty]; + $communicationprocessor->get_form_provider()->save_form_data($formdatainstance); + $fetchedurl = $communicationprocessor->get_room_provider()->get_chat_room_url(); + // It should not update the url to a null one. + $this->assertEquals($customlinkurl, $fetchedurl); } /** diff --git a/communication/tests/api_test.php b/communication/tests/api_test.php index 54d35e9986f..f97a32f60f9 100644 --- a/communication/tests/api_test.php +++ b/communication/tests/api_test.php @@ -487,6 +487,8 @@ class api_test extends \advanced_testcase { // Now delete all the ad-hoc tasks. $DB->delete_records('task_adhoc'); + $course->customlinkurl = $course->customlinkurl ?? 'https://moodle.org'; + // Now change the provider to another one. $communication->configure_room_and_membership_by_provider( provider: 'communication_customlink',