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 bb1eb99d25e..173fa7f663c 100644 --- a/communication/tests/api_test.php +++ b/communication/tests/api_test.php @@ -476,6 +476,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',