MDL-76704 core_communication: Default setting for communication provider

This commit is contained in:
Safat
2023-06-21 14:46:32 +10:00
parent 1b1a15a308
commit 3f48306385
3 changed files with 79 additions and 3 deletions
+25 -3
View File
@@ -115,20 +115,42 @@ class api {
return $selection;
}
/**
* Get the enabled communication providers and default provider according to the selected provider.
*
* @param string|null $selecteddefaulprovider
* @return array
*/
public static function get_enabled_providers_and_default(string $selecteddefaulprovider = null): array {
$communicationproviders = self::get_communication_plugin_list_for_form();
$defaulprovider = processor::PROVIDER_NONE;
if (!empty($selecteddefaulprovider) && array_key_exists($selecteddefaulprovider, $communicationproviders)) {
$defaulprovider = $selecteddefaulprovider;
}
return [$communicationproviders, $defaulprovider];
}
/**
* Define the form elements for the communication api.
* This method will be called from the form definition method of the instance.
*
* @param \MoodleQuickForm $mform The form element
* @param string $selectdefaultcommunication The default selected communication provider in the form field
*/
public function form_definition(\MoodleQuickForm $mform): void {
public function form_definition(
\MoodleQuickForm $mform,
string $selectdefaultcommunication = processor::PROVIDER_NONE
): void {
global $PAGE;
list($communicationproviders, $defaulprovider) = self::
get_enabled_providers_and_default($selectdefaultcommunication);
$PAGE->requires->js_call_amd('core_communication/providerchooser', 'init');
$mform->addElement('header', 'communication', get_string('communication', 'communication'));
// List the communication providers.
$communicationproviders = self::get_communication_plugin_list_for_form();
$mform->addElement(
'select',
'selectedcommunication',
@@ -137,7 +159,7 @@ class api {
['data-communicationchooser-field' => 'selector'],
);
$mform->addHelpButton('selectedcommunication', 'seleccommunicationprovider', 'communication');
$mform->setDefault('selectedcommunication', processor::PROVIDER_NONE);
$mform->setDefault('selectedcommunication', $defaulprovider);
$mform->registerNoSubmitButton('updatecommunicationprovider');
$mform->addElement('submit',
+14
View File
@@ -312,4 +312,18 @@ class api_test extends \advanced_testcase {
$adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room');
$this->assertCount(1, $adhoctask);
}
/**
* Test the enabled communication plugin list and default.
*
* @covers ::get_enabled_providers_and_default
*/
public function test_get_enabled_providers_and_default(): void {
list($communicationproviders, $defaulprovider) = \core_communication\api::get_enabled_providers_and_default();
// Get the communication plugins.
$plugins = \core_component::get_plugin_list('communication');
// Check the number of plugins matches plus 1 as we have none in the selection.
$this->assertCount(count($plugins) + 1, $communicationproviders);
$this->assertEquals(processor::PROVIDER_NONE, $defaulprovider);
}
}
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../tests/communication_test_helper_trait.php');
/**
* Class behat_communication for behat custom steps and configuration for communication api.
*
* @package core_communication
* @category test
* @copyright 2023 Safat Shahin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_communication extends \behat_base {
use \core_communication\communication_test_helper_trait;
/**
* Configure and enable communication experimental feature.
*
* @Given /^I enable communication experimental feature$/
*/
public function enable_communication_experimental_feature(): void {
$this->setup_communication_configs();
}
}