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 externallib_testcase
extends basic_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->assertEquals(count($result), 2);
40 $this->assertTrue(key($result) === 'someid');
41 $this->assertTrue($result['someid'] === 6);
42 $this->assertTrue($result['text'] === 'aaa');
45 $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666);
46 $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Some ID')),
47 'scalar' => new external_value(PARAM_ALPHANUM
, 'Some text value')));
48 $result = external_api
::validate_parameters($description, $params);
49 $this->assertEquals(count($result), 2);
51 $this->assertTrue(key($result) === 'someids');
52 $this->assertTrue($result['someids'] == array(0=>1, 1=>2, 2=>3));
53 $this->assertTrue($result['scalar'] === '666');
56 $params = array('text'=>'aaa');
57 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT
, 'Some int value', false),
58 'text' => new external_value(PARAM_ALPHA
, 'Some text value')));
59 $result = external_api
::validate_parameters($description, $params);
60 $this->assertEquals(count($result), 2);
62 $this->assertTrue(key($result) === 'someid');
63 $this->assertTrue($result['someid'] === null);
64 $this->assertTrue($result['text'] === 'aaa');
67 $params = array('text'=>'aaa');
68 $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT
, 'Some int value', false, 6),
69 'text' => new external_value(PARAM_ALPHA
, 'Some text value')));
70 $result = external_api
::validate_parameters($description, $params);
71 $this->assertEquals(count($result), 2);
73 $this->assertTrue(key($result) === 'someid');
74 $this->assertTrue($result['someid'] === 6);
75 $this->assertTrue($result['text'] === 'aaa');