Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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