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\tests;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Helper trait for external function tests.
|
|
|
21 |
*
|
|
|
22 |
* @package core_badges
|
|
|
23 |
* @copyright 2025 Moodle Pty Ltd
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
trait external_helper {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Asserts that a badge class returned by an external function matches the given data.
|
|
|
30 |
*
|
|
|
31 |
* @param array $expected Expected badge data.
|
|
|
32 |
* @param array $actual Actual badge class data returned by the external function.
|
|
|
33 |
* @param bool $canconfiguredetails True if user has capability "moodle/badges:configuredetails".
|
|
|
34 |
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
35 |
*/
|
|
|
36 |
private function assert_badge_class(array $expected, array $actual, bool $canconfiguredetails): void {
|
|
|
37 |
$this->assertEquals('BadgeClass', $actual['type']);
|
|
|
38 |
$badgeurl = new \moodle_url('/badges/badgeclass.php', ['id' => $expected['id']]);
|
|
|
39 |
$this->assertEquals($badgeurl->out(false), $actual['id']);
|
|
|
40 |
$this->assertEquals($expected['issuername'], $actual['issuer']);
|
|
|
41 |
$this->assertEquals($expected['name'], $actual['name']);
|
|
|
42 |
$this->assertEquals($expected['badgeurl'], $actual['image']);
|
|
|
43 |
$this->assertEquals($expected['description'], $actual['description']);
|
|
|
44 |
$this->assertEquals($expected['issuerurl'], $actual['hostedUrl']);
|
|
|
45 |
$this->assertEquals($expected['coursefullname'], $actual['coursefullname'] ?? null);
|
|
|
46 |
if ($canconfiguredetails) {
|
|
|
47 |
$this->assertEquals($expected['courseid'], $actual['courseid'] ?? null);
|
|
|
48 |
} else {
|
|
|
49 |
$this->assertArrayNotHasKey('courseid', $actual);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$alignments = $expected['alignment'];
|
|
|
53 |
if (!$canconfiguredetails) {
|
|
|
54 |
foreach ($alignments as $index => $alignment) {
|
|
|
55 |
$alignments[$index] = [
|
|
|
56 |
'id' => $alignment['id'],
|
|
|
57 |
'badgeid' => $alignment['badgeid'],
|
|
|
58 |
'targetName' => $alignment['targetName'],
|
|
|
59 |
'targetUrl' => $alignment['targetUrl'],
|
|
|
60 |
];
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
$this->assertEquals($alignments, $actual['alignment'] ?? []);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Asserts that an issued badge returned by an external function matches the given data.
|
|
|
68 |
*
|
|
|
69 |
* @param array $expected Expected badge data.
|
|
|
70 |
* @param array $actual Actual badge data returned by the external function.
|
|
|
71 |
* @param bool $isrecipient True if user is the badge recipient.
|
|
|
72 |
* @param bool $canconfiguredetails True if user has capability "moodle/badges:configuredetails".
|
|
|
73 |
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
74 |
*/
|
|
|
75 |
protected function assert_issued_badge(array $expected, array $actual, bool $isrecipient, bool $canconfiguredetails): void {
|
|
|
76 |
$this->assertEquals($expected['id'], $actual['id']);
|
|
|
77 |
$this->assertEquals($expected['name'], $actual['name']);
|
|
|
78 |
$this->assertEquals($expected['type'], $actual['type']);
|
|
|
79 |
$this->assertEquals($expected['description'], $actual['description']);
|
|
|
80 |
$this->assertEquals($expected['issuername'], $actual['issuername']);
|
|
|
81 |
$this->assertEquals($expected['issuerurl'], $actual['issuerurl']);
|
|
|
82 |
$this->assertEquals($expected['issuercontact'], $actual['issuercontact']);
|
|
|
83 |
$this->assertEquals($expected['uniquehash'], $actual['uniquehash']);
|
|
|
84 |
$this->assertEquals($expected['dateissued'], $actual['dateissued']);
|
|
|
85 |
$this->assertEquals($expected['dateexpire'], $actual['dateexpire']);
|
|
|
86 |
$this->assertEquals($expected['version'], $actual['version']);
|
|
|
87 |
$this->assertEquals($expected['language'], $actual['language']);
|
|
|
88 |
$this->assertEquals($expected['imagecaption'], $actual['imagecaption']);
|
|
|
89 |
$this->assertEquals($expected['badgeurl'], $actual['badgeurl']);
|
|
|
90 |
$this->assertEquals($expected['recipientid'], $actual['recipientid']);
|
|
|
91 |
$this->assertEquals($expected['recipientfullname'], $actual['recipientfullname']);
|
|
|
92 |
$this->assertEquals($expected['coursefullname'], $actual['coursefullname'] ?? null);
|
|
|
93 |
$this->assertEquals($expected['endorsement'] ?? null, $actual['endorsement'] ?? null);
|
|
|
94 |
|
|
|
95 |
if ($isrecipient || $canconfiguredetails) {
|
|
|
96 |
$this->assertTimeCurrent($expected['timecreated']);
|
|
|
97 |
$this->assertTimeCurrent($expected['timemodified']);
|
|
|
98 |
$this->assertEquals($expected['usercreated'], $actual['usercreated']);
|
|
|
99 |
$this->assertEquals($expected['usermodified'], $actual['usermodified']);
|
|
|
100 |
$this->assertEquals($expected['expiredate'], $actual['expiredate']);
|
|
|
101 |
$this->assertEquals($expected['expireperiod'], $actual['expireperiod']);
|
|
|
102 |
$this->assertEquals($expected['courseid'], $actual['courseid']);
|
|
|
103 |
$this->assertEquals($expected['message'], $actual['message']);
|
|
|
104 |
$this->assertEquals($expected['messagesubject'], $actual['messagesubject']);
|
|
|
105 |
$this->assertEquals($expected['attachment'], $actual['attachment']);
|
|
|
106 |
$this->assertEquals($expected['notification'], $actual['notification']);
|
|
|
107 |
$this->assertEquals($expected['nextcron'], $actual['nextcron']);
|
|
|
108 |
$this->assertEquals($expected['status'], $actual['status']);
|
|
|
109 |
$this->assertEquals($expected['issuedid'], $actual['issuedid']);
|
|
|
110 |
$this->assertEquals($expected['visible'], $actual['visible']);
|
|
|
111 |
$this->assertEquals($expected['email'], $actual['email']);
|
|
|
112 |
} else {
|
|
|
113 |
$this->assertEquals(0, $actual['timecreated']);
|
|
|
114 |
$this->assertEquals(0, $actual['timemodified']);
|
|
|
115 |
$this->assertArrayNotHasKey('usercreated', $actual);
|
|
|
116 |
$this->assertArrayNotHasKey('usermodified', $actual);
|
|
|
117 |
$this->assertArrayNotHasKey('expiredate', $actual);
|
|
|
118 |
$this->assertArrayNotHasKey('expireperiod', $actual);
|
|
|
119 |
$this->assertArrayNotHasKey('courseid', $actual);
|
|
|
120 |
$this->assertArrayNotHasKey('message', $actual);
|
|
|
121 |
$this->assertArrayNotHasKey('messagesubject', $actual);
|
|
|
122 |
$this->assertEquals(1, $actual['attachment']);
|
|
|
123 |
$this->assertEquals(1, $actual['notification']);
|
|
|
124 |
$this->assertArrayNotHasKey('nextcron', $actual);
|
|
|
125 |
$this->assertEquals(0, $actual['status']);
|
|
|
126 |
$this->assertArrayNotHasKey('issuedid', $actual);
|
|
|
127 |
$this->assertEquals(0, $actual['visible']);
|
|
|
128 |
$this->assertArrayNotHasKey('email', $actual);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$alignments = $expected['alignment'];
|
|
|
132 |
if (!$canconfiguredetails) {
|
|
|
133 |
foreach ($alignments as $index => $alignment) {
|
|
|
134 |
$alignments[$index] = [
|
|
|
135 |
'id' => $alignment['id'],
|
|
|
136 |
'badgeid' => $alignment['badgeid'],
|
|
|
137 |
'targetName' => $alignment['targetName'],
|
|
|
138 |
'targetUrl' => $alignment['targetUrl'],
|
|
|
139 |
];
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
$this->assertEquals($alignments, $actual['alignment']);
|
|
|
143 |
|
|
|
144 |
$relatedbadges = $expected['relatedbadges'];
|
|
|
145 |
if (!$canconfiguredetails) {
|
|
|
146 |
foreach ($relatedbadges as $index => $relatedbadge) {
|
|
|
147 |
$relatedbadges[$index] = [
|
|
|
148 |
'id' => $relatedbadge['id'],
|
|
|
149 |
'name' => $relatedbadge['name'],
|
|
|
150 |
];
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
$this->assertEquals($relatedbadges, $actual['relatedbadges']);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Creates and returns test data for external functions.
|
|
|
158 |
*
|
|
|
159 |
* The test data includes:
|
|
|
160 |
* - A teacher.
|
|
|
161 |
* - A student.
|
|
|
162 |
* - A site badge, with an endorsment and 2 alignments, issued to the student by the teacher.
|
|
|
163 |
* - A course badge, related to the site badge, issued to the student by the teacher.
|
|
|
164 |
*
|
|
|
165 |
* @return array
|
|
|
166 |
*/
|
|
|
167 |
private function prepare_test_data(): array {
|
|
|
168 |
global $DB;
|
|
|
169 |
|
|
|
170 |
$this->resetAfterTest();
|
|
|
171 |
$this->setAdminUser();
|
|
|
172 |
$generator = $this->getDataGenerator();
|
|
|
173 |
|
|
|
174 |
// Setup test data.
|
|
|
175 |
$course = $generator->create_course();
|
|
|
176 |
|
|
|
177 |
// Create users and enrolments.
|
|
|
178 |
$student = $generator->create_and_enrol($course);
|
|
|
179 |
$teacher = $generator->create_and_enrol($course, 'editingteacher');
|
|
|
180 |
$badgegenerator = $generator->get_plugin_generator('core_badges');
|
|
|
181 |
|
|
|
182 |
$systemcontext = \context_system::instance();
|
|
|
183 |
$coursecontext = \context_course::instance($course->id);
|
|
|
184 |
|
|
|
185 |
// Create a site badge.
|
|
|
186 |
$now = time();
|
|
|
187 |
$sitebadge = $badgegenerator->create_badge([
|
|
|
188 |
'name' => "Test badge site",
|
|
|
189 |
'description' => "Testing badges site",
|
|
|
190 |
'timecreated' => $now,
|
|
|
191 |
'timemodified' => $now,
|
|
|
192 |
'usercreated' => (int) $teacher->id,
|
|
|
193 |
'usermodified' => (int) $teacher->id,
|
|
|
194 |
'expiredate' => $now + YEARSECS,
|
|
|
195 |
'expireperiod' => YEARSECS,
|
|
|
196 |
'type' => BADGE_TYPE_SITE,
|
|
|
197 |
]);
|
|
|
198 |
|
|
|
199 |
$sitebadge->issue($student->id, true);
|
|
|
200 |
$siteissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $sitebadge->id ]);
|
|
|
201 |
|
|
|
202 |
// Change issued time to ensure badges are fetched in a consistent order.
|
|
|
203 |
$siteissuedbadge->dateissued = $now - 1;
|
|
|
204 |
$DB->update_record('badge_issued', $siteissuedbadge);
|
|
|
205 |
|
|
|
206 |
$sitebadgedata = [
|
|
|
207 |
...(array) $sitebadge,
|
|
|
208 |
'issuedid' => (int) $siteissuedbadge->id,
|
|
|
209 |
'uniquehash' => $siteissuedbadge->uniquehash,
|
|
|
210 |
'dateissued' => (int) $siteissuedbadge->dateissued,
|
|
|
211 |
'dateexpire' => $siteissuedbadge->dateexpire,
|
|
|
212 |
'visible' => (int) $siteissuedbadge->visible,
|
|
|
213 |
'badgeurl' => \moodle_url::make_webservice_pluginfile_url($systemcontext->id, 'badges', 'badgeimage',
|
|
|
214 |
$sitebadge->id, '/', 'f3')->out(false),
|
|
|
215 |
'recipientid' => $student->id,
|
|
|
216 |
'recipientfullname' => fullname($student),
|
|
|
217 |
'email' => $student->email,
|
|
|
218 |
'coursefullname' => null,
|
|
|
219 |
'endorsement' => null,
|
|
|
220 |
'alignment' => [],
|
|
|
221 |
'relatedbadges' => [],
|
|
|
222 |
];
|
|
|
223 |
|
|
|
224 |
// Add an endorsement for the site badge.
|
|
|
225 |
$endorsement = new \stdClass();
|
|
|
226 |
$endorsement->badgeid = $sitebadge->id;
|
|
|
227 |
$endorsement->issuername = 'Issuer name';
|
|
|
228 |
$endorsement->issuerurl = 'http://endorsement-issuer-url.domain.co.nz';
|
|
|
229 |
$endorsement->issueremail = 'endorsementissuer@example.com';
|
|
|
230 |
$endorsement->claimid = 'http://claim-url.domain.co.nz';
|
|
|
231 |
$endorsement->claimcomment = 'Claim comment';
|
|
|
232 |
$endorsement->dateissued = $now;
|
|
|
233 |
$endorsement->id = $sitebadge->save_endorsement($endorsement);
|
|
|
234 |
$sitebadgedata['endorsement'] = (array) $endorsement;
|
|
|
235 |
|
|
|
236 |
// Add 2 alignments to the site badge.
|
|
|
237 |
$alignment = new \stdClass();
|
|
|
238 |
$alignment->badgeid = $sitebadge->id;
|
|
|
239 |
$alignment->targetname = 'Alignment 1';
|
|
|
240 |
$alignment->targeturl = 'http://a1-target-url.domain.co.nz';
|
|
|
241 |
$alignment->targetdescription = 'A1 target description';
|
|
|
242 |
$alignment->targetframework = 'A1 framework';
|
|
|
243 |
$alignment->targetcode = 'A1 code';
|
|
|
244 |
$alignment->id = $sitebadge->save_alignment($alignment);
|
|
|
245 |
$sitebadgedata['alignment'][] = [
|
|
|
246 |
'id' => $alignment->id,
|
|
|
247 |
'badgeid' => $alignment->badgeid,
|
|
|
248 |
'targetName' => $alignment->targetname,
|
|
|
249 |
'targetUrl' => $alignment->targeturl,
|
|
|
250 |
'targetDescription' => $alignment->targetdescription,
|
|
|
251 |
'targetFramework' => $alignment->targetframework,
|
|
|
252 |
'targetCode' => $alignment->targetcode,
|
|
|
253 |
];
|
|
|
254 |
|
|
|
255 |
$alignment = new \stdClass();
|
|
|
256 |
$alignment->badgeid = $sitebadge->id;
|
|
|
257 |
$alignment->targetname = 'Alignment 2';
|
|
|
258 |
$alignment->targeturl = 'http://a2-target-url.domain.co.nz';
|
|
|
259 |
$alignment->targetdescription = 'A2 target description';
|
|
|
260 |
$alignment->targetframework = 'A2 framework';
|
|
|
261 |
$alignment->targetcode = 'A2 code';
|
|
|
262 |
$alignment->id = $sitebadge->save_alignment($alignment);
|
|
|
263 |
$sitebadgedata['alignment'][] = [
|
|
|
264 |
'id' => $alignment->id,
|
|
|
265 |
'badgeid' => $alignment->badgeid,
|
|
|
266 |
'targetName' => $alignment->targetname,
|
|
|
267 |
'targetUrl' => $alignment->targeturl,
|
|
|
268 |
'targetDescription' => $alignment->targetdescription,
|
|
|
269 |
'targetFramework' => $alignment->targetframework,
|
|
|
270 |
'targetCode' => $alignment->targetcode,
|
|
|
271 |
];
|
|
|
272 |
|
|
|
273 |
// Create a course badge.
|
|
|
274 |
$coursebadge = $badgegenerator->create_badge([
|
|
|
275 |
'name' => "Test badge course",
|
|
|
276 |
'description' => "Testing badges course",
|
|
|
277 |
'timecreated' => $now,
|
|
|
278 |
'timemodified' => $now,
|
|
|
279 |
'usercreated' => (int) $teacher->id,
|
|
|
280 |
'usermodified' => (int) $teacher->id,
|
|
|
281 |
'expiredate' => $now + YEARSECS,
|
|
|
282 |
'expireperiod' => YEARSECS,
|
|
|
283 |
'type' => BADGE_TYPE_COURSE,
|
|
|
284 |
'courseid' => (int) $course->id,
|
|
|
285 |
]);
|
|
|
286 |
$coursebadge->issue($student->id, true);
|
|
|
287 |
|
|
|
288 |
$courseissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $coursebadge->id ]);
|
|
|
289 |
$coursebadgedata = [
|
|
|
290 |
...(array) $coursebadge,
|
|
|
291 |
'issuedid' => (int) $courseissuedbadge->id,
|
|
|
292 |
'uniquehash' => $courseissuedbadge->uniquehash,
|
|
|
293 |
'dateissued' => (int) $courseissuedbadge->dateissued,
|
|
|
294 |
'dateexpire' => $courseissuedbadge->dateexpire,
|
|
|
295 |
'visible' => (int) $courseissuedbadge->visible,
|
|
|
296 |
'badgeurl' => \moodle_url::make_webservice_pluginfile_url($coursecontext->id, 'badges', 'badgeimage',
|
|
|
297 |
$coursebadge->id, '/', 'f3')->out(false),
|
|
|
298 |
'recipientid' => $student->id,
|
|
|
299 |
'recipientfullname' => fullname($student),
|
|
|
300 |
'email' => $student->email,
|
|
|
301 |
'coursefullname' => \core_external\util::format_string($course->fullname, $coursecontext),
|
|
|
302 |
'endorsement' => null,
|
|
|
303 |
'alignment' => [],
|
|
|
304 |
'relatedbadges' => [],
|
|
|
305 |
];
|
|
|
306 |
|
|
|
307 |
// Add the course badge to the site badge.
|
|
|
308 |
$sitebadge->add_related_badges([$coursebadge->id]);
|
|
|
309 |
$sitebadgedata['relatedbadges'][] = [
|
|
|
310 |
'id' => (int) $coursebadge->id,
|
|
|
311 |
'name' => $coursebadge->name,
|
|
|
312 |
'version' => $coursebadge->version,
|
|
|
313 |
'language' => $coursebadge->language,
|
|
|
314 |
'type' => $coursebadge->type,
|
|
|
315 |
];
|
|
|
316 |
$coursebadgedata['relatedbadges'][] = [
|
|
|
317 |
'id' => (int) $sitebadge->id,
|
|
|
318 |
'name' => $sitebadge->name,
|
|
|
319 |
'version' => $sitebadge->version,
|
|
|
320 |
'language' => $sitebadge->language,
|
|
|
321 |
'type' => $sitebadge->type,
|
|
|
322 |
];
|
|
|
323 |
|
|
|
324 |
return [
|
|
|
325 |
'coursebadge' => $coursebadgedata,
|
|
|
326 |
'sitebadge' => $sitebadgedata,
|
|
|
327 |
'student' => $student,
|
|
|
328 |
'teacher' => $teacher,
|
|
|
329 |
];
|
|
|
330 |
}
|
|
|
331 |
}
|