MDL-78738 core_communication: Set avatar from a stored_file
[moodle.git] / communication / provider / matrix / tests / matrix_communication_test.php
blob68d3e8464fe019307df82c5492c48492ffdd0c41
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace communication_matrix;
19 use core_communication\processor;
20 use core_communication\communication_test_helper_trait;
22 defined('MOODLE_INTERNAL') || die();
24 require_once(__DIR__ . '/matrix_test_helper_trait.php');
25 require_once(__DIR__ . '/../../../tests/communication_test_helper_trait.php');
27 /**
28 * Class matrix_provider_test to test the matrix provider scenarios using the matrix endpoints.
30 * @package communication_matrix
31 * @category test
32 * @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class matrix_communication_test extends \advanced_testcase {
37 use matrix_test_helper_trait;
38 use communication_test_helper_trait;
40 public function setUp(): void {
41 parent::setUp();
42 $this->resetAfterTest();
43 $this->setup_communication_configs();
44 $this->initialise_mock_server();
47 /**
48 * Test creating course with matrix provider creates all the associated data and matrix room.
50 * @covers \core_communication\api::create_and_configure_room
51 * @covers \core_communication\task\create_and_configure_room_task::execute
52 * @covers \core_communication\task\create_and_configure_room_task::queue
54 public function test_create_course_with_matrix_provider(): void {
55 // Sample data.
56 $roomname = 'Samplematrixroom';
57 $provider = 'communication_matrix';
58 $course = $this->get_course($roomname, $provider);
60 // Run the task.
61 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
63 $communicationprocessor = processor::load_by_instance(
64 'core_course',
65 'coursecommunication',
66 $course->id,
69 // Initialize the matrix room object.
70 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
72 // Test against the data.
73 $matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
74 $this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
75 $this->assertEquals($roomname, $matrixroomdata->name);
78 /**
79 * Test update course with matrix provider.
81 * @covers \core_communication\api::update_room
82 * @covers \core_communication\task\update_room_task::execute
83 * @covers \core_communication\task\update_room_task::queue
85 public function test_update_course_with_matrix_provider(): void {
86 global $CFG;
87 $course = $this->get_course();
89 // Run the task.
90 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
92 // Sample data.
93 $communicationroomname = 'Sampleroomupdated';
94 $selectedcommunication = 'communication_matrix';
95 $logo = $this->create_communication_file('moodle_logo.jpg', 'logo.jpg');
97 $communication = \core_communication\api::load_by_instance(
98 'core_course',
99 'coursecommunication',
100 $course->id,
102 $communication->update_room($selectedcommunication, $communicationroomname, $logo);
104 // Pending avatar update should indicate avatar is not in sync.
105 $communicationprocessor = processor::load_by_instance(
106 'core_course',
107 'coursecommunication',
108 $course->id
110 $this->assertFalse($communicationprocessor->is_avatar_synced());
112 // Run the task.
113 $this->runAdhocTasks('\core_communication\task\update_room_task');
115 $communicationprocessor = processor::load_by_instance(
116 'core_course',
117 'coursecommunication',
118 $course->id
121 // Check that the avatar is now synced with Matrix again.
122 $this->assertTrue($communicationprocessor->is_avatar_synced());
124 // Initialize the matrix room object.
125 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
127 // Test against the data.
128 $matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
129 $this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
130 $this->assertEquals($communicationroomname, $matrixroomdata->name);
134 * Test course delete with matrix provider.
136 * @covers \core_communication\api::delete_room
137 * @covers \core_communication\task\delete_room_task::execute
138 * @covers \core_communication\task\delete_room_task::queue
140 public function test_delete_course_with_matrix_provider(): void {
141 global $DB;
142 // Sample data.
143 $roomname = 'Samplematrixroom';
144 $provider = 'communication_matrix';
145 $course = $this->get_course($roomname, $provider);
147 // Run the task.
148 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
150 $communicationprocessor = processor::load_by_instance(
151 'core_course',
152 'coursecommunication',
153 $course->id
155 $communicationid = $communicationprocessor->get_id();
157 // Initialize the matrix room object.
158 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
160 // Test against the data.
161 $matrixroomdata = $this->get_matrix_room_data($matrixrooms->get_matrix_room_id());
162 $this->assertEquals($matrixrooms->get_matrix_room_id(), $matrixroomdata->room_id);
164 // Now delete the course.
165 delete_course($course, false);
167 // Run the task.
168 $this->runAdhocTasks('\core_communication\task\delete_room_task');
170 $communicationprocessor = processor::load_by_instance(
171 'core_course',
172 'coursecommunication',
173 $course->id
175 $this->assertNull($communicationprocessor);
177 // Initialize the matrix room object.
178 $matrixrooms = $DB->get_record('matrix_rooms', ['commid' => $communicationid]);
179 $this->assertEmpty($matrixrooms);
183 * Test creating course with matrix provider creates all the associated data and matrix room.
185 * @covers \core_communication\api::add_members_to_room
186 * @covers \core_communication\task\add_members_to_room_task::execute
187 * @covers \core_communication\task\add_members_to_room_task::queue
189 public function test_create_members_with_matrix_provider(): void {
190 $course = $this->get_course('Samplematrixroom', 'communication_matrix');
191 $user = $this->get_user('Samplefnmatrix', 'Samplelnmatrix', 'sampleunmatrix');
193 // Run room operation task.
194 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
196 // Enrol the user in the course.
197 $enrol = enrol_get_plugin('manual');
198 $enrolinstances = enrol_get_instances($course->id, true);
199 $enrol->enrol_user(reset($enrolinstances), $user->id);
201 // Run user operation task.
202 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
204 $communicationprocessor = processor::load_by_instance(
205 'core_course',
206 'coursecommunication',
207 $course->id
209 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
210 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
212 // Get matrix user id from moodle.
213 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $eventmanager->matrixhomeserverurl);
214 $this->assertNotNull($matrixuserid);
216 // Get matrix user id from matrix.
217 $matrixuserdata = $this->get_matrix_user_data($matrixrooms->get_matrix_room_id(), $matrixuserid);
218 $this->assertNotEmpty($matrixuserdata);
219 $this->assertEquals("Samplefnmatrix Samplelnmatrix", $matrixuserdata->displayname);
223 * Test enrolment adds the user to a Matrix room.
225 * @covers \core_communication\api::add_members_to_room
226 * @covers \core_communication\task\add_members_to_room_task::execute
227 * @covers \core_communication\task\add_members_to_room_task::queue
229 public function test_enrolling_user_adds_user_to_matrix_room(): void {
230 global $CFG;
231 require_once($CFG->dirroot . '/lib/enrollib.php');
233 // Sample data.
234 $roomname = 'Samplematrixroom';
235 $provider = 'communication_matrix';
236 $course = $this->get_course($roomname, $provider);
237 $user = $this->get_user();
239 // Run room tasks.
240 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
242 // Enrol the user in the course.
243 $enrol = enrol_get_plugin('manual');
244 $enrolinstances = enrol_get_instances($course->id, true);
245 $instance = reset($enrolinstances);
246 $enrol->enrol_user($instance, $user->id);
248 // Run the user tasks.
249 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
251 $communicationprocessor = processor::load_by_instance(
252 'core_course',
253 'coursecommunication',
254 $course->id
257 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
258 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
259 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
261 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
262 // Check our Matrix user id has room membership.
263 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
267 * Test enrolment removes the user from a Matrix room.
269 * @covers \core_communication\api::remove_members_from_room
270 * @covers \core_communication\task\remove_members_from_room::execute
271 * @covers \core_communication\task\remove_members_from_room::queue
273 public function test_unenrolling_user_removes_user_from_matrix_room(): void {
274 global $CFG;
275 require_once($CFG->dirroot . '/lib/enrollib.php');
277 // Sample data.
278 $roomname = 'Samplematrixroom';
279 $provider = 'communication_matrix';
280 $course = $this->get_course($roomname, $provider);
281 $user = $this->get_user();
283 // Run room tasks.
284 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
286 // Enrol the user in the course.
287 $enrol = enrol_get_plugin('manual');
288 $enrolinstances = enrol_get_instances($course->id, true);
289 $instance = reset($enrolinstances);
290 $enrol->enrol_user($instance, $user->id);
292 // Run the user tasks.
293 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
295 $communicationprocessor = processor::load_by_instance(
296 'core_course',
297 'coursecommunication',
298 $course->id
301 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
302 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
303 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
305 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
306 // Check our Matrix user id has room membership.
307 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
308 // Unenrol the user from the course.
309 $enrol->unenrol_user($instance, $user->id);
310 // Run the user tasks.
311 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
312 // Check our Matrix user id no longer has membership.
313 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
317 * Test enrolled users in a course lose access to a room when their enrolment is suspended.
319 * @covers \core_communication\api::remove_members_from_room
320 * @covers \core_communication\task\remove_members_from_room::execute
321 * @covers \core_communication\task\remove_members_from_room::queue
323 public function test_users_removed_from_room_when_suspending_enrolment(): void {
324 global $CFG;
325 require_once($CFG->dirroot . '/lib/enrollib.php');
327 // Sample data.
328 $roomname = 'Samplematrixroom';
329 $provider = 'communication_matrix';
330 $course = $this->get_course($roomname, $provider);
331 $user = $this->get_user();
333 // Run room tasks.
334 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
336 // Enrol the user in the course.
337 $enrol = enrol_get_plugin('manual');
338 $enrolinstances = enrol_get_instances($course->id, true);
339 $instance = reset($enrolinstances);
340 $enrol->enrol_user($instance, $user->id);
342 // Run the user tasks.
343 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
345 $communicationprocessor = processor::load_by_instance(
346 'core_course',
347 'coursecommunication',
348 $course->id
350 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
351 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
352 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
354 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
355 // Check our Matrix user id has room membership.
356 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
357 // Suspend user enrolment.
358 $enrol->update_user_enrol($instance, $user->id, 1);
359 // Run the user tasks.
360 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
361 // Check our Matrix user id no longer has membership.
362 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
366 * Test enrolled users in a course lose access to a room when the instance is deleted.
368 * @covers \core_communication\api::remove_members_from_room
369 * @covers \core_communication\task\remove_members_from_room::execute
370 * @covers \core_communication\task\remove_members_from_room::queue
372 public function test_users_removed_from_room_when_deleting_instance(): void {
373 global $CFG;
374 require_once($CFG->dirroot . '/lib/enrollib.php');
376 // Sample data.
377 $roomname = 'Samplematrixroom';
378 $provider = 'communication_matrix';
379 $course = $this->get_course($roomname, $provider);
380 $user = $this->get_user();
382 // Run room tasks.
383 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
385 // Enrol the user in the course.
386 $enrol = enrol_get_plugin('manual');
387 $enrolinstances = enrol_get_instances($course->id, true);
388 $instance = reset($enrolinstances);
389 $enrol->enrol_user($instance, $user->id);
391 // Run the user tasks.
392 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
394 $communicationprocessor = processor::load_by_instance(
395 'core_course',
396 'coursecommunication',
397 $course->id
399 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
400 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
401 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
403 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
404 // Check our Matrix user id has room membership.
405 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
406 // Delete instance.
407 $enrol->delete_instance($instance);
408 // Run the user tasks.
409 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
410 // Check our Matrix user id no longer has membership.
411 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
415 * Test enrolled users in a course lose access to a room when the instance is disabled.
417 * @covers \core_communication\api::remove_members_from_room
418 * @covers \core_communication\task\remove_members_from_room::execute
419 * @covers \core_communication\task\remove_members_from_room::queue
421 public function test_users_removed_from_room_when_disabling_instance(): void {
422 global $CFG;
423 require_once($CFG->dirroot . '/lib/enrollib.php');
425 // Sample data.
426 $roomname = 'Samplematrixroom';
427 $provider = 'communication_matrix';
428 $course = $this->get_course($roomname, $provider);
429 $user = $this->get_user();
431 // Run room tasks.
432 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
434 // Enrol the user in the course.
435 $enrol = enrol_get_plugin('manual');
436 $enrolinstances = enrol_get_instances($course->id, true);
437 $instance = reset($enrolinstances);
438 $enrol->enrol_user($instance, $user->id);
440 // Run the user tasks.
441 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
443 $communicationprocessor = processor::load_by_instance(
444 'core_course',
445 'coursecommunication',
446 $course->id
448 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
449 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
450 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
452 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
453 // Check our Matrix user id has room membership.
454 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
455 // Update enrolment communication.
456 $enrol->update_communication($instance->id, 'remove', $course->id);
457 // Run the user tasks.
458 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
459 // Check our Matrix user id no longer has membership.
460 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
464 * Test enrolled users memerbship toggles correctly when an instance is disabled and reenabled again.
466 * @covers \core_communication\api::add_members_to_room
467 * @covers \core_communication\task\add_members_to_room_task::execute
468 * @covers \core_communication\task\add_members_to_room_task::queue
469 * @covers \core_communication\api::remove_members_from_room
470 * @covers \core_communication\task\remove_members_from_room::execute
471 * @covers \core_communication\task\remove_members_from_room::queue
473 public function test_users_memerbship_toggles_when_disabling_and_reenabling_instance(): void {
474 global $CFG;
475 require_once($CFG->dirroot . '/lib/enrollib.php');
477 // Sample data.
478 $roomname = 'Samplematrixroom';
479 $provider = 'communication_matrix';
480 $course = $this->get_course($roomname, $provider);
481 $user = $this->get_user();
483 // Run room tasks.
484 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
486 // Enrol the user in the course.
487 $enrol = enrol_get_plugin('manual');
488 $enrolinstances = enrol_get_instances($course->id, true);
489 $instance = reset($enrolinstances);
490 $enrol->enrol_user($instance, $user->id);
492 // Run the user tasks.
493 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
495 $communicationprocessor = processor::load_by_instance(
496 'core_course',
497 'coursecommunication',
498 $course->id
500 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
501 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
502 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
504 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
505 // Check our Matrix user id has room membership.
506 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
507 // Update enrolment communication when updating instance to disabled.
508 $enrol->update_communication($instance->id, 'remove', $course->id);
509 // Run the user tasks.
510 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
511 // Check our Matrix user id no longer has membership.
512 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
513 // Update enrolment communication when updating instance to enabled.
514 $enrol->update_communication($instance->id, 'add', $course->id);
515 // Run the user tasks.
516 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
517 // Check our Matrix user id no longer has membership.
518 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
522 * Test enrolled users in a course lose access to a room when the provider is disabled.
524 * @covers \core_communication\api::remove_members_from_room
525 * @covers \core_communication\task\remove_members_from_room::execute
526 * @covers \core_communication\task\remove_members_from_room::queue
528 public function test_users_removed_from_room_when_disabling_provider(): void {
529 global $CFG;
530 require_once($CFG->dirroot . '/lib/enrollib.php');
531 require_once($CFG->dirroot . '/course/lib.php');
533 // Sample data.
534 $roomname = 'Samplematrixroom';
535 $provider = 'communication_matrix';
536 $course = $this->get_course($roomname, $provider);
537 $user = $this->get_user();
539 // Run room tasks.
540 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
542 // Enrol the user in the course.
543 $enrol = enrol_get_plugin('manual');
544 $enrolinstances = enrol_get_instances($course->id, true);
545 $instance = reset($enrolinstances);
546 $enrol->enrol_user($instance, $user->id);
548 // Run the user tasks.
549 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
551 $communicationprocessor = processor::load_by_instance(
552 'core_course',
553 'coursecommunication',
554 $course->id
556 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
557 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
558 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
560 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
561 // Check our Matrix user id has room membership.
562 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
563 // Disable communication provider.
564 $course->selectedcommunication = 'none';
565 update_course($course);
566 // Run the user tasks.
567 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
568 // Check our Matrix user id no longer has membership.
569 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
573 * Test enrolled users in a course lose access to a room when their user account is suspended.
575 * @covers \core_communication\api::remove_members_from_room
576 * @covers \core_communication\task\remove_members_from_room::execute
577 * @covers \core_communication\task\remove_members_from_room::queue
579 public function test_users_removed_from_room_when_suspending_user(): void {
580 global $CFG;
581 require_once($CFG->dirroot . '/lib/enrollib.php');
583 // Sample data.
584 $roomname = 'Samplematrixroom';
585 $provider = 'communication_matrix';
586 $course = $this->get_course($roomname, $provider);
587 $user = $this->get_user();
589 // Run room tasks.
590 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
592 // Enrol the user in the course.
593 $enrol = enrol_get_plugin('manual');
594 $enrolinstances = enrol_get_instances($course->id, true);
595 $instance = reset($enrolinstances);
596 $enrol->enrol_user($instance, $user->id);
598 // Run the user tasks.
599 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
601 $communicationprocessor = processor::load_by_instance(
602 'core_course',
603 'coursecommunication',
604 $course->id
606 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
607 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
608 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
610 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
611 // Check our Matrix user id has room membership.
612 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
613 // Suspend user.
614 $user->suspended = 1;
615 user_update_user($user, false, false);
616 // Run the user tasks.
617 $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
618 // Check our Matrix user id no longer has membership.
619 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
623 * Test enrolled users in a course lose access to a room when their user account is deleted.
625 * @covers \core_communication\api::remove_members_from_room
626 * @covers \core_communication\task\remove_members_from_room::execute
627 * @covers \core_communication\task\remove_members_from_room::queue
629 public function test_users_removed_from_room_when_deleting_user(): void {
630 global $CFG;
631 require_once($CFG->dirroot . '/lib/enrollib.php');
633 // Sample data.
634 $roomname = 'Samplematrixroom';
635 $provider = 'communication_matrix';
636 $course = $this->get_course($roomname, $provider);
637 $user = $this->get_user();
639 // Run room tasks.
640 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
642 // Enrol the user in the course.
643 $enrol = enrol_get_plugin('manual');
644 $enrolinstances = enrol_get_instances($course->id, true);
645 $instance = reset($enrolinstances);
646 $enrol->enrol_user($instance, $user->id);
648 // Run the user tasks.
649 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
651 $communicationprocessor = processor::load_by_instance(
652 'core_course',
653 'coursecommunication',
654 $course->id
656 $matrixrooms = new matrix_rooms($communicationprocessor->get_id());
657 $eventmanager = new matrix_events_manager($matrixrooms->get_matrix_room_id());
658 $matrixhomeserverurl = $eventmanager->matrixhomeserverurl;
660 $matrixuserid = matrix_user_manager::get_matrixid_from_moodle($user->id, $matrixhomeserverurl);
661 // Check our Matrix user id has room membership.
662 $this->assertTrue($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
663 // Delete user.
664 delete_user($user);
665 // Run the user tasks.
666 // $this->runAdhocTasks('\core_communication\task\remove_members_from_room');
667 // Check our Matrix user id no longer has membership.
668 $this->assertFalse($communicationprocessor->get_room_provider()->check_room_membership($matrixuserid));
672 * Test create instance user mapping.
674 * @covers \core_communication\processor::create_instance_user_mapping
675 * @covers \core_communication\processor::mark_users_as_synced
676 * @covers \core_communication\processor::get_instance_userids
678 public function test_create_instance_user_mapping(): void {
679 $this->resetAfterTest();
681 global $DB;
682 $course = $this->get_course('Sampleroom', 'none');
683 $userid = $this->get_user()->id;
685 // Sample data.
686 $communicationroomname = 'Sampleroom';
687 $selectedcommunication = 'communication_matrix';
688 $component = 'core_course';
689 $instancetype = 'coursecommunication';
691 // First test the adding members to a room.
692 $communication = \core_communication\api::load_by_instance(
693 'core_course',
694 'coursecommunication',
695 $course->id
697 $communication->create_and_configure_room($selectedcommunication, $communicationroomname);
698 $communication->add_members_to_room([$userid]);
700 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
701 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
703 // Test against the object.
704 $communicationprocessor = processor::load_by_instance(
705 $component,
706 $instancetype,
707 $course->id
710 // Test against the database.
711 $communicationuserrecord = $DB->get_record('communication_user', [
712 'commid' => $communicationprocessor->get_id(),
713 'userid' => $userid
716 $this->assertEquals($communicationuserrecord->userid, $userid);
717 $this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
721 * Test update instance user mapping.
723 * @covers \core_communication\processor::create_instance_user_mapping
724 * @covers \core_communication\processor::mark_users_as_synced
725 * @covers \core_communication\processor::get_instance_userids
726 * @covers \core_communication\processor::delete_instance_user_mapping
728 public function test_update_instance_user_mapping(): void {
729 $this->resetAfterTest();
731 global $DB;
732 $course = $this->get_course();
733 $userid = $this->get_user()->id;
735 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
737 // Sample data.
738 $communicationroomname = 'Sampleroom';
739 $selectedcommunication = 'communication_matrix';
740 $component = 'core_course';
741 $instancetype = 'coursecommunication';
743 $communication = \core_communication\api::load_by_instance(
744 'core_course',
745 'coursecommunication',
746 $course->id
748 $communication->update_room($selectedcommunication, $communicationroomname);
749 $communication->add_members_to_room([$userid]);
751 $this->runAdhocTasks('\core_communication\task\update_room_task');
752 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
754 // Test against the object.
755 $communicationprocessor = processor::load_by_instance(
756 $component,
757 $instancetype,
758 $course->id
761 // Test against the database.
762 $communicationuserrecord = $DB->get_record('communication_user', [
763 'commid' => $communicationprocessor->get_id(),
764 'userid' => $userid
767 $this->assertEquals($communicationuserrecord->userid, $userid);
768 $this->assertEquals($communicationuserrecord->commid, $communicationprocessor->get_id());
770 // Now add again.
771 $communicationprocessor->delete_instance_user_mapping([$userid]);
773 // Test against the database.
774 $communicationuserrecord = $DB->get_record('communication_user', [
775 'commid' => $communicationprocessor->get_id(),
776 'userid' => $userid
779 $this->assertEmpty($communicationuserrecord);
783 * Test delete instance user mapping.
785 * @covers \core_communication\processor::create_instance_user_mapping
786 * @covers \core_communication\processor::mark_users_as_synced
787 * @covers \core_communication\processor::get_instance_userids
788 * @covers \core_communication\processor::delete_instance_user_mapping
790 public function test_delete_instance_user_mapping(): void {
791 $this->resetAfterTest();
793 global $DB;
794 $course = $this->get_course('Sampleroom', 'none');
795 $userid = $this->get_user()->id;
797 // Sample data.
798 $communicationroomname = 'Sampleroom';
799 $selectedcommunication = 'communication_matrix';
800 $component = 'core_course';
801 $instancetype = 'coursecommunication';
803 // First test the adding members to a room.
804 $communication = \core_communication\api::load_by_instance(
805 'core_course',
806 'coursecommunication',
807 $course->id
809 $communication->create_and_configure_room($selectedcommunication, $communicationroomname);
810 $communication->add_members_to_room([$userid]);
812 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
813 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
815 // Test against the object.
816 $communicationprocessor = processor::load_by_instance(
817 $component,
818 $instancetype,
819 $course->id
822 $this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
824 // Delete the user mapping.
825 $communicationprocessor->delete_instance_user_mapping([$userid]);
827 $this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
829 // Test against the database.
830 $communicationuserrecord = $DB->get_record('communication_user', [
831 'commid' => $communicationprocessor->get_id(),
832 'userid' => $userid
835 $this->assertEmpty($communicationuserrecord);
839 * Test delete user mappings for instance.
841 * @covers \core_communication\processor::create_instance_user_mapping
842 * @covers \core_communication\processor::mark_users_as_synced
843 * @covers \core_communication\processor::get_instance_userids
844 * @covers \core_communication\processor::delete_user_mappings_for_instance
846 public function test_delete_user_mappings_for_instance(): void {
847 $this->resetAfterTest();
849 global $DB;
850 $course = $this->get_course('Sampleroom', 'none');
851 $userid = $this->get_user()->id;
853 // Sample data.
854 $communicationroomname = 'Sampleroom';
855 $selectedcommunication = 'communication_matrix';
856 $component = 'core_course';
857 $instancetype = 'coursecommunication';
859 // First test the adding members to a room.
860 $communication = \core_communication\api::load_by_instance(
861 'core_course',
862 'coursecommunication',
863 $course->id
865 $communication->create_and_configure_room($selectedcommunication, $communicationroomname);
866 $communication->add_members_to_room([$userid]);
868 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
869 $this->runAdhocTasks('\core_communication\task\add_members_to_room_task');
871 // Test against the object.
872 $communicationprocessor = processor::load_by_instance(
873 $component,
874 $instancetype,
875 $course->id
878 $this->assertEquals([$userid], $communicationprocessor->get_all_userids_for_instance());
880 // Delete the user mapping.
881 $communicationprocessor->delete_user_mappings_for_instance();
883 $this->assertEmpty($communicationprocessor->get_all_userids_for_instance());
885 // Test against the database.
886 $communicationuserrecord = $DB->get_record('communication_user', [
887 'commid' => $communicationprocessor->get_id(),
888 'userid' => $userid
891 $this->assertEmpty($communicationuserrecord);
895 * Test status notifications of a communication room are generated correctly.
897 * @covers ::show_communication_room_status_notification
899 public function test_show_communication_room_status_notification(): void {
900 $course = $this->get_course();
902 // Get communication api object.
903 $communication = \core_communication\api::load_by_instance(
904 'core_course',
905 'coursecommunication',
906 $course->id
909 // Room should be in 'pending' state before the task is run and show a notification.
910 $communication->show_communication_room_status_notification();
911 $notifications = \core\notification::fetch();
912 $this->assertStringContainsString('Your Matrix room will be ready soon.', $notifications[0]->get_message());
914 // Run the task.
915 $this->runAdhocTasks('\core_communication\task\create_and_configure_room_task');
917 // Get updated communication api after room configuration.
918 $communication = \core_communication\api::load_by_instance(
919 'core_course',
920 'coursecommunication',
921 $course->id
924 // Check the room is now in 'ready' state and show a notification.
925 $communication->show_communication_room_status_notification();
926 $notifications = \core\notification::fetch();
927 $this->assertStringContainsString('Your Matrix room is ready!', $notifications[0]->get_message());
931 * Test set provider data from handler.
933 * @covers ::set_data
935 public function test_set_provider_data(): void {
936 $this->resetAfterTest();
937 $course = $this->get_course();
938 $communication = \core_communication\api::load_by_instance(
939 'core_course',
940 'coursecommunication',
941 $course->id
944 // Sample data.
945 $roomname = 'Sampleroom';
946 $provider = 'communication_matrix';
948 // Set the data.
949 $communication->set_data($course);
951 // Test the set data.
952 $this->assertEquals($roomname, $course->communicationroomname);
953 $this->assertEquals($provider, $course->selectedcommunication);