| Línea 14... |
Línea 14... |
| 14 |
// You should have received a copy of the GNU General Public License
|
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/>.
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
| Línea 16... |
Línea 16... |
| 16 |
|
16 |
|
| Línea -... |
Línea 17... |
| - |
|
17 |
namespace core_badges\external;
|
| 17 |
namespace core_badges\external;
|
18 |
|
| Línea 18... |
Línea 19... |
| 18 |
|
19 |
use core_badges\tests\external_helper;
|
| Línea 19... |
Línea 20... |
| 19 |
use externallib_advanced_testcase;
|
20 |
use externallib_advanced_testcase;
|
| Línea 20... |
Línea 21... |
| 20 |
|
21 |
|
| 21 |
defined('MOODLE_INTERNAL') || die();
|
- |
|
| Línea 22... |
Línea 22... |
| 22 |
|
22 |
defined('MOODLE_INTERNAL') || die();
|
| 23 |
global $CFG;
|
23 |
|
| 24 |
|
24 |
global $CFG;
|
| 25 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
25 |
|
| Línea 33... |
Línea 33... |
| 33 |
* @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
|
33 |
* @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
|
| 34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 35 |
* @since Moodle 4.3
|
35 |
* @since Moodle 4.3
|
| 36 |
* @coversDefaultClass \core_badges\external\get_user_badge_by_hash
|
36 |
* @coversDefaultClass \core_badges\external\get_user_badge_by_hash
|
| 37 |
*/
|
37 |
*/
|
| 38 |
class get_user_badge_by_hash_test extends externallib_advanced_testcase {
|
38 |
final class get_user_badge_by_hash_test extends externallib_advanced_testcase {
|
| 39 |
|
- |
|
| 40 |
/**
|
- |
|
| 41 |
* Prepare the test.
|
39 |
use external_helper;
|
| 42 |
*
|
- |
|
| 43 |
* @return array
|
- |
|
| 44 |
*/
|
- |
|
| 45 |
private function prepare_test_data(): array {
|
- |
|
| 46 |
global $DB;
|
- |
|
| 47 |
$this->resetAfterTest();
|
- |
|
| 48 |
$this->setAdminUser();
|
- |
|
| 49 |
|
- |
|
| 50 |
// Setup test data.
|
- |
|
| 51 |
$course = $this->getDataGenerator()->create_course();
|
- |
|
| 52 |
|
- |
|
| 53 |
// Create users and enrolments.
|
- |
|
| 54 |
$student1 = $this->getDataGenerator()->create_and_enrol($course);
|
- |
|
| 55 |
$student2 = $this->getDataGenerator()->create_and_enrol($course);
|
- |
|
| 56 |
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
|
- |
|
| 57 |
|
- |
|
| 58 |
// Mock up a site badge.
|
- |
|
| 59 |
$now = time();
|
- |
|
| 60 |
$badge = new \stdClass();
|
- |
|
| 61 |
$badge->id = null;
|
- |
|
| 62 |
$badge->name = "Test badge site";
|
- |
|
| 63 |
$badge->description = "Testing badges site";
|
- |
|
| 64 |
$badge->timecreated = $now;
|
- |
|
| 65 |
$badge->timemodified = $now;
|
- |
|
| 66 |
$badge->usercreated = (int) $teacher->id;
|
- |
|
| 67 |
$badge->usermodified = (int) $teacher->id;
|
- |
|
| 68 |
$badge->expiredate = null;
|
- |
|
| 69 |
$badge->expireperiod = null;
|
- |
|
| 70 |
$badge->type = BADGE_TYPE_SITE;
|
- |
|
| 71 |
$badge->courseid = null;
|
- |
|
| 72 |
$badge->messagesubject = "Test message subject for badge";
|
- |
|
| 73 |
$badge->message = "Test message body for badge";
|
- |
|
| 74 |
$badge->attachment = 1;
|
- |
|
| 75 |
$badge->notification = 0;
|
- |
|
| 76 |
$badge->status = BADGE_STATUS_ACTIVE;
|
- |
|
| 77 |
$badge->version = '1';
|
- |
|
| 78 |
$badge->language = 'en';
|
- |
|
| 79 |
$badge->imageauthorname = 'Image author';
|
- |
|
| 80 |
$badge->imageauthoremail = 'imageauthor@example.com';
|
- |
|
| 81 |
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
|
- |
|
| 82 |
$badge->imagecaption = 'Caption';
|
- |
|
| 83 |
|
- |
|
| 84 |
$badgeid = $DB->insert_record('badge', $badge, true);
|
- |
|
| 85 |
$badge->id = $badgeid;
|
- |
|
| 86 |
$sitebadge = new \badge($badgeid);
|
- |
|
| 87 |
$sitebadge->issue($student1->id, true);
|
- |
|
| 88 |
$siteissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $badge->id ]);
|
- |
|
| 89 |
|
- |
|
| 90 |
$badge->issuername = $sitebadge->issuername;
|
- |
|
| 91 |
$badge->issuercontact = $sitebadge->issuercontact;
|
- |
|
| 92 |
$badge->issuerurl = $sitebadge->issuerurl;
|
- |
|
| 93 |
$badge->nextcron = $sitebadge->nextcron;
|
- |
|
| 94 |
$badge->issuedid = (int) $siteissuedbadge->id;
|
- |
|
| 95 |
$badge->uniquehash = $siteissuedbadge->uniquehash;
|
- |
|
| 96 |
$badge->dateissued = (int) $siteissuedbadge->dateissued;
|
- |
|
| 97 |
$badge->dateexpire = $siteissuedbadge->dateexpire;
|
- |
|
| 98 |
$badge->visible = (int) $siteissuedbadge->visible;
|
- |
|
| 99 |
$badge->email = $student1->email;
|
- |
|
| 100 |
$context = \context_system::instance();
|
- |
|
| 101 |
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
|
- |
|
| 102 |
'f3')->out(false);
|
- |
|
| 103 |
$badge->status = BADGE_STATUS_ACTIVE_LOCKED;
|
- |
|
| 104 |
|
- |
|
| 105 |
// Add an endorsement for the badge.
|
- |
|
| 106 |
$endorsement = new \stdClass();
|
- |
|
| 107 |
$endorsement->badgeid = $badgeid;
|
- |
|
| 108 |
$endorsement->issuername = 'Issuer name';
|
- |
|
| 109 |
$endorsement->issuerurl = 'http://endorsement-issuer-url.domain.co.nz';
|
- |
|
| 110 |
$endorsement->issueremail = 'endorsementissuer@example.com';
|
- |
|
| 111 |
$endorsement->claimid = 'http://claim-url.domain.co.nz';
|
- |
|
| 112 |
$endorsement->claimcomment = 'Claim comment';
|
- |
|
| 113 |
$endorsement->dateissued = $now;
|
- |
|
| 114 |
$endorsement->id = $sitebadge->save_endorsement($endorsement);
|
- |
|
| 115 |
$badge->endorsement = (array) $endorsement;
|
- |
|
| 116 |
|
- |
|
| 117 |
// Add 2 alignments.
|
- |
|
| 118 |
$alignment = new \stdClass();
|
- |
|
| 119 |
$alignment->badgeid = $badgeid;
|
- |
|
| 120 |
$alignment->id = $sitebadge->save_alignment($alignment);
|
- |
|
| 121 |
$badge->alignment[] = (array) $alignment;
|
- |
|
| 122 |
|
- |
|
| 123 |
$alignment->id = $sitebadge->save_alignment($alignment);
|
- |
|
| 124 |
$badge->alignment[] = (array) $alignment;
|
- |
|
| 125 |
$badge->relatedbadges = [];
|
- |
|
| 126 |
$usersitebadge[] = (array) $badge;
|
- |
|
| 127 |
|
- |
|
| 128 |
// Now a course badge.
|
- |
|
| 129 |
$badge->id = null;
|
- |
|
| 130 |
$badge->name = "Test badge course";
|
- |
|
| 131 |
$badge->description = "Testing badges course";
|
- |
|
| 132 |
$badge->type = BADGE_TYPE_COURSE;
|
- |
|
| 133 |
$badge->courseid = (int) $course->id;
|
- |
|
| 134 |
|
- |
|
| 135 |
$badge->id = $DB->insert_record('badge', $badge, true);
|
- |
|
| 136 |
$coursebadge = new \badge($badge->id );
|
- |
|
| 137 |
$coursebadge->issue($student1->id, true);
|
- |
|
| 138 |
$courseissuedbadge = $DB->get_record('badge_issued', [ 'badgeid' => $badge->id ]);
|
- |
|
| 139 |
|
- |
|
| 140 |
$badge->issuername = $coursebadge->issuername;
|
- |
|
| 141 |
$badge->issuercontact = $coursebadge->issuercontact;
|
- |
|
| 142 |
$badge->issuerurl = $coursebadge->issuerurl;
|
- |
|
| 143 |
$badge->nextcron = $coursebadge->nextcron;
|
- |
|
| 144 |
$badge->issuedid = (int) $courseissuedbadge->id;
|
- |
|
| 145 |
$badge->uniquehash = $courseissuedbadge->uniquehash;
|
- |
|
| 146 |
$badge->dateissued = (int) $courseissuedbadge->dateissued;
|
- |
|
| 147 |
$badge->dateexpire = $courseissuedbadge->dateexpire;
|
- |
|
| 148 |
$badge->visible = (int) $courseissuedbadge->visible;
|
- |
|
| 149 |
$badge->email = $student1->email;
|
- |
|
| 150 |
$context = \context_course::instance($badge->courseid);
|
- |
|
| 151 |
$badge->badgeurl = \moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id , '/',
|
- |
|
| 152 |
'f3')->out(false);
|
- |
|
| 153 |
|
- |
|
| 154 |
unset($badge->endorsement);
|
- |
|
| 155 |
$badge->alignment = [];
|
- |
|
| 156 |
$usercoursebadge[] = (array) $badge;
|
- |
|
| 157 |
// Make the site badge a related badge.
|
- |
|
| 158 |
$sitebadge->add_related_badges([$badge->id]);
|
- |
|
| 159 |
$usersitebadge[0]['relatedbadges'][0] = [
|
- |
|
| 160 |
'id' => (int) $coursebadge->id,
|
- |
|
| 161 |
'name' => $coursebadge->name
|
- |
|
| 162 |
];
|
- |
|
| 163 |
$usercoursebadge[0]['relatedbadges'][0] = [
|
- |
|
| 164 |
'id' => (int) $sitebadge->id,
|
- |
|
| 165 |
'name' => $sitebadge->name
|
- |
|
| 166 |
];
|
- |
|
| 167 |
return [
|
- |
|
| 168 |
'coursebadge' => $usercoursebadge,
|
- |
|
| 169 |
'sitebadge' => $usersitebadge,
|
- |
|
| 170 |
'student1' => $student1,
|
- |
|
| 171 |
'student2' => $student2
|
- |
|
| 172 |
];
|
- |
|
| 173 |
}
|
- |
|
| Línea 174... |
Línea 40... |
| 174 |
|
40 |
|
| 175 |
/**
|
41 |
/**
|
| 176 |
* Test get user badge by hash.
|
42 |
* Test get user badge by hash.
|
| 177 |
* These are a basic tests since the badges_get_my_user_badges used by the external function already has unit tests.
|
43 |
* These are a basic tests since the badges_get_my_user_badges used by the external function already has unit tests.
|
| 178 |
* @covers ::execute
|
44 |
* @covers ::execute
|
| 179 |
*/
|
45 |
*/
|
| 180 |
public function test_get_user_badge_by_hash(): void {
|
46 |
public function test_get_user_badge_by_hash(): void {
|
| 181 |
$data = $this->prepare_test_data();
|
- |
|
| Línea 182... |
Línea 47... |
| 182 |
$this->setUser($data['student1']);
|
47 |
$data = $this->prepare_test_data();
|
| - |
|
48 |
|
| 183 |
|
49 |
// Site badge fetched by recipient.
|
| 184 |
// Site badge.
|
50 |
$this->setUser($data['student']);
|
| 185 |
$result = get_user_badge_by_hash::execute($data['sitebadge'][0]['uniquehash']);
|
51 |
$result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
|
| 186 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
52 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| Línea -... |
Línea 53... |
| - |
|
53 |
$this->assert_issued_badge($data['sitebadge'], $result['badge'][0], true, false);
|
| 187 |
$this->assertEquals($data['sitebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
|
54 |
$this->assertEmpty($result['warnings']);
|
| 188 |
$this->assertEmpty($result['warnings']);
|
55 |
|
| 189 |
|
56 |
// Site badge fetched by user without "moodle/badges:configuredetails" capability.
|
| 190 |
// Course badge.
|
57 |
$this->setGuestUser();
|
| 191 |
$result = get_user_badge_by_hash::execute($data['coursebadge'][0]['uniquehash']);
|
58 |
$result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
|
| Línea -... |
Línea 59... |
| - |
|
59 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| 192 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
60 |
$this->assert_issued_badge($data['sitebadge'], $result['badge'][0], false, false);
|
| 193 |
$this->assertEquals($data['coursebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
|
61 |
$this->assertEmpty($result['warnings']);
|
| 194 |
$this->assertEmpty($result['warnings']);
|
62 |
|
| 195 |
|
63 |
// Site badge fetched by user with "moodle/badges:configuredetails" capability.
|
| 196 |
// Wrong hash.
|
64 |
$this->setAdminUser();
|
| 197 |
$result = get_user_badge_by_hash::execute('1234');
|
- |
|
| 198 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
- |
|
| 199 |
$this->assertEmpty($result['badge']);
|
- |
|
| 200 |
$this->assertNotEmpty($result['warnings']);
|
- |
|
| 201 |
$this->assertEquals('badgeawardnotfound', $result['warnings'][0]['warningcode']);
|
- |
|
| 202 |
}
|
- |
|
| 203 |
|
- |
|
| 204 |
/**
|
- |
|
| 205 |
* Test get user badge by hash with restrictions.
|
- |
|
| 206 |
* @covers ::execute
|
- |
|
| Línea 207... |
Línea 65... |
| 207 |
*/
|
65 |
$result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
|
| - |
|
66 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| 208 |
public function test_get_user_badge_by_hash_with_restrictions(): void {
|
67 |
$this->assert_issued_badge($data['sitebadge'], $result['badge'][0], false, true);
|
| 209 |
$data = $this->prepare_test_data();
|
68 |
$this->assertEmpty($result['warnings']);
|
| 210 |
$this->setUser($data['student2']);
|
69 |
|
| 211 |
|
70 |
// Course badge fetched by recipient.
|
| Línea 212... |
Línea 71... |
| 212 |
// Site badge.
|
71 |
$this->setUser($data['student']);
|
| 213 |
$result = get_user_badge_by_hash::execute($data['sitebadge'][0]['uniquehash']);
|
- |
|
| 214 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
72 |
$result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
|
| 215 |
$this->assertNotEmpty($result['badge']);
|
- |
|
| 216 |
$this->assertEmpty($result['warnings']);
|
- |
|
| 217 |
|
- |
|
| 218 |
// Check that we don't have permissions for view the complete information for site badges.
|
73 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| 219 |
if (isset($result['badge'][0]['type']) && $result['badge'][0]['type'] == BADGE_TYPE_SITE) {
|
- |
|
| 220 |
$this->assertFalse(isset($result['badge'][0]['message']));
|
- |
|
| 221 |
|
74 |
$this->assert_issued_badge($data['coursebadge'], $result['badge'][0], true, false);
|
| 222 |
// Check that we have permissions to see all the data in alignments and related badges.
|
75 |
$this->assertEmpty($result['warnings']);
|
| 223 |
foreach ($result['badge'][0]['alignment'] as $alignment) {
|
- |
|
| 224 |
$this->assertTrue(isset($alignment['id']));
|
- |
|
| 225 |
}
|
76 |
|
| 226 |
|
- |
|
| Línea -... |
Línea 77... |
| - |
|
77 |
// Course badge fetched by user without "moodle/badges:configuredetails" capability.
|
| 227 |
foreach ($result['badge'][0]['relatedbadges'] as $relatedbadge) {
|
78 |
$this->setGuestUser();
|
| 228 |
$this->assertTrue(isset($relatedbadge['id']));
|
79 |
$result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
|
| 229 |
}
|
80 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| 230 |
} else {
|
81 |
$this->assert_issued_badge($data['coursebadge'], $result['badge'][0], false, false);
|
| 231 |
$this->assertTrue(isset($result['badge'][0]['message']));
|
82 |
$this->assertEmpty($result['warnings']);
|
| Línea -... |
Línea 83... |
| - |
|
83 |
|
| 232 |
}
|
84 |
// Course badge fetched by user with "moodle/badges:configuredetails" capability.
|
| 233 |
|
85 |
$this->setAdminUser();
|
| 234 |
// Course badge.
|
86 |
$result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
|
| 235 |
$result = get_user_badge_by_hash::execute($data['coursebadge'][0]['uniquehash']);
|
87 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
| 236 |
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
|
88 |
$this->assert_issued_badge($data['coursebadge'], $result['badge'][0], false, true);
|
| 237 |
$this->assertNotEmpty($result['badge']);
|
- |
|
| 238 |
$this->assertEmpty($result['warnings']);
|
89 |
$this->assertEmpty($result['warnings']);
|
| 239 |
|
90 |
|