Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_badges\external;
18
 
19
use core_badges_generator;
20
use core_badges\badge;
21
use externallib_advanced_testcase;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
 
27
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
28
require_once($CFG->libdir . '/badgeslib.php');
29
 
30
/**
31
 * Tests for external function disable_badges.
32
 *
33
 * @package    core_badges
34
 * @copyright  2024 Sara Arjona <sara@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 * @since      Moodle 4.5
37
 * @covers     \core_badges\external\disable_badges
38
 */
39
final class disable_badges_test extends externallib_advanced_testcase {
40
 
41
    /**
42
     * Test execute method.
43
     */
44
    public function test_execute(): void {
45
        $this->resetAfterTest();
46
        $this->setAdminUser();
47
 
48
        $data = $this->prepare_test_data();
49
 
50
        $this->assertTrue($data['sitebadge']->is_active());
51
        $this->assertTrue($data['coursebadge']->is_active());
52
 
53
        $result = disable_badges::execute([
54
            $data['sitebadge']->id,
55
            $data['coursebadge']->id,
56
        ]);
57
        $result = \core_external\external_api::clean_returnvalue(disable_badges::execute_returns(), $result);
58
        $this->assertTrue($result['result']);
59
        $this->assertEmpty($result['warnings']);
60
 
61
        $sitebadge = new badge($data['sitebadge']->id);
62
        $coursebadge = new badge($data['coursebadge']->id);
63
        $this->assertFalse($sitebadge->is_active());
64
        $this->assertFalse($coursebadge->is_active());
65
    }
66
 
67
    /**
68
     * Test execute method when badges are disabled.
69
     */
70
    public function test_execute_badgesdisabled(): void {
71
        $this->resetAfterTest();
72
        $this->setAdminUser();
73
 
74
        // Disable course badges.
75
        set_config('enablebadges', 0);
76
 
77
        $data = $this->prepare_test_data();
78
 
79
        $this->expectException(\moodle_exception::class);
80
        $this->expectExceptionMessage(get_string('badgesdisabled', 'core_badges'));
81
        $result = disable_badges::execute([
82
            $data['sitebadge']->id,
83
            $data['coursebadge']->id,
84
        ]);
85
        $result = \core_external\external_api::clean_returnvalue(disable_badges::execute_returns(), $result);
86
    }
87
 
88
    /**
89
     * Test execute method when course badges are disabled.
90
     */
91
    public function test_execute_coursebadgesdisabled(): void {
92
        $this->resetAfterTest();
93
        $this->setAdminUser();
94
 
95
        // Disable course badges.
96
        set_config('badges_allowcoursebadges', 0);
97
 
98
        $data = $this->prepare_test_data();
99
 
100
        $this->assertTrue($data['sitebadge']->is_active());
101
        $this->assertTrue($data['coursebadge']->is_active());
102
 
103
        $result = disable_badges::execute([
104
            $data['sitebadge']->id,
105
            $data['coursebadge']->id,
106
        ]);
107
        $result = \core_external\external_api::clean_returnvalue(disable_badges::execute_returns(), $result);
108
        // Course badge can't be disabled because course badges are disabled.
109
        $this->assertFalse($result['result']);
110
        $this->assertNotEmpty($result['warnings']);
111
        $this->assertEquals($data['coursebadge']->id, $result['warnings'][0]['item']);
112
        $this->assertEquals('coursebadgesdisabled', $result['warnings'][0]['warningcode']);
113
 
114
        $sitebadge = new badge($data['sitebadge']->id);
115
        $coursebadge = new badge($data['coursebadge']->id);
116
        $this->assertFalse($sitebadge->is_active());
117
        $this->assertTrue($coursebadge->is_active());
118
    }
119
 
120
    /**
121
     * Test execute method when the user doesn't have the capability to disable badges.
122
     */
123
    public function test_execute_without_capability(): void {
124
        $this->resetAfterTest();
125
        $this->setAdminUser();
126
 
127
        $data = $this->prepare_test_data();
128
        $this->setUser($data['teacher']);
129
 
130
        $this->assertTrue($data['sitebadge']->is_active());
131
        $this->assertTrue($data['coursebadge']->is_active());
132
 
133
        $result = disable_badges::execute([
134
            $data['sitebadge']->id,
135
            $data['coursebadge']->id,
136
        ]);
137
        $result = \core_external\external_api::clean_returnvalue(disable_badges::execute_returns(), $result);
138
        // Teacher doesn't have capability to disable site badges.
139
        $this->assertFalse($result['result']);
140
        $this->assertNotEmpty($result['warnings']);
141
        $this->assertEquals($data['sitebadge']->id, $result['warnings'][0]['item']);
142
        $this->assertEquals('nopermissions', $result['warnings'][0]['warningcode']);
143
 
144
        $sitebadge = new badge($data['sitebadge']->id);
145
        $coursebadge = new badge($data['coursebadge']->id);
146
        $this->assertTrue($sitebadge->is_active());
147
        $this->assertFalse($coursebadge->is_active());
148
    }
149
 
150
    /**
151
     * Test execute method when the badge is already disabled.
152
     */
153
    public function test_execute_disabledisabledbadge(): void {
154
        $this->resetAfterTest();
155
        $this->setAdminUser();
156
 
157
        $data = $this->prepare_test_data();
158
 
159
        $this->assertTrue($data['coursebadge']->is_active());
160
        $data['coursebadge']->set_status(BADGE_STATUS_INACTIVE);
161
        $this->assertFalse($data['coursebadge']->is_active());
162
 
163
        $result = disable_badges::execute([
164
            $data['coursebadge']->id,
165
        ]);
166
        $result = \core_external\external_api::clean_returnvalue(disable_badges::execute_returns(), $result);
167
        // Disabled badges can be disabled again.
168
        $this->assertTrue($result['result']);
169
        $this->assertEmpty($result['warnings']);
170
 
171
        $coursebadge = new badge($data['coursebadge']->id);
172
        $this->assertFalse($coursebadge->is_active());
173
    }
174
 
175
    /**
176
     * Prepare the test, creating a few users and badges.
177
     *
178
     * @return array Test data.
179
     */
180
    private function prepare_test_data(): array {
181
        global $DB;
182
 
183
        // Setup test data.
184
        $course = $this->getDataGenerator()->create_course();
185
 
186
        // Create users and enrolments.
187
        $student1 = $this->getDataGenerator()->create_and_enrol($course);
188
        $student2 = $this->getDataGenerator()->create_and_enrol($course);
189
        $teacher  = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
190
 
191
        /** @var core_badges_generator $generator */
192
        $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
193
        $sitebadge = $generator->create_badge([
194
            'name' => 'Site badge',
195
            'description' => 'Site badge',
196
            'status' => BADGE_STATUS_ACTIVE,
197
        ]);
198
        $coursebadge = $generator->create_badge([
199
            'name' => 'Course badge',
200
            'description' => 'Course badge',
201
            'type' => BADGE_TYPE_COURSE,
202
            'courseid' => $course->id,
203
            'status' => BADGE_STATUS_ACTIVE,
204
        ]);
205
 
206
        // Create criteria for manually awarding by role.
207
        $managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
208
        $generator->create_criteria(['badgeid' => $sitebadge->id, 'roleid' => $managerrole]);
209
        $generator->create_criteria(['badgeid' => $coursebadge->id, 'roleid' => $managerrole]);
210
 
211
        // Issue badges to student1.
212
        $sitebadge->issue($student1->id, true);
213
        $coursebadge->issue($student1->id, true);
214
 
215
        return [
216
            'course'          => $course,
217
            'student1'        => $student1,
218
            'student2'        => $student2,
219
            'teacher'         => $teacher,
220
            'sitebadge'       => $sitebadge,
221
            'coursebadge'     => $coursebadge,
222
        ];
223
    }
224
}