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. 13 // 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/>. 16 17 namespace core_communication; 18 19 use communication_matrix\matrix_test_helper_trait; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 require_once (__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php'); 24 require_once (__DIR__ . '/communication_test_helper_trait.php'); 25 26 /** 27 * Class processor_test to test the communication internal api and its associated methods. 28 * 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 34 */ 35 class processor_test extends \advanced_testcase { 36 use matrix_test_helper_trait; 37 use communication_test_helper_trait; 38 39 public function setUp(): void { 40 parent::setUp(); 41 $this->resetAfterTest(); 42 $this->setup_communication_configs(); 43 $this->initialise_mock_server(); 44 } 45 46 /** 47 * Test create instance. 48 * 49 * @covers ::create_instance 50 * @covers ::get_id 51 * @covers ::get_instance 52 * @covers ::get_room_name 53 */ 54 public function test_create_instance(): void { 55 global $DB; 56 $this->resetAfterTest(); 57 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'; 65 66 $communicationprocessor = processor::create_instance( 67 $context, 68 $selectedcommunication, 69 $instanceid, 70 $component, 71 $instancetype, 72 $communicationroomname, 73 ); 74 75 // Now test the record against the database. 76 $communicationrecord = $DB->get_record( 77 'communication', 78 ['instanceid' => $instanceid, 'component' => $component, 'instancetype' => $instancetype] 79 ); 80 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); 89 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); 96 } 97 98 /** 99 * Test update instance. 100 * 101 * @covers ::update_instance 102 * @covers ::is_instance_active 103 * @covers ::get_id 104 * @covers ::get_room_name 105 */ 106 public function test_update_instance(): void { 107 global $DB; 108 $this->resetAfterTest(); 109 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'; 117 118 $communicationprocessor = processor::create_instance( 119 $context, 120 $selectedcommunication, 121 $instanceid, 122 $component, 123 $instancetype, 124 $communicationroomname, 125 ); 126 127 $selectedcommunication = 'none'; 128 $communicationroomname = 'communicationroomedited'; 129 130 $communicationprocessor->update_instance(processor::PROVIDER_INACTIVE, $communicationroomname); 131 132 // Now test the record against the database. 133 $communicationrecord = $DB->get_record('communication', [ 134 'instanceid' => $instanceid, 135 'component' => $component, 136 'instancetype' => $instancetype, 137 ]); 138 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); 147 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); 154 } 155 156 /** 157 * Test delete instance. 158 * 159 * @covers ::delete_instance 160 * @covers ::create_instance 161 * @covers ::load_by_instance 162 */ 163 public function test_delete_instance(): void { 164 global $DB; 165 $this->resetAfterTest(); 166 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'; 174 175 $communicationprocessor = processor::create_instance( 176 $context, 177 $selectedcommunication, 178 $instanceid, 179 $component, 180 $instancetype, 181 $communicationroomname, 182 ); 183 184 $communicationprocessor->delete_instance(); 185 186 // Now test the record against the database. 187 $communicationrecord = $DB->get_record('communication', [ 188 'instanceid' => $instanceid, 189 'component' => $component, 190 'instancetype' => $instancetype, 191 ]); 192 193 // Test against the set data. 194 $this->assertEmpty($communicationrecord); 195 196 // Test against the object. 197 $communicationprocessor = processor::load_by_instance( 198 context: $context, 199 component: $component, 200 instancetype: $instancetype, 201 instanceid: $instanceid, 202 ); 203 $this->assertNull($communicationprocessor); 204 } 205 206 /** 207 * Test load by id. 208 * 209 * @covers ::load_by_instance 210 * @covers ::get_room_provider 211 */ 212 public function test_load_by_instance(): void { 213 $this->resetAfterTest(); 214 $course = $this->get_course(); 215 $context = \core\context\course::instance($course->id); 216 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, 223 ); 224 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()); 230 } 231 232 /** 233 * Test load by id. 234 * 235 * @covers ::load_by_id 236 * @covers ::get_room_provider 237 * @covers ::load_by_instance 238 */ 239 public function test_load_by_id(): void { 240 $this->resetAfterTest(); 241 $course = $this->get_course(); 242 $context = \core\context\course::instance($course->id); 243 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, 250 ); 251 252 $communicationprocessorbyid = processor::load_by_id($communicationprocessor->get_id()); 253 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()); 259 } 260 261 /** 262 * Test get component. 263 * 264 * @covers ::get_component 265 * @covers ::load_by_instance 266 */ 267 public function test_get_component(): void { 268 $this->resetAfterTest(); 269 $course = $this->get_course(); 270 $context = \core\context\course::instance($course->id); 271 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, 278 ); 279 280 $this->assertEquals('core_course', $communicationprocessor->get_component()); 281 } 282 283 /** 284 * Test get provider. 285 * 286 * @covers ::get_provider 287 * @covers ::load_by_instance 288 */ 289 public function test_get_provider(): void { 290 $this->resetAfterTest(); 291 $course = $this->get_course(); 292 $context = \core\context\course::instance($course->id); 293 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, 300 ); 301 302 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider()); 303 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', 311 ); 312 313 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider()); 314 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', 323 ); 324 325 $this->assertEquals('communication_matrix', $communicationprocessor->get_provider()); 326 } 327 328 /** 329 * Test get room name. 330 * 331 * @covers ::get_room_name 332 * @covers ::load_by_instance 333 */ 334 public function test_get_room_name(): void { 335 $this->resetAfterTest(); 336 $course = $this->get_course(); 337 $context = \core\context\course::instance($course->id); 338 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, 345 ); 346 347 $this->assertEquals('Sampleroom', $communicationprocessor->get_room_name()); 348 } 349 350 /** 351 * Test get room provider. 352 * 353 * @covers ::get_room_provider 354 * @covers ::require_room_features 355 * @covers ::supports_room_features 356 * @covers ::load_by_instance 357 */ 358 public function test_get_room_provider(): void { 359 $this->resetAfterTest(); 360 $course = $this->get_course(); 361 $context = \core\context\course::instance($course->id); 362 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, 369 ); 370 371 $this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider()); 372 } 373 374 /** 375 * Test get user provider. 376 * 377 * @covers ::get_user_provider 378 * @covers ::require_user_features 379 * @covers ::supports_user_features 380 * @covers ::load_by_instance 381 */ 382 public function test_get_user_provider(): void { 383 $this->resetAfterTest(); 384 $course = $this->get_course(); 385 $context = \core\context\course::instance($course->id); 386 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, 393 ); 394 395 $this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider()); 396 } 397 398 /** 399 * Test get room user provider. 400 * 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 407 */ 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); 412 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, 419 ); 420 421 $this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_user_provider()); 422 } 423 424 /** 425 * Test get avatar. 426 * 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 432 */ 433 public function test_get_avatar(): void { 434 $this->resetAfterTest(); 435 $this->setAdminUser(); 436 437 global $CFG; 438 $course = $this->get_course('Sampleroom', 'none'); 439 440 // Sample data. 441 $communicationroomname = 'Sampleroom'; 442 $selectedcommunication = 'communication_matrix'; 443 $avatar = $this->create_communication_file( 444 'moodle_logo.jpg', 445 'moodle_logo.jpg', 446 ); 447 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, 454 ); 455 $communication->create_and_configure_room($communicationroomname, $avatar); 456 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, 462 ); 463 464 $avatar = $communicationprocessor->get_avatar(); 465 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()); 473 474 // Change the avatar file name to something else and check it was set. 475 $communicationprocessor->set_avatar_filename('newname.svg'); 476 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, 482 ); 483 $this->assertEquals($communicationprocessor->get_avatar_filename(), 'newname.svg'); 484 } 485 486 /** 487 * Test if the provider is enabled and configured, or disabled. 488 * 489 * @covers ::is_provider_available 490 */ 491 public function test_is_provider_available(): void { 492 $this->resetAfterTest(); 493 $communicationprovider = 'communication_matrix'; 494 $this->assertTrue(processor::is_provider_available($communicationprovider)); 495 496 // Now test is disabling the plugin returns false. 497 set_config('disabled', 1, $communicationprovider); 498 $this->assertFalse(processor::is_provider_available($communicationprovider)); 499 } 500 501 /** 502 * Test delete flagged user id's return correct users. 503 * 504 * @covers ::get_all_delete_flagged_userids 505 */ 506 public function test_get_all_delete_flagged_userids(): void { 507 $this->resetAfterTest(); 508 509 $course = $this->get_course('Sampleroom', 'none'); 510 $user1 = $this->getDataGenerator()->create_user()->id; 511 $user2 = $this->getDataGenerator()->create_user()->id; 512 513 // Sample data. 514 $communicationroomname = 'Sampleroom'; 515 $selectedcommunication = 'communication_matrix'; 516 $component = 'core_course'; 517 $instancetype = 'coursecommunication'; 518 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, 526 ); 527 $communication->create_and_configure_room($communicationroomname); 528 $communication->add_members_to_room([$user1, $user2]); 529 530 // Now remove user1 from the room. 531 $communication->remove_members_from_room([$user1]); 532 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, 539 ); 540 541 $this->assertEquals([$user1], $communicationprocessor->get_all_delete_flagged_userids()); 542 } 543 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body