MDL-78723 communication_matrix: Address issues with user manager

This commit addresses a number of issues with the Matrix user manager.
These are, unfortunately, tough to break out into smaller commits.

The following issues are addressed:

Matrix usernames should be kept intact in the profile field. Prior to
this change, usernames were mangled and the hostname removed entirely.
Instead the hostname was added back when it is used. This approach is
not suited to a case where a user inserts their own matrix username on a
federated server.

Unit tests should have the minimum of requirements and dependencies.
Prior to this change, unit tests were setting up an entire mock system
which was completely unnecessary. These unit tests should only test the
static methods that they claim to test, not the entire communication
subsystem, matrix API, matrix client, processors, and providers.

Matrix host names should not be curtailed. Prior to this change the
hostname of the matrix server was modified if it contained any .
characters. For example, the following changes were previously made:

| hostname           | before  | after              |
| ------------------ | ------- | ------------------ |
| matrix.example.com | matrix  | matrix.example.com |
| www.example.com    | example | example.com        |

I believe that the original intent was to strip the www from the front,
but this is not documented anywhere that I have found. In any case, the
username should be the completed and fully-qualified username.

Many of the methods were poorly named:
- `set_qualified_matrix_user_id` is actually a userid formatter.
  This has been replaced with `get_formatted_matrix_userid`.
- `set_matrix_home_server` is actually a hostname formatter.
  This has been replaced with `get_formatted_matrix_home_server`.
- `add_user_matrix_id_to_moodle` sets the matrix userid for a moodle
  user, it does not add more than one.
  This has been replaced with `set_matrix_userid_in_moodle`.

The `set_qualified_matrix_user_id` method was silently returning with a
false value if the profile custom field did not exist, but the
`get_matrixid_from_moodle` method was creating the profile custom field
in the same situation. These have been swapped so a set operation will
create the field if it does not exist, but a get operation will not.
This commit is contained in:
Andrew Nicols 2023-07-17 21:49:35 +08:00
parent b4cd63746a
commit f067d6004f
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
5 changed files with 260 additions and 278 deletions

View File

@ -83,10 +83,7 @@ class communication_feature implements
'external_ids' => []
];
list($qualifiedmuid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id(
$userid,
$this->eventmanager->matrixhomeserverurl
);
$qualifiedmuid = matrix_user_manager::get_formatted_matrix_userid($user->username);
// First create user in matrix.
$response = $this->eventmanager->request($json)->put($this->eventmanager->get_create_user_endpoint($qualifiedmuid));
@ -94,7 +91,7 @@ class communication_feature implements
if (!empty($matrixuserid = $response->name)) {
// Then create matrix user id in moodle.
matrix_user_manager::add_user_matrix_id_to_moodle($userid, $pureusername);
matrix_user_manager::set_matrix_userid_in_moodle($userid, $qualifiedmuid);
if ($this->add_registered_matrix_user_to_room($matrixuserid)) {
$addedmembers[] = $userid;
}
@ -116,10 +113,7 @@ class communication_feature implements
$addedmembers = [];
foreach ($userids as $userid) {
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle(
$userid,
$this->eventmanager->matrixhomeserverurl
);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid);
if ($matrixuserid && $this->check_user_exists($matrixuserid)) {
if ($this->add_registered_matrix_user_to_room($matrixuserid)) {
@ -173,10 +167,7 @@ class communication_feature implements
foreach ($userids as $userid) {
// Check user is member of room first.
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle(
$userid,
$this->eventmanager->matrixhomeserverurl
);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid);
// Check if user is the room admin and halt removal of this user.
$matrixroomdata = $this->eventmanager->request()->get($this->eventmanager->get_room_info_endpoint());

View File

@ -28,97 +28,80 @@ class matrix_user_manager {
/**
* Gets matrix user id from moodle.
*
* @param string $userid Moodle user id
* @param string $homeserver Matrix home server url
* @param int $userid Moodle user id
* @return string|null
*/
public static function get_matrixid_from_moodle(
string $userid,
string $homeserver
int $userid,
) : ?string {
global $CFG;
require_once("$CFG->dirroot/user/profile/lib.php");
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
if (!$matrixprofilefield) {
$matrixprofilefield = self::create_matrix_user_profile_fields();
}
self::load_requirements();
$field = profile_user_record($userid);
$pureusername = $field->{$matrixprofilefield} ?? null;
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
if ($pureusername) {
$homeserver = self::set_matrix_home_server($homeserver);
return "@{$pureusername}:$homeserver";
if ($matrixprofilefield === false) {
return null;
}
return $pureusername;
return $field->{$matrixprofilefield} ?? null;
}
/**
* Sets qualified matrix user id
* Get a qualified matrix user id based on a Moodle username.
*
* @param string $userid Moodle user id
* @param string $homeserver Matrix home server url
* @return array
* @param string $username The moodle username to turn into a Matrix username
* @return string
*/
public static function set_qualified_matrix_user_id(
string $userid,
string $homeserver
) : array {
$user = \core_user::get_user($userid);
$username = preg_replace('/[@#$%^&*()+{}|<>?!,]/i', '.', $user->username);
public static function get_formatted_matrix_userid(
string $username,
): string {
$username = preg_replace('/[@#$%^&*()+{}|<>?!,]/i', '.', $username);
$username = ltrim(rtrim($username, '.'), '.');
$homeserver = self::set_matrix_home_server($homeserver);
$homeserver = self::get_formatted_matrix_home_server();
return ["@{$username}:{$homeserver}", $username];
return "@{$username}:{$homeserver}";
}
/**
* Add user's Matrix user id.
*
* @param string $userid Moodle user id
* @param int $userid Moodle user id
* @param string $matrixuserid Matrix user id
* @return bool
*/
public static function add_user_matrix_id_to_moodle(
string $userid,
string $matrixuserid
): bool {
global $CFG;
require_once("$CFG->dirroot/user/profile/lib.php");
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
public static function set_matrix_userid_in_moodle(
int $userid,
string $matrixuserid,
): void {
$matrixprofilefield = self::get_profile_field_name();
$field = profile_get_custom_field_data_by_shortname($matrixprofilefield);
if ($field !== null) {
$userinfodata = new \stdClass();
$userinfodata->id = $userid;
$userinfodata->data = $matrixuserid;
$userinfodata->fieldid = $field->id;
$userinfodata->{"profile_field_{$matrixprofilefield}"} = $matrixuserid;
profile_save_data($userinfodata);
return true;
if ($field === null) {
return;
}
return false;
$userinfodata = (object) [
'id' => $userid,
'data' => $matrixuserid,
'fieldid' => $field->id,
"profile_field_{$matrixprofilefield}" => $matrixuserid,
];
profile_save_data($userinfodata);
}
/**
* Sets home server for user matrix id
*
* @param string $homeserver Matrix home server url
* @return string
*/
public static function set_matrix_home_server(string $homeserver) : string {
public static function get_formatted_matrix_home_server(): string {
$homeserver = get_config('communication_matrix', 'matrixhomeserverurl');
if ($homeserver === false) {
throw new \moodle_exception('Unknown matrix home server url');
}
$homeserver = parse_url($homeserver)['host'];
if (strpos($homeserver, '.') !== false) {
$host = explode('.', $homeserver);
return strpos($homeserver, 'www') !== false ? $host[1] : $host[0];
if (str_starts_with($homeserver, 'www.')) {
$homeserver = str_replace('www.', '', $homeserver);
}
return $homeserver;
@ -183,4 +166,30 @@ class matrix_user_manager {
return 'matrixuserid';
}
}
/**
* Get the profile field name, creating the profiel field if it does not exist.
*
* @return string
*/
protected static function get_profile_field_name(): string {
self::load_requirements();
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
if ($matrixprofilefield === false) {
$matrixprofilefield = self::create_matrix_user_profile_fields();
}
return $matrixprofilefield;
}
/**
* Load requirements for profile field management.
*
* This is just a helper to keep loading legacy files isolated.
*/
protected static function load_requirements(): void {
global $CFG;
require_once("{$CFG->dirroot}/user/profile/lib.php");
}
}

View File

@ -219,7 +219,10 @@ class communication_feature_test extends \advanced_testcase {
public function test_create_members(): void {
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
$user = $this->getDataGenerator()->create_user((object) [
'username' => 'colin.creavey',
]);
$userid = $user->id;
// Sample data.
$communicationroomname = 'Sampleroom';
@ -252,17 +255,17 @@ class communication_feature_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
// Get created matrixuserid from moodle.
$elementserver = matrix_user_manager::set_matrix_home_server($eventmanager->matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid, $eventmanager->matrixhomeserverurl);
$elementserver = matrix_user_manager::get_formatted_matrix_home_server();
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
$this->assertNotNull($matrixuserid);
$this->assertEquals("@sampleun:{$elementserver}", $matrixuserid);
$this->assertEquals("@{$user->username}:{$elementserver}", $matrixuserid);
// Add api call to get user data and test against set data.
$matrixuserdata = $this->get_matrix_user_data($matrixrooms->get_matrix_room_id(), $matrixuserid);
$this->assertNotEmpty($matrixuserdata);
$this->assertEquals("Samplefn Sampleln", $matrixuserdata->displayname);
$this->assertEquals(fullname($user), $matrixuserdata->displayname);
}
/**
@ -309,7 +312,7 @@ class communication_feature_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
// Get created matrixuserid from moodle.
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid, $eventmanager->matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid);
// Test user is a member of the room.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));

View File

@ -213,7 +213,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
// Get matrix user id from moodle.
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $eventmanager->matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
$this->assertNotNull($matrixuserid);
// Get matrix user id from matrix.
@ -261,7 +261,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
@ -305,7 +305,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Unenrol the user from the course.
@ -354,7 +354,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Suspend user enrolment.
@ -403,7 +403,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Delete instance.
@ -452,7 +452,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Update enrolment communication.
@ -504,7 +504,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Update enrolment communication when updating instance to disabled.
@ -560,7 +560,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Disable communication provider.
@ -610,7 +610,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Suspend user.
@ -660,7 +660,7 @@ class matrix_communication_test extends \advanced_testcase {
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Delete user.
@ -897,7 +897,7 @@ class matrix_communication_test extends \advanced_testcase {
/**
* Test status notifications of a communication room are generated correctly.
*
* @covers ::show_communication_room_status_notification
* @covers \core_communication\api::show_communication_room_status_notification
*/
public function test_show_communication_room_status_notification(): void {
$course = $this->get_course();
@ -933,7 +933,8 @@ class matrix_communication_test extends \advanced_testcase {
/**
* Test set provider data from handler.
*
* @covers ::set_data
* @covers \core_communication\api::set_data
* @covers \communication_matrix\communication_feature::set_form_data
*/
public function test_set_provider_data(): void {
$this->resetAfterTest();

View File

@ -17,12 +17,7 @@
namespace communication_matrix;
use core_communication\processor;
use core_communication\communication_test_helper_trait;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/matrix_test_helper_trait.php');
require_once(__DIR__ . '/../../../tests/communication_test_helper_trait.php');
use moodle_exception;
/**
* Class matrix_user_manager_test to test the matrix user manager.
@ -31,225 +26,208 @@ require_once(__DIR__ . '/../../../tests/communication_test_helper_trait.php');
* @category test
* @copyright 2023 Stevani Andolo <stevani.andolo@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \communication_matrix\matrix_user_manager
* @covers \communication_matrix\matrix_user_manager
*/
class matrix_user_manager_test extends \advanced_testcase {
/**
* Test fetcihing a users matrix userid from Moodle.
*/
public function test_get_matrixid_from_moodle_without_field(): void {
$user = get_admin();
use matrix_test_helper_trait;
use communication_test_helper_trait;
public function setUp(): void {
parent::setUp();
$this->resetAfterTest();
$this->setup_communication_configs();
$this->initialise_mock_server();
// And confirm that they're fetched back.
$this->assertNull(matrix_user_manager::get_matrixid_from_moodle($user->id));
}
/**
* Test get matrix id from moodle.
*
* @covers ::get_matrixid_from_moodle
* Test fetching a user's matrix userid from Moodle.
*/
public function test_get_matrixid_from_moodle(): void {
$course = $this->get_course();
$userid = $this->get_user()->id;
$this->resetAfterTest();
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
// Add user ids to both users.
matrix_user_manager::set_matrix_userid_in_moodle(
$user1->id,
'@someexampleuser:matrix.moodle.org',
);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
matrix_user_manager::set_matrix_userid_in_moodle(
$user2->id,
'@someotherexampleuser:matrix.moodle.org',
);
// And confirm that they're fetched back.
$this->assertEquals(
'@someexampleuser:matrix.moodle.org',
matrix_user_manager::get_matrixid_from_moodle($user1->id),
);
$this->assertEquals(
'@someotherexampleuser:matrix.moodle.org',
matrix_user_manager::get_matrixid_from_moodle($user2->id),
);
}
/**
* Test fetching a formatted matrix userid from Moodle when no server is set.
*/
public function test_get_formatted_matrix_userid_unset(): void {
$this->expectException(moodle_exception::class);
matrix_user_manager::get_formatted_matrix_userid('No value');
}
/**
* Test fetch of a formatted matrix userid.
*
* @dataProvider get_formatted_matrix_userid_provider
* @param string $server
* @param string $username The moodle username to turn into a Matrix username
* @param string $expecteduserid The expected matrix user id
*/
public function test_get_formatted_matrix_userid(
string $server,
string $username,
string $expecteduserid,
): void {
$this->resetAfterTest();
set_config('matrixhomeserverurl', $server, 'communication_matrix');
$this->assertEquals(
$expecteduserid,
matrix_user_manager::get_formatted_matrix_userid($username),
);
}
/**
* Data provider for get_formatted_matrix_userid.
*
* @return array
*/
public function get_formatted_matrix_userid_provider(): array {
return [
'alphanumeric' => [
'https://matrix.example.org',
'alphabet1',
'@alphabet1:matrix.example.org',
],
'chara' => [
'https://matrix.example.org',
'asdf#$%^&*()+{}|<>?!,asdf',
'@asdf.................asdf:matrix.example.org',
],
'local server' => [
'https://synapse',
'colin.creavey',
'@colin.creavey:synapse',
],
'server with port' => [
'https://matrix.example.org:8448',
'colin.creavey',
'@colin.creavey:matrix.example.org',
],
];
}
/**
* Data provider for set_matrix_userid_in_moodle.
*
* @return array
*/
public function set_matrix_userid_in_moodle_provider(): array {
return array_combine(
array_keys($this->get_formatted_matrix_userid_provider()),
array_map(
fn($value) => [$value[2]],
$this->get_formatted_matrix_userid_provider(),
),
);
}
/**
* Test setting of a user's matrix userid in Moodle.
*
* @dataProvider set_matrix_userid_in_moodle_provider
* @param string $expectedusername
*/
public function test_set_matrix_userid_in_moodle(
string $expectedusername,
): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
matrix_user_manager::set_matrix_userid_in_moodle($user->id, $expectedusername);
// Get created matrixuserid from moodle.
$elementserver = matrix_user_manager::set_matrix_home_server($matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid, $matrixhomeserverurl);
$this->assertNotNull($matrixuserid);
$this->assertEquals("@sampleun:{$elementserver}", $matrixuserid);
$this->assertEquals(
$expectedusername,
matrix_user_manager::get_matrixid_from_moodle($user->id),
);
}
/**
* Sets qualified matrix user id.
* Test for getting a formatted matrix home server id.
*
* @return void
* @covers ::set_qualified_matrix_user_id
* @dataProvider get_formatted_matrix_home_server_provider
* @param string $input
* @param string $expectedoutput
*/
public function test_set_qualified_matrix_user_id(): void {
public function test_get_formatted_matrix_home_server(
string $input,
string $expectedoutput
): void {
$this->resetAfterTest();
$course = $this->get_course();
$user = $this->get_user();
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->add_members_to_room([$user->id]);
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
set_config(
'matrixhomeserverurl',
$input,
'communication_matrix',
);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$elementserver = matrix_user_manager::set_matrix_home_server($matrixhomeserverurl);
// Sets qualified matrix id test1.
list($matrixuserid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id($user->id, $matrixhomeserverurl);
$this->assertEquals("@{$user->username}:{$elementserver}", $matrixuserid);
$this->assertEquals("sampleun", $pureusername);
// Sets qualified matrix id test2.
$user = $this->get_user('moodlefn', 'moodleln', 'admin@moodle.com');
list($matrixuserid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id($user->id, $matrixhomeserverurl);
$this->assertEquals("@admin.moodle.com:{$elementserver}", $matrixuserid);
$this->assertEquals("admin.moodle.com", $pureusername);
// Sets qualified matrix id test3.
$user = $this->get_user('moodlefn', 'moodleln', 'admin-user@moodle.com');
list($matrixuserid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id($user->id, $matrixhomeserverurl);
$this->assertEquals("@admin-user.moodle.com:{$elementserver}", $matrixuserid);
$this->assertEquals("admin-user.moodle.com", $pureusername);
$this->assertEquals(
$expectedoutput,
matrix_user_manager::get_formatted_matrix_home_server(),
);
}
/**
* Add user's matrix id to moodle.
* Data provider for get_formatted_matrix_home_server.
*
* @covers ::add_user_matrix_id_to_moodle
* @return array
*/
public function test_add_user_matrix_id_to_moodle(): void {
$course = $this->get_course();
$userid = $this->get_user()->id;
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
// Sets qualified matrix id.
list($qualifiedmuid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id(
$userid,
$eventmanager->matrixhomeserverurl
);
$this->assertNotNull($qualifiedmuid);
$this->assertNotNull($pureusername);
// Will return true on success.
$this->assertTrue(matrix_user_manager::add_user_matrix_id_to_moodle($userid, $pureusername));
// Get created matrixuserid from moodle.
$elementserver = matrix_user_manager::set_matrix_home_server($eventmanager->matrixhomeserverurl);
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($userid, $eventmanager->matrixhomeserverurl);
$this->assertNotNull($matrixuserid);
$this->assertEquals("@sampleun:{$elementserver}", $matrixuserid);
public function get_formatted_matrix_home_server_provider(): array {
return [
'www is removed' => [
'https://www.example.org',
'example.org',
],
'www is not removed if it is not at the beginning' => [
'https://matrix.www.example.org',
'matrix.www.example.org',
],
'others are not removed' => [
'https://matrix.example.org',
'matrix.example.org',
],
];
}
/**
* Add matrix home server for qualified matrix id.
*
* @return void
* @covers ::set_matrix_home_server
*/
public function test_set_matrix_home_server(): void {
$course = $this->get_course();
$userid = $this->get_user()->id;
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
// Will generate matrix home server.
$generatedhomeserver = matrix_user_manager::set_matrix_home_server($eventmanager->matrixhomeserverurl);
$this->assertNotNull($generatedhomeserver);
}
/**
* Test post matrix insert new user field record.
*
* @covers ::execute
* Test creation of matrix user profile fields.
*/
public function test_create_matrix_user_profile_fields(): void {
$course = $this->get_course();
$userid = $this->get_user()->id;
$this->resetAfterTest();
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
$this->assertFalse($matrixprofilefield);
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->add_members_to_room([$userid]);
$this->assertIsString(matrix_user_manager::create_matrix_user_profile_fields());
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
$this->assertNotFalse($matrixprofilefield);
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Check if "Communication" field has been added.
$categoryfield = get_config('core_communication', 'communication_category_field');
$this->assertNotNull($categoryfield);
$this->assertEquals('Communication', $categoryfield);
// Check if "matrixuserid" field has been added.
$infofield = get_config('communication_matrix', 'matrixuserid_field');
$this->assertNotNull($infofield);
$this->assertEquals('matrixuserid', $infofield);
$user = $this->getDataGenerator()->create_user();
$this->assertObjectHasAttribute($matrixprofilefield, profile_user_record($user->id));
}
}