Proyectos de Subversion Moodle

Rev

| 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 trait.
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
namespace mod_bigbluebuttonbn\test;
26
 
27
use context_module;
28
use mod_bigbluebuttonbn\instance;
29
use mod_bigbluebuttonbn\local\config;
30
use mod_bigbluebuttonbn\local\proxy\recording_proxy;
31
use mod_bigbluebuttonbn\meeting;
32
use stdClass;
33
use testing_data_generator;
34
use core\plugininfo\mod;
35
 
36
trait testcase_helper_trait {
37
    /** @var testing_data_generator|null */
38
    protected $generator = null;
39
 
40
    /** @var object|null */
41
    protected $course = null;
42
 
43
    /**
44
     * Convenience function to create an instance of a bigbluebuttonactivty.
45
     *
46
     * @param stdClass|null $course course to add the module to
47
     * @param array $params Array of parameters to pass to the generator
48
     * @param array $options Array of options to pass to the generator
49
     * @return array($context, $cm, $instance) Testable wrapper around the assign class.
50
     */
51
    protected function create_instance(?stdClass $course = null, array $params = [], array $options = []): array {
52
        // Prior to creating the instance, make sure that the BigBlueButton module is enabled.
53
        $modules = \core_plugin_manager::instance()->get_plugins_of_type('mod');
54
        if (!$modules['bigbluebuttonbn']->is_enabled()) {
55
            mod::enable_plugin('bigbluebuttonbn', true);
56
        }
57
 
58
        if (!$course) {
59
            $course = $this->get_course();
60
        }
61
        $params['course'] = $course->id;
62
        $options['visible'] = 1;
63
        $instance = $this->getDataGenerator()->create_module('bigbluebuttonbn', $params, $options);
64
        list($course, $cm) = get_course_and_cm_from_instance($instance, 'bigbluebuttonbn');
65
        $context = context_module::instance($cm->id);
66
 
67
        return [$context, $cm, $instance];
68
    }
69
 
70
    /**
71
     * Get the matching form data
72
     *
73
     * @param stdClass $bbactivity the current bigbluebutton activity
74
     * @param stdClass|null $course the course or null (taken from $this->get_course() if null)
75
     * @return mixed
76
     */
77
    protected function get_form_data_from_instance(stdClass $bbactivity, ?stdClass $course = null): object {
78
        global $USER;
79
 
80
        if (!$course) {
81
            $course = $this->get_course();
82
        }
83
        $this->setAdminUser();
84
        $bbactivitycm = get_coursemodule_from_instance('bigbluebuttonbn', $bbactivity->id);
85
        list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($bbactivitycm, $course);
86
        $this->setUser($USER);
87
        return $data;
88
    }
89
 
90
    /**
91
     * Get or create course if it does not exist
92
     *
93
     * @return stdClass|null
94
     */
95
    protected function get_course(): stdClass {
96
        if (!$this->course) {
97
            $this->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
98
        }
99
        return $this->course;
100
    }
101
 
102
    /**
103
     * Generate a course, several students and several groups
104
     *
105
     * @param stdClass $courserecord
106
     * @param int $numstudents
107
     * @param int $numteachers
108
     * @param int $groupsnum
109
     * @return array
110
     */
111
    protected function setup_course_students_teachers(stdClass $courserecord, int $numstudents, int $numteachers,
112
        int $groupsnum): array {
113
        global $DB;
114
        $generator = $this->getDataGenerator();
115
        $course = $generator->create_course($courserecord);
116
        $groups = [];
117
        for ($i = 0; $i < $groupsnum; $i++) {
118
            $groups[] = $generator->create_group(['courseid' => $course->id]);
119
        }
120
        $generator->create_group(['courseid' => $course->id]);
121
        $generator->create_group(['courseid' => $course->id]);
122
 
123
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
124
 
125
        $students = [];
126
        for ($i = 0; $i < $numstudents; $i++) {
127
            $student = $generator->create_user();
128
            $generator->enrol_user($student->id, $course->id, $roleids['student']);
129
            $groupid = $groups[$i % $groupsnum]->id;
130
            groups_add_member($groupid, $student->id);
131
            $students[] = $student;
132
        }
133
 
134
        $teachers = [];
135
        for ($i = 0; $i < $numteachers; $i++) {
136
            $teacher = $generator->create_user();
137
            $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']);
138
            $groupid = $groups[$i % $groupsnum]->id;
139
            groups_add_member($groupid, $teacher->id);
140
            $teachers[] = $teacher;
141
        }
142
        $bbactivity = $generator->create_module(
143
            'bigbluebuttonbn',
144
            ['course' => $course->id],
145
            ['visible' => true]);
146
 
147
        get_fast_modinfo(0, 0, true);
148
        return [$course, $groups, $students, $teachers, $bbactivity, $roleids];
149
    }
150
 
151
    /**
152
     * This test requires mock server to be present.
153
     */
154
    protected function initialise_mock_server(): void {
155
        if (!defined('TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER')) {
156
            $this->markTestSkipped(
157
                'The TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER constant must be defined to run mod_bigbluebuttonbn tests'
158
            );
159
        }
160
        try {
161
            $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn')->reset_mock();
162
            // Mock server expects a value. By default this field is empty.
163
            set_config('bigbluebuttonbn_shared_secret', config::DEFAULT_SHARED_SECRET);
164
        } catch (\moodle_exception $e) {
165
            $this->markTestSkipped(
166
                'Cannot connect to the mock server for this test. Make sure that TEST_MOD_BIGBLUEBUTTONBN_MOCK_SERVER points
167
                to an active Mock server'
168
            );
169
        }
170
    }
171
 
172
    /**
173
     * Create an return an array of recordings
174
     *
175
     * @param instance $instance
176
     * @param array $recordingdata array of recording information
177
     * @param array $additionalmeetingdata
178
     * @return array
179
     */
180
    protected function create_recordings_for_instance(instance $instance, array $recordingdata = [],
181
        $additionalmeetingdata = []): array {
182
        $recordings = [];
183
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
184
        // Create the meetings on the mock server, so like this we can find the recordings.
185
        $meeting = new meeting($instance);
186
        $meeting->update_cache(); // The meeting has just been created but we need to force fetch info from the server.
187
        if (!$meeting->is_running()) {
188
            $additionalmeetingdata = array_merge([
189
                'instanceid' => $instance->get_instance_id(),
190
                'groupid' => $instance->get_group_id()
191
            ], $additionalmeetingdata);
192
            $bbbgenerator->create_meeting($additionalmeetingdata);
193
        }
194
        foreach ($recordingdata as $rindex => $data) {
195
            $recordings[] = $bbbgenerator->create_recording(
196
                array_merge([
197
                    'bigbluebuttonbnid' => $instance->get_instance_id(),
198
                    'groupid' => $instance->get_group_id()
199
                ], $data)
200
            );
201
        }
202
        return $recordings;
203
    }
204
 
205
    /**
206
     * Create an activity which includes a set of recordings.
207
     *
208
     * @param stdClass $course
209
     * @param int $type
210
     * @param array $recordingdata array of recording information
211
     * @param int $groupid
212
     * @return array
213
     */
214
    protected function create_activity_with_recordings(stdClass $course, int $type, array $recordingdata, int $groupid = 0): array {
215
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
216
 
217
        $activity = $generator->create_instance([
218
            'course' => $course->id,
219
            'type' => $type
220
        ]);
221
 
222
        $instance = instance::get_from_instanceid($activity->id);
223
        $instance->set_group_id($groupid);
224
        $recordings = $this->create_recordings_for_instance($instance, $recordingdata);
225
        return [
226
            'course' => $course,
227
            'activity' => $activity,
228
            'recordings' => $recordings,
229
        ];
230
    }
231
 
232
    /**
233
     * Create a course, users and recording from dataset given in an array form
234
     *
235
     * @param array $dataset
236
     * @return mixed
237
     */
238
    protected function create_from_dataset(array $dataset) {
239
        list('type' => $type, 'recordingsdata' => $recordingsdata, 'groups' => $groups,
240
            'users' => $users) = $dataset;
241
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
242
 
243
        $coursedata = empty($groups) ? [] : ['groupmodeforce' => true, 'groupmode' => $dataset['coursemode'] ?? VISIBLEGROUPS];
244
        $this->course = $this->getDataGenerator()->create_course($coursedata);
245
 
246
        foreach ($users as $userdata) {
247
            $this->getDataGenerator()->create_and_enrol($this->course, $userdata['role'], ['username' => $userdata['username']]);
248
        }
249
 
250
        if ($groups) {
251
            foreach ($groups as $groupname => $students) {
252
                $group = $this->getDataGenerator()->create_group(['name' => $groupname, 'courseid' => $this->course->id]);
253
                foreach ($students as $username) {
254
                    $user = \core_user::get_user_by_username($username);
255
                    $this->getDataGenerator()->create_group_member(['userid' => $user->id, 'groupid' => $group->id]);
256
                }
257
            }
258
        }
259
        $instancesettings = [
260
            'course' => $this->course->id,
261
            'type' => $type,
262
            'name' => 'Example',
263
        ];
264
        if (!empty($dataset['additionalsettings'])) {
265
            $instancesettings = array_merge($instancesettings, $dataset['additionalsettings']);
266
        }
267
        $activity = $plugingenerator->create_instance($instancesettings);
268
        $instance = instance::get_from_instanceid($activity->id);
269
        foreach ($recordingsdata as $groupname => $recordings) {
270
            if ($groups) {
271
                $groupid = groups_get_group_by_name($this->course->id, $groupname);
272
                $instance->set_group_id($groupid);
273
            }
274
            $this->create_recordings_for_instance($instance, $recordings);
275
        }
276
        return $activity->id;
277
    }
278
 
279
    /**
280
     * Create the legacy log entries for this task.
281
     *
282
     * @param instance $instance
283
     * @param int $userid
284
     * @param int $count
285
     * @param bool $importedrecordings
286
     * @param bool $withremoterecordings create recording on the mock server ?
287
     * @return array
288
     */
289
    protected function create_log_entries(
290
        instance $instance,
291
        int $userid,
292
        int $count = 30,
293
        bool $importedrecordings = false,
294
        bool $withremoterecordings = true
295
    ): array {
296
        // Create log entries for each (30 for the ungrouped, 30 for the grouped).
297
        $baselogdata = [
298
            'courseid' => $instance->get_course_id(),
299
            'userid' => $userid,
300
            'log' => $importedrecordings ? 'Import' : 'Create',
301
            'meta' => json_encode(['record' => true]),
302
            'imported' => $importedrecordings,
303
        ];
304
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
305
        for ($i = 0; $i < $count; $i++) {
306
            if ($withremoterecordings) {
307
                // Create a recording.
308
                $starttime = time() - random_int(HOURSECS, WEEKSECS);
309
                $recording = $plugingenerator->create_recording([
310
                    'bigbluebuttonbnid' => $instance->get_instance_id(),
311
                    'groupid' => $instance->get_group_id(),
312
                    'starttime' => $starttime,
313
                    'endtime' => $starttime + HOURSECS,
314
                ], true); // Create them on the server only.
315
 
316
                $baselogdata['meetingid'] = $instance->get_meeting_id();
317
                if ($importedrecordings) {
318
                    // Fetch the data.
319
                    $data = recording_proxy::fetch_recordings([$recording->recordingid]);
320
                    $data = end($data);
321
                    if ($data) {
322
                        $metaonly = array_filter($data, function($key) {
323
                            return strstr($key, 'meta_');
324
                        }, ARRAY_FILTER_USE_KEY);
325
                    } else {
326
                        $data = [];
327
                    }
328
                    $baselogdata['meta'] = json_encode(array_merge([
329
                        'recording' => array_diff_key($data, $metaonly),
330
                    ], $metaonly));
331
 
332
                } else {
333
                    $baselogdata['meta'] = json_encode((object) ['record' => true]);
334
                }
335
            }
336
            // Insert the legacy log entry.
337
            $logs[] = $plugingenerator->create_log(array_merge($baselogdata, [
338
                'bigbluebuttonbnid' => $instance->get_instance_id(),
339
                'timecreated' => time() - random_int(HOURSECS, WEEKSECS) + (HOURSECS * $i),
340
            ]));
341
        }
342
 
343
        return $logs;
344
    }
345
}