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