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