config = $this->dice->create(IManageConfigValues::class);
$this->config->set('system', 'url', 'http://localhost');
$this->config->set('system', 'hostname', 'localhost');
$this->config->set('system', 'worker_dont_fork', true);
// Default config
$this->config->set('config', 'hostname', 'localhost');
$this->config->set('system', 'throttle_limit_day', 100);
$this->config->set('system', 'throttle_limit_week', 100);
$this->config->set('system', 'throttle_limit_month', 100);
$this->config->set('system', 'theme', 'system_theme');
/** @var App app */
$this->app = DI::app();
DI::args()->setArgc(1);
// User data that the test database is populated with
$this->selfUser = [
'id' => 42,
'name' => 'Self contact',
'nick' => 'selfcontact',
'nurl' => 'http://localhost/profile/selfcontact'
];
$this->friendUser = [
'id' => 44,
'name' => 'Friend contact',
'nick' => 'friendcontact',
'nurl' => 'http://localhost/profile/friendcontact'
];
$this->otherUser = [
'id' => 43,
'name' => 'othercontact',
'nick' => 'othercontact',
'nurl' => 'http://localhost/profile/othercontact'
];
// User ID that we know is not in the database
$this->wrongUserId = 666;
DI::session()->start();
// Most API require login so we force the session
$_SESSION = [
'authenticated' => true,
'uid' => $this->selfUser['id']
];
BasicAuth::setCurrentUserID($this->selfUser['id']);
}
/**
* Assert that a list array contains expected keys.
*
* @param array $list List array
*
* @return void
*/
private function assertList(array $list = [])
{
self::assertIsString($list['name']);
self::assertIsInt($list['id']);
self::assertIsString('string', $list['id_str']);
self::assertContains($list['mode'], ['public', 'private']);
// We could probably do more checks here.
}
/**
* Assert that the string is XML and contain the root element.
*
* @param string $result XML string
* @param string $root_element Root element name
*
* @return void
*/
private function assertXml($result = '', $root_element = '')
{
self::assertStringStartsWith('', $result);
self::assertStringContainsString('<' . $root_element, $result);
// We could probably do more checks here.
}
/**
* Test the api_user() function.
*
* @return void
*/
public function testApiUser()
{
self::assertEquals($this->selfUser['id'], BaseApi::getCurrentUserID());
}
/**
* Test the api_user() function with an unallowed user.
*
* @return void
*/
public function testApiUserWithUnallowedUser()
{
// self::assertEquals(false, api_user());
}
/**
* Test the api_source() function.
*
* @return void
*/
public function testApiSource()
{
self::assertEquals('api', BasicAuth::getCurrentApplicationToken()['name']);
}
/**
* Test the api_source() function with a Twidere user agent.
*
* @return void
*/
public function testApiSourceWithTwidere()
{
$_SERVER['HTTP_USER_AGENT'] = 'Twidere';
self::assertEquals('Twidere', BasicAuth::getCurrentApplicationToken()['name']);
}
/**
* Test the api_source() function with a GET parameter.
*
* @return void
*/
public function testApiSourceWithGet()
{
$_REQUEST['source'] = 'source_name';
self::assertEquals('source_name', BasicAuth::getCurrentApplicationToken()['name']);
}
/**
* Test the api_date() function.
*
* @return void
*/
public function testApiDate()
{
self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
}
/**
* Test the api_register_func() function.
*
* @return void
*/
public function testApiRegisterFunc()
{
global $API;
self::assertNull(
api_register_func(
'api_path',
function () {
},
true,
'method'
)
);
self::assertTrue(is_callable($API['api_path']['func']));
}
/**
* Test the BasicAuth::getCurrentUserID() function without any login.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @preserveGlobalState disabled
*/
public function testApiLoginWithoutLogin()
{
BasicAuth::setCurrentUserID();
$this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
BasicAuth::getCurrentUserID(true);
}
/**
* Test the BasicAuth::getCurrentUserID() function with a bad login.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @preserveGlobalState disabled
*/
public function testApiLoginWithBadLogin()
{
BasicAuth::setCurrentUserID();
$this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
$_SERVER['PHP_AUTH_USER'] = 'user@server';
BasicAuth::getCurrentUserID(true);
}
/**
* Test the BasicAuth::getCurrentUserID() function with oAuth.
*
* @return void
*/
public function testApiLoginWithOauth()
{
$this->markTestIncomplete('Can we test this easily?');
}
/**
* Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
*
* @return void
*/
public function testApiLoginWithAddonAuth()
{
$this->markTestIncomplete('Can we test this easily?');
}
/**
* Test the BasicAuth::getCurrentUserID() function with a correct login.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @doesNotPerformAssertions
*/
public function testApiLoginWithCorrectLogin()
{
BasicAuth::setCurrentUserID();
$_SERVER['PHP_AUTH_USER'] = 'Test user';
$_SERVER['PHP_AUTH_PW'] = 'password';
BasicAuth::getCurrentUserID(true);
}
/**
* Test the BasicAuth::getCurrentUserID() function with a remote user.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiLoginWithRemoteUser()
{
BasicAuth::setCurrentUserID();
$this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
$_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
BasicAuth::getCurrentUserID(true);
}
/**
* Test the api_call() function.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCall()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return ['data' => ['some_data']];
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path';
$_GET['callback'] = 'callback_name';
self::assertEquals(
'callback_name(["some_data"])',
api_call('api_path', 'json')
);
}
/**
* Test the api_call() function with the profiled enabled.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCallWithProfiler()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return ['data' => ['some_data']];
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path';
$this->config->set('system', 'profiler', true);
$this->config->set('rendertime', 'callstack', true);
$this->app->callstack = [
'database' => ['some_function' => 200],
'database_write' => ['some_function' => 200],
'cache' => ['some_function' => 200],
'cache_write' => ['some_function' => 200],
'network' => ['some_function' => 200]
];
self::assertEquals(
'["some_data"]',
api_call('api_path', 'json')
);
}
/**
* Test the api_call() function with a JSON result.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCallWithJson()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return ['data' => ['some_data']];
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
self::assertEquals(
'["some_data"]',
api_call('api_path.json', 'json')
);
}
/**
* Test the api_call() function with an XML result.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCallWithXml()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return 'some_data';
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
$args = DI::args()->determine($_SERVER, $_GET);
self::assertEquals(
'some_data',
api_call('api_path.xml', 'xml')
);
}
/**
* Test the api_call() function with an RSS result.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCallWithRss()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return 'some_data';
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
self::assertEquals(
'' . "\n" .
'some_data',
api_call('api_path.rss', 'rss')
);
}
/**
* Test the api_call() function with an Atom result.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiCallWithAtom()
{
global $API;
$API['api_path'] = [
'method' => 'method',
'func' => function () {
return 'some_data';
}
];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
self::assertEquals(
'' . "\n" .
'some_data',
api_call('api_path.atom', 'atom')
);
}
/**
* Test the api_rss_extra() function.
*
* @return void
*/
public function testApiRssExtra()
{
/*
$user_info = ['url' => 'user_url', 'lang' => 'en'];
$result = api_rss_extra([], $user_info);
self::assertEquals($user_info, $result['$user']);
self::assertEquals($user_info['url'], $result['$rss']['alternate']);
self::assertArrayHasKey('self', $result['$rss']);
self::assertArrayHasKey('base', $result['$rss']);
self::assertArrayHasKey('updated', $result['$rss']);
self::assertArrayHasKey('atom_updated', $result['$rss']);
self::assertArrayHasKey('language', $result['$rss']);
self::assertArrayHasKey('logo', $result['$rss']);
*/
}
/**
* Test the api_rss_extra() function without any user info.
*
* @return void
*/
public function testApiRssExtraWithoutUserInfo()
{
/*
$result = api_rss_extra([], null);
self::assertIsArray($result['$user']);
self::assertArrayHasKey('alternate', $result['$rss']);
self::assertArrayHasKey('self', $result['$rss']);
self::assertArrayHasKey('base', $result['$rss']);
self::assertArrayHasKey('updated', $result['$rss']);
self::assertArrayHasKey('atom_updated', $result['$rss']);
self::assertArrayHasKey('language', $result['$rss']);
self::assertArrayHasKey('logo', $result['$rss']);
*/
}
/**
* Test the api_get_user() function.
*
* @return void
*/
public function testApiGetUser()
{
// $user = api_get_user();
// self::assertSelfUser($user);
// self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
// self::assertEquals('6fdbe8', $user['profile_link_color']);
// self::assertEquals('ededed', $user['profile_background_color']);
}
/**
* Test the api_get_user() function with a Frio schema.
*
* @return void
*/
public function testApiGetUserWithFrioSchema()
{
// $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
// $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
// $user = api_get_user();
// self::assertSelfUser($user);
// self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
// self::assertEquals('6fdbe8', $user['profile_link_color']);
// self::assertEquals('ededed', $user['profile_background_color']);
}
/**
* Test the api_get_user() function with an empty Frio schema.
*
* @return void
*/
public function testApiGetUserWithEmptyFrioSchema()
{
// $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
// $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
// $user = api_get_user();
// self::assertSelfUser($user);
// self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
// self::assertEquals('6fdbe8', $user['profile_link_color']);
// self::assertEquals('ededed', $user['profile_background_color']);
}
/**
* Test the api_get_user() function with a custom Frio schema.
*
* @return void
*/
public function testApiGetUserWithCustomFrioSchema()
{
// $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
// $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
// $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
// $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
// $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
// $user = api_get_user();
// self::assertSelfUser($user);
// self::assertEquals('123456', $user['profile_sidebar_fill_color']);
// self::assertEquals('123456', $user['profile_link_color']);
// self::assertEquals('123456', $user['profile_background_color']);
}
/**
* Test the api_get_user() function with an user that is not allowed to use the API.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testApiGetUserWithoutApiUser()
{
// api_get_user() with empty parameters is not used anymore
/*
$_SERVER['PHP_AUTH_USER'] = 'Test user';
$_SERVER['PHP_AUTH_PW'] = 'password';
BasicAuth::setCurrentUserID();
self::assertFalse(api_get_user());
*/
}
/**
* Test the api_get_user() function with an user ID in a GET parameter.
*
* @return void
*/
public function testApiGetUserWithGetId()
{
// self::assertOtherUser(api_get_user());
}
/**
* Test the api_get_user() function with a wrong user ID in a GET parameter.
*
* @return void
*/
public function testApiGetUserWithWrongGetId()
{
// $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
// self::assertOtherUser(api_get_user());
}
/**
* Test the api_get_user() function with an user name in a GET parameter.
*
* @return void
*/
public function testApiGetUserWithGetName()
{
// self::assertSelfUser(api_get_user());
}
/**
* Test the api_get_user() function with a profile URL in a GET parameter.
*
* @return void
*/
public function testApiGetUserWithGetUrl()
{
// self::assertSelfUser(api_get_user());
}
/**
* Test the api_get_user() function with an user ID in the API path.
*
* @return void
*/
public function testApiGetUserWithNumericCalledApi()
{
// global $called_api;
// $called_api = ['api_path'];
// DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
// self::assertOtherUser(api_get_user());
}
/**
* Test the api_get_user() function with the $called_api global variable.
*
* @return void
*/
public function testApiGetUserWithCalledApi()
{
// global $called_api;
// $called_api = ['api', 'api_path'];
// self::assertSelfUser(api_get_user());
}
/**
* Test the Arrays::walkRecursive() function.
*
* @return void
*/
public function testApiWalkRecursive()
{
$array = ['item1'];
self::assertEquals(
$array,
Arrays::walkRecursive(
$array,
function () {
// Should we test this with a callback that actually does something?
return true;
}
)
);
}
/**
* Test the Arrays::walkRecursive() function with an array.
*
* @return void
*/
public function testApiWalkRecursiveWithArray()
{
$array = [['item1'], ['item2']];
self::assertEquals(
$array,
Arrays::walkRecursive(
$array,
function () {
// Should we test this with a callback that actually does something?
return true;
}
)
);
}
/**
* Test the BaseApi::reformatXML() function.
*
* @return void
*/
public function testApiReformatXml()
{
$item = true;
$key = '';
self::assertTrue(ApiResponse::reformatXML($item, $key));
self::assertEquals('true', $item);
}
/**
* Test the BaseApi::reformatXML() function with a statusnet_api key.
*
* @return void
*/
public function testApiReformatXmlWithStatusnetKey()
{
$item = '';
$key = 'statusnet_api';
self::assertTrue(ApiResponse::reformatXML($item, $key));
self::assertEquals('statusnet:api', $key);
}
/**
* Test the BaseApi::reformatXML() function with a friendica_api key.
*
* @return void
*/
public function testApiReformatXmlWithFriendicaKey()
{
$item = '';
$key = 'friendica_api';
self::assertTrue(ApiResponse::reformatXML($item, $key));
self::assertEquals('friendica:api', $key);
}
/**
* Test the BaseApi::createXML() function.
*
* @return void
*/
public function testApiCreateXml()
{
self::assertEquals(
'' . "\n" .
'