MDL-12886 more unit tests

This commit is contained in:
skodak
2009-10-06 19:49:07 +00:00
parent 3a465d1dc9
commit 1d7db36f28
2 changed files with 25 additions and 3 deletions
+2
View File
@@ -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
+23 -3
View File
@@ -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');
}
}