[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/badges/tests/ -> external_test.php (source)

   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  
  89          $badgeid = $DB->insert_record('badge', $badge, true);
  90          $badge = new badge($badgeid);
  91          $badge->issue($this->student->id, true);
  92  
  93          // Hack the database to adjust the time each badge was issued.
  94          $DB->set_field('badge_issued', 'dateissued', $now - 11, array('userid' => $this->student->id, 'badgeid' => $badgeid));
  95  
  96          // Now a course badge.
  97          $badge->id = null;
  98          $badge->name = "Test badge course";
  99          $badge->description = "Testing badges course";
 100          $badge->type = BADGE_TYPE_COURSE;
 101          $badge->courseid = $this->course->id;
 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  
 111      /**
 112       * Test get user badges.
 113       * These is a basic test since the badges_get_my_user_badges used by the external function already has unit tests.
 114       */
 115      public function test_get_my_user_badges() {
 116  
 117          $this->setUser($this->student);
 118  
 119          $result = core_badges_external::get_user_badges();
 120          $result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
 121          $this->assertCount(2, $result['badges']);
 122  
 123          // Pagination and filtering.
 124          $result = core_badges_external::get_user_badges(0, $this->course->id, 0, 1, '', true);
 125          $result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
 126          $this->assertCount(1, $result['badges']);
 127          $this->assertEquals($this->course->id, $result['badges'][0]['courseid']);
 128      }
 129  
 130      /**
 131       * Test get user badges.
 132       */
 133      public function test_get_other_user_badges() {
 134  
 135          $this->setUser($this->teacher);
 136  
 137          $result = core_badges_external::get_user_badges($this->student->id);
 138          $result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
 139  
 140          $this->assertCount(2, $result['badges']);
 141  
 142          // Check that we don't have permissions for view the complete information for site badges.
 143          foreach ($result['badges'] as $badge) {
 144              if (isset($badge['type']) and $badge['type'] == BADGE_TYPE_COURSE) {
 145                  $this->assertTrue(isset($badge['message']));
 146              } else {
 147                  $this->assertFalse(isset($badge['message']));
 148              }
 149          }
 150      }
 151  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1