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/>.
18 * Unit tests for /lib/externallib.php.
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();
29 require_once($CFG->libdir
. '/externallib.php');
32 class core_externallib_testcase
extends advanced_testcase
{
35 public function setUp() {
39 public function tearDown() {
41 if ($this->DB
!== null) {
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);
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);
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);
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);
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);
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();
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);
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.
148 $testformat = FORMAT_MARKDOWN
;
149 $correct = array('<p>$$ \pi $$</p>
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>!';
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(
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);
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(
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();
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();
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).
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).
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
);
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.
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);
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);
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() {
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() {
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() {
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() {
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');
430 foreach ($functions as $f) {
431 $return[$f->name
] = array($f);
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
);
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, but still return courses even if they fail validation.
487 public function test_validate_courses_keepfails() {
488 $this->resetAfterTest(true);
490 $c1 = $this->getDataGenerator()->create_course();
491 $c2 = $this->getDataGenerator()->create_course();
492 $c3 = $this->getDataGenerator()->create_course();
493 $u1 = $this->getDataGenerator()->create_user();
494 $this->getDataGenerator()->enrol_user($u1->id
, $c1->id
);
495 $courseids = array($c1->id
, $c2->id
, $c3->id
);
498 list($courses, $warnings) = external_util
::validate_courses($courseids, [], false, true);
499 $this->assertCount(2, $warnings);
500 $this->assertEquals($c2->id
, $warnings[0]['itemid']);
501 $this->assertEquals($c3->id
, $warnings[1]['itemid']);
502 $this->assertCount(3, $courses);
503 $this->assertTrue($courses[$c1->id
]->contextvalidated
);
504 $this->assertFalse($courses[$c2->id
]->contextvalidated
);
505 $this->assertFalse($courses[$c3->id
]->contextvalidated
);
509 * Validate courses can re-use an array of prefetched courses.
511 public function test_validate_courses_prefetch() {
512 $this->resetAfterTest(true);
514 $c1 = $this->getDataGenerator()->create_course();
515 $c2 = $this->getDataGenerator()->create_course();
516 $c3 = $this->getDataGenerator()->create_course();
517 $c4 = $this->getDataGenerator()->create_course();
518 $u1 = $this->getDataGenerator()->create_user();
519 $this->getDataGenerator()->enrol_user($u1->id
, $c1->id
);
520 $this->getDataGenerator()->enrol_user($u1->id
, $c2->id
);
522 $courseids = array($c1->id
, $c2->id
, $c3->id
);
523 $courses = array($c2->id
=> $c2, $c3->id
=> $c3, $c4->id
=> $c4);
526 list($courses, $warnings) = external_util
::validate_courses($courseids, $courses);
527 $this->assertCount(2, $courses);
528 $this->assertCount(1, $warnings);
529 $this->assertArrayHasKey($c1->id
, $courses);
530 $this->assertSame($c2, $courses[$c2->id
]);
531 $this->assertArrayNotHasKey($c3->id
, $courses);
532 // The extra course passed is not returned.
533 $this->assertArrayNotHasKey($c4->id
, $courses);
537 public function test_call_external_function() {
538 global $PAGE, $COURSE, $CFG;
540 $this->resetAfterTest(true);
542 // Call some webservice functions and verify they are correctly handling $PAGE and $COURSE.
543 // First test a function that calls validate_context outside a course.
544 $this->setAdminUser();
545 $category = $this->getDataGenerator()->create_category();
547 'contextid' => context_coursecat
::instance($category->id
)->id
,
548 'name' => 'aaagrrryyy',
552 $cohort1 = $this->getDataGenerator()->create_cohort($params);
553 $cohort2 = $this->getDataGenerator()->create_cohort();
556 $beforecourse = $COURSE;
557 $params = array('cohortids' => array($cohort1->id
, $cohort2->id
));
558 $result = external_api
::call_external_function('core_cohort_get_cohorts', $params);
560 $this->assertSame($beforepage, $PAGE);
561 $this->assertSame($beforecourse, $COURSE);
563 // Now test a function that calls validate_context inside a course.
564 $course = $this->getDataGenerator()->create_course();
567 $beforecourse = $COURSE;
568 $params = array('courseid' => $course->id
, 'options' => array());
569 $result = external_api
::call_external_function('core_enrol_get_enrolled_users', $params);
571 $this->assertSame($beforepage, $PAGE);
572 $this->assertSame($beforecourse, $COURSE);
574 // Test a function that triggers a PHP exception.
575 require_once($CFG->dirroot
. '/lib/tests/fixtures/test_external_function_throwable.php');
577 // Call our test function.
578 $result = test_external_function_throwable
::call_external_function('core_throw_exception', array(), false);
580 $this->assertTrue($result['error']);
581 $this->assertArrayHasKey('exception', $result);
582 $this->assertEquals($result['exception']->message
, 'Exception - Modulo by zero');
586 * Text external_util::get_area_files
588 public function test_external_util_get_area_files() {
592 $DB = $this->getMockBuilder('moodle_database')->getMock();
594 $content = base64_encode("Let us create a nice simple file.");
595 $timemodified = 102030405;
597 $filesize = strlen($content);
599 $DB->method('get_records_sql')->willReturn([
601 'filename' => 'example.txt',
603 'mimetype' => 'text/plain',
604 'filesize' => $filesize,
605 'timemodified' => $timemodified,
607 'pathnamehash' => sha1('/example.txt'),
611 $component = 'mod_foo';
615 $expectedfiles[] = array(
616 'filename' => 'example.txt',
618 'fileurl' => "{$CFG->wwwroot}/webservice/pluginfile.php/{$context}/{$component}/{$filearea}/{$itemid}/example.txt",
619 'timemodified' => $timemodified,
620 'filesize' => $filesize,
621 'mimetype' => 'text/plain',
622 'isexternalfile' => false,
624 // Get all the files for the area.
625 $files = external_util
::get_area_files($context, $component, $filearea, false);
626 $this->assertEquals($expectedfiles, $files);
628 $DB->method('get_in_or_equal')->willReturn([
633 // Get just the file indicated by $itemid.
634 $files = external_util
::get_area_files($context, $component, $filearea, $itemid);
635 $this->assertEquals($expectedfiles, $files);
640 * Text external files structure.
642 public function test_external_files() {
644 $description = new external_files();
646 // First check that the expected default values and keys are returned.
647 $expectedkeys = array_flip(array('filename', 'filepath', 'filesize', 'fileurl', 'timemodified', 'mimetype',
648 'isexternalfile', 'repositorytype'));
649 $returnedkeys = array_flip(array_keys($description->content
->keys
));
650 $this->assertEquals($expectedkeys, $returnedkeys);
651 $this->assertEquals('List of files.', $description->desc
);
652 $this->assertEquals(VALUE_REQUIRED
, $description->required
);
653 foreach ($description->content
->keys
as $key) {
654 $this->assertEquals(VALUE_OPTIONAL
, $key->required
);
660 * Test default time for user created tokens.
662 public function test_user_created_tokens_duration() {
664 $this->resetAfterTest(true);
666 $CFG->enablewebservices
= 1;
667 $CFG->enablemobilewebservice
= 1;
668 $user1 = $this->getDataGenerator()->create_user();
669 $user2 = $this->getDataGenerator()->create_user();
670 $service = $DB->get_record('external_services', array('shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE
, 'enabled' => 1));
672 $this->setUser($user1);
674 $token = external_generate_token_for_current_user($service);
675 $this->assertGreaterThanOrEqual($timenow +
$CFG->tokenduration
, $token->validuntil
);
677 // Change token default time.
678 $this->setUser($user2);
679 set_config('tokenduration', DAYSECS
);
680 $token = external_generate_token_for_current_user($service);
682 $this->assertLessThanOrEqual($timenow + DAYSECS
, $token->validuntil
);
687 * Just a wrapper to access protected apis for testing
689 class test_exernal_api
extends external_api
{
691 public static function get_context_wrapper($params) {
692 return self
::get_context_from_params($params);