4483d14056d9dea0c42d3f4b33d2c79da8319696
[moodle.git] / badges / tests / badgeslib_test.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/badgeslib.php');
32 class core_badges_badgeslib_testcase extends advanced_testcase {
33     protected $badgeid;
34     protected $course;
35     protected $user;
36     protected $module;
37     protected $coursebadge;
38     protected $assertion;
40     protected function setUp() {
41         global $DB, $CFG;
42         $this->resetAfterTest(true);
44         $CFG->enablecompletion = true;
46         $user = $this->getDataGenerator()->create_user();
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;
56         $fordb->issuername = "Test issuer";
57         $fordb->issuerurl = "http://issuer-url.domain.co.nz";
58         $fordb->issuercontact = "issuer@example.com";
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;
69         $this->badgeid = $DB->insert_record('badge', $fordb, true);
71         // Create a course with activity and auto completion tracking.
72         $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
73         $this->user = $this->getDataGenerator()->create_user();
74         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
75         $this->assertNotEmpty($studentrole);
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'));
84         $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
85         $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
87         // Build badge and criteria.
88         $fordb->type = BADGE_TYPE_COURSE;
89         $fordb->courseid = $this->course->id;
90         $fordb->status = BADGE_STATUS_ACTIVE;
92         $this->coursebadge = $DB->insert_record('badge', $fordb, true);
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"}';
97     }
99     public function test_create_badge() {
100         $badge = new badge($this->badgeid);
102         $this->assertInstanceOf('badge', $badge);
103         $this->assertEquals($this->badgeid, $badge->id);
104     }
106     public function test_clone_badge() {
107         $badge = new badge($this->badgeid);
108         $newid = $badge->make_clone();
109         $cloned_badge = new badge($newid);
111         $this->assertEquals($badge->description, $cloned_badge->description);
112         $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
113         $this->assertEquals($badge->issuername, $cloned_badge->issuername);
114         $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
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     }
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     }
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     }
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));
146         $this->assertCount(1, $badge->get_criteria());
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);
152         $this->assertCount(2, $badge->get_criteria());
153     }
155     public function test_add_badge_criteria_description() {
156         $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
157         $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'description' => 'Overall description'));
159         $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
160         $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'description' => 'Description');
161         $criteriaprofile->save($params);
163         $badge = new badge($this->badgeid);
164         $this->assertEquals('Overall description', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->description);
165         $this->assertEquals('Description', $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->description);
166     }
168     public function test_delete_badge_criteria() {
169         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
170         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
171         $badge = new badge($this->badgeid);
173         $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
175         $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
176         $this->assertEmpty($badge->get_criteria());
177     }
179     public function test_badge_awards() {
180         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
181         $badge = new badge($this->badgeid);
182         $user1 = $this->getDataGenerator()->create_user();
184         $badge->issue($user1->id, true);
185         $this->assertTrue($badge->is_issued($user1->id));
187         $user2 = $this->getDataGenerator()->create_user();
188         $badge->issue($user2->id, true);
189         $this->assertTrue($badge->is_issued($user2->id));
191         $this->assertCount(2, $badge->get_awards());
192     }
194     /**
195      * Test the {@link badges_get_user_badges()} function in lib/badgeslib.php
196      */
197     public function test_badges_get_user_badges() {
198         global $DB;
200         // Messaging is not compatible with transactions.
201         $this->preventResetByRollback();
203         $badges = array();
204         $user1 = $this->getDataGenerator()->create_user();
205         $user2 = $this->getDataGenerator()->create_user();
207         // Record the current time, we need to be precise about a couple of things.
208         $now = time();
209         // Create 11 badges with which to test.
210         for ($i = 1; $i <= 11; $i++) {
211             // Mock up a badge.
212             $badge = new stdClass();
213             $badge->id = null;
214             $badge->name = "Test badge $i";
215             $badge->description = "Testing badges $i";
216             $badge->timecreated = $now - 12;
217             $badge->timemodified = $now - 12;
218             $badge->usercreated = $user1->id;
219             $badge->usermodified = $user1->id;
220             $badge->issuername = "Test issuer";
221             $badge->issuerurl = "http://issuer-url.domain.co.nz";
222             $badge->issuercontact = "issuer@example.com";
223             $badge->expiredate = null;
224             $badge->expireperiod = null;
225             $badge->type = BADGE_TYPE_SITE;
226             $badge->courseid = null;
227             $badge->messagesubject = "Test message subject for badge $i";
228             $badge->message = "Test message body for badge $i";
229             $badge->attachment = 1;
230             $badge->notification = 0;
231             $badge->status = BADGE_STATUS_INACTIVE;
233             $badgeid = $DB->insert_record('badge', $badge, true);
234             $badges[$badgeid] = new badge($badgeid);
235             $badges[$badgeid]->issue($user2->id, true);
236             // Check it all actually worked.
237             $this->assertCount(1, $badges[$badgeid]->get_awards());
239             // Hack the database to adjust the time each badge was issued.
240             // The alternative to this is sleep which is a no-no in unit tests.
241             $DB->set_field('badge_issued', 'dateissued', $now - 11 + $i, array('userid' => $user2->id, 'badgeid' => $badgeid));
242         }
244         // Make sure the first user has no badges.
245         $result = badges_get_user_badges($user1->id);
246         $this->assertInternalType('array', $result);
247         $this->assertCount(0, $result);
249         // Check that the second user has the expected 11 badges.
250         $result = badges_get_user_badges($user2->id);
251         $this->assertCount(11, $result);
253         // Test pagination.
254         // Ordering is by time issued desc, so things will come out with the last awarded badge first.
255         $result = badges_get_user_badges($user2->id, 0, 0, 4);
256         $this->assertCount(4, $result);
257         $lastbadgeissued = reset($result);
258         $this->assertSame('Test badge 11', $lastbadgeissued->name);
259         // Page 2. Expecting 4 results again.
260         $result = badges_get_user_badges($user2->id, 0, 1, 4);
261         $this->assertCount(4, $result);
262         $lastbadgeissued = reset($result);
263         $this->assertSame('Test badge 7', $lastbadgeissued->name);
264         // Page 3. Expecting just three results here.
265         $result = badges_get_user_badges($user2->id, 0, 2, 4);
266         $this->assertCount(3, $result);
267         $lastbadgeissued = reset($result);
268         $this->assertSame('Test badge 3', $lastbadgeissued->name);
269         // Page 4.... there is no page 4.
270         $result = badges_get_user_badges($user2->id, 0, 3, 4);
271         $this->assertCount(0, $result);
273         // Test search.
274         $result = badges_get_user_badges($user2->id, 0, 0, 0, 'badge 1');
275         $this->assertCount(3, $result);
276         $lastbadgeissued = reset($result);
277         $this->assertSame('Test badge 11', $lastbadgeissued->name);
278         // The term Totara doesn't appear anywhere in the badges.
279         $result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara');
280         $this->assertCount(0, $result);
281     }
283     public function data_for_message_from_template() {
284         return array(
285             array(
286                 'This is a message with no variables',
287                 array(), // no params
288                 'This is a message with no variables'
289             ),
290             array(
291                 'This is a message with %amissing% variables',
292                 array(), // no params
293                 'This is a message with %amissing% variables'
294             ),
295             array(
296                 'This is a message with %one% variable',
297                 array('one' => 'a single'),
298                 'This is a message with a single variable'
299             ),
300             array(
301                 'This is a message with %one% %two% %three% variables',
302                 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
303                 'This is a message with more than one variables'
304             ),
305             array(
306                 'This is a message with %three% %two% %one%',
307                 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
308                 'This is a message with randomly ordered variables'
309             ),
310             array(
311                 'This is a message with %repeated% %one% %repeated% of variables',
312                 array('one' => 'and', 'repeated' => 'lots'),
313                 'This is a message with lots and lots of variables'
314             ),
315         );
316     }
318     /**
319      * @dataProvider data_for_message_from_template
320      */
321     public function test_badge_message_from_template($message, $params, $result) {
322         $this->assertEquals(badge_message_from_template($message, $params), $result);
323     }
325     /**
326      * Test badges observer when course module completion event id fired.
327      */
328     public function test_badges_observer_course_module_criteria_review() {
329         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
330         $badge = new badge($this->coursebadge);
331         $this->assertFalse($badge->is_issued($this->user->id));
333         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
334         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
335         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
336         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
338         // Set completion for forum activity.
339         $c = new completion_info($this->course);
340         $activities = $c->get_activities();
341         $this->assertEquals(1, count($activities));
342         $this->assertTrue(isset($activities[$this->module->cmid]));
343         $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
345         $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
346         $current->completionstate = COMPLETION_COMPLETE;
347         $current->timemodified = time();
348         $sink = $this->redirectEmails();
349         $c->internal_set_data($activities[$this->module->cmid], $current);
350         $this->assertCount(1, $sink->get_messages());
351         $sink->close();
353         // Check if badge is awarded.
354         $this->assertDebuggingCalled('Error baking badge image!');
355         $this->assertTrue($badge->is_issued($this->user->id));
356     }
358     /**
359      * Test badges observer when course_completed event is fired.
360      */
361     public function test_badges_observer_course_criteria_review() {
362         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
363         $badge = new badge($this->coursebadge);
364         $this->assertFalse($badge->is_issued($this->user->id));
366         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
367         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
368         $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
369         $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
371         $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
373         // Mark course as complete.
374         $sink = $this->redirectEmails();
375         $ccompletion->mark_complete();
376         $this->assertCount(1, $sink->get_messages());
377         $sink->close();
379         // Check if badge is awarded.
380         $this->assertDebuggingCalled('Error baking badge image!');
381         $this->assertTrue($badge->is_issued($this->user->id));
382     }
384     /**
385      * Test badges observer when user_updated event is fired.
386      */
387     public function test_badges_observer_profile_criteria_review() {
388         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
389         $badge = new badge($this->coursebadge);
390         $this->assertFalse($badge->is_issued($this->user->id));
392         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
393         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
394         $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
395         $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim'));
397         $this->user->address = 'Test address';
398         $this->user->aim = '999999999';
399         $sink = $this->redirectEmails();
400         user_update_user($this->user, false);
401         $this->assertCount(1, $sink->get_messages());
402         $sink->close();
403         // Check if badge is awarded.
404         $this->assertDebuggingCalled('Error baking badge image!');
405         $this->assertTrue($badge->is_issued($this->user->id));
406     }
408     /**
409      * Test badges assertion generated when a badge is issued.
410      */
411     public function test_badges_assertion() {
412         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
413         $badge = new badge($this->coursebadge);
414         $this->assertFalse($badge->is_issued($this->user->id));
416         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
417         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
418         $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
419         $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
421         $this->user->address = 'Test address';
422         $sink = $this->redirectEmails();
423         user_update_user($this->user, false);
424         $this->assertCount(1, $sink->get_messages());
425         $sink->close();
426         // Check if badge is awarded.
427         $this->assertDebuggingCalled('Error baking badge image!');
428         $awards = $badge->get_awards();
429         $this->assertCount(1, $awards);
431         // Get assertion.
432         $award = reset($awards);
433         $assertion = new core_badges_assertion($award->uniquehash);
434         $testassertion = $this->assertion;
436         // Make sure JSON strings have the same structure.
437         $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
438         $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
439         $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
440     }