Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 core_communication\task\add_members_to_room_task;
20
use core_communication\task\create_and_configure_room_task;
21
use communication_matrix\matrix_test_helper_trait;
22
use core_communication\task\synchronise_provider_task;
23
use core_communication\task\synchronise_providers_task;
24
use core_communication\task\remove_members_from_room;
25
use core_communication\task\update_room_task;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once(__DIR__ . '/../provider/matrix/tests/matrix_test_helper_trait.php');
30
require_once(__DIR__ . '/communication_test_helper_trait.php');
31
 
32
/**
33
 * Class api_test to test the communication public api and its associated methods.
34
 *
35
 * @package    core_communication
36
 * @category   test
37
 * @copyright  2023 Safat Shahin <safat.shahin@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 * @covers \core_communication\api
40
 */
1441 ariadna 41
final class api_test extends \advanced_testcase {
1 efrain 42
    use matrix_test_helper_trait;
43
    use communication_test_helper_trait;
44
 
45
    public function setUp(): void {
46
        parent::setUp();
47
        $this->resetAfterTest();
48
        $this->setup_communication_configs();
49
        $this->initialise_mock_server();
50
    }
51
 
52
    /**
53
     * Test set data to the instance.
54
     */
55
    public function test_set_data(): void {
56
        $course = $this->get_course();
57
 
58
        $communication = \core_communication\api::load_by_instance(
59
            context: \core\context\course::instance($course->id),
60
            component: 'core_course',
61
            instancetype: 'coursecommunication',
62
            instanceid: $course->id,
63
        );
64
 
65
        // Sample data.
66
        $roomname = 'Sampleroom';
67
        $provider = 'communication_matrix';
68
 
69
        // Set the data.
70
        $communication->set_data($course);
71
 
1441 ariadna 72
        $roomnameidentifier = $communication->get_provider() . 'roomname';
11 efrain 73
 
1 efrain 74
        // Test the set data.
1441 ariadna 75
        $this->assertEquals($roomname, $course->$roomnameidentifier);
1 efrain 76
        $this->assertEquals($provider, $course->selectedcommunication);
77
    }
78
 
79
    /**
80
     * Test get_current_communication_provider method.
81
     */
82
    public function test_get_provider(): void {
83
        $course = $this->get_course();
84
 
85
        $communication = \core_communication\api::load_by_instance(
86
            context: \core\context\course::instance($course->id),
87
            component: 'core_course',
88
            instancetype: 'coursecommunication',
89
            instanceid: $course->id,
90
        );
91
 
92
        $this->assertEquals('communication_matrix', $communication->get_provider());
93
    }
94
 
95
    /**
96
     * Test set_avatar method.
97
     */
98
    public function test_set_avatar(): void {
99
        global $CFG;
100
        $this->setAdminUser();
101
        $course = $this->get_course('Sampleroom', 'none');
102
 
103
        // Sample data.
104
        $communicationroomname = 'Sampleroom';
105
        $selectedcommunication = 'communication_matrix';
106
 
107
        $avatar = $this->create_communication_file(
108
            'moodle_logo.jpg',
109
            'moodle_logo.jpg',
110
        );
111
 
112
        // Create the room, settingthe avatar.
113
        $communication = \core_communication\api::load_by_instance(
114
            context: \core\context\course::instance($course->id),
115
            component: 'core_course',
116
            instancetype: 'coursecommunication',
117
            instanceid: $course->id,
118
            provider: $selectedcommunication,
119
        );
120
 
121
        $communication->create_and_configure_room($communicationroomname, $avatar);
122
 
123
        // Reload the communication processor.
124
        $communicationprocessor = processor::load_by_instance(
125
            context: \core\context\course::instance($course->id),
126
            component: 'core_course',
127
            instancetype: 'coursecommunication',
128
            instanceid: $course->id,
129
        );
130
 
131
        // Compare result.
132
        $this->assertEquals(
133
            $avatar->get_contenthash(),
134
            $communicationprocessor->get_avatar()->get_contenthash(),
135
        );
136
    }
137
 
138
    /**
139
     * Test the create_and_configure_room method to add/create tasks.
140
     */
141
    public function test_create_and_configure_room(): void {
142
        // Get the course by disabling communication so that we can create it manually calling the api.
143
        $course = $this->get_course('Sampleroom', 'none');
144
 
145
        // Sample data.
146
        $communicationroomname = 'Sampleroom';
147
        $selectedcommunication = 'communication_matrix';
148
 
149
        $communication = \core_communication\api::load_by_instance(
150
            context: \core\context\course::instance($course->id),
151
            component: 'core_course',
152
            instancetype: 'coursecommunication',
153
            instanceid: $course->id,
154
            provider: $selectedcommunication,
155
        );
156
        $communication->create_and_configure_room($communicationroomname);
157
 
158
        // Test the tasks added.
159
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task');
160
        $this->assertCount(1, $adhoctask);
161
 
162
        $adhoctask = reset($adhoctask);
163
        $this->assertInstanceOf('\\core_communication\\task\\create_and_configure_room_task', $adhoctask);
164
 
165
        // Test the communication record exists.
166
        $communicationprocessor = processor::load_by_instance(
167
            context: \core\context\course::instance($course->id),
168
            component: 'core_course',
169
            instancetype: 'coursecommunication',
170
            instanceid: $course->id,
171
        );
172
 
173
        $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name());
174
        $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider());
175
    }
176
 
177
    /**
178
     * Test the create_and_configure_room method to add/create tasks when no communication provider selected.
179
     */
180
    public function test_create_and_configure_room_without_communication_provider_selected(): void {
181
        // Get the course by disabling communication so that we can create it manually calling the api.
182
        $course = $this->get_course('Sampleroom', 'none');
183
 
184
        // Test the tasks added.
185
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\create_and_configure_room_task');
186
        $this->assertCount(0, $adhoctask);
187
 
188
        // Test the communication record exists.
189
        $communicationprocessor = processor::load_by_instance(
190
            context: \core\context\course::instance($course->id),
191
            component: 'core_course',
192
            instancetype: 'coursecommunication',
193
            instanceid: $course->id,
194
        );
195
 
196
        $this->assertNull($communicationprocessor);
197
    }
198
 
199
    /**
200
     * Test update operation.
201
     */
202
    public function test_update_room(): void {
203
        $course = $this->get_course();
204
 
205
        // Sample data.
206
        $communicationroomname = 'Sampleroomupdated';
207
        $selectedcommunication = 'communication_matrix';
208
 
209
        $communication = \core_communication\api::load_by_instance(
210
            context: \core\context\course::instance($course->id),
211
            component: 'core_course',
212
            instancetype: 'coursecommunication',
213
            instanceid: $course->id,
214
        );
215
        $communication->update_room(processor::PROVIDER_ACTIVE, $communicationroomname);
216
 
217
        // Test the communication record exists.
218
        $communicationprocessor = processor::load_by_instance(
219
            context: \core\context\course::instance($course->id),
220
            component: 'core_course',
221
            instancetype: 'coursecommunication',
222
            instanceid: $course->id,
223
        );
224
 
225
        $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name());
226
        $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider());
227
        $this->assertTrue($communicationprocessor->is_instance_active());
228
 
229
        $communication->update_room(processor::PROVIDER_INACTIVE, $communicationroomname);
230
 
231
        // Test updating active state.
232
        $communicationprocessor = processor::load_by_instance(
233
            context: \core\context\course::instance($course->id),
234
            component: 'core_course',
235
            instancetype: 'coursecommunication',
236
            instanceid: $course->id,
237
            provider: $selectedcommunication,
238
        );
239
 
240
        $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name());
241
        $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider());
242
        $this->assertFalse($communicationprocessor->is_instance_active());
243
    }
244
 
245
    /**
246
     * Test delete operation.
247
     */
248
    public function test_delete_room(): void {
249
        $course = $this->get_course();
250
 
251
        // Sample data.
252
        $communicationroomname = 'Sampleroom';
253
        $selectedcommunication = 'communication_matrix';
254
 
255
        // Test the communication record exists.
256
        $communicationprocessor = processor::load_by_instance(
257
            context: \core\context\course::instance($course->id),
258
            component: 'core_course',
259
            instancetype: 'coursecommunication',
260
            instanceid: $course->id,
261
        );
262
 
263
        $this->assertEquals($communicationroomname, $communicationprocessor->get_room_name());
264
        $this->assertEquals($selectedcommunication, $communicationprocessor->get_provider());
265
 
266
        $communication = \core_communication\api::load_by_instance(
267
            context: \core\context\course::instance($course->id),
268
            component: 'core_course',
269
            instancetype: 'coursecommunication',
270
            instanceid: $course->id,
271
        );
272
        $communication->delete_room();
273
 
274
        // Test the tasks added.
275
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\delete_room_task');
276
        // Should be 2 as one for create, another for update.
277
        $this->assertCount(1, $adhoctask);
278
 
279
        $adhoctask = reset($adhoctask);
280
        $this->assertInstanceOf('\\core_communication\\task\\delete_room_task', $adhoctask);
281
    }
282
 
283
    /**
284
     * Test the adding and removing of members from room.
285
     */
286
    public function test_adding_and_removing_of_room_membership(): void {
287
        $course = $this->get_course();
288
        $userid = $this->get_user()->id;
289
 
290
        // First test the adding members to a room.
291
        $communication = \core_communication\api::load_by_instance(
292
            context: \core\context\course::instance($course->id),
293
            component: 'core_course',
294
            instancetype: 'coursecommunication',
295
            instanceid: $course->id,
296
        );
297
        $communication->add_members_to_room([$userid]);
298
 
299
        // Test the tasks added.
300
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task');
301
        $this->assertCount(1, $adhoctask);
302
 
303
        // Now test the removing members from a room.
304
        $communication->remove_members_from_room([$userid]);
305
 
306
        // Test the tasks added.
307
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room');
308
        $this->assertCount(1, $adhoctask);
309
    }
310
 
311
    /**
312
     * Test the update of room membership with the change user role.
313
     */
314
    public function test_update_room_membership_on_user_role_change(): void {
315
        global $DB;
316
 
317
        // Generate the data.
318
        $user = $this->getDataGenerator()->create_user();
319
        $course = $this->get_course();
320
        $coursecontext = \context_course::instance($course->id);
321
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
322
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
323
 
324
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task');
325
        $this->assertCount(1, $adhoctask);
326
 
327
        $adhoctask = reset($adhoctask);
328
        $this->assertInstanceOf('\\core_communication\\task\\add_members_to_room_task', $adhoctask);
329
 
330
        // Test the tasks added as the role is a teacher.
331
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\update_room_membership_task');
332
        $this->assertCount(1, $adhoctask);
333
 
334
        $adhoctask = reset($adhoctask);
335
        $this->assertInstanceOf('\\core_communication\\task\\update_room_membership_task', $adhoctask);
336
    }
337
 
338
    /**
339
     * Test sync_provider method for the sync of available provider.
340
     */
341
    public function test_sync_provider(): void {
342
        // Generate the data.
343
        $user = $this->getDataGenerator()->create_user();
344
        $course1 = $this->get_course();
345
        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
346
        $course2 = $this->get_course();
347
        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
348
        // Now run the task to add sync providers.
349
        $this->execute_task(synchronise_providers_task::class);
350
        $adhoctask = \core\task\manager::get_adhoc_tasks(synchronise_provider_task::class);
351
        $this->assertCount(2, $adhoctask);
352
    }
353
 
354
    /**
355
     * Test the removal of all members from the room.
356
     */
357
    public function test_remove_all_members_from_room(): void {
358
        $course = $this->get_course();
359
        $userid = $this->get_user()->id;
360
        $communication = \core_communication\api::load_by_instance(
361
            context: \core\context\course::instance($course->id),
362
            component: 'core_course',
363
            instancetype: 'coursecommunication',
364
            instanceid: $course->id,
365
        );
366
        $communication->add_members_to_room([$userid]);
367
 
368
        // Now test the removing members from a room.
369
        $communication->remove_all_members_from_room();
370
 
371
        // Test the remove members tasks added.
372
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
373
        $this->assertCount(1, $adhoctask);
374
    }
375
 
376
    /**
377
     * Test the configuration of room changes as well as the membership with the change of provider.
378
     */
379
    public function test_configure_room_and_membership_by_provider(): void {
380
        global $DB;
381
 
382
        $course = $this->get_course('Sampleroom', 'none');
383
        $userid = $this->get_user()->id;
384
        $provider = 'communication_matrix';
385
 
386
        $communication = \core_communication\api::load_by_instance(
387
            context: \core\context\course::instance($course->id),
388
            component: 'core_course',
389
            instancetype: 'coursecommunication',
390
            instanceid: $course->id,
391
        );
392
 
393
        $communication->configure_room_and_membership_by_provider(
394
            provider: $provider,
395
            instance: $course,
396
            communicationroomname: $course->fullname,
397
            users: [$userid],
398
        );
399
        $communication->reload();
400
 
401
        // Test that the task to create a room is added.
402
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
403
        $this->assertCount(1, $adhoctask);
404
 
405
        // Test that no update tasks are added.
406
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
407
        $this->assertCount(0, $adhoctask);
408
 
409
        // Test that the task to add members to room is not added, as we are adding the user mapping not the task.
410
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
411
        $this->assertCount(0, $adhoctask);
412
 
413
        // Now delete all the ad-hoc tasks.
414
        $DB->delete_records('task_adhoc');
415
 
416
        // Now disable the provider by setting none.
417
        $communication->configure_room_and_membership_by_provider(
418
            provider: processor::PROVIDER_NONE,
419
            instance: $course,
420
            communicationroomname: $course->fullname,
421
            users: [$userid],
422
        );
423
        $communication->reload();
424
 
425
        // Test that the task to delete a room is added.
426
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
427
        $this->assertCount(1, $adhoctask);
428
 
429
        // Test that the task to remove members from room is added.
430
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
431
        $this->assertCount(1, $adhoctask);
432
 
433
        // Now delete all the ad-hoc tasks.
434
        $DB->delete_records('task_adhoc');
435
 
436
        // Now try to set the same none provider again.
437
        $communication->configure_room_and_membership_by_provider(
438
            provider: processor::PROVIDER_NONE,
439
            instance: $course,
440
            communicationroomname: $course->fullname,
441
            users: [$userid],
442
        );
443
 
444
        // Test that no communicaiton task is added.
445
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
446
        $this->assertCount(0, $adhoctask);
447
 
448
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
449
        $this->assertCount(0, $adhoctask);
450
 
451
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
452
        $this->assertCount(0, $adhoctask);
453
 
454
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
455
        $this->assertCount(0, $adhoctask);
456
 
457
        // Now let's change it back to matrix and test the update task is added.
458
        $communication->configure_room_and_membership_by_provider(
459
            provider: $provider,
460
            instance: $course,
461
            communicationroomname: $course->fullname,
462
            users: [$userid],
463
        );
464
        $communication->reload();
465
 
466
        // Test create task is not added because communication has been created in the past.
467
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
468
        $this->assertCount(0, $adhoctask);
469
 
470
        // Test an update task added.
471
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
472
        $this->assertCount(1, $adhoctask);
473
 
474
        // Test add membership task is added.
475
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
476
        $this->assertCount(1, $adhoctask);
477
 
478
        // Now delete all the ad-hoc tasks.
479
        $DB->delete_records('task_adhoc');
480
 
11 efrain 481
        $course->customlinkurl = $course->customlinkurl ?? 'https://moodle.org';
482
 
1 efrain 483
        // Now change the provider to another one.
484
        $communication->configure_room_and_membership_by_provider(
485
            provider: 'communication_customlink',
486
            instance: $course,
487
            communicationroomname: $course->fullname,
488
            users: [$userid],
489
        );
490
        $communication->reload();
491
 
492
        // Remove membership and update room task for the previous provider.
493
        // Create room task for new one.
494
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
495
        $this->assertCount(1, $adhoctask);
496
 
497
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
498
        $this->assertCount(1, $adhoctask);
499
 
500
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
501
        $this->assertCount(1, $adhoctask);
502
 
503
        // Now delete all the ad-hoc tasks.
504
        $DB->delete_records('task_adhoc');
505
 
506
        // Now disable the provider.
507
        $communication->configure_room_and_membership_by_provider(
508
            provider: processor::PROVIDER_NONE,
509
            instance: $course,
510
            communicationroomname: $course->fullname,
511
            users: [$userid],
512
        );
513
        $communication->reload();
514
 
515
        // Should have one update and one remove task.
516
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
517
        $this->assertCount(1, $adhoctask);
518
 
519
        // This provider doesn't have any membership, so no remove task.
520
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
521
        $this->assertCount(0, $adhoctask);
522
 
523
        // Now delete all the ad-hoc tasks.
524
        $DB->delete_records('task_adhoc');
525
 
526
        // Now enable the same provider again.
527
        $communication->configure_room_and_membership_by_provider(
528
            provider: $provider,
529
            instance: $course,
530
            communicationroomname: $course->fullname,
531
            users: [$userid],
532
        );
533
 
534
        // Now it should have one update and one add task.
535
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
536
        $this->assertCount(1, $adhoctask);
537
 
538
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
539
        $this->assertCount(1, $adhoctask);
540
    }
541
}