on-demand release 3.6dev+
[moodle.git] / badges / tests / badgeslib_test.php
CommitLineData
27806552
YB
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
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->libdir . '/badgeslib.php');
bef0d6b0 31require_once($CFG->dirroot . '/badges/lib.php');
27806552 32
f007e899 33class core_badges_badgeslib_testcase extends advanced_testcase {
27806552 34 protected $badgeid;
cdc54199
RT
35 protected $course;
36 protected $user;
37 protected $module;
38 protected $coursebadge;
853e506a 39 protected $assertion;
27806552
YB
40
41 protected function setUp() {
cdc54199 42 global $DB, $CFG;
27806552
YB
43 $this->resetAfterTest(true);
44
7fbe33fc
MG
45 $CFG->enablecompletion = true;
46
27806552
YB
47 $user = $this->getDataGenerator()->create_user();
48
49 $fordb = new stdClass();
50 $fordb->id = null;
d77a6026 51 $fordb->name = "Test badge with 'apostrophe' and other friends (<>&@#)";
27806552
YB
52 $fordb->description = "Testing badges";
53 $fordb->timecreated = time();
54 $fordb->timemodified = time();
55 $fordb->usercreated = $user->id;
56 $fordb->usermodified = $user->id;
27806552
YB
57 $fordb->issuername = "Test issuer";
58 $fordb->issuerurl = "http://issuer-url.domain.co.nz";
853e506a 59 $fordb->issuercontact = "issuer@example.com";
27806552
YB
60 $fordb->expiredate = null;
61 $fordb->expireperiod = null;
62 $fordb->type = BADGE_TYPE_SITE;
63 $fordb->courseid = null;
64 $fordb->messagesubject = "Test message subject";
65 $fordb->message = "Test message body";
66 $fordb->attachment = 1;
67 $fordb->notification = 0;
68 $fordb->status = BADGE_STATUS_INACTIVE;
69
70 $this->badgeid = $DB->insert_record('badge', $fordb, true);
cdc54199
RT
71
72 // Create a course with activity and auto completion tracking.
7fbe33fc 73 $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
cdc54199
RT
74 $this->user = $this->getDataGenerator()->create_user();
75 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
76 $this->assertNotEmpty($studentrole);
77
78 // Get manual enrolment plugin and enrol user.
79 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
80 $manplugin = enrol_get_plugin('manual');
81 $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
82 $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
83 $this->assertEquals(1, $DB->count_records('user_enrolments'));
84
85 $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
86 $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
87
88 // Build badge and criteria.
89 $fordb->type = BADGE_TYPE_COURSE;
90 $fordb->courseid = $this->course->id;
91 $fordb->status = BADGE_STATUS_ACTIVE;
92
93 $this->coursebadge = $DB->insert_record('badge', $fordb, true);
853e506a
Y
94 $this->assertion = new stdClass();
95 $this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s"}';
96 $this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s"}';
97 $this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
27806552
YB
98 }
99
100 public function test_create_badge() {
101 $badge = new badge($this->badgeid);
102
103 $this->assertInstanceOf('badge', $badge);
104 $this->assertEquals($this->badgeid, $badge->id);
105 }
106
107 public function test_clone_badge() {
108 $badge = new badge($this->badgeid);
109 $newid = $badge->make_clone();
110 $cloned_badge = new badge($newid);
111
27806552
YB
112 $this->assertEquals($badge->description, $cloned_badge->description);
113 $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
114 $this->assertEquals($badge->issuername, $cloned_badge->issuername);
853e506a 115 $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
27806552
YB
116 $this->assertEquals($badge->issuerurl, $cloned_badge->issuerurl);
117 $this->assertEquals($badge->expiredate, $cloned_badge->expiredate);
118 $this->assertEquals($badge->expireperiod, $cloned_badge->expireperiod);
119 $this->assertEquals($badge->type, $cloned_badge->type);
120 $this->assertEquals($badge->courseid, $cloned_badge->courseid);
121 $this->assertEquals($badge->message, $cloned_badge->message);
122 $this->assertEquals($badge->messagesubject, $cloned_badge->messagesubject);
123 $this->assertEquals($badge->attachment, $cloned_badge->attachment);
124 $this->assertEquals($badge->notification, $cloned_badge->notification);
125 }
126
127 public function test_badge_status() {
128 $badge = new badge($this->badgeid);
129 $old_status = $badge->status;
130 $badge->set_status(BADGE_STATUS_ACTIVE);
131 $this->assertAttributeNotEquals($old_status, 'status', $badge);
132 $this->assertAttributeEquals(BADGE_STATUS_ACTIVE, 'status', $badge);
133 }
134
135 public function test_delete_badge() {
136 $badge = new badge($this->badgeid);
137 $badge->delete();
138 // We don't actually delete badges. We archive them.
139 $this->assertAttributeEquals(BADGE_STATUS_ARCHIVED, 'status', $badge);
140 }
141
142 public function test_create_badge_criteria() {
143 $badge = new badge($this->badgeid);
144 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
145 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
146
147 $this->assertCount(1, $badge->get_criteria());
148
149 $criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
150 $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
151 $criteria_profile->save($params);
152
153 $this->assertCount(2, $badge->get_criteria());
154 }
155
3784d3be
YB
156 public function test_add_badge_criteria_description() {
157 $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
fffeb03f
YB
158 $criteriaoverall->save(array(
159 'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
160 'description' => 'Overall description',
161 'descriptionformat' => FORMAT_HTML
162 ));
3784d3be
YB
163
164 $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
fffeb03f
YB
165 $params = array(
166 'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
167 'field_address' => 'address',
168 'description' => 'Description',
169 'descriptionformat' => FORMAT_HTML
170 );
3784d3be
YB
171 $criteriaprofile->save($params);
172
173 $badge = new badge($this->badgeid);
174 $this->assertEquals('Overall description', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->description);
175 $this->assertEquals('Description', $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->description);
176 }
177
27806552
YB
178 public function test_delete_badge_criteria() {
179 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
180 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
181 $badge = new badge($this->badgeid);
182
183 $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
184
185 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
186 $this->assertEmpty($badge->get_criteria());
187 }
188
189 public function test_badge_awards() {
be2b37cf 190 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
27806552
YB
191 $badge = new badge($this->badgeid);
192 $user1 = $this->getDataGenerator()->create_user();
193
194 $badge->issue($user1->id, true);
195 $this->assertTrue($badge->is_issued($user1->id));
196
197 $user2 = $this->getDataGenerator()->create_user();
198 $badge->issue($user2->id, true);
199 $this->assertTrue($badge->is_issued($user2->id));
200
201 $this->assertCount(2, $badge->get_awards());
202 }
203
d237385e
SH
204 /**
205 * Test the {@link badges_get_user_badges()} function in lib/badgeslib.php
206 */
207 public function test_badges_get_user_badges() {
208 global $DB;
209
210 // Messaging is not compatible with transactions.
211 $this->preventResetByRollback();
212
213 $badges = array();
214 $user1 = $this->getDataGenerator()->create_user();
215 $user2 = $this->getDataGenerator()->create_user();
216
217 // Record the current time, we need to be precise about a couple of things.
218 $now = time();
219 // Create 11 badges with which to test.
220 for ($i = 1; $i <= 11; $i++) {
221 // Mock up a badge.
222 $badge = new stdClass();
223 $badge->id = null;
224 $badge->name = "Test badge $i";
225 $badge->description = "Testing badges $i";
226 $badge->timecreated = $now - 12;
227 $badge->timemodified = $now - 12;
228 $badge->usercreated = $user1->id;
229 $badge->usermodified = $user1->id;
230 $badge->issuername = "Test issuer";
231 $badge->issuerurl = "http://issuer-url.domain.co.nz";
232 $badge->issuercontact = "issuer@example.com";
233 $badge->expiredate = null;
234 $badge->expireperiod = null;
235 $badge->type = BADGE_TYPE_SITE;
236 $badge->courseid = null;
237 $badge->messagesubject = "Test message subject for badge $i";
238 $badge->message = "Test message body for badge $i";
239 $badge->attachment = 1;
240 $badge->notification = 0;
241 $badge->status = BADGE_STATUS_INACTIVE;
242
243 $badgeid = $DB->insert_record('badge', $badge, true);
244 $badges[$badgeid] = new badge($badgeid);
245 $badges[$badgeid]->issue($user2->id, true);
246 // Check it all actually worked.
247 $this->assertCount(1, $badges[$badgeid]->get_awards());
248
249 // Hack the database to adjust the time each badge was issued.
250 // The alternative to this is sleep which is a no-no in unit tests.
251 $DB->set_field('badge_issued', 'dateissued', $now - 11 + $i, array('userid' => $user2->id, 'badgeid' => $badgeid));
252 }
253
254 // Make sure the first user has no badges.
255 $result = badges_get_user_badges($user1->id);
256 $this->assertInternalType('array', $result);
257 $this->assertCount(0, $result);
258
259 // Check that the second user has the expected 11 badges.
260 $result = badges_get_user_badges($user2->id);
261 $this->assertCount(11, $result);
262
263 // Test pagination.
264 // Ordering is by time issued desc, so things will come out with the last awarded badge first.
265 $result = badges_get_user_badges($user2->id, 0, 0, 4);
266 $this->assertCount(4, $result);
267 $lastbadgeissued = reset($result);
268 $this->assertSame('Test badge 11', $lastbadgeissued->name);
269 // Page 2. Expecting 4 results again.
270 $result = badges_get_user_badges($user2->id, 0, 1, 4);
271 $this->assertCount(4, $result);
272 $lastbadgeissued = reset($result);
273 $this->assertSame('Test badge 7', $lastbadgeissued->name);
274 // Page 3. Expecting just three results here.
275 $result = badges_get_user_badges($user2->id, 0, 2, 4);
276 $this->assertCount(3, $result);
277 $lastbadgeissued = reset($result);
278 $this->assertSame('Test badge 3', $lastbadgeissued->name);
279 // Page 4.... there is no page 4.
280 $result = badges_get_user_badges($user2->id, 0, 3, 4);
281 $this->assertCount(0, $result);
282
283 // Test search.
284 $result = badges_get_user_badges($user2->id, 0, 0, 0, 'badge 1');
285 $this->assertCount(3, $result);
286 $lastbadgeissued = reset($result);
287 $this->assertSame('Test badge 11', $lastbadgeissued->name);
288 // The term Totara doesn't appear anywhere in the badges.
289 $result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara');
290 $this->assertCount(0, $result);
b4846b88
AO
291
292 // Issue a user with a course badge and verify its returned based on if
293 // coursebadges are enabled or disabled.
294 $sitebadgeid = key($badges);
295 $badges[$sitebadgeid]->issue($this->user->id, true);
296
297 $badge = new stdClass();
298 $badge->id = null;
299 $badge->name = "Test course badge";
300 $badge->description = "Testing course badge";
301 $badge->timecreated = $now;
302 $badge->timemodified = $now;
303 $badge->usercreated = $user1->id;
304 $badge->usermodified = $user1->id;
305 $badge->issuername = "Test issuer";
306 $badge->issuerurl = "http://issuer-url.domain.co.nz";
307 $badge->issuercontact = "issuer@example.com";
308 $badge->expiredate = null;
309 $badge->expireperiod = null;
310 $badge->type = BADGE_TYPE_COURSE;
311 $badge->courseid = $this->course->id;
312 $badge->messagesubject = "Test message subject for course badge";
313 $badge->message = "Test message body for course badge";
314 $badge->attachment = 1;
315 $badge->notification = 0;
316 $badge->status = BADGE_STATUS_ACTIVE;
317
318 $badgeid = $DB->insert_record('badge', $badge, true);
319 $badges[$badgeid] = new badge($badgeid);
320 $badges[$badgeid]->issue($this->user->id, true);
321
322 // With coursebadges off, we should only get the site badge.
323 set_config('badges_allowcoursebadges', false);
324 $result = badges_get_user_badges($this->user->id);
325 $this->assertCount(1, $result);
326
327 // With it on, we should get both.
328 set_config('badges_allowcoursebadges', true);
329 $result = badges_get_user_badges($this->user->id);
330 $this->assertCount(2, $result);
331
d237385e
SH
332 }
333
27806552
YB
334 public function data_for_message_from_template() {
335 return array(
336 array(
337 'This is a message with no variables',
338 array(), // no params
339 'This is a message with no variables'
340 ),
341 array(
342 'This is a message with %amissing% variables',
343 array(), // no params
344 'This is a message with %amissing% variables'
345 ),
346 array(
347 'This is a message with %one% variable',
348 array('one' => 'a single'),
349 'This is a message with a single variable'
350 ),
351 array(
352 'This is a message with %one% %two% %three% variables',
353 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
354 'This is a message with more than one variables'
355 ),
356 array(
357 'This is a message with %three% %two% %one%',
358 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
359 'This is a message with randomly ordered variables'
360 ),
361 array(
362 'This is a message with %repeated% %one% %repeated% of variables',
363 array('one' => 'and', 'repeated' => 'lots'),
364 'This is a message with lots and lots of variables'
365 ),
366 );
367 }
368
369 /**
370 * @dataProvider data_for_message_from_template
371 */
372 public function test_badge_message_from_template($message, $params, $result) {
373 $this->assertEquals(badge_message_from_template($message, $params), $result);
374 }
375
137d94f3
RT
376 /**
377 * Test badges observer when course module completion event id fired.
137d94f3 378 */
cdc54199 379 public function test_badges_observer_course_module_criteria_review() {
be2b37cf 380 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
cdc54199
RT
381 $badge = new badge($this->coursebadge);
382 $this->assertFalse($badge->is_issued($this->user->id));
137d94f3
RT
383
384 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
385 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
386 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
74b63eae 387 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
137d94f3 388
4f2424ca
MG
389 // Assert the badge will not be issued to the user as is.
390 $badge = new badge($this->coursebadge);
391 $badge->review_all_criteria();
392 $this->assertFalse($badge->is_issued($this->user->id));
393
137d94f3 394 // Set completion for forum activity.
cdc54199 395 $c = new completion_info($this->course);
137d94f3
RT
396 $activities = $c->get_activities();
397 $this->assertEquals(1, count($activities));
cdc54199
RT
398 $this->assertTrue(isset($activities[$this->module->cmid]));
399 $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
137d94f3 400
cdc54199 401 $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
137d94f3
RT
402 $current->completionstate = COMPLETION_COMPLETE;
403 $current->timemodified = time();
f007e899 404 $sink = $this->redirectEmails();
cdc54199 405 $c->internal_set_data($activities[$this->module->cmid], $current);
f007e899
PS
406 $this->assertCount(1, $sink->get_messages());
407 $sink->close();
137d94f3
RT
408
409 // Check if badge is awarded.
410 $this->assertDebuggingCalled('Error baking badge image!');
cdc54199 411 $this->assertTrue($badge->is_issued($this->user->id));
137d94f3 412 }
1cb1e5fe
RT
413
414 /**
415 * Test badges observer when course_completed event is fired.
416 */
417 public function test_badges_observer_course_criteria_review() {
be2b37cf 418 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
1cb1e5fe
RT
419 $badge = new badge($this->coursebadge);
420 $this->assertFalse($badge->is_issued($this->user->id));
421
422 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
423 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
424 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
425 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
426
427 $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
428
4f2424ca
MG
429 // Assert the badge will not be issued to the user as is.
430 $badge = new badge($this->coursebadge);
431 $badge->review_all_criteria();
432 $this->assertFalse($badge->is_issued($this->user->id));
433
1cb1e5fe 434 // Mark course as complete.
f007e899 435 $sink = $this->redirectEmails();
1cb1e5fe 436 $ccompletion->mark_complete();
f007e899
PS
437 $this->assertCount(1, $sink->get_messages());
438 $sink->close();
1cb1e5fe
RT
439
440 // Check if badge is awarded.
441 $this->assertDebuggingCalled('Error baking badge image!');
442 $this->assertTrue($badge->is_issued($this->user->id));
443 }
bb78e249
RT
444
445 /**
446 * Test badges observer when user_updated event is fired.
447 */
448 public function test_badges_observer_profile_criteria_review() {
4f2424ca
MG
449 global $CFG, $DB;
450 require_once($CFG->dirroot.'/user/profile/lib.php');
451
452 // Add a custom field of textarea type.
453 $customprofileid = $DB->insert_record('user_info_field', array(
454 'shortname' => 'newfield', 'name' => 'Description of new field', 'categoryid' => 1,
455 'datatype' => 'textarea'));
456
be2b37cf 457 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
bb78e249 458 $badge = new badge($this->coursebadge);
bb78e249
RT
459
460 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
461 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
462 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
4f2424ca
MG
463 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim',
464 'field_' . $customprofileid => $customprofileid));
465
466 // Assert the badge will not be issued to the user as is.
467 $badge = new badge($this->coursebadge);
468 $badge->review_all_criteria();
469 $this->assertFalse($badge->is_issued($this->user->id));
bb78e249 470
4f2424ca 471 // Set the required fields and make sure the badge got issued.
bb78e249 472 $this->user->address = 'Test address';
c8d2f392 473 $this->user->aim = '999999999';
f007e899 474 $sink = $this->redirectEmails();
4f2424ca 475 profile_save_data((object)array('id' => $this->user->id, 'profile_field_newfield' => 'X'));
bb78e249 476 user_update_user($this->user, false);
f007e899
PS
477 $this->assertCount(1, $sink->get_messages());
478 $sink->close();
bb78e249
RT
479 // Check if badge is awarded.
480 $this->assertDebuggingCalled('Error baking badge image!');
481 $this->assertTrue($badge->is_issued($this->user->id));
482 }
853e506a 483
75653a30
SB
484 /**
485 * Test badges observer when cohort_member_added event is fired.
486 */
487 public function test_badges_observer_cohort_criteria_review() {
488 global $CFG;
489
490 require_once("$CFG->dirroot/cohort/lib.php");
491
492 $cohort = $this->getDataGenerator()->create_cohort();
493
494 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
495 $badge = new badge($this->badgeid);
496 $this->assertFalse($badge->is_issued($this->user->id));
497
498 // Set up the badge criteria.
499 $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
500 $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
501 $criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
502 $criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'cohort_cohorts' => array('0' => $cohort->id)));
503
504 // Make the badge active.
505 $badge->set_status(BADGE_STATUS_ACTIVE);
506
507 // Add the user to the cohort.
508 cohort_add_member($cohort->id, $this->user->id);
509
510 // Verify that the badge was awarded.
511 $this->assertDebuggingCalled();
512 $this->assertTrue($badge->is_issued($this->user->id));
513
514 }
515
853e506a
Y
516 /**
517 * Test badges assertion generated when a badge is issued.
518 */
519 public function test_badges_assertion() {
be2b37cf 520 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
853e506a
Y
521 $badge = new badge($this->coursebadge);
522 $this->assertFalse($badge->is_issued($this->user->id));
523
524 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
525 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
526 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
527 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
528
529 $this->user->address = 'Test address';
f007e899 530 $sink = $this->redirectEmails();
853e506a 531 user_update_user($this->user, false);
f007e899
PS
532 $this->assertCount(1, $sink->get_messages());
533 $sink->close();
853e506a
Y
534 // Check if badge is awarded.
535 $this->assertDebuggingCalled('Error baking badge image!');
536 $awards = $badge->get_awards();
537 $this->assertCount(1, $awards);
538
539 // Get assertion.
540 $award = reset($awards);
541 $assertion = new core_badges_assertion($award->uniquehash);
542 $testassertion = $this->assertion;
543
544 // Make sure JSON strings have the same structure.
545 $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
546 $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
547 $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
548 }
bef0d6b0
AA
549
550 /**
be5e0110 551 * Tests the core_badges_myprofile_navigation() function.
bef0d6b0
AA
552 */
553 public function test_core_badges_myprofile_navigation() {
be5e0110
MN
554 // Set up the test.
555 $tree = new \core_user\output\myprofile\tree();
bef0d6b0 556 $this->setAdminUser();
bef0d6b0 557 $badge = new badge($this->badgeid);
be5e0110
MN
558 $badge->issue($this->user->id, true);
559 $iscurrentuser = true;
bef0d6b0 560 $course = null;
bef0d6b0
AA
561
562 // Enable badges.
563 set_config('enablebadges', true);
bef0d6b0 564
be5e0110
MN
565 // Check the node tree is correct.
566 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
567 $reflector = new ReflectionObject($tree);
568 $nodes = $reflector->getProperty('nodes');
569 $nodes->setAccessible(true);
570 $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
571 }
572
573 /**
574 * Tests the core_badges_myprofile_navigation() function with badges disabled..
575 */
576 public function test_core_badges_myprofile_navigation_badges_disabled() {
577 // Set up the test.
578 $tree = new \core_user\output\myprofile\tree();
579 $this->setAdminUser();
580 $badge = new badge($this->badgeid);
581 $badge->issue($this->user->id, true);
582 $iscurrentuser = false;
583 $course = null;
584
585 // Disable badges.
586 set_config('enablebadges', false);
bef0d6b0 587
be5e0110
MN
588 // Check the node tree is correct.
589 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
590 $reflector = new ReflectionObject($tree);
591 $nodes = $reflector->getProperty('nodes');
592 $nodes->setAccessible(true);
593 $this->assertArrayNotHasKey('localbadges', $nodes->getValue($tree));
594 }
595
596 /**
597 * Tests the core_badges_myprofile_navigation() function with a course badge.
598 */
599 public function test_core_badges_myprofile_navigation_with_course_badge() {
600 // Set up the test.
601 $tree = new \core_user\output\myprofile\tree();
602 $this->setAdminUser();
bef0d6b0 603 $badge = new badge($this->coursebadge);
be5e0110 604 $badge->issue($this->user->id, true);
bef0d6b0 605 $iscurrentuser = false;
be5e0110
MN
606
607 // Check the node tree is correct.
608 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $this->course);
609 $reflector = new ReflectionObject($tree);
610 $nodes = $reflector->getProperty('nodes');
611 $nodes->setAccessible(true);
612 $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
bef0d6b0 613 }
27806552 614}