weekly release 2.9dev
[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
155 public function test_delete_badge_criteria() {
156 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
157 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
158 $badge = new badge($this->badgeid);
159
160 $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
161
162 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
163 $this->assertEmpty($badge->get_criteria());
164 }
165
166 public function test_badge_awards() {
be2b37cf 167 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
27806552
YB
168 $badge = new badge($this->badgeid);
169 $user1 = $this->getDataGenerator()->create_user();
170
171 $badge->issue($user1->id, true);
172 $this->assertTrue($badge->is_issued($user1->id));
173
174 $user2 = $this->getDataGenerator()->create_user();
175 $badge->issue($user2->id, true);
176 $this->assertTrue($badge->is_issued($user2->id));
177
178 $this->assertCount(2, $badge->get_awards());
179 }
180
181 public function data_for_message_from_template() {
182 return array(
183 array(
184 'This is a message with no variables',
185 array(), // no params
186 'This is a message with no variables'
187 ),
188 array(
189 'This is a message with %amissing% variables',
190 array(), // no params
191 'This is a message with %amissing% variables'
192 ),
193 array(
194 'This is a message with %one% variable',
195 array('one' => 'a single'),
196 'This is a message with a single variable'
197 ),
198 array(
199 'This is a message with %one% %two% %three% variables',
200 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
201 'This is a message with more than one variables'
202 ),
203 array(
204 'This is a message with %three% %two% %one%',
205 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
206 'This is a message with randomly ordered variables'
207 ),
208 array(
209 'This is a message with %repeated% %one% %repeated% of variables',
210 array('one' => 'and', 'repeated' => 'lots'),
211 'This is a message with lots and lots of variables'
212 ),
213 );
214 }
215
216 /**
217 * @dataProvider data_for_message_from_template
218 */
219 public function test_badge_message_from_template($message, $params, $result) {
220 $this->assertEquals(badge_message_from_template($message, $params), $result);
221 }
222
137d94f3
RT
223 /**
224 * Test badges observer when course module completion event id fired.
137d94f3 225 */
cdc54199 226 public function test_badges_observer_course_module_criteria_review() {
be2b37cf 227 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
cdc54199
RT
228 $badge = new badge($this->coursebadge);
229 $this->assertFalse($badge->is_issued($this->user->id));
137d94f3
RT
230
231 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
232 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
233 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
74b63eae 234 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
137d94f3
RT
235
236 // Set completion for forum activity.
cdc54199 237 $c = new completion_info($this->course);
137d94f3
RT
238 $activities = $c->get_activities();
239 $this->assertEquals(1, count($activities));
cdc54199
RT
240 $this->assertTrue(isset($activities[$this->module->cmid]));
241 $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
137d94f3 242
cdc54199 243 $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
137d94f3
RT
244 $current->completionstate = COMPLETION_COMPLETE;
245 $current->timemodified = time();
f007e899 246 $sink = $this->redirectEmails();
cdc54199 247 $c->internal_set_data($activities[$this->module->cmid], $current);
f007e899
PS
248 $this->assertCount(1, $sink->get_messages());
249 $sink->close();
137d94f3
RT
250
251 // Check if badge is awarded.
252 $this->assertDebuggingCalled('Error baking badge image!');
cdc54199 253 $this->assertTrue($badge->is_issued($this->user->id));
137d94f3 254 }
1cb1e5fe
RT
255
256 /**
257 * Test badges observer when course_completed event is fired.
258 */
259 public function test_badges_observer_course_criteria_review() {
be2b37cf 260 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
1cb1e5fe
RT
261 $badge = new badge($this->coursebadge);
262 $this->assertFalse($badge->is_issued($this->user->id));
263
264 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
265 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
266 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
267 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
268
269 $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
270
271 // Mark course as complete.
f007e899 272 $sink = $this->redirectEmails();
1cb1e5fe 273 $ccompletion->mark_complete();
f007e899
PS
274 $this->assertCount(1, $sink->get_messages());
275 $sink->close();
1cb1e5fe
RT
276
277 // Check if badge is awarded.
278 $this->assertDebuggingCalled('Error baking badge image!');
279 $this->assertTrue($badge->is_issued($this->user->id));
280 }
bb78e249
RT
281
282 /**
283 * Test badges observer when user_updated event is fired.
284 */
285 public function test_badges_observer_profile_criteria_review() {
be2b37cf 286 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
bb78e249
RT
287 $badge = new badge($this->coursebadge);
288 $this->assertFalse($badge->is_issued($this->user->id));
289
290 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
291 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
292 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
c8d2f392 293 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim'));
bb78e249
RT
294
295 $this->user->address = 'Test address';
c8d2f392 296 $this->user->aim = '999999999';
f007e899 297 $sink = $this->redirectEmails();
bb78e249 298 user_update_user($this->user, false);
f007e899
PS
299 $this->assertCount(1, $sink->get_messages());
300 $sink->close();
bb78e249
RT
301 // Check if badge is awarded.
302 $this->assertDebuggingCalled('Error baking badge image!');
303 $this->assertTrue($badge->is_issued($this->user->id));
304 }
853e506a
Y
305
306 /**
307 * Test badges assertion generated when a badge is issued.
308 */
309 public function test_badges_assertion() {
be2b37cf 310 $this->preventResetByRollback(); // Messaging is not compatible with transactions.
853e506a
Y
311 $badge = new badge($this->coursebadge);
312 $this->assertFalse($badge->is_issued($this->user->id));
313
314 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
315 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
316 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
317 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
318
319 $this->user->address = 'Test address';
f007e899 320 $sink = $this->redirectEmails();
853e506a 321 user_update_user($this->user, false);
f007e899
PS
322 $this->assertCount(1, $sink->get_messages());
323 $sink->close();
853e506a
Y
324 // Check if badge is awarded.
325 $this->assertDebuggingCalled('Error baking badge image!');
326 $awards = $badge->get_awards();
327 $this->assertCount(1, $awards);
328
329 // Get assertion.
330 $award = reset($awards);
331 $assertion = new core_badges_assertion($award->uniquehash);
332 $testassertion = $this->assertion;
333
334 // Make sure JSON strings have the same structure.
335 $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
336 $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
337 $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
338 }
27806552 339}