Merge branch 'wip-MDL-41231-master' of git://github.com/abgreeve/moodle
[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
8252b7c2 32class core_badgeslib_testcase extends advanced_testcase {
27806552 33 protected $badgeid;
cdc54199
RT
34 protected $course;
35 protected $user;
36 protected $module;
37 protected $coursebadge;
27806552
YB
38
39 protected function setUp() {
cdc54199 40 global $DB, $CFG;
27806552
YB
41 $this->resetAfterTest(true);
42
43 $user = $this->getDataGenerator()->create_user();
44
45 $fordb = new stdClass();
46 $fordb->id = null;
47 $fordb->name = "Test badge";
48 $fordb->description = "Testing badges";
49 $fordb->timecreated = time();
50 $fordb->timemodified = time();
51 $fordb->usercreated = $user->id;
52 $fordb->usermodified = $user->id;
27806552
YB
53 $fordb->issuername = "Test issuer";
54 $fordb->issuerurl = "http://issuer-url.domain.co.nz";
55 $fordb->expiredate = null;
56 $fordb->expireperiod = null;
57 $fordb->type = BADGE_TYPE_SITE;
58 $fordb->courseid = null;
59 $fordb->messagesubject = "Test message subject";
60 $fordb->message = "Test message body";
61 $fordb->attachment = 1;
62 $fordb->notification = 0;
63 $fordb->status = BADGE_STATUS_INACTIVE;
64
65 $this->badgeid = $DB->insert_record('badge', $fordb, true);
cdc54199
RT
66
67 // Create a course with activity and auto completion tracking.
68 $this->course = $this->getDataGenerator()->create_course();
69 $this->user = $this->getDataGenerator()->create_user();
70 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
71 $this->assertNotEmpty($studentrole);
72
73 // Get manual enrolment plugin and enrol user.
74 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
75 $manplugin = enrol_get_plugin('manual');
76 $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
77 $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
78 $this->assertEquals(1, $DB->count_records('user_enrolments'));
79
80 $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
81 $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
82
83 // Build badge and criteria.
84 $fordb->type = BADGE_TYPE_COURSE;
85 $fordb->courseid = $this->course->id;
86 $fordb->status = BADGE_STATUS_ACTIVE;
87
88 $this->coursebadge = $DB->insert_record('badge', $fordb, true);
27806552
YB
89 }
90
91 public function test_create_badge() {
92 $badge = new badge($this->badgeid);
93
94 $this->assertInstanceOf('badge', $badge);
95 $this->assertEquals($this->badgeid, $badge->id);
96 }
97
98 public function test_clone_badge() {
99 $badge = new badge($this->badgeid);
100 $newid = $badge->make_clone();
101 $cloned_badge = new badge($newid);
102
27806552
YB
103 $this->assertEquals($badge->description, $cloned_badge->description);
104 $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
105 $this->assertEquals($badge->issuername, $cloned_badge->issuername);
106 $this->assertEquals($badge->issuerurl, $cloned_badge->issuerurl);
107 $this->assertEquals($badge->expiredate, $cloned_badge->expiredate);
108 $this->assertEquals($badge->expireperiod, $cloned_badge->expireperiod);
109 $this->assertEquals($badge->type, $cloned_badge->type);
110 $this->assertEquals($badge->courseid, $cloned_badge->courseid);
111 $this->assertEquals($badge->message, $cloned_badge->message);
112 $this->assertEquals($badge->messagesubject, $cloned_badge->messagesubject);
113 $this->assertEquals($badge->attachment, $cloned_badge->attachment);
114 $this->assertEquals($badge->notification, $cloned_badge->notification);
115 }
116
117 public function test_badge_status() {
118 $badge = new badge($this->badgeid);
119 $old_status = $badge->status;
120 $badge->set_status(BADGE_STATUS_ACTIVE);
121 $this->assertAttributeNotEquals($old_status, 'status', $badge);
122 $this->assertAttributeEquals(BADGE_STATUS_ACTIVE, 'status', $badge);
123 }
124
125 public function test_delete_badge() {
126 $badge = new badge($this->badgeid);
127 $badge->delete();
128 // We don't actually delete badges. We archive them.
129 $this->assertAttributeEquals(BADGE_STATUS_ARCHIVED, 'status', $badge);
130 }
131
132 public function test_create_badge_criteria() {
133 $badge = new badge($this->badgeid);
134 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
135 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
136
137 $this->assertCount(1, $badge->get_criteria());
138
139 $criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
140 $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
141 $criteria_profile->save($params);
142
143 $this->assertCount(2, $badge->get_criteria());
144 }
145
146 public function test_delete_badge_criteria() {
147 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
148 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
149 $badge = new badge($this->badgeid);
150
151 $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
152
153 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
154 $this->assertEmpty($badge->get_criteria());
155 }
156
157 public function test_badge_awards() {
158 $badge = new badge($this->badgeid);
159 $user1 = $this->getDataGenerator()->create_user();
160
161 $badge->issue($user1->id, true);
162 $this->assertTrue($badge->is_issued($user1->id));
163
164 $user2 = $this->getDataGenerator()->create_user();
165 $badge->issue($user2->id, true);
166 $this->assertTrue($badge->is_issued($user2->id));
167
168 $this->assertCount(2, $badge->get_awards());
169 }
170
171 public function data_for_message_from_template() {
172 return array(
173 array(
174 'This is a message with no variables',
175 array(), // no params
176 'This is a message with no variables'
177 ),
178 array(
179 'This is a message with %amissing% variables',
180 array(), // no params
181 'This is a message with %amissing% variables'
182 ),
183 array(
184 'This is a message with %one% variable',
185 array('one' => 'a single'),
186 'This is a message with a single variable'
187 ),
188 array(
189 'This is a message with %one% %two% %three% variables',
190 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
191 'This is a message with more than one variables'
192 ),
193 array(
194 'This is a message with %three% %two% %one%',
195 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
196 'This is a message with randomly ordered variables'
197 ),
198 array(
199 'This is a message with %repeated% %one% %repeated% of variables',
200 array('one' => 'and', 'repeated' => 'lots'),
201 'This is a message with lots and lots of variables'
202 ),
203 );
204 }
205
206 /**
207 * @dataProvider data_for_message_from_template
208 */
209 public function test_badge_message_from_template($message, $params, $result) {
210 $this->assertEquals(badge_message_from_template($message, $params), $result);
211 }
212
137d94f3
RT
213 /**
214 * Test badges observer when course module completion event id fired.
137d94f3 215 */
cdc54199
RT
216 public function test_badges_observer_course_module_criteria_review() {
217 $badge = new badge($this->coursebadge);
218 $this->assertFalse($badge->is_issued($this->user->id));
137d94f3
RT
219
220 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
221 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
222 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
cdc54199 223 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->id => $this->module->id));
137d94f3
RT
224
225 // Set completion for forum activity.
cdc54199 226 $c = new completion_info($this->course);
137d94f3
RT
227 $activities = $c->get_activities();
228 $this->assertEquals(1, count($activities));
cdc54199
RT
229 $this->assertTrue(isset($activities[$this->module->cmid]));
230 $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
137d94f3 231
cdc54199 232 $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
137d94f3
RT
233 $current->completionstate = COMPLETION_COMPLETE;
234 $current->timemodified = time();
cdc54199 235 $c->internal_set_data($activities[$this->module->cmid], $current);
137d94f3
RT
236
237 // Check if badge is awarded.
238 $this->assertDebuggingCalled('Error baking badge image!');
cdc54199 239 $this->assertTrue($badge->is_issued($this->user->id));
137d94f3 240 }
1cb1e5fe
RT
241
242 /**
243 * Test badges observer when course_completed event is fired.
244 */
245 public function test_badges_observer_course_criteria_review() {
246 $badge = new badge($this->coursebadge);
247 $this->assertFalse($badge->is_issued($this->user->id));
248
249 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
250 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
251 $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
252 $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
253
254 $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
255
256 // Mark course as complete.
257 $ccompletion->mark_complete();
258
259 // Check if badge is awarded.
260 $this->assertDebuggingCalled('Error baking badge image!');
261 $this->assertTrue($badge->is_issued($this->user->id));
262 }
27806552 263}