6 namespace Friendica\Test\legacy;
9 use Friendica\Core\Config\Capability\IManageConfigValues;
11 use Friendica\Module\Api\ApiResponse;
12 use Friendica\Module\BaseApi;
13 use Friendica\Security\BasicAuth;
14 use Friendica\Test\FixtureTest;
15 use Friendica\Util\Arrays;
16 use Friendica\Util\DateTimeFormat;
17 use Monolog\Handler\TestHandler;
19 require_once __DIR__ . '/../../include/api.php';
22 * Tests for the API functions.
24 * Functions that use header() need to be tested in a separate process.
25 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
27 * @backupGlobals enabled
29 class ApiTest extends FixtureTest
32 * @var TestHandler Can handle log-outputs
39 protected $friendUser;
43 protected $wrongUserId;
48 /** @var IManageConfigValues */
52 * Create variables used by tests.
54 protected function setUp() : void
56 global $API, $called_api;
62 /** @var IManageConfigValues $config */
63 $this->config = $this->dice->create(IManageConfigValues::class);
65 $this->config->set('system', 'url', 'http://localhost');
66 $this->config->set('system', 'hostname', 'localhost');
67 $this->config->set('system', 'worker_dont_fork', true);
70 $this->config->set('config', 'hostname', 'localhost');
71 $this->config->set('system', 'throttle_limit_day', 100);
72 $this->config->set('system', 'throttle_limit_week', 100);
73 $this->config->set('system', 'throttle_limit_month', 100);
74 $this->config->set('system', 'theme', 'system_theme');
78 $this->app = DI::app();
80 DI::args()->setArgc(1);
82 // User data that the test database is populated with
85 'name' => 'Self contact',
86 'nick' => 'selfcontact',
87 'nurl' => 'http://localhost/profile/selfcontact'
91 'name' => 'Friend contact',
92 'nick' => 'friendcontact',
93 'nurl' => 'http://localhost/profile/friendcontact'
97 'name' => 'othercontact',
98 'nick' => 'othercontact',
99 'nurl' => 'http://localhost/profile/othercontact'
102 // User ID that we know is not in the database
103 $this->wrongUserId = 666;
105 DI::session()->start();
107 // Most API require login so we force the session
109 'authenticated' => true,
110 'uid' => $this->selfUser['id']
112 BasicAuth::setCurrentUserID($this->selfUser['id']);
116 * Assert that a list array contains expected keys.
118 * @param array $list List array
122 private function assertList(array $list = [])
124 self::assertIsString($list['name']);
125 self::assertIsInt($list['id']);
126 self::assertIsString('string', $list['id_str']);
127 self::assertContains($list['mode'], ['public', 'private']);
128 // We could probably do more checks here.
132 * Assert that the string is XML and contain the root element.
134 * @param string $result XML string
135 * @param string $root_element Root element name
139 private function assertXml($result = '', $root_element = '')
141 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
142 self::assertStringContainsString('<' . $root_element, $result);
143 // We could probably do more checks here.
147 * Test the api_user() function.
151 public function testApiUser()
153 self::assertEquals($this->selfUser['id'], BaseApi::getCurrentUserID());
157 * Test the api_user() function with an unallowed user.
161 public function testApiUserWithUnallowedUser()
163 // self::assertEquals(false, api_user());
167 * Test the api_source() function.
171 public function testApiSource()
173 self::assertEquals('api', BasicAuth::getCurrentApplicationToken()['name']);
177 * Test the api_source() function with a Twidere user agent.
181 public function testApiSourceWithTwidere()
183 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
184 self::assertEquals('Twidere', BasicAuth::getCurrentApplicationToken()['name']);
188 * Test the api_source() function with a GET parameter.
192 public function testApiSourceWithGet()
194 $_REQUEST['source'] = 'source_name';
195 self::assertEquals('source_name', BasicAuth::getCurrentApplicationToken()['name']);
199 * Test the api_date() function.
203 public function testApiDate()
205 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
209 * Test the api_register_func() function.
213 public function testApiRegisterFunc()
225 self::assertTrue(is_callable($API['api_path']['func']));
229 * Test the BasicAuth::getCurrentUserID() function without any login.
231 * @runInSeparateProcess
232 * @preserveGlobalState disabled
233 * @preserveGlobalState disabled
235 public function testApiLoginWithoutLogin()
237 BasicAuth::setCurrentUserID();
238 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
239 BasicAuth::getCurrentUserID(true);
243 * Test the BasicAuth::getCurrentUserID() function with a bad login.
245 * @runInSeparateProcess
246 * @preserveGlobalState disabled
247 * @preserveGlobalState disabled
249 public function testApiLoginWithBadLogin()
251 BasicAuth::setCurrentUserID();
252 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
253 $_SERVER['PHP_AUTH_USER'] = 'user@server';
254 BasicAuth::getCurrentUserID(true);
258 * Test the BasicAuth::getCurrentUserID() function with oAuth.
262 public function testApiLoginWithOauth()
264 $this->markTestIncomplete('Can we test this easily?');
268 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
272 public function testApiLoginWithAddonAuth()
274 $this->markTestIncomplete('Can we test this easily?');
278 * Test the BasicAuth::getCurrentUserID() function with a correct login.
280 * @runInSeparateProcess
281 * @preserveGlobalState disabled
282 * @doesNotPerformAssertions
284 public function testApiLoginWithCorrectLogin()
286 BasicAuth::setCurrentUserID();
287 $_SERVER['PHP_AUTH_USER'] = 'Test user';
288 $_SERVER['PHP_AUTH_PW'] = 'password';
289 BasicAuth::getCurrentUserID(true);
293 * Test the BasicAuth::getCurrentUserID() function with a remote user.
295 * @runInSeparateProcess
296 * @preserveGlobalState disabled
298 public function testApiLoginWithRemoteUser()
300 BasicAuth::setCurrentUserID();
301 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
302 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
303 BasicAuth::getCurrentUserID(true);
307 * Test the api_call() function.
309 * @runInSeparateProcess
310 * @preserveGlobalState disabled
312 public function testApiCall()
316 'method' => 'method',
317 'func' => function () {
318 return ['data' => ['some_data']];
321 $_SERVER['REQUEST_METHOD'] = 'method';
322 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
323 $_GET['callback'] = 'callback_name';
326 'callback_name(["some_data"])',
327 api_call('api_path', 'json')
332 * Test the api_call() function with the profiled enabled.
334 * @runInSeparateProcess
335 * @preserveGlobalState disabled
337 public function testApiCallWithProfiler()
341 'method' => 'method',
342 'func' => function () {
343 return ['data' => ['some_data']];
347 $_SERVER['REQUEST_METHOD'] = 'method';
348 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
350 $this->config->set('system', 'profiler', true);
351 $this->config->set('rendertime', 'callstack', true);
352 $this->app->callstack = [
353 'database' => ['some_function' => 200],
354 'database_write' => ['some_function' => 200],
355 'cache' => ['some_function' => 200],
356 'cache_write' => ['some_function' => 200],
357 'network' => ['some_function' => 200]
362 api_call('api_path', 'json')
367 * Test the api_call() function with a JSON result.
369 * @runInSeparateProcess
370 * @preserveGlobalState disabled
372 public function testApiCallWithJson()
376 'method' => 'method',
377 'func' => function () {
378 return ['data' => ['some_data']];
381 $_SERVER['REQUEST_METHOD'] = 'method';
382 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
386 api_call('api_path.json', 'json')
391 * Test the api_call() function with an XML result.
393 * @runInSeparateProcess
394 * @preserveGlobalState disabled
396 public function testApiCallWithXml()
400 'method' => 'method',
401 'func' => function () {
405 $_SERVER['REQUEST_METHOD'] = 'method';
406 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
408 $args = DI::args()->determine($_SERVER, $_GET);
412 api_call('api_path.xml', 'xml')
417 * Test the api_call() function with an RSS result.
419 * @runInSeparateProcess
420 * @preserveGlobalState disabled
422 public function testApiCallWithRss()
426 'method' => 'method',
427 'func' => function () {
431 $_SERVER['REQUEST_METHOD'] = 'method';
432 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
435 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
437 api_call('api_path.rss', 'rss')
442 * Test the api_call() function with an Atom result.
444 * @runInSeparateProcess
445 * @preserveGlobalState disabled
447 public function testApiCallWithAtom()
451 'method' => 'method',
452 'func' => function () {
456 $_SERVER['REQUEST_METHOD'] = 'method';
457 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
460 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
462 api_call('api_path.atom', 'atom')
467 * Test the api_rss_extra() function.
471 public function testApiRssExtra()
474 $user_info = ['url' => 'user_url', 'lang' => 'en'];
475 $result = api_rss_extra([], $user_info);
476 self::assertEquals($user_info, $result['$user']);
477 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
478 self::assertArrayHasKey('self', $result['$rss']);
479 self::assertArrayHasKey('base', $result['$rss']);
480 self::assertArrayHasKey('updated', $result['$rss']);
481 self::assertArrayHasKey('atom_updated', $result['$rss']);
482 self::assertArrayHasKey('language', $result['$rss']);
483 self::assertArrayHasKey('logo', $result['$rss']);
488 * Test the api_rss_extra() function without any user info.
492 public function testApiRssExtraWithoutUserInfo()
495 $result = api_rss_extra([], null);
496 self::assertIsArray($result['$user']);
497 self::assertArrayHasKey('alternate', $result['$rss']);
498 self::assertArrayHasKey('self', $result['$rss']);
499 self::assertArrayHasKey('base', $result['$rss']);
500 self::assertArrayHasKey('updated', $result['$rss']);
501 self::assertArrayHasKey('atom_updated', $result['$rss']);
502 self::assertArrayHasKey('language', $result['$rss']);
503 self::assertArrayHasKey('logo', $result['$rss']);
508 * Test the api_get_user() function.
512 public function testApiGetUser()
514 // $user = api_get_user();
515 // self::assertSelfUser($user);
516 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
517 // self::assertEquals('6fdbe8', $user['profile_link_color']);
518 // self::assertEquals('ededed', $user['profile_background_color']);
522 * Test the api_get_user() function with a Frio schema.
526 public function testApiGetUserWithFrioSchema()
528 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
529 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
530 // $user = api_get_user();
531 // self::assertSelfUser($user);
532 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
533 // self::assertEquals('6fdbe8', $user['profile_link_color']);
534 // self::assertEquals('ededed', $user['profile_background_color']);
538 * Test the api_get_user() function with an empty Frio schema.
542 public function testApiGetUserWithEmptyFrioSchema()
544 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
545 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
546 // $user = api_get_user();
547 // self::assertSelfUser($user);
548 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
549 // self::assertEquals('6fdbe8', $user['profile_link_color']);
550 // self::assertEquals('ededed', $user['profile_background_color']);
554 * Test the api_get_user() function with a custom Frio schema.
558 public function testApiGetUserWithCustomFrioSchema()
560 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
561 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
562 // $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
563 // $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
564 // $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
565 // $user = api_get_user();
566 // self::assertSelfUser($user);
567 // self::assertEquals('123456', $user['profile_sidebar_fill_color']);
568 // self::assertEquals('123456', $user['profile_link_color']);
569 // self::assertEquals('123456', $user['profile_background_color']);
573 * Test the api_get_user() function with an user that is not allowed to use the API.
575 * @runInSeparateProcess
576 * @preserveGlobalState disabled
578 public function testApiGetUserWithoutApiUser()
580 // api_get_user() with empty parameters is not used anymore
582 $_SERVER['PHP_AUTH_USER'] = 'Test user';
583 $_SERVER['PHP_AUTH_PW'] = 'password';
584 BasicAuth::setCurrentUserID();
585 self::assertFalse(api_get_user());
590 * Test the api_get_user() function with an user ID in a GET parameter.
594 public function testApiGetUserWithGetId()
596 // self::assertOtherUser(api_get_user());
600 * Test the api_get_user() function with a wrong user ID in a GET parameter.
604 public function testApiGetUserWithWrongGetId()
606 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
607 // self::assertOtherUser(api_get_user());
611 * Test the api_get_user() function with an user name in a GET parameter.
615 public function testApiGetUserWithGetName()
617 // self::assertSelfUser(api_get_user());
621 * Test the api_get_user() function with a profile URL in a GET parameter.
625 public function testApiGetUserWithGetUrl()
627 // self::assertSelfUser(api_get_user());
631 * Test the api_get_user() function with an user ID in the API path.
635 public function testApiGetUserWithNumericCalledApi()
637 // global $called_api;
638 // $called_api = ['api_path'];
639 // DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
640 // self::assertOtherUser(api_get_user());
644 * Test the api_get_user() function with the $called_api global variable.
648 public function testApiGetUserWithCalledApi()
650 // global $called_api;
651 // $called_api = ['api', 'api_path'];
652 // self::assertSelfUser(api_get_user());
656 * Test the Arrays::walkRecursive() function.
660 public function testApiWalkRecursive()
665 Arrays::walkRecursive(
668 // Should we test this with a callback that actually does something?
676 * Test the Arrays::walkRecursive() function with an array.
680 public function testApiWalkRecursiveWithArray()
682 $array = [['item1'], ['item2']];
685 Arrays::walkRecursive(
688 // Should we test this with a callback that actually does something?
696 * Test the BaseApi::reformatXML() function.
700 public function testApiReformatXml()
704 self::assertTrue(ApiResponse::reformatXML($item, $key));
705 self::assertEquals('true', $item);
709 * Test the BaseApi::reformatXML() function with a statusnet_api key.
713 public function testApiReformatXmlWithStatusnetKey()
716 $key = 'statusnet_api';
717 self::assertTrue(ApiResponse::reformatXML($item, $key));
718 self::assertEquals('statusnet:api', $key);
722 * Test the BaseApi::reformatXML() function with a friendica_api key.
726 public function testApiReformatXmlWithFriendicaKey()
729 $key = 'friendica_api';
730 self::assertTrue(ApiResponse::reformatXML($item, $key));
731 self::assertEquals('friendica:api', $key);
735 * Test the BaseApi::createXML() function.
739 public function testApiCreateXml()
742 '<?xml version="1.0"?>' . "\n" .
743 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
744 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
745 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
746 ' <data>some_data</data>' . "\n" .
747 '</root_element>' . "\n",
748 DI::apiResponse()->createXML(['data' => ['some_data']], 'root_element')
753 * Test the BaseApi::createXML() function without any XML namespace.
757 public function testApiCreateXmlWithoutNamespaces()
760 '<?xml version="1.0"?>' . "\n" .
762 ' <data>some_data</data>' . "\n" .
764 DI::apiResponse()->createXML(['data' => ['some_data']], 'ok')
769 * Test the BaseApi::formatData() function.
773 public function testApiFormatData()
775 $data = ['some_data'];
776 self::assertEquals($data, DI::apiResponse()->formatData('root_element', 'json', $data));
780 * Test the BaseApi::formatData() function with an XML result.
784 public function testApiFormatDataWithXml()
787 '<?xml version="1.0"?>' . "\n" .
788 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
789 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
790 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
791 ' <data>some_data</data>' . "\n" .
792 '</root_element>' . "\n",
793 DI::apiResponse()->formatData('root_element', 'xml', ['data' => ['some_data']])
798 * Test the api_statuses_mediap() function.
802 public function testApiStatusesMediap()
805 DI::args()->setArgc(2);
813 'tmp_name' => $this->getTempImage(),
814 'name' => 'spacer.png',
815 'type' => 'image/png'
818 $_GET['status'] = '<b>Status content</b>';
820 $result = api_statuses_mediap('json');
821 self::assertStatus($result['status']);
826 * Test the api_statuses_mediap() function without an authenticated user.
830 public function testApiStatusesMediapWithoutAuthenticatedUser()
832 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
833 // BasicAuth::setCurrentUserID();
834 // $_SESSION['authenticated'] = false;
835 // api_statuses_mediap('json');
839 * Test the api_statuses_repeat() function.
843 public function testApiStatusesRepeat()
845 // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
846 // api_statuses_repeat('json');
850 * Test the api_statuses_repeat() function without an authenticated user.
854 public function testApiStatusesRepeatWithoutAuthenticatedUser()
856 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
857 // BasicAuth::setCurrentUserID();
858 // $_SESSION['authenticated'] = false;
859 // api_statuses_repeat('json');
863 * Test the api_statuses_repeat() function with an ID.
867 public function testApiStatusesRepeatWithId()
869 // DI::args()->setArgv(['', '', '', 1]);
870 // $result = api_statuses_repeat('json');
871 // self::assertStatus($result['status']);
873 // Also test with a shared status
874 // DI::args()->setArgv(['', '', '', 5]);
875 // $result = api_statuses_repeat('json');
876 // self::assertStatus($result['status']);
880 * Test the api_favorites_create_destroy() function.
884 public function testApiFavoritesCreateDestroy()
886 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
887 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
888 // api_favorites_create_destroy('json');
892 * Test the api_favorites_create_destroy() function with an invalid ID.
896 public function testApiFavoritesCreateDestroyWithInvalidId()
898 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
899 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
900 // api_favorites_create_destroy('json');
904 * Test the api_favorites_create_destroy() function with an invalid action.
908 public function testApiFavoritesCreateDestroyWithInvalidAction()
910 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
911 // DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
912 // $_REQUEST['id'] = 1;
913 // api_favorites_create_destroy('json');
917 * Test the api_favorites_create_destroy() function with the create action.
921 public function testApiFavoritesCreateDestroyWithCreateAction()
923 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
924 // $_REQUEST['id'] = 3;
925 // $result = api_favorites_create_destroy('json');
926 // self::assertStatus($result['status']);
930 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
934 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
936 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
937 // $_REQUEST['id'] = 3;
938 // $result = api_favorites_create_destroy('rss');
939 // self::assertXml($result, 'status');
943 * Test the api_favorites_create_destroy() function with the destroy action.
947 public function testApiFavoritesCreateDestroyWithDestroyAction()
949 // DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
950 // $_REQUEST['id'] = 3;
951 // $result = api_favorites_create_destroy('json');
952 // self::assertStatus($result['status']);
956 * Test the api_favorites_create_destroy() function without an authenticated user.
960 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
963 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
964 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
965 BasicAuth::setCurrentUserID();
966 $_SESSION['authenticated'] = false;
967 api_favorites_create_destroy('json');
974 * Test the api_format_messages() function.
978 public function testApiFormatMessages()
980 $result = api_format_messages(
981 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
982 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
983 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
985 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
986 self::assertEquals(1, $result['id']);
987 self::assertEquals(2, $result['recipient_id']);
988 self::assertEquals(3, $result['sender_id']);
989 self::assertEquals('recipient_name', $result['recipient_screen_name']);
990 self::assertEquals('sender_name', $result['sender_screen_name']);
994 * Test the api_format_messages() function with HTML.
998 public function testApiFormatMessagesWithHtmlText()
1000 $_GET['getText'] = 'html';
1001 $result = api_format_messages(
1002 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1003 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1004 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1006 self::assertEquals('item_title', $result['title']);
1007 self::assertEquals('<strong>item_body</strong>', $result['text']);
1011 * Test the api_format_messages() function with plain text.
1015 public function testApiFormatMessagesWithPlainText()
1017 $_GET['getText'] = 'plain';
1018 $result = api_format_messages(
1019 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1020 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1021 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1023 self::assertEquals('item_title', $result['title']);
1024 self::assertEquals('item_body', $result['text']);
1028 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1032 public function testApiFormatMessagesWithoutUserObjects()
1034 $_GET['getUserObjects'] = 'false';
1035 $result = api_format_messages(
1036 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1037 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1038 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1040 self::assertTrue(!isset($result['sender']));
1041 self::assertTrue(!isset($result['recipient']));
1045 * Test the api_convert_item() function.
1049 public function testApiConvertItem()
1052 $result = api_convert_item(
1054 'network' => 'feed',
1055 'title' => 'item_title',
1057 // We need a long string to test that it is correctly cut
1058 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
1059 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
1060 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
1061 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
1062 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
1063 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
1064 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
1065 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
1066 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
1067 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
1068 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
1069 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
1070 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
1071 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
1072 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
1073 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
1074 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
1075 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
1076 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
1077 'repellat officia illum quos impedit quam iste esse unde qui ' .
1078 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
1079 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
1080 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
1081 'plink' => 'item_plink'
1084 self::assertStringStartsWith('item_title', $result['text']);
1085 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
1090 * Test the api_convert_item() function with an empty item body.
1094 public function testApiConvertItemWithoutBody()
1097 $result = api_convert_item(
1099 'network' => 'feed',
1100 'title' => 'item_title',
1103 'plink' => 'item_plink'
1106 self::assertEquals("item_title", $result['text']);
1107 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
1112 * Test the api_convert_item() function with the title in the body.
1116 public function testApiConvertItemWithTitleInBody()
1119 $result = api_convert_item(
1121 'title' => 'item_title',
1122 'body' => 'item_title item_body',
1126 self::assertEquals('item_title item_body', $result['text']);
1127 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
1132 * Test the api_get_attachments() function.
1136 public function testApiGetAttachments()
1139 // self::assertEmpty(api_get_attachments($body, 0));
1143 * Test the api_get_attachments() function with an img tag.
1147 public function testApiGetAttachmentsWithImage()
1149 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
1150 // self::assertIsArray(api_get_attachments($body, 0));
1154 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
1158 public function testApiGetAttachmentsWithImageAndAndStatus()
1160 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
1161 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
1162 // self::assertIsArray(api_get_attachments($body, 0));
1166 * Test the api_get_entitities() function.
1170 public function testApiGetEntitities()
1173 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
1177 * Test the api_get_entitities() function with the include_entities parameter.
1181 public function testApiGetEntititiesWithIncludeEntities()
1184 $_REQUEST['include_entities'] = 'true';
1186 $result = api_get_entitities($text, 'bbcode', 0);
1187 self::assertIsArray($result['hashtags']);
1188 self::assertIsArray($result['symbols']);
1189 self::assertIsArray($result['urls']);
1190 self::assertIsArray($result['user_mentions']);
1195 * Test the api_format_items_embeded_images() function.
1199 public function testApiFormatItemsEmbededImages()
1203 'text ' . DI::baseUrl() . '/display/item_guid',
1204 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
1210 * Test the api_format_items_activities() function.
1214 public function testApiFormatItemsActivities()
1216 $item = ['uid' => 0, 'uri-id' => 1];
1217 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
1218 self::assertArrayHasKey('like', $result);
1219 self::assertArrayHasKey('dislike', $result);
1220 self::assertArrayHasKey('attendyes', $result);
1221 self::assertArrayHasKey('attendno', $result);
1222 self::assertArrayHasKey('attendmaybe', $result);
1226 * Test the api_format_items_activities() function with an XML result.
1230 public function testApiFormatItemsActivitiesWithXml()
1232 $item = ['uid' => 0, 'uri-id' => 1];
1233 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
1234 self::assertArrayHasKey('friendica:like', $result);
1235 self::assertArrayHasKey('friendica:dislike', $result);
1236 self::assertArrayHasKey('friendica:attendyes', $result);
1237 self::assertArrayHasKey('friendica:attendno', $result);
1238 self::assertArrayHasKey('friendica:attendmaybe', $result);
1242 * Test the api_format_items() function.
1243 * @doesNotPerformAssertions
1245 public function testApiFormatItems()
1248 $items = Post::selectToArray([], ['uid' => 42]);
1249 foreach ($items as $item) {
1250 $status = api_format_item($item);
1251 self::assertStatus($status);
1257 * Test the api_format_items() function with an XML result.
1258 * @doesNotPerformAssertions
1260 public function testApiFormatItemsWithXml()
1263 $items = Post::selectToArray([], ['uid' => 42]);
1264 foreach ($items as $item) {
1265 $status = api_format_item($item, 'xml');
1266 self::assertStatus($status);
1272 * Test the api_lists_list() function.
1276 public function testApiListsList()
1278 $result = api_lists_list('json');
1279 self::assertEquals(['lists_list' => []], $result);
1283 * Test the api_lists_ownerships() function.
1287 public function testApiListsOwnerships()
1289 $result = api_lists_ownerships('json');
1290 foreach ($result['lists']['lists'] as $list) {
1291 self::assertList($list);
1296 * Test the api_lists_ownerships() function without an authenticated user.
1300 public function testApiListsOwnershipsWithoutAuthenticatedUser()
1302 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1303 BasicAuth::setCurrentUserID();
1304 $_SESSION['authenticated'] = false;
1305 api_lists_ownerships('json');
1309 * Test the api_statuses_f() function.
1313 public function testApiStatusesFWithIncoming()
1315 // $result = api_statuses_f('incoming');
1316 // self::assertArrayHasKey('user', $result);
1321 * Test the api_direct_messages_new() function.
1325 public function testApiDirectMessagesNew()
1327 $result = api_direct_messages_new('json');
1328 self::assertNull($result);
1332 * Test the api_direct_messages_new() function without an authenticated user.
1336 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
1338 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1339 BasicAuth::setCurrentUserID();
1340 $_SESSION['authenticated'] = false;
1341 api_direct_messages_new('json');
1345 * Test the api_direct_messages_new() function with an user ID.
1349 public function testApiDirectMessagesNewWithUserId()
1351 $_POST['text'] = 'message_text';
1352 $_REQUEST['user_id'] = $this->otherUser['id'];
1353 $result = api_direct_messages_new('json');
1354 self::assertEquals(['direct_message' => ['error' => -1]], $result);
1358 * Test the api_direct_messages_new() function with a screen name.
1362 public function testApiDirectMessagesNewWithScreenName()
1364 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1365 $_POST['text'] = 'message_text';
1366 $_REQUEST['user_id'] = $this->friendUser['id'];
1367 $result = api_direct_messages_new('json');
1368 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1369 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1370 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1374 * Test the api_direct_messages_new() function with a title.
1378 public function testApiDirectMessagesNewWithTitle()
1380 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1381 $_POST['text'] = 'message_text';
1382 $_REQUEST['user_id'] = $this->friendUser['id'];
1383 $_REQUEST['title'] = 'message_title';
1384 $result = api_direct_messages_new('json');
1385 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1386 self::assertStringContainsString('message_title', $result['direct_message']['text']);
1387 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1388 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1392 * Test the api_direct_messages_new() function with an RSS result.
1396 public function testApiDirectMessagesNewWithRss()
1398 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1399 $_POST['text'] = 'message_text';
1400 $_REQUEST['user_id'] = $this->friendUser['id'];
1401 $result = api_direct_messages_new('rss');
1402 self::assertXml($result, 'direct-messages');
1406 * Test the api_direct_messages_destroy() function.
1410 public function testApiDirectMessagesDestroy()
1412 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1413 api_direct_messages_destroy('json');
1417 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
1421 public function testApiDirectMessagesDestroyWithVerbose()
1423 $_GET['friendica_verbose'] = 'true';
1424 $result = api_direct_messages_destroy('json');
1428 'result' => 'error',
1429 'message' => 'message id or parenturi not specified'
1437 * Test the api_direct_messages_destroy() function without an authenticated user.
1441 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
1443 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1444 BasicAuth::setCurrentUserID();
1445 $_SESSION['authenticated'] = false;
1446 api_direct_messages_destroy('json');
1450 * Test the api_direct_messages_destroy() function with a non-zero ID.
1454 public function testApiDirectMessagesDestroyWithId()
1456 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1457 $_REQUEST['id'] = 1;
1458 api_direct_messages_destroy('json');
1462 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
1466 public function testApiDirectMessagesDestroyWithIdAndVerbose()
1468 $_REQUEST['id'] = 1;
1469 $_REQUEST['friendica_parenturi'] = 'parent_uri';
1470 $_GET['friendica_verbose'] = 'true';
1471 $result = api_direct_messages_destroy('json');
1475 'result' => 'error',
1476 'message' => 'message id not in database'
1484 * Test the api_direct_messages_destroy() function with a non-zero ID.
1488 public function testApiDirectMessagesDestroyWithCorrectId()
1490 $this->markTestIncomplete('We need to add a dataset for this.');
1494 * Test the api_direct_messages_box() function.
1498 public function testApiDirectMessagesBoxWithSentbox()
1500 $_REQUEST['page'] = -1;
1501 $_REQUEST['max_id'] = 10;
1502 $result = api_direct_messages_box('json', 'sentbox', 'false');
1503 self::assertArrayHasKey('direct_message', $result);
1507 * Test the api_direct_messages_box() function.
1511 public function testApiDirectMessagesBoxWithConversation()
1513 $result = api_direct_messages_box('json', 'conversation', 'false');
1514 self::assertArrayHasKey('direct_message', $result);
1518 * Test the api_direct_messages_box() function.
1522 public function testApiDirectMessagesBoxWithAll()
1524 $result = api_direct_messages_box('json', 'all', 'false');
1525 self::assertArrayHasKey('direct_message', $result);
1529 * Test the api_direct_messages_box() function.
1533 public function testApiDirectMessagesBoxWithInbox()
1535 $result = api_direct_messages_box('json', 'inbox', 'false');
1536 self::assertArrayHasKey('direct_message', $result);
1540 * Test the api_direct_messages_box() function.
1544 public function testApiDirectMessagesBoxWithVerbose()
1546 $result = api_direct_messages_box('json', 'sentbox', 'true');
1550 'result' => 'error',
1551 'message' => 'no mails available'
1559 * Test the api_direct_messages_box() function with a RSS result.
1563 public function testApiDirectMessagesBoxWithRss()
1565 $result = api_direct_messages_box('rss', 'sentbox', 'false');
1566 self::assertXml($result, 'direct-messages');
1570 * Test the api_direct_messages_box() function without an authenticated user.
1574 public function testApiDirectMessagesBoxWithUnallowedUser()
1576 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1577 BasicAuth::setCurrentUserID();
1578 api_direct_messages_box('json', 'sentbox', 'false');
1582 * Test the api_direct_messages_sentbox() function.
1586 public function testApiDirectMessagesSentbox()
1588 $result = api_direct_messages_sentbox('json');
1589 self::assertArrayHasKey('direct_message', $result);
1593 * Test the api_direct_messages_inbox() function.
1597 public function testApiDirectMessagesInbox()
1599 $result = api_direct_messages_inbox('json');
1600 self::assertArrayHasKey('direct_message', $result);
1604 * Test the api_direct_messages_all() function.
1608 public function testApiDirectMessagesAll()
1610 $result = api_direct_messages_all('json');
1611 self::assertArrayHasKey('direct_message', $result);
1615 * Test the api_direct_messages_conversation() function.
1619 public function testApiDirectMessagesConversation()
1621 $result = api_direct_messages_conversation('json');
1622 self::assertArrayHasKey('direct_message', $result);
1626 * Test the api_oauth_request_token() function.
1630 public function testApiOauthRequestToken()
1632 $this->markTestIncomplete('exit() kills phpunit as well');
1636 * Test the api_oauth_access_token() function.
1640 public function testApiOauthAccessToken()
1642 $this->markTestIncomplete('exit() kills phpunit as well');
1646 * Test the api_fr_photos_list() function.
1650 public function testApiFrPhotosList()
1652 $result = api_fr_photos_list('json');
1653 self::assertArrayHasKey('photo', $result);
1657 * Test the api_fr_photos_list() function without an authenticated user.
1661 public function testApiFrPhotosListWithoutAuthenticatedUser()
1663 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1664 BasicAuth::setCurrentUserID();
1665 $_SESSION['authenticated'] = false;
1666 api_fr_photos_list('json');
1670 * Test the api_fr_photo_create_update() function.
1672 public function testApiFrPhotoCreateUpdate()
1674 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1675 api_fr_photo_create_update('json');
1679 * Test the api_fr_photo_create_update() function without an authenticated user.
1683 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
1685 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1686 BasicAuth::setCurrentUserID();
1687 $_SESSION['authenticated'] = false;
1688 api_fr_photo_create_update('json');
1692 * Test the api_fr_photo_create_update() function with an album name.
1696 public function testApiFrPhotoCreateUpdateWithAlbum()
1698 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1699 $_REQUEST['album'] = 'album_name';
1700 api_fr_photo_create_update('json');
1704 * Test the api_fr_photo_create_update() function with the update mode.
1708 public function testApiFrPhotoCreateUpdateWithUpdate()
1710 $this->markTestIncomplete('We need to create a dataset for this');
1714 * Test the api_fr_photo_create_update() function with an uploaded file.
1718 public function testApiFrPhotoCreateUpdateWithFile()
1720 $this->markTestIncomplete();
1724 * Test the api_fr_photo_detail() function.
1728 public function testApiFrPhotoDetail()
1730 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1731 api_fr_photo_detail('json');
1735 * Test the api_fr_photo_detail() function without an authenticated user.
1739 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
1741 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1742 BasicAuth::setCurrentUserID();
1743 $_SESSION['authenticated'] = false;
1744 api_fr_photo_detail('json');
1748 * Test the api_fr_photo_detail() function with a photo ID.
1752 public function testApiFrPhotoDetailWithPhotoId()
1754 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1755 $_REQUEST['photo_id'] = 1;
1756 api_fr_photo_detail('json');
1760 * Test the api_fr_photo_detail() function with a correct photo ID.
1764 public function testApiFrPhotoDetailCorrectPhotoId()
1766 $this->markTestIncomplete('We need to create a dataset for this.');
1770 * Test the api_account_update_profile_image() function.
1774 public function testApiAccountUpdateProfileImage()
1776 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1777 api_account_update_profile_image('json');
1781 * Test the api_account_update_profile_image() function without an authenticated user.
1785 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
1787 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1788 BasicAuth::setCurrentUserID();
1789 $_SESSION['authenticated'] = false;
1790 api_account_update_profile_image('json');
1794 * Test the api_account_update_profile_image() function with an uploaded file.
1798 public function testApiAccountUpdateProfileImageWithUpload()
1800 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1801 $this->markTestIncomplete();
1805 * Test the check_acl_input() function.
1809 public function testCheckAclInput()
1811 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
1812 // Where does this result come from?
1813 self::assertEquals(1, $result);
1817 * Test the check_acl_input() function with an empty ACL string.
1821 public function testCheckAclInputWithEmptyAclString()
1823 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
1824 self::assertFalse($result);
1828 * Test the save_media_to_database() function.
1832 public function testSaveMediaToDatabase()
1834 $this->markTestIncomplete();
1838 * Test the post_photo_item() function.
1842 public function testPostPhotoItem()
1844 $this->markTestIncomplete();
1848 * Test the prepare_photo_data() function.
1852 public function testPreparePhotoData()
1854 $this->markTestIncomplete();
1858 * Test the api_share_as_retweet() function with a valid item.
1862 public function testApiShareAsRetweetWithValidItem()
1864 $this->markTestIncomplete();
1868 * Test the api_in_reply_to() function with a valid item.
1872 public function testApiInReplyToWithValidItem()
1874 $this->markTestIncomplete();
1878 * Test the api_clean_plain_items() function.
1882 public function testApiCleanPlainItems()
1884 $_REQUEST['include_entities'] = 'true';
1885 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
1886 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
1890 * Test the api_best_nickname() function with contacts.
1894 public function testApiBestNicknameWithContacts()
1896 $this->markTestIncomplete();
1900 * Test the api_friendica_group_show() function.
1904 public function testApiFriendicaGroupShow()
1906 $this->markTestIncomplete();
1910 * Test the api_friendica_group_delete() function.
1914 public function testApiFriendicaGroupDelete()
1916 $this->markTestIncomplete();
1920 * Test the api_lists_destroy() function.
1924 public function testApiListsDestroy()
1926 $this->markTestIncomplete();
1930 * Test the group_create() function.
1934 public function testGroupCreate()
1936 $this->markTestIncomplete();
1940 * Test the api_friendica_group_create() function.
1944 public function testApiFriendicaGroupCreate()
1946 $this->markTestIncomplete();
1950 * Test the api_lists_create() function.
1954 public function testApiListsCreate()
1956 $this->markTestIncomplete();
1960 * Test the api_friendica_group_update() function.
1964 public function testApiFriendicaGroupUpdate()
1966 $this->markTestIncomplete();
1970 * Test the api_lists_update() function.
1974 public function testApiListsUpdate()
1976 $this->markTestIncomplete();
1980 * Test the api_friendica_activity() function.
1984 public function testApiFriendicaActivity()
1986 $this->markTestIncomplete();
1990 * Test the api_friendica_notification_seen() function.
1994 public function testApiFriendicaNotificationSeen()
1996 $this->markTestIncomplete();
2000 * Test the api_friendica_direct_messages_setseen() function.
2004 public function testApiFriendicaDirectMessagesSetseen()
2006 $this->markTestIncomplete();
2010 * Test the api_friendica_direct_messages_search() function.
2014 public function testApiFriendicaDirectMessagesSearch()
2016 $this->markTestIncomplete();