Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 */
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
 
11 efrain 72
        $roomnameidenfier = $communication->get_provider() . 'roomname';
73
 
1 efrain 74
        // Test the set data.
11 efrain 75
        $this->assertEquals($roomname, $course->$roomnameidenfier);
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
     * @covers ::add_members_to_room
287
     * @covers ::remove_members_from_room
288
     */
289
    public function test_adding_and_removing_of_room_membership(): void {
290
        $course = $this->get_course();
291
        $userid = $this->get_user()->id;
292
 
293
        // First test the adding members to a room.
294
        $communication = \core_communication\api::load_by_instance(
295
            context: \core\context\course::instance($course->id),
296
            component: 'core_course',
297
            instancetype: 'coursecommunication',
298
            instanceid: $course->id,
299
        );
300
        $communication->add_members_to_room([$userid]);
301
 
302
        // Test the tasks added.
303
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task');
304
        $this->assertCount(1, $adhoctask);
305
 
306
        // Now test the removing members from a room.
307
        $communication->remove_members_from_room([$userid]);
308
 
309
        // Test the tasks added.
310
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\remove_members_from_room');
311
        $this->assertCount(1, $adhoctask);
312
    }
313
 
314
    /**
315
     * Test the update of room membership with the change user role.
316
     *
317
     * @covers ::update_room_membership
318
     */
319
    public function test_update_room_membership_on_user_role_change(): void {
320
        global $DB;
321
 
322
        // Generate the data.
323
        $user = $this->getDataGenerator()->create_user();
324
        $course = $this->get_course();
325
        $coursecontext = \context_course::instance($course->id);
326
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
327
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
328
 
329
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\add_members_to_room_task');
330
        $this->assertCount(1, $adhoctask);
331
 
332
        $adhoctask = reset($adhoctask);
333
        $this->assertInstanceOf('\\core_communication\\task\\add_members_to_room_task', $adhoctask);
334
 
335
        // Test the tasks added as the role is a teacher.
336
        $adhoctask = \core\task\manager::get_adhoc_tasks('\\core_communication\\task\\update_room_membership_task');
337
        $this->assertCount(1, $adhoctask);
338
 
339
        $adhoctask = reset($adhoctask);
340
        $this->assertInstanceOf('\\core_communication\\task\\update_room_membership_task', $adhoctask);
341
    }
342
 
343
    /**
344
     * Test sync_provider method for the sync of available provider.
345
     *
346
     * @covers ::sync_provider
347
     */
348
    public function test_sync_provider(): void {
349
        // Generate the data.
350
        $user = $this->getDataGenerator()->create_user();
351
        $course1 = $this->get_course();
352
        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
353
        $course2 = $this->get_course();
354
        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
355
        // Now run the task to add sync providers.
356
        $this->execute_task(synchronise_providers_task::class);
357
        $adhoctask = \core\task\manager::get_adhoc_tasks(synchronise_provider_task::class);
358
        $this->assertCount(2, $adhoctask);
359
    }
360
 
361
    /**
362
     * Test the removal of all members from the room.
363
     *
364
     * @covers ::remove_all_members_from_room
365
     */
366
    public function test_remove_all_members_from_room(): void {
367
        $course = $this->get_course();
368
        $userid = $this->get_user()->id;
369
        $communication = \core_communication\api::load_by_instance(
370
            context: \core\context\course::instance($course->id),
371
            component: 'core_course',
372
            instancetype: 'coursecommunication',
373
            instanceid: $course->id,
374
        );
375
        $communication->add_members_to_room([$userid]);
376
 
377
        // Now test the removing members from a room.
378
        $communication->remove_all_members_from_room();
379
 
380
        // Test the remove members tasks added.
381
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
382
        $this->assertCount(1, $adhoctask);
383
    }
384
 
385
    /**
386
     * Test the configuration of room changes as well as the membership with the change of provider.
387
     *
388
     * @covers ::configure_room_and_membership_by_provider
389
     */
390
    public function test_configure_room_and_membership_by_provider(): void {
391
        global $DB;
392
 
393
        $course = $this->get_course('Sampleroom', 'none');
394
        $userid = $this->get_user()->id;
395
        $provider = 'communication_matrix';
396
 
397
        $communication = \core_communication\api::load_by_instance(
398
            context: \core\context\course::instance($course->id),
399
            component: 'core_course',
400
            instancetype: 'coursecommunication',
401
            instanceid: $course->id,
402
        );
403
 
404
        $communication->configure_room_and_membership_by_provider(
405
            provider: $provider,
406
            instance: $course,
407
            communicationroomname: $course->fullname,
408
            users: [$userid],
409
        );
410
        $communication->reload();
411
 
412
        // Test that the task to create a room is added.
413
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
414
        $this->assertCount(1, $adhoctask);
415
 
416
        // Test that no update tasks are added.
417
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
418
        $this->assertCount(0, $adhoctask);
419
 
420
        // Test that the task to add members to room is not added, as we are adding the user mapping not the task.
421
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
422
        $this->assertCount(0, $adhoctask);
423
 
424
        // Now delete all the ad-hoc tasks.
425
        $DB->delete_records('task_adhoc');
426
 
427
        // Now disable the provider by setting none.
428
        $communication->configure_room_and_membership_by_provider(
429
            provider: processor::PROVIDER_NONE,
430
            instance: $course,
431
            communicationroomname: $course->fullname,
432
            users: [$userid],
433
        );
434
        $communication->reload();
435
 
436
        // Test that the task to delete a room is added.
437
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
438
        $this->assertCount(1, $adhoctask);
439
 
440
        // Test that the task to remove members from room is added.
441
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
442
        $this->assertCount(1, $adhoctask);
443
 
444
        // Now delete all the ad-hoc tasks.
445
        $DB->delete_records('task_adhoc');
446
 
447
        // Now try to set the same none provider again.
448
        $communication->configure_room_and_membership_by_provider(
449
            provider: processor::PROVIDER_NONE,
450
            instance: $course,
451
            communicationroomname: $course->fullname,
452
            users: [$userid],
453
        );
454
 
455
        // Test that no communicaiton task is added.
456
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
457
        $this->assertCount(0, $adhoctask);
458
 
459
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
460
        $this->assertCount(0, $adhoctask);
461
 
462
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
463
        $this->assertCount(0, $adhoctask);
464
 
465
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
466
        $this->assertCount(0, $adhoctask);
467
 
468
        // Now let's change it back to matrix and test the update task is added.
469
        $communication->configure_room_and_membership_by_provider(
470
            provider: $provider,
471
            instance: $course,
472
            communicationroomname: $course->fullname,
473
            users: [$userid],
474
        );
475
        $communication->reload();
476
 
477
        // Test create task is not added because communication has been created in the past.
478
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
479
        $this->assertCount(0, $adhoctask);
480
 
481
        // Test an update task added.
482
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
483
        $this->assertCount(1, $adhoctask);
484
 
485
        // Test add membership task is added.
486
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
487
        $this->assertCount(1, $adhoctask);
488
 
489
        // Now delete all the ad-hoc tasks.
490
        $DB->delete_records('task_adhoc');
491
 
11 efrain 492
        $course->customlinkurl = $course->customlinkurl ?? 'https://moodle.org';
493
 
1 efrain 494
        // Now change the provider to another one.
495
        $communication->configure_room_and_membership_by_provider(
496
            provider: 'communication_customlink',
497
            instance: $course,
498
            communicationroomname: $course->fullname,
499
            users: [$userid],
500
        );
501
        $communication->reload();
502
 
503
        // Remove membership and update room task for the previous provider.
504
        // Create room task for new one.
505
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
506
        $this->assertCount(1, $adhoctask);
507
 
508
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
509
        $this->assertCount(1, $adhoctask);
510
 
511
        $adhoctask = \core\task\manager::get_adhoc_tasks(create_and_configure_room_task::class);
512
        $this->assertCount(1, $adhoctask);
513
 
514
        // Now delete all the ad-hoc tasks.
515
        $DB->delete_records('task_adhoc');
516
 
517
        // Now disable the provider.
518
        $communication->configure_room_and_membership_by_provider(
519
            provider: processor::PROVIDER_NONE,
520
            instance: $course,
521
            communicationroomname: $course->fullname,
522
            users: [$userid],
523
        );
524
        $communication->reload();
525
 
526
        // Should have one update and one remove task.
527
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
528
        $this->assertCount(1, $adhoctask);
529
 
530
        // This provider doesn't have any membership, so no remove task.
531
        $adhoctask = \core\task\manager::get_adhoc_tasks(remove_members_from_room::class);
532
        $this->assertCount(0, $adhoctask);
533
 
534
        // Now delete all the ad-hoc tasks.
535
        $DB->delete_records('task_adhoc');
536
 
537
        // Now enable the same provider again.
538
        $communication->configure_room_and_membership_by_provider(
539
            provider: $provider,
540
            instance: $course,
541
            communicationroomname: $course->fullname,
542
            users: [$userid],
543
        );
544
 
545
        // Now it should have one update and one add task.
546
        $adhoctask = \core\task\manager::get_adhoc_tasks(update_room_task::class);
547
        $this->assertCount(1, $adhoctask);
548
 
549
        $adhoctask = \core\task\manager::get_adhoc_tasks(add_members_to_room_task::class);
550
        $this->assertCount(1, $adhoctask);
551
    }
552
}