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]

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