Proyectos de Subversion Moodle

Rev

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