]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12886 more unit tests
authorskodak <skodak>
Tue, 6 Oct 2009 19:49:07 +0000 (19:49 +0000)
committerskodak <skodak>
Tue, 6 Oct 2009 19:49:07 +0000 (19:49 +0000)
lib/externallib.php
lib/simpletest/testexternallib.php

index d2ec2676105eb4aa98db3db18df3c1ad240c7370..e2595bad211d0652830330de1751088c517e6aac 100644 (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
index fb98102822eb5a0771739f3147d969550a3b3305..6c271ce9d6a980c4826da75c04636e5151d7e5d7 100644 (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');
+    }
 }