MDL-78750 communication_matrix: Dynamic settings

This commit is contained in:
Stevani Andolo 2023-09-06 13:40:52 +08:00
parent 440bf51af2
commit d644c3d89d
10 changed files with 100 additions and 20 deletions

View file

@ -34,4 +34,12 @@ interface communication_provider {
* @param processor $communication The communication object * @param processor $communication The communication object
*/ */
public static function load_for_instance(processor $communication): self; public static function load_for_instance(processor $communication): self;
/**
* Check if the provider is configured or not.
*
* This method is intended to check if the plugin have got any settings and if all the settings are set properly.
* This checking helps to reduce errors in future when a communication instance is added for the provider and not configured.
*/
public static function is_configured(): bool;
} }

View file

@ -347,7 +347,7 @@ class processor {
public static function load_by_id(int $id): ?self { public static function load_by_id(int $id): ?self {
global $DB; global $DB;
$record = $DB->get_record('communication', ['id' => $id]); $record = $DB->get_record('communication', ['id' => $id]);
if ($record && self::is_provider_enabled($record->provider)) { if ($record && self::is_provider_available($record->provider)) {
return new self($record); return new self($record);
} }
@ -376,7 +376,7 @@ class processor {
'instancetype' => $instancetype, 'instancetype' => $instancetype,
]); ]);
if ($record && self::is_provider_enabled($record->provider)) { if ($record && self::is_provider_available($record->provider)) {
return new self($record); return new self($record);
} }
@ -671,12 +671,16 @@ class processor {
} }
/** /**
* Is communication provider enabled/disabled. * Is the communication provider enabled and configured, or disabled.
* *
* @param string $provider provider component name * @param string $provider provider component name
* @return bool * @return bool
*/ */
public static function is_provider_enabled(string $provider): bool { public static function is_provider_available(string $provider): bool {
return \core\plugininfo\communication::is_plugin_enabled($provider); if (\core\plugininfo\communication::is_plugin_enabled($provider)) {
$providerclass = "{$provider}\\communication_feature";
return $providerclass::is_configured();
}
return false;
} }
} }

View file

@ -170,4 +170,8 @@ class communication_feature implements
'addcommunicationoptionshere' 'addcommunicationoptionshere'
), 'addcommunicationoptionshere'); ), 'addcommunicationoptionshere');
} }
public static function is_configured(): bool {
return true;
}
} }

View file

@ -113,4 +113,14 @@ class communication_feature_test extends \advanced_testcase {
return $communicationprocessor; return $communicationprocessor;
} }
/**
* Test if the selected provider is configured.
*
* @return void
*/
public function test_is_configured() {
$communicationprocessor = $this->get_test_communication_processor();
$this->assertTrue($communicationprocessor->get_form_provider()->is_configured());
}
} }

View file

@ -92,7 +92,7 @@ class communication_feature implements
$this->homeserverurl = get_config('communication_matrix', 'matrixhomeserverurl'); $this->homeserverurl = get_config('communication_matrix', 'matrixhomeserverurl');
$this->webclienturl = get_config('communication_matrix', 'matrixelementurl'); $this->webclienturl = get_config('communication_matrix', 'matrixelementurl');
if ($this->homeserverurl) { if ($processor::is_provider_available('communication_matrix')) {
// Generate the API instance. // Generate the API instance.
$this->matrixapi = matrix_client::instance( $this->matrixapi = matrix_client::instance(
serverurl: $this->homeserverurl, serverurl: $this->homeserverurl,
@ -751,4 +751,25 @@ class communication_feature implements
return $powerlevel; return $powerlevel;
} }
/*
* Check if matrix settings are configured
*
* @return boolean
*/
public static function is_configured(): bool {
// Matrix communication settings.
$matrixhomeserverurl = get_config('communication_matrix', 'matrixhomeserverurl');
$matrixaccesstoken = get_config('communication_matrix', 'matrixaccesstoken');
$matrixelementurl = get_config('communication_matrix', 'matrixelementurl');
if (
!empty($matrixhomeserverurl) &&
!empty($matrixaccesstoken) &&
(PHPUNIT_TEST || BEHAT_SITE_RUNNING || !empty($matrixelementurl))
) {
return true;
}
return false;
}
} }

View file

@ -28,20 +28,15 @@ if ($hassiteconfig) {
// Home server URL. // Home server URL.
$name = new lang_string('matrixhomeserverurl', 'communication_matrix'); $name = new lang_string('matrixhomeserverurl', 'communication_matrix');
$desc = new lang_string('matrixhomeserverurl_desc', 'communication_matrix'); $desc = new lang_string('matrixhomeserverurl_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredtext('communication_matrix/matrixhomeserverurl', $name, $desc, '')); $settings->add(new admin_setting_configtext('communication_matrix/matrixhomeserverurl', $name, $desc, ''));
// Access token. // Access token.
$name = new lang_string('matrixaccesstoken', 'communication_matrix'); $name = new lang_string('matrixaccesstoken', 'communication_matrix');
$desc = new lang_string('matrixaccesstoken_desc', 'communication_matrix'); $desc = new lang_string('matrixaccesstoken_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredpasswordunmask('communication_matrix/matrixaccesstoken', $name, $desc, '')); $settings->add(new admin_setting_configpasswordunmask('communication_matrix/matrixaccesstoken', $name, $desc, ''));
// Refresh token.
$name = new lang_string('matrixrefreshtoken', 'communication_matrix');
$desc = new lang_string('matrixrefreshtoken_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredpasswordunmask('communication_matrix/matrixrefreshtoken', $name, $desc, ''));
// Element web URL. // Element web URL.
$name = new lang_string('matrixelementurl', 'communication_matrix'); $name = new lang_string('matrixelementurl', 'communication_matrix');
$desc = new lang_string('matrixelementurl_desc', 'communication_matrix'); $desc = new lang_string('matrixelementurl_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredtext('communication_matrix/matrixelementurl', $name, $desc, '')); $settings->add(new admin_setting_configtext('communication_matrix/matrixelementurl', $name, $desc, ''));
} }

View file

@ -539,4 +539,24 @@ class communication_feature_test extends \advanced_testcase {
$communication->reload(); $communication->reload();
return $communication; return $communication;
} }
/**
* Test if the selected provider is configured.
*
* @return void
*/
public function test_is_configured() {
$course = $this->get_course();
$communicationprocessor = processor::load_by_instance(
component: 'core_course',
instancetype: 'coursecommunication',
instanceid: $course->id
);
$this->assertTrue($communicationprocessor->get_room_provider()->is_configured());
// Unset communication_matrix settings.
unset_config('matrixhomeserverurl', 'communication_matrix');
unset_config('matrixaccesstoken', 'communication_matrix');
$this->assertFalse($communicationprocessor->get_room_provider()->is_configured());
}
} }

View file

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->component = 'communication_matrix'; $plugin->component = 'communication_matrix';
$plugin->version = 2023090600; $plugin->version = 2023092300;
$plugin->requires = 2023011300; $plugin->requires = 2023011300;
$plugin->maturity = MATURITY_ALPHA; $plugin->maturity = MATURITY_ALPHA;

View file

@ -18,8 +18,11 @@ namespace core_communication;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php');
require_once(__DIR__ . '/communication_test_helper_trait.php'); require_once(__DIR__ . '/communication_test_helper_trait.php');
use \communication_matrix\matrix_test_helper_trait;
/** /**
* Class api_test to test the communication public api and its associated methods. * Class api_test to test the communication public api and its associated methods.
* *
@ -30,12 +33,15 @@ require_once(__DIR__ . '/communication_test_helper_trait.php');
* @covers \core_communication\api * @covers \core_communication\api
*/ */
class api_test extends \advanced_testcase { class api_test extends \advanced_testcase {
use matrix_test_helper_trait;
use communication_test_helper_trait; use communication_test_helper_trait;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
$this->resetAfterTest(); $this->resetAfterTest();
$this->setup_communication_configs(); $this->setup_communication_configs();
$this->initialise_mock_server();
} }
/** /**

View file

@ -18,8 +18,11 @@ namespace core_communication;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php');
require_once(__DIR__ . '/communication_test_helper_trait.php'); require_once(__DIR__ . '/communication_test_helper_trait.php');
use \communication_matrix\matrix_test_helper_trait;
/** /**
* Class processor_test to test the communication internal api and its associated methods. * Class processor_test to test the communication internal api and its associated methods.
* *
@ -30,8 +33,17 @@ require_once(__DIR__ . '/communication_test_helper_trait.php');
* @coversDefaultClass \core_communication\processor * @coversDefaultClass \core_communication\processor
*/ */
class processor_test extends \advanced_testcase { class processor_test extends \advanced_testcase {
use matrix_test_helper_trait;
use communication_test_helper_trait; use communication_test_helper_trait;
public function setUp(): void {
parent::setUp();
$this->resetAfterTest();
$this->setup_communication_configs();
$this->initialise_mock_server();
}
/** /**
* Test create instance. * Test create instance.
* *
@ -417,18 +429,18 @@ class processor_test extends \advanced_testcase {
} }
/** /**
* Test if the provider is enabled or disabled. * Test if the provider is enabled and configured, or disabled.
* *
* @covers ::is_provider_enabled * @covers ::is_provider_available
*/ */
public function test_is_provider_enabled(): void { public function test_is_provider_available(): void {
$this->resetAfterTest(); $this->resetAfterTest();
$communicationprovider = 'communication_matrix'; $communicationprovider = 'communication_matrix';
$this->assertTrue(processor::is_provider_enabled($communicationprovider)); $this->assertTrue(processor::is_provider_available($communicationprovider));
// Now test is disabling the plugin returns false. // Now test is disabling the plugin returns false.
set_config('disabled', 1, $communicationprovider); set_config('disabled', 1, $communicationprovider);
$this->assertFalse(processor::is_provider_enabled($communicationprovider)); $this->assertFalse(processor::is_provider_available($communicationprovider));
} }
/** /**