MDL-35073 badges: Open badges integration
[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
32class badges_testcase extends advanced_testcase {
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;
49 $fordb->image = 0;
50 $fordb->issuername = "Test issuer";
51 $fordb->issuerurl = "http://issuer-url.domain.co.nz";
52 $fordb->expiredate = null;
53 $fordb->expireperiod = null;
54 $fordb->type = BADGE_TYPE_SITE;
55 $fordb->courseid = null;
56 $fordb->messagesubject = "Test message subject";
57 $fordb->message = "Test message body";
58 $fordb->attachment = 1;
59 $fordb->notification = 0;
60 $fordb->status = BADGE_STATUS_INACTIVE;
61
62 $this->badgeid = $DB->insert_record('badge', $fordb, true);
63 }
64
65 public function test_create_badge() {
66 $badge = new badge($this->badgeid);
67
68 $this->assertInstanceOf('badge', $badge);
69 $this->assertEquals($this->badgeid, $badge->id);
70 }
71
72 public function test_clone_badge() {
73 $badge = new badge($this->badgeid);
74 $newid = $badge->make_clone();
75 $cloned_badge = new badge($newid);
76
77 $this->assertEquals($badge->image, $cloned_badge->image);
78 $this->assertEquals($badge->description, $cloned_badge->description);
79 $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
80 $this->assertEquals($badge->issuername, $cloned_badge->issuername);
81 $this->assertEquals($badge->issuerurl, $cloned_badge->issuerurl);
82 $this->assertEquals($badge->expiredate, $cloned_badge->expiredate);
83 $this->assertEquals($badge->expireperiod, $cloned_badge->expireperiod);
84 $this->assertEquals($badge->type, $cloned_badge->type);
85 $this->assertEquals($badge->courseid, $cloned_badge->courseid);
86 $this->assertEquals($badge->message, $cloned_badge->message);
87 $this->assertEquals($badge->messagesubject, $cloned_badge->messagesubject);
88 $this->assertEquals($badge->attachment, $cloned_badge->attachment);
89 $this->assertEquals($badge->notification, $cloned_badge->notification);
90 }
91
92 public function test_badge_status() {
93 $badge = new badge($this->badgeid);
94 $old_status = $badge->status;
95 $badge->set_status(BADGE_STATUS_ACTIVE);
96 $this->assertAttributeNotEquals($old_status, 'status', $badge);
97 $this->assertAttributeEquals(BADGE_STATUS_ACTIVE, 'status', $badge);
98 }
99
100 public function test_delete_badge() {
101 $badge = new badge($this->badgeid);
102 $badge->delete();
103 // We don't actually delete badges. We archive them.
104 $this->assertAttributeEquals(BADGE_STATUS_ARCHIVED, 'status', $badge);
105 }
106
107 public function test_create_badge_criteria() {
108 $badge = new badge($this->badgeid);
109 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
110 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
111
112 $this->assertCount(1, $badge->get_criteria());
113
114 $criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
115 $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
116 $criteria_profile->save($params);
117
118 $this->assertCount(2, $badge->get_criteria());
119 }
120
121 public function test_delete_badge_criteria() {
122 $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
123 $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
124 $badge = new badge($this->badgeid);
125
126 $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
127
128 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
129 $this->assertEmpty($badge->get_criteria());
130 }
131
132 public function test_badge_awards() {
133 $badge = new badge($this->badgeid);
134 $user1 = $this->getDataGenerator()->create_user();
135
136 $badge->issue($user1->id, true);
137 $this->assertTrue($badge->is_issued($user1->id));
138
139 $user2 = $this->getDataGenerator()->create_user();
140 $badge->issue($user2->id, true);
141 $this->assertTrue($badge->is_issued($user2->id));
142
143 $this->assertCount(2, $badge->get_awards());
144 }
145
146 public function data_for_message_from_template() {
147 return array(
148 array(
149 'This is a message with no variables',
150 array(), // no params
151 'This is a message with no variables'
152 ),
153 array(
154 'This is a message with %amissing% variables',
155 array(), // no params
156 'This is a message with %amissing% variables'
157 ),
158 array(
159 'This is a message with %one% variable',
160 array('one' => 'a single'),
161 'This is a message with a single variable'
162 ),
163 array(
164 'This is a message with %one% %two% %three% variables',
165 array('one' => 'more', 'two' => 'than', 'three' => 'one'),
166 'This is a message with more than one variables'
167 ),
168 array(
169 'This is a message with %three% %two% %one%',
170 array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
171 'This is a message with randomly ordered variables'
172 ),
173 array(
174 'This is a message with %repeated% %one% %repeated% of variables',
175 array('one' => 'and', 'repeated' => 'lots'),
176 'This is a message with lots and lots of variables'
177 ),
178 );
179 }
180
181 /**
182 * @dataProvider data_for_message_from_template
183 */
184 public function test_badge_message_from_template($message, $params, $result) {
185 $this->assertEquals(badge_message_from_template($message, $params), $result);
186 }
187
188}