Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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 $DB->insert_record('external_tokens', $externaltoken); 90 91 $siteinfo = \core_webservice_external::get_site_info(); 92 93 // We need to execute the return values cleaning process to simulate the web service server. 94 $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); 95 96 $this->assertEquals('johnd', $siteinfo['username']); 97 $this->assertEquals('John', $siteinfo['firstname']); 98 $this->assertEquals('Doe', $siteinfo['lastname']); 99 $this->assertEquals(current_language(), $siteinfo['lang']); 100 $this->assertEquals($USER->id, $siteinfo['userid']); 101 $this->assertEquals(SITEID, $siteinfo['siteid']); 102 $this->assertEquals(true, $siteinfo['downloadfiles']); 103 $this->assertEquals($CFG->release, $siteinfo['release']); 104 $this->assertEquals($CFG->version, $siteinfo['version']); 105 $this->assertEquals('', $siteinfo['mobilecssurl']); 106 $this->assertEquals(count($siteinfo['functions']), 1); 107 $function = array_pop($siteinfo['functions']); 108 $this->assertEquals($function['name'], 'core_course_get_contents'); 109 $this->assertEquals($function['version'], $siteinfo['version']); 110 $this->assertEquals(1, $siteinfo['downloadfiles']); 111 $this->assertEquals(1, $siteinfo['uploadfiles']); 112 113 foreach ($siteinfo['advancedfeatures'] as $feature) { 114 if ($feature['name'] == 'mnet_dispatcher_mode') { 115 if ($CFG->mnet_dispatcher_mode == 'off') { 116 $this->assertEquals(0, $feature['value']); 117 } else { 118 $this->assertEquals(1, $feature['value']); 119 } 120 } else { 121 $this->assertEquals($CFG->{$feature['name']}, $feature['value']); 122 } 123 } 124 125 $this->assertEquals($userquota, $siteinfo['userquota']); 126 // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is 127 // covered below for admin user. This test is for user not allowed to ignore limits. 128 $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']); 129 $this->assertEquals(true, $siteinfo['usercanmanageownfiles']); 130 $userkey = get_user_key('core_files', $USER->id); 131 $this->assertEquals($userkey, $siteinfo['userprivateaccesskey']); 132 133 $this->assertEquals(HOMEPAGE_MY, $siteinfo['userhomepage']); 134 $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']); 135 if (!empty($USER->calendartype)) { 136 $this->assertEquals($USER->calendartype, $siteinfo['usercalendartype']); 137 } else { 138 $this->assertEquals($CFG->calendartype, $siteinfo['usercalendartype']); 139 } 140 $this->assertFalse($siteinfo['userissiteadmin']); 141 $this->assertEquals($CFG->calendartype, $siteinfo['sitecalendartype']); 142 $this->assertEquals($user['theme'], $siteinfo['theme']); 143 144 // Now as admin. 145 $this->setAdminUser(); 146 147 // Set a fake token for the user admin. 148 $_POST['wstoken'] = 'testtoken'; 149 $externaltoken = new \stdClass(); 150 $externaltoken->token = 'testtoken'; 151 $externaltoken->tokentype = 0; 152 $externaltoken->userid = $USER->id; 153 $externaltoken->externalserviceid = $externalserviceid; 154 $externaltoken->contextid = 1; 155 $externaltoken->creatorid = $USER->id; 156 $externaltoken->timecreated = time(); 157 $DB->insert_record('external_tokens', $externaltoken); 158 159 // Set a home page by user preferences. 160 $CFG->defaulthomepage = HOMEPAGE_USER; 161 set_user_preference('user_home_page_preference', HOMEPAGE_SITE); 162 163 $siteinfo = \core_webservice_external::get_site_info(); 164 165 // We need to execute the return values cleaning process to simulate the web service server. 166 $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); 167 168 $this->assertEquals(0, $siteinfo['userquota']); 169 $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']); 170 $this->assertEquals(true, $siteinfo['usercanmanageownfiles']); 171 $this->assertTrue($siteinfo['userissiteadmin']); 172 $this->assertEmpty($USER->theme); 173 $this->assertEquals($PAGE->theme->name, $siteinfo['theme']); 174 $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']); 175 $this->assertFalse(isset($siteinfo['usersessionscount'])); 176 177 $CFG->limitconcurrentlogins = 1; 178 $record = new \stdClass(); 179 $record->state = 0; 180 $record->sessdata = null; 181 $record->userid = $USER->id; 182 $record->timemodified = time(); 183 $record->firstip = $record->lastip = '10.0.0.1'; 184 $record->sid = md5('hokus1'); 185 $record->timecreated = time(); 186 $DB->insert_record('sessions', $record); 187 188 $siteinfo = \core_webservice_external::get_site_info(); 189 $siteinfo = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $siteinfo); 190 $this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']); 191 $this->assertEquals(1, $siteinfo['usersessionscount']); 192 } 193 194 /** 195 * Test get_site_info with values > PHP_INT_MAX. We check only userquota since maxbytes require PHP ini changes. 196 */ 197 public function test_get_site_info_max_int() { 198 $this->resetAfterTest(true); 199 200 self::setUser(self::getDataGenerator()->create_user()); 201 202 // Check values higher than PHP_INT_MAX. This value may come from settings (as string). 203 $userquota = PHP_INT_MAX . '000'; 204 set_config('userquota', $userquota); 205 206 $result = \core_webservice_external::get_site_info(); 207 $result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result); 208 $this->assertEquals(PHP_INT_MAX, $result['userquota']); 209 } 210 211 /** 212 * Test get_site_info with missing components. 213 */ 214 public function test_get_site_missing_components() { 215 global $USER, $DB; 216 217 $this->resetAfterTest(true); 218 $this->setAdminUser(); 219 220 // Add a web service and token. 221 $webservice = new \stdClass(); 222 $webservice->name = 'Test web service'; 223 $webservice->enabled = true; 224 $webservice->restrictedusers = false; 225 $webservice->component = 'moodle'; 226 $webservice->timecreated = time(); 227 $webservice->downloadfiles = true; 228 $webservice->uploadfiles = true; 229 $externalserviceid = $DB->insert_record('external_services', $webservice); 230 231 // Add a function to the service (missing plugin). 232 $DB->insert_record('external_functions', 233 [ 234 'component' => 'mod_random', 235 'name' => 'mod_random_get_info' 236 ] 237 ); 238 239 // Insert one from missing component. 240 $DB->insert_record('external_services_functions', 241 [ 242 'externalserviceid' => $externalserviceid, 243 'functionname' => 'mod_random_get_info' 244 ] 245 ); 246 // Insert a core one. 247 $DB->insert_record('external_services_functions', 248 [ 249 'externalserviceid' => $externalserviceid, 250 'functionname' => 'core_user_get_users' 251 ] 252 ); 253 254 $_POST['wstoken'] = 'testtoken'; 255 $externaltoken = new \stdClass(); 256 $externaltoken->token = 'testtoken'; 257 $externaltoken->tokentype = 0; 258 $externaltoken->userid = $USER->id; 259 $externaltoken->externalserviceid = $externalserviceid; 260 $externaltoken->contextid = 1; 261 $externaltoken->creatorid = $USER->id; 262 $externaltoken->timecreated = time(); 263 $DB->insert_record('external_tokens', $externaltoken); 264 265 // Execution should complete. 266 $result = \core_webservice_external::get_site_info(); 267 $result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result); 268 // Check we ignore the missing component function. 269 $this->assertCount(1, $result['functions']); 270 $this->assertEquals('core_user_get_users', $result['functions'][0]['name']); 271 } 272 273 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body