MDL-63513 mod_assignment: Add removal of context users.
[moodle.git] / lib / tests / externallib_test.php
blobe7480dc6f9f3845ba22334320d966d57021f4ccc
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 protected $DB;
35 public function setUp() {
36 $this->DB = null;
39 public function tearDown() {
40 global $DB;
41 if ($this->DB !== null) {
42 $DB = $this->DB;
46 /**
47 * Tests for external_settings class.
49 public function test_external_settings() {
51 $settings = \external_settings::get_instance();
52 $currentraw = $settings->get_raw();
53 $currentfilter = $settings->get_filter();
54 $currentfile = $settings->get_file();
55 $currentfileurl = $settings->get_fileurl();
57 $this->assertInstanceOf('external_settings', $settings);
59 // Check apis.
60 $settings->set_file('plugin.php');
61 $this->assertEquals('plugin.php', $settings->get_file());
62 $settings->set_filter(false);
63 $this->assertFalse($settings->get_filter());
64 $settings->set_fileurl(false);
65 $this->assertFalse($settings->get_fileurl());
66 $settings->set_raw(true);
67 $this->assertTrue($settings->get_raw());
69 // Restore original values.
70 $settings->set_file($currentfile);
71 $settings->set_filter($currentfilter);
72 $settings->set_fileurl($currentfileurl);
73 $settings->set_raw($currentraw);
76 public function test_validate_params() {
77 $params = array('text'=>'aaa', 'someid'=>'6');
78 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value'),
79 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
80 $result = external_api::validate_parameters($description, $params);
81 $this->assertCount(2, $result);
82 reset($result);
83 $this->assertSame('someid', key($result));
84 $this->assertSame(6, $result['someid']);
85 $this->assertSame('aaa', $result['text']);
87 $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666);
88 $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT, 'Some ID')),
89 'scalar' => new external_value(PARAM_ALPHANUM, 'Some text value')));
90 $result = external_api::validate_parameters($description, $params);
91 $this->assertCount(2, $result);
92 reset($result);
93 $this->assertSame('someids', key($result));
94 $this->assertEquals(array(0=>1, 1=>2, 2=>3), $result['someids']);
95 $this->assertSame('666', $result['scalar']);
97 $params = array('text'=>'aaa');
98 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false),
99 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
100 $result = external_api::validate_parameters($description, $params);
101 $this->assertCount(2, $result);
102 reset($result);
103 $this->assertSame('someid', key($result));
104 $this->assertNull($result['someid']);
105 $this->assertSame('aaa', $result['text']);
107 $params = array('text'=>'aaa');
108 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false, 6),
109 'text' => new external_value(PARAM_ALPHA, 'Some text value')));
110 $result = external_api::validate_parameters($description, $params);
111 $this->assertCount(2, $result);
112 reset($result);
113 $this->assertSame('someid', key($result));
114 $this->assertSame(6, $result['someid']);
115 $this->assertSame('aaa', $result['text']);
118 public function test_external_format_text() {
119 $settings = external_settings::get_instance();
121 $currentraw = $settings->get_raw();
122 $currentfilter = $settings->get_filter();
124 $settings->set_raw(true);
125 $settings->set_filter(false);
126 $context = context_system::instance();
128 $test = '$$ \pi $$';
129 $testformat = FORMAT_MARKDOWN;
130 $correct = array($test, $testformat);
131 // Function external_format_text should work with context id or context instance.
132 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
133 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct);
135 $settings->set_raw(false);
136 $settings->set_filter(true);
138 $test = '$$ \pi $$';
139 $testformat = FORMAT_MARKDOWN;
140 $correct = array('<span class="filter_mathjaxloader_equation"><p><span class="nolink">$$ \pi $$</span></p>
141 </span>', FORMAT_HTML);
142 // Function external_format_text should work with context id or context instance.
143 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
144 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0), $correct);
146 // Filters can be opted out from by the developer.
147 $test = '$$ \pi $$';
148 $testformat = FORMAT_MARKDOWN;
149 $correct = array('<p>$$ \pi $$</p>
150 ', FORMAT_HTML);
151 // Function external_format_text should work with context id or context instance.
152 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, ['filter' => false]), $correct);
153 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, ['filter' => false]), $correct);
155 $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
156 $testformat = FORMAT_HTML;
157 $correct = array($test, FORMAT_HTML);
158 $options = array('allowid' => true);
159 // Function external_format_text should work with context id or context instance.
160 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
161 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
163 $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
164 $testformat = FORMAT_HTML;
165 $correct = array('<p><a></a><a href="#test">Text</a></p>', FORMAT_HTML);
166 $options = new StdClass();
167 $options->allowid = false;
168 // Function external_format_text should work with context id or context instance.
169 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
170 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
172 $test = '<p><a id="test"></a><a href="#test">Text</a></p>'."\n".'Newline';
173 $testformat = FORMAT_MOODLE;
174 $correct = array('<p><a id="test"></a><a href="#test">Text</a></p> Newline', FORMAT_HTML);
175 $options = new StdClass();
176 $options->newlines = false;
177 // Function external_format_text should work with context id or context instance.
178 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
179 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
181 $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
182 $testformat = FORMAT_MOODLE;
183 $correct = array('<div class="text_to_html">'.$test.'</div>', FORMAT_HTML);
184 $options = new StdClass();
185 $options->para = true;
186 // Function external_format_text should work with context id or context instance.
187 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
188 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
190 $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
191 $testformat = FORMAT_MOODLE;
192 $correct = array($test, FORMAT_HTML);
193 $options = new StdClass();
194 $options->context = $context;
195 // Function external_format_text should work with context id or context instance.
196 $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
197 $this->assertSame(external_format_text($test, $testformat, $context, 'core', '', 0, $options), $correct);
199 $settings->set_raw($currentraw);
200 $settings->set_filter($currentfilter);
203 public function test_external_format_string() {
204 $this->resetAfterTest();
205 $settings = external_settings::get_instance();
206 $currentraw = $settings->get_raw();
207 $currentfilter = $settings->get_filter();
209 // Enable multilang filter to on content and heading.
210 filter_set_global_state('multilang', TEXTFILTER_ON);
211 filter_set_applies_to_strings('multilang', 1);
212 $filtermanager = filter_manager::instance();
213 $filtermanager->reset_caches();
215 $settings->set_raw(true);
216 $settings->set_filter(true);
217 $context = context_system::instance();
219 $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
220 '<script>hi</script> <h3>there</h3>!';
221 $correct = $test;
222 // Function external_format_string should work with context id or context instance.
223 $this->assertSame($correct, external_format_string($test, $context->id));
224 $this->assertSame($correct, external_format_string($test, $context));
226 $settings->set_raw(false);
227 $settings->set_filter(false);
229 $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
230 '<script>hi</script> <h3>there</h3>?';
231 $correct = 'ENFR hi there?';
232 // Function external_format_string should work with context id or context instance.
233 $this->assertSame($correct, external_format_string($test, $context->id));
234 $this->assertSame($correct, external_format_string($test, $context));
236 $settings->set_filter(true);
238 $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
239 '<script>hi</script> <h3>there</h3>@';
240 $correct = 'EN hi there@';
241 // Function external_format_string should work with context id or context instance.
242 $this->assertSame($correct, external_format_string($test, $context->id));
243 $this->assertSame($correct, external_format_string($test, $context));
245 // Filters can be opted out.
246 $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' .
247 '<script>hi</script> <h3>there</h3>%';
248 $correct = 'ENFR hi there%';
249 // Function external_format_string should work with context id or context instance.
250 $this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false]));
251 $this->assertSame($correct, external_format_string($test, $context, false, ['filter' => false]));
253 $this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));
255 $settings->set_raw($currentraw);
256 $settings->set_filter($currentfilter);
260 * Test for clean_returnvalue() for testing that returns the PHP type.
262 public function test_clean_returnvalue_return_php_type() {
264 $returndesc = new external_single_structure(
265 array(
266 'value' => new external_value(PARAM_RAW, 'Some text', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED)
270 // Check return type on exception because the external values does not allow NULL values.
271 $testdata = array('value' => null);
272 try {
273 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
274 } catch (moodle_exception $e) {
275 $this->assertInstanceOf('invalid_response_exception', $e);
276 $this->assertContains('of PHP type "NULL"', $e->debuginfo);
281 * Test for clean_returnvalue().
283 public function test_clean_returnvalue() {
285 // Build some return value decription.
286 $returndesc = new external_multiple_structure(
287 new external_single_structure(
288 array(
289 'object' => new external_single_structure(
290 array('value1' => new external_value(PARAM_INT, 'this is a int'))),
291 'value2' => new external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL))
294 // Clean an object (it should be cast into an array).
295 $object = new stdClass();
296 $object->value1 = 1;
297 $singlestructure['object'] = $object;
298 $singlestructure['value2'] = 'Some text';
299 $testdata = array($singlestructure);
300 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
301 $cleanedsinglestructure = array_pop($cleanedvalue);
302 $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
303 $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
305 // Missing VALUE_OPTIONAL.
306 $object = new stdClass();
307 $object->value1 = 1;
308 $singlestructure = new stdClass();
309 $singlestructure->object = $object;
310 $testdata = array($singlestructure);
311 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
312 $cleanedsinglestructure = array_pop($cleanedvalue);
313 $this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
314 $this->assertArrayNotHasKey('value2', $cleanedsinglestructure);
316 // Unknown attribute (the value should be ignored).
317 $object = array();
318 $object['value1'] = 1;
319 $singlestructure = array();
320 $singlestructure['object'] = $object;
321 $singlestructure['value2'] = 'Some text';
322 $singlestructure['unknownvalue'] = 'Some text to ignore';
323 $testdata = array($singlestructure);
324 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
325 $cleanedsinglestructure = array_pop($cleanedvalue);
326 $this->assertSame($object['value1'], $cleanedsinglestructure['object']['value1']);
327 $this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
328 $this->assertArrayNotHasKey('unknownvalue', $cleanedsinglestructure);
330 // Missing required value (an exception is thrown).
331 $object = array();
332 $singlestructure = array();
333 $singlestructure['object'] = $object;
334 $singlestructure['value2'] = 'Some text';
335 $testdata = array($singlestructure);
336 $this->expectException('invalid_response_exception');
337 $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
340 * Test external_api::get_context_from_params().
342 public function test_get_context_from_params() {
343 $this->resetAfterTest(true);
344 $course = $this->getDataGenerator()->create_course();
345 $realcontext = context_course::instance($course->id);
347 // Use context id.
348 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => $realcontext->id));
349 $this->assertEquals($realcontext, $fetchedcontext);
351 // Use context level and instance id.
352 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "course", "instanceid" => $course->id));
353 $this->assertEquals($realcontext, $fetchedcontext);
355 // Passing empty values.
356 try {
357 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => 0));
358 $this->fail('Exception expected from get_context_wrapper()');
359 } catch (moodle_exception $e) {
360 $this->assertInstanceOf('invalid_parameter_exception', $e);
363 try {
364 $fetchedcontext = test_exernal_api::get_context_wrapper(array("instanceid" => 0));
365 $this->fail('Exception expected from get_context_wrapper()');
366 } catch (moodle_exception $e) {
367 $this->assertInstanceOf('invalid_parameter_exception', $e);
370 try {
371 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => null));
372 $this->fail('Exception expected from get_context_wrapper()');
373 } catch (moodle_exception $e) {
374 $this->assertInstanceOf('invalid_parameter_exception', $e);
377 // Tests for context with instanceid equal to 0 (System context).
378 $realcontext = context_system::instance();
379 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "system", "instanceid" => 0));
380 $this->assertEquals($realcontext, $fetchedcontext);
382 // Passing wrong level.
383 $this->expectException('invalid_parameter_exception');
384 $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "random", "instanceid" => $course->id));
388 * Test external_api::get_context()_from_params parameter validation.
390 public function test_get_context_params() {
391 global $USER;
393 // Call without correct context details.
394 $this->expectException('invalid_parameter_exception');
395 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id));
399 * Test external_api::get_context()_from_params parameter validation.
401 public function test_get_context_params2() {
402 global $USER;
404 // Call without correct context details.
405 $this->expectException('invalid_parameter_exception');
406 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course"));
410 * Test external_api::get_context()_from_params parameter validation.
412 public function test_get_context_params3() {
413 global $USER;
415 // Call without correct context details.
416 $this->resetAfterTest(true);
417 $course = self::getDataGenerator()->create_course();
418 $this->expectException('invalid_parameter_exception');
419 test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
422 public function all_external_info_provider() {
423 global $DB;
425 // We are testing here that all the external function descriptions can be generated without
426 // producing warnings. E.g. misusing optional params will generate a debugging message which
427 // will fail this test.
428 $functions = $DB->get_records('external_functions', array(), 'name');
429 $return = array();
430 foreach ($functions as $f) {
431 $return[$f->name] = array($f);
433 return $return;
437 * @dataProvider all_external_info_provider
439 public function test_all_external_info($f) {
440 $desc = external_api::external_function_info($f);
441 $this->assertNotEmpty($desc->name);
442 $this->assertNotEmpty($desc->classname);
443 $this->assertNotEmpty($desc->methodname);
444 $this->assertEquals($desc->component, clean_param($desc->component, PARAM_COMPONENT));
445 $this->assertInstanceOf('external_function_parameters', $desc->parameters_desc);
446 if ($desc->returns_desc != null) {
447 $this->assertInstanceOf('external_description', $desc->returns_desc);
451 public function test_validate_courses() {
452 $this->resetAfterTest(true);
454 $c1 = $this->getDataGenerator()->create_course();
455 $c2 = $this->getDataGenerator()->create_course();
456 $c3 = $this->getDataGenerator()->create_course();
457 $u1 = $this->getDataGenerator()->create_user();
458 $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
459 $courseids = array($c1->id, $c2->id, $c3->id);
461 $this->setAdminUser();
462 list($courses, $warnings) = external_util::validate_courses($courseids);
463 $this->assertEmpty($warnings);
464 $this->assertCount(3, $courses);
465 $this->assertArrayHasKey($c1->id, $courses);
466 $this->assertArrayHasKey($c2->id, $courses);
467 $this->assertArrayHasKey($c3->id, $courses);
468 $this->assertEquals($c1->id, $courses[$c1->id]->id);
469 $this->assertEquals($c2->id, $courses[$c2->id]->id);
470 $this->assertEquals($c3->id, $courses[$c3->id]->id);
472 $this->setUser($u1);
473 list($courses, $warnings) = external_util::validate_courses($courseids);
474 $this->assertCount(2, $warnings);
475 $this->assertEquals($c2->id, $warnings[0]['itemid']);
476 $this->assertEquals($c3->id, $warnings[1]['itemid']);
477 $this->assertCount(1, $courses);
478 $this->assertArrayHasKey($c1->id, $courses);
479 $this->assertArrayNotHasKey($c2->id, $courses);
480 $this->assertArrayNotHasKey($c3->id, $courses);
481 $this->assertEquals($c1->id, $courses[$c1->id]->id);
485 * Validate courses can re-use an array of prefetched courses.
487 public function test_validate_courses_prefetch() {
488 $this->resetAfterTest(true);
490 $c1 = $this->getDataGenerator()->create_course();
491 $c2 = $this->getDataGenerator()->create_course();
492 $c3 = $this->getDataGenerator()->create_course();
493 $c4 = $this->getDataGenerator()->create_course();
494 $u1 = $this->getDataGenerator()->create_user();
495 $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
496 $this->getDataGenerator()->enrol_user($u1->id, $c2->id);
498 $courseids = array($c1->id, $c2->id, $c3->id);
499 $courses = array($c2->id => $c2, $c3->id => $c3, $c4->id => $c4);
501 $this->setUser($u1);
502 list($courses, $warnings) = external_util::validate_courses($courseids, $courses);
503 $this->assertCount(2, $courses);
504 $this->assertCount(1, $warnings);
505 $this->assertArrayHasKey($c1->id, $courses);
506 $this->assertSame($c2, $courses[$c2->id]);
507 $this->assertArrayNotHasKey($c3->id, $courses);
508 // The extra course passed is not returned.
509 $this->assertArrayNotHasKey($c4->id, $courses);
513 public function test_call_external_function() {
514 global $PAGE, $COURSE;
516 $this->resetAfterTest(true);
518 // Call some webservice functions and verify they are correctly handling $PAGE and $COURSE.
519 // First test a function that calls validate_context outside a course.
520 $this->setAdminUser();
521 $category = $this->getDataGenerator()->create_category();
522 $params = array(
523 'contextid' => context_coursecat::instance($category->id)->id,
524 'name' => 'aaagrrryyy',
525 'idnumber' => '',
526 'description' => ''
528 $cohort1 = $this->getDataGenerator()->create_cohort($params);
529 $cohort2 = $this->getDataGenerator()->create_cohort();
531 $beforepage = $PAGE;
532 $beforecourse = $COURSE;
533 $params = array('cohortids' => array($cohort1->id, $cohort2->id));
534 $result = external_api::call_external_function('core_cohort_get_cohorts', $params);
536 $this->assertSame($beforepage, $PAGE);
537 $this->assertSame($beforecourse, $COURSE);
539 // Now test a function that calls validate_context inside a course.
540 $course = $this->getDataGenerator()->create_course();
542 $beforepage = $PAGE;
543 $beforecourse = $COURSE;
544 $params = array('courseid' => $course->id, 'options' => array());
545 $result = external_api::call_external_function('core_enrol_get_enrolled_users', $params);
547 $this->assertSame($beforepage, $PAGE);
548 $this->assertSame($beforecourse, $COURSE);
552 * Text external_util::get_area_files
554 public function test_external_util_get_area_files() {
555 global $CFG, $DB;
557 $this->DB = $DB;
558 $DB = $this->getMockBuilder('moodle_database')->getMock();
560 $content = base64_encode("Let us create a nice simple file.");
561 $timemodified = 102030405;
562 $itemid = 42;
563 $filesize = strlen($content);
565 $DB->method('get_records_sql')->willReturn([
566 (object) [
567 'filename' => 'example.txt',
568 'filepath' => '/',
569 'mimetype' => 'text/plain',
570 'filesize' => $filesize,
571 'timemodified' => $timemodified,
572 'itemid' => $itemid,
573 'pathnamehash' => sha1('/example.txt'),
577 $component = 'mod_foo';
578 $filearea = 'area';
579 $context = 12345;
581 $expectedfiles[] = array(
582 'filename' => 'example.txt',
583 'filepath' => '/',
584 'fileurl' => "{$CFG->wwwroot}/webservice/pluginfile.php/{$context}/{$component}/{$filearea}/{$itemid}/example.txt",
585 'timemodified' => $timemodified,
586 'filesize' => $filesize,
587 'mimetype' => 'text/plain',
588 'isexternalfile' => false,
590 // Get all the files for the area.
591 $files = external_util::get_area_files($context, $component, $filearea, false);
592 $this->assertEquals($expectedfiles, $files);
594 // Get just the file indicated by $itemid.
595 $files = external_util::get_area_files($context, $component, $filearea, $itemid);
596 $this->assertEquals($expectedfiles, $files);
601 * Text external files structure.
603 public function test_external_files() {
605 $description = new external_files();
607 // First check that the expected default values and keys are returned.
608 $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype',
609 'isexternalfile', 'repositorytype'));
610 $returnedkeys = array_flip(array_keys($description->content->keys));
611 $this->assertEquals($expectedkeys, $returnedkeys);
612 $this->assertEquals('List of files.', $description->desc);
613 $this->assertEquals(VALUE_REQUIRED, $description->required);
614 foreach ($description->content->keys as $key) {
615 $this->assertEquals(VALUE_OPTIONAL, $key->required);
621 * Test default time for user created tokens.
623 public function test_user_created_tokens_duration() {
624 global $CFG, $DB;
625 $this->resetAfterTest(true);
627 $CFG->enablewebservices = 1;
628 $CFG->enablemobilewebservice = 1;
629 $user1 = $this->getDataGenerator()->create_user();
630 $user2 = $this->getDataGenerator()->create_user();
631 $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE, 'enabled' => 1));
633 $this->setUser($user1);
634 $timenow = time();
635 $token = external_generate_token_for_current_user($service);
636 $this->assertGreaterThanOrEqual($timenow + $CFG->tokenduration, $token->validuntil);
638 // Change token default time.
639 $this->setUser($user2);
640 set_config('tokenduration', DAYSECS);
641 $token = external_generate_token_for_current_user($service);
642 $timenow = time();
643 $this->assertLessThanOrEqual($timenow + DAYSECS, $token->validuntil);
648 * Just a wrapper to access protected apis for testing
650 class test_exernal_api extends external_api {
652 public static function get_context_wrapper($params) {
653 return self::get_context_from_params($params);