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\external;
18
 
19
use core_external\external_api;
20
use mod_bigbluebuttonbn\instance;
21
use mod_bigbluebuttonbn\test\testcase_helper_trait;
22
use moodle_exception;
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
global $CFG;
27
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
28
 
29
/**
30
 * Tests for the update_course class.
31
 *
32
 * @package    mod_bigbluebuttonbn
33
 * @category   test
34
 * @copyright  2021 Andrew Lyons <andrew@nicols.co.uk>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 * @covers \mod_bigbluebuttonbn\external\can_join
37
 */
38
class can_join_test extends \externallib_advanced_testcase {
39
    use testcase_helper_trait;
40
    /**
41
     * Setup for test
42
     */
43
    public function setUp(): void {
44
        parent::setUp();
45
        $this->initialise_mock_server();
46
    }
47
 
48
    /**
49
     * Helper
50
     *
51
     * @param mixed ...$params
52
     * @return mixed
53
     */
54
    protected function can_join(...$params) {
55
        $canjoin = can_join::execute(...$params);
56
 
57
        return external_api::clean_returnvalue(can_join::execute_returns(), $canjoin);
58
    }
59
 
60
    /**
61
     * Test execute API CALL with no instance
62
     */
11 efrain 63
    public function test_execute_no_instance(): void {
1 efrain 64
        $this->resetAfterTest();
65
        $canjoin = $this->can_join(1234, 5678);
66
 
67
        $this->assertIsArray($canjoin);
68
        $this->assertArrayHasKey('can_join', $canjoin);
69
        $this->assertEquals(false, $canjoin['can_join']);
70
    }
71
 
72
    /**
73
     * Test execute API CALL without login
74
     */
11 efrain 75
    public function test_execute_without_login(): void {
1 efrain 76
        $this->resetAfterTest();
77
 
78
        $course = $this->getDataGenerator()->create_course();
79
        $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
80
        $instance = instance::get_from_instanceid($record->id);
81
 
82
        $this->expectException(moodle_exception::class);
83
        $this->can_join($instance->get_cm_id());
84
    }
85
 
86
    /**
87
     * Test execute API CALL with invalid login
88
     */
11 efrain 89
    public function test_execute_with_invalid_login(): void {
1 efrain 90
        $this->resetAfterTest();
91
 
92
        $generator = $this->getDataGenerator();
93
        $course = $generator->create_course();
94
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
95
        $instance = instance::get_from_instanceid($record->id);
96
 
97
        $user = $generator->create_user();
98
        $this->setUser($user);
99
 
100
        $this->expectException(moodle_exception::class);
101
        $this->can_join($instance->get_cm_id());
102
    }
103
 
104
    /**
105
     * When login as a student
106
     */
11 efrain 107
    public function test_execute_with_valid_login(): void {
1 efrain 108
        $this->resetAfterTest();
109
 
110
        $generator = $this->getDataGenerator();
111
        $course = $generator->create_course();
112
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
113
        $instance = instance::get_from_instanceid($record->id);
114
 
115
        $user = $generator->create_and_enrol($course, 'student');
116
        $this->setUser($user);
117
 
118
        $canjoin = $this->can_join($instance->get_cm_id());
119
 
120
        $this->assertIsArray($canjoin);
121
        $this->assertArrayHasKey('can_join', $canjoin);
122
        $this->assertEquals(true, $canjoin['can_join']);
123
    }
124
}