MDL-77576 communication_matrix: Create Matrix plugin

This commit will implement a matrix communication plugin
to integrate matrix services with core communication.

Originally implemented as MDL-76701, MDL-76702, MDL-77357,
MDL-76705, MDL-77473 and  MDL-76708.

Co-Authored-By: Stevani Andolo <stevani.andolo@moodle.com>
Co-Authored-By: David Woloszyn <david.woloszyn@moodle.com>
Co-Authored-By: Safat Shahin <safat.shahin@moodle.com>
This commit is contained in:
Safat 2023-01-19 16:04:19 +11:00
parent ca1e5d0beb
commit 9a060a6d8d
20 changed files with 3101 additions and 588 deletions

View File

@ -0,0 +1,316 @@
<?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/>.
namespace communication_matrix;
use core_communication\processor;
/**
* class communication_feature to handle matrix specific actions.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class communication_feature implements
\core_communication\communication_provider,
\core_communication\user_provider,
\core_communication\room_chat_provider,
\core_communication\room_user_provider {
/** @var matrix_events_manager $eventmanager The event manager object to get the endpoints */
private matrix_events_manager $eventmanager;
/** @var matrix_rooms $matrixrooms The matrix room object to update room information */
private matrix_rooms $matrixrooms;
/**
* Load the communication provider for the communication api.
*
* @param processor $communication The communication processor object
* @return communication_feature The communication provider object
*/
public static function load_for_instance(processor $communication): self {
return new self($communication);
}
/**
* Constructor for communication provider to initialize necessary objects for api cals etc..
*
* @param processor $communication The communication processor object
*/
private function __construct(
private \core_communication\processor $communication,
) {
$this->matrixrooms = new matrix_rooms($communication->get_id());
$this->eventmanager = new matrix_events_manager($this->matrixrooms->get_matrix_room_id());
}
/**
* Create members.
*
* @param array $userids The Moodle user ids to create
*/
public function create_members(array $userids): void {
$addedmembers = [];
foreach ($userids as $userid) {
$user = \core_user::get_user($userid);
$userfullname = fullname($user);
// Proceed if we have a user's full name and email to work with.
if (!empty($user->email) && !empty($userfullname)) {
$json = [
'displayname' => $userfullname,
'threepids' => [(object)[
'medium' => 'email',
'address' => $user->email
]],
'external_ids' => []
];
list($qualifiedmuid, $pureusername) = matrix_user_manager::set_qualified_matrix_user_id(
$userid,
$this->eventmanager->matrixhomeserverurl
);
// First create user in matrix.
$response = $this->eventmanager->request($json)->put($this->eventmanager->get_create_user_endpoint($qualifiedmuid));
$response = json_decode($response->getBody());
if (!empty($matrixuserid = $response->name)) {
// Then create matrix user id in moodle.
matrix_user_manager::add_user_matrix_id_to_moodle($userid, $pureusername);
if ($this->add_registered_matrix_user_to_room($matrixuserid)) {
$addedmembers[] = $userid;
}
}
}
}
// Mark then users as synced for the added members.
$this->communication->mark_users_as_synced($addedmembers);
}
/**
* Add members to a room.
*
* @param array $userids The user ids to add
*/
public function add_members_to_room(array $userids): void {
$unregisteredmembers = [];
$addedmembers = [];
foreach ($userids as $userid) {
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle(
$userid,
$this->eventmanager->matrixhomeserverurl
);
if ($matrixuserid && $this->check_user_exists($matrixuserid)) {
if ($this->add_registered_matrix_user_to_room($matrixuserid)) {
$addedmembers[] = $userid;
}
} else {
$unregisteredmembers[] = $userid;
}
}
// Mark then users as synced for the added members.
$this->communication->mark_users_as_synced($addedmembers);
// Create Matrix users.
if (count($unregisteredmembers) > 0) {
$this->create_members($unregisteredmembers);
}
}
/**
* Adds the registered matrix user id to room.
*
* @param string $matrixuserid Registered matrix user id
*/
private function add_registered_matrix_user_to_room(string $matrixuserid): bool {
if (!$this->check_room_membership($matrixuserid)) {
$json = ['user_id' => $matrixuserid];
$headers = ['Content-Type' => 'application/json'];
$response = $this->eventmanager->request(
$json,
$headers
)->post(
$this->eventmanager->get_room_membership_join_endpoint()
);
$response = json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
if (!empty($roomid = $response->room_id) && $roomid === $this->eventmanager->roomid) {
return true;
}
}
return false;
}
/**
* Remove members from a room.
*
* @param array $userids The Moodle user ids to remove
*/
public function remove_members_from_room(array $userids): void {
$membersremoved = [];
foreach ($userids as $userid) {
// Check user is member of room first.
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle(
$userid,
$this->eventmanager->matrixhomeserverurl
);
// Check if user is the room admin and halt removal of this user.
$matrixroomdata = $this->eventmanager->request()->get($this->eventmanager->get_room_info_endpoint());
$matrixroomdata = json_decode($matrixroomdata->getBody(), false, 512, JSON_THROW_ON_ERROR);
$roomadmin = $matrixroomdata->creator;
$isadmin = $matrixuserid === $roomadmin;
if (
!$isadmin && $matrixuserid && $this->check_user_exists($matrixuserid) &&
$this->check_room_membership($matrixuserid)
) {
$json = ['user_id' => $matrixuserid];
$headers = ['Content-Type' => 'application/json'];
$this->eventmanager->request(
$json,
$headers
)->post(
$this->eventmanager->get_room_membership_kick_endpoint()
);
$membersremoved[] = $userid;
}
}
$this->communication->delete_instance_user_mapping($membersremoved);
}
/**
* Check if a user exists in Matrix.
* Use if user existence is needed before doing something else.
*
* @param string $matrixuserid The Matrix user id to check
* @return bool
*/
public function check_user_exists(string $matrixuserid): bool {
$response = $this->eventmanager->request([], [], false)->get($this->eventmanager->get_user_info_endpoint($matrixuserid));
$response = json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
return isset($response->name);
}
/**
* Check if a user is a member of a room.
* Use if membership confirmation is needed before doing something else.
*
* @param string $matrixuserid The Matrix user id to check
* @return bool
*/
public function check_room_membership(string $matrixuserid): bool {
$response = $this->eventmanager->request([], [], false)->get($this->eventmanager->get_room_membership_joined_endpoint());
$response = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
// Check user id is in the returned room member ids.
return isset($response['joined']) && array_key_exists($matrixuserid, $response['joined']);
}
public function create_chat_room(): bool {
if ($this->matrixrooms->room_record_exists()) {
return $this->update_chat_room();
}
// Create a new room.
$json = [
'name' => $this->communication->get_room_name(),
'visibility' => 'private',
'preset' => 'private_chat',
'initial_state' => [],
];
$response = $this->eventmanager->request($json)->post($this->eventmanager->get_create_room_endpoint());
$response = json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
// Check if room was created.
if (!empty($roomid = $response->room_id)) {
$this->matrixrooms->create_matrix_room_record($this->communication->get_id(), $roomid);
$this->eventmanager->roomid = $roomid;
$this->update_room_avatar();
return true;
}
return false;
}
public function update_chat_room(): bool {
if (!$this->matrixrooms->room_record_exists()) {
return $this->create_chat_room();
}
// Get room data.
$matrixroomdata = $this->eventmanager->request()->get($this->eventmanager->get_room_info_endpoint());
$matrixroomdata = json_decode($matrixroomdata->getBody(), false, 512, JSON_THROW_ON_ERROR);
// Update the room name when it's updated from the form.
if ($matrixroomdata->name !== $this->communication->get_room_name()) {
$json = ['name' => $this->communication->get_room_name()];
$this->eventmanager->request($json)->put($this->eventmanager->get_update_room_name_endpoint());
}
// Update room avatar.
$this->update_room_avatar();
return true;
}
public function delete_chat_room(): bool {
return $this->matrixrooms->delete_matrix_room_record();
}
/**
* Update the room avatar when an instance image is added or updated.
*/
public function update_room_avatar(): void {
$instanceimage = $this->communication->get_avatar();
$contenturi = null;
// If avatar is set for the instance, update in matrix.
if (!empty($instanceimage)) {
// First upload the content.
$contenturi = $this->eventmanager->upload_matrix_content($instanceimage->get_content());
}
// Now update the room avatar.
$json = [
'url' => $contenturi,
];
$this->eventmanager->request($json, [], false)->put($this->eventmanager->get_update_avatar_endpoint());
}
public function get_chat_room_url(): ?string {
// Check for room record in Moodle and that it exists in Matrix.
if (!$this->matrixrooms->room_record_exists()) {
return null;
}
return $this->eventmanager->matrixwebclienturl . '#/room/' . $this->matrixrooms->get_matrix_room_id();
}
}

View File

@ -0,0 +1,246 @@
<?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/>.
namespace communication_matrix;
use core\http_client;
use GuzzleHttp\Psr7\Request;
/**
* Class matrix_endpoint_manager to manage the api endpoints of matrix provider.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class matrix_events_manager {
/**
* @var string|false|mixed|object $matrixhomeserverurl The URL of the home server
*/
public string $matrixhomeserverurl;
/**
* @var string $matrixwebclienturl The URL of the web client
*/
public string $matrixwebclienturl;
/**
* @var string|false|mixed|object $matrixaccesstoken The access token of the matrix server
*/
private string $matrixaccesstoken;
/**
* @var string $roomid The id of the room from matrix server
*/
public string $roomid;
/**
* Matrix events constructor to get the room id and refresh token usage if required.
*
* @param string|null $roomid The id of the room from matrix server
*/
public function __construct(?string $roomid = null) {
if (!empty($roomid)) {
$this->roomid = $roomid;
}
$this->matrixhomeserverurl = get_config('communication_matrix', 'matrixhomeserverurl');
$this->matrixaccesstoken = get_config('communication_matrix', 'matrixaccesstoken');
$this->matrixwebclienturl = get_config('communication_matrix', 'matrixelementurl');
}
/**
* Get the current token in use.
*
* @return string
*/
public function get_token(): string {
return $this->matrixaccesstoken;
}
/**
* Get the matrix api endpoint for creating a new room.
*
* @return string
*/
public function get_create_room_endpoint(): string {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/createRoom';
}
/**
* Get the matrix api endpoint for updating the room topic.
*
* @return string
*/
public function get_update_room_topic_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($this->roomid) . '/' . 'state/m.room.topic/';
}
}
/**
* Get the matrix api endpoint for updating room name.
*
* @return string
*/
public function get_update_room_name_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($this->roomid) . '/' . 'state/m.room.name/';
}
}
/**
* Get matrix api endpoint for getting room information.
*
* @return string
*/
public function get_room_info_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/_synapse/admin/v1/rooms/' . urlencode($this->roomid);
}
}
/**
* Get delete room endpoint.
*
* @return string
*/
public function get_delete_room_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_synapse/admin/v1/rooms/' . urlencode($this->roomid);
}
}
/**
* Get the matrix api endpoint for uploading a content to synapse server.
*/
public function get_upload_content_endpoint(): string {
return $this->matrixhomeserverurl . '/' . '_matrix/media/r0/upload/';
}
/**
* Get the matrix api endpoint for updating room avatar.
*
* @return string
*/
public function get_update_avatar_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($this->roomid) . '/' . 'state/m.room.avatar/';
}
}
/**
* Get the members of a room. Useful when performing actions where member needs to exist first.
*
* @return string
*/
public function get_room_membership_joined_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($this->roomid) . '/' . 'joined_members';
}
}
/**
* Get the 'join' room membership endpoint. This adds users to a room.
*
* @return string
*/
public function get_room_membership_join_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_synapse/admin/v1/join' . '/' . urlencode($this->roomid);
}
}
/**
* Get the 'kick' room membership endpoint. This removes users from a room.
*
* @return string
*/
public function get_room_membership_kick_endpoint(): string {
if (!empty($this->roomid)) {
return $this->matrixhomeserverurl . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($this->roomid) . '/' . 'kick';
}
}
/**
* Get the matrix api endpoint for creating a new user.
*
* @param string $matrixuserid Matrix user id
* @return string
*/
public function get_create_user_endpoint(string $matrixuserid): string {
return $this->matrixhomeserverurl . '/' . '_synapse/admin/v2/users/' . urlencode($matrixuserid);
}
/**
* Get the matrix api endpoint for creating a new user.
*
* @param string $matrixuserid Matrix user id
* @return string
*/
public function get_user_info_endpoint(string $matrixuserid): string {
return $this->matrixhomeserverurl . '/' . '_synapse/admin/v2/users/' . urlencode($matrixuserid);
}
/**
* The http request for the api call.
*
* @param array $jsonarray The array of json
* @param array $headers The array of headers
* @param bool $httperror Enable or disable http error from response
* @return \core\http_client
*/
public function request(array $jsonarray = [], array $headers = [], bool $httperror = true): \core\http_client {
$header = ['Authorization' => 'Bearer ' . $this->get_token()];
$headers = array_merge($header, $headers);
$response = new \core\http_client([
'http_errors' => $httperror,
'headers' => $headers,
'json' => $jsonarray,
]);
return $response;
}
/**
* Upload the content in the matrix/synapse server.
*
* @param string $content The content to be uploaded
* @return string|false
*/
public function upload_matrix_content(string $content): bool|string {
$headers = [
'Authorization' => 'Bearer ' . $this->get_token(),
'Content-Type' => 'image/png'
];
$client = new http_client();
$request = new Request('POST', $this->get_upload_content_endpoint(), $headers, $content);
$response = $client->send($request);
$response = json_decode($response->getBody());
if ($response) {
return $response->content_uri;
}
return false;
}
}

View File

@ -0,0 +1,116 @@
<?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/>.
namespace communication_matrix;
/**
* Class matrix_rooms to manage the updates to the room information in db.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class matrix_rooms {
/**
* @var \stdClass|null $matrixroomrecord The matrix room record from db
*/
private ?\stdClass $matrixroomrecord = null;
/**
* Matrix rooms constructor to load the matrix room information from matrix_rooms table.
*
* @param int $commid The id of the communication record
*/
public function __construct(int $commid) {
$this->load_matrix_room_data($commid);
}
/**
* Get the matrix room data from database. Either get the data object or return false if no data found.
*
* @param int $commid The id of the communication record
*/
public function load_matrix_room_data(int $commid): void {
global $DB;
if ($record = $DB->get_record('matrix_rooms', ['commid' => $commid])) {
$this->matrixroomrecord = $record;
}
}
/**
* Create matrix room data.
*
* @param int $commid The id of the communication record
* @param string $roomid The id of the room from matrix
*/
public function create_matrix_room_record(int $commid, string $roomid): void {
global $DB;
$roomrecord = new \stdClass();
$roomrecord->commid = $commid;
$roomrecord->roomid = $roomid;
$roomrecord->id = $DB->insert_record('matrix_rooms', $roomrecord);
$this->matrixroomrecord = $roomrecord;
}
/**
* Update matrix room data.
*
* @param string $roomid The id of the room from matrix
*/
public function update_matrix_room_record(string $roomid): void {
global $DB;
if ($this->room_record_exists()) {
$this->matrixroomrecord->roomid = $roomid;
$DB->update_record('matrix_rooms', $this->matrixroomrecord);
}
}
/**
* Delete matrix room data.
*
* @return bool
*/
public function delete_matrix_room_record(): bool {
global $DB;
if ($this->room_record_exists()) {
return $DB->delete_records('matrix_rooms', ['commid' => $this->matrixroomrecord->commid]);
}
return false;
}
/**
* Get the matrix room id.
*
* @return string|null
*/
public function get_matrix_room_id(): ?string {
if ($this->room_record_exists()) {
return $this->matrixroomrecord->roomid;
}
return null;
}
/**
* Check if room record exist for matrix.
*
* @return bool
*/
public function room_record_exists(): bool {
return (bool) $this->matrixroomrecord;
}
}

View File

@ -0,0 +1,186 @@
<?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/>.
namespace communication_matrix;
/**
* class matrix_user_manager to handle specific actions.
*
* @package communication_matrix
* @copyright 2023 Stevani Andolo <stevani.andolo@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class matrix_user_manager {
/**
* Gets matrix user id from moodle.
*
* @param string $userid Moodle user id
* @param string $homeserver Matrix home server url
* @return string|null
*/
public static function get_matrixid_from_moodle(
string $userid,
string $homeserver
) : ?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();
}
$field = profile_user_record($userid);
$pureusername = $field->{$matrixprofilefield} ?? null;
if ($pureusername) {
$homeserver = self::set_matrix_home_server($homeserver);
return "@{$pureusername}:$homeserver";
}
return $pureusername;
}
/**
* Sets qualified matrix user id
*
* @param string $userid Moodle user id
* @param string $homeserver Matrix home server url
* @return array
*/
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);
$username = ltrim(rtrim($username, '.'), '.');
$homeserver = self::set_matrix_home_server($homeserver);
return ["@{$username}:{$homeserver}", $username];
}
/**
* Add user's Matrix user id.
*
* @param string $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');
$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;
}
return false;
}
/**
* 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 {
$homeserver = parse_url($homeserver)['host'];
if (strpos($homeserver, '.') !== false) {
$host = explode('.', $homeserver);
return strpos($homeserver, 'www') !== false ? $host[1] : $host[0];
}
return $homeserver;
}
/**
* Insert "Communication" category and "matrixuserid" field.
*
* @return string
*/
public static function create_matrix_user_profile_fields(): string {
global $CFG, $DB;
require_once($CFG->dirroot . '/user/profile/definelib.php');
require_once($CFG->dirroot . '/user/profile/field/text/define.class.php');
// Check if communication category exists.
$categoryname = get_string('communication', 'core_communication');
$category = $DB->count_records('user_info_category', ['name' => $categoryname]);
if ($category < 1) {
$data = new \stdClass();
$data->sortorder = $DB->count_records('user_info_category') + 1;
$data->name = $categoryname;
$data->id = $DB->insert_record('user_info_category', $data);
$createdcategory = $DB->get_record('user_info_category', ['id' => $data->id]);
$categoryid = $createdcategory->id;
\core\event\user_info_category_created::create_from_category($createdcategory)->trigger();
} else {
$category = $DB->get_record('user_info_category', ['name' => $categoryname]);
$categoryid = $category->id;
}
set_config('communication_category_field', $categoryname, 'core_communication');
// Check if matrixuserid exists in user_info_field table.
$matrixuserid = $DB->count_records('user_info_field', [
'shortname' => 'matrixuserid',
'categoryid' => $categoryid
]);
if ($matrixuserid < 1) {
$profileclass = new \profile_define_text();
$data = (object) [
'shortname' => 'matrixuserid',
'name' => get_string('matrixuserid', 'communication_matrix'),
'datatype' => 'text',
'description' => get_string('matrixuserid_desc', 'communication_matrix'),
'descriptionformat' => 1,
'categoryid' => $categoryid,
'forceunique' => 1,
'visible' => 0,
'locked' => 1,
'param1' => 30,
'param2' => 2048
];
$profileclass->define_save($data);
set_config('matrixuserid_field', 'matrixuserid', 'communication_matrix');
return 'matrixuserid';
}
}
}

View File

@ -0,0 +1,39 @@
<?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/>.
namespace communication_matrix\privacy;
use core_privacy\local\metadata\null_provider;
/**
* Privacy Subsystem for communication_matrix implementing null_provider.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="communication/provider/matrix/db" VERSION="2023011900" COMMENT="Stores the matrix room information associated with the communication instance."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="matrix_rooms" COMMENT="Stores the matrix room information associated with the communication instance.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="commid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the communication record"/>
<FIELD NAME="roomid" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="ID of the matrix room instance"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="fk_commid" TYPE="foreign" FIELDS="commid" REFTABLE="communication" REFFIELDS="id" COMMENT="Foreign key for communication reference"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

View File

@ -0,0 +1,36 @@
<?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/>.
/**
* Strings for component communication_matrix, language 'en'.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['matrixuserid'] = 'Matrix user ID';
$string['matrixuserid_desc'] = 'The user ID to be used for Matrix';
$string['matrixhomeserverurl'] = 'Home server URL';
$string['matrixhomeserverurl_desc'] = 'The URL of the Synapse home server to connect to, for user and room creation.';
$string['matrixaccesstoken'] = 'Access token';
$string['matrixaccesstoken_desc'] = 'Admin access token to authenticate against the Synapse Home server.';
$string['matrixrefreshtoken'] = 'Refresh token';
$string['matrixrefreshtoken_desc'] = 'Admin refresh token to associated with the access token.';
$string['matrixelementurl'] = 'Element web URL';
$string['matrixelementurl_desc'] = 'The URL to Element Web instance.';
$string['pluginname'] = 'Matrix';
$string['privacy:metadata'] = 'Matrix communication plugin does not store any personal data.';

View File

@ -0,0 +1,47 @@
<?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/>.
/**
* Matrix communication plugin settings.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
if ($hassiteconfig) {
// Home server URL.
$name = new lang_string('matrixhomeserverurl', 'communication_matrix');
$desc = new lang_string('matrixhomeserverurl_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredtext('communication_matrix/matrixhomeserverurl', $name, $desc, ''));
// Access token.
$name = new lang_string('matrixaccesstoken', 'communication_matrix');
$desc = new lang_string('matrixaccesstoken_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredpasswordunmask('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.
$name = new lang_string('matrixelementurl', 'communication_matrix');
$desc = new lang_string('matrixelementurl_desc', 'communication_matrix');
$settings->add(new admin_setting_requiredtext('communication_matrix/matrixelementurl', $name, $desc, ''));
}

View File

@ -0,0 +1,65 @@
<?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/>.
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use communication_matrix\matrix_test_helper_trait;
use Moodle\BehatExtension\Exception\SkippedException;
require_once(__DIR__ . '/../matrix_test_helper_trait.php');
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../../../tests/communication_test_helper_trait.php');
/**
* Class behat_communication_matrix for behat custom steps and configuration for matrix.
*
* @package communication_matrix
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_communication_matrix extends \behat_base {
use \core_communication\communication_test_helper_trait;
use matrix_test_helper_trait;
/**
* BeforeScenario hook to reset the mock server.
*
* @BeforeScenario @communication_matrix
*
* @param BeforeScenarioScope $scope
*/
public function before_scenario(BeforeScenarioScope $scope) {
if (defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
$this->reset_mock();
}
}
/**
* Setup and configure and mock server for matrix.
*
* @Given /^a Matrix mock server is configured$/
*/
public function initialize_mock_server(): void {
if (!defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
throw new SkippedException(
'The TEST_COMMUNICATION_MATRIX_MOCK_SERVER constant must be defined to run communication_matrix tests'
);
}
$this->setup_communication_configs();
$this->initialise_mock_configs();
}
}

View File

@ -0,0 +1,48 @@
@communication @communication_matrix @javascript
Feature: Communication matrix
Access the Matrix room using the link provided
As a student or a teacher
Background: Make sure the mock server is initialized and a course is created
Given a Matrix mock server is configured
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | category | selectedcommunication | communicationroomname |
| Test course | Test course | 0 | communication_matrix | matrixroom |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | Test course | editingteacher |
| student1 | Test course | student |
And I run all adhoc tasks
Scenario: I can access the matrix room using the room link icon as a teacher
Given I am on the "Test course" "Course" page logged in as "teacher1"
Then ".btn-footer-communication" "css_element" should be visible
And I change window size to "mobile"
Then ".btn-footer-communication" "css_element" should not be visible
# You should not be able to see the following line unless you are in Mobile view.
# Behat is currently setup to always show footer links.
And ".footer-link-communication" "css_element" should be visible
Scenario: I can access the matrix room using the room link icon as a student
Given I am on the "Test course" "Course" page logged in as "student1"
Then ".btn-footer-communication" "css_element" should be visible
And I change window size to "mobile"
Then ".btn-footer-communication" "css_element" should not be visible
# You should not be able to see the following line unless you are in Mobile view.
# Behat is currently setup to always show footer links.
And ".footer-link-communication" "css_element" should be visible
Scenario: I cannot see the matrix room link when communication provider is disabled
Given I am on the "Test course" "Course editing" page logged in as "teacher1"
And I set the following fields to these values:
| selectedcommunication | none |
And I press "Save and display"
And I run all adhoc tasks
And I reload the page
Then ".btn-footer-communication" "css_element" should not be visible
And I change window size to "mobile"
And ".footer-link-communication" "css_element" should not be visible

View File

@ -0,0 +1,324 @@
<?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/>.
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');
/**
* Class communication_feature_test to test the matrix features implemented using the core interfaces.
*
* @package communication_matrix
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \communication_matrix\communication_feature
*/
class communication_feature_test extends \advanced_testcase {
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();
}
/**
* Test create or update chat room.
*
* @covers ::create_chat_room
* @covers ::update_chat_room
*/
public function test_create_or_update_chat_room() {
$course = $this->getDataGenerator()->create_course();
// Sameple test data.
$instanceid = $course->id;
$component = 'core_course';
$instancetype = 'coursecommunication';
$selectedcommunication = 'communication_matrix';
$communicationroomname = 'communicationroom';
$communicationprocessor = processor::create_instance(
$selectedcommunication,
$instanceid,
$component,
$instancetype,
$communicationroomname,
);
$communicationprocessor->get_room_provider()->create_chat_room();
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
// Test the response against the stored data.
$this->assertNotEmpty($matrixrooms->get_matrix_room_id());
// Add api call to get room data and test against set data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
$this->assertEquals($communicationprocessor->get_room_name(), $matrixroomdata->name);
$communicationroomname = 'communicationroomedited';
$communicationprocessor->update_instance($selectedcommunication, $communicationroomname);
$communicationprocessor->get_room_provider()->update_chat_room();
// Add api call to get room data and test against set data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertEquals($communicationprocessor->get_room_name(), $matrixroomdata->name);
}
/**
* Test delete chat room.
*
* @covers ::delete_chat_room
*/
public function test_delete_chat_room(): void {
$course = $this->getDataGenerator()->create_course();
// Sameple test data.
$instanceid = $course->id;
$component = 'core_course';
$instancetype = 'coursecommunication';
$selectedcommunication = 'communication_matrix';
$communicationroomname = 'communicationroom';
$communicationprocessor = processor::create_instance(
$selectedcommunication,
$instanceid,
$component,
$instancetype,
$communicationroomname,
);
$communicationprocessor->get_room_provider()->create_chat_room();
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$communicationprocessor->get_room_provider()->delete_chat_room();
// We are not deleting any matrix room, just deleting local record.
$matrixroomsafterdeletion = new matrix_rooms($communicationprocessor->get_id());
$this->assertFalse($matrixroomsafterdeletion->room_record_exists());
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertNotEmpty($matrixroomdata);
$this->assertEquals($communicationprocessor->get_room_name(), $matrixroomdata->name);
}
/**
* Test update room avatar.
*
* @covers ::update_room_avatar
*/
public function test_update_room_avatar(): void {
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room(
$selectedcommunication,
$communicationroomname,
$avatarurl
);
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationprocessor->get_room_provider()->create_chat_room();
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
// Add api call to get room data and test against set data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertNotEmpty($matrixroomdata->avatar);
}
/**
* Test get chat room url.
*
* @covers ::get_chat_room_url
*/
public function test_get_chat_room_url(): void {
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room(
$selectedcommunication,
$communicationroomname,
$avatarurl
);
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationprocessor->get_room_provider()->create_chat_room();
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$this->assertNotNull($communicationprocessor->get_room_provider()->get_chat_room_url());
$this->assertStringContainsString(
$matrixrooms->get_matrix_room_id(),
$communicationprocessor->get_room_provider()->get_chat_room_url()
);
}
/**
* Test create members.
*
* @covers ::create_members
* @covers ::add_registered_matrix_user_to_room
*/
public function test_create_members(): void {
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room(
$selectedcommunication,
$communicationroomname,
$avatarurl
);
$communication->add_members_to_room([$userid]);
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationprocessor->get_room_provider()->create_chat_room();
$communicationprocessor->get_room_provider()->add_members_to_room([$userid]);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$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);
$this->assertNotNull($matrixuserid);
$this->assertEquals("@sampleun:{$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);
}
/**
* Test add/remove members from room.
*
* @covers ::remove_members_from_room
* @covers ::add_members_to_room
* @covers ::add_registered_matrix_user_to_room
* @covers ::check_room_membership
*/
public function test_add_and_remove_members_from_room(): void {
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room(
$selectedcommunication,
$communicationroomname,
$avatarurl
);
$communication->add_members_to_room([$userid]);
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationprocessor->get_room_provider()->create_chat_room();
$communicationprocessor->get_room_provider()->add_members_to_room([$userid]);
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
$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);
// Test user is a member of the room.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Remove member from matrix room.
$communicationprocessor->get_room_provider()->remove_members_from_room([$userid]);
// Test user is no longer a member of the room.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
}

View File

@ -0,0 +1,883 @@
<?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/>.
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');
/**
* Class matrix_provider_test to test the matrix provider scenarios using the matrix endpoints.
*
* @package communication_matrix
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class matrix_communication_test extends \advanced_testcase {
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();
}
/**
* Test creating course with matrix provider creates all the associated data and matrix room.
*
* @covers \core_communication\api::create_and_configure_room
* @covers \core_communication\task\create_and_configure_room_task::execute
* @covers \core_communication\task\create_and_configure_room_task::queue
*/
public function test_create_course_with_matrix_provider(): void {
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
// Run the task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
// Initialize the matrix room object.
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
// Test against the data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
$this->assertEquals($roomname, $matrixroomdata->name);
}
/**
* Test update course with matrix provider.
*
* @covers \core_communication\api::update_room
* @covers \core_communication\task\update_room_task::execute
* @covers \core_communication\task\update_room_task::queue
*/
public function test_update_course_with_matrix_provider(): void {
global $CFG;
$course = $this->get_course();
// Run the task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Sample data.
$communicationroomname = 'Sampleroomupdated';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->update_room($selectedcommunication, $communicationroomname, $avatarurl);
// Run the task.
$this->runAdhocTasks('\core_communication\task\update_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
// Initialize the matrix room object.
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
// Test against the data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
$this->assertEquals($communicationroomname, $matrixroomdata->name);
$this->assertNotEmpty($matrixroomdata->avatar);
}
/**
* Test course delete with matrix provider.
*
* @covers \core_communication\api::delete_room
* @covers \core_communication\task\delete_room_task::execute
* @covers \core_communication\task\delete_room_task::queue
*/
public function test_delete_course_with_matrix_provider(): void {
global $DB;
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
// Run the task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationid = $communicationprocessor->get_id();
// Initialize the matrix room object.
$matrixrooms = new matrix_rooms($communicationprocessor->get_id());
// Test against the data.
$matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
$this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
// Now delete the course.
delete_course($course, false);
// Run the task.
$this->runAdhocTasks('\core_communication\task\delete_room_task');
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertNull($communicationprocessor);
// Initialize the matrix room object.
$matrixrooms = $DB->get_record('matrix_rooms', ['commid' => $communicationid]);
$this->assertEmpty($matrixrooms);
}
/**
* Test creating course with matrix provider creates all the associated data and matrix room.
*
* @covers \core_communication\api::add_members_to_room
* @covers \core_communication\task\add_members_to_room_task::execute
* @covers \core_communication\task\add_members_to_room_task::queue
*/
public function test_create_members_with_matrix_provider(): void {
$course = $this->get_course('Samplematrixroom', 'communication_matrix');
$user = $this->get_user('Samplefnmatrix', 'Samplelnmatrix', 'sampleunmatrix');
// Run room operation task.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$enrol->enrol_user(reset($enrolinstances), $user->id);
// Run user operation task.
$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());
// Get matrix user id from moodle.
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $eventmanager->matrixhomeserverurl);
$this->assertNotNull($matrixuserid);
// Get matrix user id from matrix.
$matrixuserdata = $this->get_matrix_user_data($matrixrooms->get_matrix_room_id(), $matrixuserid);
$this->assertNotEmpty($matrixuserdata);
$this->assertEquals("Samplefnmatrix Samplelnmatrix", $matrixuserdata->displayname);
}
/**
* Test enrolment adds the user to a Matrix room.
*
* @covers \core_communication\api::add_members_to_room
* @covers \core_communication\task\add_members_to_room_task::execute
* @covers \core_communication\task\add_members_to_room_task::queue
*/
public function test_enrolling_user_adds_user_to_matrix_room(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolment removes the user from a Matrix room.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_unenrolling_user_removes_user_from_matrix_room(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Unenrol the user from the course.
$enrol->unenrol_user($instance, $user->id);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when their enrolment is suspended.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_suspending_enrolment(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Suspend user enrolment.
$enrol->update_user_enrol($instance, $user->id, 1);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when the instance is deleted.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_deleting_instance(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Delete instance.
$enrol->delete_instance($instance);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when the instance is disabled.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_disabling_instance(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Update enrolment communication.
$enrol->update_communication($instance->id, 'remove', $course->id);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users memerbship toggles correctly when an instance is disabled and reenabled again.
*
* @covers \core_communication\api::add_members_to_room
* @covers \core_communication\task\add_members_to_room_task::execute
* @covers \core_communication\task\add_members_to_room_task::queue
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_memerbship_toggles_when_disabling_and_reenabling_instance(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// 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.
$enrol->update_communication($instance->id, 'remove', $course->id);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Update enrolment communication when updating instance to enabled.
$enrol->update_communication($instance->id, 'add', $course->id);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Check our Matrix user id no longer has membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when the provider is disabled.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_disabling_provider(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
require_once($CFG->dirroot . '/course/lib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Disable communication provider.
$course->selectedcommunication = 'none';
update_course($course);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when their user account is suspended.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_suspending_user(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Suspend user.
$user->suspended = 1;
user_update_user($user, false, false);
// Run the user tasks.
$this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test enrolled users in a course lose access to a room when their user account is deleted.
*
* @covers \core_communication\api::remove_members_from_room
* @covers \core_communication\task\remove_members_from_room::execute
* @covers \core_communication\task\remove_members_from_room::queue
*/
public function test_users_removed_from_room_when_deleting_user(): void {
global $CFG;
require_once($CFG->dirroot . '/lib/enrollib.php');
// Sample data.
$roomname = 'Samplematrixroom';
$provider = 'communication_matrix';
$course = $this->get_course($roomname, $provider);
$user = $this->get_user();
// Run room tasks.
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Enrol the user in the course.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course->id, true);
$instance = reset($enrolinstances);
$enrol->enrol_user($instance, $user->id);
// Run the user tasks.
$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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
$matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
// Check our Matrix user id has room membership.
$this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
// Delete user.
delete_user($user);
// Run the user tasks.
// $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
// Check our Matrix user id no longer has membership.
$this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
}
/**
* Test create instance user mapping.
*
* @covers \core_communication\processor::create_instance_user_mapping
* @covers \core_communication\processor::mark_users_as_synced
* @covers \core_communication\processor::get_instance_userids
*/
public function test_create_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Test against the object.
$communicationprocessor = processor::load_by_instance(
$component,
$instancetype,
$course->id
);
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEquals($communicationuserrecord->userid, $userid);
$this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
}
/**
* Test update instance user mapping.
*
* @covers \core_communication\processor::create_instance_user_mapping
* @covers \core_communication\processor::mark_users_as_synced
* @covers \core_communication\processor::get_instance_userids
* @covers \core_communication\processor::delete_instance_user_mapping
*/
public function test_update_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course();
$userid = $this->get_user()->id;
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->update_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\update_room_task');
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Test against the object.
$communicationprocessor = processor::load_by_instance(
$component,
$instancetype,
$course->id
);
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEquals($communicationuserrecord->userid, $userid);
$this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
// Now add again.
$communicationprocessor->delete_instance_user_mapping([$userid]);
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
/**
* Test delete instance user mapping.
*
* @covers \core_communication\processor::create_instance_user_mapping
* @covers \core_communication\processor::mark_users_as_synced
* @covers \core_communication\processor::get_instance_userids
* @covers \core_communication\processor::delete_instance_user_mapping
*/
public function test_delete_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Test against the object.
$communicationprocessor = processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
// Delete the user mapping.
$communicationprocessor->delete_instance_user_mapping([$userid]);
$this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
/**
* Test delete user mappings for instance.
*
* @covers \core_communication\processor::create_instance_user_mapping
* @covers \core_communication\processor::mark_users_as_synced
* @covers \core_communication\processor::get_instance_userids
* @covers \core_communication\processor::delete_user_mappings_for_instance
*/
public function test_delete_user_mappings_for_instance(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
$this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
$this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
// Test against the object.
$communicationprocessor = processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
// Delete the user mapping.
$communicationprocessor->delete_user_mappings_for_instance();
$this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
}

View File

@ -0,0 +1,125 @@
<?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/>.
namespace communication_matrix;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/matrix_test_helper_trait.php');
/**
* Class matrix_events_manager_test to test the matrix events endpoint.
*
* @package communication_matrix
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \communication_matrix\matrix_events_manager
*/
class matrix_events_manager_test extends \advanced_testcase {
use matrix_test_helper_trait;
public function setUp(): void {
parent::setUp();
$this->initialise_mock_server();
}
/**
* Test the api endpoints url's for matrix.
*
* @return void
* @covers ::get_token
* @covers ::get_update_avatar_endpoint
* @covers ::get_update_room_topic_endpoint
* @covers ::get_update_room_name_endpoint
* @covers ::get_create_room_endpoint
* @covers ::get_delete_room_endpoint
* @covers ::get_upload_content_endpoint
*/
public function test_matrix_api_endpoints(): void {
$this->resetAfterTest();
$mockroomid = 'sampleroomid';
$mockuserid = 'sampleuserid';
$matrixeventsmanager = new matrix_events_manager($mockroomid);
// Test the endpoints and information.
$this->assertEquals($this->get_matrix_access_token(), $matrixeventsmanager->get_token());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/client/r0/createRoom',
$matrixeventsmanager->get_create_room_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($mockroomid) . '/' . 'state/m.room.topic/',
$matrixeventsmanager->get_update_room_topic_endpoint());
$this->assertEquals($this->get_matrix_server_url(). '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($mockroomid) . '/' . 'state/m.room.name/',
$matrixeventsmanager->get_update_room_name_endpoint());
$this->assertEquals($this->get_matrix_server_url(). '/' . '_synapse/admin/v1/rooms' .
'/' . urlencode($mockroomid), $matrixeventsmanager->get_room_info_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_synapse/admin/v1/rooms/' . urlencode($mockroomid),
$matrixeventsmanager->get_delete_room_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/media/r0/upload/',
$matrixeventsmanager->get_upload_content_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($mockroomid) . '/' . 'state/m.room.avatar/',
$matrixeventsmanager->get_update_avatar_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($mockroomid) . '/' . 'joined_members',
$matrixeventsmanager->get_room_membership_joined_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_synapse/admin/v1/join' .
'/' . urlencode($mockroomid),
$matrixeventsmanager->get_room_membership_join_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_matrix/client/r0/rooms' .
'/' . urlencode($mockroomid) . '/' . 'kick',
$matrixeventsmanager->get_room_membership_kick_endpoint());
$this->assertEquals($this->get_matrix_server_url() . '/' . '_synapse/admin/v2/users/' . urlencode($mockuserid),
$matrixeventsmanager->get_create_user_endpoint($mockuserid));
$this->assertEquals($this->get_matrix_server_url() . '/' . '_synapse/admin/v2/users/' . urlencode($mockuserid),
$matrixeventsmanager->get_user_info_endpoint($mockuserid));
}
/**
* Test upload matrix content.
*
* @return void
* @covers ::upload_matrix_content
*/
public function test_upload_matrix_content(): void {
global $CFG;
$this->resetAfterTest();
$mockroomid = 'sampleroomid';
$filecontent = file_get_contents($CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg');
$matrixeventsmanager = new matrix_events_manager($mockroomid);
$response = $matrixeventsmanager->upload_matrix_content($filecontent);
$this->assertNotFalse($response);
$this->assertNotEmpty($response);
}
}

View File

@ -0,0 +1,179 @@
<?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/>.
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');
/**
* Class matrix_rooms_test to test the matrix room data in db.
*
* @package communication_matrix
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \communication_matrix\matrix_rooms
*/
class matrix_rooms_test extends \advanced_testcase {
use matrix_test_helper_trait;
use communication_test_helper_trait;
public function setUp(): void {
parent::setUp();
$this->resetAfterTest();
$this->setup_communication_configs();
}
/**
* Test the matrix room creation in database.
*
* @covers ::create_matrix_room_record
*/
public function test_create_matrix_room_record(): void {
global $DB;
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
$matrixroom->create_matrix_room_record(
$communicationprocessor->get_id(),
$sampleroomid,
);
// Test the object.
$this->assertEquals($matrixroom->get_matrix_room_id(), $sampleroomid);
// Get the record from db.
$matrixrecord = $DB->get_record('matrix_rooms',
['commid' => $communicationprocessor->get_id()]);
// Check the record against sample data.
$this->assertNotEmpty($matrixrecord);
$this->assertEquals($sampleroomid, $matrixrecord->roomid);
$this->assertEquals($communicationprocessor->get_id(), $matrixrecord->commid);
}
/**
* Test matrix room record updates.
*
* @covers ::update_matrix_room_record
*/
public function test_update_matrix_room_record(): void {
global $DB;
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
$matrixroom->create_matrix_room_record(
$communicationprocessor->get_id(),
$sampleroomid,
);
// Get the record from db.
$matrixrecord = $DB->get_record('matrix_rooms',
['commid' => $communicationprocessor->get_id()]);
// Check the record against sample data.
$this->assertNotEmpty($matrixrecord);
$sampleroomidupdated = 'samplematrixroomidupdated';
$matrixroom->update_matrix_room_record(
$sampleroomidupdated,
);
// Test the object.
$this->assertEquals($matrixroom->get_matrix_room_id(), $sampleroomidupdated);
// Get the record from db.
$matrixrecord = $DB->get_record('matrix_rooms',
['commid' => $communicationprocessor->get_id()]);
// Check the record against sample data.
$this->assertNotEmpty($matrixrecord);
$this->assertEquals($sampleroomidupdated, $matrixrecord->roomid);
$this->assertEquals($communicationprocessor->get_id(), $matrixrecord->commid);
}
/**
* Test matrix room deletion.
*
* @covers ::delete_matrix_room_record
* @covers ::get_matrix_room_id
*/
public function test_delete_matrix_room_record(): void {
global $DB;
$course = $this->get_course();
$sampleroomid = 'samplematrixroomid';
// Communication internal api call.
$communicationprocessor = processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
// Call matrix room object to create the matrix data.
$matrixroom = new \communication_matrix\matrix_rooms($communicationprocessor->get_id());
$matrixroom->create_matrix_room_record(
$communicationprocessor->get_id(),
$sampleroomid,
);
// Get the record from db.
$matrixrecord = $DB->get_record('matrix_rooms',
['commid' => $communicationprocessor->get_id()]);
// Check the record against sample data.
$this->assertNotEmpty($matrixrecord);
// Now delete the record.
$matrixroom->delete_matrix_room_record();
// Get the record from db.
$matrixrecord = $DB->get_record('matrix_rooms',
['commid' => $communicationprocessor->get_id()]);
// Check the record against sample data.
$this->assertEmpty($matrixrecord);
}
}

View File

@ -0,0 +1,166 @@
<?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/>.
namespace communication_matrix;
/**
* Trait matrix_helper_trait to generate initial setup for matrix mock and associated helpers.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait matrix_test_helper_trait {
/**
* @var string $accesstoken The token for matrix connection
*/
protected string $accesstoken;
/**
* @var string $matrixhomeserverurl The server url of matrix synapse server
*/
protected string $matrixhomeserverurl;
/**
* Initialize the mock configs in settings.
*
* @return void
*/
protected function initialise_mock_configs(): void {
$this->matrixhomeserverurl = TEST_COMMUNICATION_MATRIX_MOCK_SERVER;
set_config('matrixhomeserverurl', $this->matrixhomeserverurl, 'communication_matrix');
$request = $this->request();
$response = $request->post($this->matrixhomeserverurl. '/backoffice/create-admin');
$admindata = json_decode($response->getBody());
$json = [
'identifier' => [
'type' => 'm.id.user',
'user' => $admindata->user_id,
],
'type' => 'm.login.password',
'password' => $admindata->password,
];
$request = $this->request($json);
$response = $request->post($this->matrixhomeserverurl. '/_matrix/client/r0/login');
$response = json_decode($response->getBody());
if (empty($response->access_token)) {
$this->markTestSkipped(
'The matrix mock server is not responsive, can not continue the tests'
);
}
$this->accesstoken = $response->access_token;
set_config('matrixaccesstoken', $this->accesstoken, 'communication_matrix');
}
/**
* Get the mock server url.
*
* @return string
*/
public function get_matrix_server_url(): string {
if (empty($this->matrixhomeserverurl)) {
throw new \coding_exception('Can not get this information without initializing the mock server.');
}
return $this->matrixhomeserverurl;
}
/**
* Get the matrix access token.
*
* @return string
*/
public function get_matrix_access_token(): string {
if (empty($this->accesstoken)) {
throw new \coding_exception('Can not get this information without initializing the mock server.');
}
return $this->accesstoken;
}
/**
* This test requires mock server to be present.
*
* @return void
*/
protected function initialise_mock_server(): void {
if (!defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
$this->markTestSkipped(
'The TEST_COMMUNICATION_MATRIX_MOCK_SERVER constant must be defined to run communication_matrix tests'
);
}
$this->reset_mock();
$this->initialise_mock_configs();
}
/**
* Get matrix room data from matrix server.
*
* @param string $roomid The id of the room
* @return \stdClass
*/
public function get_matrix_room_data(string $roomid): \stdClass {
$matrixeventmanager = new matrix_events_manager($roomid);
$response = $matrixeventmanager->request()->get($matrixeventmanager->get_room_info_endpoint());
return json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
}
/**
* Get matrix user data from matrix server.
*
* @param string $roomid The id of the room
* @param string $matrixuserid The id of the user
* @return \stdClass
*/
public function get_matrix_user_data(string $roomid, string $matrixuserid): \stdClass {
$matrixeventmanager = new matrix_events_manager($roomid);
$response = $matrixeventmanager->request()->get($matrixeventmanager->get_user_info_endpoint($matrixuserid));
return json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
}
/**
* The http request for the api call.
*
* @param array $jsonarray The array of json
* @param array $headers The array of headers
* @return \core\http_client
*/
public function request(array $jsonarray = [], array $headers = []): \core\http_client {
$response = new \core\http_client([
'headers' => $headers,
'json' => $jsonarray,
]);
return $response;
}
/**
* Reset the mock server
*
* @return void
*/
public function reset_mock(): void {
if (defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
$request = $this->request();
$response = $request->post(TEST_COMMUNICATION_MATRIX_MOCK_SERVER. '/backoffice/reset');
$response = json_decode($response->getBody());
if (empty($response->reset)) {
$this->markTestSkipped(
'The matrix mock server is not responsive, can not continue the tests'
);
}
}
}
}

View File

@ -0,0 +1,255 @@
<?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/>.
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');
/**
* Class matrix_user_manager_test to test the matrix user manager.
*
* @package communication_matrix
* @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
*/
class matrix_user_manager_test extends \advanced_testcase {
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();
}
/**
* Test get matrix id from moodle.
*
* @covers ::get_matrixid_from_moodle
*/
public function test_get_matrixid_from_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());
$matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
// 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);
}
/**
* Sets qualified matrix user id.
*
* @return void
* @covers ::set_qualified_matrix_user_id
*/
public function test_set_qualified_matrix_user_id(): void {
$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
);
$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);
}
/**
* Add user's matrix id to moodle.
*
* @covers ::add_user_matrix_id_to_moodle
*/
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);
}
/**
* 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
*/
public function test_create_matrix_user_profile_fields(): 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');
// 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);
}
}

View File

@ -0,0 +1,30 @@
<?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/>.
/**
* Version information for communication_matrix.
*
* @package communication_matrix
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'communication_matrix';
$plugin->version = 2023032900;
$plugin->requires = 2023011300;
$plugin->maturity = MATURITY_ALPHA;

View File

@ -1,587 +0,0 @@
<?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/>.
namespace core_communication;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/communication_test_helper_trait.php');
/**
* Class communication_processor_test to test the communication internal api and its associated methods.
*
* @package core_communication
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_communication\communication_processor
*/
class communication_processor_test extends \advanced_testcase {
use communication_test_helper_trait;
/**
* Test create instance.
*
* @covers ::create_instance
*/
public function test_create_instance(): void {
global $DB;
$this->resetAfterTest();
// Sameple test data.
$instanceid = 10;
$component = 'core_course';
$instancetype = 'coursecommunication';
$selectedcommunication = 'communication_matrix';
$communicationroomname = 'communicationroom';
$communicationprocessor = communication_processor::create_instance(
$selectedcommunication,
$instanceid,
$component,
$instancetype,
$communicationroomname,
);
// Now test the record against the database.
$communicationrecord = $DB->get_record('communication',
['instanceid' => $instanceid, 'component' => $component, 'instancetype' => $instancetype]);
// Test against the set data.
$this->assertNotEmpty($communicationrecord);
$this->assertEquals($instanceid, $communicationrecord->instanceid);
$this->assertEquals($component, $communicationrecord->component);
$this->assertEquals($selectedcommunication, $communicationrecord->provider);
$this->assertEquals($communicationroomname, $communicationrecord->roomname);
$this->assertEquals($instancetype, $communicationrecord->instancetype);
// Test against the object.
$this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
$this->assertEquals($communicationprocessor->get_provider(), $communicationrecord->provider);
$this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
}
/**
* Test update instance.
*
* @covers ::update_instance
*/
public function test_update_instance(): void {
global $DB;
$this->resetAfterTest();
// Sameple test data.
$instanceid = 10;
$component = 'core_course';
$instancetype = 'coursecommunication';
$selectedcommunication = 'communication_matrix';
$communicationroomname = 'communicationroom';
$communicationprocessor = communication_processor::create_instance(
$selectedcommunication,
$instanceid,
$component,
$instancetype,
$communicationroomname,
);
$selectedcommunication = 'none';
$communicationroomname = 'communicationroomedited';
$communicationprocessor->update_instance($selectedcommunication, $communicationroomname);
// Now test the record against the database.
$communicationrecord = $DB->get_record('communication', [
'instanceid' => $instanceid,
'component' => $component,
'instancetype' => $instancetype
]);
// Test against the set data.
$this->assertNotEmpty($communicationrecord);
$this->assertEquals($instanceid, $communicationrecord->instanceid);
$this->assertEquals($component, $communicationrecord->component);
$this->assertEquals($selectedcommunication, $communicationrecord->provider);
$this->assertEquals($communicationroomname, $communicationrecord->roomname);
$this->assertEquals($instancetype, $communicationrecord->instancetype);
// Test against the object.
$this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
$this->assertEquals($communicationprocessor->get_provider(), $communicationrecord->provider);
$this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
}
/**
* Test delete instance.
*
* @covers ::delete_instance
*/
public function test_delete_instance(): void {
global $DB;
$this->resetAfterTest();
// Sameple test data.
$instanceid = 10;
$component = 'core_course';
$instancetype = 'coursecommunication';
$selectedcommunication = 'communication_matrix';
$communicationroomname = 'communicationroom';
$communicationprocessor = communication_processor::create_instance(
$selectedcommunication,
$instanceid,
$component,
$instancetype,
$communicationroomname,
);
$communicationprocessor->delete_instance();
// Now test the record against the database.
$communicationrecord = $DB->get_record('communication', [
'instanceid' => $instanceid,
'component' => $component,
'instancetype' => $instancetype
]);
// Test against the set data.
$this->assertEmpty($communicationrecord);
// Test against the object.
$communicationprocessor = communication_processor::load_by_instance(
$component,
$instancetype,
$instanceid);
$this->assertNull($communicationprocessor);
}
/**
* Test create instance user mapping.
*
* @covers ::create_instance_user_mapping
* @covers ::get_all_userids_for_instance
*/
public function test_create_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
// Test against the object.
$communicationprocessor = communication_processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEquals($communicationuserrecord->userid, $userid);
$this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
}
/**
* Test update instance user mapping.
*
* @covers ::get_all_userids_for_instance
*/
public function test_update_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course();
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->update_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
// Test against the object.
$communicationprocessor = communication_processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertnotEmpty($communicationprocessor->get_instance_userids_by_synced());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEquals($communicationuserrecord->userid, $userid);
$this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
// Now add again.
$communicationprocessor->delete_instance_user_mapping([$userid]);
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
/**
* Test delete instance user mapping.
*
* @covers ::delete_instance_user_mapping
* @covers ::get_all_userids_for_instance
*/
public function test_delete_instance_user_mapping(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
// Test against the object.
$communicationprocessor = communication_processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
// Delete the user mapping.
$communicationprocessor->delete_instance_user_mapping([$userid]);
$this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
/**
* Test delete user mappings for instance.
*
* @covers ::delete_user_mappings_for_instance
* @covers ::get_all_userids_for_instance
*/
public function test_delete_user_mappings_for_instance(): void {
$this->resetAfterTest();
global $DB;
$course = $this->get_course('Sampleroom', 'none');
$userid = $this->get_user()->id;
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$component = 'core_course';
$instancetype = 'coursecommunication';
// First test the adding members to a room.
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname);
$communication->add_members_to_room([$userid]);
// Test against the object.
$communicationprocessor = communication_processor::load_by_instance(
$component,
$instancetype,
$course->id
);
$this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
// Delete the user mapping.
$communicationprocessor->delete_user_mappings_for_instance();
$this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
// Test against the database.
$communicationuserrecord = $DB->get_record('communication_user', [
'commid' => $communicationprocessor->get_id(),
'userid' => $userid
]);
$this->assertEmpty($communicationuserrecord);
}
/**
* Test load by id.
*
* @covers ::load_by_instance
*/
public function test_load_by_instance(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertNotNull($communicationprocessor);
$this->assertInstanceOf(communication_provider::class, $communicationprocessor->get_room_provider());
$this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
$this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_provider());
$this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
}
/**
* Test load by id.
*
* @covers ::load_by_id
*/
public function test_load_by_id(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communicationprocessorbyid = communication_processor::load_by_id($communicationprocessor->get_id());
$this->assertNotNull($communicationprocessorbyid);
$this->assertInstanceOf(communication_provider::class, $communicationprocessorbyid->get_room_provider());
$this->assertInstanceOf(room_chat_provider::class, $communicationprocessorbyid->get_room_provider());
$this->assertInstanceOf(room_user_provider::class, $communicationprocessorbyid->get_room_provider());
$this->assertInstanceOf(user_provider::class, $communicationprocessorbyid->get_room_provider());
}
/**
* Test get component.
*
* @covers ::get_component
*/
public function test_get_component(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertEquals('core_course', $communicationprocessor->get_component());
}
/**
* Test get provider.
*
* @covers ::get_provider
*/
public function test_get_provider(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
}
/**
* Test get room name.
*
* @covers ::get_room_name
*/
public function test_get_room_name(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertEquals('Sampleroom', $communicationprocessor->get_room_name());
}
/**
* Test get room provider.
*
* @covers ::get_room_provider
* @covers ::require_room_features
* @covers ::supports_room_features
*/
public function test_get_room_provider(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
}
/**
* Test get user provider.
*
* @covers ::get_user_provider
* @covers ::require_user_features
* @covers ::supports_user_features
*/
public function test_get_user_provider(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
}
/**
* Test get room user provider.
*
* @covers ::get_room_user_provider
* @covers ::require_room_features
* @covers ::require_room_user_features
* @covers ::supports_room_user_features
* @covers ::supports_room_features
*/
public function test_get_room_user_provider(): void {
$this->resetAfterTest();
$course = $this->get_course();
// Test the communication record exists.
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_user_provider());
}
/**
* Test get avatar.
*
* @covers ::get_avatar
*/
public function test_get_avatar(): void {
$this->resetAfterTest();
global $CFG;
$course = $this->get_course('Sampleroom', 'none');
// Sample data.
$communicationroomname = 'Sampleroom';
$selectedcommunication = 'communication_matrix';
$avatarurl = $CFG->dirroot . '/communication/tests/fixtures/moodle_logo.jpg';
$communication = \core_communication\api::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$communication->create_and_configure_room($selectedcommunication, $communicationroomname, $avatarurl);
$communicationprocessor = communication_processor::load_by_instance(
'core_course',
'coursecommunication',
$course->id
);
$avatar = $communicationprocessor->get_avatar();
$this->assertNotNull($avatar);
$this->assertEquals($avatar->get_component(), 'core_communication');
$this->assertEquals($avatar->get_filearea(), 'avatar');
$this->assertEquals($avatar->get_itemid(), $communicationprocessor->get_id());
$this->assertEquals($avatar->get_filepath(), '/');
$this->assertEquals($avatar->get_filearea(), 'avatar');
}
}

View File

@ -1825,7 +1825,9 @@ class core_plugin_manager {
'gregorian'
),
'communication' => [],
'communication' => [
'matrix',
],
'contenttype' => array(
'h5p'

View File

@ -113,4 +113,22 @@ class communication extends base {
return true;
}
/**
* Checks if a communication plugin is ready to be used.
* It checks the plugin status as well as the plugin is missing or not.
*
* @param string $fullpluginname the name of the plugin
* @return bool
*/
public static function is_plugin_enabled($fullpluginname): bool {
$pluginmanager = \core_plugin_manager::instance();
$communicationinfo = $pluginmanager->get_plugin_info($fullpluginname);
if (empty($communicationinfo)) {
return false;
}
$communicationavailable = $communicationinfo->get_status();
return !($communicationavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING ||
!empty(get_config($fullpluginname, 'disabled')));
}
}