123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- /**
- * Elgg Test Web Services - General Web Service API
- *
- * @package Elgg
- * @subpackage Test
- */
- class ElggCoreWebServicesApiTest extends ElggCoreUnitTest {
- /**
- * Called after each test method.
- */
- public function tearDown() {
- global $API_METHODS;
- $API_METHODS = array();
- }
-
- // elgg_ws_expose_function
- public function testExposeFunctionNoMethod() {
- try {
- @elgg_ws_expose_function();
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'InvalidParameterException');
- $this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
- }
- }
-
- public function testExposeFunctionNoFunction() {
- try {
- @elgg_ws_expose_function('test');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'InvalidParameterException');
- $this->assertIdentical($e->getMessage(), elgg_echo('InvalidParameterException:APIMethodOrFunctionNotSet'));
- }
- }
-
- public function testExposeFunctionBadParameters() {
- try {
- @elgg_ws_expose_function('test', 'test', 'BAD');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'InvalidParameterException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
- }
- }
-
- public function testExposeFunctionParametersBadArray() {
- try {
- elgg_ws_expose_function('test', 'test', array('param1' => 'string'));
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'InvalidParameterException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), 'test'));
- }
- }
-
- public function testExposeFunctionBadHttpMethod() {
- try {
- @elgg_ws_expose_function('test', 'test', null, '', 'BAD');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'InvalidParameterException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('InvalidParameterException:UnrecognisedHttpMethod'), 'BAD', 'test'));
- }
- }
-
- public function testExposeFunctionSuccess() {
- global $API_METHODS;
- // this is a general test but also tests specifically for setting 'required' correctly
- $parameters = array('param1' => array('type' => 'int', 'required' => true),
- 'param2' => array('type' => 'bool'),
- 'param3' => array('type' => 'string', 'required' => false), );
-
- $this->assertTrue(elgg_ws_expose_function('test', 'foo', $parameters));
-
- $parameters = array('param1' => array('type' => 'int', 'required' => true),
- 'param2' => array('type' => 'bool', 'required' => true),
- 'param3' => array('type' => 'string', 'required' => false), );
- $method['description'] = '';
- $method['function'] = 'foo';
- $method['parameters'] = $parameters;
- $method['call_method'] = 'GET';
- $method['require_api_auth'] = false;
- $method['require_user_auth'] = false;
- $this->assertIdentical($method, $API_METHODS['test']);
- }
- // elgg_ws_unexpose_function
- public function testUnexposeFunction() {
- global $API_METHODS;
-
- $this->registerFunction();
-
- elgg_ws_unexpose_function('test');
- $this->assertIdentical(array(), $API_METHODS);
- }
- // authenticate_method
- public function testAuthenticateMethodNotImplemented() {
- try {
- authenticate_method('BAD');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
- }
- }
-
- public function testAuthenticateMethodApiAuth() {
- $this->registerFunction(true);
- try {
- authenticate_method('test');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), elgg_echo('APIException:APIAuthenticationFailed'));
- }
- }
-
- public function testAuthenticateMethodUserAuth() {
- $this->registerFunction(false, true);
- try {
- authenticate_method('test');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- }
- }
-
- public function testAuthenticateMethod() {
- $this->registerFunction(false, false);
- // anonymous with no user authentication
- $this->assertTrue(authenticate_method('test'));
- }
-
- // execute_method
- public function testExecuteMethodNotImplemented() {
- try {
- execute_method('BAD');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MethodCallNotImplemented'), 'BAD'));
- }
- }
- public function testExecuteMethodNonCallable() {
- elgg_ws_expose_function('test', 'foo');
-
- try {
- execute_method('test');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:FunctionDoesNotExist'), 'test'));
- }
- }
- public function testExecuteMethodWrongMethod() {
- $this->registerFunction();
-
- try {
- // GET when it should be a POST
- execute_method('test');
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'CallException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('CallException:InvalidCallMethod'), 'test', 'POST'));
- }
- }
- // verify parameters
- public function testVerifyParametersTypeNotSet() {
- $params = array('param1' => array('required' => true));
- elgg_ws_expose_function('test', 'elgg_echo', $params);
-
- try {
- verify_parameters('test', array());
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:InvalidParameter'), 'param1', 'test'));
- }
- }
-
- public function testVerifyParametersMissing() {
- $params = array('param1' => array('type' => 'int', 'required' => true));
- elgg_ws_expose_function('test', 'elgg_echo', $params);
-
- try {
- verify_parameters('test', array());
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), sprintf(elgg_echo('APIException:MissingParameterInMethod'), 'param1', 'test'));
- }
- }
-
- public function testVerifyParameters() {
- $this->registerFunction();
-
- $parameters = array('param1' => 0);
- $this->assertTrue(verify_parameters('test', $parameters));
- }
-
- public function testSerialiseParameters() {
-
- // int and bool
- $this->registerFunction();
- $parameters = array('param1' => 1, 'param2' => 0);
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ',1,false');
-
- // string
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => 'testing');
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",'testing'");
- // test string with " in it
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => 'test"ing');
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ',\'test"ing\'');
-
- // test string with ' in it
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => 'test\'ing');
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",'test\'ing'");
-
- // test string with \ in it
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => 'test\ing');
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",'test\\ing'");
-
- // test string with \' in it
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => "test\'ing");
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",'test\\\\'ing'"); // test\\'ing
-
- // test string reported by twall in #1364
- $this->registerFunction(false, false, array('param1' => array('type' => 'string')));
- $parameters = array('param1' => '{"html":"<div><img src=\\"http://foo.com\\"/>Blah Blah</div>"}');
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",'{\"html\":\"<div><img src=\\\"http://foo.com\\\"/>Blah Blah</div>\"}'");
-
- // float
- $this->registerFunction(false, false, array('param1' => array('type' => 'float')));
- $parameters = array('param1' => 2.5);
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ',2.5');
- // indexed array of strings
- $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
- $parameters = array('param1' => array('one', 'two'));
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",array('0'=>'one','1'=>'two')");
- // associative array of strings
- $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
- $parameters = array('param1' => array('first' => 'one', 'second' => 'two'));
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",array('first'=>'one','second'=>'two')");
- // indexed array of strings
- $this->registerFunction(false, false, array('param1' => array('type' => 'array')));
- $parameters = array('param1' => array(1, 2));
- $s = serialise_parameters('test', $parameters);
- $this->assertIdentical($s, ",array('0'=>'1','1'=>'2')");
- // test unknown type
- $this->registerFunction(false, false, array('param1' => array('type' => 'bad')));
- $parameters = array('param1' => 'test');
- $this->expectException('APIException');
- $s = serialise_parameters('test', $parameters);
- }
-
- // api key methods
- //public function testApiAuthenticate() {
- // $this->assertFalse(pam_authenticate(null, "api"));
- //}
-
- public function testApiAuthKeyNoKey() {
- try {
- api_auth_key();
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), elgg_echo('APIException:MissingAPIKey'));
- }
- }
- public function testApiAuthKeyBadKey() {
- global $CONFIG;
-
- set_input('api_key', 'BAD');
- try {
- api_auth_key();
- $this->assertTrue(FALSE);
- } catch (Exception $e) {
- $this->assertIsA($e, 'APIException');
- $this->assertIdentical($e->getMessage(), elgg_echo('APIException:BadAPIKey'));
- }
- }
-
- protected function registerFunction($api_auth = false, $user_auth = false, $params = null) {
- $parameters = array('param1' => array('type' => 'int', 'required' => true),
- 'param2' => array('type' => 'bool', 'required' => false), );
-
- if ($params == null) {
- $params = $parameters;
- }
- elgg_ws_expose_function('test', 'elgg_echo', $params, '', 'POST', $api_auth, $user_auth);
- }
-
- }
|