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 core_external\external_api;
20
use mod_bigbluebuttonbn\instance;
21
use mod_bigbluebuttonbn\test\testcase_helper_trait;
22
use require_login_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 completion_validate class.
31
 *
32
 * @package    mod_bigbluebuttonbn
33
 * @category   test
34
 * @copyright  2021 - present, Blindside Networks Inc
35
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 * @author    Laurent David (laurent@call-learning.fr)
37
 * @covers \mod_bigbluebuttonbn\external\completion_validate
38
 */
39
class completion_validate_test extends \externallib_advanced_testcase {
40
    use testcase_helper_trait;
41
 
42
    /**
43
     * Setup for test
44
     */
45
    public function setUp(): void {
46
        parent::setUp();
47
        $this->initialise_mock_server();
48
    }
49
 
50
    /**
51
     * Helper
52
     *
53
     * @param mixed ...$params
54
     * @return mixed
55
     */
56
    protected function completion_validate(...$params) {
57
        $returnvalue = completion_validate::execute(...$params);
58
 
59
        return external_api::clean_returnvalue(completion_validate::execute_returns(), $returnvalue);
60
    }
61
 
62
    /**
63
     * Test execute API CALL with no instance
64
     */
65
    public function test_execute_no_instance() {
66
        $this->resetAfterTest();
67
        $result = $this->completion_validate(1234);
68
 
69
        $this->assertIsArray($result);
70
        $this->assertArrayHasKey('warnings', $result);
71
        $this->assertEquals([
72
            'item' => 'mod_bigbluebuttonbn',
73
            'itemid' => 1234,
74
            'warningcode' => 'indexerrorbbtn',
75
            'message' => 'BigBlueButton ID 1234 is incorrect',
76
        ], $result['warnings'][0]);
77
    }
78
 
79
    /**
80
     * Test execute API CALL without login
81
     */
82
    public function test_execute_without_login() {
83
        $this->resetAfterTest();
84
 
85
        $course = $this->getDataGenerator()->create_course();
86
        $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
87
        $instance = instance::get_from_instanceid($record->id);
88
        $this->expectException(require_login_exception::class);
89
        $this->completion_validate($instance->get_instance_id());
90
    }
91
 
92
    /**
93
     * Test execute API CALL with invalid login
94
     */
95
    public function test_execute_with_invalid_login() {
96
        $this->resetAfterTest();
97
 
98
        $generator = $this->getDataGenerator();
99
        $course = $generator->create_course();
100
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
101
        $instance = instance::get_from_instanceid($record->id);
102
 
103
        $user = $generator->create_user($course);
104
        $this->setUser($user);
105
        $this->expectException(require_login_exception::class);
106
        $this->completion_validate($instance->get_instance_id());
107
    }
108
 
109
    /**
110
     * When login as a student
111
     */
112
    public function test_execute_with_valid_login_but_student() {
113
        $this->resetAfterTest();
114
 
115
        $generator = $this->getDataGenerator();
116
        $course = $generator->create_course();
117
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
118
        $instance = instance::get_from_instanceid($record->id);
119
 
120
        $user = $generator->create_and_enrol($course, 'student');
121
        $this->setUser($user);
122
 
123
        $returnvalue = $this->completion_validate($instance->get_instance_id());
124
 
125
        $this->assertArrayHasKey('warnings', $returnvalue);
126
 
127
        $this->assertEquals([
128
            'item' => 'mod_bigbluebuttonbn',
129
            'itemid' => $record->id,
130
            'warningcode' => 'nopermissions',
131
            'message' => 'Sorry, but you do not currently have permissions to do that (completion_validate).',
132
        ], $returnvalue['warnings'][0]);
133
    }
134
 
135
    /**
136
     * When login as a student
137
     */
138
    public function test_execute_with_valid_login_with_teacher() {
139
        $this->resetAfterTest();
140
 
141
        $generator = $this->getDataGenerator();
142
        $course = $generator->create_course();
143
        $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
144
        $instance = instance::get_from_instanceid($record->id);
145
 
146
        $user = $generator->create_and_enrol($course, 'editingteacher');
147
        $this->setUser($user);
148
 
149
        $returnvalue = $this->completion_validate($instance->get_instance_id());
150
 
151
        $this->assertArrayHasKey('warnings', $returnvalue);
152
        $this->assertEmpty($returnvalue['warnings']);
153
    }
154
}
155