Skip to content

Commit

Permalink
MDL-70963 core_badges: Use expected JSON format for OBv2.1
Browse files Browse the repository at this point in the history
This patch adds some changes to guarantee that OpenBadges 2.1 (OBv2.1)
will return always the same JSON format that OBv2.0 (because the only
difference between both versions it's the BadgeConnect API).
  • Loading branch information
sarjona committed Mar 29, 2021
1 parent d65ed58 commit 51ec496
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion badges/assertion.php
Expand Up @@ -52,7 +52,7 @@
} else { // Revoked badge.
header("HTTP/1.0 410 Gone");
$assertion = array();
if ($obversion == OPEN_BADGES_V2) {
if ($obversion >= OPEN_BADGES_V2) {
$assertionurl = new moodle_url('/badges/assertion.php', array('b' => $hash));
$assertion['id'] = $assertionurl->out();
}
Expand Down
4 changes: 2 additions & 2 deletions badges/classes/assertion.php
Expand Up @@ -128,7 +128,7 @@ public function get_badge_assertion($issued = true, $usesalt = true) {
$email = empty($this->_data->backpackemail) ? $this->_data->email : $this->_data->backpackemail;
$assertionurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'obversion' => $this->_obversion));

if ($this->_obversion == OPEN_BADGES_V2) {
if ($this->_obversion >= OPEN_BADGES_V2) {
$classurl = new moodle_url('/badges/badge_json.php', array('id' => $this->get_badge_id()));
} else {
$classurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'action' => 1));
Expand Down Expand Up @@ -325,7 +325,7 @@ public function get_alignments() {
*/
protected function embed_data_badge_version2 (&$json, $type = OPEN_BADGES_V2_TYPE_ASSERTION) {
// Specification Version 2.0.
if ($this->_obversion == OPEN_BADGES_V2) {
if ($this->_obversion >= OPEN_BADGES_V2) {
$badge = new badge($this->_data->id);
if (empty($this->_data->courseid)) {
$context = context_system::instance();
Expand Down
12 changes: 11 additions & 1 deletion badges/tests/badgeslib_test.php
Expand Up @@ -32,7 +32,7 @@

use core_badges\helper;

class core_badges_badgeslib_testcase extends advanced_testcase {
class badgeslib_test extends advanced_testcase {
protected $badgeid;
protected $course;
protected $user;
Expand Down Expand Up @@ -698,6 +698,16 @@ public function test_badges_assertion() {
$this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
$this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
$this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));

// Test Openbadge specification version 2.1. It has the same format as OBv2.0.
// Get assertion version 2.1.
$award = reset($awards);
$assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2P1);

// Make sure JSON strings have the same structure.
$this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
$this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
$this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion badges/tests/events_test.php
Expand Up @@ -31,7 +31,7 @@
* @copyright 2015 onwards Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_badges_events_testcase extends core_badges_badgeslib_testcase {
class events_test extends badgeslib_test {

/**
* Test badge awarded event.
Expand Down

0 comments on commit 51ec496

Please sign in to comment.