weekly release 3.1dev
[moodle.git] / webservice / tests / externallib_test.php
CommitLineData
639bc8b2
PC
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
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
498bc10c 20require_once($CFG->dirroot . '/webservice/externallib.php');
639bc8b2
PC
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22
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 */
8252b7c2 31class core_webservice_externallib_testcase extends externallib_advanced_testcase {
639bc8b2 32
498bc10c
EL
33 public function setUp() {
34 // Calling parent is good, always
35 parent::setUp();
36
37 // We always need enabled WS for this testcase
639bc8b2 38 set_config('enablewebservices', '1');
639bc8b2
PC
39 }
40
41 public function test_get_site_info() {
42 global $DB, $USER, $CFG;
43
44 $this->resetAfterTest(true);
498bc10c
EL
45
46 // This is the info we are going to check
47 set_config('release', '2.4dev (Build: 20120823)');
48 set_config('version', '2012083100.00');
49
116c4a62
JL
50 $maxbytes = 10485760;
51 $userquota = 5242880;
52 set_config('maxbytes', $maxbytes);
53 set_config('userquota', $userquota);
54
498bc10c 55 // Set current user
639bc8b2
PC
56 $user = array();
57 $user['username'] = 'johnd';
58 $user['firstname'] = 'John';
59 $user['lastname'] = 'Doe';
60 self::setUser(self::getDataGenerator()->create_user($user));
61
62 // Add a web service and token.
63 $webservice = new stdClass();
64 $webservice->name = 'Test web service';
65 $webservice->enabled = true;
66 $webservice->restrictedusers = false;
67 $webservice->component = 'moodle';
68 $webservice->timecreated = time();
69 $webservice->downloadfiles = true;
106c55fb 70 $webservice->uploadfiles = true;
639bc8b2
PC
71 $externalserviceid = $DB->insert_record('external_services', $webservice);
72
87ede25c
JM
73 // Add a function to the service
74 $DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
75 'functionname' => 'core_course_get_contents'));
76
639bc8b2
PC
77 $_POST['wstoken'] = 'testtoken';
78 $externaltoken = new stdClass();
79 $externaltoken->token = 'testtoken';
80 $externaltoken->tokentype = 0;
81 $externaltoken->userid = $USER->id;
82 $externaltoken->externalserviceid = $externalserviceid;
83 $externaltoken->contextid = 1;
84 $externaltoken->creatorid = $USER->id;
85 $externaltoken->timecreated = time();
86 $DB->insert_record('external_tokens', $externaltoken);
87
88 $siteinfo = core_webservice_external::get_site_info();
89
fb695f6e
JM
90 // We need to execute the return values cleaning process to simulate the web service server.
91 $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
92
639bc8b2
PC
93 $this->assertEquals('johnd', $siteinfo['username']);
94 $this->assertEquals('John', $siteinfo['firstname']);
95 $this->assertEquals('Doe', $siteinfo['lastname']);
92fa81d3 96 $this->assertEquals(current_language(), $siteinfo['lang']);
639bc8b2
PC
97 $this->assertEquals($USER->id, $siteinfo['userid']);
98 $this->assertEquals(true, $siteinfo['downloadfiles']);
99 $this->assertEquals($CFG->release, $siteinfo['release']);
100 $this->assertEquals($CFG->version, $siteinfo['version']);
44b3927b 101 $this->assertEquals($CFG->mobilecssurl, $siteinfo['mobilecssurl']);
87ede25c
JM
102 $this->assertEquals(count($siteinfo['functions']), 1);
103 $function = array_pop($siteinfo['functions']);
104 $this->assertEquals($function['name'], 'core_course_get_contents');
105 $this->assertEquals($function['version'], $siteinfo['version']);
106c55fb
DW
106 $this->assertEquals(1, $siteinfo['downloadfiles']);
107 $this->assertEquals(1, $siteinfo['uploadfiles']);
14ae63be
JL
108
109 foreach ($siteinfo['advancedfeatures'] as $feature) {
110 if ($feature['name'] == 'mnet_dispatcher_mode') {
111 if ($CFG->mnet_dispatcher_mode == 'off') {
112 $this->assertEquals(0, $feature['value']);
113 } else {
114 $this->assertEquals(1, $feature['value']);
115 }
116 } else {
117 $this->assertEquals($CFG->{$feature['name']}, $feature['value']);
118 }
119 }
120
116c4a62 121 $this->assertEquals($userquota, $siteinfo['userquota']);
33d65f70
EL
122 // We can use the function for the expectation because USER_CAN_IGNORE_FILE_SIZE_LIMITS is
123 // covered below for admin user. This test is for user not allowed to ignore limits.
124 $this->assertEquals(get_max_upload_file_size($maxbytes), $siteinfo['usermaxuploadfilesize']);
116c4a62
JL
125 $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
126
127 // Now as admin.
128 $this->setAdminUser();
129
130 // Set a fake token for the user admin.
131 $_POST['wstoken'] = 'testtoken';
132 $externaltoken = new stdClass();
133 $externaltoken->token = 'testtoken';
134 $externaltoken->tokentype = 0;
135 $externaltoken->userid = $USER->id;
136 $externaltoken->externalserviceid = $externalserviceid;
137 $externaltoken->contextid = 1;
138 $externaltoken->creatorid = $USER->id;
139 $externaltoken->timecreated = time();
140 $DB->insert_record('external_tokens', $externaltoken);
141 $siteinfo = core_webservice_external::get_site_info();
142
143 // We need to execute the return values cleaning process to simulate the web service server.
144 $siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
145
146 $this->assertEquals(0, $siteinfo['userquota']);
147 $this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS, $siteinfo['usermaxuploadfilesize']);
148 $this->assertEquals(true, $siteinfo['usercanmanageownfiles']);
149
639bc8b2
PC
150 }
151
152}