Merge branch 'MDL-81110-main' of https://github.com/roland04/moodle
[moodle.git] / communication / tests / processor_test.php
blob29c6344726a0acce3608bb4f31e1aa3beba5e52a
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 core_communication;
19 use communication_matrix\matrix_test_helper_trait;
21 defined('MOODLE_INTERNAL') || die();
23 require_once(__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php');
24 require_once(__DIR__ . '/communication_test_helper_trait.php');
26 /**
27 * Class processor_test to test the communication internal api and its associated methods.
29 * @package core_communication
30 * @category test
31 * @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 * @coversDefaultClass \core_communication\processor
35 class processor_test extends \advanced_testcase {
36 use matrix_test_helper_trait;
37 use communication_test_helper_trait;
39 public function setUp(): void {
40 parent::setUp();
41 $this->resetAfterTest();
42 $this->setup_communication_configs();
43 $this->initialise_mock_server();
46 /**
47 * Test create instance.
49 * @covers ::create_instance
50 * @covers ::get_id
51 * @covers ::get_instance
52 * @covers ::get_room_name
54 public function test_create_instance(): void {
55 global $DB;
56 $this->resetAfterTest();
58 // Sample test data.
59 $instanceid = 10;
60 $context = \core\context\system::instance();
61 $component = 'core';
62 $instancetype = 'mycommunication';
63 $selectedcommunication = 'communication_matrix';
64 $communicationroomname = 'communicationroom';
66 $communicationprocessor = processor::create_instance(
67 $context,
68 $selectedcommunication,
69 $instanceid,
70 $component,
71 $instancetype,
72 $communicationroomname,
75 // Now test the record against the database.
76 $communicationrecord = $DB->get_record(
77 'communication',
78 ['instanceid' => $instanceid, 'component' => $component, 'instancetype' => $instancetype]
81 // Test against the set data.
82 $this->assertNotEmpty($communicationrecord);
83 $this->assertEquals($context->id, $communicationrecord->contextid);
84 $this->assertEquals($instanceid, $communicationrecord->instanceid);
85 $this->assertEquals($component, $communicationrecord->component);
86 $this->assertEquals($selectedcommunication, $communicationrecord->provider);
87 $this->assertEquals($communicationroomname, $communicationrecord->roomname);
88 $this->assertEquals($instancetype, $communicationrecord->instancetype);
90 // Test against the object.
91 $this->assertEquals($context->id, $communicationprocessor->get_context_id());
92 $this->assertEquals($context, $communicationprocessor->get_context());
93 $this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
94 $this->assertEquals($communicationprocessor->get_provider(), $communicationrecord->provider);
95 $this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
98 /**
99 * Test update instance.
101 * @covers ::update_instance
102 * @covers ::is_instance_active
103 * @covers ::get_id
104 * @covers ::get_room_name
106 public function test_update_instance(): void {
107 global $DB;
108 $this->resetAfterTest();
110 // Sameple test data.
111 $instanceid = 10;
112 $context = \core\context\system::instance();
113 $component = 'core';
114 $instancetype = 'mycommunication';
115 $selectedcommunication = 'communication_matrix';
116 $communicationroomname = 'communicationroom';
118 $communicationprocessor = processor::create_instance(
119 $context,
120 $selectedcommunication,
121 $instanceid,
122 $component,
123 $instancetype,
124 $communicationroomname,
127 $selectedcommunication = 'none';
128 $communicationroomname = 'communicationroomedited';
130 $communicationprocessor->update_instance(processor::PROVIDER_INACTIVE, $communicationroomname);
132 // Now test the record against the database.
133 $communicationrecord = $DB->get_record('communication', [
134 'instanceid' => $instanceid,
135 'component' => $component,
136 'instancetype' => $instancetype,
139 // Test against the set data.
140 $this->assertNotEmpty($communicationrecord);
141 $this->assertEquals($context->id, $communicationrecord->contextid);
142 $this->assertEquals($instanceid, $communicationrecord->instanceid);
143 $this->assertEquals($component, $communicationrecord->component);
144 $this->assertEquals(processor::PROVIDER_INACTIVE, $communicationrecord->active);
145 $this->assertEquals($communicationroomname, $communicationrecord->roomname);
146 $this->assertEquals($instancetype, $communicationrecord->instancetype);
148 // Test against the object.
149 $this->assertEquals($context->id, $communicationprocessor->get_context_id());
150 $this->assertEquals($context, $communicationprocessor->get_context());
151 $this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
152 $this->assertEquals($communicationprocessor->is_instance_active(), $communicationrecord->active);
153 $this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
157 * Test delete instance.
159 * @covers ::delete_instance
160 * @covers ::create_instance
161 * @covers ::load_by_instance
163 public function test_delete_instance(): void {
164 global $DB;
165 $this->resetAfterTest();
167 // Sameple test data.
168 $instanceid = 10;
169 $context = \core\context\system::instance();
170 $component = 'core';
171 $instancetype = 'mycommunication';
172 $selectedcommunication = 'communication_matrix';
173 $communicationroomname = 'communicationroom';
175 $communicationprocessor = processor::create_instance(
176 $context,
177 $selectedcommunication,
178 $instanceid,
179 $component,
180 $instancetype,
181 $communicationroomname,
184 $communicationprocessor->delete_instance();
186 // Now test the record against the database.
187 $communicationrecord = $DB->get_record('communication', [
188 'instanceid' => $instanceid,
189 'component' => $component,
190 'instancetype' => $instancetype,
193 // Test against the set data.
194 $this->assertEmpty($communicationrecord);
196 // Test against the object.
197 $communicationprocessor = processor::load_by_instance(
198 context: $context,
199 component: $component,
200 instancetype: $instancetype,
201 instanceid: $instanceid,
203 $this->assertNull($communicationprocessor);
207 * Test load by id.
209 * @covers ::load_by_instance
210 * @covers ::get_room_provider
212 public function test_load_by_instance(): void {
213 $this->resetAfterTest();
214 $course = $this->get_course();
215 $context = \core\context\course::instance($course->id);
217 // Test the communication record exists.
218 $communicationprocessor = processor::load_by_instance(
219 context: $context,
220 component: 'core_course',
221 instancetype: 'coursecommunication',
222 instanceid: $course->id,
225 $this->assertNotNull($communicationprocessor);
226 $this->assertInstanceOf(communication_provider::class, $communicationprocessor->get_room_provider());
227 $this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
228 $this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_provider());
229 $this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
233 * Test load by id.
235 * @covers ::load_by_id
236 * @covers ::get_room_provider
237 * @covers ::load_by_instance
239 public function test_load_by_id(): void {
240 $this->resetAfterTest();
241 $course = $this->get_course();
242 $context = \core\context\course::instance($course->id);
244 // Test the communication record exists.
245 $communicationprocessor = processor::load_by_instance(
246 context: $context,
247 component: 'core_course',
248 instancetype: 'coursecommunication',
249 instanceid: $course->id,
252 $communicationprocessorbyid = processor::load_by_id($communicationprocessor->get_id());
254 $this->assertNotNull($communicationprocessorbyid);
255 $this->assertInstanceOf(communication_provider::class, $communicationprocessorbyid->get_room_provider());
256 $this->assertInstanceOf(room_chat_provider::class, $communicationprocessorbyid->get_room_provider());
257 $this->assertInstanceOf(room_user_provider::class, $communicationprocessorbyid->get_room_provider());
258 $this->assertInstanceOf(user_provider::class, $communicationprocessorbyid->get_room_provider());
262 * Test get component.
264 * @covers ::get_component
265 * @covers ::load_by_instance
267 public function test_get_component(): void {
268 $this->resetAfterTest();
269 $course = $this->get_course();
270 $context = \core\context\course::instance($course->id);
272 // Test the communication record exists.
273 $communicationprocessor = processor::load_by_instance(
274 context: $context,
275 component: 'core_course',
276 instancetype: 'coursecommunication',
277 instanceid: $course->id,
280 $this->assertEquals('core_course', $communicationprocessor->get_component());
284 * Test get provider.
286 * @covers ::get_provider
287 * @covers ::load_by_instance
289 public function test_get_provider(): void {
290 $this->resetAfterTest();
291 $course = $this->get_course();
292 $context = \core\context\course::instance($course->id);
294 // Test the communication record exists when fetching the active provider.
295 $communicationprocessor = processor::load_by_instance(
296 context: $context,
297 component: 'core_course',
298 instancetype: 'coursecommunication',
299 instanceid: $course->id,
302 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
304 // Test the communication record exists when specifying the provider.
305 $communicationprocessor = processor::load_by_instance(
306 context: $context,
307 component: 'core_course',
308 instancetype: 'coursecommunication',
309 instanceid: $course->id,
310 provider: 'communication_matrix',
313 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
315 // Test the communication record exists when the provider is not active.
316 $communicationprocessor->update_instance(processor::PROVIDER_INACTIVE);
317 $communicationprocessor = processor::load_by_instance(
318 context: $context,
319 component: 'core_course',
320 instancetype: 'coursecommunication',
321 instanceid: $course->id,
322 provider: 'communication_matrix',
325 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
329 * Test get room name.
331 * @covers ::get_room_name
332 * @covers ::load_by_instance
334 public function test_get_room_name(): void {
335 $this->resetAfterTest();
336 $course = $this->get_course();
337 $context = \core\context\course::instance($course->id);
339 // Test the communication record exists.
340 $communicationprocessor = processor::load_by_instance(
341 context: $context,
342 component: 'core_course',
343 instancetype: 'coursecommunication',
344 instanceid: $course->id,
347 $this->assertEquals('Sampleroom', $communicationprocessor->get_room_name());
351 * Test get room provider.
353 * @covers ::get_room_provider
354 * @covers ::require_room_features
355 * @covers ::supports_room_features
356 * @covers ::load_by_instance
358 public function test_get_room_provider(): void {
359 $this->resetAfterTest();
360 $course = $this->get_course();
361 $context = \core\context\course::instance($course->id);
363 // Test the communication record exists.
364 $communicationprocessor = processor::load_by_instance(
365 context: $context,
366 component: 'core_course',
367 instancetype: 'coursecommunication',
368 instanceid: $course->id,
371 $this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
375 * Test get user provider.
377 * @covers ::get_user_provider
378 * @covers ::require_user_features
379 * @covers ::supports_user_features
380 * @covers ::load_by_instance
382 public function test_get_user_provider(): void {
383 $this->resetAfterTest();
384 $course = $this->get_course();
385 $context = \core\context\course::instance($course->id);
387 // Test the communication record exists.
388 $communicationprocessor = processor::load_by_instance(
389 context: $context,
390 component: 'core_course',
391 instancetype: 'coursecommunication',
392 instanceid: $course->id,
395 $this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
399 * Test get room user provider.
401 * @covers ::get_room_user_provider
402 * @covers ::require_room_features
403 * @covers ::require_room_user_features
404 * @covers ::supports_room_user_features
405 * @covers ::supports_room_features
406 * @covers ::load_by_instance
408 public function test_get_room_user_provider(): void {
409 $this->resetAfterTest();
410 $course = $this->get_course();
411 $context = \core\context\course::instance($course->id);
413 // Test the communication record exists.
414 $communicationprocessor = processor::load_by_instance(
415 context: $context,
416 component: 'core_course',
417 instancetype: 'coursecommunication',
418 instanceid: $course->id,
421 $this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_user_provider());
425 * Test get avatar.
427 * @covers ::get_avatar
428 * @covers ::load_by_instance
429 * @covers ::get_avatar_filename
430 * @covers ::set_avatar_filename
431 * @covers ::set_avatar_synced_flag
433 public function test_get_avatar(): void {
434 $this->resetAfterTest();
435 $this->setAdminUser();
437 global $CFG;
438 $course = $this->get_course('Sampleroom', 'none');
440 // Sample data.
441 $communicationroomname = 'Sampleroom';
442 $selectedcommunication = 'communication_matrix';
443 $avatar = $this->create_communication_file(
444 'moodle_logo.jpg',
445 'moodle_logo.jpg',
448 $communication = \core_communication\api::load_by_instance(
449 context: \core\context\course::instance($course->id),
450 component: 'core_course',
451 instancetype: 'coursecommunication',
452 instanceid: $course->id,
453 provider: $selectedcommunication,
455 $communication->create_and_configure_room($communicationroomname, $avatar);
457 $communicationprocessor = processor::load_by_instance(
458 context: \core\context\course::instance($course->id),
459 component: 'core_course',
460 instancetype: 'coursecommunication',
461 instanceid: $course->id,
464 $avatar = $communicationprocessor->get_avatar();
466 $this->assertNotNull($avatar);
467 $this->assertEquals($avatar->get_component(), 'core_communication');
468 $this->assertEquals($avatar->get_filearea(), 'avatar');
469 $this->assertEquals($avatar->get_itemid(), $communicationprocessor->get_id());
470 $this->assertEquals($avatar->get_filepath(), '/');
471 $this->assertEquals($avatar->get_filearea(), 'avatar');
472 $this->assertEquals($avatar->get_filename(), $communicationprocessor->get_avatar_filename());
474 // Change the avatar file name to something else and check it was set.
475 $communicationprocessor->set_avatar_filename('newname.svg');
477 $communicationprocessor = processor::load_by_instance(
478 context: \core\context\course::instance($course->id),
479 component: 'core_course',
480 instancetype: 'coursecommunication',
481 instanceid: $course->id,
483 $this->assertEquals($communicationprocessor->get_avatar_filename(), 'newname.svg');
487 * Test if the provider is enabled and configured, or disabled.
489 * @covers ::is_provider_available
491 public function test_is_provider_available(): void {
492 $this->resetAfterTest();
493 $communicationprovider = 'communication_matrix';
494 $this->assertTrue(processor::is_provider_available($communicationprovider));
496 // Now test is disabling the plugin returns false.
497 set_config('disabled', 1, $communicationprovider);
498 $this->assertFalse(processor::is_provider_available($communicationprovider));
502 * Test delete flagged user id's return correct users.
504 * @covers ::get_all_delete_flagged_userids
506 public function test_get_all_delete_flagged_userids(): void {
507 $this->resetAfterTest();
509 $course = $this->get_course('Sampleroom', 'none');
510 $user1 = $this->getDataGenerator()->create_user()->id;
511 $user2 = $this->getDataGenerator()->create_user()->id;
513 // Sample data.
514 $communicationroomname = 'Sampleroom';
515 $selectedcommunication = 'communication_matrix';
516 $component = 'core_course';
517 $instancetype = 'coursecommunication';
519 // Load the communication api.
520 $communication = \core_communication\api::load_by_instance(
521 context: \core\context\course::instance($course->id),
522 component: $component,
523 instancetype: $instancetype,
524 instanceid: $course->id,
525 provider: $selectedcommunication,
527 $communication->create_and_configure_room($communicationroomname);
528 $communication->add_members_to_room([$user1, $user2]);
530 // Now remove user1 from the room.
531 $communication->remove_members_from_room([$user1]);
533 // Test against the object.
534 $communicationprocessor = processor::load_by_instance(
535 context: \core\context\course::instance($course->id),
536 component: $component,
537 instancetype: $instancetype,
538 instanceid: $course->id,
541 $this->assertEquals([$user1], $communicationprocessor->get_all_delete_flagged_userids());