f845b622b2b1500033b09a2d48e1998efb247faa
[moodle.git] / webservice / tests / externallib_test.php
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/>.
17 defined('MOODLE_INTERNAL') || die();
19 global $CFG;
20 require_once($CFG->dirroot . '/webservice/externallib.php');
21 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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 {
33     public function setUp() {
34         // Calling parent is good, always
35         parent::setUp();
37         // We always need enabled WS for this testcase
38         set_config('enablewebservices', '1');
39     }
41     public function test_get_site_info() {
42         global $DB, $USER, $CFG;
44         $this->resetAfterTest(true);
46         $maxbytes = 10485760;
47         $userquota = 5242880;
48         set_config('maxbytes', $maxbytes);
49         set_config('userquota', $userquota);
51         // Set current user
52         $user = array();
53         $user['username'] = 'johnd';
54         $user['firstname'] = 'John';
55         $user['lastname'] = 'Doe';
56         self::setUser(self::getDataGenerator()->create_user($user));
58         // Add a web service and token.
59         $webservice = new stdClass();
60         $webservice->name = 'Test web service';
61         $webservice->enabled = true;
62         $webservice->restrictedusers = false;
63         $webservice->component = 'moodle';
64         $webservice->timecreated = time();
65         $webservice->downloadfiles = true;
66         $webservice->uploadfiles = true;
67         $externalserviceid = $DB->insert_record('external_services', $webservice);
69         // Add a function to the service
70         $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
71             'functionname' => 'core_course_get_contents'));
73         $_POST['wstoken'] = 'testtoken';
74         $externaltoken = new stdClass();
75         $externaltoken->token = 'testtoken';
76         $externaltoken->tokentype = 0;
77         $externaltoken->userid = $USER->id;
78         $externaltoken->externalserviceid = $externalserviceid;
79         $externaltoken->contextid = 1;
80         $externaltoken->creatorid = $USER->id;
81         $externaltoken->timecreated = time();
82         $DB->insert_record('external_tokens', $externaltoken);
84         $siteinfo = core_webservice_external::get_site_info();
86         // We need to execute the return values cleaning process to simulate the web service server.
87         $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
89         $this->assertEquals('johnd', $siteinfo['username']);
90         $this->assertEquals('John', $siteinfo['firstname']);
91         $this->assertEquals('Doe', $siteinfo['lastname']);
92         $this->assertEquals(current_language(), $siteinfo['lang']);
93         $this->assertEquals($USER->id, $siteinfo['userid']);
94         $this->assertEquals(SITEID, $siteinfo['siteid']);
95         $this->assertEquals(true, $siteinfo['downloadfiles']);
96         $this->assertEquals($CFG->release, $siteinfo['release']);
97         $this->assertEquals($CFG->version, $siteinfo['version']);
98         $this->assertEquals('', $siteinfo['mobilecssurl']);
99         $this->assertEquals(count($siteinfo['functions']), 1);
100         $function = array_pop($siteinfo['functions']);
101         $this->assertEquals($function['name'], 'core_course_get_contents');
102         $this->assertEquals($function['version'], $siteinfo['version']);
103         $this->assertEquals(1, $siteinfo['downloadfiles']);
104         $this->assertEquals(1, $siteinfo['uploadfiles']);
106         foreach ($siteinfo['advancedfeatures'] as $feature) {
107             if ($feature['name'] == 'mnet_dispatcher_mode') {
108                 if ($CFG->mnet_dispatcher_mode == 'off') {
109                     $this->assertEquals(0, $feature['value']);
110                 } else {
111                     $this->assertEquals(1, $feature['value']);
112                 }
113             } else {
114                 $this->assertEquals($CFG->{$feature['name']}, $feature['value']);
115             }
116         }
118         $this->assertEquals($userquota, $siteinfo['userquota']);
119         // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is
120         // covered below for admin user. This test is for user not allowed to ignore limits.
121         $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']);
122         $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
124         $this->assertEquals(HOMEPAGE_MY, $siteinfo['userhomepage']);
126         // Now as admin.
127         $this->setAdminUser();
129         // Set a fake token for the user admin.
130         $_POST['wstoken'] = 'testtoken';
131         $externaltoken = new stdClass();
132         $externaltoken->token = 'testtoken';
133         $externaltoken->tokentype = 0;
134         $externaltoken->userid = $USER->id;
135         $externaltoken->externalserviceid = $externalserviceid;
136         $externaltoken->contextid = 1;
137         $externaltoken->creatorid = $USER->id;
138         $externaltoken->timecreated = time();
139         $DB->insert_record('external_tokens', $externaltoken);
141         // Set a home page by user preferences.
142         $CFG->defaulthomepage = HOMEPAGE_USER;
143         set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
145         $siteinfo = core_webservice_external::get_site_info();
147         // We need to execute the return values cleaning process to simulate the web service server.
148         $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
150         $this->assertEquals(0, $siteinfo['userquota']);
151         $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']);
152         $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
154         $this->assertEquals(HOMEPAGE_SITE, $siteinfo['userhomepage']);
156     }