2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core_webservice
;
19 use externallib_advanced_testcase
;
21 defined('MOODLE_INTERNAL') ||
die();
24 require_once($CFG->dirroot
. '/webservice/externallib.php');
25 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
28 * External course functions unit tests
30 * @package core_webservice
32 * @copyright 2012 Paul Charsley
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class externallib_test
extends externallib_advanced_testcase
{
37 public function setUp(): void
{
38 // Calling parent is good, always
41 // We always need enabled WS for this testcase
42 set_config('enablewebservices', '1');
45 public function test_get_site_info() {
46 global $DB, $USER, $CFG, $PAGE;
48 $this->resetAfterTest(true);
52 set_config('maxbytes', $maxbytes);
53 set_config('userquota', $userquota);
56 set_config('allowuserthemes', 1);
58 $user['username'] = 'johnd';
59 $user['firstname'] = 'John';
60 $user['lastname'] = 'Doe';
61 $user['theme'] = 'boost';
62 self
::setUser(self
::getDataGenerator()->create_user($user));
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);
75 // Add a function to the service
76 $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
77 'functionname' => 'core_course_get_contents'));
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);
90 $siteinfo = \core_webservice_external
::get_site_info();
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);
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']);
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']);
117 $this->assertEquals(1, $feature['value']);
120 $this->assertEquals($CFG->{$feature['name']}, $feature['value']);
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']);
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']);
137 $this->assertEquals($CFG->calendartype
, $siteinfo['usercalendartype']);
139 $this->assertFalse($siteinfo['userissiteadmin']);
140 $this->assertEquals($CFG->calendartype
, $siteinfo['sitecalendartype']);
141 $this->assertEquals($user['theme'], $siteinfo['theme']);
144 $this->setAdminUser();
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);
158 // Set a home page by user preferences.
159 $CFG->defaulthomepage
= HOMEPAGE_USER
;
160 set_user_preference('user_home_page_preference', HOMEPAGE_SITE
);
162 $siteinfo = \core_webservice_external
::get_site_info();
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);
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']);
176 * Test get_site_info with values > PHP_INT_MAX. We check only userquota since maxbytes require PHP ini changes.
178 public function test_get_site_info_max_int() {
179 $this->resetAfterTest(true);
181 self
::setUser(self
::getDataGenerator()->create_user());
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);
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']);