]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
more unit tests for REST api
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 22 Oct 2009 12:01:41 +0000 (12:01 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 22 Oct 2009 12:01:41 +0000 (12:01 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3569 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/api.php
engine/tests/services/api.php
languages/en.php

index f2424c76644c189afebe430efec7783d9c7dbcf5..63826cf74ce912543d704beb60f0c5b772d547ec 100644 (file)
@@ -335,8 +335,16 @@ function expose_function($method, $function, array $parameters = NULL, $descript
        // does not check whether callable - done in execute_method()
        $API_METHODS[$method]["function"] = $function;
 
-       if ($parameters != NULL && !is_array($parameters)) {
-               throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:APIParametersNotArray'), $method));    
+       if ($parameters != NULL) {      
+               if (!is_array($parameters)) {
+                       throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), $method));
+               }
+               
+               // catch common mistake of not setting up param array correctly
+               $first = current($parameters);
+               if (!is_array($first)) {
+                       throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), $method));
+               }
        }
        
        if ($parameters != NULL) {
@@ -668,10 +676,13 @@ function serialise_parameters($method, $parameters) {
                        case 'boolean': 
                                // change word false to boolean false
                                if (strcasecmp(trim($parameters[$key]), "false") == 0) { 
-                                       $parameters[$key] = false;
+                                       $serialised_parameters .= ',false';
+                               } else if ($parameters[$key] == 0) {
+                                       $serialised_parameters .= ',false';
+                               } else {
+                                       $serialised_parameters .= ',true';
                                }
                                
-                               $serialised_parameters .= "," . (bool)trim($parameters[$key]); 
                                break;
                        case 'string': 
                                $serialised_parameters .= ",'" .  (string)mysql_real_escape_string(trim($parameters[$key])) . "'"; 
@@ -681,17 +692,18 @@ function serialise_parameters($method, $parameters) {
                                break;
                        case 'array':
                                // we can handle an array of strings, maybe ints, definitely not booleans or other arrays                                                                               
-                               $array = "array(";
                                if (!is_array($parameters[$key]))
                                {
                                        throw APIException(sprintf(elgg_echo('APIException:ParameterNotArray'), $key));
                                }
-                                                       
+                                                                       
+                               $array = "array(";
+                               
                                foreach ($parameters[$key] as $k => $v)
                                {
                                        $k = sanitise_string($k);
                                        $v = sanitise_string($v);
-                                                               
+                                                                       
                                        $array .= "'$k'=>'$v',";
                                }
                                                        
index 4c30195385a7099acaa13d82ba85e29c9e8eed0b..cad28a45240fd6448ea4463f3e3e61bbc9e8e1c3 100644 (file)
@@ -20,6 +20,7 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
        \r
 // expose_function\r
        public function testExposeFunctionNoMethod() {\r
+               \r
                $this->expectException('InvalidParameterException');\r
                expose_function();\r
        }\r
@@ -34,6 +35,11 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
                expose_function('test', 'test', 'BAD');\r
        }\r
        \r
+       public function testExposeFunctionParametersNotArray() {\r
+               $this->expectException('InvalidParameterException');\r
+               expose_function('test', 'test', array('param1' => 'string'));\r
+       }\r
+       \r
        public function testExposeFunctionBadHttpMethod() {\r
                $this->expectException('InvalidParameterException');\r
                expose_function('test', 'test', null, '', 'BAD');\r
@@ -101,9 +107,17 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
        \r
 // execute_method\r
        public function testExecuteMethodNonCallable() {\r
+               expose_function('test', 'foo');\r
+               \r
+               $this->expectException('ApiException');\r
+               execute_method('test');\r
+       }\r
+\r
+       public function testExecuteMethodWrongMethod() {\r
                $this->registerFunction();\r
                \r
-               $this->expectException('APIException');\r
+               // get when it should be a post\r
+               $this->expectException('CallException');\r
                execute_method('test');\r
        }\r
        \r
@@ -120,13 +134,58 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest {
        \r
        public function testserialise_parameters() {\r
                \r
+               // int and bool\r
+               $this->registerFunction();\r
+               $parameters = array('param1' => 1, 'param2' => 0);\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, ',1,false');\r
+               \r
+               // string\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'string')));\r
+               $parameters = array('param1' => 'testing');\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, ",'testing'");\r
+\r
+               // float\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'float')));\r
+               $parameters = array('param1' => 2.5);\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, ',2.5');\r
+\r
+               // indexed array of strings\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'array')));\r
+               $parameters = array('param1' => array('one', 'two'));\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, "array('0'=>'one','1'=>'two')");\r
+\r
+               // associative array of strings\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'array')));\r
+               $parameters = array('param1' => array('first' => 'one', 'second' => 'two'));\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, "array('first'=>'one','second'=>'two')");\r
+\r
+               // indexed array of strings\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'array')));\r
+               $parameters = array('param1' => array(1, 2));\r
+               $s = serialise_parameters('test', $parameters);\r
+               $this->assertIdentical($s, "array('0'=>'1','1'=>'2')");\r
+\r
+               // test unknown type\r
+               $this->registerFunction(false, false, array('param1' => array('type' => 'bad')));\r
+               $parameters = array('param1' => 'test');\r
+               $this->expectException('APIException');\r
+               $s = serialise_parameters('test', $parameters);\r
        }\r
        \r
-       protected function registerFunction($api_auth = false, $user_auth = false) {\r
+       protected function registerFunction($api_auth = false, $user_auth = false, $params = null) {\r
                $parameters = array('param1' => array('type' => 'int', 'required' => true),\r
                                                        'param2' => array('type' => 'bool', 'required' => false), );\r
+               \r
+               if ($params == null) {\r
+                       $params = $parameters;\r
+               }\r
 \r
-               expose_function('test', 'foo', $parameters, '', 'GET', $api_auth, $user_auth);\r
+               expose_function('test', 'elgg_echo', $params, '', 'POST', $api_auth, $user_auth);\r
        }\r
        \r
 }\r
index 6a692e55b2eb723ec3bd35cfba61d3980650c730..78310b9f7131ebc504d1822a6336830ce25e0db2 100644 (file)
@@ -121,7 +121,7 @@ $english = array(
        'SecurityException:APIAccessDenied' => "Sorry, API access has been disabled by the administrator.",
        'SecurityException:NoAuthMethods' => "No authentication methods were found that could authenticate this API request.",
        'InvalidParameterException:APIMethodOrFunctionNotSet' => "Method or function not set in call in expose_method()",
-       'InvalidParameterException:APIParametersNotArray' => "Parameters must be array in call to expose method '%s'",
+       'InvalidParameterException:APIParametersArrayStructure' => "Parameters array structure is incorrect for call to expose method '%s'",
        'InvalidParameterException:UnrecognisedHttpMethod' => "Unrecognised http method %s for api method '%s'",
        'APIException:MissingParameterInMethod' => "Missing parameter %s in method %s",
        'APIException:ParameterNotArray' => "%s does not appear to be an array.",