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 |
/**
|
|
|
18 |
* Unit tests for badges
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @subpackage badges
|
|
|
22 |
* @copyright 2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
global $CFG;
|
|
|
30 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
31 |
require_once($CFG->dirroot . '/badges/lib.php');
|
|
|
32 |
|
|
|
33 |
use core_badges\helper;
|
|
|
34 |
use core\task\manager;
|
|
|
35 |
|
|
|
36 |
class badgeslib_test extends advanced_testcase {
|
|
|
37 |
protected $badgeid;
|
|
|
38 |
protected $course;
|
|
|
39 |
protected $user;
|
|
|
40 |
protected $module;
|
|
|
41 |
protected $coursebadge;
|
|
|
42 |
protected $assertion;
|
|
|
43 |
|
|
|
44 |
/** @var $assertion2 to define json format for Open badge version 2 */
|
|
|
45 |
protected $assertion2;
|
|
|
46 |
|
|
|
47 |
protected function setUp(): void {
|
|
|
48 |
global $DB, $CFG;
|
|
|
49 |
$this->resetAfterTest(true);
|
|
|
50 |
$CFG->enablecompletion = true;
|
|
|
51 |
$user = $this->getDataGenerator()->create_user();
|
|
|
52 |
$fordb = new stdClass();
|
|
|
53 |
$fordb->id = null;
|
|
|
54 |
$fordb->name = "Test badge with 'apostrophe' and other friends (<>&@#)";
|
|
|
55 |
$fordb->description = "Testing badges";
|
|
|
56 |
$fordb->timecreated = time();
|
|
|
57 |
$fordb->timemodified = time();
|
|
|
58 |
$fordb->usercreated = $user->id;
|
|
|
59 |
$fordb->usermodified = $user->id;
|
|
|
60 |
$fordb->issuername = "Test issuer";
|
|
|
61 |
$fordb->issuerurl = "http://issuer-url.domain.co.nz";
|
|
|
62 |
$fordb->issuercontact = "issuer@example.com";
|
|
|
63 |
$fordb->expiredate = null;
|
|
|
64 |
$fordb->expireperiod = null;
|
|
|
65 |
$fordb->type = BADGE_TYPE_SITE;
|
|
|
66 |
$fordb->version = 1;
|
|
|
67 |
$fordb->language = 'en';
|
|
|
68 |
$fordb->courseid = null;
|
|
|
69 |
$fordb->messagesubject = "Test message subject";
|
|
|
70 |
$fordb->message = "Test message body";
|
|
|
71 |
$fordb->attachment = 1;
|
|
|
72 |
$fordb->notification = 0;
|
|
|
73 |
$fordb->imageauthorname = "Image Author 1";
|
|
|
74 |
$fordb->imageauthoremail = "author@example.com";
|
|
|
75 |
$fordb->imageauthorurl = "http://author-url.example.com";
|
|
|
76 |
$fordb->imagecaption = "Test caption image";
|
|
|
77 |
$fordb->status = BADGE_STATUS_INACTIVE;
|
|
|
78 |
|
|
|
79 |
$this->badgeid = $DB->insert_record('badge', $fordb, true);
|
|
|
80 |
|
|
|
81 |
// Set the default Issuer (because OBv2 needs them).
|
|
|
82 |
set_config('badges_defaultissuername', $fordb->issuername);
|
|
|
83 |
set_config('badges_defaultissuercontact', $fordb->issuercontact);
|
|
|
84 |
|
|
|
85 |
// Create a course with activity and auto completion tracking.
|
|
|
86 |
$this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
|
|
87 |
$this->user = $this->getDataGenerator()->create_user();
|
|
|
88 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
89 |
$this->assertNotEmpty($studentrole);
|
|
|
90 |
|
|
|
91 |
// Get manual enrolment plugin and enrol user.
|
|
|
92 |
require_once($CFG->dirroot.'/enrol/manual/locallib.php');
|
|
|
93 |
$manplugin = enrol_get_plugin('manual');
|
|
|
94 |
$maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
|
|
|
95 |
$manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
|
|
|
96 |
$this->assertEquals(1, $DB->count_records('user_enrolments'));
|
|
|
97 |
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
|
|
|
98 |
$this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
|
|
|
99 |
|
|
|
100 |
// Build badge and criteria.
|
|
|
101 |
$fordb->type = BADGE_TYPE_COURSE;
|
|
|
102 |
$fordb->courseid = $this->course->id;
|
|
|
103 |
$fordb->status = BADGE_STATUS_ACTIVE;
|
|
|
104 |
$this->coursebadge = $DB->insert_record('badge', $fordb, true);
|
|
|
105 |
|
|
|
106 |
// Insert Endorsement.
|
|
|
107 |
$endorsement = new stdClass();
|
|
|
108 |
$endorsement->badgeid = $this->coursebadge;
|
|
|
109 |
$endorsement->issuername = "Issuer 123";
|
|
|
110 |
$endorsement->issueremail = "issuer123@email.com";
|
|
|
111 |
$endorsement->issuerurl = "https://example.org/issuer-123";
|
|
|
112 |
$endorsement->dateissued = 1524567747;
|
|
|
113 |
$endorsement->claimid = "https://example.org/robotics-badge.json";
|
|
|
114 |
$endorsement->claimcomment = "Test endorser comment";
|
|
|
115 |
$DB->insert_record('badge_endorsement', $endorsement, true);
|
|
|
116 |
|
|
|
117 |
// Insert related badges.
|
|
|
118 |
$badge = new badge($this->coursebadge);
|
|
|
119 |
$clonedid = $badge->make_clone();
|
|
|
120 |
$badgeclone = new badge($clonedid);
|
|
|
121 |
$badgeclone->status = BADGE_STATUS_ACTIVE;
|
|
|
122 |
$badgeclone->save();
|
|
|
123 |
|
|
|
124 |
$relatebadge = new stdClass();
|
|
|
125 |
$relatebadge->badgeid = $this->coursebadge;
|
|
|
126 |
$relatebadge->relatedbadgeid = $clonedid;
|
|
|
127 |
$relatebadge->relatedid = $DB->insert_record('badge_related', $relatebadge, true);
|
|
|
128 |
|
|
|
129 |
// Insert a aligment.
|
|
|
130 |
$alignment = new stdClass();
|
|
|
131 |
$alignment->badgeid = $this->coursebadge;
|
|
|
132 |
$alignment->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
|
|
|
133 |
$alignment->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
|
|
|
134 |
$alignment->targetdescription = 'Test target description';
|
|
|
135 |
$alignment->targetframework = 'CCSS.RST.11-12.3';
|
|
|
136 |
$alignment->targetcode = 'CCSS.RST.11-12.3';
|
|
|
137 |
$DB->insert_record('badge_alignment', $alignment, true);
|
|
|
138 |
|
|
|
139 |
// Insert tags.
|
|
|
140 |
core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), ['tag1', 'tag2']);
|
|
|
141 |
|
|
|
142 |
$this->assertion = new stdClass();
|
|
|
143 |
$this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
|
|
|
144 |
'"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s","tags":%s}';
|
|
|
145 |
$this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s","tags":%s}';
|
|
|
146 |
$this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
|
|
|
147 |
// Format JSON-LD for Openbadge specification version 2.0.
|
|
|
148 |
$this->assertion2 = new stdClass();
|
|
|
149 |
$this->assertion2->badge = '{"recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
|
|
|
150 |
'"badge":{"name":"%s","description":"%s","image":"%s",' .
|
|
|
151 |
'"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
|
|
|
152 |
'"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
|
|
|
153 |
'"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
|
|
|
154 |
'"@language":"en","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
|
|
|
155 |
'"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
|
|
|
156 |
'"targetCode":"%s"}]},"verify":{"type":"hosted","url":"%s"},"issuedOn":"%s","evidence":"%s","tags":%s,' .
|
|
|
157 |
'"@context":"https:\/\/w3id.org\/openbadges\/v2","type":"Assertion","id":"%s"}';
|
|
|
158 |
|
|
|
159 |
$this->assertion2->class = '{"name":"%s","description":"%s","image":"%s",' .
|
|
|
160 |
'"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
|
|
|
161 |
'"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
|
|
|
162 |
'"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
|
|
|
163 |
'"@language":"%s","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
|
|
|
164 |
'"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
|
|
|
165 |
'"targetCode":"%s"}]}';
|
|
|
166 |
$this->assertion2->issuer = '{"name":"%s","url":"%s","email":"%s",' .
|
|
|
167 |
'"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"}';
|
|
|
168 |
}
|
|
|
169 |
|
11 |
efrain |
170 |
public function test_create_badge(): void {
|
1 |
efrain |
171 |
$badge = new badge($this->badgeid);
|
|
|
172 |
|
|
|
173 |
$this->assertInstanceOf('badge', $badge);
|
|
|
174 |
$this->assertEquals($this->badgeid, $badge->id);
|
|
|
175 |
}
|
|
|
176 |
|
11 |
efrain |
177 |
public function test_clone_badge(): void {
|
1 |
efrain |
178 |
$badge = new badge($this->badgeid);
|
|
|
179 |
$newid = $badge->make_clone();
|
|
|
180 |
$clonedbadge = new badge($newid);
|
|
|
181 |
|
|
|
182 |
$this->assertEquals($badge->description, $clonedbadge->description);
|
|
|
183 |
$this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
|
|
|
184 |
$this->assertEquals($badge->issuername, $clonedbadge->issuername);
|
|
|
185 |
$this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
|
|
|
186 |
$this->assertEquals($badge->issuerurl, $clonedbadge->issuerurl);
|
|
|
187 |
$this->assertEquals($badge->expiredate, $clonedbadge->expiredate);
|
|
|
188 |
$this->assertEquals($badge->expireperiod, $clonedbadge->expireperiod);
|
|
|
189 |
$this->assertEquals($badge->type, $clonedbadge->type);
|
|
|
190 |
$this->assertEquals($badge->courseid, $clonedbadge->courseid);
|
|
|
191 |
$this->assertEquals($badge->message, $clonedbadge->message);
|
|
|
192 |
$this->assertEquals($badge->messagesubject, $clonedbadge->messagesubject);
|
|
|
193 |
$this->assertEquals($badge->attachment, $clonedbadge->attachment);
|
|
|
194 |
$this->assertEquals($badge->notification, $clonedbadge->notification);
|
|
|
195 |
$this->assertEquals($badge->version, $clonedbadge->version);
|
|
|
196 |
$this->assertEquals($badge->language, $clonedbadge->language);
|
|
|
197 |
$this->assertEquals($badge->imagecaption, $clonedbadge->imagecaption);
|
|
|
198 |
$this->assertEquals($badge->imageauthorname, $clonedbadge->imageauthorname);
|
|
|
199 |
$this->assertEquals($badge->imageauthoremail, $clonedbadge->imageauthoremail);
|
|
|
200 |
$this->assertEquals($badge->imageauthorurl, $clonedbadge->imageauthorurl);
|
|
|
201 |
}
|
|
|
202 |
|
11 |
efrain |
203 |
public function test_badge_status(): void {
|
1 |
efrain |
204 |
$badge = new badge($this->badgeid);
|
|
|
205 |
$old_status = $badge->status;
|
|
|
206 |
$badge->set_status(BADGE_STATUS_ACTIVE);
|
|
|
207 |
$this->assertNotEquals($old_status, $badge->status);
|
|
|
208 |
$this->assertEquals(BADGE_STATUS_ACTIVE, $badge->status);
|
|
|
209 |
}
|
|
|
210 |
|
11 |
efrain |
211 |
public function test_delete_badge(): void {
|
1 |
efrain |
212 |
$badge = new badge($this->badgeid);
|
|
|
213 |
$badge->delete();
|
|
|
214 |
// We don't actually delete badges. We archive them.
|
|
|
215 |
$this->assertEquals(BADGE_STATUS_ARCHIVED, $badge->status);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Really delete the badge.
|
|
|
220 |
*/
|
11 |
efrain |
221 |
public function test_delete_badge_for_real(): void {
|
1 |
efrain |
222 |
global $DB;
|
|
|
223 |
|
|
|
224 |
$badge = new badge($this->badgeid);
|
|
|
225 |
// Insert tags for the badge.
|
|
|
226 |
core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), ['tag1', 'tag2']);
|
|
|
227 |
|
|
|
228 |
$newid1 = $badge->make_clone();
|
|
|
229 |
$newid2 = $badge->make_clone();
|
|
|
230 |
$newid3 = $badge->make_clone();
|
|
|
231 |
|
|
|
232 |
// Insert related badges to badge 1.
|
|
|
233 |
$badge->add_related_badges([$newid1, $newid2, $newid3]);
|
|
|
234 |
|
|
|
235 |
// Another badge.
|
|
|
236 |
$badge2 = new badge($newid2);
|
|
|
237 |
// Make badge 1 related for badge 2.
|
|
|
238 |
$badge2->add_related_badges([$this->badgeid]);
|
|
|
239 |
|
|
|
240 |
// Confirm that the records about this badge about its relations have been removed as well.
|
|
|
241 |
$relatedsql = 'badgeid = :badgeid OR relatedbadgeid = :relatedbadgeid';
|
|
|
242 |
$relatedparams = array(
|
|
|
243 |
'badgeid' => $this->badgeid,
|
|
|
244 |
'relatedbadgeid' => $this->badgeid
|
|
|
245 |
);
|
|
|
246 |
// Badge 1 has 4 related records. 3 where it's the badgeid, 1 where it's the relatedbadgeid.
|
|
|
247 |
$this->assertEquals(4, $DB->count_records_select('badge_related', $relatedsql, $relatedparams));
|
|
|
248 |
|
|
|
249 |
// Badge has 2 tag instance records.
|
|
|
250 |
$this->assertEquals(2, $DB->count_records('tag_instance', ['itemid' => $this->badgeid]));
|
|
|
251 |
|
|
|
252 |
// Delete the badge for real.
|
|
|
253 |
$badge->delete(false);
|
|
|
254 |
|
|
|
255 |
// Confirm that the badge itself has been removed.
|
|
|
256 |
$this->assertFalse($DB->record_exists('badge', ['id' => $this->badgeid]));
|
|
|
257 |
|
|
|
258 |
// Confirm that the records about this badge about its relations have been removed as well.
|
|
|
259 |
$this->assertFalse($DB->record_exists_select('badge_related', $relatedsql, $relatedparams));
|
|
|
260 |
|
|
|
261 |
// Confirm that the tag instance of the badge has been removed.
|
|
|
262 |
$this->assertFalse($DB->record_exists('tag_instance', ['itemid' => $this->badgeid]));
|
|
|
263 |
}
|
|
|
264 |
|
11 |
efrain |
265 |
public function test_create_badge_criteria(): void {
|
1 |
efrain |
266 |
$badge = new badge($this->badgeid);
|
|
|
267 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
268 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
|
|
|
269 |
|
|
|
270 |
$this->assertCount(1, $badge->get_criteria());
|
|
|
271 |
|
|
|
272 |
$criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
|
|
|
273 |
$params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
|
|
|
274 |
$criteria_profile->save($params);
|
|
|
275 |
|
|
|
276 |
$this->assertCount(2, $badge->get_criteria());
|
|
|
277 |
}
|
|
|
278 |
|
11 |
efrain |
279 |
public function test_add_badge_criteria_description(): void {
|
1 |
efrain |
280 |
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
|
|
|
281 |
$criteriaoverall->save(array(
|
|
|
282 |
'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
|
|
|
283 |
'description' => 'Overall description',
|
|
|
284 |
'descriptionformat' => FORMAT_HTML
|
|
|
285 |
));
|
|
|
286 |
|
|
|
287 |
$criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
|
|
|
288 |
$params = array(
|
|
|
289 |
'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
|
|
|
290 |
'field_address' => 'address',
|
|
|
291 |
'description' => 'Description',
|
|
|
292 |
'descriptionformat' => FORMAT_HTML
|
|
|
293 |
);
|
|
|
294 |
$criteriaprofile->save($params);
|
|
|
295 |
|
|
|
296 |
$badge = new badge($this->badgeid);
|
|
|
297 |
$this->assertEquals('Overall description', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->description);
|
|
|
298 |
$this->assertEquals('Description', $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->description);
|
|
|
299 |
}
|
|
|
300 |
|
11 |
efrain |
301 |
public function test_delete_badge_criteria(): void {
|
1 |
efrain |
302 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
|
|
|
303 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
|
|
|
304 |
$badge = new badge($this->badgeid);
|
|
|
305 |
|
|
|
306 |
$this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
|
|
|
307 |
|
|
|
308 |
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
|
|
|
309 |
$this->assertEmpty($badge->get_criteria());
|
|
|
310 |
}
|
|
|
311 |
|
11 |
efrain |
312 |
public function test_badge_awards(): void {
|
1 |
efrain |
313 |
global $DB;
|
|
|
314 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
315 |
$badge = new badge($this->badgeid);
|
|
|
316 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
317 |
|
|
|
318 |
$sink = $this->redirectMessages();
|
|
|
319 |
|
|
|
320 |
$DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
|
|
|
321 |
set_user_preference('message_provider_moodle_badgerecipientnotice_enabled', 'email', $user1);
|
|
|
322 |
|
|
|
323 |
$badge->issue($user1->id, false);
|
|
|
324 |
$this->assertDebuggingCalled(); // Expect debugging while baking a badge via phpunit.
|
|
|
325 |
$this->assertTrue($badge->is_issued($user1->id));
|
|
|
326 |
|
|
|
327 |
$messages = $sink->get_messages();
|
|
|
328 |
$sink->close();
|
|
|
329 |
$this->assertCount(1, $messages);
|
|
|
330 |
$message = array_pop($messages);
|
|
|
331 |
// Check we have the expected data.
|
|
|
332 |
$customdata = json_decode($message->customdata);
|
|
|
333 |
$this->assertObjectHasProperty('notificationiconurl', $customdata);
|
|
|
334 |
$this->assertObjectHasProperty('hash', $customdata);
|
|
|
335 |
|
|
|
336 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
337 |
$badge->issue($user2->id, true);
|
|
|
338 |
$this->assertTrue($badge->is_issued($user2->id));
|
|
|
339 |
|
|
|
340 |
$this->assertCount(2, $badge->get_awards());
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Test the {@link badges_get_user_badges()} function in lib/badgeslib.php
|
|
|
345 |
*/
|
11 |
efrain |
346 |
public function test_badges_get_user_badges(): void {
|
1 |
efrain |
347 |
global $DB;
|
|
|
348 |
|
|
|
349 |
// Messaging is not compatible with transactions.
|
|
|
350 |
$this->preventResetByRollback();
|
|
|
351 |
|
|
|
352 |
$badges = array();
|
|
|
353 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
354 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
355 |
|
|
|
356 |
// Record the current time, we need to be precise about a couple of things.
|
|
|
357 |
$now = time();
|
|
|
358 |
// Create 11 badges with which to test.
|
|
|
359 |
for ($i = 1; $i <= 11; $i++) {
|
|
|
360 |
// Mock up a badge.
|
|
|
361 |
$badge = new stdClass();
|
|
|
362 |
$badge->id = null;
|
|
|
363 |
$badge->name = "Test badge $i";
|
|
|
364 |
$badge->description = "Testing badges $i";
|
|
|
365 |
$badge->timecreated = $now - 12;
|
|
|
366 |
$badge->timemodified = $now - 12;
|
|
|
367 |
$badge->usercreated = $user1->id;
|
|
|
368 |
$badge->usermodified = $user1->id;
|
|
|
369 |
$badge->issuername = "Test issuer";
|
|
|
370 |
$badge->issuerurl = "http://issuer-url.domain.co.nz";
|
|
|
371 |
$badge->issuercontact = "issuer@example.com";
|
|
|
372 |
$badge->expiredate = null;
|
|
|
373 |
$badge->expireperiod = null;
|
|
|
374 |
$badge->type = BADGE_TYPE_SITE;
|
|
|
375 |
$badge->courseid = null;
|
|
|
376 |
$badge->messagesubject = "Test message subject for badge $i";
|
|
|
377 |
$badge->message = "Test message body for badge $i";
|
|
|
378 |
$badge->attachment = 1;
|
|
|
379 |
$badge->notification = 0;
|
|
|
380 |
$badge->status = BADGE_STATUS_INACTIVE;
|
|
|
381 |
$badge->version = "Version $i";
|
|
|
382 |
$badge->language = "en";
|
|
|
383 |
$badge->imagecaption = "Image caption $i";
|
|
|
384 |
$badge->imageauthorname = "Image author's name $i";
|
|
|
385 |
$badge->imageauthoremail = "author$i@example.com";
|
|
|
386 |
$badge->imageauthorname = "Image author's name $i";
|
|
|
387 |
|
|
|
388 |
$badgeid = $DB->insert_record('badge', $badge, true);
|
|
|
389 |
$badges[$badgeid] = new badge($badgeid);
|
|
|
390 |
$badges[$badgeid]->issue($user2->id, true);
|
|
|
391 |
// Check it all actually worked.
|
|
|
392 |
$this->assertCount(1, $badges[$badgeid]->get_awards());
|
|
|
393 |
|
|
|
394 |
// Hack the database to adjust the time each badge was issued.
|
|
|
395 |
// The alternative to this is sleep which is a no-no in unit tests.
|
|
|
396 |
$DB->set_field('badge_issued', 'dateissued', $now - 11 + $i, array('userid' => $user2->id, 'badgeid' => $badgeid));
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
// Make sure the first user has no badges.
|
|
|
400 |
$result = badges_get_user_badges($user1->id);
|
|
|
401 |
$this->assertIsArray($result);
|
|
|
402 |
$this->assertCount(0, $result);
|
|
|
403 |
|
|
|
404 |
// Check that the second user has the expected 11 badges.
|
|
|
405 |
$result = badges_get_user_badges($user2->id);
|
|
|
406 |
$this->assertCount(11, $result);
|
|
|
407 |
|
|
|
408 |
// Test pagination.
|
|
|
409 |
// Ordering is by time issued desc, so things will come out with the last awarded badge first.
|
|
|
410 |
$result = badges_get_user_badges($user2->id, 0, 0, 4);
|
|
|
411 |
$this->assertCount(4, $result);
|
|
|
412 |
$lastbadgeissued = reset($result);
|
|
|
413 |
$this->assertSame('Test badge 11', $lastbadgeissued->name);
|
|
|
414 |
// Page 2. Expecting 4 results again.
|
|
|
415 |
$result = badges_get_user_badges($user2->id, 0, 1, 4);
|
|
|
416 |
$this->assertCount(4, $result);
|
|
|
417 |
$lastbadgeissued = reset($result);
|
|
|
418 |
$this->assertSame('Test badge 7', $lastbadgeissued->name);
|
|
|
419 |
// Page 3. Expecting just three results here.
|
|
|
420 |
$result = badges_get_user_badges($user2->id, 0, 2, 4);
|
|
|
421 |
$this->assertCount(3, $result);
|
|
|
422 |
$lastbadgeissued = reset($result);
|
|
|
423 |
$this->assertSame('Test badge 3', $lastbadgeissued->name);
|
|
|
424 |
// Page 4.... there is no page 4.
|
|
|
425 |
$result = badges_get_user_badges($user2->id, 0, 3, 4);
|
|
|
426 |
$this->assertCount(0, $result);
|
|
|
427 |
|
|
|
428 |
// Test search.
|
|
|
429 |
$result = badges_get_user_badges($user2->id, 0, 0, 0, 'badge 1');
|
|
|
430 |
$this->assertCount(3, $result);
|
|
|
431 |
$lastbadgeissued = reset($result);
|
|
|
432 |
$this->assertSame('Test badge 11', $lastbadgeissued->name);
|
|
|
433 |
// The term Totara doesn't appear anywhere in the badges.
|
|
|
434 |
$result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara');
|
|
|
435 |
$this->assertCount(0, $result);
|
|
|
436 |
|
|
|
437 |
// Issue a user with a course badge and verify its returned based on if
|
|
|
438 |
// coursebadges are enabled or disabled.
|
|
|
439 |
$sitebadgeid = key($badges);
|
|
|
440 |
$badges[$sitebadgeid]->issue($this->user->id, true);
|
|
|
441 |
|
|
|
442 |
$badge = new stdClass();
|
|
|
443 |
$badge->id = null;
|
|
|
444 |
$badge->name = "Test course badge";
|
|
|
445 |
$badge->description = "Testing course badge";
|
|
|
446 |
$badge->timecreated = $now;
|
|
|
447 |
$badge->timemodified = $now;
|
|
|
448 |
$badge->usercreated = $user1->id;
|
|
|
449 |
$badge->usermodified = $user1->id;
|
|
|
450 |
$badge->issuername = "Test issuer";
|
|
|
451 |
$badge->issuerurl = "http://issuer-url.domain.co.nz";
|
|
|
452 |
$badge->issuercontact = "issuer@example.com";
|
|
|
453 |
$badge->expiredate = null;
|
|
|
454 |
$badge->expireperiod = null;
|
|
|
455 |
$badge->type = BADGE_TYPE_COURSE;
|
|
|
456 |
$badge->courseid = $this->course->id;
|
|
|
457 |
$badge->messagesubject = "Test message subject for course badge";
|
|
|
458 |
$badge->message = "Test message body for course badge";
|
|
|
459 |
$badge->attachment = 1;
|
|
|
460 |
$badge->notification = 0;
|
|
|
461 |
$badge->status = BADGE_STATUS_ACTIVE;
|
|
|
462 |
$badge->version = "Version $i";
|
|
|
463 |
$badge->language = "en";
|
|
|
464 |
$badge->imagecaption = "Image caption";
|
|
|
465 |
$badge->imageauthorname = "Image author's name";
|
|
|
466 |
$badge->imageauthoremail = "author@example.com";
|
|
|
467 |
$badge->imageauthorname = "Image author's name";
|
|
|
468 |
|
|
|
469 |
$badgeid = $DB->insert_record('badge', $badge, true);
|
|
|
470 |
$badges[$badgeid] = new badge($badgeid);
|
|
|
471 |
$badges[$badgeid]->issue($this->user->id, true);
|
|
|
472 |
|
|
|
473 |
// With coursebadges off, we should only get the site badge.
|
|
|
474 |
set_config('badges_allowcoursebadges', false);
|
|
|
475 |
$result = badges_get_user_badges($this->user->id);
|
|
|
476 |
$this->assertCount(1, $result);
|
|
|
477 |
|
|
|
478 |
// With it on, we should get both.
|
|
|
479 |
set_config('badges_allowcoursebadges', true);
|
|
|
480 |
$result = badges_get_user_badges($this->user->id);
|
|
|
481 |
$this->assertCount(2, $result);
|
|
|
482 |
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
public function data_for_message_from_template() {
|
|
|
486 |
return array(
|
|
|
487 |
array(
|
|
|
488 |
'This is a message with no variables',
|
|
|
489 |
array(), // no params
|
|
|
490 |
'This is a message with no variables'
|
|
|
491 |
),
|
|
|
492 |
array(
|
|
|
493 |
'This is a message with %amissing% variables',
|
|
|
494 |
array(), // no params
|
|
|
495 |
'This is a message with %amissing% variables'
|
|
|
496 |
),
|
|
|
497 |
array(
|
|
|
498 |
'This is a message with %one% variable',
|
|
|
499 |
array('one' => 'a single'),
|
|
|
500 |
'This is a message with a single variable'
|
|
|
501 |
),
|
|
|
502 |
array(
|
|
|
503 |
'This is a message with %one% %two% %three% variables',
|
|
|
504 |
array('one' => 'more', 'two' => 'than', 'three' => 'one'),
|
|
|
505 |
'This is a message with more than one variables'
|
|
|
506 |
),
|
|
|
507 |
array(
|
|
|
508 |
'This is a message with %three% %two% %one%',
|
|
|
509 |
array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
|
|
|
510 |
'This is a message with randomly ordered variables'
|
|
|
511 |
),
|
|
|
512 |
array(
|
|
|
513 |
'This is a message with %repeated% %one% %repeated% of variables',
|
|
|
514 |
array('one' => 'and', 'repeated' => 'lots'),
|
|
|
515 |
'This is a message with lots and lots of variables'
|
|
|
516 |
),
|
|
|
517 |
);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
/**
|
|
|
521 |
* @dataProvider data_for_message_from_template
|
|
|
522 |
*/
|
11 |
efrain |
523 |
public function test_badge_message_from_template($message, $params, $result): void {
|
1 |
efrain |
524 |
$this->assertEquals(badge_message_from_template($message, $params), $result);
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
/**
|
|
|
528 |
* Test for working around the 61 tables join limit of mysql in award_criteria_activity in combination with the scheduled task.
|
|
|
529 |
*
|
|
|
530 |
* @covers \core_badges\badge::review_all_criteria
|
|
|
531 |
*/
|
11 |
efrain |
532 |
public function test_badge_activity_criteria_with_a_huge_number_of_coursemodules(): void {
|
1 |
efrain |
533 |
global $CFG;
|
|
|
534 |
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
|
|
|
535 |
|
|
|
536 |
if (!PHPUNIT_LONGTEST) {
|
|
|
537 |
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
// Messaging is not compatible with transactions.
|
|
|
541 |
$this->preventResetByRollback();
|
|
|
542 |
|
|
|
543 |
// Create more than 61 modules to potentially trigger an mysql db error.
|
|
|
544 |
$assigncount = 75;
|
|
|
545 |
$assigns = [];
|
|
|
546 |
for ($i = 1; $i <= $assigncount; $i++) {
|
|
|
547 |
$assigns[] = $this->getDataGenerator()->create_module('assign', ['course' => $this->course->id], ['completion' => 1]);
|
|
|
548 |
}
|
|
|
549 |
$assigncmids = array_flip(array_map(fn ($assign) => $assign->cmid, $assigns));
|
|
|
550 |
$criteriaactivityarray = array_fill_keys(array_keys($assigncmids), 1);
|
|
|
551 |
|
|
|
552 |
// Set completion criteria.
|
|
|
553 |
$criteriadata = (object) [
|
|
|
554 |
'id' => $this->course->id,
|
|
|
555 |
'criteria_activity' => $criteriaactivityarray,
|
|
|
556 |
];
|
|
|
557 |
$criterion = new completion_criteria_activity();
|
|
|
558 |
$criterion->update_config($criteriadata);
|
|
|
559 |
|
|
|
560 |
$badge = new badge($this->coursebadge);
|
|
|
561 |
|
|
|
562 |
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
563 |
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
564 |
$criteriaactivity = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id]);
|
|
|
565 |
|
|
|
566 |
$modulescrit = ['agg' => BADGE_CRITERIA_AGGREGATION_ALL];
|
|
|
567 |
foreach ($assigns as $assign) {
|
|
|
568 |
$modulescrit['module_' . $assign->cmid] = $assign->cmid;
|
|
|
569 |
}
|
|
|
570 |
$criteriaactivity->save($modulescrit);
|
|
|
571 |
|
|
|
572 |
// Take one assign to complete it later.
|
|
|
573 |
$assigntemp = array_shift($assigns);
|
|
|
574 |
|
|
|
575 |
// Mark the user to complete the modules.
|
|
|
576 |
foreach ($assigns as $assign) {
|
|
|
577 |
$cmassign = get_coursemodule_from_id('assign', $assign->cmid);
|
|
|
578 |
$completion = new \completion_info($this->course);
|
|
|
579 |
$completion->update_state($cmassign, COMPLETION_COMPLETE, $this->user->id);
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
// Run the scheduled task to issue the badge. But the badge should not be issued.
|
|
|
583 |
ob_start();
|
|
|
584 |
$task = manager::get_scheduled_task('core\task\badges_cron_task');
|
|
|
585 |
$task->execute();
|
|
|
586 |
ob_end_clean();
|
|
|
587 |
|
|
|
588 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
589 |
|
|
|
590 |
// Now complete the last uncompleted module.
|
|
|
591 |
$cmassign = get_coursemodule_from_id('assign', $assigntemp->cmid);
|
|
|
592 |
$completion = new \completion_info($this->course);
|
|
|
593 |
$completion->update_state($cmassign, COMPLETION_COMPLETE, $this->user->id);
|
|
|
594 |
|
|
|
595 |
// Run the scheduled task to issue the badge. Now the badge schould be issued.
|
|
|
596 |
ob_start();
|
|
|
597 |
$task = manager::get_scheduled_task('core\task\badges_cron_task');
|
|
|
598 |
$task->execute();
|
|
|
599 |
ob_end_clean();
|
|
|
600 |
|
|
|
601 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
602 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
603 |
}
|
|
|
604 |
|
|
|
605 |
/**
|
|
|
606 |
* Test badges observer when course module completion event id fired.
|
|
|
607 |
*/
|
11 |
efrain |
608 |
public function test_badges_observer_course_module_criteria_review(): void {
|
1 |
efrain |
609 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
610 |
$badge = new badge($this->coursebadge);
|
|
|
611 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
612 |
|
|
|
613 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
614 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
615 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
|
|
|
616 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
|
|
|
617 |
|
|
|
618 |
// Assert the badge will not be issued to the user as is.
|
|
|
619 |
$badge = new badge($this->coursebadge);
|
|
|
620 |
$badge->review_all_criteria();
|
|
|
621 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
622 |
|
|
|
623 |
// Set completion for forum activity.
|
|
|
624 |
$c = new completion_info($this->course);
|
|
|
625 |
$activities = $c->get_activities();
|
|
|
626 |
$this->assertEquals(1, count($activities));
|
|
|
627 |
$this->assertTrue(isset($activities[$this->module->cmid]));
|
|
|
628 |
$this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
|
|
|
629 |
|
|
|
630 |
$current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
|
|
|
631 |
$current->completionstate = COMPLETION_COMPLETE;
|
|
|
632 |
$current->timemodified = time();
|
|
|
633 |
$sink = $this->redirectEmails();
|
|
|
634 |
$c->internal_set_data($activities[$this->module->cmid], $current);
|
|
|
635 |
$this->assertCount(1, $sink->get_messages());
|
|
|
636 |
$sink->close();
|
|
|
637 |
|
|
|
638 |
// Check if badge is awarded.
|
|
|
639 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
640 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
/**
|
|
|
644 |
* Test badges observer when course_completed event is fired.
|
|
|
645 |
*/
|
11 |
efrain |
646 |
public function test_badges_observer_course_criteria_review(): void {
|
1 |
efrain |
647 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
648 |
$badge = new badge($this->coursebadge);
|
|
|
649 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
650 |
|
|
|
651 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
652 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
653 |
$criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
|
|
|
654 |
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
|
|
|
655 |
|
|
|
656 |
$ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
|
|
|
657 |
|
|
|
658 |
// Assert the badge will not be issued to the user as is.
|
|
|
659 |
$badge = new badge($this->coursebadge);
|
|
|
660 |
$badge->review_all_criteria();
|
|
|
661 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
662 |
|
|
|
663 |
// Mark course as complete.
|
|
|
664 |
$sink = $this->redirectMessages();
|
|
|
665 |
$ccompletion->mark_complete();
|
|
|
666 |
// Two messages are generated: One for the course completed and the other one for the badge awarded.
|
|
|
667 |
$messages = $sink->get_messages();
|
|
|
668 |
$this->assertCount(2, $messages);
|
|
|
669 |
$this->assertEquals('badgerecipientnotice', $messages[0]->eventtype);
|
|
|
670 |
$this->assertEquals('coursecompleted', $messages[1]->eventtype);
|
|
|
671 |
$sink->close();
|
|
|
672 |
|
|
|
673 |
// Check if badge is awarded.
|
|
|
674 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
675 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
/**
|
|
|
679 |
* Test badges observer when user_updated event is fired.
|
|
|
680 |
*/
|
11 |
efrain |
681 |
public function test_badges_observer_profile_criteria_review(): void {
|
1 |
efrain |
682 |
global $CFG, $DB;
|
|
|
683 |
require_once($CFG->dirroot.'/user/profile/lib.php');
|
|
|
684 |
|
|
|
685 |
// Add a custom field of textarea type.
|
|
|
686 |
$customprofileid = $this->getDataGenerator()->create_custom_profile_field(array(
|
|
|
687 |
'shortname' => 'newfield', 'name' => 'Description of new field',
|
|
|
688 |
'datatype' => 'textarea'))->id;
|
|
|
689 |
|
|
|
690 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
691 |
$badge = new badge($this->coursebadge);
|
|
|
692 |
|
|
|
693 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
694 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
695 |
$criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
|
|
|
696 |
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address',
|
|
|
697 |
'field_department' => 'department', 'field_' . $customprofileid => $customprofileid));
|
|
|
698 |
|
|
|
699 |
// Assert the badge will not be issued to the user as is.
|
|
|
700 |
$badge = new badge($this->coursebadge);
|
|
|
701 |
$badge->review_all_criteria();
|
|
|
702 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
703 |
|
|
|
704 |
// Set the required fields and make sure the badge got issued.
|
|
|
705 |
$this->user->address = 'Test address';
|
|
|
706 |
$this->user->department = 'sillywalks';
|
|
|
707 |
$sink = $this->redirectEmails();
|
|
|
708 |
profile_save_data((object)array('id' => $this->user->id, 'profile_field_newfield' => 'X'));
|
|
|
709 |
user_update_user($this->user, false);
|
|
|
710 |
$this->assertCount(1, $sink->get_messages());
|
|
|
711 |
$sink->close();
|
|
|
712 |
// Check if badge is awarded.
|
|
|
713 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
714 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
715 |
}
|
|
|
716 |
|
|
|
717 |
/**
|
|
|
718 |
* Test badges observer when cohort_member_added event is fired and user required to belong to any cohort.
|
|
|
719 |
*
|
|
|
720 |
* @covers \award_criteria_cohort
|
|
|
721 |
*/
|
11 |
efrain |
722 |
public function test_badges_observer_any_cohort_criteria_review(): void {
|
1 |
efrain |
723 |
global $CFG;
|
|
|
724 |
|
|
|
725 |
require_once("$CFG->dirroot/cohort/lib.php");
|
|
|
726 |
|
|
|
727 |
$cohort1 = $this->getDataGenerator()->create_cohort();
|
|
|
728 |
$cohort2 = $this->getDataGenerator()->create_cohort();
|
|
|
729 |
|
|
|
730 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
731 |
|
|
|
732 |
$badge = new badge($this->badgeid);
|
|
|
733 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
734 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
735 |
|
|
|
736 |
// Set up the badge criteria.
|
|
|
737 |
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
738 |
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
739 |
$criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
|
|
|
740 |
$criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY,
|
|
|
741 |
'cohort_cohorts' => array('0' => $cohort1->id, '1' => $cohort2->id)));
|
|
|
742 |
$badge->set_status(BADGE_STATUS_ACTIVE);
|
|
|
743 |
|
|
|
744 |
// Reload it to contain criteria.
|
|
|
745 |
$badge = new badge($this->badgeid);
|
|
|
746 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
747 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
748 |
|
|
|
749 |
// Add the user to the cohort.
|
|
|
750 |
cohort_add_member($cohort2->id, $this->user->id);
|
|
|
751 |
$this->assertDebuggingCalled();
|
|
|
752 |
|
|
|
753 |
// Verify that the badge was awarded.
|
|
|
754 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
755 |
// As the badge has been awarded to user because core_badges_observer been called when the member has been added to the
|
|
|
756 |
// cohort, there are no other users that can award this badge.
|
|
|
757 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
758 |
}
|
|
|
759 |
|
|
|
760 |
/**
|
11 |
efrain |
761 |
* Test badges observer when user_updated event is fired.
|
|
|
762 |
* @covers \award_criteria_courseset
|
|
|
763 |
*/
|
|
|
764 |
public function test_badges_observer_courseset_criteria_review(): void {
|
|
|
765 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
766 |
$badge = new badge($this->coursebadge);
|
|
|
767 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
768 |
|
|
|
769 |
$additionalcourse = $this->getDataGenerator()->create_course(['enablecompletion' => true]);
|
|
|
770 |
$this->getDataGenerator()->enrol_user($this->user->id, $additionalcourse->id);
|
|
|
771 |
|
|
|
772 |
$criteriaoverall = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
|
|
|
773 |
$criteriaoverall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ANY]);
|
|
|
774 |
$criteriaoverall1 = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_COURSESET, 'badgeid' => $badge->id]);
|
|
|
775 |
$criteriaoverall1->save(['agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_' . $this->course->id => $this->course->id,
|
|
|
776 |
'course_' . $additionalcourse->id => $additionalcourse->id]);
|
|
|
777 |
|
|
|
778 |
$ccompletion = new completion_completion(['course' => $this->course->id, 'userid' => $this->user->id]);
|
|
|
779 |
$ccompletion2 = new completion_completion(['course' => $additionalcourse->id, 'userid' => $this->user->id]);
|
|
|
780 |
// Assert the badge will not be issued to the user as is.
|
|
|
781 |
$badge = new badge($this->coursebadge);
|
|
|
782 |
$badge->review_all_criteria();
|
|
|
783 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
784 |
|
|
|
785 |
// Mark course as complete.
|
|
|
786 |
$sink = $this->redirectMessages();
|
|
|
787 |
$ccompletion->mark_complete();
|
|
|
788 |
$ccompletion2->mark_complete();
|
|
|
789 |
// Thee messages are generated: Two for the course completed and the other one for the badge awarded.
|
|
|
790 |
$messages = $sink->get_messages();
|
|
|
791 |
$this->assertCount(3, $messages);
|
|
|
792 |
$this->assertEquals('badgerecipientnotice', $messages[0]->eventtype);
|
|
|
793 |
$this->assertEquals('coursecompleted', $messages[1]->eventtype);
|
|
|
794 |
$sink->close();
|
|
|
795 |
|
|
|
796 |
// Check if badge is awarded.
|
|
|
797 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
798 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
799 |
}
|
|
|
800 |
|
|
|
801 |
/**
|
|
|
802 |
* Test the criteria review method for courseset
|
|
|
803 |
* @covers \award_criteria_courseset::review
|
|
|
804 |
*/
|
|
|
805 |
public function test_badges_courseset_criteria_review_empty_courseset(): void {
|
|
|
806 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
807 |
$badge = new badge($this->coursebadge);
|
|
|
808 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
809 |
|
|
|
810 |
$criteriaoverall = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
|
|
|
811 |
$criteriaoverall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ANY]);
|
|
|
812 |
$criteriaoverall1 = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_COURSESET, 'badgeid' => $badge->id]);
|
|
|
813 |
$criteriaoverall1->save();
|
|
|
814 |
// Assert the badge will not be issued to the user as is.
|
|
|
815 |
$badge = new badge($this->coursebadge);
|
|
|
816 |
$badge->review_all_criteria();
|
|
|
817 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
/**
|
1 |
efrain |
821 |
* Test badges observer when cohort_member_added event is fired and user required to belong to multiple (all) cohorts.
|
|
|
822 |
*
|
|
|
823 |
* @covers \award_criteria_cohort
|
|
|
824 |
*/
|
11 |
efrain |
825 |
public function test_badges_observer_all_cohort_criteria_review(): void {
|
1 |
efrain |
826 |
global $CFG;
|
|
|
827 |
|
|
|
828 |
require_once("$CFG->dirroot/cohort/lib.php");
|
|
|
829 |
|
|
|
830 |
$cohort1 = $this->getDataGenerator()->create_cohort();
|
|
|
831 |
$cohort2 = $this->getDataGenerator()->create_cohort();
|
|
|
832 |
$cohort3 = $this->getDataGenerator()->create_cohort();
|
|
|
833 |
|
|
|
834 |
// Add user2 to cohort1 and cohort3.
|
|
|
835 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
836 |
cohort_add_member($cohort3->id, $user2->id);
|
|
|
837 |
cohort_add_member($cohort1->id, $user2->id);
|
|
|
838 |
|
|
|
839 |
// Add user3 to cohort1, cohort2 and cohort3.
|
|
|
840 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
841 |
cohort_add_member($cohort1->id, $user3->id);
|
|
|
842 |
cohort_add_member($cohort2->id, $user3->id);
|
|
|
843 |
cohort_add_member($cohort3->id, $user3->id);
|
|
|
844 |
|
|
|
845 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
846 |
|
|
|
847 |
// Cohort criteria are used in site badges.
|
|
|
848 |
$badge = new badge($this->badgeid);
|
|
|
849 |
|
|
|
850 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
851 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
852 |
|
|
|
853 |
// Set up the badge criteria.
|
|
|
854 |
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
855 |
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
856 |
$criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
|
|
|
857 |
$criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL,
|
|
|
858 |
'cohort_cohorts' => array('0' => $cohort1->id, '1' => $cohort2->id, '2' => $cohort3->id)));
|
|
|
859 |
$badge->set_status(BADGE_STATUS_ACTIVE);
|
|
|
860 |
|
|
|
861 |
// Reload it to contain criteria.
|
|
|
862 |
$badge = new badge($this->badgeid);
|
|
|
863 |
|
|
|
864 |
// Verify that the badge was not awarded yet (ALL cohorts are needed and review_all_criteria has to be called).
|
|
|
865 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
866 |
$this->assertFalse($badge->is_issued($user2->id));
|
|
|
867 |
$this->assertFalse($badge->is_issued($user3->id));
|
|
|
868 |
|
|
|
869 |
// Verify that after calling review_all_criteria, users with the criteria (user3) award the badge instantly.
|
|
|
870 |
$this->assertSame(1, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
871 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
872 |
$this->assertFalse($badge->is_issued($user2->id));
|
|
|
873 |
$this->assertTrue($badge->is_issued($user3->id));
|
|
|
874 |
$this->assertDebuggingCalled();
|
|
|
875 |
|
|
|
876 |
// Add the user to the cohort1.
|
|
|
877 |
cohort_add_member($cohort1->id, $this->user->id);
|
|
|
878 |
|
|
|
879 |
// Verify that the badge was not awarded yet (ALL cohorts are needed).
|
|
|
880 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
881 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
882 |
|
|
|
883 |
// Add the user to the cohort3.
|
|
|
884 |
cohort_add_member($cohort3->id, $this->user->id);
|
|
|
885 |
|
|
|
886 |
// Verify that the badge was not awarded yet (ALL cohorts are needed).
|
|
|
887 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
888 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
889 |
|
|
|
890 |
// Add user to cohort2.
|
|
|
891 |
cohort_add_member($cohort2->id, $this->user->id);
|
|
|
892 |
$this->assertDebuggingCalled();
|
|
|
893 |
|
|
|
894 |
// Verify that the badge was awarded (ALL cohorts).
|
|
|
895 |
$this->assertTrue($badge->is_issued($this->user->id));
|
|
|
896 |
// As the badge has been awarded to user because core_badges_observer been called when the member has been added to the
|
|
|
897 |
// cohort, there are no other users that can award this badge.
|
|
|
898 |
$this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
|
|
|
899 |
}
|
|
|
900 |
|
|
|
901 |
/**
|
|
|
902 |
* Test badges assertion generated when a badge is issued.
|
|
|
903 |
*/
|
11 |
efrain |
904 |
public function test_badges_assertion(): void {
|
1 |
efrain |
905 |
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
|
|
906 |
$badge = new badge($this->coursebadge);
|
|
|
907 |
$this->assertFalse($badge->is_issued($this->user->id));
|
|
|
908 |
|
|
|
909 |
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
|
|
|
910 |
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
|
|
|
911 |
$criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
|
|
|
912 |
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
|
|
|
913 |
|
|
|
914 |
$this->user->address = 'Test address';
|
|
|
915 |
$sink = $this->redirectEmails();
|
|
|
916 |
user_update_user($this->user, false);
|
|
|
917 |
$this->assertCount(1, $sink->get_messages());
|
|
|
918 |
$sink->close();
|
|
|
919 |
// Check if badge is awarded.
|
|
|
920 |
$this->assertDebuggingCalled('Error baking badge image!');
|
|
|
921 |
$awards = $badge->get_awards();
|
|
|
922 |
$this->assertCount(1, $awards);
|
|
|
923 |
|
|
|
924 |
// Get assertion.
|
|
|
925 |
$award = reset($awards);
|
|
|
926 |
$assertion = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V1);
|
|
|
927 |
$testassertion = $this->assertion;
|
|
|
928 |
|
|
|
929 |
// Make sure JSON strings have the same structure.
|
|
|
930 |
$this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
|
|
|
931 |
$this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
|
|
|
932 |
$this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
|
|
|
933 |
|
|
|
934 |
// Test Openbadge specification version 2.
|
|
|
935 |
// Get assertion version 2.
|
|
|
936 |
$award = reset($awards);
|
|
|
937 |
$assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2);
|
|
|
938 |
$testassertion2 = $this->assertion2;
|
|
|
939 |
|
|
|
940 |
// Make sure JSON strings have the same structure.
|
|
|
941 |
$this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
|
|
|
942 |
$this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
|
|
|
943 |
$this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));
|
|
|
944 |
|
|
|
945 |
// Test Openbadge specification version 2.1. It has the same format as OBv2.0.
|
|
|
946 |
// Get assertion version 2.1.
|
|
|
947 |
$award = reset($awards);
|
|
|
948 |
$assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2P1);
|
|
|
949 |
|
|
|
950 |
// Make sure JSON strings have the same structure.
|
|
|
951 |
$this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
|
|
|
952 |
$this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
|
|
|
953 |
$this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));
|
|
|
954 |
}
|
|
|
955 |
|
|
|
956 |
/**
|
|
|
957 |
* Tests the core_badges_myprofile_navigation() function.
|
|
|
958 |
*/
|
11 |
efrain |
959 |
public function test_core_badges_myprofile_navigation(): void {
|
1 |
efrain |
960 |
// Set up the test.
|
|
|
961 |
$tree = new \core_user\output\myprofile\tree();
|
|
|
962 |
$this->setAdminUser();
|
|
|
963 |
$badge = new badge($this->badgeid);
|
|
|
964 |
$badge->issue($this->user->id, true);
|
|
|
965 |
$iscurrentuser = true;
|
|
|
966 |
$course = null;
|
|
|
967 |
|
|
|
968 |
// Enable badges.
|
|
|
969 |
set_config('enablebadges', true);
|
|
|
970 |
|
|
|
971 |
// Check the node tree is correct.
|
|
|
972 |
core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
|
|
|
973 |
$reflector = new ReflectionObject($tree);
|
|
|
974 |
$nodes = $reflector->getProperty('nodes');
|
|
|
975 |
$this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
|
|
|
976 |
}
|
|
|
977 |
|
|
|
978 |
/**
|
|
|
979 |
* Tests the core_badges_myprofile_navigation() function with badges disabled..
|
|
|
980 |
*/
|
11 |
efrain |
981 |
public function test_core_badges_myprofile_navigation_badges_disabled(): void {
|
1 |
efrain |
982 |
// Set up the test.
|
|
|
983 |
$tree = new \core_user\output\myprofile\tree();
|
|
|
984 |
$this->setAdminUser();
|
|
|
985 |
$badge = new badge($this->badgeid);
|
|
|
986 |
$badge->issue($this->user->id, true);
|
|
|
987 |
$iscurrentuser = false;
|
|
|
988 |
$course = null;
|
|
|
989 |
|
|
|
990 |
// Disable badges.
|
|
|
991 |
set_config('enablebadges', false);
|
|
|
992 |
|
|
|
993 |
// Check the node tree is correct.
|
|
|
994 |
core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
|
|
|
995 |
$reflector = new ReflectionObject($tree);
|
|
|
996 |
$nodes = $reflector->getProperty('nodes');
|
|
|
997 |
$this->assertArrayNotHasKey('localbadges', $nodes->getValue($tree));
|
|
|
998 |
}
|
|
|
999 |
|
|
|
1000 |
/**
|
|
|
1001 |
* Tests the core_badges_myprofile_navigation() function with a course badge.
|
|
|
1002 |
*/
|
11 |
efrain |
1003 |
public function test_core_badges_myprofile_navigation_with_course_badge(): void {
|
1 |
efrain |
1004 |
// Set up the test.
|
|
|
1005 |
$tree = new \core_user\output\myprofile\tree();
|
|
|
1006 |
$this->setAdminUser();
|
|
|
1007 |
$badge = new badge($this->coursebadge);
|
|
|
1008 |
$badge->issue($this->user->id, true);
|
|
|
1009 |
$iscurrentuser = false;
|
|
|
1010 |
|
|
|
1011 |
// Check the node tree is correct.
|
|
|
1012 |
core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $this->course);
|
|
|
1013 |
$reflector = new ReflectionObject($tree);
|
|
|
1014 |
$nodes = $reflector->getProperty('nodes');
|
|
|
1015 |
$this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
|
|
|
1016 |
}
|
|
|
1017 |
|
|
|
1018 |
/**
|
|
|
1019 |
* Test insert and update endorsement with a site badge.
|
|
|
1020 |
*/
|
11 |
efrain |
1021 |
public function test_badge_endorsement(): void {
|
1 |
efrain |
1022 |
$badge = new badge($this->badgeid);
|
|
|
1023 |
|
|
|
1024 |
// Insert Endorsement.
|
|
|
1025 |
$endorsement = new stdClass();
|
|
|
1026 |
$endorsement->badgeid = $this->badgeid;
|
|
|
1027 |
$endorsement->issuername = "Issuer 123";
|
|
|
1028 |
$endorsement->issueremail = "issuer123@email.com";
|
|
|
1029 |
$endorsement->issuerurl = "https://example.org/issuer-123";
|
|
|
1030 |
$endorsement->dateissued = 1524567747;
|
|
|
1031 |
$endorsement->claimid = "https://example.org/robotics-badge.json";
|
|
|
1032 |
$endorsement->claimcomment = "Test endorser comment";
|
|
|
1033 |
|
|
|
1034 |
$badge->save_endorsement($endorsement);
|
|
|
1035 |
$endorsement1 = $badge->get_endorsement();
|
|
|
1036 |
$this->assertEquals($endorsement->badgeid, $endorsement1->badgeid);
|
|
|
1037 |
$this->assertEquals($endorsement->issuername, $endorsement1->issuername);
|
|
|
1038 |
$this->assertEquals($endorsement->issueremail, $endorsement1->issueremail);
|
|
|
1039 |
$this->assertEquals($endorsement->issuerurl, $endorsement1->issuerurl);
|
|
|
1040 |
$this->assertEquals($endorsement->dateissued, $endorsement1->dateissued);
|
|
|
1041 |
$this->assertEquals($endorsement->claimid, $endorsement1->claimid);
|
|
|
1042 |
$this->assertEquals($endorsement->claimcomment, $endorsement1->claimcomment);
|
|
|
1043 |
|
|
|
1044 |
// Update Endorsement.
|
|
|
1045 |
$endorsement1->issuername = "Issuer update";
|
|
|
1046 |
$badge->save_endorsement($endorsement1);
|
|
|
1047 |
$endorsement2 = $badge->get_endorsement();
|
|
|
1048 |
$this->assertEquals($endorsement1->id, $endorsement2->id);
|
|
|
1049 |
$this->assertEquals($endorsement1->issuername, $endorsement2->issuername);
|
|
|
1050 |
}
|
|
|
1051 |
|
|
|
1052 |
/**
|
|
|
1053 |
* Test insert and delete related badge with a site badge.
|
|
|
1054 |
*/
|
11 |
efrain |
1055 |
public function test_badge_related(): void {
|
1 |
efrain |
1056 |
$badge = new badge($this->badgeid);
|
|
|
1057 |
$newid1 = $badge->make_clone();
|
|
|
1058 |
$newid2 = $badge->make_clone();
|
|
|
1059 |
$newid3 = $badge->make_clone();
|
|
|
1060 |
|
|
|
1061 |
// Insert an related badge.
|
|
|
1062 |
$badge->add_related_badges([$newid1, $newid2, $newid3]);
|
|
|
1063 |
$this->assertCount(3, $badge->get_related_badges());
|
|
|
1064 |
|
|
|
1065 |
// Only get related is active.
|
|
|
1066 |
$clonedbage1 = new badge($newid1);
|
|
|
1067 |
$clonedbage1->status = BADGE_STATUS_ACTIVE;
|
|
|
1068 |
$clonedbage1->save();
|
|
|
1069 |
$this->assertCount(1, $badge->get_related_badges(true));
|
|
|
1070 |
|
|
|
1071 |
// Delete an related badge.
|
|
|
1072 |
$badge->delete_related_badge($newid2);
|
|
|
1073 |
$this->assertCount(2, $badge->get_related_badges());
|
|
|
1074 |
}
|
|
|
1075 |
|
|
|
1076 |
/**
|
|
|
1077 |
* Test insert, update, delete alignment with a site badge.
|
|
|
1078 |
*/
|
11 |
efrain |
1079 |
public function test_alignments(): void {
|
1 |
efrain |
1080 |
$badge = new badge($this->badgeid);
|
|
|
1081 |
|
|
|
1082 |
// Insert a alignment.
|
|
|
1083 |
$alignment1 = new stdClass();
|
|
|
1084 |
$alignment1->badgeid = $this->badgeid;
|
|
|
1085 |
$alignment1->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
|
|
|
1086 |
$alignment1->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
|
|
|
1087 |
$alignment1->targetdescription = 'Test target description';
|
|
|
1088 |
$alignment1->targetframework = 'CCSS.RST.11-12.3';
|
|
|
1089 |
$alignment1->targetcode = 'CCSS.RST.11-12.3';
|
|
|
1090 |
$alignment2 = clone $alignment1;
|
|
|
1091 |
$newid1 = $badge->save_alignment($alignment1);
|
|
|
1092 |
$newid2 = $badge->save_alignment($alignment2);
|
|
|
1093 |
$alignments1 = $badge->get_alignments();
|
|
|
1094 |
$this->assertCount(2, $alignments1);
|
|
|
1095 |
|
|
|
1096 |
$this->assertEquals($alignment1->badgeid, $alignments1[$newid1]->badgeid);
|
|
|
1097 |
$this->assertEquals($alignment1->targetname, $alignments1[$newid1]->targetname);
|
|
|
1098 |
$this->assertEquals($alignment1->targeturl, $alignments1[$newid1]->targeturl);
|
|
|
1099 |
$this->assertEquals($alignment1->targetdescription, $alignments1[$newid1]->targetdescription);
|
|
|
1100 |
$this->assertEquals($alignment1->targetframework, $alignments1[$newid1]->targetframework);
|
|
|
1101 |
$this->assertEquals($alignment1->targetcode, $alignments1[$newid1]->targetcode);
|
|
|
1102 |
|
|
|
1103 |
// Update aligment.
|
|
|
1104 |
$alignments1[$newid1]->targetname = 'CCSS.ELA-Literacy.RST.11-12.3 update';
|
|
|
1105 |
$badge->save_alignment($alignments1[$newid1], $alignments1[$newid1]->id);
|
|
|
1106 |
$alignments2 = $badge->get_alignments();
|
|
|
1107 |
$this->assertEquals($alignments1[$newid1]->id, $alignments2[$newid1]->id);
|
|
|
1108 |
$this->assertEquals($alignments1[$newid1]->targetname, $alignments2[$newid1]->targetname);
|
|
|
1109 |
|
|
|
1110 |
// Delete alignment.
|
|
|
1111 |
$badge->delete_alignment($alignments1[$newid2]->id);
|
|
|
1112 |
$this->assertCount(1, $badge->get_alignments());
|
|
|
1113 |
}
|
|
|
1114 |
|
|
|
1115 |
/**
|
|
|
1116 |
* Test badges_delete_site_backpack().
|
|
|
1117 |
*
|
|
|
1118 |
*/
|
|
|
1119 |
public function test_badges_delete_site_backpack(): void {
|
|
|
1120 |
global $DB;
|
|
|
1121 |
|
|
|
1122 |
$this->setAdminUser();
|
|
|
1123 |
|
|
|
1124 |
// Create one backpack.
|
|
|
1125 |
$total = $DB->count_records('badge_external_backpack');
|
|
|
1126 |
$this->assertEquals(1, $total);
|
|
|
1127 |
|
|
|
1128 |
$data = new \stdClass();
|
|
|
1129 |
$data->apiversion = OPEN_BADGES_V2P1;
|
|
|
1130 |
$data->backpackapiurl = 'https://dc.imsglobal.org/obchost/ims/ob/v2p1';
|
|
|
1131 |
$data->backpackweburl = 'https://dc.imsglobal.org';
|
|
|
1132 |
badges_create_site_backpack($data);
|
|
|
1133 |
$backpack = $DB->get_record('badge_external_backpack', ['backpackweburl' => $data->backpackweburl]);
|
|
|
1134 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
1135 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
1136 |
// User1 is connected to the backpack to be removed and has 2 collections.
|
|
|
1137 |
$backpackuser1 = helper::create_fake_backpack(['userid' => $user1->id, 'externalbackpackid' => $backpack->id]);
|
|
|
1138 |
helper::create_fake_backpack_collection(['backpackid' => $backpackuser1->id]);
|
|
|
1139 |
helper::create_fake_backpack_collection(['backpackid' => $backpackuser1->id]);
|
|
|
1140 |
// User2 is connected to a different backpack and has 1 collection.
|
|
|
1141 |
$backpackuser2 = helper::create_fake_backpack(['userid' => $user2->id]);
|
|
|
1142 |
helper::create_fake_backpack_collection(['backpackid' => $backpackuser2->id]);
|
|
|
1143 |
|
|
|
1144 |
$total = $DB->count_records('badge_external_backpack');
|
|
|
1145 |
$this->assertEquals(2, $total);
|
|
|
1146 |
$total = $DB->count_records('badge_backpack');
|
|
|
1147 |
$this->assertEquals(2, $total);
|
|
|
1148 |
$total = $DB->count_records('badge_external');
|
|
|
1149 |
$this->assertEquals(3, $total);
|
|
|
1150 |
|
|
|
1151 |
// Remove the backpack created previously.
|
|
|
1152 |
$result = badges_delete_site_backpack($backpack->id);
|
|
|
1153 |
$this->assertTrue($result);
|
|
|
1154 |
|
|
|
1155 |
$total = $DB->count_records('badge_external_backpack');
|
|
|
1156 |
$this->assertEquals(1, $total);
|
|
|
1157 |
|
|
|
1158 |
$total = $DB->count_records('badge_backpack');
|
|
|
1159 |
$this->assertEquals(1, $total);
|
|
|
1160 |
|
|
|
1161 |
$total = $DB->count_records('badge_external');
|
|
|
1162 |
$this->assertEquals(1, $total);
|
|
|
1163 |
|
|
|
1164 |
// Try to remove an non-existent backpack.
|
|
|
1165 |
$result = badges_delete_site_backpack($backpack->id);
|
|
|
1166 |
$this->assertFalse($result);
|
|
|
1167 |
}
|
|
|
1168 |
|
|
|
1169 |
/**
|
|
|
1170 |
* Test to validate badges_save_backpack_credentials.
|
|
|
1171 |
*
|
|
|
1172 |
* @dataProvider save_backpack_credentials_provider
|
|
|
1173 |
* @param bool $addbackpack True if backpack data has to be created; false otherwise (empty data will be used then).
|
|
|
1174 |
* @param string|null $mail Backpack mail address.
|
|
|
1175 |
* @param string|null $password Backpack password.
|
|
|
1176 |
*/
|
11 |
efrain |
1177 |
public function test_save_backpack_credentials(bool $addbackpack = true, ?string $mail = null, ?string $password = null): void {
|
1 |
efrain |
1178 |
global $DB;
|
|
|
1179 |
|
|
|
1180 |
$this->resetAfterTest();
|
|
|
1181 |
$this->setAdminUser();
|
|
|
1182 |
|
|
|
1183 |
$data = [];
|
|
|
1184 |
if ($addbackpack) {
|
|
|
1185 |
$data = new \stdClass();
|
|
|
1186 |
$data->apiversion = OPEN_BADGES_V2P1;
|
|
|
1187 |
$data->backpackapiurl = 'https://dc.imsglobal.org/obchost/ims/ob/v2p1';
|
|
|
1188 |
$data->backpackweburl = 'https://dc.imsglobal.org';
|
|
|
1189 |
badges_create_site_backpack($data);
|
|
|
1190 |
$backpack = $DB->get_record('badge_external_backpack', ['backpackweburl' => $data->backpackweburl]);
|
|
|
1191 |
$user = $this->getDataGenerator()->create_user();
|
|
|
1192 |
|
|
|
1193 |
$data = [
|
|
|
1194 |
'externalbackpackid' => $backpack->id,
|
|
|
1195 |
'userid' => $user->id,
|
|
|
1196 |
];
|
|
|
1197 |
|
|
|
1198 |
if (!empty($mail)) {
|
|
|
1199 |
$data['backpackemail'] = $mail;
|
|
|
1200 |
}
|
|
|
1201 |
if (!empty($password)) {
|
|
|
1202 |
$data['password'] = $password;
|
|
|
1203 |
}
|
|
|
1204 |
}
|
|
|
1205 |
|
|
|
1206 |
$return = badges_save_backpack_credentials((object) $data);
|
|
|
1207 |
if (array_key_exists('userid', $data)) {
|
|
|
1208 |
$record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
|
|
|
1209 |
} else {
|
|
|
1210 |
$record = $DB->get_records('badge_backpack');
|
|
|
1211 |
}
|
|
|
1212 |
|
|
|
1213 |
if (!empty($mail) && !empty($password)) {
|
|
|
1214 |
// The backpack credentials are created if the given information is right.
|
|
|
1215 |
$this->assertNotEmpty($record);
|
|
|
1216 |
$this->assertEquals($data['externalbackpackid'], $return);
|
|
|
1217 |
} else if ($addbackpack) {
|
|
|
1218 |
// If no email and password are given, no backpack is created/modified.
|
|
|
1219 |
$this->assertEmpty($record);
|
|
|
1220 |
$this->assertEquals($data['externalbackpackid'], $return);
|
|
|
1221 |
} else {
|
|
|
1222 |
// There weren't fields to add to the backpack so no DB change is expected.
|
|
|
1223 |
$this->assertEmpty($record);
|
|
|
1224 |
$this->assertEquals(0, $return);
|
|
|
1225 |
}
|
|
|
1226 |
|
|
|
1227 |
// Confirm the existing backpack credential can be updated (if it has been created).
|
|
|
1228 |
if (!empty($record)) {
|
|
|
1229 |
$data['backpackemail'] = 'modified_' . $mail;
|
|
|
1230 |
$data['id'] = $record->id;
|
|
|
1231 |
$return = badges_save_backpack_credentials((object) $data);
|
|
|
1232 |
$record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
|
|
|
1233 |
|
|
|
1234 |
$this->assertNotEmpty($record);
|
|
|
1235 |
$this->assertEquals($data['backpackemail'], $record->email);
|
|
|
1236 |
$this->assertEquals($data['externalbackpackid'], $return);
|
|
|
1237 |
}
|
|
|
1238 |
}
|
|
|
1239 |
|
|
|
1240 |
/**
|
|
|
1241 |
* Data provider for test_create_backpack_credentials().
|
|
|
1242 |
*
|
|
|
1243 |
* @return array
|
|
|
1244 |
*/
|
|
|
1245 |
public function save_backpack_credentials_provider(): array {
|
|
|
1246 |
return [
|
|
|
1247 |
'Empty fields' => [
|
|
|
1248 |
false,
|
|
|
1249 |
],
|
|
|
1250 |
'No backpack mail or password are defined' => [
|
|
|
1251 |
true,
|
|
|
1252 |
],
|
|
|
1253 |
'Both backpack mail and password are defined' => [
|
|
|
1254 |
true, 'test@test.com', '1234',
|
|
|
1255 |
],
|
|
|
1256 |
'Only backpack mail is defined (no password is given)' => [
|
|
|
1257 |
true, 'test@test.com', null,
|
|
|
1258 |
],
|
|
|
1259 |
'Only backpack password is defined (no mail is given)' => [
|
|
|
1260 |
true, null, '1234'
|
|
|
1261 |
],
|
|
|
1262 |
];
|
|
|
1263 |
}
|
|
|
1264 |
|
|
|
1265 |
/**
|
|
|
1266 |
* Test badges_save_external_backpack.
|
|
|
1267 |
*
|
|
|
1268 |
* @dataProvider badges_save_external_backpack_provider
|
|
|
1269 |
* @param array $data Backpack data to save.
|
|
|
1270 |
* @param bool $adduser True if a real user has to be used for creating the backpack; false otherwise.
|
|
|
1271 |
* @param bool $duplicates True if duplicates has to be tested too; false otherwise.
|
|
|
1272 |
*/
|
11 |
efrain |
1273 |
public function test_badges_save_external_backpack(array $data, bool $adduser, bool $duplicates): void {
|
1 |
efrain |
1274 |
global $DB;
|
|
|
1275 |
|
|
|
1276 |
$this->resetAfterTest();
|
|
|
1277 |
|
|
|
1278 |
$userid = 0;
|
|
|
1279 |
if ($adduser) {
|
|
|
1280 |
$user = $this->getDataGenerator()->create_user();
|
|
|
1281 |
$userid = $user->id;
|
|
|
1282 |
$data['userid'] = $user->id;
|
|
|
1283 |
}
|
|
|
1284 |
|
|
|
1285 |
$result = badges_save_external_backpack((object) $data);
|
|
|
1286 |
$this->assertNotEquals(0, $result);
|
|
|
1287 |
$record = $DB->get_record('badge_external_backpack', ['id' => $result]);
|
|
|
1288 |
$this->assertEquals($record->backpackweburl, $data['backpackweburl']);
|
|
|
1289 |
$this->assertEquals($record->backpackapiurl, $data['backpackapiurl']);
|
|
|
1290 |
|
|
|
1291 |
$record = $DB->get_record('badge_backpack', ['externalbackpackid' => $result]);
|
|
|
1292 |
if (!array_key_exists('backpackemail', $data) && !array_key_exists('password', $data)) {
|
|
|
1293 |
$this->assertEmpty($record);
|
|
|
1294 |
$total = $DB->count_records('badge_backpack');
|
|
|
1295 |
$this->assertEquals(0, $total);
|
|
|
1296 |
} else {
|
|
|
1297 |
$this->assertNotEmpty($record);
|
|
|
1298 |
$this->assertEquals($record->userid, $userid);
|
|
|
1299 |
}
|
|
|
1300 |
|
|
|
1301 |
if ($duplicates) {
|
|
|
1302 |
// We shouldn't be able to insert multiple external_backpacks with the same values.
|
|
|
1303 |
$this->expectException('dml_write_exception');
|
|
|
1304 |
$result = badges_save_external_backpack((object)$data);
|
|
|
1305 |
}
|
|
|
1306 |
}
|
|
|
1307 |
|
|
|
1308 |
/**
|
|
|
1309 |
* Provider for test_badges_save_external_backpack
|
|
|
1310 |
*
|
|
|
1311 |
* @return array
|
|
|
1312 |
*/
|
|
|
1313 |
public function badges_save_external_backpack_provider() {
|
|
|
1314 |
$data = [
|
|
|
1315 |
'apiversion' => 2,
|
|
|
1316 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1317 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1318 |
];
|
|
|
1319 |
return [
|
|
|
1320 |
'Test without user and auth details. Check duplicates too' => [
|
|
|
1321 |
'data' => $data,
|
|
|
1322 |
'adduser' => false,
|
|
|
1323 |
'duplicates' => true,
|
|
|
1324 |
],
|
|
|
1325 |
'Test without user and auth details. No duplicates' => [
|
|
|
1326 |
'data' => $data,
|
|
|
1327 |
'adduser' => false,
|
|
|
1328 |
'duplicates' => false,
|
|
|
1329 |
],
|
|
|
1330 |
'Test with user and without auth details' => [
|
|
|
1331 |
'data' => $data,
|
|
|
1332 |
'adduser' => true,
|
|
|
1333 |
'duplicates' => false,
|
|
|
1334 |
],
|
|
|
1335 |
'Test with user and without auth details. Check duplicates too' => [
|
|
|
1336 |
'data' => $data,
|
|
|
1337 |
'adduser' => true,
|
|
|
1338 |
'duplicates' => true,
|
|
|
1339 |
],
|
|
|
1340 |
'Test with empty backpackemail, password and id' => [
|
|
|
1341 |
'data' => array_merge($data, [
|
|
|
1342 |
'backpackemail' => '',
|
|
|
1343 |
'password' => '',
|
|
|
1344 |
'id' => 0,
|
|
|
1345 |
]),
|
|
|
1346 |
'adduser' => false,
|
|
|
1347 |
'duplicates' => false,
|
|
|
1348 |
],
|
|
|
1349 |
'Test with empty backpackemail, password and id but with user' => [
|
|
|
1350 |
'data' => array_merge($data, [
|
|
|
1351 |
'backpackemail' => '',
|
|
|
1352 |
'password' => '',
|
|
|
1353 |
'id' => 0,
|
|
|
1354 |
]),
|
|
|
1355 |
'adduser' => true,
|
|
|
1356 |
'duplicates' => false,
|
|
|
1357 |
],
|
|
|
1358 |
'Test with auth details but without user' => [
|
|
|
1359 |
'data' => array_merge($data, [
|
|
|
1360 |
'backpackemail' => 'test@test.com',
|
|
|
1361 |
'password' => 'test',
|
|
|
1362 |
]),
|
|
|
1363 |
'adduser' => false,
|
|
|
1364 |
'duplicates' => false,
|
|
|
1365 |
],
|
|
|
1366 |
'Test with auth details and user' => [
|
|
|
1367 |
'data' => array_merge($data, [
|
|
|
1368 |
'backpackemail' => 'test@test.com',
|
|
|
1369 |
'password' => 'test',
|
|
|
1370 |
]),
|
|
|
1371 |
'adduser' => true,
|
|
|
1372 |
'duplicates' => false,
|
|
|
1373 |
],
|
|
|
1374 |
];
|
|
|
1375 |
}
|
|
|
1376 |
|
|
|
1377 |
/**
|
|
|
1378 |
* Test backpack creation/update with auth details provided
|
|
|
1379 |
*
|
|
|
1380 |
* @param boolean $isadmin
|
|
|
1381 |
* @param boolean $updatetest
|
|
|
1382 |
* @dataProvider badges_create_site_backpack_provider
|
|
|
1383 |
*/
|
11 |
efrain |
1384 |
public function test_badges_create_site_backpack($isadmin, $updatetest): void {
|
1 |
efrain |
1385 |
global $DB;
|
|
|
1386 |
$this->resetAfterTest();
|
|
|
1387 |
|
|
|
1388 |
$data = [
|
|
|
1389 |
'apiversion' => 2,
|
|
|
1390 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1391 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1392 |
];
|
|
|
1393 |
|
|
|
1394 |
$data['backpackemail'] = 'test@test.com';
|
|
|
1395 |
$data['password'] = 'test';
|
|
|
1396 |
if ($isadmin || $updatetest) {
|
|
|
1397 |
$this->setAdminUser();
|
|
|
1398 |
$lastmax = $DB->get_field_sql('SELECT MAX(sortorder) FROM {badge_external_backpack}');
|
|
|
1399 |
$backpack = badges_create_site_backpack((object) $data);
|
|
|
1400 |
}
|
|
|
1401 |
|
|
|
1402 |
if ($isadmin) {
|
|
|
1403 |
if ($updatetest) {
|
|
|
1404 |
$record = $DB->get_record('badge_backpack', ['userid' => 0]);
|
|
|
1405 |
$data['badgebackpack'] = $record->id;
|
|
|
1406 |
$data['backpackapiurl'] = 'https://api.ca.badgr.io/v3';
|
|
|
1407 |
badges_update_site_backpack($backpack, (object)$data);
|
|
|
1408 |
}
|
|
|
1409 |
$record = $DB->get_record('badge_external_backpack', ['id' => $backpack]);
|
|
|
1410 |
$this->assertEquals($data['backpackweburl'], $record->backpackweburl);
|
|
|
1411 |
$this->assertEquals($data['backpackapiurl'], $record->backpackapiurl);
|
|
|
1412 |
$this->assertEquals($lastmax + 1, $record->sortorder);
|
|
|
1413 |
$record = $DB->get_record('badge_backpack', ['userid' => 0]);
|
|
|
1414 |
$this->assertNotEmpty($record);
|
|
|
1415 |
} else {
|
|
|
1416 |
$user = $this->getDataGenerator()->create_user();
|
|
|
1417 |
$this->setUser($user);
|
|
|
1418 |
$this->expectException('required_capability_exception');
|
|
|
1419 |
if ($updatetest) {
|
|
|
1420 |
$result = badges_update_site_backpack($backpack, (object) $data);
|
|
|
1421 |
} else {
|
|
|
1422 |
$result = badges_create_site_backpack((object)$data);
|
|
|
1423 |
}
|
|
|
1424 |
}
|
|
|
1425 |
}
|
|
|
1426 |
|
|
|
1427 |
/**
|
|
|
1428 |
* Provider for test_badges_(create/update)_site_backpack
|
|
|
1429 |
*/
|
|
|
1430 |
public function badges_create_site_backpack_provider() {
|
|
|
1431 |
return [
|
|
|
1432 |
"Test as admin user - creation test" => [true, true],
|
|
|
1433 |
"Test as admin user - update test" => [true, false],
|
|
|
1434 |
"Test as normal user - creation test" => [false, true],
|
|
|
1435 |
"Test as normal user - update test" => [false, false],
|
|
|
1436 |
];
|
|
|
1437 |
}
|
|
|
1438 |
|
|
|
1439 |
/**
|
|
|
1440 |
* Test the badges_open_badges_backpack_api with different backpacks
|
|
|
1441 |
*/
|
11 |
efrain |
1442 |
public function test_badges_open_badges_backpack_api(): void {
|
1 |
efrain |
1443 |
$this->resetAfterTest();
|
|
|
1444 |
|
|
|
1445 |
$data = [
|
|
|
1446 |
'apiversion' => 2,
|
|
|
1447 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1448 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1449 |
'sortorder' => 2,
|
|
|
1450 |
];
|
|
|
1451 |
|
|
|
1452 |
// Given a complete set of unique data, a new backpack and auth records should exist in the tables.
|
|
|
1453 |
$data['backpackemail'] = 'test@test.com';
|
|
|
1454 |
$data['password'] = 'test';
|
|
|
1455 |
$backpack1 = badges_save_external_backpack((object) $data);
|
|
|
1456 |
$data['backpackweburl'] = 'https://eu.badgr.io';
|
|
|
1457 |
$data['backpackapiurl'] = 'https://api.eu.badgr.io/v2';
|
|
|
1458 |
$data['apiversion'] = '2.1';
|
|
|
1459 |
$data['sortorder'] = 3;
|
|
|
1460 |
$backpack2 = badges_save_external_backpack((object) $data);
|
|
|
1461 |
|
|
|
1462 |
// Move backpack2 to the first position to set it as primary site backpack.
|
|
|
1463 |
$this->move_backpack_to_first_position($backpack2);
|
|
|
1464 |
|
|
|
1465 |
// The default response should check the default site backpack api version.
|
|
|
1466 |
$this->assertEquals(2.1, badges_open_badges_backpack_api());
|
|
|
1467 |
// Check the api version for the other backpack created.
|
|
|
1468 |
$this->assertEquals(2, badges_open_badges_backpack_api($backpack1));
|
|
|
1469 |
$this->assertEquals(2.1, badges_open_badges_backpack_api($backpack2));
|
|
|
1470 |
}
|
|
|
1471 |
|
|
|
1472 |
/**
|
|
|
1473 |
* Test the badges_get_site_backpack function
|
|
|
1474 |
*/
|
11 |
efrain |
1475 |
public function test_badges_get_site_backpack(): void {
|
1 |
efrain |
1476 |
$this->resetAfterTest();
|
|
|
1477 |
$user = $this->getDataGenerator()->create_user();
|
|
|
1478 |
$data = [
|
|
|
1479 |
'apiversion' => '2',
|
|
|
1480 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1481 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1482 |
];
|
|
|
1483 |
$backpack1 = badges_save_external_backpack((object) $data);
|
|
|
1484 |
$data2 = array_merge($data, [
|
|
|
1485 |
'backpackapiurl' => 'https://api.eu.badgr.io/v2',
|
|
|
1486 |
'backpackweburl' => 'https://eu.badgr.io',
|
|
|
1487 |
'backpackemail' => 'test@test.com',
|
|
|
1488 |
'password' => 'test',
|
|
|
1489 |
]);
|
|
|
1490 |
$backpack2 = badges_save_external_backpack((object) $data2);
|
|
|
1491 |
$data3 = array_merge($data2, [
|
|
|
1492 |
'userid' => $user->id,
|
|
|
1493 |
'externalbackpackid' => $backpack2,
|
|
|
1494 |
'backpackemail' => 'test2@test.com'
|
|
|
1495 |
]);
|
|
|
1496 |
// In the following case, the id returned below equals backpack2. So we aren't storing it.
|
|
|
1497 |
badges_save_backpack_credentials((object) $data3);
|
|
|
1498 |
unset($data3['userid']);
|
|
|
1499 |
|
|
|
1500 |
// Get a site back based on the id returned from creation and no user id provided.
|
|
|
1501 |
$this->assertEquals($data, array_intersect($data, (array) badges_get_site_backpack($backpack1)));
|
|
|
1502 |
$this->assertEquals($data2, array_intersect($data2, (array) badges_get_site_backpack($backpack2)));
|
|
|
1503 |
$this->assertEquals($data2, array_intersect($data2, (array) badges_get_site_backpack($backpack2, 0)));
|
|
|
1504 |
$this->assertEquals($data3, array_intersect($data3, (array) badges_get_site_backpack($backpack2, $user->id)));
|
|
|
1505 |
|
|
|
1506 |
// Non-existent user backpack should return only configuration details and not auth details.
|
|
|
1507 |
$userbackpack = badges_get_site_backpack($backpack1, $user->id);
|
|
|
1508 |
$this->assertNull($userbackpack->badgebackpack);
|
|
|
1509 |
$this->assertNull($userbackpack->password);
|
|
|
1510 |
$this->assertNull($userbackpack->backpackemail);
|
|
|
1511 |
}
|
|
|
1512 |
|
|
|
1513 |
/**
|
|
|
1514 |
* Test the badges_get_user_backpack function
|
|
|
1515 |
*/
|
11 |
efrain |
1516 |
public function test_badges_get_user_backpack(): void {
|
1 |
efrain |
1517 |
$this->resetAfterTest();
|
|
|
1518 |
$user = $this->getDataGenerator()->create_user();
|
|
|
1519 |
$data = [
|
|
|
1520 |
'apiversion' => '2',
|
|
|
1521 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1522 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1523 |
];
|
|
|
1524 |
$backpack1 = badges_save_external_backpack((object) $data);
|
|
|
1525 |
$data2 = array_merge($data, [
|
|
|
1526 |
'backpackapiurl' => 'https://api.eu.badgr.io/v2',
|
|
|
1527 |
'backpackweburl' => 'https://eu.badgr.io',
|
|
|
1528 |
'backpackemail' => 'test@test.com',
|
|
|
1529 |
'password' => 'test',
|
|
|
1530 |
]);
|
|
|
1531 |
$backpack2 = badges_save_external_backpack((object) $data2);
|
|
|
1532 |
$data3 = array_merge($data2, [
|
|
|
1533 |
'userid' => $user->id,
|
|
|
1534 |
'externalbackpackid' => $backpack2,
|
|
|
1535 |
'backpackemail' => 'test2@test.com'
|
|
|
1536 |
]);
|
|
|
1537 |
// In the following case, the id returned below equals backpack2. So we aren't storing it.
|
|
|
1538 |
badges_save_backpack_credentials((object) $data3);
|
|
|
1539 |
unset($data3['userid']);
|
|
|
1540 |
|
|
|
1541 |
// Currently logged in as admin.
|
|
|
1542 |
$this->assertEquals($data2, array_intersect($data2, (array) badges_get_user_backpack()));
|
|
|
1543 |
$this->assertEquals($data2, array_intersect($data2, (array) badges_get_user_backpack(0)));
|
|
|
1544 |
$this->assertEquals($data3, array_intersect($data3, (array) badges_get_user_backpack($user->id)));
|
|
|
1545 |
|
|
|
1546 |
// Non-existent user backpack should return nothing.
|
|
|
1547 |
$this->assertFalse(badges_get_user_backpack($backpack1, $user->id));
|
|
|
1548 |
|
|
|
1549 |
// Login as user.
|
|
|
1550 |
$this->setUser($user);
|
|
|
1551 |
$this->assertEquals($data3, array_intersect($data3, (array) badges_get_user_backpack()));
|
|
|
1552 |
}
|
|
|
1553 |
|
|
|
1554 |
/**
|
|
|
1555 |
* Test the badges_get_site_primary_backpack function
|
|
|
1556 |
*
|
|
|
1557 |
* @param boolean $withauth Testing with authentication or not.
|
|
|
1558 |
* @dataProvider badges_get_site_primary_backpack_provider
|
|
|
1559 |
*/
|
11 |
efrain |
1560 |
public function test_badges_get_site_primary_backpack($withauth): void {
|
1 |
efrain |
1561 |
$data = [
|
|
|
1562 |
'apiversion' => '2',
|
|
|
1563 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1564 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1565 |
'sortorder' => '2',
|
|
|
1566 |
];
|
|
|
1567 |
if ($withauth) {
|
|
|
1568 |
$data = array_merge($data, [
|
|
|
1569 |
'backpackemail' => 'test@test.com',
|
|
|
1570 |
'password' => 'test',
|
|
|
1571 |
]);
|
|
|
1572 |
}
|
|
|
1573 |
$backpack = badges_save_external_backpack((object) $data);
|
|
|
1574 |
|
|
|
1575 |
// Check the backpack created is not the primary one.
|
|
|
1576 |
$sitebackpack = badges_get_site_primary_backpack();
|
|
|
1577 |
$this->assertNotEquals($backpack, $sitebackpack->id);
|
|
|
1578 |
|
|
|
1579 |
// Move backpack to the first position to set it as primary site backpack.
|
|
|
1580 |
$this->move_backpack_to_first_position($backpack);
|
|
|
1581 |
|
|
|
1582 |
$sitebackpack = badges_get_site_primary_backpack();
|
|
|
1583 |
$this->assertEquals($backpack, $sitebackpack->id);
|
|
|
1584 |
|
|
|
1585 |
if ($withauth) {
|
|
|
1586 |
$this->assertEquals($data, array_intersect($data, (array) $sitebackpack));
|
|
|
1587 |
$this->assertEquals($data['password'], $sitebackpack->password);
|
|
|
1588 |
$this->assertEquals($data['backpackemail'], $sitebackpack->backpackemail);
|
|
|
1589 |
} else {
|
|
|
1590 |
$this->assertNull($sitebackpack->badgebackpack);
|
|
|
1591 |
$this->assertNull($sitebackpack->password);
|
|
|
1592 |
$this->assertNull($sitebackpack->backpackemail);
|
|
|
1593 |
}
|
|
|
1594 |
}
|
|
|
1595 |
|
|
|
1596 |
/**
|
|
|
1597 |
* Test the test_badges_get_site_primary_backpack function.
|
|
|
1598 |
*
|
|
|
1599 |
* @return array
|
|
|
1600 |
*/
|
|
|
1601 |
public function badges_get_site_primary_backpack_provider() {
|
|
|
1602 |
return [
|
|
|
1603 |
"Test with auth details" => [true],
|
|
|
1604 |
"Test without auth details" => [false],
|
|
|
1605 |
];
|
|
|
1606 |
}
|
|
|
1607 |
|
|
|
1608 |
/**
|
|
|
1609 |
* Test badges_change_sortorder_backpacks().
|
|
|
1610 |
*
|
|
|
1611 |
* @dataProvider badges_change_sortorder_backpacks_provider
|
|
|
1612 |
* @covers ::badges_change_sortorder_backpacks
|
|
|
1613 |
*
|
|
|
1614 |
* @param int $backpacktomove Backpack index to move (from 0 to 5).
|
|
|
1615 |
* @param int $direction Direction to move the backpack.
|
|
|
1616 |
* @param int|null $expectedsortorder Expected sortorder or null if an exception is expected.
|
|
|
1617 |
*/
|
|
|
1618 |
public function test_badges_change_sortorder_backpacks(int $backpacktomove, int $direction, ?int $expectedsortorder): void {
|
|
|
1619 |
global $DB;
|
|
|
1620 |
|
|
|
1621 |
$this->resetAfterTest();
|
|
|
1622 |
$this->setAdminUser();
|
|
|
1623 |
|
|
|
1624 |
// Create 5 more backpacks.
|
|
|
1625 |
for ($i = 0; $i < 5; $i++) {
|
|
|
1626 |
$data = new \stdClass();
|
|
|
1627 |
$data->apiversion = OPEN_BADGES_V2P1;
|
|
|
1628 |
$data->backpackapiurl = "https://myurl$i.cat/ob/v2p1";
|
|
|
1629 |
$data->backpackweburl = "https://myurl$i.cat";
|
|
|
1630 |
badges_create_site_backpack($data);
|
|
|
1631 |
}
|
|
|
1632 |
|
|
|
1633 |
// Check there are 6 backpacks (1 pre-existing + 5 news).
|
|
|
1634 |
$total = $DB->count_records('badge_external_backpack');
|
|
|
1635 |
$this->assertEquals(6, $total);
|
|
|
1636 |
$backpacks = array_values(badges_get_site_backpacks());
|
|
|
1637 |
|
|
|
1638 |
if (is_null($expectedsortorder)) {
|
|
|
1639 |
$this->expectException('moodle_exception');
|
|
|
1640 |
}
|
|
|
1641 |
|
|
|
1642 |
// Move the backpack.
|
|
|
1643 |
badges_change_sortorder_backpacks($backpacks[$backpacktomove]->id, $direction);
|
|
|
1644 |
|
|
|
1645 |
if (!is_null($expectedsortorder)) {
|
|
|
1646 |
$backpack = badges_get_site_backpack($backpacks[$backpacktomove]->id);
|
|
|
1647 |
$this->assertEquals($expectedsortorder, $backpack->sortorder);
|
|
|
1648 |
}
|
|
|
1649 |
}
|
|
|
1650 |
|
|
|
1651 |
/**
|
|
|
1652 |
* Provider for test_badges_change_sortorder_backpacks.
|
|
|
1653 |
*
|
|
|
1654 |
* @return array
|
|
|
1655 |
*/
|
|
|
1656 |
public function badges_change_sortorder_backpacks_provider(): array {
|
|
|
1657 |
return [
|
|
|
1658 |
"Test up" => [
|
|
|
1659 |
'backpacktomove' => 1,
|
|
|
1660 |
'direction' => BACKPACK_MOVE_UP,
|
|
|
1661 |
'expectedsortorder' => 1,
|
|
|
1662 |
],
|
|
|
1663 |
"Test down" => [
|
|
|
1664 |
'backpacktomove' => 1,
|
|
|
1665 |
'direction' => BACKPACK_MOVE_DOWN,
|
|
|
1666 |
'expectedsortorder' => 3,
|
|
|
1667 |
],
|
|
|
1668 |
"Test up the very first element" => [
|
|
|
1669 |
'backpacktomove' => 0,
|
|
|
1670 |
'direction' => BACKPACK_MOVE_UP,
|
|
|
1671 |
'expectedsortorder' => 1,
|
|
|
1672 |
],
|
|
|
1673 |
"Test down the very last element" => [
|
|
|
1674 |
'backpacktomove' => 5,
|
|
|
1675 |
'direction' => BACKPACK_MOVE_DOWN,
|
|
|
1676 |
'expectedsortorder' => 6,
|
|
|
1677 |
],
|
|
|
1678 |
"Test with an invalid direction value" => [
|
|
|
1679 |
'backpacktomove' => 1,
|
|
|
1680 |
'direction' => 10,
|
|
|
1681 |
'expectedsortorder' => null,
|
|
|
1682 |
],
|
|
|
1683 |
];
|
|
|
1684 |
}
|
|
|
1685 |
|
|
|
1686 |
/**
|
|
|
1687 |
* Test the Badgr URL generator function
|
|
|
1688 |
*
|
|
|
1689 |
* @param mixed $type Type corresponding to the badge entites
|
|
|
1690 |
* @param string $expected Expected string result
|
|
|
1691 |
* @dataProvider badgr_open_url_generator
|
|
|
1692 |
*/
|
11 |
efrain |
1693 |
public function test_badges_generate_badgr_open_url($type, $expected): void {
|
1 |
efrain |
1694 |
$data = [
|
|
|
1695 |
'apiversion' => '2',
|
|
|
1696 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1697 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1698 |
'backpackemail' => 'test@test.com',
|
|
|
1699 |
'password' => 'test',
|
|
|
1700 |
];
|
|
|
1701 |
$backpack2 = badges_save_external_backpack((object) $data);
|
|
|
1702 |
$backpack = badges_get_site_backpack($backpack2);
|
|
|
1703 |
$this->assertEquals($expected, badges_generate_badgr_open_url($backpack, $type, 123455));
|
|
|
1704 |
}
|
|
|
1705 |
|
|
|
1706 |
/**
|
|
|
1707 |
* Data provider for test_badges_generate_badgr_open_url
|
|
|
1708 |
* @return array
|
|
|
1709 |
*/
|
|
|
1710 |
public function badgr_open_url_generator() {
|
|
|
1711 |
return [
|
|
|
1712 |
'Badgr Assertion URL test' => [
|
|
|
1713 |
OPEN_BADGES_V2_TYPE_ASSERTION, "https://api.ca.badgr.io/public/assertions/123455"
|
|
|
1714 |
],
|
|
|
1715 |
'Badgr Issuer URL test' => [
|
|
|
1716 |
OPEN_BADGES_V2_TYPE_ISSUER, "https://api.ca.badgr.io/public/issuers/123455"
|
|
|
1717 |
],
|
|
|
1718 |
'Badgr Badge URL test' => [
|
|
|
1719 |
OPEN_BADGES_V2_TYPE_BADGE, "https://api.ca.badgr.io/public/badges/123455"
|
|
|
1720 |
]
|
|
|
1721 |
];
|
|
|
1722 |
}
|
|
|
1723 |
|
|
|
1724 |
/**
|
|
|
1725 |
* Test badges_external_get_mapping function
|
|
|
1726 |
*
|
|
|
1727 |
* @param int $internalid The internal id of the mapping
|
|
|
1728 |
* @param int $externalid The external / remote ref to the mapping
|
|
|
1729 |
* @param mixed $expected The expected result from the function
|
|
|
1730 |
* @param string|null $field The field we are passing to the function. Null if we don't want to pass anything.ss
|
|
|
1731 |
*
|
|
|
1732 |
* @dataProvider badges_external_get_mapping_provider
|
|
|
1733 |
*/
|
11 |
efrain |
1734 |
public function test_badges_external_get_mapping($internalid, $externalid, $expected, $field = null): void {
|
1 |
efrain |
1735 |
$data = [
|
|
|
1736 |
'apiversion' => '2',
|
|
|
1737 |
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
|
|
|
1738 |
'backpackweburl' => 'https://ca.badgr.io',
|
|
|
1739 |
'backpackemail' => 'test@test.com',
|
|
|
1740 |
'password' => 'test',
|
|
|
1741 |
];
|
|
|
1742 |
$backpack2 = badges_save_external_backpack((object) $data);
|
|
|
1743 |
badges_external_create_mapping($backpack2, OPEN_BADGES_V2_TYPE_BADGE, $internalid, $externalid);
|
|
|
1744 |
$expected = $expected == "id" ? $backpack2 : $expected;
|
|
|
1745 |
if ($field) {
|
|
|
1746 |
$this->assertEquals($expected, badges_external_get_mapping($backpack2, OPEN_BADGES_V2_TYPE_BADGE, $internalid, $field));
|
|
|
1747 |
} else {
|
|
|
1748 |
$this->assertEquals($expected, badges_external_get_mapping($backpack2, OPEN_BADGES_V2_TYPE_BADGE, $internalid));
|
|
|
1749 |
}
|
|
|
1750 |
}
|
|
|
1751 |
|
|
|
1752 |
/**
|
|
|
1753 |
* Data provider for badges_external_get_mapping_provider
|
|
|
1754 |
*
|
|
|
1755 |
* @return array
|
|
|
1756 |
*/
|
|
|
1757 |
public function badges_external_get_mapping_provider() {
|
|
|
1758 |
return [
|
|
|
1759 |
"Get the site backpack value" => [
|
|
|
1760 |
1234, 4321, 'id', 'sitebackpackid'
|
|
|
1761 |
],
|
|
|
1762 |
"Get the type of the mapping" => [
|
|
|
1763 |
1234, 4321, OPEN_BADGES_V2_TYPE_BADGE, 'type'
|
|
|
1764 |
],
|
|
|
1765 |
"Get the externalid of the mapping" => [
|
|
|
1766 |
1234, 4321, 4321, 'externalid'
|
|
|
1767 |
],
|
|
|
1768 |
"Get the externalid of the mapping without providing a param" => [
|
|
|
1769 |
1234, 4321, 4321, null
|
|
|
1770 |
],
|
|
|
1771 |
"Get the internalid of the mapping" => [
|
|
|
1772 |
1234, 4321, 1234, 'internalid'
|
|
|
1773 |
]
|
|
|
1774 |
];
|
|
|
1775 |
}
|
|
|
1776 |
|
|
|
1777 |
/**
|
|
|
1778 |
* Move the backpack to the first position, to set it as primary site backpack.
|
|
|
1779 |
*
|
|
|
1780 |
* @param int $backpackid The backpack identifier.
|
|
|
1781 |
*/
|
|
|
1782 |
private function move_backpack_to_first_position(int $backpackid): void {
|
|
|
1783 |
$backpack = badges_get_site_backpack($backpackid);
|
|
|
1784 |
while ($backpack->sortorder > 1) {
|
|
|
1785 |
badges_change_sortorder_backpacks($backpackid, BACKPACK_MOVE_UP);
|
|
|
1786 |
$backpack = badges_get_site_backpack($backpackid);
|
|
|
1787 |
}
|
|
|
1788 |
}
|
|
|
1789 |
|
|
|
1790 |
/**
|
|
|
1791 |
* Testing function test_badge_get_tagged_badges - search tagged badges
|
|
|
1792 |
*
|
|
|
1793 |
* @covers ::badge_get_tagged_badges
|
|
|
1794 |
*/
|
11 |
efrain |
1795 |
public function test_badge_get_tagged_badges(): void {
|
1 |
efrain |
1796 |
$this->resetAfterTest();
|
|
|
1797 |
$this->setAdminUser();
|
|
|
1798 |
|
|
|
1799 |
// Setup test data.
|
|
|
1800 |
$badge = new badge($this->coursebadge);
|
|
|
1801 |
\core_tag_tag::set_item_tags('core_badges', 'badge', $this->badgeid, $badge->get_context(),
|
|
|
1802 |
['Cats', 'Dogs']);
|
|
|
1803 |
|
|
|
1804 |
$tag = \core_tag_tag::get_by_name(0, 'Cats');
|
|
|
1805 |
|
|
|
1806 |
$res = badge_get_tagged_badges($tag, false, 0, 0, 1, 0);
|
|
|
1807 |
$this->assertStringContainsString("Test badge with 'apostrophe' and other friends (<>&@#)", $res->content);
|
|
|
1808 |
}
|
|
|
1809 |
}
|