Merge branch 'MDL-51434-master' of git://github.com/jleyva/moodle
[moodle.git] / lib / tests / externallib_test.php
blob0caa69eb8bf0118a58d23b57fcffd54c2ca21cd1
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.
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 /**
18 * Unit tests for /lib/externallib.php.
20 * @package core
21 * @subpackage phpunit
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/externallib.php');
32 class core_externallib_testcase extends advanced_testcase {
33 public function test_validate_params() {
34 $params = array('text'=>'aaa', 'someid'=>'6');
35 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value'),
36 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
37 $result = external_api::validate_parameters($description, $params);
38 $this->assertCount(2, $result);
39 reset($result);
40 $this->assertSame('someid', key($result));
41 $this->assertSame(6, $result['someid']);
42 $this->assertSame('aaa', $result['text']);
44 $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666);
45 $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT, 'Some ID')),
46 'scalar' => new external_value(PARAM_ALPHANUM, 'Some text value')));
47 $result = external_api::validate_parameters($description, $params);
48 $this->assertCount(2, $result);
49 reset($result);
50 $this->assertSame('someids', key($result));
51 $this->assertEquals(array(0=>1, 1=>2, 2=>3), $result['someids']);
52 $this->assertSame('666', $result['scalar']);
54 $params = array('text'=>'aaa');
55 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false),
56 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
57 $result = external_api::validate_parameters($description, $params);
58 $this->assertCount(2, $result);
59 reset($result);
60 $this->assertSame('someid', key($result));
61 $this->assertNull($result['someid']);
62 $this->assertSame('aaa', $result['text']);
64 $params = array('text'=>'aaa');
65 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false, 6),
66 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
67 $result = external_api::validate_parameters($description, $params);
68 $this->assertCount(2, $result);
69 reset($result);
70 $this->assertSame('someid', key($result));
71 $this->assertSame(6, $result['someid']);
72 $this->assertSame('aaa', $result['text']);
75 public function test_external_format_text() {
76 $settings = external_settings::get_instance();
78 $currentraw = $settings->get_raw();
79 $currentfilter = $settings->get_filter();
81 $settings->set_raw(true);
82 $settings->set_filter(false);
83 $context = context_system::instance();
85 $test = '$$ \pi $$';
86 $testformat = FORMAT_MARKDOWN;
87 $correct = array($test, $testformat);
88 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
90 $settings->set_raw(false);
91 $settings->set_filter(true);
93 $test = '$$ \pi $$';
94 $testformat = FORMAT_MARKDOWN;
95 $correct = array('<span class="nolink"><span class="filter_mathjaxloader_equation"><p>$$ \pi $$</p>
96 </span></span>', FORMAT_HTML);
97 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
99 $settings->set_raw($currentraw);
100 $settings->set_filter($currentfilter);
103 public function test_external_format_string() {
104 $settings = external_settings::get_instance();
106 $currentraw = $settings->get_raw();
107 $currentfilter = $settings->get_filter();
109 $settings->set_raw(true);
110 $context = context_system::instance();
112 $test = '$$ \pi $$ <script>hi</script> <h3>there</h3>';
113 $correct = $test;
114 $this->assertSame(external_format_string($test, $context->id), $correct);
116 $settings->set_raw(false);
118 $test = '$$ \pi $$<script>hi</script> <h3>there</h3>';
119 $correct = '$$ \pi $$hi there';
120 $this->assertSame(external_format_string($test, $context->id), $correct);
122 $settings->set_raw($currentraw);
123 $settings->set_filter($currentfilter);
127 * Test for clean_returnvalue().
129 public function test_clean_returnvalue() {
131 // Build some return value decription.
132 $returndesc = new external_multiple_structure(
133 new external_single_structure(
134 array(
135 'object' => new external_single_structure(
136 array('value1' => new external_value(PARAM_INT, 'this is a int'))),
137 'value2' => new external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL))
140 // Clean an object (it should be cast into an array).
141 $object = new stdClass();
142 $object->value1 = 1;
143 $singlestructure['object'] = $object;
144 $singlestructure['value2'] = 'Some text';
145 $testdata = array($singlestructure);
146 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
147 $cleanedsinglestructure = array_pop($cleanedvalue);
148 $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
149 $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
151 // Missing VALUE_OPTIONAL.
152 $object = new stdClass();
153 $object->value1 = 1;
154 $singlestructure = new stdClass();
155 $singlestructure->object = $object;
156 $testdata = array($singlestructure);
157 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
158 $cleanedsinglestructure = array_pop($cleanedvalue);
159 $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
160 $this->assertArrayNotHasKey('value2', $cleanedsinglestructure);
162 // Unknown attribute (the value should be ignored).
163 $object = array();
164 $object['value1'] = 1;
165 $singlestructure = array();
166 $singlestructure['object'] = $object;
167 $singlestructure['value2'] = 'Some text';
168 $singlestructure['unknownvalue'] = 'Some text to ignore';
169 $testdata = array($singlestructure);
170 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
171 $cleanedsinglestructure = array_pop($cleanedvalue);
172 $this->assertSame($object['value1'], $cleanedsinglestructure['object']['value1']);
173 $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
174 $this->assertArrayNotHasKey('unknownvalue', $cleanedsinglestructure);
176 // Missing required value (an exception is thrown).
177 $object = array();
178 $singlestructure = array();
179 $singlestructure['object'] = $object;
180 $singlestructure['value2'] = 'Some text';
181 $testdata = array($singlestructure);
182 $this->setExpectedException('invalid_response_exception');
183 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
186 * Test external_api::get_context_from_params().
188 public function test_get_context_from_params() {
189 $this->resetAfterTest(true);
190 $course = $this->getDataGenerator()->create_course();
191 $realcontext = context_course::instance($course->id);
193 // Use context id.
194 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => $realcontext->id));
195 $this->assertEquals($realcontext, $fetchedcontext);
197 // Use context level and instance id.
198 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "course", "instanceid" => $course->id));
199 $this->assertEquals($realcontext, $fetchedcontext);
201 // Passing empty values.
202 try {
203 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => 0));
204 $this->fail('Exception expected from get_context_wrapper()');
205 } catch (moodle_exception $e) {
206 $this->assertInstanceOf('invalid_parameter_exception', $e);
209 try {
210 $fetchedcontext = test_exernal_api::get_context_wrapper(array("instanceid" => 0));
211 $this->fail('Exception expected from get_context_wrapper()');
212 } catch (moodle_exception $e) {
213 $this->assertInstanceOf('invalid_parameter_exception', $e);
216 try {
217 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => null));
218 $this->fail('Exception expected from get_context_wrapper()');
219 } catch (moodle_exception $e) {
220 $this->assertInstanceOf('invalid_parameter_exception', $e);
223 // Tests for context with instanceid equal to 0 (System context).
224 $realcontext = context_system::instance();
225 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "system", "instanceid" => 0));
226 $this->assertEquals($realcontext, $fetchedcontext);
228 // Passing wrong level.
229 $this->setExpectedException('invalid_parameter_exception');
230 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "random", "instanceid" => $course->id));
234 * Test external_api::get_context()_from_params parameter validation.
236 public function test_get_context_params() {
237 global $USER;
239 // Call without correct context details.
240 $this->setExpectedException('invalid_parameter_exception');
241 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id));
245 * Test external_api::get_context()_from_params parameter validation.
247 public function test_get_context_params2() {
248 global $USER;
250 // Call without correct context details.
251 $this->setExpectedException('invalid_parameter_exception');
252 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course"));
256 * Test external_api::get_context()_from_params parameter validation.
258 public function test_get_context_params3() {
259 global $USER;
261 // Call without correct context details.
262 $this->resetAfterTest(true);
263 $course = self::getDataGenerator()->create_course();
264 $this->setExpectedException('invalid_parameter_exception');
265 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
270 * Just a wrapper to access protected apis for testing
272 class test_exernal_api extends external_api {
274 public static function get_context_wrapper($params) {
275 return self::get_context_from_params($params);