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::assertEquals('method', $API['api_path']['method']);
299 self::assertTrue(is_callable($API['api_path']['func']));
303 * Test the BasicAuth::getCurrentUserID() function without any login.
305 * @runInSeparateProcess
306 * @preserveGlobalState disabled
307 * @preserveGlobalState disabled
309 public function testApiLoginWithoutLogin()
311 BasicAuth::setCurrentUserID();
312 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
313 BasicAuth::getCurrentUserID(true);
317 * Test the BasicAuth::getCurrentUserID() function with a bad login.
319 * @runInSeparateProcess
320 * @preserveGlobalState disabled
321 * @preserveGlobalState disabled
323 public function testApiLoginWithBadLogin()
325 BasicAuth::setCurrentUserID();
326 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
327 $_SERVER['PHP_AUTH_USER'] = 'user@server';
328 BasicAuth::getCurrentUserID(true);
332 * Test the BasicAuth::getCurrentUserID() function with oAuth.
336 public function testApiLoginWithOauth()
338 $this->markTestIncomplete('Can we test this easily?');
342 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
346 public function testApiLoginWithAddonAuth()
348 $this->markTestIncomplete('Can we test this easily?');
352 * Test the BasicAuth::getCurrentUserID() function with a correct login.
354 * @runInSeparateProcess
355 * @preserveGlobalState disabled
356 * @doesNotPerformAssertions
358 public function testApiLoginWithCorrectLogin()
360 BasicAuth::setCurrentUserID();
361 $_SERVER['PHP_AUTH_USER'] = 'Test user';
362 $_SERVER['PHP_AUTH_PW'] = 'password';
363 BasicAuth::getCurrentUserID(true);
367 * Test the BasicAuth::getCurrentUserID() function with a remote user.
369 * @runInSeparateProcess
370 * @preserveGlobalState disabled
372 public function testApiLoginWithRemoteUser()
374 BasicAuth::setCurrentUserID();
375 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
376 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
377 BasicAuth::getCurrentUserID(true);
381 * Test the api_call() function.
383 * @runInSeparateProcess
384 * @preserveGlobalState disabled
386 public function testApiCall()
390 'method' => 'method',
391 'func' => function () {
392 return ['data' => ['some_data']];
395 $_SERVER['REQUEST_METHOD'] = 'method';
396 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
397 $_GET['callback'] = 'callback_name';
400 'callback_name(["some_data"])',
401 api_call('api_path', 'json')
406 * Test the api_call() function with the profiled enabled.
408 * @runInSeparateProcess
409 * @preserveGlobalState disabled
411 public function testApiCallWithProfiler()
415 'method' => 'method',
416 'func' => function () {
417 return ['data' => ['some_data']];
421 $_SERVER['REQUEST_METHOD'] = 'method';
422 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
424 $this->config->set('system', 'profiler', true);
425 $this->config->set('rendertime', 'callstack', true);
426 $this->app->callstack = [
427 'database' => ['some_function' => 200],
428 'database_write' => ['some_function' => 200],
429 'cache' => ['some_function' => 200],
430 'cache_write' => ['some_function' => 200],
431 'network' => ['some_function' => 200]
436 api_call('api_path', 'json')
441 * Test the api_call() function with a JSON result.
443 * @runInSeparateProcess
444 * @preserveGlobalState disabled
446 public function testApiCallWithJson()
450 'method' => 'method',
451 'func' => function () {
452 return ['data' => ['some_data']];
455 $_SERVER['REQUEST_METHOD'] = 'method';
456 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
460 api_call('api_path.json', 'json')
465 * Test the api_call() function with an XML result.
467 * @runInSeparateProcess
468 * @preserveGlobalState disabled
470 public function testApiCallWithXml()
474 'method' => 'method',
475 'func' => function () {
479 $_SERVER['REQUEST_METHOD'] = 'method';
480 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
482 $args = DI::args()->determine($_SERVER, $_GET);
486 api_call('api_path.xml', 'xml')
491 * Test the api_call() function with an RSS result.
493 * @runInSeparateProcess
494 * @preserveGlobalState disabled
496 public function testApiCallWithRss()
500 'method' => 'method',
501 'func' => function () {
505 $_SERVER['REQUEST_METHOD'] = 'method';
506 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
509 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
511 api_call('api_path.rss', 'rss')
516 * Test the api_call() function with an Atom result.
518 * @runInSeparateProcess
519 * @preserveGlobalState disabled
521 public function testApiCallWithAtom()
525 'method' => 'method',
526 'func' => function () {
530 $_SERVER['REQUEST_METHOD'] = 'method';
531 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
534 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
536 api_call('api_path.atom', 'atom')
541 * Test the api_rss_extra() function.
545 public function testApiRssExtra()
548 $user_info = ['url' => 'user_url', 'lang' => 'en'];
549 $result = api_rss_extra([], $user_info);
550 self::assertEquals($user_info, $result['$user']);
551 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
552 self::assertArrayHasKey('self', $result['$rss']);
553 self::assertArrayHasKey('base', $result['$rss']);
554 self::assertArrayHasKey('updated', $result['$rss']);
555 self::assertArrayHasKey('atom_updated', $result['$rss']);
556 self::assertArrayHasKey('language', $result['$rss']);
557 self::assertArrayHasKey('logo', $result['$rss']);
562 * Test the api_rss_extra() function without any user info.
566 public function testApiRssExtraWithoutUserInfo()
569 $result = api_rss_extra([], null);
570 self::assertIsArray($result['$user']);
571 self::assertArrayHasKey('alternate', $result['$rss']);
572 self::assertArrayHasKey('self', $result['$rss']);
573 self::assertArrayHasKey('base', $result['$rss']);
574 self::assertArrayHasKey('updated', $result['$rss']);
575 self::assertArrayHasKey('atom_updated', $result['$rss']);
576 self::assertArrayHasKey('language', $result['$rss']);
577 self::assertArrayHasKey('logo', $result['$rss']);
582 * Test the api_get_user() function.
586 public function testApiGetUser()
588 // $user = api_get_user();
589 // self::assertSelfUser($user);
590 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
591 // self::assertEquals('6fdbe8', $user['profile_link_color']);
592 // self::assertEquals('ededed', $user['profile_background_color']);
596 * Test the api_get_user() function with a Frio schema.
600 public function testApiGetUserWithFrioSchema()
602 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
603 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
604 // $user = api_get_user();
605 // self::assertSelfUser($user);
606 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
607 // self::assertEquals('6fdbe8', $user['profile_link_color']);
608 // self::assertEquals('ededed', $user['profile_background_color']);
612 * Test the api_get_user() function with an empty Frio schema.
616 public function testApiGetUserWithEmptyFrioSchema()
618 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
619 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
620 // $user = api_get_user();
621 // self::assertSelfUser($user);
622 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
623 // self::assertEquals('6fdbe8', $user['profile_link_color']);
624 // self::assertEquals('ededed', $user['profile_background_color']);
628 * Test the api_get_user() function with a custom Frio schema.
632 public function testApiGetUserWithCustomFrioSchema()
634 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
635 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
636 // $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
637 // $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
638 // $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
639 // $user = api_get_user();
640 // self::assertSelfUser($user);
641 // self::assertEquals('123456', $user['profile_sidebar_fill_color']);
642 // self::assertEquals('123456', $user['profile_link_color']);
643 // self::assertEquals('123456', $user['profile_background_color']);
647 * Test the api_get_user() function with an user that is not allowed to use the API.
649 * @runInSeparateProcess
650 * @preserveGlobalState disabled
652 public function testApiGetUserWithoutApiUser()
654 // api_get_user() with empty parameters is not used anymore
656 $_SERVER['PHP_AUTH_USER'] = 'Test user';
657 $_SERVER['PHP_AUTH_PW'] = 'password';
658 BasicAuth::setCurrentUserID();
659 self::assertFalse(api_get_user());
664 * Test the api_get_user() function with an user ID in a GET parameter.
668 public function testApiGetUserWithGetId()
670 // self::assertOtherUser(api_get_user());
674 * Test the api_get_user() function with a wrong user ID in a GET parameter.
678 public function testApiGetUserWithWrongGetId()
680 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
681 // self::assertOtherUser(api_get_user());
685 * Test the api_get_user() function with an user name in a GET parameter.
689 public function testApiGetUserWithGetName()
691 // self::assertSelfUser(api_get_user());
695 * Test the api_get_user() function with a profile URL in a GET parameter.
699 public function testApiGetUserWithGetUrl()
701 // self::assertSelfUser(api_get_user());
705 * Test the api_get_user() function with an user ID in the API path.
709 public function testApiGetUserWithNumericCalledApi()
711 // global $called_api;
712 // $called_api = ['api_path'];
713 // DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
714 // self::assertOtherUser(api_get_user());
718 * Test the api_get_user() function with the $called_api global variable.
722 public function testApiGetUserWithCalledApi()
724 // global $called_api;
725 // $called_api = ['api', 'api_path'];
726 // self::assertSelfUser(api_get_user());
730 * Test the Arrays::walkRecursive() function.
734 public function testApiWalkRecursive()
739 Arrays::walkRecursive(
742 // Should we test this with a callback that actually does something?
750 * Test the Arrays::walkRecursive() function with an array.
754 public function testApiWalkRecursiveWithArray()
756 $array = [['item1'], ['item2']];
759 Arrays::walkRecursive(
762 // Should we test this with a callback that actually does something?
770 * Test the BaseApi::reformatXML() function.
774 public function testApiReformatXml()
778 self::assertTrue(ApiResponse::reformatXML($item, $key));
779 self::assertEquals('true', $item);
783 * Test the BaseApi::reformatXML() function with a statusnet_api key.
787 public function testApiReformatXmlWithStatusnetKey()
790 $key = 'statusnet_api';
791 self::assertTrue(ApiResponse::reformatXML($item, $key));
792 self::assertEquals('statusnet:api', $key);
796 * Test the BaseApi::reformatXML() function with a friendica_api key.
800 public function testApiReformatXmlWithFriendicaKey()
803 $key = 'friendica_api';
804 self::assertTrue(ApiResponse::reformatXML($item, $key));
805 self::assertEquals('friendica:api', $key);
809 * Test the BaseApi::createXML() function.
813 public function testApiCreateXml()
816 '<?xml version="1.0"?>' . "\n" .
817 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
818 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
819 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
820 ' <data>some_data</data>' . "\n" .
821 '</root_element>' . "\n",
822 DI::apiResponse()->createXML(['data' => ['some_data']], 'root_element')
827 * Test the BaseApi::createXML() function without any XML namespace.
831 public function testApiCreateXmlWithoutNamespaces()
834 '<?xml version="1.0"?>' . "\n" .
836 ' <data>some_data</data>' . "\n" .
838 DI::apiResponse()->createXML(['data' => ['some_data']], 'ok')
843 * Test the BaseApi::formatData() function.
847 public function testApiFormatData()
849 $data = ['some_data'];
850 self::assertEquals($data, DI::apiResponse()->formatData('root_element', 'json', $data));
854 * Test the BaseApi::formatData() function with an XML result.
858 public function testApiFormatDataWithXml()
861 '<?xml version="1.0"?>' . "\n" .
862 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
863 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
864 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
865 ' <data>some_data</data>' . "\n" .
866 '</root_element>' . "\n",
867 DI::apiResponse()->formatData('root_element', 'xml', ['data' => ['some_data']])
872 * Test the api_account_verify_credentials() function.
876 public function testApiAccountVerifyCredentials()
878 // self::assertArrayHasKey('user', api_account_verify_credentials('json'));
882 * Test the api_account_verify_credentials() function without an authenticated user.
886 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
888 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
889 // BasicAuth::setCurrentUserID();
890 // $_SESSION['authenticated'] = false;
891 // api_account_verify_credentials('json');
895 * Test the api_statuses_mediap() function.
899 public function testApiStatusesMediap()
901 DI::args()->setArgc(2);
909 'tmp_name' => $this->getTempImage(),
910 'name' => 'spacer.png',
911 'type' => 'image/png'
914 $_GET['status'] = '<b>Status content</b>';
916 $result = api_statuses_mediap('json');
917 self::assertStatus($result['status']);
921 * Test the api_statuses_mediap() function without an authenticated user.
925 public function testApiStatusesMediapWithoutAuthenticatedUser()
927 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
928 BasicAuth::setCurrentUserID();
929 $_SESSION['authenticated'] = false;
930 api_statuses_mediap('json');
934 * Test the api_statuses_update() function.
938 public function testApiStatusesUpdate()
940 $_REQUEST['status'] = 'Status content #friendica';
941 $_REQUEST['in_reply_to_status_id'] = -1;
942 $_REQUEST['lat'] = 48;
943 $_REQUEST['long'] = 7;
950 'tmp_name' => $this->getTempImage(),
951 'name' => 'spacer.png',
952 'type' => 'image/png'
956 $result = api_statuses_update('json');
957 self::assertStatus($result['status']);
961 * Test the api_statuses_update() function with an HTML status.
965 public function testApiStatusesUpdateWithHtml()
967 $_REQUEST['htmlstatus'] = '<b>Status content</b>';
969 $result = api_statuses_update('json');
970 self::assertStatus($result['status']);
974 * Test the api_statuses_update() function without an authenticated user.
978 public function testApiStatusesUpdateWithoutAuthenticatedUser()
980 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
981 BasicAuth::setCurrentUserID();
982 $_SESSION['authenticated'] = false;
983 api_statuses_update('json');
987 * Test the api_statuses_update() function with a parent status.
991 public function testApiStatusesUpdateWithParent()
993 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
997 * Test the api_statuses_update() function with a media_ids parameter.
1001 public function testApiStatusesUpdateWithMediaIds()
1003 $this->markTestIncomplete();
1007 * Test the api_statuses_update() function with the throttle limit reached.
1011 public function testApiStatusesUpdateWithDayThrottleReached()
1013 $this->markTestIncomplete();
1017 * Test the api_media_upload() function.
1018 * @runInSeparateProcess
1019 * @preserveGlobalState disabled
1021 public function testApiMediaUpload()
1023 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1028 * Test the api_media_upload() function without an authenticated user.
1032 public function testApiMediaUploadWithoutAuthenticatedUser()
1034 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1035 BasicAuth::setCurrentUserID();
1036 $_SESSION['authenticated'] = false;
1041 * Test the api_media_upload() function with an invalid uploaded media.
1045 public function testApiMediaUploadWithMedia()
1047 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1051 'tmp_name' => 'tmp_name'
1058 * Test the api_media_upload() function with an valid uploaded media.
1062 public function testApiMediaUploadWithValidMedia()
1070 'tmp_name' => $this->getTempImage(),
1071 'name' => 'spacer.png',
1072 'type' => 'image/png'
1076 DI::args()->setArgc(2);
1078 $result = api_media_upload();
1079 self::assertEquals('image/png', $result['media']['image']['image_type']);
1080 self::assertEquals(1, $result['media']['image']['w']);
1081 self::assertEquals(1, $result['media']['image']['h']);
1082 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1086 * Test the api_status_show() function.
1088 public function testApiStatusShowWithJson()
1090 // $result = api_status_show('json', 1);
1091 // self::assertStatus($result['status']);
1095 * Test the api_status_show() function with an XML result.
1097 public function testApiStatusShowWithXml()
1099 // $result = api_status_show('xml', 1);
1100 // self::assertXml($result, 'statuses');
1104 * Test the api_get_last_status() function
1106 public function testApiGetLastStatus()
1108 // $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1109 // self::assertNotNull($item);
1113 * Test the api_users_show() function.
1117 public function testApiUsersShow()
1120 $result = api_users_show('json');
1121 // We can't use assertSelfUser() here because the user object is missing some properties.
1122 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1123 self::assertEquals('DFRN', $result['user']['location']);
1124 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1125 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1126 self::assertTrue($result['user']['verified']);
1131 * Test the api_users_show() function with an XML result.
1135 public function testApiUsersShowWithXml()
1137 // $result = api_users_show('xml');
1138 // self::assertXml($result, 'statuses');
1142 * Test the api_users_search() function.
1146 public function testApiUsersSearch()
1148 // $_GET['q'] = 'othercontact';
1149 // $result = api_users_search('json');
1150 // self::assertOtherUser($result['users'][0]);
1154 * Test the api_users_search() function with an XML result.
1158 public function testApiUsersSearchWithXml()
1160 // $_GET['q'] = 'othercontact';
1161 // $result = api_users_search('xml');
1162 // self::assertXml($result, 'users');
1166 * Test the api_users_search() function without a GET q parameter.
1170 public function testApiUsersSearchWithoutQuery()
1172 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1173 // api_users_search('json');
1177 * Test the api_users_lookup() function.
1181 public function testApiUsersLookup()
1183 // $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1184 // api_users_lookup('json');
1188 * Test the api_users_lookup() function with an user ID.
1192 public function testApiUsersLookupWithUserId()
1194 // $_REQUEST['user_id'] = $this->otherUser['id'];
1195 // $result = api_users_lookup('json');
1196 // self::assertOtherUser($result['users'][0]);
1200 * Test the api_search() function.
1204 public function testApiSearch()
1207 $_REQUEST['q'] = 'reply';
1208 $_REQUEST['max_id'] = 10;
1209 $result = api_search('json');
1210 foreach ($result['status'] as $status) {
1211 self::assertStatus($status);
1212 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1218 * Test the api_search() function a count parameter.
1222 public function testApiSearchWithCount()
1225 $_REQUEST['q'] = 'reply';
1226 $_REQUEST['count'] = 20;
1227 $result = api_search('json');
1228 foreach ($result['status'] as $status) {
1229 self::assertStatus($status);
1230 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1236 * Test the api_search() function with an rpp parameter.
1240 public function testApiSearchWithRpp()
1243 $_REQUEST['q'] = 'reply';
1244 $_REQUEST['rpp'] = 20;
1245 $result = api_search('json');
1246 foreach ($result['status'] as $status) {
1247 self::assertStatus($status);
1248 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1254 * Test the api_search() function with an q parameter contains hashtag.
1255 * @doesNotPerformAssertions
1257 public function testApiSearchWithHashtag()
1260 $_REQUEST['q'] = '%23friendica';
1261 $result = api_search('json');
1262 foreach ($result['status'] as $status) {
1263 self::assertStatus($status);
1264 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1270 * Test the api_search() function with an exclude_replies parameter.
1271 * @doesNotPerformAssertions
1273 public function testApiSearchWithExcludeReplies()
1276 $_REQUEST['max_id'] = 10;
1277 $_REQUEST['exclude_replies'] = true;
1278 $_REQUEST['q'] = 'friendica';
1279 $result = api_search('json');
1280 foreach ($result['status'] as $status) {
1281 self::assertStatus($status);
1287 * Test the api_search() function without an authenticated user.
1291 public function testApiSearchWithUnallowedUser()
1293 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1294 // BasicAuth::setCurrentUserID();
1295 // api_search('json');
1299 * Test the api_search() function without any GET query parameter.
1303 public function testApiSearchWithoutQuery()
1305 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1306 // api_search('json');
1310 * Test the api_statuses_home_timeline() function.
1314 public function testApiStatusesHomeTimeline()
1317 $_REQUEST['max_id'] = 10;
1318 $_REQUEST['exclude_replies'] = true;
1319 $_REQUEST['conversation_id'] = 1;
1320 $result = api_statuses_home_timeline('json');
1321 self::assertNotEmpty($result['status']);
1322 foreach ($result['status'] as $status) {
1323 self::assertStatus($status);
1329 * Test the api_statuses_home_timeline() function with a negative page parameter.
1333 public function testApiStatusesHomeTimelineWithNegativePage()
1336 $_REQUEST['page'] = -2;
1337 $result = api_statuses_home_timeline('json');
1338 self::assertNotEmpty($result['status']);
1339 foreach ($result['status'] as $status) {
1340 self::assertStatus($status);
1346 * Test the api_statuses_home_timeline() with an unallowed user.
1350 public function testApiStatusesHomeTimelineWithUnallowedUser()
1353 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1354 BasicAuth::setCurrentUserID();
1355 api_statuses_home_timeline('json');
1360 * Test the api_statuses_home_timeline() function with an RSS result.
1364 public function testApiStatusesHomeTimelineWithRss()
1366 // $result = api_statuses_home_timeline('rss');
1367 // self::assertXml($result, 'statuses');
1371 * Test the api_statuses_public_timeline() function.
1375 public function testApiStatusesPublicTimeline()
1378 $_REQUEST['max_id'] = 10;
1379 $_REQUEST['conversation_id'] = 1;
1380 $result = api_statuses_public_timeline('json');
1381 self::assertNotEmpty($result['status']);
1382 foreach ($result['status'] as $status) {
1383 self::assertStatus($status);
1389 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1393 public function testApiStatusesPublicTimelineWithExcludeReplies()
1396 $_REQUEST['max_id'] = 10;
1397 $_REQUEST['exclude_replies'] = true;
1398 $result = api_statuses_public_timeline('json');
1399 self::assertNotEmpty($result['status']);
1400 foreach ($result['status'] as $status) {
1401 self::assertStatus($status);
1407 * Test the api_statuses_public_timeline() function with a negative page parameter.
1411 public function testApiStatusesPublicTimelineWithNegativePage()
1414 $_REQUEST['page'] = -2;
1415 $result = api_statuses_public_timeline('json');
1416 self::assertNotEmpty($result['status']);
1417 foreach ($result['status'] as $status) {
1418 self::assertStatus($status);
1424 * Test the api_statuses_public_timeline() function with an unallowed user.
1428 public function testApiStatusesPublicTimelineWithUnallowedUser()
1430 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1431 // BasicAuth::setCurrentUserID();
1432 // api_statuses_public_timeline('json');
1436 * Test the api_statuses_public_timeline() function with an RSS result.
1440 public function testApiStatusesPublicTimelineWithRss()
1442 // $result = api_statuses_public_timeline('rss');
1443 // self::assertXml($result, 'statuses');
1447 * Test the api_statuses_networkpublic_timeline() function.
1451 public function testApiStatusesNetworkpublicTimeline()
1454 $_REQUEST['max_id'] = 10;
1455 $result = api_statuses_networkpublic_timeline('json');
1456 self::assertNotEmpty($result['status']);
1457 foreach ($result['status'] as $status) {
1458 self::assertStatus($status);
1464 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1468 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1471 $_REQUEST['page'] = -2;
1472 $result = api_statuses_networkpublic_timeline('json');
1473 self::assertNotEmpty($result['status']);
1474 foreach ($result['status'] as $status) {
1475 self::assertStatus($status);
1481 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1485 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1487 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1488 // BasicAuth::setCurrentUserID();
1489 // api_statuses_networkpublic_timeline('json');
1493 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1497 public function testApiStatusesNetworkpublicTimelineWithRss()
1499 // $result = api_statuses_networkpublic_timeline('rss');
1500 // self::assertXml($result, 'statuses');
1504 * Test the api_statuses_show() function.
1508 public function testApiStatusesShow()
1510 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1511 // api_statuses_show('json');
1515 * Test the api_statuses_show() function with an ID.
1519 public function testApiStatusesShowWithId()
1521 // DI::args()->setArgv(['', '', '', 1]);
1522 // $result = api_statuses_show('json');
1523 // self::assertStatus($result['status']);
1527 * Test the api_statuses_show() function with the conversation parameter.
1531 public function testApiStatusesShowWithConversation()
1534 DI::args()->setArgv(['', '', '', 1]);
1535 $_REQUEST['conversation'] = 1;
1536 $result = api_statuses_show('json');
1537 self::assertNotEmpty($result['status']);
1538 foreach ($result['status'] as $status) {
1539 self::assertStatus($status);
1545 * Test the api_statuses_show() function with an unallowed user.
1549 public function testApiStatusesShowWithUnallowedUser()
1551 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1552 // BasicAuth::setCurrentUserID();
1553 // api_statuses_show('json');
1557 * Test the api_conversation_show() function.
1561 public function testApiConversationShow()
1563 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1564 // api_conversation_show('json');
1568 * Test the api_conversation_show() function with an ID.
1572 public function testApiConversationShowWithId()
1575 DI::args()->setArgv(['', '', '', 1]);
1576 $_REQUEST['max_id'] = 10;
1577 $_REQUEST['page'] = -2;
1578 $result = api_conversation_show('json');
1579 self::assertNotEmpty($result['status']);
1580 foreach ($result['status'] as $status) {
1581 self::assertStatus($status);
1587 * Test the api_conversation_show() function with an unallowed user.
1591 public function testApiConversationShowWithUnallowedUser()
1593 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1594 // BasicAuth::setCurrentUserID();
1595 // api_conversation_show('json');
1599 * Test the api_statuses_repeat() function.
1603 public function testApiStatusesRepeat()
1605 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1606 api_statuses_repeat('json');
1610 * Test the api_statuses_repeat() function without an authenticated user.
1614 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1616 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1617 BasicAuth::setCurrentUserID();
1618 $_SESSION['authenticated'] = false;
1619 api_statuses_repeat('json');
1623 * Test the api_statuses_repeat() function with an ID.
1627 public function testApiStatusesRepeatWithId()
1629 DI::args()->setArgv(['', '', '', 1]);
1630 $result = api_statuses_repeat('json');
1631 self::assertStatus($result['status']);
1633 // Also test with a shared status
1634 DI::args()->setArgv(['', '', '', 5]);
1635 $result = api_statuses_repeat('json');
1636 self::assertStatus($result['status']);
1640 * Test the api_statuses_destroy() function.
1644 public function testApiStatusesDestroy()
1646 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1647 // api_statuses_destroy('json');
1651 * Test the api_statuses_destroy() function without an authenticated user.
1655 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1657 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1658 // BasicAuth::setCurrentUserID();
1659 // $_SESSION['authenticated'] = false;
1660 // api_statuses_destroy('json');
1664 * Test the api_statuses_destroy() function with an ID.
1668 public function testApiStatusesDestroyWithId()
1670 // DI::args()->setArgv(['', '', '', 1]);
1671 // $result = api_statuses_destroy('json');
1672 // self::assertStatus($result['status']);
1676 * Test the api_statuses_mentions() function.
1680 public function testApiStatusesMentions()
1683 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1684 $_REQUEST['max_id'] = 10;
1685 $result = api_statuses_mentions('json');
1686 self::assertEmpty($result['status']);
1687 // We should test with mentions in the database.
1692 * Test the api_statuses_mentions() function with a negative page parameter.
1696 public function testApiStatusesMentionsWithNegativePage()
1698 // $_REQUEST['page'] = -2;
1699 // $result = api_statuses_mentions('json');
1700 // self::assertEmpty($result['status']);
1704 * Test the api_statuses_mentions() function with an unallowed user.
1708 public function testApiStatusesMentionsWithUnallowedUser()
1710 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1711 // BasicAuth::setCurrentUserID();
1712 // api_statuses_mentions('json');
1716 * Test the api_statuses_mentions() function with an RSS result.
1720 public function testApiStatusesMentionsWithRss()
1722 // $result = api_statuses_mentions('rss');
1723 // self::assertXml($result, 'statuses');
1727 * Test the api_statuses_user_timeline() function.
1731 public function testApiStatusesUserTimeline()
1734 $_REQUEST['user_id'] = 42;
1735 $_REQUEST['max_id'] = 10;
1736 $_REQUEST['exclude_replies'] = true;
1737 $_REQUEST['conversation_id'] = 7;
1739 $result = api_statuses_user_timeline('json');
1740 self::assertNotEmpty($result['status']);
1741 foreach ($result['status'] as $status) {
1742 self::assertStatus($status);
1748 * Test the api_statuses_user_timeline() function with a negative page parameter.
1752 public function testApiStatusesUserTimelineWithNegativePage()
1755 $_REQUEST['user_id'] = 42;
1756 $_REQUEST['page'] = -2;
1758 $result = api_statuses_user_timeline('json');
1759 self::assertNotEmpty($result['status']);
1760 foreach ($result['status'] as $status) {
1761 self::assertStatus($status);
1767 * Test the api_statuses_user_timeline() function with an RSS result.
1771 public function testApiStatusesUserTimelineWithRss()
1773 // $result = api_statuses_user_timeline('rss');
1774 // self::assertXml($result, 'statuses');
1778 * Test the api_statuses_user_timeline() function with an unallowed user.
1782 public function testApiStatusesUserTimelineWithUnallowedUser()
1784 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1785 // BasicAuth::setCurrentUserID();
1786 // api_statuses_user_timeline('json');
1790 * Test the api_favorites_create_destroy() function.
1794 public function testApiFavoritesCreateDestroy()
1796 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1797 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1798 api_favorites_create_destroy('json');
1802 * Test the api_favorites_create_destroy() function with an invalid ID.
1806 public function testApiFavoritesCreateDestroyWithInvalidId()
1808 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1809 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1810 api_favorites_create_destroy('json');
1814 * Test the api_favorites_create_destroy() function with an invalid action.
1818 public function testApiFavoritesCreateDestroyWithInvalidAction()
1820 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1821 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1822 $_REQUEST['id'] = 1;
1823 api_favorites_create_destroy('json');
1827 * Test the api_favorites_create_destroy() function with the create action.
1831 public function testApiFavoritesCreateDestroyWithCreateAction()
1833 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1834 $_REQUEST['id'] = 3;
1835 $result = api_favorites_create_destroy('json');
1836 self::assertStatus($result['status']);
1840 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1844 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1846 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1847 $_REQUEST['id'] = 3;
1848 $result = api_favorites_create_destroy('rss');
1849 self::assertXml($result, 'status');
1853 * Test the api_favorites_create_destroy() function with the destroy action.
1857 public function testApiFavoritesCreateDestroyWithDestroyAction()
1859 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1860 $_REQUEST['id'] = 3;
1861 $result = api_favorites_create_destroy('json');
1862 self::assertStatus($result['status']);
1866 * Test the api_favorites_create_destroy() function without an authenticated user.
1870 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1872 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1873 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1874 BasicAuth::setCurrentUserID();
1875 $_SESSION['authenticated'] = false;
1876 api_favorites_create_destroy('json');
1880 * Test the api_favorites() function.
1884 public function testApiFavorites()
1887 $_REQUEST['page'] = -1;
1888 $_REQUEST['max_id'] = 10;
1889 $result = api_favorites('json');
1890 foreach ($result['status'] as $status) {
1891 self::assertStatus($status);
1897 * Test the api_favorites() function with an RSS result.
1901 public function testApiFavoritesWithRss()
1903 // $result = api_favorites('rss');
1904 // self::assertXml($result, 'statuses');
1908 * Test the api_favorites() function with an unallowed user.
1912 public function testApiFavoritesWithUnallowedUser()
1914 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1915 // BasicAuth::setCurrentUserID();
1916 // api_favorites('json');
1920 * Test the api_format_messages() function.
1924 public function testApiFormatMessages()
1926 $result = api_format_messages(
1927 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1928 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1929 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1931 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1932 self::assertEquals(1, $result['id']);
1933 self::assertEquals(2, $result['recipient_id']);
1934 self::assertEquals(3, $result['sender_id']);
1935 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1936 self::assertEquals('sender_name', $result['sender_screen_name']);
1940 * Test the api_format_messages() function with HTML.
1944 public function testApiFormatMessagesWithHtmlText()
1946 $_GET['getText'] = 'html';
1947 $result = api_format_messages(
1948 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1949 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1950 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1952 self::assertEquals('item_title', $result['title']);
1953 self::assertEquals('<strong>item_body</strong>', $result['text']);
1957 * Test the api_format_messages() function with plain text.
1961 public function testApiFormatMessagesWithPlainText()
1963 $_GET['getText'] = 'plain';
1964 $result = api_format_messages(
1965 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1966 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1967 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1969 self::assertEquals('item_title', $result['title']);
1970 self::assertEquals('item_body', $result['text']);
1974 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1978 public function testApiFormatMessagesWithoutUserObjects()
1980 $_GET['getUserObjects'] = 'false';
1981 $result = api_format_messages(
1982 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1983 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1984 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1986 self::assertTrue(!isset($result['sender']));
1987 self::assertTrue(!isset($result['recipient']));
1991 * Test the api_convert_item() function.
1995 public function testApiConvertItem()
1998 $result = api_convert_item(
2000 'network' => 'feed',
2001 'title' => 'item_title',
2003 // We need a long string to test that it is correctly cut
2004 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2005 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2006 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2007 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2008 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2009 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2010 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2011 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2012 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2013 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2014 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2015 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2016 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2017 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2018 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2019 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2020 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2021 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2022 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2023 'repellat officia illum quos impedit quam iste esse unde qui ' .
2024 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2025 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2026 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2027 'plink' => 'item_plink'
2030 self::assertStringStartsWith('item_title', $result['text']);
2031 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2036 * Test the api_convert_item() function with an empty item body.
2040 public function testApiConvertItemWithoutBody()
2043 $result = api_convert_item(
2045 'network' => 'feed',
2046 'title' => 'item_title',
2049 'plink' => 'item_plink'
2052 self::assertEquals("item_title", $result['text']);
2053 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2058 * Test the api_convert_item() function with the title in the body.
2062 public function testApiConvertItemWithTitleInBody()
2065 $result = api_convert_item(
2067 'title' => 'item_title',
2068 'body' => 'item_title item_body',
2072 self::assertEquals('item_title item_body', $result['text']);
2073 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2078 * Test the api_get_attachments() function.
2082 public function testApiGetAttachments()
2085 // self::assertEmpty(api_get_attachments($body, 0));
2089 * Test the api_get_attachments() function with an img tag.
2093 public function testApiGetAttachmentsWithImage()
2095 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2096 // self::assertIsArray(api_get_attachments($body, 0));
2100 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2104 public function testApiGetAttachmentsWithImageAndAndStatus()
2106 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2107 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2108 // self::assertIsArray(api_get_attachments($body, 0));
2112 * Test the api_get_entitities() function.
2116 public function testApiGetEntitities()
2119 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2123 * Test the api_get_entitities() function with the include_entities parameter.
2127 public function testApiGetEntititiesWithIncludeEntities()
2130 $_REQUEST['include_entities'] = 'true';
2132 $result = api_get_entitities($text, 'bbcode', 0);
2133 self::assertIsArray($result['hashtags']);
2134 self::assertIsArray($result['symbols']);
2135 self::assertIsArray($result['urls']);
2136 self::assertIsArray($result['user_mentions']);
2141 * Test the api_format_items_embeded_images() function.
2145 public function testApiFormatItemsEmbededImages()
2149 'text ' . DI::baseUrl() . '/display/item_guid',
2150 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2156 * Test the api_format_items_activities() function.
2160 public function testApiFormatItemsActivities()
2162 $item = ['uid' => 0, 'uri-id' => 1];
2163 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
2164 self::assertArrayHasKey('like', $result);
2165 self::assertArrayHasKey('dislike', $result);
2166 self::assertArrayHasKey('attendyes', $result);
2167 self::assertArrayHasKey('attendno', $result);
2168 self::assertArrayHasKey('attendmaybe', $result);
2172 * Test the api_format_items_activities() function with an XML result.
2176 public function testApiFormatItemsActivitiesWithXml()
2178 $item = ['uid' => 0, 'uri-id' => 1];
2179 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
2180 self::assertArrayHasKey('friendica:like', $result);
2181 self::assertArrayHasKey('friendica:dislike', $result);
2182 self::assertArrayHasKey('friendica:attendyes', $result);
2183 self::assertArrayHasKey('friendica:attendno', $result);
2184 self::assertArrayHasKey('friendica:attendmaybe', $result);
2188 * Test the api_format_items() function.
2189 * @doesNotPerformAssertions
2191 public function testApiFormatItems()
2194 $items = Post::selectToArray([], ['uid' => 42]);
2195 foreach ($items as $item) {
2196 $status = api_format_item($item);
2197 self::assertStatus($status);
2203 * Test the api_format_items() function with an XML result.
2204 * @doesNotPerformAssertions
2206 public function testApiFormatItemsWithXml()
2209 $items = Post::selectToArray([], ['uid' => 42]);
2210 foreach ($items as $item) {
2211 $status = api_format_item($item, 'xml');
2212 self::assertStatus($status);
2218 * Test the api_lists_list() function.
2222 public function testApiListsList()
2224 $result = api_lists_list('json');
2225 self::assertEquals(['lists_list' => []], $result);
2229 * Test the api_lists_ownerships() function.
2233 public function testApiListsOwnerships()
2235 $result = api_lists_ownerships('json');
2236 foreach ($result['lists']['lists'] as $list) {
2237 self::assertList($list);
2242 * Test the api_lists_ownerships() function without an authenticated user.
2246 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2248 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2249 BasicAuth::setCurrentUserID();
2250 $_SESSION['authenticated'] = false;
2251 api_lists_ownerships('json');
2255 * Test the api_lists_statuses() function.
2259 public function testApiListsStatuses()
2261 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2262 // api_lists_statuses('json');
2266 * Test the api_lists_statuses() function with a list ID.
2267 * @doesNotPerformAssertions
2269 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);
2283 * Test the api_lists_statuses() function with a list ID and a RSS result.
2287 public function testApiListsStatusesWithListIdAndRss()
2289 // $_REQUEST['list_id'] = 1;
2290 // $result = api_lists_statuses('rss');
2291 // self::assertXml($result, 'statuses');
2295 * Test the api_lists_statuses() function with an unallowed user.
2299 public function testApiListsStatusesWithUnallowedUser()
2301 // $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2302 // BasicAuth::setCurrentUserID();
2303 // api_lists_statuses('json');
2307 * Test the api_statuses_f() function.
2311 public function testApiStatusesFWithFriends()
2314 $result = api_statuses_f('friends');
2315 self::assertArrayHasKey('user', $result);
2319 * Test the api_statuses_f() function.
2323 public function testApiStatusesFWithFollowers()
2325 $result = api_statuses_f('followers');
2326 self::assertArrayHasKey('user', $result);
2330 * Test the api_statuses_f() function.
2334 public function testApiStatusesFWithBlocks()
2336 $result = api_statuses_f('blocks');
2337 self::assertArrayHasKey('user', $result);
2341 * Test the api_statuses_f() function.
2345 public function testApiStatusesFWithIncoming()
2347 $result = api_statuses_f('incoming');
2348 self::assertArrayHasKey('user', $result);
2352 * Test the api_statuses_f() function an undefined cursor GET variable.
2356 public function testApiStatusesFWithUndefinedCursor()
2358 $_GET['cursor'] = 'undefined';
2359 self::assertFalse(api_statuses_f('friends'));
2363 * Test the api_statuses_friends() function.
2367 public function testApiStatusesFriends()
2369 $result = api_statuses_friends('json');
2370 self::assertArrayHasKey('user', $result);
2374 * Test the api_statuses_friends() function an undefined cursor GET variable.
2378 public function testApiStatusesFriendsWithUndefinedCursor()
2380 $_GET['cursor'] = 'undefined';
2381 self::assertFalse(api_statuses_friends('json'));
2385 * Test the api_statuses_followers() function.
2389 public function testApiStatusesFollowers()
2391 $result = api_statuses_followers('json');
2392 self::assertArrayHasKey('user', $result);
2396 * Test the api_statuses_followers() function an undefined cursor GET variable.
2400 public function testApiStatusesFollowersWithUndefinedCursor()
2402 $_GET['cursor'] = 'undefined';
2403 self::assertFalse(api_statuses_followers('json'));
2407 * Test the api_blocks_list() function.
2411 public function testApiBlocksList()
2413 $result = api_blocks_list('json');
2414 self::assertArrayHasKey('user', $result);
2418 * Test the api_blocks_list() function an undefined cursor GET variable.
2422 public function testApiBlocksListWithUndefinedCursor()
2424 $_GET['cursor'] = 'undefined';
2425 self::assertFalse(api_blocks_list('json'));
2429 * Test the api_friendships_incoming() function.
2433 public function testApiFriendshipsIncoming()
2435 $result = api_friendships_incoming('json');
2436 self::assertArrayHasKey('id', $result);
2440 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2444 public function testApiFriendshipsIncomingWithUndefinedCursor()
2446 $_GET['cursor'] = 'undefined';
2447 self::assertFalse(api_friendships_incoming('json'));
2451 * Test the api_statusnet_config() function.
2455 public function testApiStatusnetConfig()
2458 $result = api_statusnet_config('json');
2459 self::assertEquals('localhost', $result['config']['site']['server']);
2460 self::assertEquals('default', $result['config']['site']['theme']);
2461 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2462 self::assertTrue($result['config']['site']['fancy']);
2463 self::assertEquals('en', $result['config']['site']['language']);
2464 self::assertEquals('UTC', $result['config']['site']['timezone']);
2465 self::assertEquals(200000, $result['config']['site']['textlimit']);
2466 self::assertEquals('false', $result['config']['site']['private']);
2467 self::assertEquals('false', $result['config']['site']['ssl']);
2468 self::assertEquals(30, $result['config']['site']['shorturllength']);
2473 * Test the api_direct_messages_new() function.
2477 public function testApiDirectMessagesNew()
2479 $result = api_direct_messages_new('json');
2480 self::assertNull($result);
2484 * Test the api_direct_messages_new() function without an authenticated user.
2488 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2490 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2491 BasicAuth::setCurrentUserID();
2492 $_SESSION['authenticated'] = false;
2493 api_direct_messages_new('json');
2497 * Test the api_direct_messages_new() function with an user ID.
2501 public function testApiDirectMessagesNewWithUserId()
2503 $_POST['text'] = 'message_text';
2504 $_POST['user_id'] = $this->otherUser['id'];
2505 $result = api_direct_messages_new('json');
2506 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2510 * Test the api_direct_messages_new() function with a screen name.
2514 public function testApiDirectMessagesNewWithScreenName()
2516 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2517 $_POST['text'] = 'message_text';
2518 $_POST['user_id'] = $this->friendUser['id'];
2519 $result = api_direct_messages_new('json');
2520 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2521 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2522 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2526 * Test the api_direct_messages_new() function with a title.
2530 public function testApiDirectMessagesNewWithTitle()
2532 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2533 $_POST['text'] = 'message_text';
2534 $_POST['user_id'] = $this->friendUser['id'];
2535 $_REQUEST['title'] = 'message_title';
2536 $result = api_direct_messages_new('json');
2537 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2538 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2539 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2540 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2544 * Test the api_direct_messages_new() function with an RSS result.
2548 public function testApiDirectMessagesNewWithRss()
2550 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2551 $_POST['text'] = 'message_text';
2552 $_POST['user_id'] = $this->friendUser['id'];
2553 $result = api_direct_messages_new('rss');
2554 self::assertXml($result, 'direct-messages');
2558 * Test the api_direct_messages_destroy() function.
2562 public function testApiDirectMessagesDestroy()
2564 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2565 api_direct_messages_destroy('json');
2569 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2573 public function testApiDirectMessagesDestroyWithVerbose()
2575 $_GET['friendica_verbose'] = 'true';
2576 $result = api_direct_messages_destroy('json');
2580 'result' => 'error',
2581 'message' => 'message id or parenturi not specified'
2589 * Test the api_direct_messages_destroy() function without an authenticated user.
2593 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2595 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2596 BasicAuth::setCurrentUserID();
2597 $_SESSION['authenticated'] = false;
2598 api_direct_messages_destroy('json');
2602 * Test the api_direct_messages_destroy() function with a non-zero ID.
2606 public function testApiDirectMessagesDestroyWithId()
2608 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2609 $_REQUEST['id'] = 1;
2610 api_direct_messages_destroy('json');
2614 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2618 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2620 $_REQUEST['id'] = 1;
2621 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2622 $_GET['friendica_verbose'] = 'true';
2623 $result = api_direct_messages_destroy('json');
2627 'result' => 'error',
2628 'message' => 'message id not in database'
2636 * Test the api_direct_messages_destroy() function with a non-zero ID.
2640 public function testApiDirectMessagesDestroyWithCorrectId()
2642 $this->markTestIncomplete('We need to add a dataset for this.');
2646 * Test the api_direct_messages_box() function.
2650 public function testApiDirectMessagesBoxWithSentbox()
2652 $_REQUEST['page'] = -1;
2653 $_REQUEST['max_id'] = 10;
2654 $result = api_direct_messages_box('json', 'sentbox', 'false');
2655 self::assertArrayHasKey('direct_message', $result);
2659 * Test the api_direct_messages_box() function.
2663 public function testApiDirectMessagesBoxWithConversation()
2665 $result = api_direct_messages_box('json', 'conversation', 'false');
2666 self::assertArrayHasKey('direct_message', $result);
2670 * Test the api_direct_messages_box() function.
2674 public function testApiDirectMessagesBoxWithAll()
2676 $result = api_direct_messages_box('json', 'all', 'false');
2677 self::assertArrayHasKey('direct_message', $result);
2681 * Test the api_direct_messages_box() function.
2685 public function testApiDirectMessagesBoxWithInbox()
2687 $result = api_direct_messages_box('json', 'inbox', 'false');
2688 self::assertArrayHasKey('direct_message', $result);
2692 * Test the api_direct_messages_box() function.
2696 public function testApiDirectMessagesBoxWithVerbose()
2698 $result = api_direct_messages_box('json', 'sentbox', 'true');
2702 'result' => 'error',
2703 'message' => 'no mails available'
2711 * Test the api_direct_messages_box() function with a RSS result.
2715 public function testApiDirectMessagesBoxWithRss()
2717 $result = api_direct_messages_box('rss', 'sentbox', 'false');
2718 self::assertXml($result, 'direct-messages');
2722 * Test the api_direct_messages_box() function without an authenticated user.
2726 public function testApiDirectMessagesBoxWithUnallowedUser()
2728 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2729 BasicAuth::setCurrentUserID();
2730 api_direct_messages_box('json', 'sentbox', 'false');
2734 * Test the api_direct_messages_sentbox() function.
2738 public function testApiDirectMessagesSentbox()
2740 $result = api_direct_messages_sentbox('json');
2741 self::assertArrayHasKey('direct_message', $result);
2745 * Test the api_direct_messages_inbox() function.
2749 public function testApiDirectMessagesInbox()
2751 $result = api_direct_messages_inbox('json');
2752 self::assertArrayHasKey('direct_message', $result);
2756 * Test the api_direct_messages_all() function.
2760 public function testApiDirectMessagesAll()
2762 $result = api_direct_messages_all('json');
2763 self::assertArrayHasKey('direct_message', $result);
2767 * Test the api_direct_messages_conversation() function.
2771 public function testApiDirectMessagesConversation()
2773 $result = api_direct_messages_conversation('json');
2774 self::assertArrayHasKey('direct_message', $result);
2778 * Test the api_oauth_request_token() function.
2782 public function testApiOauthRequestToken()
2784 $this->markTestIncomplete('exit() kills phpunit as well');
2788 * Test the api_oauth_access_token() function.
2792 public function testApiOauthAccessToken()
2794 $this->markTestIncomplete('exit() kills phpunit as well');
2798 * Test the api_fr_photos_list() function.
2802 public function testApiFrPhotosList()
2804 $result = api_fr_photos_list('json');
2805 self::assertArrayHasKey('photo', $result);
2809 * Test the api_fr_photos_list() function without an authenticated user.
2813 public function testApiFrPhotosListWithoutAuthenticatedUser()
2815 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2816 BasicAuth::setCurrentUserID();
2817 $_SESSION['authenticated'] = false;
2818 api_fr_photos_list('json');
2822 * Test the api_fr_photo_create_update() function.
2824 public function testApiFrPhotoCreateUpdate()
2826 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2827 api_fr_photo_create_update('json');
2831 * Test the api_fr_photo_create_update() function without an authenticated user.
2835 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
2837 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2838 BasicAuth::setCurrentUserID();
2839 $_SESSION['authenticated'] = false;
2840 api_fr_photo_create_update('json');
2844 * Test the api_fr_photo_create_update() function with an album name.
2848 public function testApiFrPhotoCreateUpdateWithAlbum()
2850 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2851 $_REQUEST['album'] = 'album_name';
2852 api_fr_photo_create_update('json');
2856 * Test the api_fr_photo_create_update() function with the update mode.
2860 public function testApiFrPhotoCreateUpdateWithUpdate()
2862 $this->markTestIncomplete('We need to create a dataset for this');
2866 * Test the api_fr_photo_create_update() function with an uploaded file.
2870 public function testApiFrPhotoCreateUpdateWithFile()
2872 $this->markTestIncomplete();
2876 * Test the api_fr_photo_detail() function.
2880 public function testApiFrPhotoDetail()
2882 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2883 api_fr_photo_detail('json');
2887 * Test the api_fr_photo_detail() function without an authenticated user.
2891 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
2893 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2894 BasicAuth::setCurrentUserID();
2895 $_SESSION['authenticated'] = false;
2896 api_fr_photo_detail('json');
2900 * Test the api_fr_photo_detail() function with a photo ID.
2904 public function testApiFrPhotoDetailWithPhotoId()
2906 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
2907 $_REQUEST['photo_id'] = 1;
2908 api_fr_photo_detail('json');
2912 * Test the api_fr_photo_detail() function with a correct photo ID.
2916 public function testApiFrPhotoDetailCorrectPhotoId()
2918 $this->markTestIncomplete('We need to create a dataset for this.');
2922 * Test the api_account_update_profile_image() function.
2926 public function testApiAccountUpdateProfileImage()
2928 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2929 api_account_update_profile_image('json');
2933 * Test the api_account_update_profile_image() function without an authenticated user.
2937 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2939 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2940 BasicAuth::setCurrentUserID();
2941 $_SESSION['authenticated'] = false;
2942 api_account_update_profile_image('json');
2946 * Test the api_account_update_profile_image() function with an uploaded file.
2950 public function testApiAccountUpdateProfileImageWithUpload()
2952 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2953 $this->markTestIncomplete();
2958 * Test the api_account_update_profile() function.
2962 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']);
2978 * Test the check_acl_input() function.
2982 public function testCheckAclInput()
2984 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
2985 // Where does this result come from?
2986 self::assertEquals(1, $result);
2990 * Test the check_acl_input() function with an empty ACL string.
2994 public function testCheckAclInputWithEmptyAclString()
2996 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
2997 self::assertFalse($result);
3001 * Test the save_media_to_database() function.
3005 public function testSaveMediaToDatabase()
3007 $this->markTestIncomplete();
3011 * Test the post_photo_item() function.
3015 public function testPostPhotoItem()
3017 $this->markTestIncomplete();
3021 * Test the prepare_photo_data() function.
3025 public function testPreparePhotoData()
3027 $this->markTestIncomplete();
3031 * Test the api_share_as_retweet() function with a valid item.
3035 public function testApiShareAsRetweetWithValidItem()
3037 $this->markTestIncomplete();
3041 * Test the api_in_reply_to() function with a valid item.
3045 public function testApiInReplyToWithValidItem()
3047 $this->markTestIncomplete();
3051 * Test the api_clean_plain_items() function.
3055 public function testApiCleanPlainItems()
3057 $_REQUEST['include_entities'] = 'true';
3058 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3059 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3063 * Test the api_best_nickname() function with contacts.
3067 public function testApiBestNicknameWithContacts()
3069 $this->markTestIncomplete();
3073 * Test the api_friendica_group_show() function.
3077 public function testApiFriendicaGroupShow()
3079 $this->markTestIncomplete();
3083 * Test the api_friendica_group_delete() function.
3087 public function testApiFriendicaGroupDelete()
3089 $this->markTestIncomplete();
3093 * Test the api_lists_destroy() function.
3097 public function testApiListsDestroy()
3099 $this->markTestIncomplete();
3103 * Test the group_create() function.
3107 public function testGroupCreate()
3109 $this->markTestIncomplete();
3113 * Test the api_friendica_group_create() function.
3117 public function testApiFriendicaGroupCreate()
3119 $this->markTestIncomplete();
3123 * Test the api_lists_create() function.
3127 public function testApiListsCreate()
3129 $this->markTestIncomplete();
3133 * Test the api_friendica_group_update() function.
3137 public function testApiFriendicaGroupUpdate()
3139 $this->markTestIncomplete();
3143 * Test the api_lists_update() function.
3147 public function testApiListsUpdate()
3149 $this->markTestIncomplete();
3153 * Test the api_friendica_activity() function.
3157 public function testApiFriendicaActivity()
3159 $this->markTestIncomplete();
3163 * Test the api_friendica_notification_seen() function.
3167 public function testApiFriendicaNotificationSeen()
3169 $this->markTestIncomplete();
3173 * Test the api_friendica_direct_messages_setseen() function.
3177 public function testApiFriendicaDirectMessagesSetseen()
3179 $this->markTestIncomplete();
3183 * Test the api_friendica_direct_messages_search() function.
3187 public function testApiFriendicaDirectMessagesSearch()
3189 $this->markTestIncomplete();