1 |
efrain |
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 |
* Badges external functions tests.
|
|
|
19 |
*
|
|
|
20 |
* @package core_badges
|
|
|
21 |
* @category external
|
|
|
22 |
* @copyright 2016 Juan Leyva <juan@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 3.1
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace core_badges\external;
|
|
|
28 |
|
|
|
29 |
use core_badges_external;
|
|
|
30 |
use core_external\external_api;
|
|
|
31 |
use core_external\external_settings;
|
|
|
32 |
use externallib_advanced_testcase;
|
|
|
33 |
|
|
|
34 |
defined('MOODLE_INTERNAL') || die();
|
|
|
35 |
|
|
|
36 |
global $CFG;
|
|
|
37 |
|
|
|
38 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
39 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Badges external functions tests
|
|
|
43 |
*
|
|
|
44 |
* @package core_badges
|
|
|
45 |
* @category external
|
|
|
46 |
* @copyright 2016 Juan Leyva <juan@moodle.com>
|
|
|
47 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
48 |
* @since Moodle 3.1
|
|
|
49 |
*/
|
|
|
50 |
class external_test extends externallib_advanced_testcase {
|
|
|
51 |
|
|
|
52 |
/** @var stdClass $course */
|
|
|
53 |
private $course;
|
|
|
54 |
|
|
|
55 |
/** @var stdClass $student */
|
|
|
56 |
private $student;
|
|
|
57 |
|
|
|
58 |
/** @var stdClass $teacher */
|
|
|
59 |
private $teacher;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Set up for every test
|
|
|
63 |
*/
|
|
|
64 |
public function setUp(): void {
|
|
|
65 |
global $DB;
|
|
|
66 |
$this->resetAfterTest();
|
|
|
67 |
$this->setAdminUser();
|
|
|
68 |
|
|
|
69 |
// Setup test data.
|
|
|
70 |
$this->course = $this->getDataGenerator()->create_course();
|
|
|
71 |
|
|
|
72 |
// Create users and enrolments.
|
|
|
73 |
$this->student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
|
|
|
74 |
$this->teacher = $this->getDataGenerator()->create_and_enrol($this->course, 'editingteacher');
|
|
|
75 |
|
|
|
76 |
// Mock up a site badge.
|
|
|
77 |
$now = time();
|
|
|
78 |
$badge = new \stdClass();
|
|
|
79 |
$badge->id = null;
|
|
|
80 |
$badge->name = "Test badge site";
|
|
|
81 |
$badge->description = "Testing badges site";
|
|
|
82 |
$badge->timecreated = $now - 12;
|
|
|
83 |
$badge->timemodified = $now - 12;
|
|
|
84 |
$badge->usercreated = $this->teacher->id;
|
|
|
85 |
$badge->usermodified = $this->teacher->id;
|
|
|
86 |
$badge->issuername = "Test issuer";
|
|
|
87 |
$badge->issuerurl = "http://issuer-url.domain.co.nz";
|
|
|
88 |
$badge->issuercontact = "issuer@example.com";
|
|
|
89 |
$badge->expiredate = null;
|
|
|
90 |
$badge->expireperiod = null;
|
|
|
91 |
$badge->type = BADGE_TYPE_SITE;
|
|
|
92 |
$badge->courseid = null;
|
|
|
93 |
$badge->messagesubject = "Test message subject for badge";
|
|
|
94 |
$badge->message = "Test message body for badge";
|
|
|
95 |
$badge->attachment = 1;
|
|
|
96 |
$badge->notification = 0;
|
|
|
97 |
$badge->status = BADGE_STATUS_ACTIVE;
|
|
|
98 |
$badge->version = '1';
|
|
|
99 |
$badge->language = 'en';
|
|
|
100 |
$badge->imageauthorname = 'Image author';
|
|
|
101 |
$badge->imageauthoremail = 'imageauthor@example.com';
|
|
|
102 |
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
|
|
|
103 |
$badge->imagecaption = 'Caption';
|
|
|
104 |
|
|
|
105 |
$badgeid = $DB->insert_record('badge', $badge, true);
|
|
|
106 |
$badge = new \badge($badgeid);
|
|
|
107 |
$badge->issue($this->student->id, true);
|
|
|
108 |
|
|
|
109 |
// Hack the database to adjust the time each badge was issued.
|
|
|
110 |
$DB->set_field('badge_issued', 'dateissued', $now - 11, array('userid' => $this->student->id, 'badgeid' => $badgeid));
|
|
|
111 |
|
|
|
112 |
// Add an endorsement for the badge.
|
|
|
113 |
$endorsement = new \stdClass();
|
|
|
114 |
$endorsement->badgeid = $badgeid;
|
|
|
115 |
$endorsement->issuername = 'Issuer name';
|
|
|
116 |
$endorsement->issuerurl = 'http://endorsement-issuer-url.domain.co.nz';
|
|
|
117 |
$endorsement->issueremail = 'endorsementissuer@example.com';
|
|
|
118 |
$endorsement->claimid = 'http://claim-url.domain.co.nz';
|
|
|
119 |
$endorsement->claimcomment = 'Claim comment';
|
|
|
120 |
$endorsement->dateissued = $now;
|
|
|
121 |
$badge->save_endorsement($endorsement);
|
|
|
122 |
|
|
|
123 |
// Add 2 alignments.
|
|
|
124 |
$alignment = new \stdClass();
|
|
|
125 |
$alignment->badgeid = $badgeid;
|
|
|
126 |
$alignment->targetname = 'Alignment 1';
|
|
|
127 |
$alignment->targeturl = 'http://a1-target-url.domain.co.nz';
|
|
|
128 |
$alignment->targetdescription = 'A1 target description';
|
|
|
129 |
$alignment->targetframework = 'A1 framework';
|
|
|
130 |
$alignment->targetcode = 'A1 code';
|
|
|
131 |
$badge->save_alignment($alignment);
|
|
|
132 |
|
|
|
133 |
$alignment->targetname = 'Alignment 2';
|
|
|
134 |
$alignment->targeturl = 'http://a2-target-url.domain.co.nz';
|
|
|
135 |
$alignment->targetdescription = 'A2 target description';
|
|
|
136 |
$alignment->targetframework = 'A2 framework';
|
|
|
137 |
$alignment->targetcode = 'A2 code';
|
|
|
138 |
$badge->save_alignment($alignment);
|
|
|
139 |
|
|
|
140 |
// Now a course badge.
|
|
|
141 |
$badge->id = null;
|
|
|
142 |
$badge->name = "Test badge course";
|
|
|
143 |
$badge->description = "Testing badges course";
|
|
|
144 |
$badge->type = BADGE_TYPE_COURSE;
|
|
|
145 |
$badge->courseid = $this->course->id;
|
|
|
146 |
|
|
|
147 |
$coursebadgeid = $DB->insert_record('badge', $badge, true);
|
|
|
148 |
$badge = new \badge($coursebadgeid);
|
|
|
149 |
$badge->issue($this->student->id, true);
|
|
|
150 |
|
|
|
151 |
// Hack the database to adjust the time each badge was issued.
|
|
|
152 |
$DB->set_field('badge_issued', 'dateissued', $now - 10, array('userid' => $this->student->id, 'badgeid' => $coursebadgeid));
|
|
|
153 |
|
|
|
154 |
// Make the site badge a related badge.
|
|
|
155 |
$badge->add_related_badges(array($badgeid));
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Test get user badges.
|
|
|
160 |
* These is a basic test since the badges_get_my_user_badges used by the external function already has unit tests.
|
|
|
161 |
*/
|
11 |
efrain |
162 |
public function test_get_my_user_badges(): void {
|
1 |
efrain |
163 |
|
|
|
164 |
$this->setUser($this->student);
|
|
|
165 |
|
|
|
166 |
$badges = (array) badges_get_user_badges($this->student->id);
|
|
|
167 |
$expectedbadges = array();
|
|
|
168 |
$coursebadge = null;
|
|
|
169 |
|
|
|
170 |
foreach ($badges as $badge) {
|
|
|
171 |
$context = ($badge->type == BADGE_TYPE_SITE) ? \context_system::instance() : \context_course::instance($badge->courseid);
|
|
|
172 |
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
|
|
|
173 |
'f3')->out(false);
|
|
|
174 |
|
|
|
175 |
// Get the endorsement, alignments and related badges.
|
|
|
176 |
$badgeinstance = new \badge($badge->id);
|
|
|
177 |
$endorsement = $badgeinstance->get_endorsement();
|
|
|
178 |
$alignments = $badgeinstance->get_alignments();
|
|
|
179 |
$relatedbadges = $badgeinstance->get_related_badges();
|
|
|
180 |
$badge->alignment = array();
|
|
|
181 |
$badge->relatedbadges = array();
|
|
|
182 |
|
|
|
183 |
if ($endorsement) {
|
|
|
184 |
$badge->endorsement = (array) $endorsement;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
if (!empty($alignments)) {
|
|
|
188 |
foreach ($alignments as $alignment) {
|
|
|
189 |
// Students cannot see some fields of the alignments.
|
|
|
190 |
unset($alignment->targetname);
|
|
|
191 |
unset($alignment->targeturl);
|
|
|
192 |
unset($alignment->targetdescription);
|
|
|
193 |
unset($alignment->targetframework);
|
|
|
194 |
unset($alignment->targetcode);
|
|
|
195 |
|
|
|
196 |
$badge->alignment[] = (array) $alignment;
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (!empty($relatedbadges)) {
|
|
|
201 |
foreach ($relatedbadges as $relatedbadge) {
|
|
|
202 |
// Students cannot see some fields of the related badges.
|
|
|
203 |
unset($relatedbadge->version);
|
|
|
204 |
unset($relatedbadge->language);
|
|
|
205 |
unset($relatedbadge->type);
|
|
|
206 |
|
|
|
207 |
$badge->relatedbadges[] = (array) $relatedbadge;
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
$expectedbadges[] = (array) $badge;
|
|
|
212 |
if (isset($badge->courseid)) {
|
|
|
213 |
// Save the course badge to be able to compare it in our tests.
|
|
|
214 |
$coursebadge = (array) $badge;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$result = core_badges_external::get_user_badges();
|
|
|
219 |
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
|
|
|
220 |
$this->assertEquals($expectedbadges, $result['badges']);
|
|
|
221 |
|
|
|
222 |
// Pagination and filtering.
|
|
|
223 |
$result = core_badges_external::get_user_badges(0, $this->course->id, 0, 1, '', true);
|
|
|
224 |
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
|
|
|
225 |
$this->assertCount(1, $result['badges']);
|
|
|
226 |
$this->assertEquals($coursebadge, $result['badges'][0]);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Test get user badges.
|
|
|
231 |
*/
|
11 |
efrain |
232 |
public function test_get_other_user_badges(): void {
|
1 |
efrain |
233 |
|
|
|
234 |
$this->setUser($this->teacher);
|
|
|
235 |
|
|
|
236 |
$result = core_badges_external::get_user_badges($this->student->id);
|
|
|
237 |
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
|
|
|
238 |
|
|
|
239 |
$this->assertCount(2, $result['badges']);
|
|
|
240 |
|
|
|
241 |
// Check that we don't have permissions for view the complete information for site badges.
|
|
|
242 |
foreach ($result['badges'] as $badge) {
|
|
|
243 |
if (isset($badge['type']) and $badge['type'] == BADGE_TYPE_COURSE) {
|
|
|
244 |
$this->assertTrue(isset($badge['message']));
|
|
|
245 |
|
|
|
246 |
// Check that we have permissions to see all the data in alignments and related badges.
|
|
|
247 |
foreach ($badge['alignment'] as $alignment) {
|
|
|
248 |
$this->assertTrue(isset($alignment['targetdescription']));
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
foreach ($badge['relatedbadges'] as $relatedbadge) {
|
|
|
252 |
$this->assertTrue(isset($relatedbadge['type']));
|
|
|
253 |
}
|
|
|
254 |
} else {
|
|
|
255 |
$this->assertFalse(isset($badge['message']));
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Test get_user_badges where issuername contains text to be filtered
|
|
|
262 |
*/
|
|
|
263 |
public function test_get_user_badges_filter_issuername(): void {
|
|
|
264 |
global $DB;
|
|
|
265 |
|
|
|
266 |
filter_set_global_state('multilang', TEXTFILTER_ON);
|
|
|
267 |
filter_set_applies_to_strings('multilang', true);
|
|
|
268 |
|
|
|
269 |
external_settings::get_instance()->set_filter(true);
|
|
|
270 |
|
|
|
271 |
// Update issuer name of test badge.
|
|
|
272 |
$issuername = '<span class="multilang" lang="en">Issuer (en)</span><span class="multilang" lang="es">Issuer (es)</span>';
|
|
|
273 |
$DB->set_field('badge', 'issuername', $issuername, ['name' => 'Test badge site']);
|
|
|
274 |
|
|
|
275 |
// Retrieve student badges.
|
|
|
276 |
$result = core_badges_external::get_user_badges($this->student->id);
|
|
|
277 |
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
|
|
|
278 |
|
|
|
279 |
// Site badge will be last, because it has the earlier issued date.
|
|
|
280 |
$badge = end($result['badges']);
|
|
|
281 |
$this->assertEquals('Issuer (en)', $badge['issuername']);
|
|
|
282 |
}
|
|
|
283 |
}
|