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');
79 * Test for clean_returnvalue().
81 public function test_clean_returnvalue() {
83 // Build some return value decription.
84 $returndesc = new external_multiple_structure(
85 new external_single_structure(
87 'object' => new external_single_structure(
88 array('value1' => new external_value(PARAM_INT
, 'this is a int'))),
89 'value2' => new external_value(PARAM_TEXT
, 'some text', VALUE_OPTIONAL
))
92 // Clean an object (it should be cast into an array).
93 $object = new stdClass();
95 $singlestructure['object'] = $object;
96 $singlestructure['value2'] = 'Some text';
97 $testdata = array($singlestructure);
98 $cleanedvalue = external_api
::clean_returnvalue($returndesc, $testdata);
99 $cleanedsinglestructure = array_pop($cleanedvalue);
100 $this->assertEquals($object->value1
, $cleanedsinglestructure['object']['value1']);
101 $this->assertEquals($singlestructure['value2'], $cleanedsinglestructure['value2']);
103 // Missing VALUE_OPTIONAL.
104 $object = new stdClass();
106 $singlestructure = new stdClass();
107 $singlestructure->object = $object;
108 $testdata = array($singlestructure);
109 $cleanedvalue = external_api
::clean_returnvalue($returndesc, $testdata);
110 $cleanedsinglestructure = array_pop($cleanedvalue);
111 $this->assertEquals($object->value1
, $cleanedsinglestructure['object']['value1']);
112 $this->assertEquals(false, array_key_exists('value2', $cleanedsinglestructure));
114 // Unknown attribut (the value should be ignored).
116 $object['value1'] = 1;
117 $singlestructure = array();
118 $singlestructure['object'] = $object;
119 $singlestructure['value2'] = 'Some text';
120 $singlestructure['unknownvalue'] = 'Some text to ignore';
121 $testdata = array($singlestructure);
122 $cleanedvalue = external_api
::clean_returnvalue($returndesc, $testdata);
123 $cleanedsinglestructure = array_pop($cleanedvalue);
124 $this->assertEquals($object['value1'], $cleanedsinglestructure['object']['value1']);
125 $this->assertEquals($singlestructure['value2'], $cleanedsinglestructure['value2']);
126 $this->assertEquals(false, array_key_exists('unknownvalue', $cleanedsinglestructure));
129 // Missing required value (an exception is thrown).
131 $singlestructure = array();
132 $singlestructure['object'] = $object;
133 $singlestructure['value2'] = 'Some text';
134 $testdata = array($singlestructure);
135 $this->setExpectedException('invalid_response_exception');
136 $cleanedvalue = external_api
::clean_returnvalue($returndesc, $testdata);