Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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\external;
18
 
19
use context_course;
20
use core_external\external_api;
21
use core_external\restricted_context_exception;
22
use mod_bigbluebuttonbn\instance;
23
use mod_bigbluebuttonbn\test\testcase_helper_trait;
24
use moodle_exception;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
30
 
31
/**
32
 * Tests for the get_join_url class.
33
 *
34
 * @package    mod_bigbluebuttonbn
35
 * @category   test
36
 * @copyright  2021 - present, Blindside Networks Inc
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @author    Laurent David (laurent@call-learning.fr)
39
 * @covers \mod_bigbluebuttonbn\external\get_join_url
40
 */
41
class get_join_url_test extends \externallib_advanced_testcase {
42
    use testcase_helper_trait;
43
 
44
    /**
45
     * Setup for test
46
     */
47
    public function setUp(): void {
48
        parent::setUp();
49
        $this->initialise_mock_server();
50
    }
51
 
52
    /**
53
     * Helper
54
     *
55
     * @param mixed ...$params
56
     * @return mixed
57
     */
58
    protected function get_join_url(...$params) {
59
        $getjoinurl = get_join_url::execute(...$params);
60
 
61
        return external_api::clean_returnvalue(get_join_url::execute_returns(), $getjoinurl);
62
    }
63
 
64
    /**
65
     * Test execute API CALL with no instance
66
     */
67
    public function test_execute_no_instance() {
68
        $this->resetAfterTest();
69
        $this->expectExceptionMessageMatches('/No such instance.*/');
70
        $joinurl = $this->get_join_url(1234, 5678);
71
 
72
        $this->assertIsArray($joinurl);
73
        $this->assertArrayNotHasKey('join_url', $joinurl);
74
        $this->assertEquals(false, $joinurl['join_url']);
75
    }
76
 
77
    /**
78
     * Test execute API CALL without login
79
     */
80
    public function test_execute_without_login() {
81
        $this->resetAfterTest();
82
 
83
        $course = $this->getDataGenerator()->create_course();
84
        $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
85
        $instance = instance::get_from_instanceid($record->id);
86
 
87
        $this->expectException(moodle_exception::class);
88
        $this->get_join_url($instance->get_cm_id());
89
    }
90
 
91
    /**
92
     * Test execution with a user who doesn't have the capability to join the meeting
93
     */
94
    public function test_execute_without_capability(): void {
95
        global $DB;
96
 
97
        $this->resetAfterTest();
98
 
99
        $course = $this->getDataGenerator()->create_course();
100
        $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
101
        $instance = instance::get_from_instanceid($record->id);
102
 
103
        $user = $this->getDataGenerator()->create_and_enrol($course);
104
        $this->setUser($user);
105
 
106
        $student = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
107
        assign_capability('mod/bigbluebuttonbn:join', CAP_PROHIBIT, $student, context_course::instance($course->id), true);
108
 
109
        $this->expectException(restricted_context_exception::class);
110
        $this->get_join_url($instance->get_cm_id());
111
    }
112
 
113
    /**
114
     * Test execute API CALL with invalid login
115
     */
116
    public function test_execute_with_invalid_login() {
117
        $this->resetAfterTest();
118
 
119
        $generator = $this->getDataGenerator();
120
        $course = $generator->create_course();
121
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
122
        $instance = instance::get_from_instanceid($record->id);
123
 
124
        $user = $generator->create_user();
125
        $this->setUser($user);
126
 
127
        $this->expectException(moodle_exception::class);
128
        $this->get_join_url($instance->get_cm_id());
129
    }
130
 
131
    /**
132
     * When login as a student
133
     */
134
    public function test_execute_with_valid_login() {
135
        $this->resetAfterTest();
136
 
137
        $generator = $this->getDataGenerator();
138
        $course = $generator->create_course();
139
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
140
        $instance = instance::get_from_instanceid($record->id);
141
 
142
        $user = $generator->create_and_enrol($course, 'student');
143
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
144
        // Make sure the meeting is running (this is not the default with the mock server).
145
        $bbbgenerator->create_meeting([
146
            'instanceid' => $instance->get_instance_id(),
147
            'groupid' => $instance->get_group_id()
148
        ]);
149
 
150
        $this->setUser($user);
151
 
152
        $joinurl = $this->get_join_url($instance->get_cm_id());
153
 
154
        $this->assertIsArray($joinurl);
155
        $this->assertArrayHasKey('join_url', $joinurl);
156
        $this->assertEmpty($joinurl['warnings']);
157
        $this->assertNotNull($joinurl['join_url']);
158
    }
159
 
160
    /**
161
     * Check that URL are different depending on the group.
162
     */
163
    public function test_execute_with_group() {
164
        $this->resetAfterTest();
165
 
166
        $generator = $this->getDataGenerator();
167
        $course = $generator->create_course();
168
        $g1 = $generator->create_group(['courseid' => $course->id]);
169
        $g2 = $generator->create_group(['courseid' => $course->id]);
170
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
171
        $instance = instance::get_from_instanceid($record->id);
172
 
173
        $user = $generator->create_and_enrol($course, 'student');
174
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
175
        // Make sure the meeting is running (this is not the default with the mock server).
176
        $bbbgenerator->create_meeting([
177
            'instanceid' => $instance->get_instance_id(),
178
            'groupid' => $instance->get_group_id()
179
        ]);
180
 
181
        $this->setUser($user);
182
 
183
        $joinurlnogroup = $this->get_join_url($instance->get_cm_id());
184
        $joinurlnogroupv2 = $this->get_join_url($instance->get_cm_id());
185
        $joinurlg1 = $this->get_join_url($instance->get_cm_id(), $g1->id);
186
        $joinurlg2 = $this->get_join_url($instance->get_cm_id(), $g2->id);
187
 
188
        foreach ([$joinurlnogroup, $joinurlnogroupv2, $joinurlg1, $joinurlg2] as $join) {
189
            $this->assertIsArray($join);
190
            $this->assertArrayHasKey('join_url', $join);
191
            $this->assertEmpty($join['warnings']);
192
            $this->assertNotNull($join['join_url']);
193
        }
194
        $this->assertNotEquals($joinurlnogroup['join_url'], $joinurlg1['join_url']);
195
        $this->assertNotEquals($joinurlg2['join_url'], $joinurlg1['join_url']);
196
    }
197
 
198
    /**
199
     * Check that we return the same URL once meeting is started.
200
     */
201
    public function test_execute_with_same_url() {
202
        $this->resetAfterTest();
203
 
204
        $generator = $this->getDataGenerator();
205
        $course = $generator->create_course();
206
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
207
        $instance = instance::get_from_instanceid($record->id);
208
 
209
        $user = $generator->create_and_enrol($course, 'student');
210
 
211
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
212
        // Make sure the meeting is running (this is not the default with the mock server).
213
        $bbbgenerator->create_meeting([
214
            'instanceid' => $instance->get_instance_id(),
215
            'groupid' => $instance->get_group_id()
216
        ]);
217
 
218
        $this->setUser($user);
219
 
220
        $joinurl = $this->get_join_url($instance->get_cm_id());
221
        $joinurlv2 = $this->get_join_url($instance->get_cm_id());
222
 
223
        foreach ([$joinurl, $joinurlv2] as $join) {
224
            $this->assertIsArray($join);
225
            $this->assertArrayHasKey('join_url', $join);
226
            $this->assertEmpty($join['warnings']);
227
            $this->assertNotNull($join['join_url']);
228
        }
229
        $this->assertEquals($joinurl['join_url'], $joinurlv2['join_url']);
230
    }
231
 
232
    /**
233
     * Check that we return the same URL once meeting is started.
234
     */
235
    public function test_user_limit() {
236
        $this->resetAfterTest();
237
        set_config('bigbluebuttonbn_userlimit_editable', true);
238
        $generator = $this->getDataGenerator();
239
        $course = $generator->create_course();
240
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id, 'userlimit' => 2]);
241
 
242
        $user1 = $generator->create_and_enrol($course, 'student');
243
        $user2 = $generator->create_and_enrol($course, 'student');
244
        $instance = instance::get_from_instanceid($record->id);
245
 
246
        $bbbgenerator = $this->getDataGenerator()->get_plugin_generator('mod_bigbluebuttonbn');
247
        // Make sure the meeting is running (this is not the default with the mock server).
248
        $bbbgenerator->create_meeting([
249
            'instanceid' => $instance->get_instance_id(),
250
            'groupid' => $instance->get_group_id(),
251
            'participants' => 2
252
        ]);
253
        $this->setUser($user1);
254
        $joinurl = $this->get_join_url($instance->get_cm_id());
255
        $this->assertNotNull($joinurl['warnings']);
256
        $this->assertEquals('userlimitreached', $joinurl['warnings'][0]['warningcode']);
257
    }
258
}
259