on-demand release 3.2dev+
[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;
51 $fordb->name = "Test badge";
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);
291 }
292
27806552
YB
293 public function data_for_message_from_template() {
294 return array(
295 array(
296 'This is a message with no variables',
297 array(), // no params
298 'This is a message with no variables'
299 ),
300 array(
301 'This is a message with %amissing% variables',
302 array(), // no params
303 'This is a message with %amissing% variables'
304 ),
305 array(
306 'This is a message with %one% variable',
307 array('one' => 'a single'),
308 'This is a message with a single variable'
309 ),
310 array(
311 'This is a message with %one% %two% %three% variables',
312 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
313 'This is a message with more than one variables'
314 ),
315 array(
316 'This is a message with %three% %two% %one%',
317 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
318 'This is a message with randomly ordered variables'
319 ),
320 array(
321 'This is a message with %repeated% %one% %repeated% of variables',
322 array('one' => 'and', 'repeated' => 'lots'),
323 'This is a message with lots and lots of variables'
324 ),
325 );
326 }
327
328 /**
329 * @dataProvider data_for_message_from_template
330 */
331 public function test_badge_message_from_template($message, $params, $result) {
332 $this->assertEquals(badge_message_from_template($message, $params), $result);
333 }
334
137d94f3
RT
335 /**
336 * Test badges observer when course module completion event id fired.
137d94f3 337 */
cdc54199 338 public function test_badges_observer_course_module_criteria_review() {
be2b37cf 339 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
cdc54199
RT
340 $badge = new badge($this->coursebadge);
341 $this->assertFalse($badge->is_issued($this->user->id));
137d94f3
RT
342
343 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
344 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
345 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
74b63eae 346 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
137d94f3 347
4f2424ca
MG
348 // Assert the badge will not be issued to the user as is.
349 $badge = new badge($this->coursebadge);
350 $badge->review_all_criteria();
351 $this->assertFalse($badge->is_issued($this->user->id));
352
137d94f3 353 // Set completion for forum activity.
cdc54199 354 $c = new completion_info($this->course);
137d94f3
RT
355 $activities = $c->get_activities();
356 $this->assertEquals(1, count($activities));
cdc54199
RT
357 $this->assertTrue(isset($activities[$this->module->cmid]));
358 $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
137d94f3 359
cdc54199 360 $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
137d94f3
RT
361 $current->completionstate = COMPLETION_COMPLETE;
362 $current->timemodified = time();
f007e899 363 $sink = $this->redirectEmails();
cdc54199 364 $c->internal_set_data($activities[$this->module->cmid], $current);
f007e899
PS
365 $this->assertCount(1, $sink->get_messages());
366 $sink->close();
137d94f3
RT
367
368 // Check if badge is awarded.
369 $this->assertDebuggingCalled('Error baking badge image!');
cdc54199 370 $this->assertTrue($badge->is_issued($this->user->id));
137d94f3 371 }
1cb1e5fe
RT
372
373 /**
374 * Test badges observer when course_completed event is fired.
375 */
376 public function test_badges_observer_course_criteria_review() {
be2b37cf 377 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
1cb1e5fe
RT
378 $badge = new badge($this->coursebadge);
379 $this->assertFalse($badge->is_issued($this->user->id));
380
381 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
382 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
383 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
384 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
385
386 $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
387
4f2424ca
MG
388 // Assert the badge will not be issued to the user as is.
389 $badge = new badge($this->coursebadge);
390 $badge->review_all_criteria();
391 $this->assertFalse($badge->is_issued($this->user->id));
392
1cb1e5fe 393 // Mark course as complete.
f007e899 394 $sink = $this->redirectEmails();
1cb1e5fe 395 $ccompletion->mark_complete();
f007e899
PS
396 $this->assertCount(1, $sink->get_messages());
397 $sink->close();
1cb1e5fe
RT
398
399 // Check if badge is awarded.
400 $this->assertDebuggingCalled('Error baking badge image!');
401 $this->assertTrue($badge->is_issued($this->user->id));
402 }
bb78e249
RT
403
404 /**
405 * Test badges observer when user_updated event is fired.
406 */
407 public function test_badges_observer_profile_criteria_review() {
4f2424ca
MG
408 global $CFG, $DB;
409 require_once($CFG->dirroot.'/user/profile/lib.php');
410
411 // Add a custom field of textarea type.
412 $customprofileid = $DB->insert_record('user_info_field', array(
413 'shortname' => 'newfield', 'name' => 'Description of new field', 'categoryid' => 1,
414 'datatype' => 'textarea'));
415
be2b37cf 416 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
bb78e249 417 $badge = new badge($this->coursebadge);
bb78e249
RT
418
419 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
420 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
421 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
4f2424ca
MG
422 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim',
423 'field_' . $customprofileid => $customprofileid));
424
425 // Assert the badge will not be issued to the user as is.
426 $badge = new badge($this->coursebadge);
427 $badge->review_all_criteria();
428 $this->assertFalse($badge->is_issued($this->user->id));
bb78e249 429
4f2424ca 430 // Set the required fields and make sure the badge got issued.
bb78e249 431 $this->user->address = 'Test address';
c8d2f392 432 $this->user->aim = '999999999';
f007e899 433 $sink = $this->redirectEmails();
4f2424ca 434 profile_save_data((object)array('id' => $this->user->id, 'profile_field_newfield' => 'X'));
bb78e249 435 user_update_user($this->user, false);
f007e899
PS
436 $this->assertCount(1, $sink->get_messages());
437 $sink->close();
bb78e249
RT
438 // Check if badge is awarded.
439 $this->assertDebuggingCalled('Error baking badge image!');
440 $this->assertTrue($badge->is_issued($this->user->id));
441 }
853e506a
Y
442
443 /**
444 * Test badges assertion generated when a badge is issued.
445 */
446 public function test_badges_assertion() {
be2b37cf 447 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
853e506a
Y
448 $badge = new badge($this->coursebadge);
449 $this->assertFalse($badge->is_issued($this->user->id));
450
451 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
452 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
453 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
454 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
455
456 $this->user->address = 'Test address';
f007e899 457 $sink = $this->redirectEmails();
853e506a 458 user_update_user($this->user, false);
f007e899
PS
459 $this->assertCount(1, $sink->get_messages());
460 $sink->close();
853e506a
Y
461 // Check if badge is awarded.
462 $this->assertDebuggingCalled('Error baking badge image!');
463 $awards = $badge->get_awards();
464 $this->assertCount(1, $awards);
465
466 // Get assertion.
467 $award = reset($awards);
468 $assertion = new core_badges_assertion($award->uniquehash);
469 $testassertion = $this->assertion;
470
471 // Make sure JSON strings have the same structure.
472 $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
473 $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
474 $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
475 }
bef0d6b0
AA
476
477 /**
be5e0110 478 * Tests the core_badges_myprofile_navigation() function.
bef0d6b0
AA
479 */
480 public function test_core_badges_myprofile_navigation() {
be5e0110
MN
481 // Set up the test.
482 $tree = new \core_user\output\myprofile\tree();
bef0d6b0 483 $this->setAdminUser();
bef0d6b0 484 $badge = new badge($this->badgeid);
be5e0110
MN
485 $badge->issue($this->user->id, true);
486 $iscurrentuser = true;
bef0d6b0 487 $course = null;
bef0d6b0
AA
488
489 // Enable badges.
490 set_config('enablebadges', true);
bef0d6b0 491
be5e0110
MN
492 // Check the node tree is correct.
493 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
494 $reflector = new ReflectionObject($tree);
495 $nodes = $reflector->getProperty('nodes');
496 $nodes->setAccessible(true);
497 $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
498 }
499
500 /**
501 * Tests the core_badges_myprofile_navigation() function with badges disabled..
502 */
503 public function test_core_badges_myprofile_navigation_badges_disabled() {
504 // Set up the test.
505 $tree = new \core_user\output\myprofile\tree();
506 $this->setAdminUser();
507 $badge = new badge($this->badgeid);
508 $badge->issue($this->user->id, true);
509 $iscurrentuser = false;
510 $course = null;
511
512 // Disable badges.
513 set_config('enablebadges', false);
bef0d6b0 514
be5e0110
MN
515 // Check the node tree is correct.
516 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
517 $reflector = new ReflectionObject($tree);
518 $nodes = $reflector->getProperty('nodes');
519 $nodes->setAccessible(true);
520 $this->assertArrayNotHasKey('localbadges', $nodes->getValue($tree));
521 }
522
523 /**
524 * Tests the core_badges_myprofile_navigation() function with a course badge.
525 */
526 public function test_core_badges_myprofile_navigation_with_course_badge() {
527 // Set up the test.
528 $tree = new \core_user\output\myprofile\tree();
529 $this->setAdminUser();
bef0d6b0 530 $badge = new badge($this->coursebadge);
be5e0110 531 $badge->issue($this->user->id, true);
bef0d6b0 532 $iscurrentuser = false;
be5e0110
MN
533
534 // Check the node tree is correct.
535 core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $this->course);
536 $reflector = new ReflectionObject($tree);
537 $nodes = $reflector->getProperty('nodes');
538 $nodes->setAccessible(true);
539 $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
bef0d6b0 540 }
27806552 541}