From 1d7db36f28433eecd1c29200a60deaa9213cd86b Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 6 Oct 2009 19:49:07 +0000 Subject: [PATCH] MDL-12886 more unit tests --- lib/externallib.php | 2 ++ lib/simpletest/testexternallib.php | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/externallib.php b/lib/externallib.php index d2ec267610..e2595bad21 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -55,6 +55,8 @@ class external_api { /** * 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 diff --git a/lib/simpletest/testexternallib.php b/lib/simpletest/testexternallib.php index fb98102822..6c271ce9d6 100644 --- a/lib/simpletest/testexternallib.php +++ b/lib/simpletest/testexternallib.php @@ -30,7 +30,7 @@ if (!defined('MOODLE_INTERNAL')) { 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'))); @@ -51,7 +51,27 @@ class externallib_test extends UnitTestCase { $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'); + } } -- 2.39.5