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
declare(strict_types=1);
18
 
19
namespace core_badges;
20
 
21
use core_badges_generator;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once("{$CFG->libdir}/badgeslib.php");
27
 
28
/**
29
 * Unit tests for badge class.
30
 *
31
 * @package     core_badges
32
 * @covers      \core_badges\badge
33
 * @copyright   2024 Sara Arjona <sara@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
final class badge_test extends \advanced_testcase {
37
 
38
    /**
39
     * Test create_badge.
40
     *
41
     * @dataProvider badges_provider
42
     * @param bool $iscourse Whether the badge is a course badge or not.
43
     * @param array $data Badge data. It will override the default data.
44
     */
45
    public function test_create_badge(bool $iscourse = false, array $data = []): void {
46
        global $DB;
47
 
48
        $this->resetAfterTest();
49
 
50
        $courseid = null;
51
        if ($iscourse) {
52
            $course = $this->getDataGenerator()->create_course();
53
            $courseid = (int) $course->id;
54
        }
55
 
56
        $user1 = $this->getDataGenerator()->create_user();
57
        $this->setUser($user1);
58
 
59
        // Check no badges exist.
60
        $this->assertEquals(0, $DB->count_records('badge'));
61
 
62
        $data = (object) array_merge($this->get_badge(), $data);
63
 
64
        // Trigger and capture events.
65
        $sink = $this->redirectEvents();
66
 
67
        $badge = badge::create_badge($data, $courseid);
68
        // Check the badge was created with the correct data.
69
        $this->assertEquals(1, $DB->count_records('badge'));
70
        $this->assertNotEmpty($badge->id);
71
        if ($iscourse) {
72
            $this->assertEquals(BADGE_TYPE_COURSE, $badge->type);
73
            $this->assertEquals($course->id, $badge->courseid);
74
        } else {
75
            $this->assertEquals(BADGE_TYPE_SITE, $badge->type);
76
            $this->assertNull($badge->courseid);
77
        }
78
        // Badges are always inactive by default, regardless the given status.
79
        $this->assertEquals(BADGE_STATUS_INACTIVE, $badge->status);
80
 
81
        if (property_exists($data, 'tags')) {
82
            $this->assertEquals($data->tags, $badge->get_badge_tags());
83
        }
84
 
85
        // Check that the event was triggered.
86
        $events = $sink->get_events();
87
        $event = reset($events);
88
 
89
        // Check that the event data is valid.
90
        $this->assertInstanceOf('\core\event\badge_created', $event);
91
        $this->assertEquals($badge->usercreated, $event->userid);
92
        $this->assertEquals($badge->id, $event->objectid);
93
        $this->assertDebuggingNotCalled();
94
        $sink->close();
95
    }
96
 
97
    /**
98
     * Test update() in badge class.
99
     *
100
     * @dataProvider badges_provider
101
     * @param bool $iscourse Whether the badge is a course badge or not.
102
     * @param array $data Badge data to update the badge with. It will override the default data.
103
     */
104
    public function test_udpate_badge(bool $iscourse = false, array $data = []): void {
105
        global $USER, $DB;
106
 
107
        $this->resetAfterTest();
108
 
109
        $record = [];
110
        if ($iscourse) {
111
            $course = $this->getDataGenerator()->create_course();
112
            $record['type'] = BADGE_TYPE_COURSE;
113
            $record['courseid'] = $course->id;
114
        }
115
 
116
        /** @var core_badges_generator $generator */
117
        $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
118
 
119
        $user1 = $this->getDataGenerator()->create_user();
120
        $user2 = $this->getDataGenerator()->create_user();
121
        $this->setUser($user1);
122
 
123
        /** @var badge $badge */
124
        $badge = $generator->create_badge($record);
125
        $data = (object) array_merge($this->get_badge(), $data);
126
 
127
        // Check the badge has been created.
128
        $this->assertEquals(1, $DB->count_records('badge'));
129
        $this->assertNotEquals($data->name, $badge->name);
130
        $this->assertEmpty($badge->get_badge_tags());
131
 
132
        // Trigger and capture events.
133
        $sink = $this->redirectEvents();
134
 
135
        $this->setUser($user2);
136
        $this->assertTrue($badge->update($data));
137
        // Check the badge was updated with the correct data.
138
        $this->assertEquals(1, $DB->count_records('badge'));
139
        $this->assertNotEmpty($badge->id);
140
        $this->assertEquals($data->name, $badge->name);
141
        if ($iscourse) {
142
            $this->assertEquals(BADGE_TYPE_COURSE, $badge->type);
143
            $this->assertEquals($course->id, $badge->courseid);
144
        } else {
145
            $this->assertEquals(BADGE_TYPE_SITE, $badge->type);
146
            $this->assertNull($badge->courseid);
147
        }
148
        $this->assertEquals(BADGE_STATUS_ACTIVE, $badge->status);
149
        $this->assertEquals($USER->id, $badge->usermodified);
150
 
151
        if (property_exists($data, 'tags')) {
152
            $this->assertEquals($data->tags, $badge->get_badge_tags());
153
        }
154
 
155
        // Check that the event was triggered.
156
        $events = $sink->get_events();
157
        $event = reset($events);
158
 
159
        // Check that the event data is valid.
160
        $this->assertInstanceOf('\core\event\badge_updated', $event);
161
        $this->assertEquals($badge->usermodified, $event->userid);
162
        $this->assertEquals($badge->id, $event->objectid);
163
        $this->assertDebuggingNotCalled();
164
        $sink->close();
165
    }
166
 
167
    /**
168
     * Test update_message() in badge class.
169
     *
170
     * @dataProvider badges_provider
171
     * @param bool $iscourse Whether the badge is a course badge or not.
172
     * @param array $data Badge data to update the badge with. It will override the default data.
173
     */
174
    public function test_udpate_message_badge(bool $iscourse = false, array $data = []): void {
175
        global $USER, $DB;
176
 
177
        $this->resetAfterTest();
178
 
179
        $record = [];
180
        if ($iscourse) {
181
            $course = $this->getDataGenerator()->create_course();
182
            $record['type'] = BADGE_TYPE_COURSE;
183
            $record['courseid'] = $course->id;
184
        }
185
 
186
        /** @var core_badges_generator $generator */
187
        $generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
188
 
189
        $user1 = $this->getDataGenerator()->create_user();
190
        $user2 = $this->getDataGenerator()->create_user();
191
        $this->setUser($user1);
192
 
193
        /** @var badge $badge */
194
        $badge = $generator->create_badge($record);
195
        $data = (object) array_merge($this->get_badge(), $data);
196
 
197
        // Check the badge has been created.
198
        $this->assertEquals(1, $DB->count_records('badge'));
199
        $this->assertNotEquals($data->name, $badge->name);
200
        $this->assertNotEquals($data->messagesubject, $badge->messagesubject);
201
        $this->assertNotEquals($data->message_editor['text'], $badge->message);
202
        $this->assertEmpty($badge->get_badge_tags());
203
 
204
        // Trigger and capture events.
205
        $sink = $this->redirectEvents();
206
 
207
        $this->setUser($user2);
208
        $this->assertTrue($badge->update_message($data));
209
        // Check the badge was updated with the correct data.
210
        $this->assertEquals(1, $DB->count_records('badge'));
211
        $this->assertNotEmpty($badge->id);
212
        $this->assertNotEquals($data->name, $badge->name);
213
        $this->assertEquals($data->messagesubject, $badge->messagesubject);
214
        $this->assertEquals($data->message_editor['text'], $badge->message);
215
        if ($iscourse) {
216
            $this->assertEquals(BADGE_TYPE_COURSE, $badge->type);
217
            $this->assertEquals($course->id, $badge->courseid);
218
        } else {
219
            $this->assertEquals(BADGE_TYPE_SITE, $badge->type);
220
            $this->assertNull($badge->courseid);
221
        }
222
        $this->assertEquals(BADGE_STATUS_ACTIVE, $badge->status);
223
        $this->assertEquals($user1->id, $badge->usermodified);
224
 
225
        // Check that the event was triggered.
226
        $events = $sink->get_events();
227
        $event = reset($events);
228
 
229
        // Check that the event data is valid.
230
        $this->assertInstanceOf('\core\event\badge_updated', $event);
231
        $this->assertEquals($USER->id, $event->userid);
232
        $this->assertEquals($badge->id, $event->objectid);
233
        $this->assertDebuggingNotCalled();
234
        $sink->close();
235
    }
236
 
237
    /**
238
     * Data provider for badge tests.
239
     *
240
     * @return array
241
     */
242
    public static function badges_provider(): array {
243
        return [
244
            'Site badge' => [
245
            ],
246
            'Site badge with tags' => [
247
                'iscourse' => false,
248
                'data' => [
249
                    'tags' => ['tag1', 'tag2'],
250
                ],
251
            ],
252
            'Course badge' => [
253
                'iscourse' => true,
254
            ],
255
        ];
256
    }
257
 
258
    /**
259
     * Get default badge data for testing purpose.
260
     *
261
     * @return array Badge data.
262
     */
263
    private function get_badge(): array {
264
        global $USER;
265
 
266
        return [
267
            'name' => 'My test badge',
268
            'description' => 'Testing badge description',
269
            'timecreated' => time(),
270
            'timemodified' => time(),
271
            'usercreated' => $USER->id,
272
            'usermodified' => $USER->id,
273
            'issuername' => 'Test issuer',
274
            'issuerurl' => 'http://issuer-url.domain.co.nz',
275
            'issuercontact' => 'issuer@example.com',
276
            'expiry' => 0,
277
            'expiredate' => null,
278
            'expireperiod' => null,
279
            'type' => BADGE_TYPE_SITE,
280
            'courseid' => null,
281
            'messagesubject' => 'The new test message subject',
282
            'messageformat' => '1',
283
            'message_editor' => [
284
                'text' => 'The new test message body',
285
            ],
286
            'attachment' => 1,
287
            'notification' => 0,
288
            'status' => BADGE_STATUS_ACTIVE_LOCKED,
289
            'version' => OPEN_BADGES_V2,
290
            'language' => 'en',
291
            'imagecaption' => 'Image caption',
292
            'tags' => [],
293
        ];
294
    }
295
}