6 namespace Friendica\Test\legacy;
9 use Friendica\Core\Config\Capability\IManageConfigValues;
10 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
11 use Friendica\Core\Protocol;
13 use Friendica\Model\Post;
14 use Friendica\Module\Api\ApiResponse;
15 use Friendica\Module\BaseApi;
16 use Friendica\Network\HTTPException;
17 use Friendica\Security\BasicAuth;
18 use Friendica\Test\FixtureTest;
19 use Friendica\Util\Arrays;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Temporal;
22 use Monolog\Handler\TestHandler;
24 require_once __DIR__ . '/../../include/api.php';
27 * Tests for the API functions.
29 * Functions that use header() need to be tested in a separate process.
30 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
32 * @backupGlobals enabled
34 class ApiTest extends FixtureTest
37 * @var TestHandler Can handle log-outputs
44 protected $friendUser;
48 protected $wrongUserId;
53 /** @var IManageConfigValues */
57 * Create variables used by tests.
59 protected function setUp() : void
61 global $API, $called_api;
67 /** @var IManageConfigValues $config */
68 $this->config = $this->dice->create(IManageConfigValues::class);
70 $this->config->set('system', 'url', 'http://localhost');
71 $this->config->set('system', 'hostname', 'localhost');
72 $this->config->set('system', 'worker_dont_fork', true);
75 $this->config->set('config', 'hostname', 'localhost');
76 $this->config->set('system', 'throttle_limit_day', 100);
77 $this->config->set('system', 'throttle_limit_week', 100);
78 $this->config->set('system', 'throttle_limit_month', 100);
79 $this->config->set('system', 'theme', 'system_theme');
83 $this->app = DI::app();
85 DI::args()->setArgc(1);
87 // User data that the test database is populated with
90 'name' => 'Self contact',
91 'nick' => 'selfcontact',
92 'nurl' => 'http://localhost/profile/selfcontact'
96 'name' => 'Friend contact',
97 'nick' => 'friendcontact',
98 'nurl' => 'http://localhost/profile/friendcontact'
102 'name' => 'othercontact',
103 'nick' => 'othercontact',
104 'nurl' => 'http://localhost/profile/othercontact'
107 // User ID that we know is not in the database
108 $this->wrongUserId = 666;
110 DI::session()->start();
112 // Most API require login so we force the session
114 'authenticated' => true,
115 'uid' => $this->selfUser['id']
117 BasicAuth::setCurrentUserID($this->selfUser['id']);
121 * Assert that an user array contains expected keys.
123 * @param array $user User array
127 private function assertSelfUser(array $user)
129 self::assertEquals($this->selfUser['id'], $user['uid']);
130 self::assertEquals($this->selfUser['id'], $user['cid']);
131 self::assertEquals(1, $user['self']);
132 self::assertEquals('DFRN', $user['location']);
133 self::assertEquals($this->selfUser['name'], $user['name']);
134 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
135 self::assertEquals('dfrn', $user['network']);
136 self::assertTrue($user['verified']);
140 * Assert that an user array contains expected keys.
142 * @param array $user User array
146 private function assertOtherUser(array $user = [])
148 self::assertEquals($this->otherUser['id'], $user['id']);
149 self::assertEquals($this->otherUser['id'], $user['id_str']);
150 self::assertEquals($this->otherUser['name'], $user['name']);
151 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
152 self::assertFalse($user['verified']);
156 * Assert that a status array contains expected keys.
158 * @param array $status Status array
162 private function assertStatus(array $status = [])
164 self::assertIsString($status['text'] ?? '');
165 self::assertIsInt($status['id'] ?? '');
166 // We could probably do more checks here.
170 * Assert that a list array contains expected keys.
172 * @param array $list List array
176 private function assertList(array $list = [])
178 self::assertIsString($list['name']);
179 self::assertIsInt($list['id']);
180 self::assertIsString('string', $list['id_str']);
181 self::assertContains($list['mode'], ['public', 'private']);
182 // We could probably do more checks here.
186 * Assert that the string is XML and contain the root element.
188 * @param string $result XML string
189 * @param string $root_element Root element name
193 private function assertXml($result = '', $root_element = '')
195 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
196 self::assertStringContainsString('<' . $root_element, $result);
197 // We could probably do more checks here.
201 * Get the path to a temporary empty PNG image.
203 * @return string Path
205 private function getTempImage()
207 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
211 // Empty 1x1 px PNG image
212 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
220 * Test the api_user() function.
224 public function testApiUser()
226 self::assertEquals($this->selfUser['id'], BaseApi::getCurrentUserID());
230 * Test the api_user() function with an unallowed user.
234 public function testApiUserWithUnallowedUser()
236 // self::assertEquals(false, api_user());
240 * Test the api_source() function.
244 public function testApiSource()
246 self::assertEquals('api', BasicAuth::getCurrentApplicationToken()['name']);
250 * Test the api_source() function with a Twidere user agent.
254 public function testApiSourceWithTwidere()
256 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
257 self::assertEquals('Twidere', BasicAuth::getCurrentApplicationToken()['name']);
261 * Test the api_source() function with a GET parameter.
265 public function testApiSourceWithGet()
267 $_REQUEST['source'] = 'source_name';
268 self::assertEquals('source_name', BasicAuth::getCurrentApplicationToken()['name']);
272 * Test the api_date() function.
276 public function testApiDate()
278 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
282 * Test the api_register_func() function.
286 public function testApiRegisterFunc()
298 self::assertTrue($API['api_path']['auth']);
299 self::assertEquals('method', $API['api_path']['method']);
300 self::assertTrue(is_callable($API['api_path']['func']));
304 * Test the BasicAuth::getCurrentUserID() function without any login.
306 * @runInSeparateProcess
307 * @preserveGlobalState disabled
308 * @preserveGlobalState disabled
310 public function testApiLoginWithoutLogin()
312 BasicAuth::setCurrentUserID();
313 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
314 BasicAuth::getCurrentUserID(true);
318 * Test the BasicAuth::getCurrentUserID() function with a bad login.
320 * @runInSeparateProcess
321 * @preserveGlobalState disabled
322 * @preserveGlobalState disabled
324 public function testApiLoginWithBadLogin()
326 BasicAuth::setCurrentUserID();
327 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
328 $_SERVER['PHP_AUTH_USER'] = 'user@server';
329 BasicAuth::getCurrentUserID(true);
333 * Test the BasicAuth::getCurrentUserID() function with oAuth.
337 public function testApiLoginWithOauth()
339 $this->markTestIncomplete('Can we test this easily?');
343 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
347 public function testApiLoginWithAddonAuth()
349 $this->markTestIncomplete('Can we test this easily?');
353 * Test the BasicAuth::getCurrentUserID() function with a correct login.
355 * @runInSeparateProcess
356 * @preserveGlobalState disabled
357 * @doesNotPerformAssertions
359 public function testApiLoginWithCorrectLogin()
361 BasicAuth::setCurrentUserID();
362 $_SERVER['PHP_AUTH_USER'] = 'Test user';
363 $_SERVER['PHP_AUTH_PW'] = 'password';
364 BasicAuth::getCurrentUserID(true);
368 * Test the BasicAuth::getCurrentUserID() function with a remote user.
370 * @runInSeparateProcess
371 * @preserveGlobalState disabled
373 public function testApiLoginWithRemoteUser()
375 BasicAuth::setCurrentUserID();
376 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
377 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
378 BasicAuth::getCurrentUserID(true);
382 * Test the api_call() function.
384 * @runInSeparateProcess
385 * @preserveGlobalState disabled
387 public function testApiCall()
391 'method' => 'method',
392 'func' => function () {
393 return ['data' => ['some_data']];
396 $_SERVER['REQUEST_METHOD'] = 'method';
397 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
398 $_GET['callback'] = 'callback_name';
400 $args = DI::args()->determine($_SERVER, $_GET);
403 'callback_name(["some_data"])',
404 api_call($this->app, $args)
409 * Test the api_call() function with the profiled enabled.
411 * @runInSeparateProcess
412 * @preserveGlobalState disabled
414 public function testApiCallWithProfiler()
418 'method' => 'method',
419 'func' => function () {
420 return ['data' => ['some_data']];
424 $_SERVER['REQUEST_METHOD'] = 'method';
425 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
427 $args = DI::args()->determine($_SERVER, $_GET);
429 $this->config->set('system', 'profiler', true);
430 $this->config->set('rendertime', 'callstack', true);
431 $this->app->callstack = [
432 'database' => ['some_function' => 200],
433 'database_write' => ['some_function' => 200],
434 'cache' => ['some_function' => 200],
435 'cache_write' => ['some_function' => 200],
436 'network' => ['some_function' => 200]
441 api_call($this->app, $args)
446 * Test the api_call() function with a JSON result.
448 * @runInSeparateProcess
449 * @preserveGlobalState disabled
451 public function testApiCallWithJson()
455 'method' => 'method',
456 'func' => function () {
457 return ['data' => ['some_data']];
460 $_SERVER['REQUEST_METHOD'] = 'method';
461 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
463 $args = DI::args()->determine($_SERVER, $_GET);
467 api_call($this->app, $args)
472 * Test the api_call() function with an XML result.
474 * @runInSeparateProcess
475 * @preserveGlobalState disabled
477 public function testApiCallWithXml()
481 'method' => 'method',
482 'func' => function () {
486 $_SERVER['REQUEST_METHOD'] = 'method';
487 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
489 $args = DI::args()->determine($_SERVER, $_GET);
493 api_call($this->app, $args)
498 * Test the api_call() function with an RSS result.
500 * @runInSeparateProcess
501 * @preserveGlobalState disabled
503 public function testApiCallWithRss()
507 'method' => 'method',
508 'func' => function () {
512 $_SERVER['REQUEST_METHOD'] = 'method';
513 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
515 $args = DI::args()->determine($_SERVER, $_GET);
518 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
520 api_call($this->app, $args)
525 * Test the api_call() function with an Atom result.
527 * @runInSeparateProcess
528 * @preserveGlobalState disabled
530 public function testApiCallWithAtom()
534 'method' => 'method',
535 'func' => function () {
539 $_SERVER['REQUEST_METHOD'] = 'method';
540 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
542 $args = DI::args()->determine($_SERVER, $_GET);
545 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
547 api_call($this->app, $args)
552 * Test the api_rss_extra() function.
556 public function testApiRssExtra()
559 $user_info = ['url' => 'user_url', 'lang' => 'en'];
560 $result = api_rss_extra([], $user_info);
561 self::assertEquals($user_info, $result['$user']);
562 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
563 self::assertArrayHasKey('self', $result['$rss']);
564 self::assertArrayHasKey('base', $result['$rss']);
565 self::assertArrayHasKey('updated', $result['$rss']);
566 self::assertArrayHasKey('atom_updated', $result['$rss']);
567 self::assertArrayHasKey('language', $result['$rss']);
568 self::assertArrayHasKey('logo', $result['$rss']);
573 * Test the api_rss_extra() function without any user info.
577 public function testApiRssExtraWithoutUserInfo()
580 $result = api_rss_extra([], null);
581 self::assertIsArray($result['$user']);
582 self::assertArrayHasKey('alternate', $result['$rss']);
583 self::assertArrayHasKey('self', $result['$rss']);
584 self::assertArrayHasKey('base', $result['$rss']);
585 self::assertArrayHasKey('updated', $result['$rss']);
586 self::assertArrayHasKey('atom_updated', $result['$rss']);
587 self::assertArrayHasKey('language', $result['$rss']);
588 self::assertArrayHasKey('logo', $result['$rss']);
593 * Test the api_get_user() function.
597 public function testApiGetUser()
599 // $user = api_get_user();
600 // self::assertSelfUser($user);
601 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
602 // self::assertEquals('6fdbe8', $user['profile_link_color']);
603 // self::assertEquals('ededed', $user['profile_background_color']);
607 * Test the api_get_user() function with a Frio schema.
611 public function testApiGetUserWithFrioSchema()
613 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
614 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
615 // $user = api_get_user();
616 // self::assertSelfUser($user);
617 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
618 // self::assertEquals('6fdbe8', $user['profile_link_color']);
619 // self::assertEquals('ededed', $user['profile_background_color']);
623 * Test the api_get_user() function with an empty Frio schema.
627 public function testApiGetUserWithEmptyFrioSchema()
629 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
630 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
631 // $user = api_get_user();
632 // self::assertSelfUser($user);
633 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
634 // self::assertEquals('6fdbe8', $user['profile_link_color']);
635 // self::assertEquals('ededed', $user['profile_background_color']);
639 * Test the api_get_user() function with a custom Frio schema.
643 public function testApiGetUserWithCustomFrioSchema()
645 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
646 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
647 // $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
648 // $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
649 // $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
650 // $user = api_get_user();
651 // self::assertSelfUser($user);
652 // self::assertEquals('123456', $user['profile_sidebar_fill_color']);
653 // self::assertEquals('123456', $user['profile_link_color']);
654 // self::assertEquals('123456', $user['profile_background_color']);
658 * Test the api_get_user() function with an user that is not allowed to use the API.
660 * @runInSeparateProcess
661 * @preserveGlobalState disabled
663 public function testApiGetUserWithoutApiUser()
665 // api_get_user() with empty parameters is not used anymore
667 $_SERVER['PHP_AUTH_USER'] = 'Test user';
668 $_SERVER['PHP_AUTH_PW'] = 'password';
669 BasicAuth::setCurrentUserID();
670 self::assertFalse(api_get_user());
675 * Test the api_get_user() function with an user ID in a GET parameter.
679 public function testApiGetUserWithGetId()
681 // self::assertOtherUser(api_get_user());
685 * Test the api_get_user() function with a wrong user ID in a GET parameter.
689 public function testApiGetUserWithWrongGetId()
691 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
692 // self::assertOtherUser(api_get_user());
696 * Test the api_get_user() function with an user name in a GET parameter.
700 public function testApiGetUserWithGetName()
702 // self::assertSelfUser(api_get_user());
706 * Test the api_get_user() function with a profile URL in a GET parameter.
710 public function testApiGetUserWithGetUrl()
712 // self::assertSelfUser(api_get_user());
716 * Test the api_get_user() function with an user ID in the API path.
720 public function testApiGetUserWithNumericCalledApi()
722 // global $called_api;
723 // $called_api = ['api_path'];
724 // DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
725 // self::assertOtherUser(api_get_user());
729 * Test the api_get_user() function with the $called_api global variable.
733 public function testApiGetUserWithCalledApi()
735 // global $called_api;
736 // $called_api = ['api', 'api_path'];
737 // self::assertSelfUser(api_get_user());
741 * Test the Arrays::walkRecursive() function.
745 public function testApiWalkRecursive()
750 Arrays::walkRecursive(
753 // Should we test this with a callback that actually does something?
761 * Test the Arrays::walkRecursive() function with an array.
765 public function testApiWalkRecursiveWithArray()
767 $array = [['item1'], ['item2']];
770 Arrays::walkRecursive(
773 // Should we test this with a callback that actually does something?
781 * Test the BaseApi::reformatXML() function.
785 public function testApiReformatXml()
789 self::assertTrue(ApiResponse::reformatXML($item, $key));
790 self::assertEquals('true', $item);
794 * Test the BaseApi::reformatXML() function with a statusnet_api key.
798 public function testApiReformatXmlWithStatusnetKey()
801 $key = 'statusnet_api';
802 self::assertTrue(ApiResponse::reformatXML($item, $key));
803 self::assertEquals('statusnet:api', $key);
807 * Test the BaseApi::reformatXML() function with a friendica_api key.
811 public function testApiReformatXmlWithFriendicaKey()
814 $key = 'friendica_api';
815 self::assertTrue(ApiResponse::reformatXML($item, $key));
816 self::assertEquals('friendica:api', $key);
820 * Test the BaseApi::createXML() function.
824 public function testApiCreateXml()
827 '<?xml version="1.0"?>' . "\n" .
828 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
829 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
830 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
831 ' <data>some_data</data>' . "\n" .
832 '</root_element>' . "\n",
833 DI::apiResponse()->createXML(['data' => ['some_data']], 'root_element')
838 * Test the BaseApi::createXML() function without any XML namespace.
842 public function testApiCreateXmlWithoutNamespaces()
845 '<?xml version="1.0"?>' . "\n" .
847 ' <data>some_data</data>' . "\n" .
849 DI::apiResponse()->createXML(['data' => ['some_data']], 'ok')
854 * Test the BaseApi::formatData() function.
858 public function testApiFormatData()
860 $data = ['some_data'];
861 self::assertEquals($data, DI::apiResponse()->formatData('root_element', 'json', $data));
865 * Test the BaseApi::formatData() function with an XML result.
869 public function testApiFormatDataWithXml()
872 '<?xml version="1.0"?>' . "\n" .
873 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
874 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
875 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
876 ' <data>some_data</data>' . "\n" .
877 '</root_element>' . "\n",
878 DI::apiResponse()->formatData('root_element', 'xml', ['data' => ['some_data']])
883 * Test the api_account_verify_credentials() function.
887 public function testApiAccountVerifyCredentials()
889 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
893 * Test the api_account_verify_credentials() function without an authenticated user.
897 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
899 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
900 BasicAuth::setCurrentUserID();
901 $_SESSION['authenticated'] = false;
902 api_account_verify_credentials('json');
906 * Test the api_statuses_mediap() function.
910 public function testApiStatusesMediap()
912 DI::args()->setArgc(2);
920 'tmp_name' => $this->getTempImage(),
921 'name' => 'spacer.png',
922 'type' => 'image/png'
925 $_GET['status'] = '<b>Status content</b>';
927 $result = api_statuses_mediap('json');
928 self::assertStatus($result['status']);
932 * Test the api_statuses_mediap() function without an authenticated user.
936 public function testApiStatusesMediapWithoutAuthenticatedUser()
938 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
939 BasicAuth::setCurrentUserID();
940 $_SESSION['authenticated'] = false;
941 api_statuses_mediap('json');
945 * Test the api_statuses_update() function.
949 public function testApiStatusesUpdate()
951 $_GET['status'] = 'Status content #friendica';
952 $_GET['in_reply_to_status_id'] = -1;
961 'tmp_name' => $this->getTempImage(),
962 'name' => 'spacer.png',
963 'type' => 'image/png'
967 $result = api_statuses_update('json');
968 self::assertStatus($result['status']);
972 * Test the api_statuses_update() function with an HTML status.
976 public function testApiStatusesUpdateWithHtml()
978 $_GET['htmlstatus'] = '<b>Status content</b>';
980 $result = api_statuses_update('json');
981 self::assertStatus($result['status']);
985 * Test the api_statuses_update() function without an authenticated user.
989 public function testApiStatusesUpdateWithoutAuthenticatedUser()
991 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
992 BasicAuth::setCurrentUserID();
993 $_SESSION['authenticated'] = false;
994 api_statuses_update('json');
998 * Test the api_statuses_update() function with a parent status.
1002 public function testApiStatusesUpdateWithParent()
1004 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1008 * Test the api_statuses_update() function with a media_ids parameter.
1012 public function testApiStatusesUpdateWithMediaIds()
1014 $this->markTestIncomplete();
1018 * Test the api_statuses_update() function with the throttle limit reached.
1022 public function testApiStatusesUpdateWithDayThrottleReached()
1024 $this->markTestIncomplete();
1028 * Test the api_media_upload() function.
1029 * @runInSeparateProcess
1030 * @preserveGlobalState disabled
1032 public function testApiMediaUpload()
1034 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1039 * Test the api_media_upload() function without an authenticated user.
1043 public function testApiMediaUploadWithoutAuthenticatedUser()
1045 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1046 BasicAuth::setCurrentUserID();
1047 $_SESSION['authenticated'] = false;
1052 * Test the api_media_upload() function with an invalid uploaded media.
1056 public function testApiMediaUploadWithMedia()
1058 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1062 'tmp_name' => 'tmp_name'
1069 * Test the api_media_upload() function with an valid uploaded media.
1073 public function testApiMediaUploadWithValidMedia()
1081 'tmp_name' => $this->getTempImage(),
1082 'name' => 'spacer.png',
1083 'type' => 'image/png'
1087 DI::args()->setArgc(2);
1089 $result = api_media_upload();
1090 self::assertEquals('image/png', $result['media']['image']['image_type']);
1091 self::assertEquals(1, $result['media']['image']['w']);
1092 self::assertEquals(1, $result['media']['image']['h']);
1093 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1097 * Test the api_status_show() function.
1099 public function testApiStatusShowWithJson()
1101 // $result = api_status_show('json', 1);
1102 // self::assertStatus($result['status']);
1106 * Test the api_status_show() function with an XML result.
1108 public function testApiStatusShowWithXml()
1110 // $result = api_status_show('xml', 1);
1111 // self::assertXml($result, 'statuses');
1115 * Test the api_get_last_status() function
1117 public function testApiGetLastStatus()
1119 // $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1120 // self::assertNotNull($item);
1124 * Test the api_users_show() function.
1128 public function testApiUsersShow()
1130 $result = api_users_show('json');
1131 // We can't use assertSelfUser() here because the user object is missing some properties.
1132 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1133 self::assertEquals('DFRN', $result['user']['location']);
1134 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1135 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1136 self::assertTrue($result['user']['verified']);
1140 * Test the api_users_show() function with an XML result.
1144 public function testApiUsersShowWithXml()
1146 $result = api_users_show('xml');
1147 self::assertXml($result, 'statuses');
1151 * Test the api_users_search() function.
1155 public function testApiUsersSearch()
1157 $_GET['q'] = 'othercontact';
1158 $result = api_users_search('json');
1159 self::assertOtherUser($result['users'][0]);
1163 * Test the api_users_search() function with an XML result.
1167 public function testApiUsersSearchWithXml()
1169 $_GET['q'] = 'othercontact';
1170 $result = api_users_search('xml');
1171 self::assertXml($result, 'users');
1175 * Test the api_users_search() function without a GET q parameter.
1179 public function testApiUsersSearchWithoutQuery()
1181 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1182 api_users_search('json');
1186 * Test the api_users_lookup() function.
1190 public function testApiUsersLookup()
1192 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1193 api_users_lookup('json');
1197 * Test the api_users_lookup() function with an user ID.
1201 public function testApiUsersLookupWithUserId()
1203 $_REQUEST['user_id'] = $this->otherUser['id'];
1204 $result = api_users_lookup('json');
1205 self::assertOtherUser($result['users'][0]);
1209 * Test the api_search() function.
1213 public function testApiSearch()
1215 $_REQUEST['q'] = 'reply';
1216 $_REQUEST['max_id'] = 10;
1217 $result = api_search('json');
1218 foreach ($result['status'] as $status) {
1219 self::assertStatus($status);
1220 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1225 * Test the api_search() function a count parameter.
1229 public function testApiSearchWithCount()
1231 $_REQUEST['q'] = 'reply';
1232 $_REQUEST['count'] = 20;
1233 $result = api_search('json');
1234 foreach ($result['status'] as $status) {
1235 self::assertStatus($status);
1236 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1241 * Test the api_search() function with an rpp parameter.
1245 public function testApiSearchWithRpp()
1247 $_REQUEST['q'] = 'reply';
1248 $_REQUEST['rpp'] = 20;
1249 $result = api_search('json');
1250 foreach ($result['status'] as $status) {
1251 self::assertStatus($status);
1252 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1257 * Test the api_search() function with an q parameter contains hashtag.
1258 * @doesNotPerformAssertions
1260 public function testApiSearchWithHashtag()
1262 $_REQUEST['q'] = '%23friendica';
1263 $result = api_search('json');
1264 foreach ($result['status'] as $status) {
1265 self::assertStatus($status);
1266 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1271 * Test the api_search() function with an exclude_replies parameter.
1272 * @doesNotPerformAssertions
1274 public function testApiSearchWithExcludeReplies()
1276 $_REQUEST['max_id'] = 10;
1277 $_REQUEST['exclude_replies'] = true;
1278 $_REQUEST['q'] = 'friendica';
1279 $result = api_search('json');
1280 foreach ($result['status'] as $status) {
1281 self::assertStatus($status);
1286 * Test the api_search() function without an authenticated user.
1290 public function testApiSearchWithUnallowedUser()
1292 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1293 BasicAuth::setCurrentUserID();
1298 * Test the api_search() function without any GET query parameter.
1302 public function testApiSearchWithoutQuery()
1304 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1309 * Test the api_statuses_home_timeline() function.
1313 public function testApiStatusesHomeTimeline()
1315 $_REQUEST['max_id'] = 10;
1316 $_REQUEST['exclude_replies'] = true;
1317 $_REQUEST['conversation_id'] = 1;
1318 $result = api_statuses_home_timeline('json');
1319 self::assertNotEmpty($result['status']);
1320 foreach ($result['status'] as $status) {
1321 self::assertStatus($status);
1326 * Test the api_statuses_home_timeline() function with a negative page parameter.
1330 public function testApiStatusesHomeTimelineWithNegativePage()
1332 $_REQUEST['page'] = -2;
1333 $result = api_statuses_home_timeline('json');
1334 self::assertNotEmpty($result['status']);
1335 foreach ($result['status'] as $status) {
1336 self::assertStatus($status);
1341 * Test the api_statuses_home_timeline() with an unallowed user.
1345 public function testApiStatusesHomeTimelineWithUnallowedUser()
1347 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1348 BasicAuth::setCurrentUserID();
1349 api_statuses_home_timeline('json');
1353 * Test the api_statuses_home_timeline() function with an RSS result.
1357 public function testApiStatusesHomeTimelineWithRss()
1359 $result = api_statuses_home_timeline('rss');
1360 self::assertXml($result, 'statuses');
1364 * Test the api_statuses_public_timeline() function.
1368 public function testApiStatusesPublicTimeline()
1370 $_REQUEST['max_id'] = 10;
1371 $_REQUEST['conversation_id'] = 1;
1372 $result = api_statuses_public_timeline('json');
1373 self::assertNotEmpty($result['status']);
1374 foreach ($result['status'] as $status) {
1375 self::assertStatus($status);
1380 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1384 public function testApiStatusesPublicTimelineWithExcludeReplies()
1386 $_REQUEST['max_id'] = 10;
1387 $_REQUEST['exclude_replies'] = true;
1388 $result = api_statuses_public_timeline('json');
1389 self::assertNotEmpty($result['status']);
1390 foreach ($result['status'] as $status) {
1391 self::assertStatus($status);
1396 * Test the api_statuses_public_timeline() function with a negative page parameter.
1400 public function testApiStatusesPublicTimelineWithNegativePage()
1402 $_REQUEST['page'] = -2;
1403 $result = api_statuses_public_timeline('json');
1404 self::assertNotEmpty($result['status']);
1405 foreach ($result['status'] as $status) {
1406 self::assertStatus($status);
1411 * Test the api_statuses_public_timeline() function with an unallowed user.
1415 public function testApiStatusesPublicTimelineWithUnallowedUser()
1417 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1418 BasicAuth::setCurrentUserID();
1419 api_statuses_public_timeline('json');
1423 * Test the api_statuses_public_timeline() function with an RSS result.
1427 public function testApiStatusesPublicTimelineWithRss()
1429 $result = api_statuses_public_timeline('rss');
1430 self::assertXml($result, 'statuses');
1434 * Test the api_statuses_networkpublic_timeline() function.
1438 public function testApiStatusesNetworkpublicTimeline()
1440 $_REQUEST['max_id'] = 10;
1441 $result = api_statuses_networkpublic_timeline('json');
1442 self::assertNotEmpty($result['status']);
1443 foreach ($result['status'] as $status) {
1444 self::assertStatus($status);
1449 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1453 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1455 $_REQUEST['page'] = -2;
1456 $result = api_statuses_networkpublic_timeline('json');
1457 self::assertNotEmpty($result['status']);
1458 foreach ($result['status'] as $status) {
1459 self::assertStatus($status);
1464 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1468 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1470 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1471 BasicAuth::setCurrentUserID();
1472 api_statuses_networkpublic_timeline('json');
1476 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1480 public function testApiStatusesNetworkpublicTimelineWithRss()
1482 $result = api_statuses_networkpublic_timeline('rss');
1483 self::assertXml($result, 'statuses');
1487 * Test the api_statuses_show() function.
1491 public function testApiStatusesShow()
1493 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1494 api_statuses_show('json');
1498 * Test the api_statuses_show() function with an ID.
1502 public function testApiStatusesShowWithId()
1504 DI::args()->setArgv(['', '', '', 1]);
1505 $result = api_statuses_show('json');
1506 self::assertStatus($result['status']);
1510 * Test the api_statuses_show() function with the conversation parameter.
1514 public function testApiStatusesShowWithConversation()
1516 DI::args()->setArgv(['', '', '', 1]);
1517 $_REQUEST['conversation'] = 1;
1518 $result = api_statuses_show('json');
1519 self::assertNotEmpty($result['status']);
1520 foreach ($result['status'] as $status) {
1521 self::assertStatus($status);
1526 * Test the api_statuses_show() function with an unallowed user.
1530 public function testApiStatusesShowWithUnallowedUser()
1532 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1533 BasicAuth::setCurrentUserID();
1534 api_statuses_show('json');
1538 * Test the api_conversation_show() function.
1542 public function testApiConversationShow()
1544 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1545 api_conversation_show('json');
1549 * Test the api_conversation_show() function with an ID.
1553 public function testApiConversationShowWithId()
1555 DI::args()->setArgv(['', '', '', 1]);
1556 $_REQUEST['max_id'] = 10;
1557 $_REQUEST['page'] = -2;
1558 $result = api_conversation_show('json');
1559 self::assertNotEmpty($result['status']);
1560 foreach ($result['status'] as $status) {
1561 self::assertStatus($status);
1566 * Test the api_conversation_show() function with an unallowed user.
1570 public function testApiConversationShowWithUnallowedUser()
1572 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1573 BasicAuth::setCurrentUserID();
1574 api_conversation_show('json');
1578 * Test the api_statuses_repeat() function.
1582 public function testApiStatusesRepeat()
1584 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1585 api_statuses_repeat('json');
1589 * Test the api_statuses_repeat() function without an authenticated user.
1593 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1595 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1596 BasicAuth::setCurrentUserID();
1597 $_SESSION['authenticated'] = false;
1598 api_statuses_repeat('json');
1602 * Test the api_statuses_repeat() function with an ID.
1606 public function testApiStatusesRepeatWithId()
1608 DI::args()->setArgv(['', '', '', 1]);
1609 $result = api_statuses_repeat('json');
1610 self::assertStatus($result['status']);
1612 // Also test with a shared status
1613 DI::args()->setArgv(['', '', '', 5]);
1614 $result = api_statuses_repeat('json');
1615 self::assertStatus($result['status']);
1619 * Test the api_statuses_destroy() function.
1623 public function testApiStatusesDestroy()
1625 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1626 api_statuses_destroy('json');
1630 * Test the api_statuses_destroy() function without an authenticated user.
1634 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1636 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1637 BasicAuth::setCurrentUserID();
1638 $_SESSION['authenticated'] = false;
1639 api_statuses_destroy('json');
1643 * Test the api_statuses_destroy() function with an ID.
1647 public function testApiStatusesDestroyWithId()
1649 DI::args()->setArgv(['', '', '', 1]);
1650 $result = api_statuses_destroy('json');
1651 self::assertStatus($result['status']);
1655 * Test the api_statuses_mentions() function.
1659 public function testApiStatusesMentions()
1661 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1662 $_REQUEST['max_id'] = 10;
1663 $result = api_statuses_mentions('json');
1664 self::assertEmpty($result['status']);
1665 // We should test with mentions in the database.
1669 * Test the api_statuses_mentions() function with a negative page parameter.
1673 public function testApiStatusesMentionsWithNegativePage()
1675 $_REQUEST['page'] = -2;
1676 $result = api_statuses_mentions('json');
1677 self::assertEmpty($result['status']);
1681 * Test the api_statuses_mentions() function with an unallowed user.
1685 public function testApiStatusesMentionsWithUnallowedUser()
1687 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1688 BasicAuth::setCurrentUserID();
1689 api_statuses_mentions('json');
1693 * Test the api_statuses_mentions() function with an RSS result.
1697 public function testApiStatusesMentionsWithRss()
1699 $result = api_statuses_mentions('rss');
1700 self::assertXml($result, 'statuses');
1704 * Test the api_statuses_user_timeline() function.
1708 public function testApiStatusesUserTimeline()
1710 $_REQUEST['user_id'] = 42;
1711 $_REQUEST['max_id'] = 10;
1712 $_REQUEST['exclude_replies'] = true;
1713 $_REQUEST['conversation_id'] = 7;
1715 $result = api_statuses_user_timeline('json');
1716 self::assertNotEmpty($result['status']);
1717 foreach ($result['status'] as $status) {
1718 self::assertStatus($status);
1723 * Test the api_statuses_user_timeline() function with a negative page parameter.
1727 public function testApiStatusesUserTimelineWithNegativePage()
1729 $_REQUEST['user_id'] = 42;
1730 $_REQUEST['page'] = -2;
1732 $result = api_statuses_user_timeline('json');
1733 self::assertNotEmpty($result['status']);
1734 foreach ($result['status'] as $status) {
1735 self::assertStatus($status);
1740 * Test the api_statuses_user_timeline() function with an RSS result.
1744 public function testApiStatusesUserTimelineWithRss()
1746 $result = api_statuses_user_timeline('rss');
1747 self::assertXml($result, 'statuses');
1751 * Test the api_statuses_user_timeline() function with an unallowed user.
1755 public function testApiStatusesUserTimelineWithUnallowedUser()
1757 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1758 BasicAuth::setCurrentUserID();
1759 api_statuses_user_timeline('json');
1763 * Test the api_favorites_create_destroy() function.
1767 public function testApiFavoritesCreateDestroy()
1769 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1770 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1771 api_favorites_create_destroy('json');
1775 * Test the api_favorites_create_destroy() function with an invalid ID.
1779 public function testApiFavoritesCreateDestroyWithInvalidId()
1781 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1782 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1783 api_favorites_create_destroy('json');
1787 * Test the api_favorites_create_destroy() function with an invalid action.
1791 public function testApiFavoritesCreateDestroyWithInvalidAction()
1793 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1794 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1795 $_REQUEST['id'] = 1;
1796 api_favorites_create_destroy('json');
1800 * Test the api_favorites_create_destroy() function with the create action.
1804 public function testApiFavoritesCreateDestroyWithCreateAction()
1806 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1807 $_REQUEST['id'] = 3;
1808 $result = api_favorites_create_destroy('json');
1809 self::assertStatus($result['status']);
1813 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1817 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1819 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1820 $_REQUEST['id'] = 3;
1821 $result = api_favorites_create_destroy('rss');
1822 self::assertXml($result, 'status');
1826 * Test the api_favorites_create_destroy() function with the destroy action.
1830 public function testApiFavoritesCreateDestroyWithDestroyAction()
1832 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1833 $_REQUEST['id'] = 3;
1834 $result = api_favorites_create_destroy('json');
1835 self::assertStatus($result['status']);
1839 * Test the api_favorites_create_destroy() function without an authenticated user.
1843 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1845 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1846 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1847 BasicAuth::setCurrentUserID();
1848 $_SESSION['authenticated'] = false;
1849 api_favorites_create_destroy('json');
1853 * Test the api_favorites() function.
1857 public function testApiFavorites()
1859 $_REQUEST['page'] = -1;
1860 $_REQUEST['max_id'] = 10;
1861 $result = api_favorites('json');
1862 foreach ($result['status'] as $status) {
1863 self::assertStatus($status);
1868 * Test the api_favorites() function with an RSS result.
1872 public function testApiFavoritesWithRss()
1874 $result = api_favorites('rss');
1875 self::assertXml($result, 'statuses');
1879 * Test the api_favorites() function with an unallowed user.
1883 public function testApiFavoritesWithUnallowedUser()
1885 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1886 BasicAuth::setCurrentUserID();
1887 api_favorites('json');
1891 * Test the api_format_messages() function.
1895 public function testApiFormatMessages()
1897 $result = api_format_messages(
1898 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1899 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1900 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1902 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1903 self::assertEquals(1, $result['id']);
1904 self::assertEquals(2, $result['recipient_id']);
1905 self::assertEquals(3, $result['sender_id']);
1906 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1907 self::assertEquals('sender_name', $result['sender_screen_name']);
1911 * Test the api_format_messages() function with HTML.
1915 public function testApiFormatMessagesWithHtmlText()
1917 $_GET['getText'] = 'html';
1918 $result = api_format_messages(
1919 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1920 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1921 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1923 self::assertEquals('item_title', $result['title']);
1924 self::assertEquals('<strong>item_body</strong>', $result['text']);
1928 * Test the api_format_messages() function with plain text.
1932 public function testApiFormatMessagesWithPlainText()
1934 $_GET['getText'] = 'plain';
1935 $result = api_format_messages(
1936 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1937 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1938 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1940 self::assertEquals('item_title', $result['title']);
1941 self::assertEquals('item_body', $result['text']);
1945 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1949 public function testApiFormatMessagesWithoutUserObjects()
1951 $_GET['getUserObjects'] = 'false';
1952 $result = api_format_messages(
1953 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1954 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1955 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1957 self::assertTrue(!isset($result['sender']));
1958 self::assertTrue(!isset($result['recipient']));
1962 * Test the api_convert_item() function.
1966 public function testApiConvertItem()
1969 $result = api_convert_item(
1971 'network' => 'feed',
1972 'title' => 'item_title',
1974 // We need a long string to test that it is correctly cut
1975 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
1976 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
1977 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
1978 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
1979 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
1980 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
1981 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
1982 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
1983 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
1984 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
1985 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
1986 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
1987 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
1988 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
1989 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
1990 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
1991 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
1992 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
1993 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
1994 'repellat officia illum quos impedit quam iste esse unde qui ' .
1995 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
1996 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
1997 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
1998 'plink' => 'item_plink'
2001 self::assertStringStartsWith('item_title', $result['text']);
2002 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2007 * Test the api_convert_item() function with an empty item body.
2011 public function testApiConvertItemWithoutBody()
2014 $result = api_convert_item(
2016 'network' => 'feed',
2017 'title' => 'item_title',
2020 'plink' => 'item_plink'
2023 self::assertEquals("item_title", $result['text']);
2024 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2029 * Test the api_convert_item() function with the title in the body.
2033 public function testApiConvertItemWithTitleInBody()
2036 $result = api_convert_item(
2038 'title' => 'item_title',
2039 'body' => 'item_title item_body',
2043 self::assertEquals('item_title item_body', $result['text']);
2044 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2049 * Test the api_get_attachments() function.
2053 public function testApiGetAttachments()
2056 // self::assertEmpty(api_get_attachments($body, 0));
2060 * Test the api_get_attachments() function with an img tag.
2064 public function testApiGetAttachmentsWithImage()
2066 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2067 // self::assertIsArray(api_get_attachments($body, 0));
2071 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2075 public function testApiGetAttachmentsWithImageAndAndStatus()
2077 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2078 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2079 // self::assertIsArray(api_get_attachments($body, 0));
2083 * Test the api_get_entitities() function.
2087 public function testApiGetEntitities()
2090 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2094 * Test the api_get_entitities() function with the include_entities parameter.
2098 public function testApiGetEntititiesWithIncludeEntities()
2101 $_REQUEST['include_entities'] = 'true';
2103 $result = api_get_entitities($text, 'bbcode', 0);
2104 self::assertIsArray($result['hashtags']);
2105 self::assertIsArray($result['symbols']);
2106 self::assertIsArray($result['urls']);
2107 self::assertIsArray($result['user_mentions']);
2112 * Test the api_format_items_embeded_images() function.
2116 public function testApiFormatItemsEmbededImages()
2120 'text ' . DI::baseUrl() . '/display/item_guid',
2121 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2127 * Test the api_format_items_activities() function.
2131 public function testApiFormatItemsActivities()
2133 $item = ['uid' => 0, 'uri-id' => 1];
2134 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
2135 self::assertArrayHasKey('like', $result);
2136 self::assertArrayHasKey('dislike', $result);
2137 self::assertArrayHasKey('attendyes', $result);
2138 self::assertArrayHasKey('attendno', $result);
2139 self::assertArrayHasKey('attendmaybe', $result);
2143 * Test the api_format_items_activities() function with an XML result.
2147 public function testApiFormatItemsActivitiesWithXml()
2149 $item = ['uid' => 0, 'uri-id' => 1];
2150 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
2151 self::assertArrayHasKey('friendica:like', $result);
2152 self::assertArrayHasKey('friendica:dislike', $result);
2153 self::assertArrayHasKey('friendica:attendyes', $result);
2154 self::assertArrayHasKey('friendica:attendno', $result);
2155 self::assertArrayHasKey('friendica:attendmaybe', $result);
2159 * Test the api_format_items() function.
2160 * @doesNotPerformAssertions
2162 public function testApiFormatItems()
2165 $items = Post::selectToArray([], ['uid' => 42]);
2166 foreach ($items as $item) {
2167 $status = api_format_item($item);
2168 self::assertStatus($status);
2174 * Test the api_format_items() function with an XML result.
2175 * @doesNotPerformAssertions
2177 public function testApiFormatItemsWithXml()
2180 $items = Post::selectToArray([], ['uid' => 42]);
2181 foreach ($items as $item) {
2182 $status = api_format_item($item, 'xml');
2183 self::assertStatus($status);
2189 * Test the api_lists_list() function.
2193 public function testApiListsList()
2195 $result = api_lists_list('json');
2196 self::assertEquals(['lists_list' => []], $result);
2200 * Test the api_lists_ownerships() function.
2204 public function testApiListsOwnerships()
2206 $result = api_lists_ownerships('json');
2207 foreach ($result['lists']['lists'] as $list) {
2208 self::assertList($list);
2213 * Test the api_lists_ownerships() function without an authenticated user.
2217 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2219 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2220 BasicAuth::setCurrentUserID();
2221 $_SESSION['authenticated'] = false;
2222 api_lists_ownerships('json');
2226 * Test the api_lists_statuses() function.
2230 public function testApiListsStatuses()
2232 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2233 api_lists_statuses('json');
2237 * Test the api_lists_statuses() function with a list ID.
2238 * @doesNotPerformAssertions
2240 public function testApiListsStatusesWithListId()
2242 $_REQUEST['list_id'] = 1;
2243 $_REQUEST['page'] = -1;
2244 $_REQUEST['max_id'] = 10;
2245 $result = api_lists_statuses('json');
2246 foreach ($result['status'] as $status) {
2247 self::assertStatus($status);
2252 * Test the api_lists_statuses() function with a list ID and a RSS result.
2256 public function testApiListsStatusesWithListIdAndRss()
2258 $_REQUEST['list_id'] = 1;
2259 $result = api_lists_statuses('rss');
2260 self::assertXml($result, 'statuses');
2264 * Test the api_lists_statuses() function with an unallowed user.
2268 public function testApiListsStatusesWithUnallowedUser()
2270 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2271 BasicAuth::setCurrentUserID();
2272 api_lists_statuses('json');
2276 * Test the api_statuses_f() function.
2280 public function testApiStatusesFWithFriends()
2283 $result = api_statuses_f('friends');
2284 self::assertArrayHasKey('user', $result);
2288 * Test the api_statuses_f() function.
2292 public function testApiStatusesFWithFollowers()
2294 $result = api_statuses_f('followers');
2295 self::assertArrayHasKey('user', $result);
2299 * Test the api_statuses_f() function.
2303 public function testApiStatusesFWithBlocks()
2305 $result = api_statuses_f('blocks');
2306 self::assertArrayHasKey('user', $result);
2310 * Test the api_statuses_f() function.
2314 public function testApiStatusesFWithIncoming()
2316 $result = api_statuses_f('incoming');
2317 self::assertArrayHasKey('user', $result);
2321 * Test the api_statuses_f() function an undefined cursor GET variable.
2325 public function testApiStatusesFWithUndefinedCursor()
2327 $_GET['cursor'] = 'undefined';
2328 self::assertFalse(api_statuses_f('friends'));
2332 * Test the api_statuses_friends() function.
2336 public function testApiStatusesFriends()
2338 $result = api_statuses_friends('json');
2339 self::assertArrayHasKey('user', $result);
2343 * Test the api_statuses_friends() function an undefined cursor GET variable.
2347 public function testApiStatusesFriendsWithUndefinedCursor()
2349 $_GET['cursor'] = 'undefined';
2350 self::assertFalse(api_statuses_friends('json'));
2354 * Test the api_statuses_followers() function.
2358 public function testApiStatusesFollowers()
2360 $result = api_statuses_followers('json');
2361 self::assertArrayHasKey('user', $result);
2365 * Test the api_statuses_followers() function an undefined cursor GET variable.
2369 public function testApiStatusesFollowersWithUndefinedCursor()
2371 $_GET['cursor'] = 'undefined';
2372 self::assertFalse(api_statuses_followers('json'));
2376 * Test the api_blocks_list() function.
2380 public function testApiBlocksList()
2382 $result = api_blocks_list('json');
2383 self::assertArrayHasKey('user', $result);
2387 * Test the api_blocks_list() function an undefined cursor GET variable.
2391 public function testApiBlocksListWithUndefinedCursor()
2393 $_GET['cursor'] = 'undefined';
2394 self::assertFalse(api_blocks_list('json'));
2398 * Test the api_friendships_incoming() function.
2402 public function testApiFriendshipsIncoming()
2404 $result = api_friendships_incoming('json');
2405 self::assertArrayHasKey('id', $result);
2409 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2413 public function testApiFriendshipsIncomingWithUndefinedCursor()
2415 $_GET['cursor'] = 'undefined';
2416 self::assertFalse(api_friendships_incoming('json'));
2420 * Test the api_statusnet_config() function.
2424 public function testApiStatusnetConfig()
2427 $result = api_statusnet_config('json');
2428 self::assertEquals('localhost', $result['config']['site']['server']);
2429 self::assertEquals('default', $result['config']['site']['theme']);
2430 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2431 self::assertTrue($result['config']['site']['fancy']);
2432 self::assertEquals('en', $result['config']['site']['language']);
2433 self::assertEquals('UTC', $result['config']['site']['timezone']);
2434 self::assertEquals(200000, $result['config']['site']['textlimit']);
2435 self::assertEquals('false', $result['config']['site']['private']);
2436 self::assertEquals('false', $result['config']['site']['ssl']);
2437 self::assertEquals(30, $result['config']['site']['shorturllength']);
2442 * Test the api_direct_messages_new() function.
2446 public function testApiDirectMessagesNew()
2448 $result = api_direct_messages_new('json');
2449 self::assertNull($result);
2453 * Test the api_direct_messages_new() function without an authenticated user.
2457 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2459 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2460 BasicAuth::setCurrentUserID();
2461 $_SESSION['authenticated'] = false;
2462 api_direct_messages_new('json');
2466 * Test the api_direct_messages_new() function with an user ID.
2470 public function testApiDirectMessagesNewWithUserId()
2472 $_POST['text'] = 'message_text';
2473 $_POST['user_id'] = $this->otherUser['id'];
2474 $result = api_direct_messages_new('json');
2475 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2479 * Test the api_direct_messages_new() function with a screen name.
2483 public function testApiDirectMessagesNewWithScreenName()
2485 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2486 $_POST['text'] = 'message_text';
2487 $_POST['user_id'] = $this->friendUser['id'];
2488 $result = api_direct_messages_new('json');
2489 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2490 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2491 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2495 * Test the api_direct_messages_new() function with a title.
2499 public function testApiDirectMessagesNewWithTitle()
2501 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2502 $_POST['text'] = 'message_text';
2503 $_POST['user_id'] = $this->friendUser['id'];
2504 $_REQUEST['title'] = 'message_title';
2505 $result = api_direct_messages_new('json');
2506 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2507 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2508 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2509 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2513 * Test the api_direct_messages_new() function with an RSS result.
2517 public function testApiDirectMessagesNewWithRss()
2519 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2520 $_POST['text'] = 'message_text';
2521 $_POST['user_id'] = $this->friendUser['id'];
2522 $result = api_direct_messages_new('rss');
2523 self::assertXml($result, 'direct-messages');
2527 * Test the api_direct_messages_destroy() function.
2531 public function testApiDirectMessagesDestroy()
2533 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2534 api_direct_messages_destroy('json');
2538 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2542 public function testApiDirectMessagesDestroyWithVerbose()
2544 $_GET['friendica_verbose'] = 'true';
2545 $result = api_direct_messages_destroy('json');
2549 'result' => 'error',
2550 'message' => 'message id or parenturi not specified'
2558 * Test the api_direct_messages_destroy() function without an authenticated user.
2562 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2564 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2565 BasicAuth::setCurrentUserID();
2566 $_SESSION['authenticated'] = false;
2567 api_direct_messages_destroy('json');
2571 * Test the api_direct_messages_destroy() function with a non-zero ID.
2575 public function testApiDirectMessagesDestroyWithId()
2577 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2578 $_REQUEST['id'] = 1;
2579 api_direct_messages_destroy('json');
2583 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2587 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2589 $_REQUEST['id'] = 1;
2590 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2591 $_GET['friendica_verbose'] = 'true';
2592 $result = api_direct_messages_destroy('json');
2596 'result' => 'error',
2597 'message' => 'message id not in database'
2605 * Test the api_direct_messages_destroy() function with a non-zero ID.
2609 public function testApiDirectMessagesDestroyWithCorrectId()
2611 $this->markTestIncomplete('We need to add a dataset for this.');
2615 * Test the api_direct_messages_box() function.
2619 public function testApiDirectMessagesBoxWithSentbox()
2621 $_REQUEST['page'] = -1;
2622 $_REQUEST['max_id'] = 10;
2623 $result = api_direct_messages_box('json', 'sentbox', 'false');
2624 self::assertArrayHasKey('direct_message', $result);
2628 * Test the api_direct_messages_box() function.
2632 public function testApiDirectMessagesBoxWithConversation()
2634 $result = api_direct_messages_box('json', 'conversation', 'false');
2635 self::assertArrayHasKey('direct_message', $result);
2639 * Test the api_direct_messages_box() function.
2643 public function testApiDirectMessagesBoxWithAll()
2645 $result = api_direct_messages_box('json', 'all', 'false');
2646 self::assertArrayHasKey('direct_message', $result);
2650 * Test the api_direct_messages_box() function.
2654 public function testApiDirectMessagesBoxWithInbox()
2656 $result = api_direct_messages_box('json', 'inbox', 'false');
2657 self::assertArrayHasKey('direct_message', $result);
2661 * Test the api_direct_messages_box() function.
2665 public function testApiDirectMessagesBoxWithVerbose()
2667 $result = api_direct_messages_box('json', 'sentbox', 'true');
2671 'result' => 'error',
2672 'message' => 'no mails available'
2680 * Test the api_direct_messages_box() function with a RSS result.
2684 public function testApiDirectMessagesBoxWithRss()
2686 $result = api_direct_messages_box('rss', 'sentbox', 'false');
2687 self::assertXml($result, 'direct-messages');
2691 * Test the api_direct_messages_box() function without an authenticated user.
2695 public function testApiDirectMessagesBoxWithUnallowedUser()
2697 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2698 BasicAuth::setCurrentUserID();
2699 api_direct_messages_box('json', 'sentbox', 'false');
2703 * Test the api_direct_messages_sentbox() function.
2707 public function testApiDirectMessagesSentbox()
2709 $result = api_direct_messages_sentbox('json');
2710 self::assertArrayHasKey('direct_message', $result);
2714 * Test the api_direct_messages_inbox() function.
2718 public function testApiDirectMessagesInbox()
2720 $result = api_direct_messages_inbox('json');
2721 self::assertArrayHasKey('direct_message', $result);
2725 * Test the api_direct_messages_all() function.
2729 public function testApiDirectMessagesAll()
2731 $result = api_direct_messages_all('json');
2732 self::assertArrayHasKey('direct_message', $result);
2736 * Test the api_direct_messages_conversation() function.
2740 public function testApiDirectMessagesConversation()
2742 $result = api_direct_messages_conversation('json');
2743 self::assertArrayHasKey('direct_message', $result);
2747 * Test the api_oauth_request_token() function.
2751 public function testApiOauthRequestToken()
2753 $this->markTestIncomplete('exit() kills phpunit as well');
2757 * Test the api_oauth_access_token() function.
2761 public function testApiOauthAccessToken()
2763 $this->markTestIncomplete('exit() kills phpunit as well');
2767 * Test the api_fr_photos_list() function.
2771 public function testApiFrPhotosList()
2773 $result = api_fr_photos_list('json');
2774 self::assertArrayHasKey('photo', $result);
2778 * Test the api_fr_photos_list() function without an authenticated user.
2782 public function testApiFrPhotosListWithoutAuthenticatedUser()
2784 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2785 BasicAuth::setCurrentUserID();
2786 $_SESSION['authenticated'] = false;
2787 api_fr_photos_list('json');
2791 * Test the api_fr_photo_create_update() function.
2793 public function testApiFrPhotoCreateUpdate()
2795 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2796 api_fr_photo_create_update('json');
2800 * Test the api_fr_photo_create_update() function without an authenticated user.
2804 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
2806 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2807 BasicAuth::setCurrentUserID();
2808 $_SESSION['authenticated'] = false;
2809 api_fr_photo_create_update('json');
2813 * Test the api_fr_photo_create_update() function with an album name.
2817 public function testApiFrPhotoCreateUpdateWithAlbum()
2819 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2820 $_REQUEST['album'] = 'album_name';
2821 api_fr_photo_create_update('json');
2825 * Test the api_fr_photo_create_update() function with the update mode.
2829 public function testApiFrPhotoCreateUpdateWithUpdate()
2831 $this->markTestIncomplete('We need to create a dataset for this');
2835 * Test the api_fr_photo_create_update() function with an uploaded file.
2839 public function testApiFrPhotoCreateUpdateWithFile()
2841 $this->markTestIncomplete();
2845 * Test the api_fr_photo_detail() function.
2849 public function testApiFrPhotoDetail()
2851 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2852 api_fr_photo_detail('json');
2856 * Test the api_fr_photo_detail() function without an authenticated user.
2860 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
2862 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2863 BasicAuth::setCurrentUserID();
2864 $_SESSION['authenticated'] = false;
2865 api_fr_photo_detail('json');
2869 * Test the api_fr_photo_detail() function with a photo ID.
2873 public function testApiFrPhotoDetailWithPhotoId()
2875 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
2876 $_REQUEST['photo_id'] = 1;
2877 api_fr_photo_detail('json');
2881 * Test the api_fr_photo_detail() function with a correct photo ID.
2885 public function testApiFrPhotoDetailCorrectPhotoId()
2887 $this->markTestIncomplete('We need to create a dataset for this.');
2891 * Test the api_account_update_profile_image() function.
2895 public function testApiAccountUpdateProfileImage()
2897 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2898 api_account_update_profile_image('json');
2902 * Test the api_account_update_profile_image() function without an authenticated user.
2906 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2908 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2909 BasicAuth::setCurrentUserID();
2910 $_SESSION['authenticated'] = false;
2911 api_account_update_profile_image('json');
2915 * Test the api_account_update_profile_image() function with an uploaded file.
2919 public function testApiAccountUpdateProfileImageWithUpload()
2921 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2922 $this->markTestIncomplete();
2927 * Test the api_account_update_profile() function.
2931 public function testApiAccountUpdateProfile()
2933 $_POST['name'] = 'new_name';
2934 $_POST['description'] = 'new_description';
2935 $result = api_account_update_profile('json');
2936 // We can't use assertSelfUser() here because the user object is missing some properties.
2937 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
2938 self::assertEquals('DFRN', $result['user']['location']);
2939 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
2940 self::assertEquals('new_name', $result['user']['name']);
2941 self::assertEquals('new_description', $result['user']['description']);
2945 * Test the check_acl_input() function.
2949 public function testCheckAclInput()
2951 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
2952 // Where does this result come from?
2953 self::assertEquals(1, $result);
2957 * Test the check_acl_input() function with an empty ACL string.
2961 public function testCheckAclInputWithEmptyAclString()
2963 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
2964 self::assertFalse($result);
2968 * Test the save_media_to_database() function.
2972 public function testSaveMediaToDatabase()
2974 $this->markTestIncomplete();
2978 * Test the post_photo_item() function.
2982 public function testPostPhotoItem()
2984 $this->markTestIncomplete();
2988 * Test the prepare_photo_data() function.
2992 public function testPreparePhotoData()
2994 $this->markTestIncomplete();
2998 * Test the api_share_as_retweet() function with a valid item.
3002 public function testApiShareAsRetweetWithValidItem()
3004 $this->markTestIncomplete();
3008 * Test the api_in_reply_to() function with a valid item.
3012 public function testApiInReplyToWithValidItem()
3014 $this->markTestIncomplete();
3018 * Test the api_clean_plain_items() function.
3022 public function testApiCleanPlainItems()
3024 $_REQUEST['include_entities'] = 'true';
3025 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3026 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3030 * Test the api_best_nickname() function with contacts.
3034 public function testApiBestNicknameWithContacts()
3036 $this->markTestIncomplete();
3040 * Test the api_friendica_group_show() function.
3044 public function testApiFriendicaGroupShow()
3046 $this->markTestIncomplete();
3050 * Test the api_friendica_group_delete() function.
3054 public function testApiFriendicaGroupDelete()
3056 $this->markTestIncomplete();
3060 * Test the api_lists_destroy() function.
3064 public function testApiListsDestroy()
3066 $this->markTestIncomplete();
3070 * Test the group_create() function.
3074 public function testGroupCreate()
3076 $this->markTestIncomplete();
3080 * Test the api_friendica_group_create() function.
3084 public function testApiFriendicaGroupCreate()
3086 $this->markTestIncomplete();
3090 * Test the api_lists_create() function.
3094 public function testApiListsCreate()
3096 $this->markTestIncomplete();
3100 * Test the api_friendica_group_update() function.
3104 public function testApiFriendicaGroupUpdate()
3106 $this->markTestIncomplete();
3110 * Test the api_lists_update() function.
3114 public function testApiListsUpdate()
3116 $this->markTestIncomplete();
3120 * Test the api_friendica_activity() function.
3124 public function testApiFriendicaActivity()
3126 $this->markTestIncomplete();
3130 * Test the api_friendica_notification_seen() function.
3134 public function testApiFriendicaNotificationSeen()
3136 $this->markTestIncomplete();
3140 * Test the api_friendica_direct_messages_setseen() function.
3144 public function testApiFriendicaDirectMessagesSetseen()
3146 $this->markTestIncomplete();
3150 * Test the api_friendica_direct_messages_search() function.
3154 public function testApiFriendicaDirectMessagesSearch()
3156 $this->markTestIncomplete();