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