/**
* Validates submitted function parameters, if anything is incorrect
* invalid_parameter_exception is thrown.
+ * This is a simple recursive method which is intended to be called from
+ * each implementation method of external API.
* @param external_description $description description of parameters
* @param mixed $params the actual parameters
* @return mixed params with added defaults for optional items, invalid_parameters_exception thrown if any problem found
require_once($CFG->libdir . '/externallib.php');
class externallib_test extends UnitTestCase {
- public function test_validate_params1() {
+ public function test_validate_params() {
$params = array('text'=>'aaa', 'someid'=>'6',);
$description = new external_function_parameters(array('someid' => new external_param(PARAM_INT, 'Some int value'),
'text' => new external_param(PARAM_ALPHA, 'Some text value')));
$this->assertTrue(key($result) === 'someids');
$this->assertTrue($result['someids'] == array(0=>1, 1=>2, 2=>3));
$this->assertTrue($result['scalar'] === '666');
- }
- //TODO: add unittests for all description options and validation failures
+
+ $params = array('text'=>'aaa');
+ $description = new external_function_parameters(array('someid' => new external_param(PARAM_INT, 'Some int value', false),
+ 'text' => new external_param(PARAM_ALPHA, 'Some text value')));
+ $result = external_api::validate_parameters($description, $params);
+ $this->assertEqual(count($result), 2);
+ reset($result);
+ $this->assertTrue(key($result) === 'someid');
+ $this->assertTrue($result['someid'] === null);
+ $this->assertTrue($result['text'] === 'aaa');
+
+
+ $params = array('text'=>'aaa');
+ $description = new external_function_parameters(array('someid' => new external_param(PARAM_INT, 'Some int value', false, 6),
+ 'text' => new external_param(PARAM_ALPHA, 'Some text value')));
+ $result = external_api::validate_parameters($description, $params);
+ $this->assertEqual(count($result), 2);
+ reset($result);
+ $this->assertTrue(key($result) === 'someid');
+ $this->assertTrue($result['someid'] === 6);
+ $this->assertTrue($result['text'] === 'aaa');
+ }
}