Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->libdir . '/badgeslib.php');
  31  require_once($CFG->dirroot . '/badges/lib.php');
  32  
  33  use core_badges\helper;
  34  
  35  class core_badges_badgeslib_testcase extends advanced_testcase {
  36      protected $badgeid;
  37      protected $course;
  38      protected $user;
  39      protected $module;
  40      protected $coursebadge;
  41      protected $assertion;
  42  
  43      /** @var $assertion2 to define json format for Open badge version 2 */
  44      protected $assertion2;
  45  
  46      protected function setUp() {
  47          global $DB, $CFG;
  48          $this->resetAfterTest(true);
  49          $CFG->enablecompletion = true;
  50          $user = $this->getDataGenerator()->create_user();
  51          $fordb = new stdClass();
  52          $fordb->id = null;
  53          $fordb->name = "Test badge with 'apostrophe' and other friends (<>&@#)";
  54          $fordb->description = "Testing badges";
  55          $fordb->timecreated = time();
  56          $fordb->timemodified = time();
  57          $fordb->usercreated = $user->id;
  58          $fordb->usermodified = $user->id;
  59          $fordb->issuername = "Test issuer";
  60          $fordb->issuerurl = "http://issuer-url.domain.co.nz";
  61          $fordb->issuercontact = "issuer@example.com";
  62          $fordb->expiredate = null;
  63          $fordb->expireperiod = null;
  64          $fordb->type = BADGE_TYPE_SITE;
  65          $fordb->version = 1;
  66          $fordb->language = 'en';
  67          $fordb->courseid = null;
  68          $fordb->messagesubject = "Test message subject";
  69          $fordb->message = "Test message body";
  70          $fordb->attachment = 1;
  71          $fordb->notification = 0;
  72          $fordb->imageauthorname = "Image Author 1";
  73          $fordb->imageauthoremail = "author@example.com";
  74          $fordb->imageauthorurl = "http://author-url.example.com";
  75          $fordb->imagecaption = "Test caption image";
  76          $fordb->status = BADGE_STATUS_INACTIVE;
  77  
  78          $this->badgeid = $DB->insert_record('badge', $fordb, true);
  79  
  80          // Set the default Issuer (because OBv2 needs them).
  81          set_config('badges_defaultissuername', $fordb->issuername);
  82          set_config('badges_defaultissuercontact', $fordb->issuercontact);
  83  
  84          // Create a course with activity and auto completion tracking.
  85          $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
  86          $this->user = $this->getDataGenerator()->create_user();
  87          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  88          $this->assertNotEmpty($studentrole);
  89  
  90          // Get manual enrolment plugin and enrol user.
  91          require_once($CFG->dirroot.'/enrol/manual/locallib.php');
  92          $manplugin = enrol_get_plugin('manual');
  93          $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  94          $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
  95          $this->assertEquals(1, $DB->count_records('user_enrolments'));
  96          $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
  97          $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
  98  
  99          // Build badge and criteria.
 100          $fordb->type = BADGE_TYPE_COURSE;
 101          $fordb->courseid = $this->course->id;
 102          $fordb->status = BADGE_STATUS_ACTIVE;
 103          $this->coursebadge = $DB->insert_record('badge', $fordb, true);
 104  
 105          // Insert Endorsement.
 106          $endorsement = new stdClass();
 107          $endorsement->badgeid = $this->coursebadge;
 108          $endorsement->issuername = "Issuer 123";
 109          $endorsement->issueremail = "issuer123@email.com";
 110          $endorsement->issuerurl = "https://example.org/issuer-123";
 111          $endorsement->dateissued = 1524567747;
 112          $endorsement->claimid = "https://example.org/robotics-badge.json";
 113          $endorsement->claimcomment = "Test endorser comment";
 114          $DB->insert_record('badge_endorsement', $endorsement, true);
 115  
 116          // Insert related badges.
 117          $badge = new badge($this->coursebadge);
 118          $clonedid = $badge->make_clone();
 119          $badgeclone = new badge($clonedid);
 120          $badgeclone->status = BADGE_STATUS_ACTIVE;
 121          $badgeclone->save();
 122  
 123          $relatebadge = new stdClass();
 124          $relatebadge->badgeid = $this->coursebadge;
 125          $relatebadge->relatedbadgeid = $clonedid;
 126          $relatebadge->relatedid = $DB->insert_record('badge_related', $relatebadge, true);
 127  
 128          // Insert a aligment.
 129          $alignment = new stdClass();
 130          $alignment->badgeid = $this->coursebadge;
 131          $alignment->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
 132          $alignment->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
 133          $alignment->targetdescription = 'Test target description';
 134          $alignment->targetframework = 'CCSS.RST.11-12.3';
 135          $alignment->targetcode = 'CCSS.RST.11-12.3';
 136          $DB->insert_record('badge_alignment', $alignment, true);
 137  
 138          $this->assertion = new stdClass();
 139          $this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s"}';
 140          $this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s"}';
 141          $this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
 142          // Format JSON-LD for Openbadge specification version 2.0.
 143          $this->assertion2 = new stdClass();
 144          $this->assertion2->badge = '{"recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
 145              '"badge":{"name":"%s","description":"%s","image":"%s",' .
 146              '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
 147              '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
 148              '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
 149              '"@language":"en","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
 150              '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
 151              '"targetCode":"%s"}]},"verify":{"type":"hosted","url":"%s"},"issuedOn":"%s","evidence":"%s",' .
 152              '"@context":"https:\/\/w3id.org\/openbadges\/v2","type":"Assertion","id":"%s"}';
 153  
 154          $this->assertion2->class = '{"name":"%s","description":"%s","image":"%s",' .
 155              '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
 156              '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
 157              '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
 158              '"@language":"%s","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
 159              '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
 160              '"targetCode":"%s"}]}';
 161          $this->assertion2->issuer = '{"name":"%s","url":"%s","email":"%s",' .
 162              '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"}';
 163      }
 164  
 165      public function test_create_badge() {
 166          $badge = new badge($this->badgeid);
 167  
 168          $this->assertInstanceOf('badge', $badge);
 169          $this->assertEquals($this->badgeid, $badge->id);
 170      }
 171  
 172      public function test_clone_badge() {
 173          $badge = new badge($this->badgeid);
 174          $newid = $badge->make_clone();
 175          $clonedbadge = new badge($newid);
 176  
 177          $this->assertEquals($badge->description, $clonedbadge->description);
 178          $this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
 179          $this->assertEquals($badge->issuername, $clonedbadge->issuername);
 180          $this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
 181          $this->assertEquals($badge->issuerurl, $clonedbadge->issuerurl);
 182          $this->assertEquals($badge->expiredate, $clonedbadge->expiredate);
 183          $this->assertEquals($badge->expireperiod, $clonedbadge->expireperiod);
 184          $this->assertEquals($badge->type, $clonedbadge->type);
 185          $this->assertEquals($badge->courseid, $clonedbadge->courseid);
 186          $this->assertEquals($badge->message, $clonedbadge->message);
 187          $this->assertEquals($badge->messagesubject, $clonedbadge->messagesubject);
 188          $this->assertEquals($badge->attachment, $clonedbadge->attachment);
 189          $this->assertEquals($badge->notification, $clonedbadge->notification);
 190          $this->assertEquals($badge->version, $clonedbadge->version);
 191          $this->assertEquals($badge->language, $clonedbadge->language);
 192          $this->assertEquals($badge->imagecaption, $clonedbadge->imagecaption);
 193          $this->assertEquals($badge->imageauthorname, $clonedbadge->imageauthorname);
 194          $this->assertEquals($badge->imageauthoremail, $clonedbadge->imageauthoremail);
 195          $this->assertEquals($badge->imageauthorurl, $clonedbadge->imageauthorurl);
 196      }
 197  
 198      public function test_badge_status() {
 199          $badge = new badge($this->badgeid);
 200          $old_status = $badge->status;
 201          $badge->set_status(BADGE_STATUS_ACTIVE);
 202          $this->assertAttributeNotEquals($old_status, 'status', $badge);
 203          $this->assertAttributeEquals(BADGE_STATUS_ACTIVE, 'status', $badge);
 204      }
 205  
 206      public function test_delete_badge() {
 207          $badge = new badge($this->badgeid);
 208          $badge->delete();
 209          // We don't actually delete badges. We archive them.
 210          $this->assertAttributeEquals(BADGE_STATUS_ARCHIVED, 'status', $badge);
 211      }
 212  
 213      /**
 214       * Really delete the badge.
 215       */
 216      public function test_delete_badge_for_real() {
 217          global $DB;
 218  
 219          $badge = new badge($this->badgeid);
 220  
 221          $newid1 = $badge->make_clone();
 222          $newid2 = $badge->make_clone();
 223          $newid3 = $badge->make_clone();
 224  
 225          // Insert related badges to badge 1.
 226          $badge->add_related_badges([$newid1, $newid2, $newid3]);
 227  
 228          // Another badge.
 229          $badge2 = new badge($newid2);
 230          // Make badge 1 related for badge 2.
 231          $badge2->add_related_badges([$this->badgeid]);
 232  
 233          // Confirm that the records about this badge about its relations have been removed as well.
 234          $relatedsql = 'badgeid = :badgeid OR relatedbadgeid = :relatedbadgeid';
 235          $relatedparams = array(
 236              'badgeid' => $this->badgeid,
 237              'relatedbadgeid' => $this->badgeid
 238          );
 239          // Badge 1 has 4 related records. 3 where it's the badgeid, 1 where it's the relatedbadgeid.
 240          $this->assertEquals(4, $DB->count_records_select('badge_related', $relatedsql, $relatedparams));
 241  
 242          // Delete the badge for real.
 243          $badge->delete(false);
 244  
 245          // Confirm that the badge itself has been removed.
 246          $this->assertFalse($DB->record_exists('badge', ['id' => $this->badgeid]));
 247  
 248          // Confirm that the records about this badge about its relations have been removed as well.
 249          $this->assertFalse($DB->record_exists_select('badge_related', $relatedsql, $relatedparams));
 250      }
 251  
 252      public function test_create_badge_criteria() {
 253          $badge = new badge($this->badgeid);
 254          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 255          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 256  
 257          $this->assertCount(1, $badge->get_criteria());
 258  
 259          $criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 260          $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
 261          $criteria_profile->save($params);
 262  
 263          $this->assertCount(2, $badge->get_criteria());
 264      }
 265  
 266      public function test_add_badge_criteria_description() {
 267          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
 268          $criteriaoverall->save(array(
 269                  'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
 270                  'description' => 'Overall description',
 271                  'descriptionformat' => FORMAT_HTML
 272          ));
 273  
 274          $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
 275          $params = array(
 276                  'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
 277                  'field_address' => 'address',
 278                  'description' => 'Description',
 279                  'descriptionformat' => FORMAT_HTML
 280          );
 281          $criteriaprofile->save($params);
 282  
 283          $badge = new badge($this->badgeid);
 284          $this->assertEquals('Overall description', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->description);
 285          $this->assertEquals('Description', $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->description);
 286      }
 287  
 288      public function test_delete_badge_criteria() {
 289          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
 290          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 291          $badge = new badge($this->badgeid);
 292  
 293          $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
 294  
 295          $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
 296          $this->assertEmpty($badge->get_criteria());
 297      }
 298  
 299      public function test_badge_awards() {
 300          global $DB;
 301          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 302          $badge = new badge($this->badgeid);
 303          $user1 = $this->getDataGenerator()->create_user();
 304  
 305          $sink = $this->redirectMessages();
 306  
 307          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
 308          set_user_preference('message_provider_moodle_badgerecipientnotice_loggedoff', 'email', $user1);
 309  
 310          $badge->issue($user1->id, false);
 311          $this->assertDebuggingCalled(); // Expect debugging while baking a badge via phpunit.
 312          $this->assertTrue($badge->is_issued($user1->id));
 313  
 314          $messages = $sink->get_messages();
 315          $sink->close();
 316          $this->assertCount(1, $messages);
 317          $message = array_pop($messages);
 318          // Check we have the expected data.
 319          $customdata = json_decode($message->customdata);
 320          $this->assertObjectHasAttribute('notificationiconurl', $customdata);
 321          $this->assertObjectHasAttribute('hash', $customdata);
 322  
 323          $user2 = $this->getDataGenerator()->create_user();
 324          $badge->issue($user2->id, true);
 325          $this->assertTrue($badge->is_issued($user2->id));
 326  
 327          $this->assertCount(2, $badge->get_awards());
 328      }
 329  
 330      /**
 331       * Test the {@link badges_get_user_badges()} function in lib/badgeslib.php
 332       */
 333      public function test_badges_get_user_badges() {
 334          global $DB;
 335  
 336          // Messaging is not compatible with transactions.
 337          $this->preventResetByRollback();
 338  
 339          $badges = array();
 340          $user1 = $this->getDataGenerator()->create_user();
 341          $user2 = $this->getDataGenerator()->create_user();
 342  
 343          // Record the current time, we need to be precise about a couple of things.
 344          $now = time();
 345          // Create 11 badges with which to test.
 346          for ($i = 1; $i <= 11; $i++) {
 347              // Mock up a badge.
 348              $badge = new stdClass();
 349              $badge->id = null;
 350              $badge->name = "Test badge $i";
 351              $badge->description = "Testing badges $i";
 352              $badge->timecreated = $now - 12;
 353              $badge->timemodified = $now - 12;
 354              $badge->usercreated = $user1->id;
 355              $badge->usermodified = $user1->id;
 356              $badge->issuername = "Test issuer";
 357              $badge->issuerurl = "http://issuer-url.domain.co.nz";
 358              $badge->issuercontact = "issuer@example.com";
 359              $badge->expiredate = null;
 360              $badge->expireperiod = null;
 361              $badge->type = BADGE_TYPE_SITE;
 362              $badge->courseid = null;
 363              $badge->messagesubject = "Test message subject for badge $i";
 364              $badge->message = "Test message body for badge $i";
 365              $badge->attachment = 1;
 366              $badge->notification = 0;
 367              $badge->status = BADGE_STATUS_INACTIVE;
 368              $badge->version = "Version $i";
 369              $badge->language = "en";
 370              $badge->imagecaption = "Image caption $i";
 371              $badge->imageauthorname = "Image author's name $i";
 372              $badge->imageauthoremail = "author$i@example.com";
 373              $badge->imageauthorname = "Image author's name $i";
 374  
 375              $badgeid = $DB->insert_record('badge', $badge, true);
 376              $badges[$badgeid] = new badge($badgeid);
 377              $badges[$badgeid]->issue($user2->id, true);
 378              // Check it all actually worked.
 379              $this->assertCount(1, $badges[$badgeid]->get_awards());
 380  
 381              // Hack the database to adjust the time each badge was issued.
 382              // The alternative to this is sleep which is a no-no in unit tests.
 383              $DB->set_field('badge_issued', 'dateissued', $now - 11 + $i, array('userid' => $user2->id, 'badgeid' => $badgeid));
 384          }
 385  
 386          // Make sure the first user has no badges.
 387          $result = badges_get_user_badges($user1->id);
 388          $this->assertInternalType('array', $result);
 389          $this->assertCount(0, $result);
 390  
 391          // Check that the second user has the expected 11 badges.
 392          $result = badges_get_user_badges($user2->id);
 393          $this->assertCount(11, $result);
 394  
 395          // Test pagination.
 396          // Ordering is by time issued desc, so things will come out with the last awarded badge first.
 397          $result = badges_get_user_badges($user2->id, 0, 0, 4);
 398          $this->assertCount(4, $result);
 399          $lastbadgeissued = reset($result);
 400          $this->assertSame('Test badge 11', $lastbadgeissued->name);
 401          // Page 2. Expecting 4 results again.
 402          $result = badges_get_user_badges($user2->id, 0, 1, 4);
 403          $this->assertCount(4, $result);
 404          $lastbadgeissued = reset($result);
 405          $this->assertSame('Test badge 7', $lastbadgeissued->name);
 406          // Page 3. Expecting just three results here.
 407          $result = badges_get_user_badges($user2->id, 0, 2, 4);
 408          $this->assertCount(3, $result);
 409          $lastbadgeissued = reset($result);
 410          $this->assertSame('Test badge 3', $lastbadgeissued->name);
 411          // Page 4.... there is no page 4.
 412          $result = badges_get_user_badges($user2->id, 0, 3, 4);
 413          $this->assertCount(0, $result);
 414  
 415          // Test search.
 416          $result = badges_get_user_badges($user2->id, 0, 0, 0, 'badge 1');
 417          $this->assertCount(3, $result);
 418          $lastbadgeissued = reset($result);
 419          $this->assertSame('Test badge 11', $lastbadgeissued->name);
 420          // The term Totara doesn't appear anywhere in the badges.
 421          $result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara');
 422          $this->assertCount(0, $result);
 423  
 424          // Issue a user with a course badge and verify its returned based on if
 425          // coursebadges are enabled or disabled.
 426          $sitebadgeid = key($badges);
 427          $badges[$sitebadgeid]->issue($this->user->id, true);
 428  
 429          $badge = new stdClass();
 430          $badge->id = null;
 431          $badge->name = "Test course badge";
 432          $badge->description = "Testing course badge";
 433          $badge->timecreated = $now;
 434          $badge->timemodified = $now;
 435          $badge->usercreated = $user1->id;
 436          $badge->usermodified = $user1->id;
 437          $badge->issuername = "Test issuer";
 438          $badge->issuerurl = "http://issuer-url.domain.co.nz";
 439          $badge->issuercontact = "issuer@example.com";
 440          $badge->expiredate = null;
 441          $badge->expireperiod = null;
 442          $badge->type = BADGE_TYPE_COURSE;
 443          $badge->courseid = $this->course->id;
 444          $badge->messagesubject = "Test message subject for course badge";
 445          $badge->message = "Test message body for course badge";
 446          $badge->attachment = 1;
 447          $badge->notification = 0;
 448          $badge->status = BADGE_STATUS_ACTIVE;
 449          $badge->version = "Version $i";
 450          $badge->language = "en";
 451          $badge->imagecaption = "Image caption";
 452          $badge->imageauthorname = "Image author's name";
 453          $badge->imageauthoremail = "author@example.com";
 454          $badge->imageauthorname = "Image author's name";
 455  
 456          $badgeid = $DB->insert_record('badge', $badge, true);
 457          $badges[$badgeid] = new badge($badgeid);
 458          $badges[$badgeid]->issue($this->user->id, true);
 459  
 460          // With coursebadges off, we should only get the site badge.
 461          set_config('badges_allowcoursebadges', false);
 462          $result = badges_get_user_badges($this->user->id);
 463          $this->assertCount(1, $result);
 464  
 465          // With it on, we should get both.
 466          set_config('badges_allowcoursebadges', true);
 467          $result = badges_get_user_badges($this->user->id);
 468          $this->assertCount(2, $result);
 469  
 470      }
 471  
 472      public function data_for_message_from_template() {
 473          return array(
 474              array(
 475                  'This is a message with no variables',
 476                  array(), // no params
 477                  'This is a message with no variables'
 478              ),
 479              array(
 480                  'This is a message with %amissing% variables',
 481                  array(), // no params
 482                  'This is a message with %amissing% variables'
 483              ),
 484              array(
 485                  'This is a message with %one% variable',
 486                  array('one' => 'a single'),
 487                  'This is a message with a single variable'
 488              ),
 489              array(
 490                  'This is a message with %one% %two% %three% variables',
 491                  array('one' => 'more', 'two' => 'than', 'three' => 'one'),
 492                  'This is a message with more than one variables'
 493              ),
 494              array(
 495                  'This is a message with %three% %two% %one%',
 496                  array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
 497                  'This is a message with randomly ordered variables'
 498              ),
 499              array(
 500                  'This is a message with %repeated% %one% %repeated% of variables',
 501                  array('one' => 'and', 'repeated' => 'lots'),
 502                  'This is a message with lots and lots of variables'
 503              ),
 504          );
 505      }
 506  
 507      /**
 508       * @dataProvider data_for_message_from_template
 509       */
 510      public function test_badge_message_from_template($message, $params, $result) {
 511          $this->assertEquals(badge_message_from_template($message, $params), $result);
 512      }
 513  
 514      /**
 515       * Test badges observer when course module completion event id fired.
 516       */
 517      public function test_badges_observer_course_module_criteria_review() {
 518          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 519          $badge = new badge($this->coursebadge);
 520          $this->assertFalse($badge->is_issued($this->user->id));
 521  
 522          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 523          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 524          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
 525          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
 526  
 527          // Assert the badge will not be issued to the user as is.
 528          $badge = new badge($this->coursebadge);
 529          $badge->review_all_criteria();
 530          $this->assertFalse($badge->is_issued($this->user->id));
 531  
 532          // Set completion for forum activity.
 533          $c = new completion_info($this->course);
 534          $activities = $c->get_activities();
 535          $this->assertEquals(1, count($activities));
 536          $this->assertTrue(isset($activities[$this->module->cmid]));
 537          $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
 538  
 539          $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
 540          $current->completionstate = COMPLETION_COMPLETE;
 541          $current->timemodified = time();
 542          $sink = $this->redirectEmails();
 543          $c->internal_set_data($activities[$this->module->cmid], $current);
 544          $this->assertCount(1, $sink->get_messages());
 545          $sink->close();
 546  
 547          // Check if badge is awarded.
 548          $this->assertDebuggingCalled('Error baking badge image!');
 549          $this->assertTrue($badge->is_issued($this->user->id));
 550      }
 551  
 552      /**
 553       * Test badges observer when course_completed event is fired.
 554       */
 555      public function test_badges_observer_course_criteria_review() {
 556          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 557          $badge = new badge($this->coursebadge);
 558          $this->assertFalse($badge->is_issued($this->user->id));
 559  
 560          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 561          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 562          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
 563          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
 564  
 565          $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
 566  
 567          // Assert the badge will not be issued to the user as is.
 568          $badge = new badge($this->coursebadge);
 569          $badge->review_all_criteria();
 570          $this->assertFalse($badge->is_issued($this->user->id));
 571  
 572          // Mark course as complete.
 573          $sink = $this->redirectEmails();
 574          $ccompletion->mark_complete();
 575          $this->assertCount(1, $sink->get_messages());
 576          $sink->close();
 577  
 578          // Check if badge is awarded.
 579          $this->assertDebuggingCalled('Error baking badge image!');
 580          $this->assertTrue($badge->is_issued($this->user->id));
 581      }
 582  
 583      /**
 584       * Test badges observer when user_updated event is fired.
 585       */
 586      public function test_badges_observer_profile_criteria_review() {
 587          global $CFG, $DB;
 588          require_once($CFG->dirroot.'/user/profile/lib.php');
 589  
 590          // Add a custom field of textarea type.
 591          $customprofileid = $DB->insert_record('user_info_field', array(
 592              'shortname' => 'newfield', 'name' => 'Description of new field', 'categoryid' => 1,
 593              'datatype' => 'textarea'));
 594  
 595          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 596          $badge = new badge($this->coursebadge);
 597  
 598          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 599          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 600          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 601          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim',
 602              'field_' . $customprofileid => $customprofileid));
 603  
 604          // Assert the badge will not be issued to the user as is.
 605          $badge = new badge($this->coursebadge);
 606          $badge->review_all_criteria();
 607          $this->assertFalse($badge->is_issued($this->user->id));
 608  
 609          // Set the required fields and make sure the badge got issued.
 610          $this->user->address = 'Test address';
 611          $this->user->aim = '999999999';
 612          $sink = $this->redirectEmails();
 613          profile_save_data((object)array('id' => $this->user->id, 'profile_field_newfield' => 'X'));
 614          user_update_user($this->user, false);
 615          $this->assertCount(1, $sink->get_messages());
 616          $sink->close();
 617          // Check if badge is awarded.
 618          $this->assertDebuggingCalled('Error baking badge image!');
 619          $this->assertTrue($badge->is_issued($this->user->id));
 620      }
 621  
 622      /**
 623       * Test badges observer when cohort_member_added event is fired and user required to belong to any cohort.
 624       *
 625       * @covers \award_criteria_cohort
 626       */
 627      public function test_badges_observer_any_cohort_criteria_review() {
 628          global $CFG;
 629  
 630          require_once("$CFG->dirroot/cohort/lib.php");
 631  
 632          $cohort1 = $this->getDataGenerator()->create_cohort();
 633          $cohort2 = $this->getDataGenerator()->create_cohort();
 634  
 635          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 636  
 637          $badge = new badge($this->badgeid);
 638          $this->assertFalse($badge->is_issued($this->user->id));
 639          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 640  
 641          // Set up the badge criteria.
 642          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 643          $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 644          $criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
 645          $criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY,
 646              'cohort_cohorts' => array('0' => $cohort1->id, '1' => $cohort2->id)));
 647          $badge->set_status(BADGE_STATUS_ACTIVE);
 648  
 649          // Reload it to contain criteria.
 650          $badge = new badge($this->badgeid);
 651          $this->assertFalse($badge->is_issued($this->user->id));
 652          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 653  
 654          // Add the user to the cohort.
 655          cohort_add_member($cohort2->id, $this->user->id);
 656          $this->assertDebuggingCalled();
 657  
 658          // Verify that the badge was awarded.
 659          $this->assertTrue($badge->is_issued($this->user->id));
 660          // As the badge has been awarded to user because core_badges_observer been called when the member has been added to the
 661          // cohort, there are no other users that can award this badge.
 662          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 663      }
 664  
 665      /**
 666       * Test badges observer when cohort_member_added event is fired and user required to belong to multiple (all) cohorts.
 667       *
 668       * @covers \award_criteria_cohort
 669       */
 670      public function test_badges_observer_all_cohort_criteria_review() {
 671          global $CFG;
 672  
 673          require_once("$CFG->dirroot/cohort/lib.php");
 674  
 675          $cohort1 = $this->getDataGenerator()->create_cohort();
 676          $cohort2 = $this->getDataGenerator()->create_cohort();
 677          $cohort3 = $this->getDataGenerator()->create_cohort();
 678  
 679          // Add user2 to cohort1 and cohort3.
 680          $user2 = $this->getDataGenerator()->create_user();
 681          cohort_add_member($cohort3->id, $user2->id);
 682          cohort_add_member($cohort1->id, $user2->id);
 683  
 684          // Add user3 to cohort1, cohort2 and cohort3.
 685          $user3 = $this->getDataGenerator()->create_user();
 686          cohort_add_member($cohort1->id, $user3->id);
 687          cohort_add_member($cohort2->id, $user3->id);
 688          cohort_add_member($cohort3->id, $user3->id);
 689  
 690          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 691  
 692          // Cohort criteria are used in site badges.
 693          $badge = new badge($this->badgeid);
 694  
 695          $this->assertFalse($badge->is_issued($this->user->id));
 696          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 697  
 698          // Set up the badge criteria.
 699          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 700          $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 701          $criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
 702          $criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL,
 703              'cohort_cohorts' => array('0' => $cohort1->id, '1' => $cohort2->id, '2' => $cohort3->id)));
 704          $badge->set_status(BADGE_STATUS_ACTIVE);
 705  
 706          // Reload it to contain criteria.
 707          $badge = new badge($this->badgeid);
 708  
 709          // Verify that the badge was not awarded yet (ALL cohorts are needed and review_all_criteria has to be called).
 710          $this->assertFalse($badge->is_issued($this->user->id));
 711          $this->assertFalse($badge->is_issued($user2->id));
 712          $this->assertFalse($badge->is_issued($user3->id));
 713  
 714          // Verify that after calling review_all_criteria, users with the criteria (user3) award the badge instantly.
 715          $this->assertSame(1, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 716          $this->assertFalse($badge->is_issued($this->user->id));
 717          $this->assertFalse($badge->is_issued($user2->id));
 718          $this->assertTrue($badge->is_issued($user3->id));
 719          $this->assertDebuggingCalled();
 720  
 721          // Add the user to the cohort1.
 722          cohort_add_member($cohort1->id, $this->user->id);
 723  
 724          // Verify that the badge was not awarded yet (ALL cohorts are needed).
 725          $this->assertFalse($badge->is_issued($this->user->id));
 726          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 727  
 728          // Add the user to the cohort3.
 729          cohort_add_member($cohort3->id, $this->user->id);
 730  
 731          // Verify that the badge was not awarded yet (ALL cohorts are needed).
 732          $this->assertFalse($badge->is_issued($this->user->id));
 733          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 734  
 735          // Add user to cohort2.
 736          cohort_add_member($cohort2->id, $this->user->id);
 737          $this->assertDebuggingCalled();
 738  
 739          // Verify that the badge was awarded (ALL cohorts).
 740          $this->assertTrue($badge->is_issued($this->user->id));
 741          // As the badge has been awarded to user because core_badges_observer been called when the member has been added to the
 742          // cohort, there are no other users that can award this badge.
 743          $this->assertSame(0, $badge->review_all_criteria()); // Verify award_criteria_cohort->get_completed_criteria_sql().
 744      }
 745  
 746      /**
 747       * Test badges assertion generated when a badge is issued.
 748       */
 749      public function test_badges_assertion() {
 750          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 751          $badge = new badge($this->coursebadge);
 752          $this->assertFalse($badge->is_issued($this->user->id));
 753  
 754          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 755          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 756          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 757          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
 758  
 759          $this->user->address = 'Test address';
 760          $sink = $this->redirectEmails();
 761          user_update_user($this->user, false);
 762          $this->assertCount(1, $sink->get_messages());
 763          $sink->close();
 764          // Check if badge is awarded.
 765          $this->assertDebuggingCalled('Error baking badge image!');
 766          $awards = $badge->get_awards();
 767          $this->assertCount(1, $awards);
 768  
 769          // Get assertion.
 770          $award = reset($awards);
 771          $assertion = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V1);
 772          $testassertion = $this->assertion;
 773  
 774          // Make sure JSON strings have the same structure.
 775          $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
 776          $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
 777          $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
 778  
 779          // Test Openbadge specification version 2.
 780          // Get assertion version 2.
 781          $award = reset($awards);
 782          $assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2);
 783          $testassertion2 = $this->assertion2;
 784  
 785          // Make sure JSON strings have the same structure.
 786          $this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
 787          $this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
 788          $this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));
 789  
 790          // Test Openbadge specification version 2.1. It has the same format as OBv2.0.
 791          // Get assertion version 2.1.
 792          $award = reset($awards);
 793          $assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2P1);
 794  
 795          // Make sure JSON strings have the same structure.
 796          $this->assertStringMatchesFormat($testassertion2->badge, json_encode($assertion2->get_badge_assertion()));
 797          $this->assertStringMatchesFormat($testassertion2->class, json_encode($assertion2->get_badge_class()));
 798          $this->assertStringMatchesFormat($testassertion2->issuer, json_encode($assertion2->get_issuer()));
 799      }
 800  
 801      /**
 802       * Tests the core_badges_myprofile_navigation() function.
 803       */
 804      public function test_core_badges_myprofile_navigation() {
 805          // Set up the test.
 806          $tree = new \core_user\output\myprofile\tree();
 807          $this->setAdminUser();
 808          $badge = new badge($this->badgeid);
 809          $badge->issue($this->user->id, true);
 810          $iscurrentuser = true;
 811          $course = null;
 812  
 813          // Enable badges.
 814          set_config('enablebadges', true);
 815  
 816          // Check the node tree is correct.
 817          core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
 818          $reflector = new ReflectionObject($tree);
 819          $nodes = $reflector->getProperty('nodes');
 820          $nodes->setAccessible(true);
 821          $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
 822      }
 823  
 824      /**
 825       * Tests the core_badges_myprofile_navigation() function with badges disabled..
 826       */
 827      public function test_core_badges_myprofile_navigation_badges_disabled() {
 828          // Set up the test.
 829          $tree = new \core_user\output\myprofile\tree();
 830          $this->setAdminUser();
 831          $badge = new badge($this->badgeid);
 832          $badge->issue($this->user->id, true);
 833          $iscurrentuser = false;
 834          $course = null;
 835  
 836          // Disable badges.
 837          set_config('enablebadges', false);
 838  
 839          // Check the node tree is correct.
 840          core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course);
 841          $reflector = new ReflectionObject($tree);
 842          $nodes = $reflector->getProperty('nodes');
 843          $nodes->setAccessible(true);
 844          $this->assertArrayNotHasKey('localbadges', $nodes->getValue($tree));
 845      }
 846  
 847      /**
 848       * Tests the core_badges_myprofile_navigation() function with a course badge.
 849       */
 850      public function test_core_badges_myprofile_navigation_with_course_badge() {
 851          // Set up the test.
 852          $tree = new \core_user\output\myprofile\tree();
 853          $this->setAdminUser();
 854          $badge = new badge($this->coursebadge);
 855          $badge->issue($this->user->id, true);
 856          $iscurrentuser = false;
 857  
 858          // Check the node tree is correct.
 859          core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $this->course);
 860          $reflector = new ReflectionObject($tree);
 861          $nodes = $reflector->getProperty('nodes');
 862          $nodes->setAccessible(true);
 863          $this->assertArrayHasKey('localbadges', $nodes->getValue($tree));
 864      }
 865  
 866      /**
 867       * Test insert and update endorsement with a site badge.
 868       */
 869      public function test_badge_endorsement() {
 870          $badge = new badge($this->badgeid);
 871  
 872          // Insert Endorsement.
 873          $endorsement = new stdClass();
 874          $endorsement->badgeid = $this->badgeid;
 875          $endorsement->issuername = "Issuer 123";
 876          $endorsement->issueremail = "issuer123@email.com";
 877          $endorsement->issuerurl = "https://example.org/issuer-123";
 878          $endorsement->dateissued = 1524567747;
 879          $endorsement->claimid = "https://example.org/robotics-badge.json";
 880          $endorsement->claimcomment = "Test endorser comment";
 881  
 882          $badge->save_endorsement($endorsement);
 883          $endorsement1 = $badge->get_endorsement();
 884          $this->assertEquals($endorsement->badgeid, $endorsement1->badgeid);
 885          $this->assertEquals($endorsement->issuername, $endorsement1->issuername);
 886          $this->assertEquals($endorsement->issueremail, $endorsement1->issueremail);
 887          $this->assertEquals($endorsement->issuerurl, $endorsement1->issuerurl);
 888          $this->assertEquals($endorsement->dateissued, $endorsement1->dateissued);
 889          $this->assertEquals($endorsement->claimid, $endorsement1->claimid);
 890          $this->assertEquals($endorsement->claimcomment, $endorsement1->claimcomment);
 891  
 892          // Update Endorsement.
 893          $endorsement1->issuername = "Issuer update";
 894          $badge->save_endorsement($endorsement1);
 895          $endorsement2 = $badge->get_endorsement();
 896          $this->assertEquals($endorsement1->id, $endorsement2->id);
 897          $this->assertEquals($endorsement1->issuername, $endorsement2->issuername);
 898      }
 899  
 900      /**
 901       * Test insert and delete related badge with a site badge.
 902       */
 903      public function test_badge_related() {
 904          $badge = new badge($this->badgeid);
 905          $newid1 = $badge->make_clone();
 906          $newid2 = $badge->make_clone();
 907          $newid3 = $badge->make_clone();
 908  
 909          // Insert an related badge.
 910          $badge->add_related_badges([$newid1, $newid2, $newid3]);
 911          $this->assertCount(3, $badge->get_related_badges());
 912  
 913          // Only get related is active.
 914          $clonedbage1 = new badge($newid1);
 915          $clonedbage1->status = BADGE_STATUS_ACTIVE;
 916          $clonedbage1->save();
 917          $this->assertCount(1, $badge->get_related_badges(true));
 918  
 919          // Delete an related badge.
 920          $badge->delete_related_badge($newid2);
 921          $this->assertCount(2, $badge->get_related_badges());
 922      }
 923  
 924      /**
 925       * Test insert, update, delete alignment with a site badge.
 926       */
 927      public function test_alignments() {
 928          $badge = new badge($this->badgeid);
 929  
 930          // Insert a alignment.
 931          $alignment1 = new stdClass();
 932          $alignment1->badgeid = $this->badgeid;
 933          $alignment1->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
 934          $alignment1->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
 935          $alignment1->targetdescription = 'Test target description';
 936          $alignment1->targetframework = 'CCSS.RST.11-12.3';
 937          $alignment1->targetcode = 'CCSS.RST.11-12.3';
 938          $alignment2 = clone $alignment1;
 939          $newid1 = $badge->save_alignment($alignment1);
 940          $newid2 = $badge->save_alignment($alignment2);
 941          $alignments1 = $badge->get_alignments();
 942          $this->assertCount(2, $alignments1);
 943  
 944          $this->assertEquals($alignment1->badgeid, $alignments1[$newid1]->badgeid);
 945          $this->assertEquals($alignment1->targetname, $alignments1[$newid1]->targetname);
 946          $this->assertEquals($alignment1->targeturl, $alignments1[$newid1]->targeturl);
 947          $this->assertEquals($alignment1->targetdescription, $alignments1[$newid1]->targetdescription);
 948          $this->assertEquals($alignment1->targetframework, $alignments1[$newid1]->targetframework);
 949          $this->assertEquals($alignment1->targetcode, $alignments1[$newid1]->targetcode);
 950  
 951          // Update aligment.
 952          $alignments1[$newid1]->targetname = 'CCSS.ELA-Literacy.RST.11-12.3 update';
 953          $badge->save_alignment($alignments1[$newid1], $alignments1[$newid1]->id);
 954          $alignments2 = $badge->get_alignments();
 955          $this->assertEquals($alignments1[$newid1]->id, $alignments2[$newid1]->id);
 956          $this->assertEquals($alignments1[$newid1]->targetname, $alignments2[$newid1]->targetname);
 957  
 958          // Delete alignment.
 959          $badge->delete_alignment($alignments1[$newid2]->id);
 960          $this->assertCount(1, $badge->get_alignments());
 961      }
 962  
 963      /**
 964       * Test badges_delete_site_backpack().
 965       *
 966       */
 967      public function test_badges_delete_site_backpack(): void {
 968          global $DB;
 969  
 970          $this->setAdminUser();
 971  
 972          // Create one backpack.
 973          $total = $DB->count_records('badge_external_backpack');
 974          $this->assertEquals(1, $total);
 975  
 976          $data = new \stdClass();
 977          $data->apiversion = OPEN_BADGES_V2P1;
 978          $data->backpackapiurl = 'https://dc.imsglobal.org/obchost/ims/ob/v2p1';
 979          $data->backpackweburl = 'https://dc.imsglobal.org';
 980          badges_create_site_backpack($data);
 981          $backpack = $DB->get_record('badge_external_backpack', ['backpackweburl' => $data->backpackweburl]);
 982          $user1 = $this->getDataGenerator()->create_user();
 983          $user2 = $this->getDataGenerator()->create_user();
 984          // User1 is connected to the backpack to be removed and has 2 collections.
 985          $backpackuser1 = helper::create_fake_backpack(['userid' => $user1->id, 'externalbackpackid' => $backpack->id]);
 986          helper::create_fake_backpack_collection(['backpackid' => $backpackuser1->id]);
 987          helper::create_fake_backpack_collection(['backpackid' => $backpackuser1->id]);
 988          // User2 is connected to a different backpack and has 1 collection.
 989          $backpackuser2 = helper::create_fake_backpack(['userid' => $user2->id]);
 990          helper::create_fake_backpack_collection(['backpackid' => $backpackuser2->id]);
 991  
 992          $total = $DB->count_records('badge_external_backpack');
 993          $this->assertEquals(2, $total);
 994          $total = $DB->count_records('badge_backpack');
 995          $this->assertEquals(2, $total);
 996          $total = $DB->count_records('badge_external');
 997          $this->assertEquals(3, $total);
 998  
 999          // Remove the backpack created previously.
1000          $result = badges_delete_site_backpack($backpack->id);
1001          $this->assertTrue($result);
1002  
1003          $total = $DB->count_records('badge_external_backpack');
1004          $this->assertEquals(1, $total);
1005  
1006          $total = $DB->count_records('badge_backpack');
1007          $this->assertEquals(1, $total);
1008  
1009          $total = $DB->count_records('badge_external');
1010          $this->assertEquals(1, $total);
1011  
1012          // Try to remove an non-existent backpack.
1013          $result = badges_delete_site_backpack($backpack->id);
1014          $this->assertFalse($result);
1015      }
1016  }