Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  namespace core_webservice;
  18  
  19  use core_external\external_api;
  20  use externallib_advanced_testcase;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/webservice/externallib.php');
  26  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  27  
  28  /**
  29   * External course functions unit tests
  30   *
  31   * @package    core_webservice
  32   * @category   external
  33   * @copyright  2012 Paul Charsley
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class externallib_test extends externallib_advanced_testcase {
  37  
  38      public function setUp(): void {
  39          // Calling parent is good, always
  40          parent::setUp();
  41  
  42          // We always need enabled WS for this testcase
  43          set_config('enablewebservices', '1');
  44      }
  45  
  46      public function test_get_site_info() {
  47          global $DB, $USER, $CFG, $PAGE;
  48  
  49          $this->resetAfterTest(true);
  50  
  51          $maxbytes = 10485760;
  52          $userquota = 5242880;
  53          set_config('maxbytes', $maxbytes);
  54          set_config('userquota', $userquota);
  55  
  56          // Set current user
  57          set_config('allowuserthemes', 1);
  58          $user = array();
  59          $user['username'] = 'johnd';
  60          $user['firstname'] = 'John';
  61          $user['lastname'] = 'Doe';
  62          $user['theme'] = 'boost';
  63          self::setUser(self::getDataGenerator()->create_user($user));
  64  
  65          // Add a web service and token.
  66          $webservice = new \stdClass();
  67          $webservice->name = 'Test web service';
  68          $webservice->enabled = true;
  69          $webservice->restrictedusers = false;
  70          $webservice->component = 'moodle';
  71          $webservice->timecreated = time();
  72          $webservice->downloadfiles = true;
  73          $webservice->uploadfiles = true;
  74          $externalserviceid = $DB->insert_record('external_services', $webservice);
  75  
  76          // Add a function to the service
  77          $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
  78              'functionname' => 'core_course_get_contents'));
  79  
  80          $_POST['wstoken'] = 'testtoken';
  81          $externaltoken = new \stdClass();
  82          $externaltoken->token = 'testtoken';
  83          $externaltoken->tokentype = 0;
  84          $externaltoken->userid = $USER->id;
  85          $externaltoken->externalserviceid = $externalserviceid;
  86          $externaltoken->contextid = 1;
  87          $externaltoken->creatorid = $USER->id;
  88          $externaltoken->timecreated = time();
  89          $externaltoken->name = \core_external\util::generate_token_name();
  90          $DB->insert_record('external_tokens', $externaltoken);
  91  
  92          $siteinfo = \core_webservice_external::get_site_info();
  93  
  94          // We need to execute the return values cleaning process to simulate the web service server.
  95          $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo);
  96  
  97          $this->assertEquals('johnd', $siteinfo['username']);
  98          $this->assertEquals('John', $siteinfo['firstname']);
  99          $this->assertEquals('Doe', $siteinfo['lastname']);
 100          $this->assertEquals(current_language(), $siteinfo['lang']);
 101          $this->assertEquals($USER->id, $siteinfo['userid']);
 102          $this->assertEquals(SITEID, $siteinfo['siteid']);
 103          $this->assertEquals(true, $siteinfo['downloadfiles']);
 104          $this->assertEquals($CFG->release, $siteinfo['release']);
 105          $this->assertEquals($CFG->version, $siteinfo['version']);
 106          $this->assertEquals('', $siteinfo['mobilecssurl']);
 107          $this->assertEquals(count($siteinfo['functions']), 1);
 108          $function = array_pop($siteinfo['functions']);
 109          $this->assertEquals($function['name'], 'core_course_get_contents');
 110          $this->assertEquals($function['version'], $siteinfo['version']);
 111          $this->assertEquals(1, $siteinfo['downloadfiles']);
 112          $this->assertEquals(1, $siteinfo['uploadfiles']);
 113  
 114          foreach ($siteinfo['advancedfeatures'] as $feature) {
 115              if ($feature['name'] == 'mnet_dispatcher_mode') {
 116                  if ($CFG->mnet_dispatcher_mode == 'off') {
 117                      $this->assertEquals(0, $feature['value']);
 118                  } else {
 119                      $this->assertEquals(1, $feature['value']);
 120                  }
 121              } else {
 122                  $this->assertEquals($CFG->{$feature['name']}, $feature['value']);
 123              }
 124          }
 125  
 126          $this->assertEquals($userquota, $siteinfo['userquota']);
 127          // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is
 128          // covered below for admin user. This test is for user not allowed to ignore limits.
 129          $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']);
 130          $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
 131          $userkey = get_user_key('core_files', $USER->id);
 132          $this->assertEquals($userkey, $siteinfo['userprivateaccesskey']);
 133  
 134          $this->assertEquals(HOMEPAGE_MY, $siteinfo['userhomepage']);
 135          $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']);
 136          if (!empty($USER->calendartype)) {
 137              $this->assertEquals($USER->calendartype, $siteinfo['usercalendartype']);
 138          } else {
 139              $this->assertEquals($CFG->calendartype, $siteinfo['usercalendartype']);
 140          }
 141          $this->assertFalse($siteinfo['userissiteadmin']);
 142          $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']);
 143          $this->assertEquals($user['theme'], $siteinfo['theme']);
 144  
 145          // Now as admin.
 146          $this->setAdminUser();
 147  
 148          // Set a fake token for the user admin.
 149          $_POST['wstoken'] = 'testtoken';
 150          $externaltoken = new \stdClass();
 151          $externaltoken->token = 'testtoken';
 152          $externaltoken->tokentype = 0;
 153          $externaltoken->userid = $USER->id;
 154          $externaltoken->externalserviceid = $externalserviceid;
 155          $externaltoken->contextid = 1;
 156          $externaltoken->creatorid = $USER->id;
 157          $externaltoken->timecreated = time();
 158          $externaltoken->name = \core_external\util::generate_token_name();
 159          $DB->insert_record('external_tokens', $externaltoken);
 160  
 161          // Set a home page by user preferences.
 162          $CFG->defaulthomepage = HOMEPAGE_USER;
 163          set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
 164  
 165          $siteinfo = \core_webservice_external::get_site_info();
 166  
 167          // We need to execute the return values cleaning process to simulate the web service server.
 168          $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo);
 169  
 170          $this->assertEquals(0, $siteinfo['userquota']);
 171          $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']);
 172          $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
 173          $this->assertTrue($siteinfo['userissiteadmin']);
 174          $this->assertEmpty($USER->theme);
 175          $this->assertEquals($PAGE->theme->name, $siteinfo['theme']);
 176          $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']);
 177          $this->assertFalse(isset($siteinfo['usersessionscount']));
 178  
 179          $CFG->limitconcurrentlogins = 1;
 180          $record = new \stdClass();
 181          $record->state        = 0;
 182          $record->sessdata     = null;
 183          $record->userid       = $USER->id;
 184          $record->timemodified = time();
 185          $record->firstip      = $record->lastip = '10.0.0.1';
 186          $record->sid = md5('hokus1');
 187          $record->timecreated = time();
 188          $DB->insert_record('sessions', $record);
 189  
 190          $siteinfo = \core_webservice_external::get_site_info();
 191          $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo);
 192          $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']);
 193          $this->assertEquals(1, $siteinfo['usersessionscount']);
 194      }
 195  
 196      /**
 197       * Test get_site_info with values > PHP_INT_MAX. We check only userquota since maxbytes require PHP ini changes.
 198       */
 199      public function test_get_site_info_max_int() {
 200          $this->resetAfterTest(true);
 201  
 202          self::setUser(self::getDataGenerator()->create_user());
 203  
 204          // Check values higher than PHP_INT_MAX. This value may come from settings (as string).
 205          $userquota = PHP_INT_MAX . '000';
 206          set_config('userquota', $userquota);
 207  
 208          $result = \core_webservice_external::get_site_info();
 209          $result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result);
 210          $this->assertEquals(PHP_INT_MAX, $result['userquota']);
 211      }
 212  
 213      /**
 214       * Test get_site_info with missing components.
 215       */
 216      public function test_get_site_missing_components() {
 217          global $USER, $DB;
 218  
 219          $this->resetAfterTest(true);
 220          $this->setAdminUser();
 221  
 222          // Add a web service and token.
 223          $webservice = new \stdClass();
 224          $webservice->name = 'Test web service';
 225          $webservice->enabled = true;
 226          $webservice->restrictedusers = false;
 227          $webservice->component = 'moodle';
 228          $webservice->timecreated = time();
 229          $webservice->downloadfiles = true;
 230          $webservice->uploadfiles = true;
 231          $externalserviceid = $DB->insert_record('external_services', $webservice);
 232  
 233          // Add a function to the service (missing plugin).
 234          $DB->insert_record('external_functions',
 235              [
 236                  'component' => 'mod_random',
 237                  'name' => 'mod_random_get_info'
 238              ]
 239          );
 240  
 241          // Insert one from missing component.
 242          $DB->insert_record('external_services_functions',
 243              [
 244                  'externalserviceid' => $externalserviceid,
 245                  'functionname' => 'mod_random_get_info'
 246              ]
 247          );
 248          // Insert a core one.
 249          $DB->insert_record('external_services_functions',
 250              [
 251                  'externalserviceid' => $externalserviceid,
 252                  'functionname' => 'core_user_get_users'
 253              ]
 254          );
 255  
 256          $_POST['wstoken'] = 'testtoken';
 257          $externaltoken = new \stdClass();
 258          $externaltoken->token = 'testtoken';
 259          $externaltoken->tokentype = 0;
 260          $externaltoken->userid = $USER->id;
 261          $externaltoken->externalserviceid = $externalserviceid;
 262          $externaltoken->contextid = 1;
 263          $externaltoken->creatorid = $USER->id;
 264          $externaltoken->timecreated = time();
 265          $externaltoken->name = \core_external\util::generate_token_name();
 266          $DB->insert_record('external_tokens', $externaltoken);
 267  
 268          // Execution should complete.
 269          $result = \core_webservice_external::get_site_info();
 270          $result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result);
 271          // Check we ignore the missing component function.
 272          $this->assertCount(1, $result['functions']);
 273          $this->assertEquals('core_user_get_users', $result['functions'][0]['name']);
 274      }
 275  
 276  }