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
/**
18
 * BBB Library tests class.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2018 - present, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Laurent David (laurent@call-learning.fr)
24
 */
25
 
26
namespace mod_bigbluebuttonbn;
27
 
28
use calendar_event;
29
use context_module;
30
use mod_bigbluebuttonbn\test\testcase_helper_trait;
31
use mod_bigbluebuttonbn_mod_form;
32
use MoodleQuickForm;
33
use navigation_node;
34
use ReflectionClass;
35
use stdClass;
36
 
37
defined('MOODLE_INTERNAL') || die();
38
global $CFG;
39
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/lib.php');
40
 
41
/**
42
 * BBB Library tests class.
43
 *
44
 * @package   mod_bigbluebuttonbn
45
 * @copyright 2018 - present, Blindside Networks Inc
46
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 * @author    Laurent David (laurent@call-learning.fr)
48
 */
1441 ariadna 49
final class lib_test extends \advanced_testcase {
1 efrain 50
    use testcase_helper_trait;
51
 
52
    /**
53
     * Check support
54
     *
55
     * @covers ::bigbluebuttonbn_supports
56
     */
11 efrain 57
    public function test_bigbluebuttonbn_supports(): void {
1 efrain 58
        $this->resetAfterTest();
59
        $this->assertTrue(bigbluebuttonbn_supports(FEATURE_IDNUMBER));
60
        $this->assertTrue(bigbluebuttonbn_supports(FEATURE_MOD_INTRO));
1441 ariadna 61
        $this->assertTrue(bigbluebuttonbn_supports(FEATURE_GRADE_HAS_GRADE));
1 efrain 62
    }
63
 
64
    /**
65
     * Check add instance
66
     *
67
     * @covers ::bigbluebuttonbn_add_instance
68
     */
11 efrain 69
    public function test_bigbluebuttonbn_add_instance(): void {
1 efrain 70
        $this->resetAfterTest();
71
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
72
        $bbformdata = $this->get_form_data_from_instance($bbactivity);
73
        $id = bigbluebuttonbn_add_instance($bbformdata);
74
        $this->assertNotNull($id);
75
    }
76
 
77
    /**
78
     * Check update instance
79
     *
80
     * @covers ::bigbluebuttonbn_update_instance
81
     */
11 efrain 82
    public function test_bigbluebuttonbn_update_instance(): void {
1 efrain 83
        $this->resetAfterTest();
84
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
85
        $bbformdata = $this->get_form_data_from_instance($bbactivity);
86
        $result = bigbluebuttonbn_update_instance($bbformdata);
87
        $this->assertTrue($result);
88
    }
89
 
90
    /**
91
     * Check delete instance
92
     *
93
     * @covers ::bigbluebuttonbn_delete_instance
94
     */
11 efrain 95
    public function test_bigbluebuttonbn_delete_instance(): void {
1 efrain 96
        $this->resetAfterTest();
97
        $this->initialise_mock_server();
98
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
99
        $result = bigbluebuttonbn_delete_instance($bbactivity->id);
100
        $this->assertTrue($result);
101
    }
102
 
103
    /**
104
     * Check delete instance
105
     *
106
     * @covers ::bigbluebuttonbn_delete_instance
107
     */
11 efrain 108
    public function test_bigbluebuttonbn_delete_instance_with_running_meeting(): void {
1 efrain 109
        $this->resetAfterTest();
110
        $this->initialise_mock_server();
111
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
112
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
113
        $instance = instance::get_from_instanceid($bbactivity->id);
114
        // Run the meeting.
115
        $bbbgenerator->create_meeting([
116
            'instanceid' => $instance->get_instance_id(),
117
            'groupid' => $instance->get_group_id(),
118
        ]);
119
        $meeting = new meeting($instance);
120
        $meeting->update_cache();
121
        $this->assertTrue($meeting->is_running());
122
        $result = bigbluebuttonbn_delete_instance($bbactivity->id);
123
        $this->assertTrue($result);
124
        $meeting->update_cache();
125
        $this->assertFalse($meeting->is_running());
126
    }
127
 
128
    /**
129
     * Check delete instance
130
     *
131
     * @covers ::bigbluebuttonbn_delete_instance
132
     */
11 efrain 133
    public function test_bigbluebuttonbn_delete_instance_with_running_group_meetings(): void {
1 efrain 134
        $this->resetAfterTest();
135
        $this->initialise_mock_server();
136
        $datagenerator = $this->getDataGenerator();
137
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
138
        $course = $this->get_course();
139
        set_coursemodule_groupmode($bbactivitycm->id, VISIBLEGROUPS);
140
 
141
        $groups = [];
142
        foreach (['G1', 'G2'] as $gname) {
143
            $groups[] = $datagenerator->create_group(['courseid' => $course->id, 'name' => $gname]);
144
        }
145
        // Just create a user in one of the group so we check we don't just end meetings for this user...
146
        $user = $datagenerator->create_and_enrol($this->get_course());
147
        $groupids = array_map(function($g) {
148
            return $g->id;
149
        }, $groups);
150
        $datagenerator->create_group_member(['userid' => $user->id, 'groupid' => $groupids[0]]);
151
        $this->setUser($user);
152
 
153
        $groupids[] = 0; // Add no group (id=0) - as an item so it is covered in the loop.
154
        $bbbgenerator = $datagenerator->get_plugin_generator('mod_bigbluebuttonbn');
155
        $globalinstance = instance::get_from_instanceid($bbactivity->id);
156
        $meetings = [];
157
        foreach ($groupids as $groupid) {
158
            $instance = instance::get_group_instance_from_instance($globalinstance, $groupid);
159
            // Run the meetings.
160
            $bbbgenerator->create_meeting([
161
                'instanceid' => $instance->get_instance_id(),
162
                'groupid' => $instance->get_group_id(),
163
            ]);
164
            $meeting = new meeting($instance);
165
            $meeting->update_cache();
166
            $this->assertTrue($meeting->is_running());
167
            $meetings[] = $meeting;
168
        }
169
        $result = bigbluebuttonbn_delete_instance($bbactivity->id);
170
        $this->assertTrue($result);
171
        foreach ($meetings as $meeting) {
172
            $meeting->update_cache();
173
            $this->assertFalse($meeting->is_running());
174
        }
175
    }
176
    /**
177
     * Check user outline page
178
     *
179
     * @covers ::bigbluebuttonbn_user_outline
180
     */
11 efrain 181
    public function test_bigbluebuttonbn_user_outline(): void {
1 efrain 182
        $this->resetAfterTest();
183
 
184
        $generator = $this->getDataGenerator();
185
        $user = $generator->create_user();
186
        $this->setUser($user);
187
 
188
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance($this->get_course(),
189
            ['completion' => 2, 'completionview' => 1]);
190
        $result = bigbluebuttonbn_user_outline($this->get_course(), $user, $bbactivitycm, $bbactivity);
191
        $this->assertEquals((object) ['info' => '', 'time' => 0], $result);
192
 
193
        bigbluebuttonbn_view($bbactivity, $this->get_course(), $bbactivitycm, $bbactivitycontext);
194
        $result = bigbluebuttonbn_user_outline($this->get_course(), $user, $bbactivitycm, $bbactivity);
195
        $this->assertStringContainsString(get_string('report_room_view', 'mod_bigbluebuttonbn'), $result->info);
196
    }
197
 
198
    /**
199
     * Check user completion
200
     *
201
     * @covers ::bigbluebuttonbn_user_complete
202
     */
11 efrain 203
    public function test_bigbluebuttonbn_user_complete(): void {
1 efrain 204
        $this->initialise_mock_server();
205
        $this->resetAfterTest();
206
 
207
        $generator = $this->getDataGenerator();
208
        $user = $generator->create_and_enrol($this->get_course());
209
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance($this->get_course(),
210
            ['completion' => 2, 'completionview' => 1]);
211
        $this->setUser($user);
212
 
213
        // Now create a couple of logs.
214
        bigbluebuttonbn_view($bbactivity, $this->get_course(), $bbactivitycm, $bbactivitycontext);
215
        ob_start();
216
        bigbluebuttonbn_user_complete($this->get_course(), $user, $bbactivitycm, $bbactivity);
217
        $output = ob_get_contents();
218
        ob_end_clean();
219
        $this->assertStringContainsString(get_string('report_room_view', 'mod_bigbluebuttonbn'), $output);
220
    }
221
 
222
    /**
223
     * Check get recent activity
224
     *
225
     * @covers ::bigbluebuttonbn_get_recent_mod_activity
226
     */
11 efrain 227
    public function test_bigbluebuttonbn_get_recent_mod_activity(): void {
1 efrain 228
        $this->initialise_mock_server();
229
        $this->resetAfterTest();
230
 
231
        $generator = $this->getDataGenerator();
232
        $user = $generator->create_and_enrol($this->get_course());
233
        $this->setUser($user);
234
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
235
        // Now create a couple of logs.
236
        $instance = instance::get_from_instanceid($bbactivity->id);
237
        $recordings = $this->create_recordings_for_instance($instance, [['name' => "Pre-Recording 1"]]);
238
        logger::log_meeting_joined_event($instance, 0);
239
        logger::log_meeting_joined_event($instance, 0);
240
        logger::log_recording_played_event($instance, $recordings[0]->id);
241
 
242
        $activities = $this->prepare_for_recent_activity_array(0, $user->id, 0);
243
        $this->assertCount(4, $activities);
244
        $this->assertEquals(
245
            ["Meeting joined", "Meeting joined", "Recording viewed"],
246
            array_values(
247
                array_filter(
248
                    array_map(function($activity) {
249
                        return $activity->eventname ?? "";
250
                    }, $activities),
251
                    function($e) {
252
                        return !empty($e);
253
                    }
254
                )
255
            )
256
        );
257
        $this->assertEquals("Pre-Recording 1", $activities[3]->content); // The recording view event should contain the name
258
        // of the activity.
259
    }
260
 
261
    /**
262
     * Prepare the list of activities as is done in course recent activity
263
     *
264
     * @param int $date
265
     * @param int $user
266
     * @param int $group
267
     * @return array|void
268
     */
269
    protected function prepare_for_recent_activity_array($date, $user, $group) {
270
        // Same algorithm as in cource/recent.php, but stops at the first bbb activity.
271
        $course = $this->get_course();
272
        $modinfo = get_fast_modinfo($course->id);
273
        $sections = array();
274
        $index = 0;
275
        foreach ($modinfo->get_section_info_all() as $i => $section) {
276
            if (!empty($section->uservisible)) {
277
                $sections[$i] = $section;
278
            }
279
        }
280
        foreach ($sections as $sectionnum => $section) {
281
 
282
            $activity = new stdClass();
283
            $activity->type = 'section';
284
            if ($section->section > 0) {
285
                $activity->name = get_section_name($this->get_course(), $section);
286
            } else {
287
                $activity->name = '';
288
            }
289
 
290
            $activity->visible = $section->visible;
291
            $activities[$index++] = $activity;
292
 
293
            if (empty($modinfo->sections[$sectionnum])) {
294
                continue;
295
            }
296
 
297
            foreach ($modinfo->sections[$sectionnum] as $cmid) {
298
                $cm = $modinfo->cms[$cmid];
299
 
300
                if (!$cm->uservisible) {
301
                    continue;
302
                }
303
 
304
                if (!empty($filter) && $cm->modname != $filter) {
305
                    continue;
306
                }
307
 
308
                if (!empty($filtermodid) && $cmid != $filtermodid) {
309
                    continue;
310
                }
311
 
312
                if ($cm->modname == 'bigbluebuttonbn') {
313
                    return bigbluebuttonbn_get_recent_mod_activity($activities,
314
                        $index,
315
                        $date,
316
                        $course->id, $cmid,
317
                        $user,
318
                        $group);
319
                }
320
            }
321
        }
322
 
323
    }
324
 
325
    /**
326
     * Check user recent activity
327
     *
328
     * @covers ::bigbluebuttonbn_print_recent_mod_activity
329
     */
11 efrain 330
    public function test_bigbluebuttonbn_print_recent_mod_activity(): void {
1 efrain 331
        $this->initialise_mock_server();
332
        $this->resetAfterTest();
333
 
334
        $generator = $this->getDataGenerator();
335
        $user = $generator->create_and_enrol($this->get_course());
336
        $this->setUser($user);
337
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
338
        // Now create a couple of logs.
339
        $instance = instance::get_from_instanceid($bbactivity->id);
340
        $recordings = $this->create_recordings_for_instance($instance, [['name' => "Pre-Recording 1"]]);
341
        logger::log_meeting_joined_event($instance, 0);
342
        logger::log_meeting_joined_event($instance, 0);
343
        logger::log_recording_played_event($instance, $recordings[0]->id);
344
 
345
        $activities = $this->prepare_for_recent_activity_array(0, $user->id, 0);
346
        ob_start();
347
        bigbluebuttonbn_print_recent_mod_activity($activities[1], $this->get_course()->id, false, [], false);
348
        $output = ob_get_contents();
349
        ob_end_clean();
350
        $this->assertStringContainsString('Meeting joined', $output);
351
    }
352
 
353
 
354
    /**
355
     * Check recent activity for the course
356
     *
357
     * @covers ::bigbluebuttonbn_print_recent_activity
358
     */
11 efrain 359
    public function test_bigbluebuttonbn_print_recent_activity(): void {
1 efrain 360
        global $CFG;
361
        $this->initialise_mock_server();
362
        $this->resetAfterTest();
363
 
364
        $generator = $this->getDataGenerator();
365
        $user = $generator->create_and_enrol($this->get_course());
366
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
367
        // Now create a couple of logs.
368
        $timestart = time() - HOURSECS;
369
        $instance = instance::get_from_instanceid($bbactivity->id);
370
        $recordings = $this->create_recordings_for_instance($instance, [['name' => "Pre-Recording 1"]]);
371
 
372
        $this->setUser($user); // Important so the logs are set to this user.
373
        logger::log_meeting_joined_event($instance, 0);
374
        logger::log_meeting_joined_event($instance, 0);
375
        logger::log_recording_played_event($instance, $recordings[0]->id);
376
 
377
        $this->setAdminUser();
378
        // Test that everything is displayed.
379
        ob_start();
380
        bigbluebuttonbn_print_recent_activity($this->get_course(), true, $timestart);
381
        $output = ob_get_contents();
382
        ob_end_clean();
383
        $this->assertStringContainsString('Meeting joined', $output);
384
        $this->assertStringContainsString(fullname($user), $output);
385
        // Test that username are displayed in a different format.
386
        $CFG->alternativefullnameformat = 'firstname lastname firstnamephonetic lastnamephonetic middlename alternatename';
387
        $expectedname = "$user->firstname $user->lastname $user->firstnamephonetic "
388
            . "$user->lastnamephonetic $user->middlename $user->alternatename";
389
        ob_start();
390
        bigbluebuttonbn_print_recent_activity($this->get_course(), false, $timestart);
391
        $output = ob_get_contents();
392
        ob_end_clean();
393
        $this->assertStringContainsString('Meeting joined', $output);
394
        $this->assertStringNotContainsString($expectedname, $output);
395
        // Test that nothing is displayed as per timestart.
396
        ob_start();
397
        bigbluebuttonbn_print_recent_activity($this->get_course(), true, time());
398
        $output = ob_get_contents();
399
        ob_end_clean();
400
        $this->assertEmpty($output);
401
    }
402
 
403
    /**
404
     * Check extra capabilities return value
405
     *
406
     * @covers ::bigbluebuttonbn_get_extra_capabilities
407
     */
11 efrain 408
    public function test_bigbluebuttonbn_get_extra_capabilities(): void {
1 efrain 409
        $this->resetAfterTest();
410
        $this->assertEquals(['moodle/site:accessallgroups'], bigbluebuttonbn_get_extra_capabilities());
411
    }
412
 
413
    /**
414
     * Check form definition
415
     *
416
     * @covers ::bigbluebuttonbn_reset_course_form_definition
417
     */
11 efrain 418
    public function test_bigbluebuttonbn_reset_course_form_definition(): void {
1 efrain 419
        global $CFG, $PAGE;
420
        $this->initialise_mock_server();
421
 
422
        $PAGE->set_course($this->get_course());
423
        $this->setAdminUser();
424
        $this->resetAfterTest();
425
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
426
        include_once($CFG->dirroot . '/mod/bigbluebuttonbn/mod_form.php');
427
        $data = new stdClass();
428
        $data->instance = $bbactivity;
429
        $data->id = $bbactivity->id;
430
        $data->course = $bbactivity->course;
431
 
432
        $form = new mod_bigbluebuttonbn_mod_form($data, 1, $bbactivitycm, $this->get_course());
433
        $refclass = new ReflectionClass("mod_bigbluebuttonbn_mod_form");
434
        $formprop = $refclass->getProperty('_form');
435
 
436
        /* @var $mform MoodleQuickForm quickform object definition */
437
        $mform = $formprop->getValue($form);
438
        bigbluebuttonbn_reset_course_form_definition($mform);
439
        $this->assertNotNull($mform->getElement('bigbluebuttonbnheader'));
440
    }
441
 
442
    /**
443
     * Check defaults for form
444
     *
445
     * @covers ::bigbluebuttonbn_reset_course_form_defaults
446
     */
11 efrain 447
    public function test_bigbluebuttonbn_reset_course_form_defaults(): void {
1 efrain 448
        global $CFG;
449
        $this->resetAfterTest();
450
        $results = bigbluebuttonbn_reset_course_form_defaults($this->get_course());
451
        $this->assertEquals([
452
            'reset_bigbluebuttonbn_events' => 0,
453
            'reset_bigbluebuttonbn_tags' => 0,
454
            'reset_bigbluebuttonbn_logs' => 0,
455
            'reset_bigbluebuttonbn_recordings' => 0,
456
        ], $results);
457
    }
458
 
459
    /**
460
     * Reset user data
461
     *
462
     * @covers ::bigbluebuttonbn_reset_userdata
463
     */
11 efrain 464
    public function test_bigbluebuttonbn_reset_userdata(): void {
1 efrain 465
        global $DB;
466
        $this->resetAfterTest();
467
        $data = new stdClass();
468
        $user = $this->getDataGenerator()->create_user();
469
 
470
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
471
        $this->getDataGenerator()->enrol_user($user->id, $this->course->id);
472
        $this->setUser($user);
473
 
474
        logger::log_meeting_joined_event(instance::get_from_instanceid($bbactivity->id), 0);
475
        $data->courseid = $this->get_course()->id;
476
        $data->reset_bigbluebuttonbn_tags = true;
477
        $data->reset_bigbluebuttonbn_logs = true;
478
        $data->course = $bbactivity->course;
479
        // Add and Join.
480
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
481
        $results = bigbluebuttonbn_reset_userdata($data);
482
        $this->assertCount(0, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
483
        $this->assertEquals([
484
            'component' => 'BigBlueButton',
485
            'item' => 'Deleted tags',
486
            'error' => false
487
        ],
488
            $results[0]
489
        );
490
    }
491
 
492
    /**
493
     * Reset user data in a course and checks it does not delete logs elsewhere
494
     *
495
     * @covers ::bigbluebuttonbn_reset_userdata
496
     */
11 efrain 497
    public function test_bigbluebuttonbn_reset_userdata_in_a_course(): void {
1 efrain 498
        global $DB;
499
        $this->resetAfterTest();
500
        $data = new stdClass();
501
        $user = $this->getDataGenerator()->create_user();
502
 
503
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
504
        $this->getDataGenerator()->enrol_user($user->id, $this->course->id);
505
        logger::log_meeting_joined_event(instance::get_from_instanceid($bbactivity->id), 0);
506
 
507
        // Now create another activity in a course and add a couple of logs.
508
        // Aim is to make sure that only logs from one course are deleted.
509
        $course1 = $this->getDataGenerator()->create_course();
510
        list($bbactivitycontext1, $bbactivitycm1, $bbactivity1) = $this->create_instance($course1);
511
        logger::log_meeting_joined_event(instance::get_from_instanceid($bbactivity1->id), 0);
512
 
513
        $data->courseid = $this->get_course()->id;
514
        $data->reset_bigbluebuttonbn_tags = true;
515
        $data->reset_bigbluebuttonbn_logs = true;
516
        $data->course = $bbactivity->course;
517
        // Add and Join.
518
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
519
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity1->id]));
520
        bigbluebuttonbn_reset_userdata($data);
521
        $this->assertCount(0, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
522
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity1->id]));
523
    }
524
 
525
    /**
526
     * Reset user data in a course but do not delete logs
527
     *
528
     * @covers ::bigbluebuttonbn_reset_userdata
529
     */
11 efrain 530
    public function test_bigbluebuttonbn_reset_userdata_logs_not_deleted(): void {
1 efrain 531
        global $DB;
532
        $this->resetAfterTest();
533
        $data = new stdClass();
534
        $user = $this->getDataGenerator()->create_user();
535
 
536
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
537
        $this->getDataGenerator()->enrol_user($user->id, $this->course->id);
538
        $this->setUser($user);
539
        logger::log_meeting_joined_event(instance::get_from_instanceid($bbactivity->id), 0);
540
 
541
        $data->courseid = $this->get_course()->id;
542
        $data->reset_bigbluebuttonbn_logs = false;
543
        $data->course = $bbactivity->course;
544
        // Add and Join.
545
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
546
        bigbluebuttonbn_reset_userdata($data);
547
        $this->assertCount(2, $DB->get_records('bigbluebuttonbn_logs', ['bigbluebuttonbnid' => $bbactivity->id]));
548
    }
549
 
550
    /**
1441 ariadna 551
     * Reset user data and make sure grades are reset.
552
     *
553
     * @covers ::bigbluebuttonbn_reset_userdata
554
     */
555
    public function test_bigbluebuttonbn_reset_userdata_with_grades(): void {
556
        global $DB;
557
        $this->resetAfterTest();
558
        $user = $this->getDataGenerator()->create_user();
559
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance($this->course,
560
            ['grade' => 100]);
561
        $this->getDataGenerator()->enrol_user($user->id, $this->course->id);
562
        $this->setUser($user);
563
 
564
        // Pass a user grade.
565
        $grade = [];
566
        $gradeitem = $DB->get_record('grade_items', [
567
            'courseid' => $this->course->id,
568
            'itemtype' => 'mod',
569
            'itemmodule' => 'bigbluebuttonbn',
570
            'iteminstance' => $bbactivity->id,
571
        ]);
572
        $grade[$user->id] = [
573
            'userid' => $user->id,
574
            'rawgrade' => 75,
575
        ];
576
        grade_update(
577
            source: 'mod/bigbluebuttonbn',
578
            courseid: $this->course->id,
579
            itemtype: 'mod',
580
            itemmodule: 'bigbluebuttonbn',
581
            iteminstance: $bbactivity->id,
582
            itemnumber: 0,
583
            grades: $grade
584
        );
585
        $params = [
586
            'userid' => $user->id,
587
            'itemid' => $gradeitem->id,
588
            'rawgrade' => 75,
589
        ];
590
        $gradegrades = $DB->get_records('grade_grades', $params);
591
        $this->assertCount(1, $gradegrades);
592
 
593
        // Reset user data.
594
        $data = new stdClass();
595
        $data->courseid = $this->course->id;
596
        $data->course = $bbactivity->course;
597
        $data->reset_gradebook_grades = 1;
598
        bigbluebuttonbn_reset_userdata($data);
599
 
600
        // Check grades have been reset.
601
        $gradegrades = $DB->get_records('grade_grades', $params);
602
        $this->assertCount(0, $gradegrades);
603
    }
604
 
605
    /**
1 efrain 606
     * Check course module
607
     *
608
     * @covers ::bigbluebuttonbn_get_coursemodule_info
609
     */
11 efrain 610
    public function test_bigbluebuttonbn_get_coursemodule_info(): void {
1 efrain 611
        $this->resetAfterTest();
612
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
613
        $info = bigbluebuttonbn_get_coursemodule_info($bbactivitycm);
614
        $this->assertEquals($info->name, $bbactivity->name);
615
    }
616
 
617
    /**
618
     * Check update since
619
     *
620
     * @covers ::bigbluebuttonbn_check_updates_since
621
     */
11 efrain 622
    public function test_bigbluebuttonbn_check_updates_since(): void {
1 efrain 623
        $this->resetAfterTest();
624
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
625
        $result = bigbluebuttonbn_check_updates_since($bbactivitycm, 0);
1441 ariadna 626
        $expected = json_encode([
627
            'configuration' => [
628
                'updated' => false,
629
            ],
630
            'contentfiles' => [
631
                'updated' => false,
632
            ],
633
            'introfiles' => [
634
                'updated' => false,
635
            ],
636
            'completion' => [
637
                'updated' => false,
638
            ],
639
            'gradeitems' => [
640
                'updated' => false,
641
            ],
642
            'outcomes' => [
643
                'updated' => false,
644
            ],
645
        ]);
646
        $this->assertEquals($expected, json_encode($result));
1 efrain 647
    }
648
 
649
    /**
650
     * Check event action (calendar)
651
     *
652
     * @covers ::mod_bigbluebuttonbn_core_calendar_provide_event_action
653
     */
11 efrain 654
    public function test_mod_bigbluebuttonbn_core_calendar_provide_event_action(): void {
1 efrain 655
        global $DB;
656
        $this->initialise_mock_server();
657
        $this->resetAfterTest();
658
        $this->setAdminUser();
659
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
660
 
661
        // Standard use case, the meeting start and we want add an action event to join the meeting.
662
        $event = $this->create_action_event($this->get_course(), $bbactivity, logger::EVENT_MEETING_START);
663
        $factory = new \core_calendar\action_factory();
664
        $actionevent = mod_bigbluebuttonbn_core_calendar_provide_event_action($event, $factory);
665
        $this->assertEquals("Join session", $actionevent->get_name());
666
 
667
        // User has already joined the meeting (there is log event EVENT_JOIN already for this user).
668
        $instance = instance::get_from_instanceid($bbactivity->id);
669
        logger::log_meeting_joined_event($instance, 0);
670
 
671
        $bbactivity->closingtime = time() - 1000;
672
        $bbactivity->openingtime = time() - 2000;
673
        $DB->update_record('bigbluebuttonbn', $bbactivity);
674
        $event = $this->create_action_event($this->get_course(), $bbactivity, logger::EVENT_MEETING_START);
675
        $actionevent = mod_bigbluebuttonbn_core_calendar_provide_event_action($event, $factory);
676
        $this->assertNull($actionevent);
677
    }
678
 
679
    /**
680
     * Creates an action event.
681
     *
682
     * @param stdClass $course The course the bigbluebutton activity is in
683
     * @param stdClass $bbbactivity The bigbluebutton activity to create an event for
684
     * @param string $eventtype The event type. eg. ASSIGN_EVENT_TYPE_DUE.
685
     * @return bool|calendar_event
686
     */
687
    private function create_action_event(stdClass $course, stdClass $bbbactivity, string $eventtype) {
688
        $event = new stdClass();
689
        $event->name = 'Calendar event';
690
        $event->modulename = 'bigbluebuttonbn';
691
        $event->courseid = $course->id;
692
        $event->instance = $bbbactivity->id;
693
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
694
        $event->priority = CALENDAR_EVENT_USER_OVERRIDE_PRIORITY;
695
        $event->eventtype = $eventtype;
696
        $event->timestart = time();
697
 
698
        return calendar_event::create($event);
699
    }
700
 
701
    /**
702
     * Test setting navigation admin menu
703
     *
704
     * @covers ::bigbluebuttonbn_extend_settings_navigation
705
     */
11 efrain 706
    public function test_bigbluebuttonbn_extend_settings_navigation_admin(): void {
1 efrain 707
        global $PAGE, $CFG;
708
        $this->resetAfterTest();
709
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
710
        $CFG->bigbluebuttonbn_meetingevents_enabled = true;
711
 
712
        $PAGE->set_cm($bbactivitycm);
713
        $PAGE->set_context(context_module::instance($bbactivitycm->id));
714
        $PAGE->set_url('/mod/bigbluebuttonbn/view.php', ['id' => $bbactivitycm->id]);
715
        $settingnav = $PAGE->settingsnav;
716
 
717
        $this->setAdminUser();
718
        $node = navigation_node::create('testnavigationnode');
719
        bigbluebuttonbn_extend_settings_navigation($settingnav, $node);
720
        $this->assertCount(1, $node->get_children_key_list());
721
    }
722
 
723
    /**
724
     * Check additional setting menu
725
     *
726
     * @covers ::bigbluebuttonbn_extend_settings_navigation
727
     */
11 efrain 728
    public function test_bigbluebuttonbn_extend_settings_navigation_user(): void {
1 efrain 729
        global $PAGE, $CFG;
730
        $this->resetAfterTest();
731
 
732
        $generator = $this->getDataGenerator();
733
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
734
        $user = $generator->create_user();
735
        $this->setUser($user);
736
        list($course, $bbactivitycmuser) = get_course_and_cm_from_instance($bbactivity->id, 'bigbluebuttonbn');
737
 
738
        $CFG->bigbluebuttonbn_meetingevents_enabled = true;
739
 
740
        $PAGE->set_cm($bbactivitycmuser);
741
        $PAGE->set_context(context_module::instance($bbactivitycm->id));
742
        $PAGE->set_url('/mod/bigbluebuttonbn/view.php', ['id' => $bbactivitycm->id]);
743
 
744
        $settingnav = $PAGE->settingsnav;
745
        $node = navigation_node::create('testnavigationnode');
746
        bigbluebuttonbn_extend_settings_navigation($settingnav, $node);
747
        $this->assertCount(0, $node->get_children_key_list());
748
    }
749
 
750
    /**
751
     * Check the visibility on calendar
752
     * @covers ::mod_bigbluebuttonbn_core_calendar_is_event_visible
753
     */
11 efrain 754
    public function test_mod_bigbluebuttonbn_core_calendar_is_event_visible(): void {
1 efrain 755
        global $DB;
756
        $this->resetAfterTest();
757
        $this->setAdminUser();
758
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
759
        $bbactivity->closingtime = time() - 1000;
760
        $bbactivity->openingtime = time() - 2000;
761
        $DB->update_record('bigbluebuttonbn', $bbactivity);
762
        $event = $this->create_action_event($this->get_course(), $bbactivity, logger::EVENT_MEETING_START);
763
        $this->assertFalse(mod_bigbluebuttonbn_core_calendar_is_event_visible($event));
764
        $bbactivity->closingtime = time() + 1000;
765
        $DB->update_record('bigbluebuttonbn', $bbactivity);
766
        $event = $this->create_action_event($this->get_course(), $bbactivity, logger::EVENT_MEETING_START);
767
        $this->assertTrue(mod_bigbluebuttonbn_core_calendar_is_event_visible($event));
768
        $event->instance = 0;
769
        $this->assertFalse(mod_bigbluebuttonbn_core_calendar_is_event_visible($event));
770
    }
771
 
772
}