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 enable_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\enable_badges
38
 */
39
final class enable_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->assertFalse($data['sitebadge']->is_active());
51
        $this->assertFalse($data['coursebadge']->is_active());
52
 
53
        $result = enable_badges::execute([
54
            $data['sitebadge']->id,
55
            $data['coursebadge']->id,
56
        ]);
57
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
58
        $this->assertEquals($data['sitebadge']->id, $result['result'][0]['badgeid']);
59
        $this->assertEquals($data['coursebadge']->id, $result['result'][1]['badgeid']);
60
        $this->assertEmpty($result['warnings']);
61
 
62
        $sitebadge = new badge($data['sitebadge']->id);
63
        $coursebadge = new badge($data['coursebadge']->id);
64
        $this->assertTrue($sitebadge->is_active());
65
        $this->assertTrue($coursebadge->is_active());
66
    }
67
 
68
    /**
69
     * Test execute method when badges are disabled.
70
     */
71
    public function test_execute_badgesdisabled(): void {
72
        $this->resetAfterTest();
73
        $this->setAdminUser();
74
 
75
        // Disable course badges.
76
        set_config('enablebadges', 0);
77
 
78
        $data = $this->prepare_test_data();
79
 
80
        $this->expectException(\moodle_exception::class);
81
        $this->expectExceptionMessage(get_string('badgesdisabled', 'core_badges'));
82
        $result = enable_badges::execute([
83
            $data['sitebadge']->id,
84
            $data['coursebadge']->id,
85
        ]);
86
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
87
    }
88
 
89
    /**
90
     * Test execute method when course badges are disabled.
91
     */
92
    public function test_execute_coursebadgesdisabled(): void {
93
        $this->resetAfterTest();
94
        $this->setAdminUser();
95
 
96
        // Disable course badges.
97
        set_config('badges_allowcoursebadges', 0);
98
 
99
        $data = $this->prepare_test_data();
100
 
101
        $this->assertFalse($data['sitebadge']->is_active());
102
        $this->assertFalse($data['coursebadge']->is_active());
103
 
104
        $result = enable_badges::execute([
105
            $data['sitebadge']->id,
106
            $data['coursebadge']->id,
107
        ]);
108
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
109
        $this->assertEquals($data['sitebadge']->id, $result['result'][0]['badgeid']);
110
        // Course badge can't be enabled because course badges are disabled.
111
        $this->assertNotEmpty($result['warnings']);
112
        $this->assertEquals($data['coursebadge']->id, $result['warnings'][0]['item']);
113
        $this->assertEquals('coursebadgesdisabled', $result['warnings'][0]['warningcode']);
114
 
115
        $sitebadge = new badge($data['sitebadge']->id);
116
        $coursebadge = new badge($data['coursebadge']->id);
117
        $this->assertTrue($sitebadge->is_active());
118
        $this->assertFalse($coursebadge->is_active());
119
    }
120
 
121
    /**
122
     * Test execute method when badge doesn't have criteria.
123
     */
124
    public function test_execute_badgewithoutcriteria(): void {
125
        $this->resetAfterTest();
126
        $this->setAdminUser();
127
 
128
        $data = $this->prepare_test_data();
129
 
130
        $this->assertFalse($data['nocriteriabadge']->has_criteria());
131
        $this->assertFalse($data['nocriteriabadge']->is_active());
132
 
133
        $result = enable_badges::execute([
134
            $data['nocriteriabadge']->id,
135
        ]);
136
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
137
        $this->assertEmpty($result['result']);
138
        // Badges without criteria can't be enabled.
139
        $this->assertNotEmpty($result['warnings']);
140
        $this->assertEquals($data['nocriteriabadge']->id, $result['warnings'][0]['item']);
141
        $this->assertEquals('nocriteria', $result['warnings'][0]['warningcode']);
142
 
143
        $nocriteriabadge = new badge($data['nocriteriabadge']->id);
144
        $this->assertFalse($nocriteriabadge->is_active());
145
    }
146
 
147
    /**
148
     * Test execute method when the badge is already enabled.
149
     */
150
    public function test_execute_enableenabledbadge(): void {
151
        $this->resetAfterTest();
152
        $this->setAdminUser();
153
 
154
        $data = $this->prepare_test_data();
155
 
156
        $this->assertFalse($data['coursebadge']->is_active());
157
        $data['coursebadge']->set_status(BADGE_STATUS_ACTIVE);
158
        $this->assertTrue($data['coursebadge']->is_active());
159
 
160
        $result = enable_badges::execute([
161
            $data['coursebadge']->id,
162
        ]);
163
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
164
        $this->assertEquals($data['coursebadge']->id, $result['result'][0]['badgeid']);
165
        // Enabled badges can be enabled again.
166
        $this->assertEmpty($result['warnings']);
167
 
168
        $coursebadge = new badge($data['coursebadge']->id);
169
        $this->assertTrue($coursebadge->is_active());
170
    }
171
 
172
    /**
173
     * Test execute method when badgeid doesn't exist.
174
     */
175
    public function test_execute_wrongbadgeid(): void {
176
        $this->resetAfterTest();
177
        $this->setAdminUser();
178
 
179
        $badgeid = 1234;
180
        $this->expectException(\moodle_exception::class);
181
        $this->expectExceptionMessage(get_string('error:nosuchbadge', 'core_badges', $badgeid));
182
        $result = enable_badges::execute([
183
            $badgeid,
184
        ]);
185
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
186
    }
187
 
188
    /**
189
     * Test execute method when the user doesn't have the capability to enable badges.
190
     */
191
    public function test_execute_without_capability(): void {
192
        $this->resetAfterTest();
193
        $this->setAdminUser();
194
 
195
        $data = $this->prepare_test_data();
196
        $this->setUser($data['teacher']);
197
 
198
        $this->assertFalse($data['sitebadge']->is_active());
199
        $this->assertFalse($data['coursebadge']->is_active());
200
 
201
        $result = enable_badges::execute([
202
            $data['sitebadge']->id,
203
            $data['coursebadge']->id,
204
        ]);
205
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
206
        $this->assertEquals($data['coursebadge']->id, $result['result'][0]['badgeid']);
207
        // Teacher doesn't have capability to enable site badges.
208
        $this->assertNotEmpty($result['warnings']);
209
        $this->assertEquals($data['sitebadge']->id, $result['warnings'][0]['item']);
210
        $this->assertEquals('nopermissions', $result['warnings'][0]['warningcode']);
211
 
212
        $sitebadge = new badge($data['sitebadge']->id);
213
        $coursebadge = new badge($data['coursebadge']->id);
214
        $this->assertFalse($sitebadge->is_active());
215
        $this->assertTrue($coursebadge->is_active());
216
    }
217
 
218
    /**
219
     * Test execute method when the badge has the same name as another enabled badge.
220
     */
221
    public function test_execute_duplicatedname(): void {
222
        $this->resetAfterTest();
223
        $this->setAdminUser();
224
 
225
        $data = $this->prepare_test_data();
226
 
227
        $this->assertFalse($data['coursebadge']->is_active());
228
        $this->assertFalse($data['coursebadge2']->is_active());
229
 
230
        $result = enable_badges::execute([
231
            $data['coursebadge']->id,
232
            $data['coursebadge2']->id,
233
        ]);
234
        $result = \core_external\external_api::clean_returnvalue(enable_badges::execute_returns(), $result);
235
        // Badge can be enabled because badges with the same name are now allowed.
236
        $this->assertEquals($data['coursebadge']->id, $result['result'][0]['badgeid']);
237
        $this->assertEquals($data['coursebadge2']->id, $result['result'][1]['badgeid']);
238
        $this->assertEmpty($result['warnings']);
239
 
240
        $coursebadge = new badge($data['coursebadge']->id);
241
        $coursebadge2 = new badge($data['coursebadge2']->id);
242
        $this->assertTrue($coursebadge->is_active());
243
        $this->assertTrue($coursebadge2->is_active());
244
    }
245
 
246
    /**
247
     * Prepare the test, creating a few users and badges.
248
     *
249
     * @return array Test data.
250
     */
251
    private function prepare_test_data(): array {
252
        global $DB;
253
 
254
        // Setup test data.
255
        $course = $this->getDataGenerator()->create_course();
256
 
257
        // Create users and enrolments.
258
        $student1 = $this->getDataGenerator()->create_and_enrol($course);
259
        $student2 = $this->getDataGenerator()->create_and_enrol($course);
260
        $teacher  = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
261
 
262
        /** @var core_badges_generator $generator */
263
        $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
264
        $sitebadge = $generator->create_badge([
265
            'name' => 'Site badge',
266
            'description' => 'Site badge',
267
            'status' => BADGE_STATUS_INACTIVE,
268
        ]);
269
        $coursebadge = $generator->create_badge([
270
            'name' => 'Course badge',
271
            'description' => 'Course badge',
272
            'type' => BADGE_TYPE_COURSE,
273
            'courseid' => $course->id,
274
            'status' => BADGE_STATUS_INACTIVE,
275
        ]);
276
        $coursebadge2 = $generator->create_badge([
277
            'name' => 'Course badge',
278
            'description' => 'Course badge',
279
            'type' => BADGE_TYPE_COURSE,
280
            'courseid' => $course->id,
281
            'status' => BADGE_STATUS_INACTIVE,
282
        ]);
283
        $nocriteriabadge = $generator->create_badge([
284
            'name' => 'No criteria badge',
285
            'description' => 'No criteria badge',
286
            'type' => BADGE_TYPE_COURSE,
287
            'courseid' => $course->id,
288
            'status' => BADGE_STATUS_INACTIVE,
289
        ]);
290
 
291
        // Create criteria for manually awarding by role.
292
        $managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']);
293
        $generator->create_criteria(['badgeid' => $sitebadge->id, 'roleid' => $managerrole]);
294
        $generator->create_criteria(['badgeid' => $coursebadge->id, 'roleid' => $managerrole]);
295
        $generator->create_criteria(['badgeid' => $coursebadge2->id, 'roleid' => $managerrole]);
296
 
297
        // Issue badges to student1.
298
        $sitebadge->issue($student1->id, true);
299
        $coursebadge->issue($student1->id, true);
300
 
301
        return [
302
            'course'          => $course,
303
            'student1'        => $student1,
304
            'student2'        => $student2,
305
            'teacher'         => $teacher,
306
            'sitebadge'       => $sitebadge,
307
            'coursebadge'     => $coursebadge,
308
            'coursebadge2'    => $coursebadge2,
309
            'nocriteriabadge' => $nocriteriabadge,
310
        ];
311
    }
312
}