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 $_REQUEST['status'] = 'Status content #friendica';
952 $_REQUEST['in_reply_to_status_id'] = -1;
953 $_REQUEST['lat'] = 48;
954 $_REQUEST['long'] = 7;
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 $_REQUEST['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()
1131 $result = api_users_show('json');
1132 // We can't use assertSelfUser() here because the user object is missing some properties.
1133 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1134 self::assertEquals('DFRN', $result['user']['location']);
1135 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1136 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1137 self::assertTrue($result['user']['verified']);
1142 * Test the api_users_show() function with an XML result.
1146 public function testApiUsersShowWithXml()
1148 // $result = api_users_show('xml');
1149 // self::assertXml($result, 'statuses');
1153 * Test the api_users_search() function.
1157 public function testApiUsersSearch()
1159 // $_GET['q'] = 'othercontact';
1160 // $result = api_users_search('json');
1161 // self::assertOtherUser($result['users'][0]);
1165 * Test the api_users_search() function with an XML result.
1169 public function testApiUsersSearchWithXml()
1171 // $_GET['q'] = 'othercontact';
1172 // $result = api_users_search('xml');
1173 // self::assertXml($result, 'users');
1177 * Test the api_users_search() function without a GET q parameter.
1181 public function testApiUsersSearchWithoutQuery()
1183 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1184 // api_users_search('json');
1188 * Test the api_users_lookup() function.
1192 public function testApiUsersLookup()
1194 // $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1195 // api_users_lookup('json');
1199 * Test the api_users_lookup() function with an user ID.
1203 public function testApiUsersLookupWithUserId()
1205 // $_REQUEST['user_id'] = $this->otherUser['id'];
1206 // $result = api_users_lookup('json');
1207 // self::assertOtherUser($result['users'][0]);
1211 * Test the api_search() function.
1215 public function testApiSearch()
1217 $_REQUEST['q'] = 'reply';
1218 $_REQUEST['max_id'] = 10;
1219 $result = api_search('json');
1220 foreach ($result['status'] as $status) {
1221 self::assertStatus($status);
1222 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1227 * Test the api_search() function a count parameter.
1231 public function testApiSearchWithCount()
1233 $_REQUEST['q'] = 'reply';
1234 $_REQUEST['count'] = 20;
1235 $result = api_search('json');
1236 foreach ($result['status'] as $status) {
1237 self::assertStatus($status);
1238 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1243 * Test the api_search() function with an rpp parameter.
1247 public function testApiSearchWithRpp()
1249 $_REQUEST['q'] = 'reply';
1250 $_REQUEST['rpp'] = 20;
1251 $result = api_search('json');
1252 foreach ($result['status'] as $status) {
1253 self::assertStatus($status);
1254 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1259 * Test the api_search() function with an q parameter contains hashtag.
1260 * @doesNotPerformAssertions
1262 public function testApiSearchWithHashtag()
1264 $_REQUEST['q'] = '%23friendica';
1265 $result = api_search('json');
1266 foreach ($result['status'] as $status) {
1267 self::assertStatus($status);
1268 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1273 * Test the api_search() function with an exclude_replies parameter.
1274 * @doesNotPerformAssertions
1276 public function testApiSearchWithExcludeReplies()
1278 $_REQUEST['max_id'] = 10;
1279 $_REQUEST['exclude_replies'] = true;
1280 $_REQUEST['q'] = 'friendica';
1281 $result = api_search('json');
1282 foreach ($result['status'] as $status) {
1283 self::assertStatus($status);
1288 * Test the api_search() function without an authenticated user.
1292 public function testApiSearchWithUnallowedUser()
1294 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1295 BasicAuth::setCurrentUserID();
1300 * Test the api_search() function without any GET query parameter.
1304 public function testApiSearchWithoutQuery()
1306 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1311 * Test the api_statuses_home_timeline() function.
1315 public function testApiStatusesHomeTimeline()
1318 $_REQUEST['max_id'] = 10;
1319 $_REQUEST['exclude_replies'] = true;
1320 $_REQUEST['conversation_id'] = 1;
1321 $result = api_statuses_home_timeline('json');
1322 self::assertNotEmpty($result['status']);
1323 foreach ($result['status'] as $status) {
1324 self::assertStatus($status);
1330 * Test the api_statuses_home_timeline() function with a negative page parameter.
1334 public function testApiStatusesHomeTimelineWithNegativePage()
1337 $_REQUEST['page'] = -2;
1338 $result = api_statuses_home_timeline('json');
1339 self::assertNotEmpty($result['status']);
1340 foreach ($result['status'] as $status) {
1341 self::assertStatus($status);
1347 * Test the api_statuses_home_timeline() with an unallowed user.
1351 public function testApiStatusesHomeTimelineWithUnallowedUser()
1354 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1355 BasicAuth::setCurrentUserID();
1356 api_statuses_home_timeline('json');
1361 * Test the api_statuses_home_timeline() function with an RSS result.
1365 public function testApiStatusesHomeTimelineWithRss()
1367 // $result = api_statuses_home_timeline('rss');
1368 // self::assertXml($result, 'statuses');
1372 * Test the api_statuses_public_timeline() function.
1376 public function testApiStatusesPublicTimeline()
1379 $_REQUEST['max_id'] = 10;
1380 $_REQUEST['conversation_id'] = 1;
1381 $result = api_statuses_public_timeline('json');
1382 self::assertNotEmpty($result['status']);
1383 foreach ($result['status'] as $status) {
1384 self::assertStatus($status);
1390 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1394 public function testApiStatusesPublicTimelineWithExcludeReplies()
1397 $_REQUEST['max_id'] = 10;
1398 $_REQUEST['exclude_replies'] = true;
1399 $result = api_statuses_public_timeline('json');
1400 self::assertNotEmpty($result['status']);
1401 foreach ($result['status'] as $status) {
1402 self::assertStatus($status);
1408 * Test the api_statuses_public_timeline() function with a negative page parameter.
1412 public function testApiStatusesPublicTimelineWithNegativePage()
1415 $_REQUEST['page'] = -2;
1416 $result = api_statuses_public_timeline('json');
1417 self::assertNotEmpty($result['status']);
1418 foreach ($result['status'] as $status) {
1419 self::assertStatus($status);
1425 * Test the api_statuses_public_timeline() function with an unallowed user.
1429 public function testApiStatusesPublicTimelineWithUnallowedUser()
1431 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1432 // BasicAuth::setCurrentUserID();
1433 // api_statuses_public_timeline('json');
1437 * Test the api_statuses_public_timeline() function with an RSS result.
1441 public function testApiStatusesPublicTimelineWithRss()
1443 // $result = api_statuses_public_timeline('rss');
1444 // self::assertXml($result, 'statuses');
1448 * Test the api_statuses_networkpublic_timeline() function.
1452 public function testApiStatusesNetworkpublicTimeline()
1455 $_REQUEST['max_id'] = 10;
1456 $result = api_statuses_networkpublic_timeline('json');
1457 self::assertNotEmpty($result['status']);
1458 foreach ($result['status'] as $status) {
1459 self::assertStatus($status);
1465 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1469 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1472 $_REQUEST['page'] = -2;
1473 $result = api_statuses_networkpublic_timeline('json');
1474 self::assertNotEmpty($result['status']);
1475 foreach ($result['status'] as $status) {
1476 self::assertStatus($status);
1482 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1486 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1488 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1489 // BasicAuth::setCurrentUserID();
1490 // api_statuses_networkpublic_timeline('json');
1494 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1498 public function testApiStatusesNetworkpublicTimelineWithRss()
1500 // $result = api_statuses_networkpublic_timeline('rss');
1501 // self::assertXml($result, 'statuses');
1505 * Test the api_statuses_show() function.
1509 public function testApiStatusesShow()
1511 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1512 // api_statuses_show('json');
1516 * Test the api_statuses_show() function with an ID.
1520 public function testApiStatusesShowWithId()
1522 // DI::args()->setArgv(['', '', '', 1]);
1523 // $result = api_statuses_show('json');
1524 // self::assertStatus($result['status']);
1528 * Test the api_statuses_show() function with the conversation parameter.
1532 public function testApiStatusesShowWithConversation()
1535 DI::args()->setArgv(['', '', '', 1]);
1536 $_REQUEST['conversation'] = 1;
1537 $result = api_statuses_show('json');
1538 self::assertNotEmpty($result['status']);
1539 foreach ($result['status'] as $status) {
1540 self::assertStatus($status);
1546 * Test the api_statuses_show() function with an unallowed user.
1550 public function testApiStatusesShowWithUnallowedUser()
1552 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1553 // BasicAuth::setCurrentUserID();
1554 // api_statuses_show('json');
1558 * Test the api_conversation_show() function.
1562 public function testApiConversationShow()
1564 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1565 // api_conversation_show('json');
1569 * Test the api_conversation_show() function with an ID.
1573 public function testApiConversationShowWithId()
1576 DI::args()->setArgv(['', '', '', 1]);
1577 $_REQUEST['max_id'] = 10;
1578 $_REQUEST['page'] = -2;
1579 $result = api_conversation_show('json');
1580 self::assertNotEmpty($result['status']);
1581 foreach ($result['status'] as $status) {
1582 self::assertStatus($status);
1588 * Test the api_conversation_show() function with an unallowed user.
1592 public function testApiConversationShowWithUnallowedUser()
1594 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1595 // BasicAuth::setCurrentUserID();
1596 // api_conversation_show('json');
1600 * Test the api_statuses_repeat() function.
1604 public function testApiStatusesRepeat()
1606 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1607 api_statuses_repeat('json');
1611 * Test the api_statuses_repeat() function without an authenticated user.
1615 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1617 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1618 BasicAuth::setCurrentUserID();
1619 $_SESSION['authenticated'] = false;
1620 api_statuses_repeat('json');
1624 * Test the api_statuses_repeat() function with an ID.
1628 public function testApiStatusesRepeatWithId()
1630 DI::args()->setArgv(['', '', '', 1]);
1631 $result = api_statuses_repeat('json');
1632 self::assertStatus($result['status']);
1634 // Also test with a shared status
1635 DI::args()->setArgv(['', '', '', 5]);
1636 $result = api_statuses_repeat('json');
1637 self::assertStatus($result['status']);
1641 * Test the api_statuses_destroy() function.
1645 public function testApiStatusesDestroy()
1647 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1648 // api_statuses_destroy('json');
1652 * Test the api_statuses_destroy() function without an authenticated user.
1656 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1658 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1659 // BasicAuth::setCurrentUserID();
1660 // $_SESSION['authenticated'] = false;
1661 // api_statuses_destroy('json');
1665 * Test the api_statuses_destroy() function with an ID.
1669 public function testApiStatusesDestroyWithId()
1671 // DI::args()->setArgv(['', '', '', 1]);
1672 // $result = api_statuses_destroy('json');
1673 // self::assertStatus($result['status']);
1677 * Test the api_statuses_mentions() function.
1681 public function testApiStatusesMentions()
1684 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1685 $_REQUEST['max_id'] = 10;
1686 $result = api_statuses_mentions('json');
1687 self::assertEmpty($result['status']);
1688 // We should test with mentions in the database.
1693 * Test the api_statuses_mentions() function with a negative page parameter.
1697 public function testApiStatusesMentionsWithNegativePage()
1699 // $_REQUEST['page'] = -2;
1700 // $result = api_statuses_mentions('json');
1701 // self::assertEmpty($result['status']);
1705 * Test the api_statuses_mentions() function with an unallowed user.
1709 public function testApiStatusesMentionsWithUnallowedUser()
1711 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1712 // BasicAuth::setCurrentUserID();
1713 // api_statuses_mentions('json');
1717 * Test the api_statuses_mentions() function with an RSS result.
1721 public function testApiStatusesMentionsWithRss()
1723 // $result = api_statuses_mentions('rss');
1724 // self::assertXml($result, 'statuses');
1728 * Test the api_statuses_user_timeline() function.
1732 public function testApiStatusesUserTimeline()
1735 $_REQUEST['user_id'] = 42;
1736 $_REQUEST['max_id'] = 10;
1737 $_REQUEST['exclude_replies'] = true;
1738 $_REQUEST['conversation_id'] = 7;
1740 $result = api_statuses_user_timeline('json');
1741 self::assertNotEmpty($result['status']);
1742 foreach ($result['status'] as $status) {
1743 self::assertStatus($status);
1749 * Test the api_statuses_user_timeline() function with a negative page parameter.
1753 public function testApiStatusesUserTimelineWithNegativePage()
1756 $_REQUEST['user_id'] = 42;
1757 $_REQUEST['page'] = -2;
1759 $result = api_statuses_user_timeline('json');
1760 self::assertNotEmpty($result['status']);
1761 foreach ($result['status'] as $status) {
1762 self::assertStatus($status);
1768 * Test the api_statuses_user_timeline() function with an RSS result.
1772 public function testApiStatusesUserTimelineWithRss()
1774 // $result = api_statuses_user_timeline('rss');
1775 // self::assertXml($result, 'statuses');
1779 * Test the api_statuses_user_timeline() function with an unallowed user.
1783 public function testApiStatusesUserTimelineWithUnallowedUser()
1785 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1786 // BasicAuth::setCurrentUserID();
1787 // api_statuses_user_timeline('json');
1791 * Test the api_favorites_create_destroy() function.
1795 public function testApiFavoritesCreateDestroy()
1797 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1798 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1799 api_favorites_create_destroy('json');
1803 * Test the api_favorites_create_destroy() function with an invalid ID.
1807 public function testApiFavoritesCreateDestroyWithInvalidId()
1809 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1810 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1811 api_favorites_create_destroy('json');
1815 * Test the api_favorites_create_destroy() function with an invalid action.
1819 public function testApiFavoritesCreateDestroyWithInvalidAction()
1821 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1822 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1823 $_REQUEST['id'] = 1;
1824 api_favorites_create_destroy('json');
1828 * Test the api_favorites_create_destroy() function with the create action.
1832 public function testApiFavoritesCreateDestroyWithCreateAction()
1834 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1835 $_REQUEST['id'] = 3;
1836 $result = api_favorites_create_destroy('json');
1837 self::assertStatus($result['status']);
1841 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1845 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1847 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1848 $_REQUEST['id'] = 3;
1849 $result = api_favorites_create_destroy('rss');
1850 self::assertXml($result, 'status');
1854 * Test the api_favorites_create_destroy() function with the destroy action.
1858 public function testApiFavoritesCreateDestroyWithDestroyAction()
1860 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1861 $_REQUEST['id'] = 3;
1862 $result = api_favorites_create_destroy('json');
1863 self::assertStatus($result['status']);
1867 * Test the api_favorites_create_destroy() function without an authenticated user.
1871 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1873 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1874 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1875 BasicAuth::setCurrentUserID();
1876 $_SESSION['authenticated'] = false;
1877 api_favorites_create_destroy('json');
1881 * Test the api_favorites() function.
1885 public function testApiFavorites()
1888 $_REQUEST['page'] = -1;
1889 $_REQUEST['max_id'] = 10;
1890 $result = api_favorites('json');
1891 foreach ($result['status'] as $status) {
1892 self::assertStatus($status);
1898 * Test the api_favorites() function with an RSS result.
1902 public function testApiFavoritesWithRss()
1904 // $result = api_favorites('rss');
1905 // self::assertXml($result, 'statuses');
1909 * Test the api_favorites() function with an unallowed user.
1913 public function testApiFavoritesWithUnallowedUser()
1915 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1916 // BasicAuth::setCurrentUserID();
1917 // api_favorites('json');
1921 * Test the api_format_messages() function.
1925 public function testApiFormatMessages()
1927 $result = api_format_messages(
1928 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1929 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1930 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1932 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1933 self::assertEquals(1, $result['id']);
1934 self::assertEquals(2, $result['recipient_id']);
1935 self::assertEquals(3, $result['sender_id']);
1936 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1937 self::assertEquals('sender_name', $result['sender_screen_name']);
1941 * Test the api_format_messages() function with HTML.
1945 public function testApiFormatMessagesWithHtmlText()
1947 $_GET['getText'] = 'html';
1948 $result = api_format_messages(
1949 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1950 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1951 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1953 self::assertEquals('item_title', $result['title']);
1954 self::assertEquals('<strong>item_body</strong>', $result['text']);
1958 * Test the api_format_messages() function with plain text.
1962 public function testApiFormatMessagesWithPlainText()
1964 $_GET['getText'] = 'plain';
1965 $result = api_format_messages(
1966 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1967 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1968 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1970 self::assertEquals('item_title', $result['title']);
1971 self::assertEquals('item_body', $result['text']);
1975 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1979 public function testApiFormatMessagesWithoutUserObjects()
1981 $_GET['getUserObjects'] = 'false';
1982 $result = api_format_messages(
1983 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1984 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1985 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1987 self::assertTrue(!isset($result['sender']));
1988 self::assertTrue(!isset($result['recipient']));
1992 * Test the api_convert_item() function.
1996 public function testApiConvertItem()
1999 $result = api_convert_item(
2001 'network' => 'feed',
2002 'title' => 'item_title',
2004 // We need a long string to test that it is correctly cut
2005 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2006 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2007 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2008 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2009 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2010 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2011 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2012 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2013 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2014 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2015 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2016 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2017 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2018 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2019 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2020 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2021 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2022 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2023 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2024 'repellat officia illum quos impedit quam iste esse unde qui ' .
2025 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2026 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2027 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2028 'plink' => 'item_plink'
2031 self::assertStringStartsWith('item_title', $result['text']);
2032 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2037 * Test the api_convert_item() function with an empty item body.
2041 public function testApiConvertItemWithoutBody()
2044 $result = api_convert_item(
2046 'network' => 'feed',
2047 'title' => 'item_title',
2050 'plink' => 'item_plink'
2053 self::assertEquals("item_title", $result['text']);
2054 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2059 * Test the api_convert_item() function with the title in the body.
2063 public function testApiConvertItemWithTitleInBody()
2066 $result = api_convert_item(
2068 'title' => 'item_title',
2069 'body' => 'item_title item_body',
2073 self::assertEquals('item_title item_body', $result['text']);
2074 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2079 * Test the api_get_attachments() function.
2083 public function testApiGetAttachments()
2086 // self::assertEmpty(api_get_attachments($body, 0));
2090 * Test the api_get_attachments() function with an img tag.
2094 public function testApiGetAttachmentsWithImage()
2096 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2097 // self::assertIsArray(api_get_attachments($body, 0));
2101 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2105 public function testApiGetAttachmentsWithImageAndAndStatus()
2107 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2108 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2109 // self::assertIsArray(api_get_attachments($body, 0));
2113 * Test the api_get_entitities() function.
2117 public function testApiGetEntitities()
2120 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2124 * Test the api_get_entitities() function with the include_entities parameter.
2128 public function testApiGetEntititiesWithIncludeEntities()
2131 $_REQUEST['include_entities'] = 'true';
2133 $result = api_get_entitities($text, 'bbcode', 0);
2134 self::assertIsArray($result['hashtags']);
2135 self::assertIsArray($result['symbols']);
2136 self::assertIsArray($result['urls']);
2137 self::assertIsArray($result['user_mentions']);
2142 * Test the api_format_items_embeded_images() function.
2146 public function testApiFormatItemsEmbededImages()
2150 'text ' . DI::baseUrl() . '/display/item_guid',
2151 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2157 * Test the api_format_items_activities() function.
2161 public function testApiFormatItemsActivities()
2163 $item = ['uid' => 0, 'uri-id' => 1];
2164 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
2165 self::assertArrayHasKey('like', $result);
2166 self::assertArrayHasKey('dislike', $result);
2167 self::assertArrayHasKey('attendyes', $result);
2168 self::assertArrayHasKey('attendno', $result);
2169 self::assertArrayHasKey('attendmaybe', $result);
2173 * Test the api_format_items_activities() function with an XML result.
2177 public function testApiFormatItemsActivitiesWithXml()
2179 $item = ['uid' => 0, 'uri-id' => 1];
2180 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
2181 self::assertArrayHasKey('friendica:like', $result);
2182 self::assertArrayHasKey('friendica:dislike', $result);
2183 self::assertArrayHasKey('friendica:attendyes', $result);
2184 self::assertArrayHasKey('friendica:attendno', $result);
2185 self::assertArrayHasKey('friendica:attendmaybe', $result);
2189 * Test the api_format_items() function.
2190 * @doesNotPerformAssertions
2192 public function testApiFormatItems()
2195 $items = Post::selectToArray([], ['uid' => 42]);
2196 foreach ($items as $item) {
2197 $status = api_format_item($item);
2198 self::assertStatus($status);
2204 * Test the api_format_items() function with an XML result.
2205 * @doesNotPerformAssertions
2207 public function testApiFormatItemsWithXml()
2210 $items = Post::selectToArray([], ['uid' => 42]);
2211 foreach ($items as $item) {
2212 $status = api_format_item($item, 'xml');
2213 self::assertStatus($status);
2219 * Test the api_lists_list() function.
2223 public function testApiListsList()
2225 $result = api_lists_list('json');
2226 self::assertEquals(['lists_list' => []], $result);
2230 * Test the api_lists_ownerships() function.
2234 public function testApiListsOwnerships()
2236 $result = api_lists_ownerships('json');
2237 foreach ($result['lists']['lists'] as $list) {
2238 self::assertList($list);
2243 * Test the api_lists_ownerships() function without an authenticated user.
2247 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2249 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2250 BasicAuth::setCurrentUserID();
2251 $_SESSION['authenticated'] = false;
2252 api_lists_ownerships('json');
2256 * Test the api_lists_statuses() function.
2260 public function testApiListsStatuses()
2262 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2263 api_lists_statuses('json');
2267 * Test the api_lists_statuses() function with a list ID.
2268 * @doesNotPerformAssertions
2270 public function testApiListsStatusesWithListId()
2272 $_REQUEST['list_id'] = 1;
2273 $_REQUEST['page'] = -1;
2274 $_REQUEST['max_id'] = 10;
2275 $result = api_lists_statuses('json');
2276 foreach ($result['status'] as $status) {
2277 self::assertStatus($status);
2282 * Test the api_lists_statuses() function with a list ID and a RSS result.
2286 public function testApiListsStatusesWithListIdAndRss()
2288 $_REQUEST['list_id'] = 1;
2289 $result = api_lists_statuses('rss');
2290 self::assertXml($result, 'statuses');
2294 * Test the api_lists_statuses() function with an unallowed user.
2298 public function testApiListsStatusesWithUnallowedUser()
2300 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2301 BasicAuth::setCurrentUserID();
2302 api_lists_statuses('json');
2306 * Test the api_statuses_f() function.
2310 public function testApiStatusesFWithFriends()
2313 $result = api_statuses_f('friends');
2314 self::assertArrayHasKey('user', $result);
2318 * Test the api_statuses_f() function.
2322 public function testApiStatusesFWithFollowers()
2324 $result = api_statuses_f('followers');
2325 self::assertArrayHasKey('user', $result);
2329 * Test the api_statuses_f() function.
2333 public function testApiStatusesFWithBlocks()
2335 $result = api_statuses_f('blocks');
2336 self::assertArrayHasKey('user', $result);
2340 * Test the api_statuses_f() function.
2344 public function testApiStatusesFWithIncoming()
2346 $result = api_statuses_f('incoming');
2347 self::assertArrayHasKey('user', $result);
2351 * Test the api_statuses_f() function an undefined cursor GET variable.
2355 public function testApiStatusesFWithUndefinedCursor()
2357 $_GET['cursor'] = 'undefined';
2358 self::assertFalse(api_statuses_f('friends'));
2362 * Test the api_statuses_friends() function.
2366 public function testApiStatusesFriends()
2368 $result = api_statuses_friends('json');
2369 self::assertArrayHasKey('user', $result);
2373 * Test the api_statuses_friends() function an undefined cursor GET variable.
2377 public function testApiStatusesFriendsWithUndefinedCursor()
2379 $_GET['cursor'] = 'undefined';
2380 self::assertFalse(api_statuses_friends('json'));
2384 * Test the api_statuses_followers() function.
2388 public function testApiStatusesFollowers()
2390 $result = api_statuses_followers('json');
2391 self::assertArrayHasKey('user', $result);
2395 * Test the api_statuses_followers() function an undefined cursor GET variable.
2399 public function testApiStatusesFollowersWithUndefinedCursor()
2401 $_GET['cursor'] = 'undefined';
2402 self::assertFalse(api_statuses_followers('json'));
2406 * Test the api_blocks_list() function.
2410 public function testApiBlocksList()
2412 $result = api_blocks_list('json');
2413 self::assertArrayHasKey('user', $result);
2417 * Test the api_blocks_list() function an undefined cursor GET variable.
2421 public function testApiBlocksListWithUndefinedCursor()
2423 $_GET['cursor'] = 'undefined';
2424 self::assertFalse(api_blocks_list('json'));
2428 * Test the api_friendships_incoming() function.
2432 public function testApiFriendshipsIncoming()
2434 $result = api_friendships_incoming('json');
2435 self::assertArrayHasKey('id', $result);
2439 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2443 public function testApiFriendshipsIncomingWithUndefinedCursor()
2445 $_GET['cursor'] = 'undefined';
2446 self::assertFalse(api_friendships_incoming('json'));
2450 * Test the api_statusnet_config() function.
2454 public function testApiStatusnetConfig()
2457 $result = api_statusnet_config('json');
2458 self::assertEquals('localhost', $result['config']['site']['server']);
2459 self::assertEquals('default', $result['config']['site']['theme']);
2460 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2461 self::assertTrue($result['config']['site']['fancy']);
2462 self::assertEquals('en', $result['config']['site']['language']);
2463 self::assertEquals('UTC', $result['config']['site']['timezone']);
2464 self::assertEquals(200000, $result['config']['site']['textlimit']);
2465 self::assertEquals('false', $result['config']['site']['private']);
2466 self::assertEquals('false', $result['config']['site']['ssl']);
2467 self::assertEquals(30, $result['config']['site']['shorturllength']);
2472 * Test the api_direct_messages_new() function.
2476 public function testApiDirectMessagesNew()
2478 $result = api_direct_messages_new('json');
2479 self::assertNull($result);
2483 * Test the api_direct_messages_new() function without an authenticated user.
2487 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2489 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2490 BasicAuth::setCurrentUserID();
2491 $_SESSION['authenticated'] = false;
2492 api_direct_messages_new('json');
2496 * Test the api_direct_messages_new() function with an user ID.
2500 public function testApiDirectMessagesNewWithUserId()
2502 $_POST['text'] = 'message_text';
2503 $_POST['user_id'] = $this->otherUser['id'];
2504 $result = api_direct_messages_new('json');
2505 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2509 * Test the api_direct_messages_new() function with a screen name.
2513 public function testApiDirectMessagesNewWithScreenName()
2515 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2516 $_POST['text'] = 'message_text';
2517 $_POST['user_id'] = $this->friendUser['id'];
2518 $result = api_direct_messages_new('json');
2519 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2520 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2521 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2525 * Test the api_direct_messages_new() function with a title.
2529 public function testApiDirectMessagesNewWithTitle()
2531 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2532 $_POST['text'] = 'message_text';
2533 $_POST['user_id'] = $this->friendUser['id'];
2534 $_REQUEST['title'] = 'message_title';
2535 $result = api_direct_messages_new('json');
2536 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2537 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2538 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2539 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2543 * Test the api_direct_messages_new() function with an RSS result.
2547 public function testApiDirectMessagesNewWithRss()
2549 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2550 $_POST['text'] = 'message_text';
2551 $_POST['user_id'] = $this->friendUser['id'];
2552 $result = api_direct_messages_new('rss');
2553 self::assertXml($result, 'direct-messages');
2557 * Test the api_direct_messages_destroy() function.
2561 public function testApiDirectMessagesDestroy()
2563 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2564 api_direct_messages_destroy('json');
2568 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2572 public function testApiDirectMessagesDestroyWithVerbose()
2574 $_GET['friendica_verbose'] = 'true';
2575 $result = api_direct_messages_destroy('json');
2579 'result' => 'error',
2580 'message' => 'message id or parenturi not specified'
2588 * Test the api_direct_messages_destroy() function without an authenticated user.
2592 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2594 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2595 BasicAuth::setCurrentUserID();
2596 $_SESSION['authenticated'] = false;
2597 api_direct_messages_destroy('json');
2601 * Test the api_direct_messages_destroy() function with a non-zero ID.
2605 public function testApiDirectMessagesDestroyWithId()
2607 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2608 $_REQUEST['id'] = 1;
2609 api_direct_messages_destroy('json');
2613 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2617 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2619 $_REQUEST['id'] = 1;
2620 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2621 $_GET['friendica_verbose'] = 'true';
2622 $result = api_direct_messages_destroy('json');
2626 'result' => 'error',
2627 'message' => 'message id not in database'
2635 * Test the api_direct_messages_destroy() function with a non-zero ID.
2639 public function testApiDirectMessagesDestroyWithCorrectId()
2641 $this->markTestIncomplete('We need to add a dataset for this.');
2645 * Test the api_direct_messages_box() function.
2649 public function testApiDirectMessagesBoxWithSentbox()
2651 $_REQUEST['page'] = -1;
2652 $_REQUEST['max_id'] = 10;
2653 $result = api_direct_messages_box('json', 'sentbox', 'false');
2654 self::assertArrayHasKey('direct_message', $result);
2658 * Test the api_direct_messages_box() function.
2662 public function testApiDirectMessagesBoxWithConversation()
2664 $result = api_direct_messages_box('json', 'conversation', 'false');
2665 self::assertArrayHasKey('direct_message', $result);
2669 * Test the api_direct_messages_box() function.
2673 public function testApiDirectMessagesBoxWithAll()
2675 $result = api_direct_messages_box('json', 'all', 'false');
2676 self::assertArrayHasKey('direct_message', $result);
2680 * Test the api_direct_messages_box() function.
2684 public function testApiDirectMessagesBoxWithInbox()
2686 $result = api_direct_messages_box('json', 'inbox', 'false');
2687 self::assertArrayHasKey('direct_message', $result);
2691 * Test the api_direct_messages_box() function.
2695 public function testApiDirectMessagesBoxWithVerbose()
2697 $result = api_direct_messages_box('json', 'sentbox', 'true');
2701 'result' => 'error',
2702 'message' => 'no mails available'
2710 * Test the api_direct_messages_box() function with a RSS result.
2714 public function testApiDirectMessagesBoxWithRss()
2716 $result = api_direct_messages_box('rss', 'sentbox', 'false');
2717 self::assertXml($result, 'direct-messages');
2721 * Test the api_direct_messages_box() function without an authenticated user.
2725 public function testApiDirectMessagesBoxWithUnallowedUser()
2727 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2728 BasicAuth::setCurrentUserID();
2729 api_direct_messages_box('json', 'sentbox', 'false');
2733 * Test the api_direct_messages_sentbox() function.
2737 public function testApiDirectMessagesSentbox()
2739 $result = api_direct_messages_sentbox('json');
2740 self::assertArrayHasKey('direct_message', $result);
2744 * Test the api_direct_messages_inbox() function.
2748 public function testApiDirectMessagesInbox()
2750 $result = api_direct_messages_inbox('json');
2751 self::assertArrayHasKey('direct_message', $result);
2755 * Test the api_direct_messages_all() function.
2759 public function testApiDirectMessagesAll()
2761 $result = api_direct_messages_all('json');
2762 self::assertArrayHasKey('direct_message', $result);
2766 * Test the api_direct_messages_conversation() function.
2770 public function testApiDirectMessagesConversation()
2772 $result = api_direct_messages_conversation('json');
2773 self::assertArrayHasKey('direct_message', $result);
2777 * Test the api_oauth_request_token() function.
2781 public function testApiOauthRequestToken()
2783 $this->markTestIncomplete('exit() kills phpunit as well');
2787 * Test the api_oauth_access_token() function.
2791 public function testApiOauthAccessToken()
2793 $this->markTestIncomplete('exit() kills phpunit as well');
2797 * Test the api_fr_photos_list() function.
2801 public function testApiFrPhotosList()
2803 $result = api_fr_photos_list('json');
2804 self::assertArrayHasKey('photo', $result);
2808 * Test the api_fr_photos_list() function without an authenticated user.
2812 public function testApiFrPhotosListWithoutAuthenticatedUser()
2814 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2815 BasicAuth::setCurrentUserID();
2816 $_SESSION['authenticated'] = false;
2817 api_fr_photos_list('json');
2821 * Test the api_fr_photo_create_update() function.
2823 public function testApiFrPhotoCreateUpdate()
2825 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2826 api_fr_photo_create_update('json');
2830 * Test the api_fr_photo_create_update() function without an authenticated user.
2834 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
2836 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2837 BasicAuth::setCurrentUserID();
2838 $_SESSION['authenticated'] = false;
2839 api_fr_photo_create_update('json');
2843 * Test the api_fr_photo_create_update() function with an album name.
2847 public function testApiFrPhotoCreateUpdateWithAlbum()
2849 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2850 $_REQUEST['album'] = 'album_name';
2851 api_fr_photo_create_update('json');
2855 * Test the api_fr_photo_create_update() function with the update mode.
2859 public function testApiFrPhotoCreateUpdateWithUpdate()
2861 $this->markTestIncomplete('We need to create a dataset for this');
2865 * Test the api_fr_photo_create_update() function with an uploaded file.
2869 public function testApiFrPhotoCreateUpdateWithFile()
2871 $this->markTestIncomplete();
2875 * Test the api_fr_photo_detail() function.
2879 public function testApiFrPhotoDetail()
2881 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2882 api_fr_photo_detail('json');
2886 * Test the api_fr_photo_detail() function without an authenticated user.
2890 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
2892 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2893 BasicAuth::setCurrentUserID();
2894 $_SESSION['authenticated'] = false;
2895 api_fr_photo_detail('json');
2899 * Test the api_fr_photo_detail() function with a photo ID.
2903 public function testApiFrPhotoDetailWithPhotoId()
2905 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
2906 $_REQUEST['photo_id'] = 1;
2907 api_fr_photo_detail('json');
2911 * Test the api_fr_photo_detail() function with a correct photo ID.
2915 public function testApiFrPhotoDetailCorrectPhotoId()
2917 $this->markTestIncomplete('We need to create a dataset for this.');
2921 * Test the api_account_update_profile_image() function.
2925 public function testApiAccountUpdateProfileImage()
2927 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2928 api_account_update_profile_image('json');
2932 * Test the api_account_update_profile_image() function without an authenticated user.
2936 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2938 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2939 BasicAuth::setCurrentUserID();
2940 $_SESSION['authenticated'] = false;
2941 api_account_update_profile_image('json');
2945 * Test the api_account_update_profile_image() function with an uploaded file.
2949 public function testApiAccountUpdateProfileImageWithUpload()
2951 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2952 $this->markTestIncomplete();
2957 * Test the api_account_update_profile() function.
2961 public function testApiAccountUpdateProfile()
2964 $_POST['name'] = 'new_name';
2965 $_POST['description'] = 'new_description';
2966 $result = api_account_update_profile('json');
2967 // We can't use assertSelfUser() here because the user object is missing some properties.
2968 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
2969 self::assertEquals('DFRN', $result['user']['location']);
2970 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
2971 self::assertEquals('new_name', $result['user']['name']);
2972 self::assertEquals('new_description', $result['user']['description']);
2977 * Test the check_acl_input() function.
2981 public function testCheckAclInput()
2983 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
2984 // Where does this result come from?
2985 self::assertEquals(1, $result);
2989 * Test the check_acl_input() function with an empty ACL string.
2993 public function testCheckAclInputWithEmptyAclString()
2995 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
2996 self::assertFalse($result);
3000 * Test the save_media_to_database() function.
3004 public function testSaveMediaToDatabase()
3006 $this->markTestIncomplete();
3010 * Test the post_photo_item() function.
3014 public function testPostPhotoItem()
3016 $this->markTestIncomplete();
3020 * Test the prepare_photo_data() function.
3024 public function testPreparePhotoData()
3026 $this->markTestIncomplete();
3030 * Test the api_share_as_retweet() function with a valid item.
3034 public function testApiShareAsRetweetWithValidItem()
3036 $this->markTestIncomplete();
3040 * Test the api_in_reply_to() function with a valid item.
3044 public function testApiInReplyToWithValidItem()
3046 $this->markTestIncomplete();
3050 * Test the api_clean_plain_items() function.
3054 public function testApiCleanPlainItems()
3056 $_REQUEST['include_entities'] = 'true';
3057 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3058 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3062 * Test the api_best_nickname() function with contacts.
3066 public function testApiBestNicknameWithContacts()
3068 $this->markTestIncomplete();
3072 * Test the api_friendica_group_show() function.
3076 public function testApiFriendicaGroupShow()
3078 $this->markTestIncomplete();
3082 * Test the api_friendica_group_delete() function.
3086 public function testApiFriendicaGroupDelete()
3088 $this->markTestIncomplete();
3092 * Test the api_lists_destroy() function.
3096 public function testApiListsDestroy()
3098 $this->markTestIncomplete();
3102 * Test the group_create() function.
3106 public function testGroupCreate()
3108 $this->markTestIncomplete();
3112 * Test the api_friendica_group_create() function.
3116 public function testApiFriendicaGroupCreate()
3118 $this->markTestIncomplete();
3122 * Test the api_lists_create() function.
3126 public function testApiListsCreate()
3128 $this->markTestIncomplete();
3132 * Test the api_friendica_group_update() function.
3136 public function testApiFriendicaGroupUpdate()
3138 $this->markTestIncomplete();
3142 * Test the api_lists_update() function.
3146 public function testApiListsUpdate()
3148 $this->markTestIncomplete();
3152 * Test the api_friendica_activity() function.
3156 public function testApiFriendicaActivity()
3158 $this->markTestIncomplete();
3162 * Test the api_friendica_notification_seen() function.
3166 public function testApiFriendicaNotificationSeen()
3168 $this->markTestIncomplete();
3172 * Test the api_friendica_direct_messages_setseen() function.
3176 public function testApiFriendicaDirectMessagesSetseen()
3178 $this->markTestIncomplete();
3182 * Test the api_friendica_direct_messages_search() function.
3186 public function testApiFriendicaDirectMessagesSearch()
3188 $this->markTestIncomplete();