Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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