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 mod_bigbluebuttonbn\completion;
18
 
19
use completion_info;
20
use context_module;
21
use mod_bigbluebuttonbn\instance;
22
use mod_bigbluebuttonbn\logger;
23
use mod_bigbluebuttonbn\meeting;
24
use mod_bigbluebuttonbn\test\testcase_helper_trait;
25
 
26
/**
27
 * Tests for Big Blue Button Completion.
28
 *
29
 * @package   mod_bigbluebuttonbn
30
 * @copyright 2021 - present, Blindside Networks Inc
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @author    Laurent David (laurent@call-learning.fr)
33
 * @covers \mod_bigbluebuttonbn\completion\custom_completion
34
 */
35
class completion_test extends \advanced_testcase {
36
    use testcase_helper_trait;
37
 
38
    /**
39
     * Setup basic
40
     */
41
    public function setUp(): void {
42
        parent::setUp();
43
        $this->initialise_mock_server();
44
        set_config('enablecompletion', true); // Enable completion for all tests.
45
    }
46
 
47
    /**
48
     * Completion with no rules: the completion is completed as soons as we view the course.
49
     */
11 efrain 50
    public function test_get_completion_state_no_rules(): void {
1 efrain 51
        $this->resetAfterTest();
52
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
53
 
54
        $user = $this->getDataGenerator()->create_user();
55
        $this->setUser($user);
56
 
57
        $completion = new custom_completion($bbactivitycm, $user->id);
58
        $result = $completion->get_overall_completion_state();
59
        // No custom rules so complete by default.
60
        $this->assertEquals(COMPLETION_COMPLETE, $result);
61
    }
62
 
63
    /**
64
     * Completion with no rules and join meeting
65
     */
11 efrain 66
    public function test_get_completion_state_no_rules_and_join_meeting(): void {
1 efrain 67
        $this->resetAfterTest();
68
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
69
 
70
        $user = $this->getDataGenerator()->create_user();
71
        $this->setUser($user);
72
 
73
        // Now create a couple of logs.
74
        $instance = instance::get_from_instanceid($bbactivity->id);
75
        logger::log_meeting_joined_event($instance, 0);
76
        // No custom rules and we joined once so complete.
77
        $completion = new custom_completion($bbactivitycm, $user->id);
78
        $result = $completion->get_overall_completion_state();
79
        $this->assertEquals(COMPLETION_COMPLETE, $result);
80
    }
81
 
82
    /**
83
     * With state incomplete
84
     */
11 efrain 85
    public function test_get_completion_state_incomplete(): void {
1 efrain 86
        $this->resetAfterTest();
87
 
88
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance();
89
 
90
        $bbactivitycm->override_customdata('customcompletionrules', [
91
            'completionengagementchats' => '1',
92
            'completionattendance' => '1'
93
        ]);
94
 
95
        $user = $this->getDataGenerator()->create_user();
96
        $this->setUser($user);
97
 
98
        $completion = new custom_completion($bbactivitycm, $user->id);
99
        $result = $completion->get_overall_completion_state();
100
        $this->assertEquals(COMPLETION_INCOMPLETE, $result);
101
    }
102
 
103
    /**
104
     * With state complete
105
     */
11 efrain 106
    public function test_get_completion_state_complete(): void {
1 efrain 107
        $this->resetAfterTest();
108
 
109
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(
110
            $this->get_course(),
111
            [
112
                'completion' => '2',
113
                'completionengagementtalks' => 2,
114
                'completionengagementchats' => 2,
115
                'completionattendance' => 15
116
            ]
117
        );
118
        $instance = instance::get_from_instanceid($bbactivity->id);
119
 
120
        $user = $this->getDataGenerator()->create_user();
121
        $this->setUser($user);
122
 
123
        // Add a couple of fake logs.
124
        $overrides = ['meetingid' => $bbactivity->meetingid];
125
        $meta = [
126
            'origin' => 0,
127
            'data' => [
128
                'duration' => 300, // 300 seconds, i.e 5 mins.
129
                'engagement' => [
130
                    'chats' => 2,
131
                    'talks' => 2,
132
                ],
133
            ],
134
        ];
135
 
136
        // We setup a couple of logs as per engagement and duration.
137
        logger::log_event_summary($instance, $overrides, $meta);
138
        logger::log_event_summary($instance, $overrides, $meta);
139
        $completion = new custom_completion($bbactivitycm, $user->id);
140
        $result = $completion->get_overall_completion_state();
141
        $this->assertEquals(COMPLETION_INCOMPLETE, $result);
142
 
143
        // Now we have 15 mins.
144
        logger::log_event_summary($instance, $overrides, $meta);
145
        // Now that the meeting was joined, it should be complete.
146
        $completion = new custom_completion($bbactivitycm, $user->id);
147
        $result = $completion->get_overall_completion_state();
148
        $this->assertEquals(COMPLETION_COMPLETE, $result);
149
    }
150
 
151
    /**
152
     * No rule description but active
153
     */
11 efrain 154
    public function test_mod_bigbluebuttonbn_get_completion_active_rule_descriptions(): void {
1 efrain 155
        $this->resetAfterTest();
156
        $user = $this->getDataGenerator()->create_user();
157
        $this->setUser($user);
158
        // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
159
        // Inspired from the same test in forum.
160
        list($bbactivitycontext, $cm1, $bbactivity) = $this->create_instance($this->get_course(),
161
            ['completion' => '2']);
162
        $cm1->override_customdata('customcompletionrules', [
163
            'completionattendance' => '1'
164
        ]);
165
        list($bbactivitycontext, $cm2, $bbactivity) = $this->create_instance($this->get_course(),
166
            ['completion' => '2']);
167
        $cm2->override_customdata('customcompletionrules', [
168
            'completionattendance' => '0'
169
        ]);
170
 
171
        $completioncm1 = new custom_completion($cm1, $user->id);
172
        // TODO: check the return value here as there might be an issue with the function compared to the forum for example.
173
        $this->assertEquals(
174
            [
175
                'completionengagementchats' => get_string('completionengagementchats_desc', 'mod_bigbluebuttonbn', 1),
176
                'completionengagementtalks' => get_string('completionengagementtalks_desc', 'mod_bigbluebuttonbn', 1),
177
                'completionattendance' => get_string('completionattendance_desc', 'mod_bigbluebuttonbn', 1),
178
                'completionengagementraisehand' => get_string('completionengagementraisehand_desc', 'mod_bigbluebuttonbn', 1),
179
                'completionengagementpollvotes' => get_string('completionengagementpollvotes_desc', 'mod_bigbluebuttonbn', 1),
180
                'completionengagementemojis' => get_string('completionengagementemojis_desc', 'mod_bigbluebuttonbn', 1)
181
            ],
182
            $completioncm1->get_custom_rule_descriptions());
183
        $completioncm2 = new custom_completion($cm2, $user->id);
184
        $this->assertEquals(
185
            [
186
                'completionengagementchats' => get_string('completionengagementchats_desc', 'mod_bigbluebuttonbn', 1),
187
                'completionengagementtalks' => get_string('completionengagementtalks_desc', 'mod_bigbluebuttonbn', 1),
188
                'completionattendance' => get_string('completionattendance_desc', 'mod_bigbluebuttonbn', 0),
189
                'completionengagementraisehand' => get_string('completionengagementraisehand_desc', 'mod_bigbluebuttonbn', 1),
190
                'completionengagementpollvotes' => get_string('completionengagementpollvotes_desc', 'mod_bigbluebuttonbn', 1),
191
                'completionengagementemojis' => get_string('completionengagementemojis_desc', 'mod_bigbluebuttonbn', 1)
192
            ], $completioncm2->get_custom_rule_descriptions());
193
    }
194
 
195
    /**
196
     * Completion View
197
     */
11 efrain 198
    public function test_view(): void {
1 efrain 199
        $this->resetAfterTest();
200
        $this->setAdminUser();
201
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(
202
            null, ['completion' => 2, 'completionview' => 1]
203
        );
204
 
205
        // Trigger and capture the event.
206
        $sink = $this->redirectEvents();
207
 
208
        // Check completion before viewing.
209
        $completion = new completion_info($this->get_course());
210
        $completiondata = $completion->get_data($bbactivitycm);
211
        $this->assertEquals(0, $completiondata->viewed);
212
        $this->assertEquals(COMPLETION_NOT_VIEWED, $completiondata->completionstate);
213
 
214
        bigbluebuttonbn_view($bbactivity, $this->get_course(), $bbactivitycm, context_module::instance($bbactivitycm->id));
215
 
216
        $events = $sink->get_events();
217
        $this->assertTrue(count($events) > 1); // TODO : Here we have the module completion event triggered twice.
218
        // this might be a bug from 4.0 core and will need some further investigation.
219
        $event = reset($events);
220
 
221
        // Checking that the event contains the expected values.
222
        $this->assertInstanceOf('\mod_bigbluebuttonbn\event\course_module_viewed', $event);
223
        $this->assertEquals($bbactivitycontext, $event->get_context());
224
        $url = new \moodle_url('/mod/bigbluebuttonbn/view.php', ['id' => $bbactivitycontext->instanceid]);
225
        $this->assertEquals($url, $event->get_url());
226
        $this->assertEventContextNotUsed($event);
227
        $this->assertNotEmpty($event->get_name());
228
 
229
        // Check completion status.
230
        $completion = new completion_info($this->get_course());
231
        $completiondata = $completion->get_data($bbactivitycm);
232
        $this->assertEquals(1, $completiondata->viewed);
233
        $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
234
    }
235
 
236
    /**
237
     * Completion with no rules and join meeting
238
     *
239
     * @param array $customcompletionrules
240
     * @param array $events
241
     * @param int $expectedstate
242
     * @dataProvider custom_completion_data_provider
243
     */
11 efrain 244
    public function test_get_completion_with_events(array $customcompletionrules, array $events, int $expectedstate): void {
1 efrain 245
        $this->resetAfterTest();
246
        list($bbactivitycontext, $bbactivitycm, $bbactivity) = $this->create_instance(
247
            $this->get_course(),
248
            [
249
                'completion' => '2',
250
            ]
251
        );
252
        $bbactivitycm->override_customdata('customcompletionrules', $customcompletionrules);
253
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
254
 
255
        $user = $this->getDataGenerator()->create_user();
256
        $this->setUser($user);
257
 
258
        // Now create a couple of events.
259
        $instance = instance::get_from_instanceid($bbactivity->id);
260
        set_config('bigbluebuttonbn_meetingevents_enabled', true);
261
        $meeting = $plugingenerator->create_meeting([
262
            'instanceid' => $instance->get_instance_id(),
263
            'groupid' => $instance->get_group_id(),
264
            'participants' => json_encode([$user->id])
265
        ]);
266
        foreach ($events as $edesc) {
267
            $plugingenerator->add_meeting_event($user, $instance, $edesc->name, $edesc->data ?? '');
268
        }
269
        $result = $plugingenerator->send_all_events($instance);
270
        $this->assertNotEmpty($result->data);
271
        $data = json_decode(json_encode($result->data));
272
        meeting::meeting_events($instance, $data);
273
        $completion = new custom_completion($bbactivitycm, $user->id);
274
        $result = $completion->get_overall_completion_state();
275
        $this->assertEquals($expectedstate, $result);
276
    }
277
 
278
    /**
279
     * Data generator
280
     *
281
     * @return array[]
282
     */
283
    public function custom_completion_data_provider() {
284
        return [
285
            'simple' => [
286
                'customcompletionrules' => [
287
                    'completionengagementtalks' => 1,
288
                    'completionengagementchats' => 1,
289
                ],
290
                'events' => [
291
                    (object) ['name' => 'talks'],
292
                    (object) ['name' => 'chats']
293
                ],
294
                'expectedstate' => COMPLETION_COMPLETE
295
            ],
296
            'not right events' => [
297
                'customcompletionrules' => [
298
                    'completionengagementchats' => 1,
299
                ],
300
                'events' => [
301
                    (object) ['name' => 'talks']
302
                ],
303
                'expectedstate' => COMPLETION_INCOMPLETE
304
            ],
305
            'attendance' => [
306
                'customcompletionrules' => [
307
                    'completionattendance' => 1,
308
                ],
309
                'events' => [
310
                    (object) ['name' => 'talks'],
311
                    (object) ['name' => 'attendance', 'data' => '70']
312
                ],
313
                'expectedstate' => COMPLETION_COMPLETE
314
            ]
315
        ];
316
    }
317
}
318