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', BaseApi::getCurrentApplication()['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', BaseApi::getCurrentApplication()['name']);
261 * Test the api_source() function with a GET parameter.
265 public function testApiSourceWithGet()
267 $_GET['source'] = 'source_name';
268 self::assertEquals('source_name', BaseApi::getCurrentApplication()['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 requestdata() function.
910 public function testRequestdata()
912 self::assertNull(requestdata('variable_name'));
916 * Test the requestdata() function with a POST parameter.
920 public function testRequestdataWithPost()
922 $_POST['variable_name'] = 'variable_value';
923 self::assertEquals('variable_value', requestdata('variable_name'));
927 * Test the requestdata() function with a GET parameter.
931 public function testRequestdataWithGet()
933 $_GET['variable_name'] = 'variable_value';
934 self::assertEquals('variable_value', requestdata('variable_name'));
938 * Test the api_statuses_mediap() function.
942 public function testApiStatusesMediap()
944 DI::args()->setArgc(2);
952 'tmp_name' => $this->getTempImage(),
953 'name' => 'spacer.png',
954 'type' => 'image/png'
957 $_GET['status'] = '<b>Status content</b>';
959 $result = api_statuses_mediap('json');
960 self::assertStatus($result['status']);
964 * Test the api_statuses_mediap() function without an authenticated user.
968 public function testApiStatusesMediapWithoutAuthenticatedUser()
970 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
971 BasicAuth::setCurrentUserID();
972 $_SESSION['authenticated'] = false;
973 api_statuses_mediap('json');
977 * Test the api_statuses_update() function.
981 public function testApiStatusesUpdate()
983 $_GET['status'] = 'Status content #friendica';
984 $_GET['in_reply_to_status_id'] = -1;
993 'tmp_name' => $this->getTempImage(),
994 'name' => 'spacer.png',
995 'type' => 'image/png'
999 $result = api_statuses_update('json');
1000 self::assertStatus($result['status']);
1004 * Test the api_statuses_update() function with an HTML status.
1008 public function testApiStatusesUpdateWithHtml()
1010 $_GET['htmlstatus'] = '<b>Status content</b>';
1012 $result = api_statuses_update('json');
1013 self::assertStatus($result['status']);
1017 * Test the api_statuses_update() function without an authenticated user.
1021 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1023 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1024 BasicAuth::setCurrentUserID();
1025 $_SESSION['authenticated'] = false;
1026 api_statuses_update('json');
1030 * Test the api_statuses_update() function with a parent status.
1034 public function testApiStatusesUpdateWithParent()
1036 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1040 * Test the api_statuses_update() function with a media_ids parameter.
1044 public function testApiStatusesUpdateWithMediaIds()
1046 $this->markTestIncomplete();
1050 * Test the api_statuses_update() function with the throttle limit reached.
1054 public function testApiStatusesUpdateWithDayThrottleReached()
1056 $this->markTestIncomplete();
1060 * Test the api_media_upload() function.
1061 * @runInSeparateProcess
1062 * @preserveGlobalState disabled
1064 public function testApiMediaUpload()
1066 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1071 * Test the api_media_upload() function without an authenticated user.
1075 public function testApiMediaUploadWithoutAuthenticatedUser()
1077 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1078 BasicAuth::setCurrentUserID();
1079 $_SESSION['authenticated'] = false;
1084 * Test the api_media_upload() function with an invalid uploaded media.
1088 public function testApiMediaUploadWithMedia()
1090 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1094 'tmp_name' => 'tmp_name'
1101 * Test the api_media_upload() function with an valid uploaded media.
1105 public function testApiMediaUploadWithValidMedia()
1113 'tmp_name' => $this->getTempImage(),
1114 'name' => 'spacer.png',
1115 'type' => 'image/png'
1119 DI::args()->setArgc(2);
1121 $result = api_media_upload();
1122 self::assertEquals('image/png', $result['media']['image']['image_type']);
1123 self::assertEquals(1, $result['media']['image']['w']);
1124 self::assertEquals(1, $result['media']['image']['h']);
1125 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1129 * Test the api_status_show() function.
1131 public function testApiStatusShowWithJson()
1133 // $result = api_status_show('json', 1);
1134 // self::assertStatus($result['status']);
1138 * Test the api_status_show() function with an XML result.
1140 public function testApiStatusShowWithXml()
1142 // $result = api_status_show('xml', 1);
1143 // self::assertXml($result, 'statuses');
1147 * Test the api_get_last_status() function
1149 public function testApiGetLastStatus()
1151 // $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1152 // self::assertNotNull($item);
1156 * Test the api_users_show() function.
1160 public function testApiUsersShow()
1162 $result = api_users_show('json');
1163 // We can't use assertSelfUser() here because the user object is missing some properties.
1164 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1165 self::assertEquals('DFRN', $result['user']['location']);
1166 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1167 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1168 self::assertTrue($result['user']['verified']);
1172 * Test the api_users_show() function with an XML result.
1176 public function testApiUsersShowWithXml()
1178 $result = api_users_show('xml');
1179 self::assertXml($result, 'statuses');
1183 * Test the api_users_search() function.
1187 public function testApiUsersSearch()
1189 $_GET['q'] = 'othercontact';
1190 $result = api_users_search('json');
1191 self::assertOtherUser($result['users'][0]);
1195 * Test the api_users_search() function with an XML result.
1199 public function testApiUsersSearchWithXml()
1201 $_GET['q'] = 'othercontact';
1202 $result = api_users_search('xml');
1203 self::assertXml($result, 'users');
1207 * Test the api_users_search() function without a GET q parameter.
1211 public function testApiUsersSearchWithoutQuery()
1213 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1214 api_users_search('json');
1218 * Test the api_users_lookup() function.
1222 public function testApiUsersLookup()
1224 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1225 api_users_lookup('json');
1229 * Test the api_users_lookup() function with an user ID.
1233 public function testApiUsersLookupWithUserId()
1235 $_REQUEST['user_id'] = $this->otherUser['id'];
1236 $result = api_users_lookup('json');
1237 self::assertOtherUser($result['users'][0]);
1241 * Test the api_search() function.
1245 public function testApiSearch()
1247 $_REQUEST['q'] = 'reply';
1248 $_REQUEST['max_id'] = 10;
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 a count parameter.
1261 public function testApiSearchWithCount()
1263 $_REQUEST['q'] = 'reply';
1264 $_REQUEST['count'] = 20;
1265 $result = api_search('json');
1266 foreach ($result['status'] as $status) {
1267 self::assertStatus($status);
1268 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1273 * Test the api_search() function with an rpp parameter.
1277 public function testApiSearchWithRpp()
1279 $_REQUEST['q'] = 'reply';
1280 $_REQUEST['rpp'] = 20;
1281 $result = api_search('json');
1282 foreach ($result['status'] as $status) {
1283 self::assertStatus($status);
1284 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1289 * Test the api_search() function with an q parameter contains hashtag.
1290 * @doesNotPerformAssertions
1292 public function testApiSearchWithHashtag()
1294 $_REQUEST['q'] = '%23friendica';
1295 $result = api_search('json');
1296 foreach ($result['status'] as $status) {
1297 self::assertStatus($status);
1298 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1303 * Test the api_search() function with an exclude_replies parameter.
1304 * @doesNotPerformAssertions
1306 public function testApiSearchWithExcludeReplies()
1308 $_REQUEST['max_id'] = 10;
1309 $_REQUEST['exclude_replies'] = true;
1310 $_REQUEST['q'] = 'friendica';
1311 $result = api_search('json');
1312 foreach ($result['status'] as $status) {
1313 self::assertStatus($status);
1318 * Test the api_search() function without an authenticated user.
1322 public function testApiSearchWithUnallowedUser()
1324 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1325 BasicAuth::setCurrentUserID();
1330 * Test the api_search() function without any GET query parameter.
1334 public function testApiSearchWithoutQuery()
1336 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1341 * Test the api_statuses_home_timeline() function.
1345 public function testApiStatusesHomeTimeline()
1347 $_REQUEST['max_id'] = 10;
1348 $_REQUEST['exclude_replies'] = true;
1349 $_REQUEST['conversation_id'] = 1;
1350 $result = api_statuses_home_timeline('json');
1351 self::assertNotEmpty($result['status']);
1352 foreach ($result['status'] as $status) {
1353 self::assertStatus($status);
1358 * Test the api_statuses_home_timeline() function with a negative page parameter.
1362 public function testApiStatusesHomeTimelineWithNegativePage()
1364 $_REQUEST['page'] = -2;
1365 $result = api_statuses_home_timeline('json');
1366 self::assertNotEmpty($result['status']);
1367 foreach ($result['status'] as $status) {
1368 self::assertStatus($status);
1373 * Test the api_statuses_home_timeline() with an unallowed user.
1377 public function testApiStatusesHomeTimelineWithUnallowedUser()
1379 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1380 BasicAuth::setCurrentUserID();
1381 api_statuses_home_timeline('json');
1385 * Test the api_statuses_home_timeline() function with an RSS result.
1389 public function testApiStatusesHomeTimelineWithRss()
1391 $result = api_statuses_home_timeline('rss');
1392 self::assertXml($result, 'statuses');
1396 * Test the api_statuses_public_timeline() function.
1400 public function testApiStatusesPublicTimeline()
1402 $_REQUEST['max_id'] = 10;
1403 $_REQUEST['conversation_id'] = 1;
1404 $result = api_statuses_public_timeline('json');
1405 self::assertNotEmpty($result['status']);
1406 foreach ($result['status'] as $status) {
1407 self::assertStatus($status);
1412 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1416 public function testApiStatusesPublicTimelineWithExcludeReplies()
1418 $_REQUEST['max_id'] = 10;
1419 $_REQUEST['exclude_replies'] = true;
1420 $result = api_statuses_public_timeline('json');
1421 self::assertNotEmpty($result['status']);
1422 foreach ($result['status'] as $status) {
1423 self::assertStatus($status);
1428 * Test the api_statuses_public_timeline() function with a negative page parameter.
1432 public function testApiStatusesPublicTimelineWithNegativePage()
1434 $_REQUEST['page'] = -2;
1435 $result = api_statuses_public_timeline('json');
1436 self::assertNotEmpty($result['status']);
1437 foreach ($result['status'] as $status) {
1438 self::assertStatus($status);
1443 * Test the api_statuses_public_timeline() function with an unallowed user.
1447 public function testApiStatusesPublicTimelineWithUnallowedUser()
1449 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1450 BasicAuth::setCurrentUserID();
1451 api_statuses_public_timeline('json');
1455 * Test the api_statuses_public_timeline() function with an RSS result.
1459 public function testApiStatusesPublicTimelineWithRss()
1461 $result = api_statuses_public_timeline('rss');
1462 self::assertXml($result, 'statuses');
1466 * Test the api_statuses_networkpublic_timeline() function.
1470 public function testApiStatusesNetworkpublicTimeline()
1472 $_REQUEST['max_id'] = 10;
1473 $result = api_statuses_networkpublic_timeline('json');
1474 self::assertNotEmpty($result['status']);
1475 foreach ($result['status'] as $status) {
1476 self::assertStatus($status);
1481 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1485 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1487 $_REQUEST['page'] = -2;
1488 $result = api_statuses_networkpublic_timeline('json');
1489 self::assertNotEmpty($result['status']);
1490 foreach ($result['status'] as $status) {
1491 self::assertStatus($status);
1496 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1500 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1502 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1503 BasicAuth::setCurrentUserID();
1504 api_statuses_networkpublic_timeline('json');
1508 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1512 public function testApiStatusesNetworkpublicTimelineWithRss()
1514 $result = api_statuses_networkpublic_timeline('rss');
1515 self::assertXml($result, 'statuses');
1519 * Test the api_statuses_show() function.
1523 public function testApiStatusesShow()
1525 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1526 api_statuses_show('json');
1530 * Test the api_statuses_show() function with an ID.
1534 public function testApiStatusesShowWithId()
1536 DI::args()->setArgv(['', '', '', 1]);
1537 $result = api_statuses_show('json');
1538 self::assertStatus($result['status']);
1542 * Test the api_statuses_show() function with the conversation parameter.
1546 public function testApiStatusesShowWithConversation()
1548 DI::args()->setArgv(['', '', '', 1]);
1549 $_REQUEST['conversation'] = 1;
1550 $result = api_statuses_show('json');
1551 self::assertNotEmpty($result['status']);
1552 foreach ($result['status'] as $status) {
1553 self::assertStatus($status);
1558 * Test the api_statuses_show() function with an unallowed user.
1562 public function testApiStatusesShowWithUnallowedUser()
1564 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1565 BasicAuth::setCurrentUserID();
1566 api_statuses_show('json');
1570 * Test the api_conversation_show() function.
1574 public function testApiConversationShow()
1576 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1577 api_conversation_show('json');
1581 * Test the api_conversation_show() function with an ID.
1585 public function testApiConversationShowWithId()
1587 DI::args()->setArgv(['', '', '', 1]);
1588 $_REQUEST['max_id'] = 10;
1589 $_REQUEST['page'] = -2;
1590 $result = api_conversation_show('json');
1591 self::assertNotEmpty($result['status']);
1592 foreach ($result['status'] as $status) {
1593 self::assertStatus($status);
1598 * Test the api_conversation_show() function with an unallowed user.
1602 public function testApiConversationShowWithUnallowedUser()
1604 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1605 BasicAuth::setCurrentUserID();
1606 api_conversation_show('json');
1610 * Test the api_statuses_repeat() function.
1614 public function testApiStatusesRepeat()
1616 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1617 api_statuses_repeat('json');
1621 * Test the api_statuses_repeat() function without an authenticated user.
1625 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1627 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1628 BasicAuth::setCurrentUserID();
1629 $_SESSION['authenticated'] = false;
1630 api_statuses_repeat('json');
1634 * Test the api_statuses_repeat() function with an ID.
1638 public function testApiStatusesRepeatWithId()
1640 DI::args()->setArgv(['', '', '', 1]);
1641 $result = api_statuses_repeat('json');
1642 self::assertStatus($result['status']);
1644 // Also test with a shared status
1645 DI::args()->setArgv(['', '', '', 5]);
1646 $result = api_statuses_repeat('json');
1647 self::assertStatus($result['status']);
1651 * Test the api_statuses_destroy() function.
1655 public function testApiStatusesDestroy()
1657 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1658 api_statuses_destroy('json');
1662 * Test the api_statuses_destroy() function without an authenticated user.
1666 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1668 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1669 BasicAuth::setCurrentUserID();
1670 $_SESSION['authenticated'] = false;
1671 api_statuses_destroy('json');
1675 * Test the api_statuses_destroy() function with an ID.
1679 public function testApiStatusesDestroyWithId()
1681 DI::args()->setArgv(['', '', '', 1]);
1682 $result = api_statuses_destroy('json');
1683 self::assertStatus($result['status']);
1687 * Test the api_statuses_mentions() function.
1691 public function testApiStatusesMentions()
1693 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1694 $_REQUEST['max_id'] = 10;
1695 $result = api_statuses_mentions('json');
1696 self::assertEmpty($result['status']);
1697 // We should test with mentions in the database.
1701 * Test the api_statuses_mentions() function with a negative page parameter.
1705 public function testApiStatusesMentionsWithNegativePage()
1707 $_REQUEST['page'] = -2;
1708 $result = api_statuses_mentions('json');
1709 self::assertEmpty($result['status']);
1713 * Test the api_statuses_mentions() function with an unallowed user.
1717 public function testApiStatusesMentionsWithUnallowedUser()
1719 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1720 BasicAuth::setCurrentUserID();
1721 api_statuses_mentions('json');
1725 * Test the api_statuses_mentions() function with an RSS result.
1729 public function testApiStatusesMentionsWithRss()
1731 $result = api_statuses_mentions('rss');
1732 self::assertXml($result, 'statuses');
1736 * Test the api_statuses_user_timeline() function.
1740 public function testApiStatusesUserTimeline()
1742 $_REQUEST['user_id'] = 42;
1743 $_REQUEST['max_id'] = 10;
1744 $_REQUEST['exclude_replies'] = true;
1745 $_REQUEST['conversation_id'] = 7;
1747 $result = api_statuses_user_timeline('json');
1748 self::assertNotEmpty($result['status']);
1749 foreach ($result['status'] as $status) {
1750 self::assertStatus($status);
1755 * Test the api_statuses_user_timeline() function with a negative page parameter.
1759 public function testApiStatusesUserTimelineWithNegativePage()
1761 $_REQUEST['user_id'] = 42;
1762 $_REQUEST['page'] = -2;
1764 $result = api_statuses_user_timeline('json');
1765 self::assertNotEmpty($result['status']);
1766 foreach ($result['status'] as $status) {
1767 self::assertStatus($status);
1772 * Test the api_statuses_user_timeline() function with an RSS result.
1776 public function testApiStatusesUserTimelineWithRss()
1778 $result = api_statuses_user_timeline('rss');
1779 self::assertXml($result, 'statuses');
1783 * Test the api_statuses_user_timeline() function with an unallowed user.
1787 public function testApiStatusesUserTimelineWithUnallowedUser()
1789 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1790 BasicAuth::setCurrentUserID();
1791 api_statuses_user_timeline('json');
1795 * Test the api_favorites_create_destroy() function.
1799 public function testApiFavoritesCreateDestroy()
1801 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1802 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1803 api_favorites_create_destroy('json');
1807 * Test the api_favorites_create_destroy() function with an invalid ID.
1811 public function testApiFavoritesCreateDestroyWithInvalidId()
1813 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1814 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1815 api_favorites_create_destroy('json');
1819 * Test the api_favorites_create_destroy() function with an invalid action.
1823 public function testApiFavoritesCreateDestroyWithInvalidAction()
1825 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1826 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1827 $_REQUEST['id'] = 1;
1828 api_favorites_create_destroy('json');
1832 * Test the api_favorites_create_destroy() function with the create action.
1836 public function testApiFavoritesCreateDestroyWithCreateAction()
1838 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1839 $_REQUEST['id'] = 3;
1840 $result = api_favorites_create_destroy('json');
1841 self::assertStatus($result['status']);
1845 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1849 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1851 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1852 $_REQUEST['id'] = 3;
1853 $result = api_favorites_create_destroy('rss');
1854 self::assertXml($result, 'status');
1858 * Test the api_favorites_create_destroy() function with the destroy action.
1862 public function testApiFavoritesCreateDestroyWithDestroyAction()
1864 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1865 $_REQUEST['id'] = 3;
1866 $result = api_favorites_create_destroy('json');
1867 self::assertStatus($result['status']);
1871 * Test the api_favorites_create_destroy() function without an authenticated user.
1875 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1877 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1878 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1879 BasicAuth::setCurrentUserID();
1880 $_SESSION['authenticated'] = false;
1881 api_favorites_create_destroy('json');
1885 * Test the api_favorites() function.
1889 public function testApiFavorites()
1891 $_REQUEST['page'] = -1;
1892 $_REQUEST['max_id'] = 10;
1893 $result = api_favorites('json');
1894 foreach ($result['status'] as $status) {
1895 self::assertStatus($status);
1900 * Test the api_favorites() function with an RSS result.
1904 public function testApiFavoritesWithRss()
1906 $result = api_favorites('rss');
1907 self::assertXml($result, 'statuses');
1911 * Test the api_favorites() function with an unallowed user.
1915 public function testApiFavoritesWithUnallowedUser()
1917 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1918 BasicAuth::setCurrentUserID();
1919 api_favorites('json');
1923 * Test the api_format_messages() function.
1927 public function testApiFormatMessages()
1929 $result = api_format_messages(
1930 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1931 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1932 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1934 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1935 self::assertEquals(1, $result['id']);
1936 self::assertEquals(2, $result['recipient_id']);
1937 self::assertEquals(3, $result['sender_id']);
1938 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1939 self::assertEquals('sender_name', $result['sender_screen_name']);
1943 * Test the api_format_messages() function with HTML.
1947 public function testApiFormatMessagesWithHtmlText()
1949 $_GET['getText'] = 'html';
1950 $result = api_format_messages(
1951 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1952 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1953 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1955 self::assertEquals('item_title', $result['title']);
1956 self::assertEquals('<strong>item_body</strong>', $result['text']);
1960 * Test the api_format_messages() function with plain text.
1964 public function testApiFormatMessagesWithPlainText()
1966 $_GET['getText'] = 'plain';
1967 $result = api_format_messages(
1968 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1969 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1970 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1972 self::assertEquals('item_title', $result['title']);
1973 self::assertEquals('item_body', $result['text']);
1977 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1981 public function testApiFormatMessagesWithoutUserObjects()
1983 $_GET['getUserObjects'] = 'false';
1984 $result = api_format_messages(
1985 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1986 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1987 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1989 self::assertTrue(!isset($result['sender']));
1990 self::assertTrue(!isset($result['recipient']));
1994 * Test the api_convert_item() function.
1998 public function testApiConvertItem()
2001 $result = api_convert_item(
2003 'network' => 'feed',
2004 'title' => 'item_title',
2006 // We need a long string to test that it is correctly cut
2007 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2008 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2009 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2010 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2011 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2012 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2013 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2014 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2015 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2016 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2017 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2018 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2019 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2020 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2021 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2022 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2023 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2024 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2025 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2026 'repellat officia illum quos impedit quam iste esse unde qui ' .
2027 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2028 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2029 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2030 'plink' => 'item_plink'
2033 self::assertStringStartsWith('item_title', $result['text']);
2034 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2039 * Test the api_convert_item() function with an empty item body.
2043 public function testApiConvertItemWithoutBody()
2046 $result = api_convert_item(
2048 'network' => 'feed',
2049 'title' => 'item_title',
2052 'plink' => 'item_plink'
2055 self::assertEquals("item_title", $result['text']);
2056 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2061 * Test the api_convert_item() function with the title in the body.
2065 public function testApiConvertItemWithTitleInBody()
2068 $result = api_convert_item(
2070 'title' => 'item_title',
2071 'body' => 'item_title item_body',
2075 self::assertEquals('item_title item_body', $result['text']);
2076 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2081 * Test the api_get_attachments() function.
2085 public function testApiGetAttachments()
2088 // self::assertEmpty(api_get_attachments($body, 0));
2092 * Test the api_get_attachments() function with an img tag.
2096 public function testApiGetAttachmentsWithImage()
2098 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2099 // self::assertIsArray(api_get_attachments($body, 0));
2103 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2107 public function testApiGetAttachmentsWithImageAndAndStatus()
2109 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2110 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2111 // self::assertIsArray(api_get_attachments($body, 0));
2115 * Test the api_get_entitities() function.
2119 public function testApiGetEntitities()
2122 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2126 * Test the api_get_entitities() function with the include_entities parameter.
2130 public function testApiGetEntititiesWithIncludeEntities()
2133 $_REQUEST['include_entities'] = 'true';
2135 $result = api_get_entitities($text, 'bbcode', 0);
2136 self::assertIsArray($result['hashtags']);
2137 self::assertIsArray($result['symbols']);
2138 self::assertIsArray($result['urls']);
2139 self::assertIsArray($result['user_mentions']);
2144 * Test the api_format_items_embeded_images() function.
2148 public function testApiFormatItemsEmbededImages()
2152 'text ' . DI::baseUrl() . '/display/item_guid',
2153 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2159 * Test the api_format_items_activities() function.
2163 public function testApiFormatItemsActivities()
2165 $item = ['uid' => 0, 'uri' => ''];
2166 $result = api_format_items_activities($item);
2167 self::assertArrayHasKey('like', $result);
2168 self::assertArrayHasKey('dislike', $result);
2169 self::assertArrayHasKey('attendyes', $result);
2170 self::assertArrayHasKey('attendno', $result);
2171 self::assertArrayHasKey('attendmaybe', $result);
2175 * Test the api_format_items_activities() function with an XML result.
2179 public function testApiFormatItemsActivitiesWithXml()
2181 $item = ['uid' => 0, 'uri' => ''];
2182 $result = api_format_items_activities($item, 'xml');
2183 self::assertArrayHasKey('friendica:like', $result);
2184 self::assertArrayHasKey('friendica:dislike', $result);
2185 self::assertArrayHasKey('friendica:attendyes', $result);
2186 self::assertArrayHasKey('friendica:attendno', $result);
2187 self::assertArrayHasKey('friendica:attendmaybe', $result);
2191 * Test the api_format_items() function.
2192 * @doesNotPerformAssertions
2194 public function testApiFormatItems()
2197 $items = Post::selectToArray([], ['uid' => 42]);
2198 foreach ($items as $item) {
2199 $status = api_format_item($item);
2200 self::assertStatus($status);
2206 * Test the api_format_items() function with an XML result.
2207 * @doesNotPerformAssertions
2209 public function testApiFormatItemsWithXml()
2212 $items = Post::selectToArray([], ['uid' => 42]);
2213 foreach ($items as $item) {
2214 $status = api_format_item($item, 'xml');
2215 self::assertStatus($status);
2221 * Test the api_lists_list() function.
2225 public function testApiListsList()
2227 $result = api_lists_list('json');
2228 self::assertEquals(['lists_list' => []], $result);
2232 * Test the api_lists_ownerships() function.
2236 public function testApiListsOwnerships()
2238 $result = api_lists_ownerships('json');
2239 foreach ($result['lists']['lists'] as $list) {
2240 self::assertList($list);
2245 * Test the api_lists_ownerships() function without an authenticated user.
2249 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2251 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2252 BasicAuth::setCurrentUserID();
2253 $_SESSION['authenticated'] = false;
2254 api_lists_ownerships('json');
2258 * Test the api_lists_statuses() function.
2262 public function testApiListsStatuses()
2264 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2265 api_lists_statuses('json');
2269 * Test the api_lists_statuses() function with a list ID.
2270 * @doesNotPerformAssertions
2272 public function testApiListsStatusesWithListId()
2274 $_REQUEST['list_id'] = 1;
2275 $_REQUEST['page'] = -1;
2276 $_REQUEST['max_id'] = 10;
2277 $result = api_lists_statuses('json');
2278 foreach ($result['status'] as $status) {
2279 self::assertStatus($status);
2284 * Test the api_lists_statuses() function with a list ID and a RSS result.
2288 public function testApiListsStatusesWithListIdAndRss()
2290 $_REQUEST['list_id'] = 1;
2291 $result = api_lists_statuses('rss');
2292 self::assertXml($result, 'statuses');
2296 * Test the api_lists_statuses() function with an unallowed user.
2300 public function testApiListsStatusesWithUnallowedUser()
2302 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2303 BasicAuth::setCurrentUserID();
2304 api_lists_statuses('json');
2308 * Test the api_statuses_f() function.
2312 public function testApiStatusesFWithFriends()
2315 $result = api_statuses_f('friends');
2316 self::assertArrayHasKey('user', $result);
2320 * Test the api_statuses_f() function.
2324 public function testApiStatusesFWithFollowers()
2326 $result = api_statuses_f('followers');
2327 self::assertArrayHasKey('user', $result);
2331 * Test the api_statuses_f() function.
2335 public function testApiStatusesFWithBlocks()
2337 $result = api_statuses_f('blocks');
2338 self::assertArrayHasKey('user', $result);
2342 * Test the api_statuses_f() function.
2346 public function testApiStatusesFWithIncoming()
2348 $result = api_statuses_f('incoming');
2349 self::assertArrayHasKey('user', $result);
2353 * Test the api_statuses_f() function an undefined cursor GET variable.
2357 public function testApiStatusesFWithUndefinedCursor()
2359 $_GET['cursor'] = 'undefined';
2360 self::assertFalse(api_statuses_f('friends'));
2364 * Test the api_statuses_friends() function.
2368 public function testApiStatusesFriends()
2370 $result = api_statuses_friends('json');
2371 self::assertArrayHasKey('user', $result);
2375 * Test the api_statuses_friends() function an undefined cursor GET variable.
2379 public function testApiStatusesFriendsWithUndefinedCursor()
2381 $_GET['cursor'] = 'undefined';
2382 self::assertFalse(api_statuses_friends('json'));
2386 * Test the api_statuses_followers() function.
2390 public function testApiStatusesFollowers()
2392 $result = api_statuses_followers('json');
2393 self::assertArrayHasKey('user', $result);
2397 * Test the api_statuses_followers() function an undefined cursor GET variable.
2401 public function testApiStatusesFollowersWithUndefinedCursor()
2403 $_GET['cursor'] = 'undefined';
2404 self::assertFalse(api_statuses_followers('json'));
2408 * Test the api_blocks_list() function.
2412 public function testApiBlocksList()
2414 $result = api_blocks_list('json');
2415 self::assertArrayHasKey('user', $result);
2419 * Test the api_blocks_list() function an undefined cursor GET variable.
2423 public function testApiBlocksListWithUndefinedCursor()
2425 $_GET['cursor'] = 'undefined';
2426 self::assertFalse(api_blocks_list('json'));
2430 * Test the api_friendships_incoming() function.
2434 public function testApiFriendshipsIncoming()
2436 $result = api_friendships_incoming('json');
2437 self::assertArrayHasKey('id', $result);
2441 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2445 public function testApiFriendshipsIncomingWithUndefinedCursor()
2447 $_GET['cursor'] = 'undefined';
2448 self::assertFalse(api_friendships_incoming('json'));
2452 * Test the api_statusnet_config() function.
2456 public function testApiStatusnetConfig()
2459 $result = api_statusnet_config('json');
2460 self::assertEquals('localhost', $result['config']['site']['server']);
2461 self::assertEquals('default', $result['config']['site']['theme']);
2462 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2463 self::assertTrue($result['config']['site']['fancy']);
2464 self::assertEquals('en', $result['config']['site']['language']);
2465 self::assertEquals('UTC', $result['config']['site']['timezone']);
2466 self::assertEquals(200000, $result['config']['site']['textlimit']);
2467 self::assertEquals('false', $result['config']['site']['private']);
2468 self::assertEquals('false', $result['config']['site']['ssl']);
2469 self::assertEquals(30, $result['config']['site']['shorturllength']);
2474 * Test the api_direct_messages_new() function.
2478 public function testApiDirectMessagesNew()
2480 $result = api_direct_messages_new('json');
2481 self::assertNull($result);
2485 * Test the api_direct_messages_new() function without an authenticated user.
2489 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2491 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2492 BasicAuth::setCurrentUserID();
2493 $_SESSION['authenticated'] = false;
2494 api_direct_messages_new('json');
2498 * Test the api_direct_messages_new() function with an user ID.
2502 public function testApiDirectMessagesNewWithUserId()
2504 $_POST['text'] = 'message_text';
2505 $_POST['user_id'] = $this->otherUser['id'];
2506 $result = api_direct_messages_new('json');
2507 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2511 * Test the api_direct_messages_new() function with a screen name.
2515 public function testApiDirectMessagesNewWithScreenName()
2517 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2518 $_POST['text'] = 'message_text';
2519 $_POST['user_id'] = $this->friendUser['id'];
2520 $result = api_direct_messages_new('json');
2521 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2522 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2523 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2527 * Test the api_direct_messages_new() function with a title.
2531 public function testApiDirectMessagesNewWithTitle()
2533 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2534 $_POST['text'] = 'message_text';
2535 $_POST['user_id'] = $this->friendUser['id'];
2536 $_REQUEST['title'] = 'message_title';
2537 $result = api_direct_messages_new('json');
2538 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2539 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2540 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2541 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2545 * Test the api_direct_messages_new() function with an RSS result.
2549 public function testApiDirectMessagesNewWithRss()
2551 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2552 $_POST['text'] = 'message_text';
2553 $_POST['user_id'] = $this->friendUser['id'];
2554 $result = api_direct_messages_new('rss');
2555 self::assertXml($result, 'direct-messages');
2559 * Test the api_direct_messages_destroy() function.
2563 public function testApiDirectMessagesDestroy()
2565 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2566 api_direct_messages_destroy('json');
2570 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2574 public function testApiDirectMessagesDestroyWithVerbose()
2576 $_GET['friendica_verbose'] = 'true';
2577 $result = api_direct_messages_destroy('json');
2581 'result' => 'error',
2582 'message' => 'message id or parenturi not specified'
2590 * Test the api_direct_messages_destroy() function without an authenticated user.
2594 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2596 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2597 BasicAuth::setCurrentUserID();
2598 $_SESSION['authenticated'] = false;
2599 api_direct_messages_destroy('json');
2603 * Test the api_direct_messages_destroy() function with a non-zero ID.
2607 public function testApiDirectMessagesDestroyWithId()
2609 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2610 $_REQUEST['id'] = 1;
2611 api_direct_messages_destroy('json');
2615 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2619 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2621 $_REQUEST['id'] = 1;
2622 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2623 $_GET['friendica_verbose'] = 'true';
2624 $result = api_direct_messages_destroy('json');
2628 'result' => 'error',
2629 'message' => 'message id not in database'
2637 * Test the api_direct_messages_destroy() function with a non-zero ID.
2641 public function testApiDirectMessagesDestroyWithCorrectId()
2643 $this->markTestIncomplete('We need to add a dataset for this.');
2647 * Test the api_direct_messages_box() function.
2651 public function testApiDirectMessagesBoxWithSentbox()
2653 $_REQUEST['page'] = -1;
2654 $_REQUEST['max_id'] = 10;
2655 $result = api_direct_messages_box('json', 'sentbox', 'false');
2656 self::assertArrayHasKey('direct_message', $result);
2660 * Test the api_direct_messages_box() function.
2664 public function testApiDirectMessagesBoxWithConversation()
2666 $result = api_direct_messages_box('json', 'conversation', 'false');
2667 self::assertArrayHasKey('direct_message', $result);
2671 * Test the api_direct_messages_box() function.
2675 public function testApiDirectMessagesBoxWithAll()
2677 $result = api_direct_messages_box('json', 'all', 'false');
2678 self::assertArrayHasKey('direct_message', $result);
2682 * Test the api_direct_messages_box() function.
2686 public function testApiDirectMessagesBoxWithInbox()
2688 $result = api_direct_messages_box('json', 'inbox', 'false');
2689 self::assertArrayHasKey('direct_message', $result);
2693 * Test the api_direct_messages_box() function.
2697 public function testApiDirectMessagesBoxWithVerbose()
2699 $result = api_direct_messages_box('json', 'sentbox', 'true');
2703 'result' => 'error',
2704 'message' => 'no mails available'
2712 * Test the api_direct_messages_box() function with a RSS result.
2716 public function testApiDirectMessagesBoxWithRss()
2718 $result = api_direct_messages_box('rss', 'sentbox', 'false');
2719 self::assertXml($result, 'direct-messages');
2723 * Test the api_direct_messages_box() function without an authenticated user.
2727 public function testApiDirectMessagesBoxWithUnallowedUser()
2729 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2730 BasicAuth::setCurrentUserID();
2731 api_direct_messages_box('json', 'sentbox', 'false');
2735 * Test the api_direct_messages_sentbox() function.
2739 public function testApiDirectMessagesSentbox()
2741 $result = api_direct_messages_sentbox('json');
2742 self::assertArrayHasKey('direct_message', $result);
2746 * Test the api_direct_messages_inbox() function.
2750 public function testApiDirectMessagesInbox()
2752 $result = api_direct_messages_inbox('json');
2753 self::assertArrayHasKey('direct_message', $result);
2757 * Test the api_direct_messages_all() function.
2761 public function testApiDirectMessagesAll()
2763 $result = api_direct_messages_all('json');
2764 self::assertArrayHasKey('direct_message', $result);
2768 * Test the api_direct_messages_conversation() function.
2772 public function testApiDirectMessagesConversation()
2774 $result = api_direct_messages_conversation('json');
2775 self::assertArrayHasKey('direct_message', $result);
2779 * Test the api_oauth_request_token() function.
2783 public function testApiOauthRequestToken()
2785 $this->markTestIncomplete('exit() kills phpunit as well');
2789 * Test the api_oauth_access_token() function.
2793 public function testApiOauthAccessToken()
2795 $this->markTestIncomplete('exit() kills phpunit as well');
2799 * Test the api_fr_photos_list() function.
2803 public function testApiFrPhotosList()
2805 $result = api_fr_photos_list('json');
2806 self::assertArrayHasKey('photo', $result);
2810 * Test the api_fr_photos_list() function without an authenticated user.
2814 public function testApiFrPhotosListWithoutAuthenticatedUser()
2816 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2817 BasicAuth::setCurrentUserID();
2818 $_SESSION['authenticated'] = false;
2819 api_fr_photos_list('json');
2823 * Test the api_fr_photo_create_update() function.
2825 public function testApiFrPhotoCreateUpdate()
2827 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2828 api_fr_photo_create_update('json');
2832 * Test the api_fr_photo_create_update() function without an authenticated user.
2836 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
2838 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2839 BasicAuth::setCurrentUserID();
2840 $_SESSION['authenticated'] = false;
2841 api_fr_photo_create_update('json');
2845 * Test the api_fr_photo_create_update() function with an album name.
2849 public function testApiFrPhotoCreateUpdateWithAlbum()
2851 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2852 $_REQUEST['album'] = 'album_name';
2853 api_fr_photo_create_update('json');
2857 * Test the api_fr_photo_create_update() function with the update mode.
2861 public function testApiFrPhotoCreateUpdateWithUpdate()
2863 $this->markTestIncomplete('We need to create a dataset for this');
2867 * Test the api_fr_photo_create_update() function with an uploaded file.
2871 public function testApiFrPhotoCreateUpdateWithFile()
2873 $this->markTestIncomplete();
2877 * Test the api_fr_photo_detail() function.
2881 public function testApiFrPhotoDetail()
2883 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2884 api_fr_photo_detail('json');
2888 * Test the api_fr_photo_detail() function without an authenticated user.
2892 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
2894 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2895 BasicAuth::setCurrentUserID();
2896 $_SESSION['authenticated'] = false;
2897 api_fr_photo_detail('json');
2901 * Test the api_fr_photo_detail() function with a photo ID.
2905 public function testApiFrPhotoDetailWithPhotoId()
2907 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
2908 $_REQUEST['photo_id'] = 1;
2909 api_fr_photo_detail('json');
2913 * Test the api_fr_photo_detail() function with a correct photo ID.
2917 public function testApiFrPhotoDetailCorrectPhotoId()
2919 $this->markTestIncomplete('We need to create a dataset for this.');
2923 * Test the api_account_update_profile_image() function.
2927 public function testApiAccountUpdateProfileImage()
2929 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2930 api_account_update_profile_image('json');
2934 * Test the api_account_update_profile_image() function without an authenticated user.
2938 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2940 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2941 BasicAuth::setCurrentUserID();
2942 $_SESSION['authenticated'] = false;
2943 api_account_update_profile_image('json');
2947 * Test the api_account_update_profile_image() function with an uploaded file.
2951 public function testApiAccountUpdateProfileImageWithUpload()
2953 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2954 $this->markTestIncomplete();
2959 * Test the api_account_update_profile() function.
2963 public function testApiAccountUpdateProfile()
2965 $_POST['name'] = 'new_name';
2966 $_POST['description'] = 'new_description';
2967 $result = api_account_update_profile('json');
2968 // We can't use assertSelfUser() here because the user object is missing some properties.
2969 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
2970 self::assertEquals('DFRN', $result['user']['location']);
2971 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
2972 self::assertEquals('new_name', $result['user']['name']);
2973 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();