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 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
 */
1441 ariadna 35
final class processor_test extends \advanced_testcase {
1 efrain 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
1441 ariadna 51
     * @covers ::get_context
52
     * @covers ::get_context_id
53
     * @covers ::get_provider
1 efrain 54
     * @covers ::get_room_name
55
     */
56
    public function test_create_instance(): void {
57
        global $DB;
58
        $this->resetAfterTest();
59
 
60
        // Sample test data.
61
        $instanceid = 10;
62
        $context = \core\context\system::instance();
63
        $component = 'core';
64
        $instancetype = 'mycommunication';
65
        $selectedcommunication = 'communication_matrix';
66
        $communicationroomname = 'communicationroom';
67
 
68
        $communicationprocessor = processor::create_instance(
69
            $context,
70
            $selectedcommunication,
71
            $instanceid,
72
            $component,
73
            $instancetype,
74
            $communicationroomname,
75
        );
76
 
77
        // Now test the record against the database.
78
        $communicationrecord = $DB->get_record(
79
            'communication',
80
            ['instanceid' => $instanceid, 'component' => $component, 'instancetype' => $instancetype]
81
        );
82
 
83
        // Test against the set data.
84
        $this->assertNotEmpty($communicationrecord);
85
        $this->assertEquals($context->id, $communicationrecord->contextid);
86
        $this->assertEquals($instanceid, $communicationrecord->instanceid);
87
        $this->assertEquals($component, $communicationrecord->component);
88
        $this->assertEquals($selectedcommunication, $communicationrecord->provider);
89
        $this->assertEquals($communicationroomname, $communicationrecord->roomname);
90
        $this->assertEquals($instancetype, $communicationrecord->instancetype);
91
 
92
        // Test against the object.
93
        $this->assertEquals($context->id, $communicationprocessor->get_context_id());
94
        $this->assertEquals($context, $communicationprocessor->get_context());
95
        $this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
96
        $this->assertEquals($communicationprocessor->get_provider(), $communicationrecord->provider);
97
        $this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
98
    }
99
 
100
    /**
101
     * Test update instance.
102
     *
103
     * @covers ::update_instance
104
     * @covers ::is_instance_active
105
     * @covers ::get_id
106
     * @covers ::get_room_name
107
     */
108
    public function test_update_instance(): void {
109
        global $DB;
110
        $this->resetAfterTest();
111
 
112
        // Sameple test data.
113
        $instanceid = 10;
114
        $context = \core\context\system::instance();
115
        $component = 'core';
116
        $instancetype = 'mycommunication';
117
        $selectedcommunication = 'communication_matrix';
118
        $communicationroomname = 'communicationroom';
119
 
120
        $communicationprocessor = processor::create_instance(
121
            $context,
122
            $selectedcommunication,
123
            $instanceid,
124
            $component,
125
            $instancetype,
126
            $communicationroomname,
127
        );
128
 
129
        $selectedcommunication = 'none';
130
        $communicationroomname = 'communicationroomedited';
131
 
132
        $communicationprocessor->update_instance(processor::PROVIDER_INACTIVE, $communicationroomname);
133
 
134
        // Now test the record against the database.
135
        $communicationrecord = $DB->get_record('communication', [
136
            'instanceid' => $instanceid,
137
            'component' => $component,
138
            'instancetype' => $instancetype,
139
        ]);
140
 
141
        // Test against the set data.
142
        $this->assertNotEmpty($communicationrecord);
143
        $this->assertEquals($context->id, $communicationrecord->contextid);
144
        $this->assertEquals($instanceid, $communicationrecord->instanceid);
145
        $this->assertEquals($component, $communicationrecord->component);
146
        $this->assertEquals(processor::PROVIDER_INACTIVE, $communicationrecord->active);
147
        $this->assertEquals($communicationroomname, $communicationrecord->roomname);
148
        $this->assertEquals($instancetype, $communicationrecord->instancetype);
149
 
150
        // Test against the object.
151
        $this->assertEquals($context->id, $communicationprocessor->get_context_id());
152
        $this->assertEquals($context, $communicationprocessor->get_context());
153
        $this->assertEquals($communicationprocessor->get_id(), $communicationrecord->id);
154
        $this->assertEquals($communicationprocessor->is_instance_active(), $communicationrecord->active);
155
        $this->assertEquals($communicationprocessor->get_room_name(), $communicationrecord->roomname);
156
    }
157
 
158
    /**
159
     * Test delete instance.
160
     *
161
     * @covers ::delete_instance
162
     * @covers ::create_instance
163
     * @covers ::load_by_instance
164
     */
165
    public function test_delete_instance(): void {
166
        global $DB;
167
        $this->resetAfterTest();
168
 
169
        // Sameple test data.
170
        $instanceid = 10;
171
        $context = \core\context\system::instance();
172
        $component = 'core';
173
        $instancetype = 'mycommunication';
174
        $selectedcommunication = 'communication_matrix';
175
        $communicationroomname = 'communicationroom';
176
 
177
        $communicationprocessor = processor::create_instance(
178
            $context,
179
            $selectedcommunication,
180
            $instanceid,
181
            $component,
182
            $instancetype,
183
            $communicationroomname,
184
        );
185
 
186
        $communicationprocessor->delete_instance();
187
 
188
        // Now test the record against the database.
189
        $communicationrecord = $DB->get_record('communication', [
190
            'instanceid' => $instanceid,
191
            'component' => $component,
192
            'instancetype' => $instancetype,
193
        ]);
194
 
195
        // Test against the set data.
196
        $this->assertEmpty($communicationrecord);
197
 
198
        // Test against the object.
199
        $communicationprocessor = processor::load_by_instance(
200
            context: $context,
201
            component: $component,
202
            instancetype: $instancetype,
203
            instanceid: $instanceid,
204
        );
205
        $this->assertNull($communicationprocessor);
206
    }
207
 
208
    /**
209
     * Test load by id.
210
     *
211
     * @covers ::load_by_instance
212
     * @covers ::get_room_provider
213
     */
214
    public function test_load_by_instance(): void {
215
        $this->resetAfterTest();
216
        $course = $this->get_course();
217
        $context = \core\context\course::instance($course->id);
218
 
219
        // Test the communication record exists.
220
        $communicationprocessor = processor::load_by_instance(
221
            context: $context,
222
            component: 'core_course',
223
            instancetype: 'coursecommunication',
224
            instanceid: $course->id,
225
        );
226
 
227
        $this->assertNotNull($communicationprocessor);
228
        $this->assertInstanceOf(communication_provider::class, $communicationprocessor->get_room_provider());
229
        $this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
230
        $this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_provider());
231
        $this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
232
    }
233
 
234
    /**
235
     * Test load by id.
236
     *
237
     * @covers ::load_by_id
238
     * @covers ::get_room_provider
239
     * @covers ::load_by_instance
240
     */
241
    public function test_load_by_id(): void {
242
        $this->resetAfterTest();
243
        $course = $this->get_course();
244
        $context = \core\context\course::instance($course->id);
245
 
246
        // Test the communication record exists.
247
        $communicationprocessor = processor::load_by_instance(
248
            context: $context,
249
            component: 'core_course',
250
            instancetype: 'coursecommunication',
251
            instanceid: $course->id,
252
        );
253
 
254
        $communicationprocessorbyid = processor::load_by_id($communicationprocessor->get_id());
255
 
256
        $this->assertNotNull($communicationprocessorbyid);
257
        $this->assertInstanceOf(communication_provider::class, $communicationprocessorbyid->get_room_provider());
258
        $this->assertInstanceOf(room_chat_provider::class, $communicationprocessorbyid->get_room_provider());
259
        $this->assertInstanceOf(room_user_provider::class, $communicationprocessorbyid->get_room_provider());
260
        $this->assertInstanceOf(user_provider::class, $communicationprocessorbyid->get_room_provider());
261
    }
262
 
263
    /**
264
     * Test get component.
265
     *
266
     * @covers ::get_component
267
     * @covers ::load_by_instance
268
     */
269
    public function test_get_component(): void {
270
        $this->resetAfterTest();
271
        $course = $this->get_course();
272
        $context = \core\context\course::instance($course->id);
273
 
274
        // Test the communication record exists.
275
        $communicationprocessor = processor::load_by_instance(
276
            context: $context,
277
            component: 'core_course',
278
            instancetype: 'coursecommunication',
279
            instanceid: $course->id,
280
        );
281
 
282
        $this->assertEquals('core_course', $communicationprocessor->get_component());
283
    }
284
 
285
    /**
286
     * Test get provider.
287
     *
288
     * @covers ::get_provider
289
     * @covers ::load_by_instance
290
     */
291
    public function test_get_provider(): void {
292
        $this->resetAfterTest();
293
        $course = $this->get_course();
294
        $context = \core\context\course::instance($course->id);
295
 
296
        // Test the communication record exists when fetching the active provider.
297
        $communicationprocessor = processor::load_by_instance(
298
            context: $context,
299
            component: 'core_course',
300
            instancetype: 'coursecommunication',
301
            instanceid: $course->id,
302
        );
303
 
304
        $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
305
 
306
        // Test the communication record exists when specifying the provider.
307
        $communicationprocessor = processor::load_by_instance(
308
            context: $context,
309
            component: 'core_course',
310
            instancetype: 'coursecommunication',
311
            instanceid: $course->id,
312
            provider: 'communication_matrix',
313
        );
314
 
315
        $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
316
 
317
        // Test the communication record exists when the provider is not active.
318
        $communicationprocessor->update_instance(processor::PROVIDER_INACTIVE);
319
        $communicationprocessor = processor::load_by_instance(
320
            context: $context,
321
            component: 'core_course',
322
            instancetype: 'coursecommunication',
323
            instanceid: $course->id,
324
            provider: 'communication_matrix',
325
        );
326
 
327
        $this->assertEquals('communication_matrix', $communicationprocessor->get_provider());
328
    }
329
 
330
    /**
331
     * Test get room name.
332
     *
333
     * @covers ::get_room_name
334
     * @covers ::load_by_instance
335
     */
336
    public function test_get_room_name(): void {
337
        $this->resetAfterTest();
338
        $course = $this->get_course();
339
        $context = \core\context\course::instance($course->id);
340
 
341
        // Test the communication record exists.
342
        $communicationprocessor = processor::load_by_instance(
343
            context: $context,
344
            component: 'core_course',
345
            instancetype: 'coursecommunication',
346
            instanceid: $course->id,
347
        );
348
 
349
        $this->assertEquals('Sampleroom', $communicationprocessor->get_room_name());
350
    }
351
 
352
    /**
353
     * Test get room provider.
354
     *
355
     * @covers ::get_room_provider
356
     * @covers ::require_room_features
357
     * @covers ::supports_room_features
358
     * @covers ::load_by_instance
359
     */
360
    public function test_get_room_provider(): void {
361
        $this->resetAfterTest();
362
        $course = $this->get_course();
363
        $context = \core\context\course::instance($course->id);
364
 
365
        // Test the communication record exists.
366
        $communicationprocessor = processor::load_by_instance(
367
            context: $context,
368
            component: 'core_course',
369
            instancetype: 'coursecommunication',
370
            instanceid: $course->id,
371
        );
372
 
373
        $this->assertInstanceOf(room_chat_provider::class, $communicationprocessor->get_room_provider());
374
    }
375
 
376
    /**
377
     * Test get user provider.
378
     *
379
     * @covers ::get_user_provider
380
     * @covers ::require_user_features
381
     * @covers ::supports_user_features
382
     * @covers ::load_by_instance
383
     */
384
    public function test_get_user_provider(): void {
385
        $this->resetAfterTest();
386
        $course = $this->get_course();
387
        $context = \core\context\course::instance($course->id);
388
 
389
        // Test the communication record exists.
390
        $communicationprocessor = processor::load_by_instance(
391
            context: $context,
392
            component: 'core_course',
393
            instancetype: 'coursecommunication',
394
            instanceid: $course->id,
395
        );
396
 
397
        $this->assertInstanceOf(user_provider::class, $communicationprocessor->get_room_provider());
398
    }
399
 
400
    /**
401
     * Test get room user provider.
402
     *
403
     * @covers ::get_room_user_provider
404
     * @covers ::require_room_features
405
     * @covers ::require_room_user_features
406
     * @covers ::supports_room_user_features
407
     * @covers ::supports_room_features
408
     * @covers ::load_by_instance
409
     */
410
    public function test_get_room_user_provider(): void {
411
        $this->resetAfterTest();
412
        $course = $this->get_course();
413
        $context = \core\context\course::instance($course->id);
414
 
415
        // Test the communication record exists.
416
        $communicationprocessor = processor::load_by_instance(
417
            context: $context,
418
            component: 'core_course',
419
            instancetype: 'coursecommunication',
420
            instanceid: $course->id,
421
        );
422
 
423
        $this->assertInstanceOf(room_user_provider::class, $communicationprocessor->get_room_user_provider());
424
    }
425
 
426
    /**
427
     * Test get avatar.
428
     *
429
     * @covers ::get_avatar
430
     * @covers ::load_by_instance
431
     * @covers ::get_avatar_filename
432
     * @covers ::set_avatar_filename
433
     * @covers ::set_avatar_synced_flag
434
     */
435
    public function test_get_avatar(): void {
436
        $this->resetAfterTest();
437
        $this->setAdminUser();
438
 
439
        global $CFG;
440
        $course = $this->get_course('Sampleroom', 'none');
441
 
442
        // Sample data.
443
        $communicationroomname = 'Sampleroom';
444
        $selectedcommunication = 'communication_matrix';
445
        $avatar = $this->create_communication_file(
446
            'moodle_logo.jpg',
447
            'moodle_logo.jpg',
448
        );
449
 
450
        $communication = \core_communication\api::load_by_instance(
451
            context: \core\context\course::instance($course->id),
452
            component: 'core_course',
453
            instancetype: 'coursecommunication',
454
            instanceid: $course->id,
455
            provider: $selectedcommunication,
456
        );
457
        $communication->create_and_configure_room($communicationroomname, $avatar);
458
 
459
        $communicationprocessor = processor::load_by_instance(
460
            context: \core\context\course::instance($course->id),
461
            component: 'core_course',
462
            instancetype: 'coursecommunication',
463
            instanceid: $course->id,
464
        );
465
 
466
        $avatar = $communicationprocessor->get_avatar();
467
 
468
        $this->assertNotNull($avatar);
469
        $this->assertEquals($avatar->get_component(), 'core_communication');
470
        $this->assertEquals($avatar->get_filearea(), 'avatar');
471
        $this->assertEquals($avatar->get_itemid(), $communicationprocessor->get_id());
472
        $this->assertEquals($avatar->get_filepath(), '/');
473
        $this->assertEquals($avatar->get_filearea(), 'avatar');
474
        $this->assertEquals($avatar->get_filename(), $communicationprocessor->get_avatar_filename());
475
 
476
        // Change the avatar file name to something else and check it was set.
477
        $communicationprocessor->set_avatar_filename('newname.svg');
478
 
479
        $communicationprocessor = processor::load_by_instance(
480
            context: \core\context\course::instance($course->id),
481
            component: 'core_course',
482
            instancetype: 'coursecommunication',
483
            instanceid: $course->id,
484
        );
485
        $this->assertEquals($communicationprocessor->get_avatar_filename(), 'newname.svg');
486
    }
487
 
488
    /**
489
     * Test if the provider is enabled and configured, or disabled.
490
     *
491
     * @covers ::is_provider_available
492
     */
493
    public function test_is_provider_available(): void {
494
        $this->resetAfterTest();
495
        $communicationprovider = 'communication_matrix';
496
        $this->assertTrue(processor::is_provider_available($communicationprovider));
497
 
498
        // Now test is disabling the plugin returns false.
499
        set_config('disabled', 1, $communicationprovider);
500
        $this->assertFalse(processor::is_provider_available($communicationprovider));
501
    }
502
 
503
    /**
504
     * Test delete flagged user id's return correct users.
505
     *
506
     * @covers ::get_all_delete_flagged_userids
507
     */
508
    public function test_get_all_delete_flagged_userids(): void {
509
        $this->resetAfterTest();
510
 
511
        $course = $this->get_course('Sampleroom', 'none');
512
        $user1 = $this->getDataGenerator()->create_user()->id;
513
        $user2 = $this->getDataGenerator()->create_user()->id;
514
 
515
        // Sample data.
516
        $communicationroomname = 'Sampleroom';
517
        $selectedcommunication = 'communication_matrix';
518
        $component = 'core_course';
519
        $instancetype = 'coursecommunication';
520
 
521
        // Load the communication api.
522
        $communication = \core_communication\api::load_by_instance(
523
            context: \core\context\course::instance($course->id),
524
            component: $component,
525
            instancetype: $instancetype,
526
            instanceid: $course->id,
527
            provider: $selectedcommunication,
528
        );
529
        $communication->create_and_configure_room($communicationroomname);
530
        $communication->add_members_to_room([$user1, $user2]);
531
 
532
        // Now remove user1 from the room.
533
        $communication->remove_members_from_room([$user1]);
534
 
535
        // Test against the object.
536
        $communicationprocessor = processor::load_by_instance(
537
            context: \core\context\course::instance($course->id),
538
            component: $component,
539
            instancetype: $instancetype,
540
            instanceid: $course->id,
541
        );
542
 
543
        $this->assertEquals([$user1], $communicationprocessor->get_all_delete_flagged_userids());
544
    }
545
}