diff options
-rw-r--r-- | engine/lib/api.php | 2 | ||||
-rw-r--r-- | engine/tests/services/api.php | 55 | ||||
-rw-r--r-- | languages/en.php | 1 |
3 files changed, 50 insertions, 8 deletions
diff --git a/engine/lib/api.php b/engine/lib/api.php index 493e2a47f..f2424c766 100644 --- a/engine/lib/api.php +++ b/engine/lib/api.php @@ -494,7 +494,7 @@ function execute_method($method) { // function must be callable if (!(isset($API_METHODS[$method]["function"])) || !(is_callable($API_METHODS[$method]["function"]))) { - throw new APIException(sprintf(elgg_echo('APIException:MethodCallNotImplemented'), $method)); + throw new APIException(sprintf(elgg_echo('APIException:FunctionDoesNotExist'), $method)); } // check http call method diff --git a/engine/tests/services/api.php b/engine/tests/services/api.php index cf8a16bf5..4c3019538 100644 --- a/engine/tests/services/api.php +++ b/engine/tests/services/api.php @@ -18,6 +18,7 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest { $API_METHODS = array();
}
+// expose_function
public function testExposeFunctionNoMethod() {
$this->expectException('InvalidParameterException');
expose_function();
@@ -51,7 +52,18 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest { $this->assertTrue(expose_function('test', 'foo', $parameters));
$this->assertIdentical($method, $API_METHODS['test']);
}
+
+// unexpose_function
+ public function testUnexposeFunction() {
+ global $API_METHODS;
+
+ $this->registerFunction();
+ unexpose_function('test');
+ $this->assertIdentical(array(), $API_METHODS);
+ }
+
+// authenticate_method
public function testApiMethodNotImplemented() {
global $CONFIG;
@@ -60,6 +72,41 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest { $this->assertIdentical(sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'bad.method'), $obj->api[0]->message);
}
+ public function testAuthenticateForApi() {
+ $this->registerFunction(true, false);
+
+ $this->expectException('APIException');
+ authenticate_method('test');
+ }
+
+ public function testAuthenticateForUser() {
+ $this->registerFunction(false, true);
+
+ $this->expectException('APIException');
+ authenticate_method('test');
+ }
+
+ public function testAuthenticateMethod() {
+ $this->registerFunction(false, false);
+ // anonymous with no user authentication
+ $this->assertTrue(authenticate_method('test'));
+ }
+
+// api_authenticate
+ public function testApiAuthenticate() {
+ $this->registerFunction(true, false);
+
+ $this->assertFalse(api_authenticate());
+ }
+
+// execute_method
+ public function testExecuteMethodNonCallable() {
+ $this->registerFunction();
+
+ $this->expectException('APIException');
+ execute_method('test');
+ }
+
public function testVerifyParameters() {
$this->registerFunction();
@@ -78,14 +125,8 @@ class ElggCoreServicesApiTest extends ElggCoreUnitTest { protected function registerFunction($api_auth = false, $user_auth = false) {
$parameters = array('param1' => array('type' => 'int', 'required' => true),
'param2' => array('type' => 'bool', 'required' => false), );
- $method['function'] = 'foo';
- $method['parameters'] = $parameters;
- $method['call_method'] = 'GET';
- $method['description'] = '';
- $method['require_api_auth'] = $api_auth;
- $method['require_user_auth'] = $user_auth;
- expose_function('test', 'foo', $parameters);
+ expose_function('test', 'foo', $parameters, '', 'GET', $api_auth, $user_auth);
}
}
diff --git a/languages/en.php b/languages/en.php index 310f5cd09..6a692e55b 100644 --- a/languages/en.php +++ b/languages/en.php @@ -134,6 +134,7 @@ $english = array( 'SecurityException:AuthTokenExpired' => "Authentication token either missing, invalid or expired.", 'CallException:InvalidCallMethod' => "%s must be called using '%s'", 'APIException:MethodCallNotImplemented' => "Method call '%s' has not been implemented.", + 'APIException:FunctionDoesNotExist' => "Function for method '%s' is not callable", 'APIException:AlgorithmNotSupported' => "Algorithm '%s' is not supported or has been disabled.", 'ConfigurationException:CacheDirNotSet' => "Cache directory 'cache_path' not set.", 'APIException:NotGetOrPost' => "Request method must be GET or POST", |