Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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  defined('MOODLE_INTERNAL') || die();
  18  
  19  global $CFG;
  20  require_once($CFG->dirroot . '/webservice/externallib.php');
  21  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  22  
  23  /**
  24   * External course functions unit tests
  25   *
  26   * @package    core_webservice
  27   * @category   external
  28   * @copyright  2012 Paul Charsley
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class core_webservice_externallib_testcase extends externallib_advanced_testcase {
  32  
  33      public function setUp(): void {
  34          // Calling parent is good, always
  35          parent::setUp();
  36  
  37          // We always need enabled WS for this testcase
  38          set_config('enablewebservices', '1');
  39      }
  40  
  41      public function test_get_site_info() {
  42          global $DB, $USER, $CFG, $PAGE;
  43  
  44          $this->resetAfterTest(true);
  45  
  46          $maxbytes = 10485760;
  47          $userquota = 5242880;
  48          set_config('maxbytes', $maxbytes);
  49          set_config('userquota', $userquota);
  50  
  51          // Set current user
  52          set_config('allowuserthemes', 1);
  53          $user = array();
  54          $user['username'] = 'johnd';
  55          $user['firstname'] = 'John';
  56          $user['lastname'] = 'Doe';
  57          $user['theme'] = 'boost';
  58          self::setUser(self::getDataGenerator()->create_user($user));
  59  
  60          // Add a web service and token.
  61          $webservice = new stdClass();
  62          $webservice->name = 'Test web service';
  63          $webservice->enabled = true;
  64          $webservice->restrictedusers = false;
  65          $webservice->component = 'moodle';
  66          $webservice->timecreated = time();
  67          $webservice->downloadfiles = true;
  68          $webservice->uploadfiles = true;
  69          $externalserviceid = $DB->insert_record('external_services', $webservice);
  70  
  71          // Add a function to the service
  72          $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
  73              'functionname' => 'core_course_get_contents'));
  74  
  75          $_POST['wstoken'] = 'testtoken';
  76          $externaltoken = new stdClass();
  77          $externaltoken->token = 'testtoken';
  78          $externaltoken->tokentype = 0;
  79          $externaltoken->userid = $USER->id;
  80          $externaltoken->externalserviceid = $externalserviceid;
  81          $externaltoken->contextid = 1;
  82          $externaltoken->creatorid = $USER->id;
  83          $externaltoken->timecreated = time();
  84          $DB->insert_record('external_tokens', $externaltoken);
  85  
  86          $siteinfo = core_webservice_external::get_site_info();
  87  
  88          // We need to execute the return values cleaning process to simulate the web service server.
  89          $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
  90  
  91          $this->assertEquals('johnd', $siteinfo['username']);
  92          $this->assertEquals('John', $siteinfo['firstname']);
  93          $this->assertEquals('Doe', $siteinfo['lastname']);
  94          $this->assertEquals(current_language(), $siteinfo['lang']);
  95          $this->assertEquals($USER->id, $siteinfo['userid']);
  96          $this->assertEquals(SITEID, $siteinfo['siteid']);
  97          $this->assertEquals(true, $siteinfo['downloadfiles']);
  98          $this->assertEquals($CFG->release, $siteinfo['release']);
  99          $this->assertEquals($CFG->version, $siteinfo['version']);
 100          $this->assertEquals('', $siteinfo['mobilecssurl']);
 101          $this->assertEquals(count($siteinfo['functions']), 1);
 102          $function = array_pop($siteinfo['functions']);
 103          $this->assertEquals($function['name'], 'core_course_get_contents');
 104          $this->assertEquals($function['version'], $siteinfo['version']);
 105          $this->assertEquals(1, $siteinfo['downloadfiles']);
 106          $this->assertEquals(1, $siteinfo['uploadfiles']);
 107  
 108          foreach ($siteinfo['advancedfeatures'] as $feature) {
 109              if ($feature['name'] == 'mnet_dispatcher_mode') {
 110                  if ($CFG->mnet_dispatcher_mode == 'off') {
 111                      $this->assertEquals(0, $feature['value']);
 112                  } else {
 113                      $this->assertEquals(1, $feature['value']);
 114                  }
 115              } else {
 116                  $this->assertEquals($CFG->{$feature['name']}, $feature['value']);
 117              }
 118          }
 119  
 120          $this->assertEquals($userquota, $siteinfo['userquota']);
 121          // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is
 122          // covered below for admin user. This test is for user not allowed to ignore limits.
 123          $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']);
 124          $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
 125          $userkey = get_user_key('core_files', $USER->id);
 126          $this->assertEquals($userkey, $siteinfo['userprivateaccesskey']);
 127  
 128          $this->assertEquals(HOMEPAGE_MY, $siteinfo['userhomepage']);
 129          $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']);
 130          if (!empty($USER->calendartype)) {
 131              $this->assertEquals($USER->calendartype, $siteinfo['usercalendartype']);
 132          } else {
 133              $this->assertEquals($CFG->calendartype, $siteinfo['usercalendartype']);
 134          }
 135          $this->assertFalse($siteinfo['userissiteadmin']);
 136          $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']);
 137          $this->assertEquals($user['theme'], $siteinfo['theme']);
 138  
 139          // Now as admin.
 140          $this->setAdminUser();
 141  
 142          // Set a fake token for the user admin.
 143          $_POST['wstoken'] = 'testtoken';
 144          $externaltoken = new stdClass();
 145          $externaltoken->token = 'testtoken';
 146          $externaltoken->tokentype = 0;
 147          $externaltoken->userid = $USER->id;
 148          $externaltoken->externalserviceid = $externalserviceid;
 149          $externaltoken->contextid = 1;
 150          $externaltoken->creatorid = $USER->id;
 151          $externaltoken->timecreated = time();
 152          $DB->insert_record('external_tokens', $externaltoken);
 153  
 154          // Set a home page by user preferences.
 155          $CFG->defaulthomepage = HOMEPAGE_USER;
 156          set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
 157  
 158          $siteinfo = core_webservice_external::get_site_info();
 159  
 160          // We need to execute the return values cleaning process to simulate the web service server.
 161          $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
 162  
 163          $this->assertEquals(0, $siteinfo['userquota']);
 164          $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']);
 165          $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
 166          $this->assertTrue($siteinfo['userissiteadmin']);
 167          $this->assertEmpty($USER->theme);
 168          $this->assertEquals($PAGE->theme->name, $siteinfo['theme']);
 169      }
 170  
 171      /**
 172       * Test get_site_info with values > PHP_INT_MAX. We check only userquota since maxbytes require PHP ini changes.
 173       */
 174      public function test_get_site_info_max_int() {
 175          $this->resetAfterTest(true);
 176  
 177          self::setUser(self::getDataGenerator()->create_user());
 178  
 179          // Check values higher than PHP_INT_MAX. This value may come from settings (as string).
 180          $userquota = PHP_INT_MAX . '000';
 181          set_config('userquota', $userquota);
 182  
 183          $result = core_webservice_external::get_site_info();
 184          $result = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $result);
 185          $this->assertEquals(PHP_INT_MAX, $result['userquota']);
 186      }
 187  
 188  }