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\Module\BaseApi;
14 use Friendica\Network\HTTPException;
15 use Friendica\Security\BasicAuth;
16 use Friendica\Test\FixtureTest;
17 use Friendica\Util\Arrays;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
20 use Monolog\Handler\TestHandler;
22 require_once __DIR__ . '/../../include/api.php';
25 * Tests for the API functions.
27 * Functions that use header() need to be tested in a separate process.
28 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
30 * @backupGlobals enabled
32 class ApiTest extends FixtureTest
35 * @var TestHandler Can handle log-outputs
42 protected $friendUser;
46 protected $wrongUserId;
51 /** @var IManageConfigValues */
55 * Create variables used by tests.
57 protected function setUp() : void
59 global $API, $called_api;
65 /** @var IManageConfigValues $config */
66 $this->config = $this->dice->create(IManageConfigValues::class);
68 $this->config->set('system', 'url', 'http://localhost');
69 $this->config->set('system', 'hostname', 'localhost');
70 $this->config->set('system', 'worker_dont_fork', true);
73 $this->config->set('config', 'hostname', 'localhost');
74 $this->config->set('system', 'throttle_limit_day', 100);
75 $this->config->set('system', 'throttle_limit_week', 100);
76 $this->config->set('system', 'throttle_limit_month', 100);
77 $this->config->set('system', 'theme', 'system_theme');
81 $this->app = DI::app();
83 DI::args()->setArgc(1);
85 // User data that the test database is populated with
88 'name' => 'Self contact',
89 'nick' => 'selfcontact',
90 'nurl' => 'http://localhost/profile/selfcontact'
94 'name' => 'Friend contact',
95 'nick' => 'friendcontact',
96 'nurl' => 'http://localhost/profile/friendcontact'
100 'name' => 'othercontact',
101 'nick' => 'othercontact',
102 'nurl' => 'http://localhost/profile/othercontact'
105 // User ID that we know is not in the database
106 $this->wrongUserId = 666;
108 DI::session()->start();
110 // Most API require login so we force the session
113 'authenticated' => true,
114 'uid' => $this->selfUser['id']
119 * Assert that an user array contains expected keys.
121 * @param array $user User array
125 private function assertSelfUser(array $user)
127 self::assertEquals($this->selfUser['id'], $user['uid']);
128 self::assertEquals($this->selfUser['id'], $user['cid']);
129 self::assertEquals(1, $user['self']);
130 self::assertEquals('DFRN', $user['location']);
131 self::assertEquals($this->selfUser['name'], $user['name']);
132 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
133 self::assertEquals('dfrn', $user['network']);
134 self::assertTrue($user['verified']);
138 * Assert that an user array contains expected keys.
140 * @param array $user User array
144 private function assertOtherUser(array $user = [])
146 self::assertEquals($this->otherUser['id'], $user['id']);
147 self::assertEquals($this->otherUser['id'], $user['id_str']);
148 self::assertEquals(0, $user['self']);
149 self::assertEquals($this->otherUser['name'], $user['name']);
150 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
151 self::assertFalse($user['verified']);
155 * Assert that a status array contains expected keys.
157 * @param array $status Status array
161 private function assertStatus(array $status = [])
163 self::assertIsString($status['text'] ?? '');
164 self::assertIsInt($status['id'] ?? '');
165 // We could probably do more checks here.
169 * Assert that a list array contains expected keys.
171 * @param array $list List array
175 private function assertList(array $list = [])
177 self::assertIsString($list['name']);
178 self::assertIsInt($list['id']);
179 self::assertIsString('string', $list['id_str']);
180 self::assertContains($list['mode'], ['public', 'private']);
181 // We could probably do more checks here.
185 * Assert that the string is XML and contain the root element.
187 * @param string $result XML string
188 * @param string $root_element Root element name
192 private function assertXml($result = '', $root_element = '')
194 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
195 self::assertStringContainsString('<' . $root_element, $result);
196 // We could probably do more checks here.
200 * Get the path to a temporary empty PNG image.
202 * @return string Path
204 private function getTempImage()
206 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
210 // Empty 1x1 px PNG image
211 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
219 * Test the api_user() function.
223 public function testApiUser()
225 self::assertEquals($this->selfUser['id'], api_user());
229 * Test the api_user() function with an unallowed user.
233 public function testApiUserWithUnallowedUser()
235 $_SESSION = ['allow_api' => false];
236 self::assertEquals(false, api_user());
240 * Test the api_source() function.
244 public function testApiSource()
246 self::assertEquals('api', api_source());
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', api_source());
261 * Test the api_source() function with a GET parameter.
265 public function testApiSourceWithGet()
267 $_GET['source'] = 'source_name';
268 self::assertEquals('source_name', api_source());
272 * Test the api_date() function.
276 public function testApiDate()
278 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
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 $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 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
326 $_SERVER['PHP_AUTH_USER'] = 'user@server';
327 BasicAuth::getCurrentUserID(true);
331 * Test the BasicAuth::getCurrentUserID() function with oAuth.
335 public function testApiLoginWithOauth()
337 $this->markTestIncomplete('Can we test this easily?');
341 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
345 public function testApiLoginWithAddonAuth()
347 $this->markTestIncomplete('Can we test this easily?');
351 * Test the BasicAuth::getCurrentUserID() function with a correct login.
353 * @runInSeparateProcess
354 * @preserveGlobalState disabled
355 * @doesNotPerformAssertions
357 public function testApiLoginWithCorrectLogin()
359 $_SERVER['PHP_AUTH_USER'] = 'Test user';
360 $_SERVER['PHP_AUTH_PW'] = 'password';
361 BasicAuth::getCurrentUserID(true);
365 * Test the BasicAuth::getCurrentUserID() function with a remote user.
367 * @runInSeparateProcess
368 * @preserveGlobalState disabled
370 public function testApiLoginWithRemoteUser()
372 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
373 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
374 BasicAuth::getCurrentUserID(true);
378 * Test the api_check_method() function.
382 public function testApiCheckMethod()
384 self::assertFalse(api_check_method('method'));
388 * Test the api_check_method() function with a correct method.
392 public function testApiCheckMethodWithCorrectMethod()
394 $_SERVER['REQUEST_METHOD'] = 'method';
395 self::assertTrue(api_check_method('method'));
399 * Test the api_check_method() function with a wildcard.
403 public function testApiCheckMethodWithWildcard()
405 self::assertTrue(api_check_method('*'));
409 * Test the api_call() function.
411 * @runInSeparateProcess
412 * @preserveGlobalState disabled
414 public function testApiCall()
418 'method' => 'method',
419 'func' => function () {
420 return ['data' => ['some_data']];
423 $_SERVER['REQUEST_METHOD'] = 'method';
424 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
425 $_GET['callback'] = 'callback_name';
427 $args = DI::args()->determine($_SERVER, $_GET);
430 'callback_name(["some_data"])',
431 api_call($this->app, $args)
436 * Test the api_call() function with the profiled enabled.
438 * @runInSeparateProcess
439 * @preserveGlobalState disabled
441 public function testApiCallWithProfiler()
445 'method' => 'method',
446 'func' => function () {
447 return ['data' => ['some_data']];
451 $_SERVER['REQUEST_METHOD'] = 'method';
452 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
454 $args = DI::args()->determine($_SERVER, $_GET);
456 $this->config->set('system', 'profiler', true);
457 $this->config->set('rendertime', 'callstack', true);
458 $this->app->callstack = [
459 'database' => ['some_function' => 200],
460 'database_write' => ['some_function' => 200],
461 'cache' => ['some_function' => 200],
462 'cache_write' => ['some_function' => 200],
463 'network' => ['some_function' => 200]
468 api_call($this->app, $args)
473 * Test the api_call() function without any result.
475 * @runInSeparateProcess
476 * @preserveGlobalState disabled
478 public function testApiCallWithNoResult()
480 // @todo How to test the new API?
484 'method' => 'method',
485 'func' => function () {
489 $_SERVER['REQUEST_METHOD'] = 'method';
490 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
492 $args = DI::args()->determine($_SERVER, $_GET);
495 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
496 api_call($this->app, $args)
502 * Test the api_call() function with an unimplemented API.
504 * @runInSeparateProcess
505 * @preserveGlobalState disabled
507 public function testApiCallWithUninplementedApi()
509 // @todo How to test the new API?
512 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
519 * Test the api_call() function with a JSON result.
521 * @runInSeparateProcess
522 * @preserveGlobalState disabled
524 public function testApiCallWithJson()
528 'method' => 'method',
529 'func' => function () {
530 return ['data' => ['some_data']];
533 $_SERVER['REQUEST_METHOD'] = 'method';
534 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
536 $args = DI::args()->determine($_SERVER, $_GET);
540 api_call($this->app, $args)
545 * Test the api_call() function with an XML result.
547 * @runInSeparateProcess
548 * @preserveGlobalState disabled
550 public function testApiCallWithXml()
554 'method' => 'method',
555 'func' => function () {
559 $_SERVER['REQUEST_METHOD'] = 'method';
560 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
562 $args = DI::args()->determine($_SERVER, $_GET);
566 api_call($this->app, $args)
571 * Test the api_call() function with an RSS result.
573 * @runInSeparateProcess
574 * @preserveGlobalState disabled
576 public function testApiCallWithRss()
580 'method' => 'method',
581 'func' => function () {
585 $_SERVER['REQUEST_METHOD'] = 'method';
586 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
588 $args = DI::args()->determine($_SERVER, $_GET);
591 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
593 api_call($this->app, $args)
598 * Test the api_call() function with an Atom result.
600 * @runInSeparateProcess
601 * @preserveGlobalState disabled
603 public function testApiCallWithAtom()
607 'method' => 'method',
608 'func' => function () {
612 $_SERVER['REQUEST_METHOD'] = 'method';
613 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
615 $args = DI::args()->determine($_SERVER, $_GET);
618 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
620 api_call($this->app, $args)
625 * Test the api_call() function with an unallowed method.
627 * @runInSeparateProcess
628 * @preserveGlobalState disabled
630 public function testApiCallWithWrongMethod()
632 // Shouldn't be needed anymore due to the router?
635 $API['api_path'] = ['method' => 'method'];
637 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
639 $args = DI::args()->determine($_SERVER, $_GET);
642 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
643 api_call($this->app, $args)
649 * Test the api_call() function with an unauthorized user.
651 * @runInSeparateProcess
652 * @preserveGlobalState disabled
654 public function testApiCallWithWrongAuth()
656 // @todo How to test the new API?
660 'method' => 'method',
663 $_SESSION['authenticated'] = false;
664 $_SERVER['REQUEST_METHOD'] = 'method';
665 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
667 $args = DI::args()->determine($_SERVER, $_GET);
670 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
671 api_call($this->app, $args)
677 * Test the api_error() function with a JSON result.
679 * @runInSeparateProcess
680 * @preserveGlobalState disabled
682 public function testApiErrorWithJson()
684 // @todo How to test the new API?
685 // self::assertEquals(
686 // '{"status":{"error":"error_message","code":"200 OK","request":""}}',
687 // api_error('json', new HTTPException\OKException('error_message'), DI::args())
692 * Test the api_error() function with an XML result.
694 * @runInSeparateProcess
695 * @preserveGlobalState disabled
697 public function testApiErrorWithXml()
699 // @todo How to test the new API?
702 '<?xml version="1.0"?>' . "\n" .
703 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
704 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
705 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
706 ' <error>error_message</error>' . "\n" .
707 ' <code>200 OK</code>' . "\n" .
708 ' <request/>' . "\n" .
710 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
716 * Test the api_error() function with an RSS result.
718 * @runInSeparateProcess
719 * @preserveGlobalState disabled
721 public function testApiErrorWithRss()
723 // @todo How to test the new API?
726 '<?xml version="1.0"?>' . "\n" .
727 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
728 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
729 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
730 ' <error>error_message</error>' . "\n" .
731 ' <code>200 OK</code>' . "\n" .
732 ' <request/>' . "\n" .
734 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
740 * Test the api_error() function with an Atom result.
742 * @runInSeparateProcess
743 * @preserveGlobalState disabled
745 public function testApiErrorWithAtom()
747 // @todo How to test the new API?
750 '<?xml version="1.0"?>' . "\n" .
751 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
752 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
753 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
754 ' <error>error_message</error>' . "\n" .
755 ' <code>200 OK</code>' . "\n" .
756 ' <request/>' . "\n" .
758 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
764 * Test the api_rss_extra() function.
768 public function testApiRssExtra()
770 $user_info = ['url' => 'user_url', 'lang' => 'en'];
771 $result = api_rss_extra($this->app, [], $user_info);
772 self::assertEquals($user_info, $result['$user']);
773 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
774 self::assertArrayHasKey('self', $result['$rss']);
775 self::assertArrayHasKey('base', $result['$rss']);
776 self::assertArrayHasKey('updated', $result['$rss']);
777 self::assertArrayHasKey('atom_updated', $result['$rss']);
778 self::assertArrayHasKey('language', $result['$rss']);
779 self::assertArrayHasKey('logo', $result['$rss']);
783 * Test the api_rss_extra() function without any user info.
787 public function testApiRssExtraWithoutUserInfo()
789 $result = api_rss_extra($this->app, [], null);
790 self::assertIsArray($result['$user']);
791 self::assertArrayHasKey('alternate', $result['$rss']);
792 self::assertArrayHasKey('self', $result['$rss']);
793 self::assertArrayHasKey('base', $result['$rss']);
794 self::assertArrayHasKey('updated', $result['$rss']);
795 self::assertArrayHasKey('atom_updated', $result['$rss']);
796 self::assertArrayHasKey('language', $result['$rss']);
797 self::assertArrayHasKey('logo', $result['$rss']);
801 * Test the api_unique_id_to_nurl() function.
805 public function testApiUniqueIdToNurl()
807 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
811 * Test the api_unique_id_to_nurl() function with a correct ID.
815 public function testApiUniqueIdToNurlWithCorrectId()
817 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
821 * Test the api_get_user() function.
825 public function testApiGetUser()
827 $user = api_get_user();
828 self::assertSelfUser($user);
829 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
830 self::assertEquals('6fdbe8', $user['profile_link_color']);
831 self::assertEquals('ededed', $user['profile_background_color']);
835 * Test the api_get_user() function with a Frio schema.
839 public function testApiGetUserWithFrioSchema()
841 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
842 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
843 $user = api_get_user();
844 self::assertSelfUser($user);
845 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
846 self::assertEquals('6fdbe8', $user['profile_link_color']);
847 self::assertEquals('ededed', $user['profile_background_color']);
851 * Test the api_get_user() function with an empty Frio schema.
855 public function testApiGetUserWithEmptyFrioSchema()
857 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
858 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
859 $user = api_get_user();
860 self::assertSelfUser($user);
861 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
862 self::assertEquals('6fdbe8', $user['profile_link_color']);
863 self::assertEquals('ededed', $user['profile_background_color']);
867 * Test the api_get_user() function with a custom Frio schema.
871 public function testApiGetUserWithCustomFrioSchema()
873 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
874 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
875 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
876 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
877 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
878 $user = api_get_user();
879 self::assertSelfUser($user);
880 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
881 self::assertEquals('123456', $user['profile_link_color']);
882 self::assertEquals('123456', $user['profile_background_color']);
886 * Test the api_get_user() function with an user that is not allowed to use the API.
888 * @runInSeparateProcess
889 * @preserveGlobalState disabled
891 public function testApiGetUserWithoutApiUser()
893 $_SERVER['PHP_AUTH_USER'] = 'Test user';
894 $_SERVER['PHP_AUTH_PW'] = 'password';
895 $_SESSION['allow_api'] = false;
896 self::assertFalse(api_get_user());
900 * Test the api_get_user() function with an user ID in a GET parameter.
904 public function testApiGetUserWithGetId()
906 $_GET['user_id'] = $this->otherUser['id'];
907 self::assertOtherUser(api_get_user());
911 * Test the api_get_user() function with a wrong user ID in a GET parameter.
915 public function testApiGetUserWithWrongGetId()
917 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
918 $_GET['user_id'] = $this->wrongUserId;
919 self::assertOtherUser(api_get_user());
923 * Test the api_get_user() function with an user name in a GET parameter.
927 public function testApiGetUserWithGetName()
929 $_GET['screen_name'] = $this->selfUser['nick'];
930 self::assertSelfUser(api_get_user());
934 * Test the api_get_user() function with a profile URL in a GET parameter.
938 public function testApiGetUserWithGetUrl()
940 $_GET['profileurl'] = $this->selfUser['nurl'];
941 self::assertSelfUser(api_get_user());
945 * Test the api_get_user() function with an user ID in the API path.
949 public function testApiGetUserWithNumericCalledApi()
952 $called_api = ['api_path'];
953 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
954 self::assertOtherUser(api_get_user());
958 * Test the api_get_user() function with the $called_api global variable.
962 public function testApiGetUserWithCalledApi()
965 $called_api = ['api', 'api_path'];
966 self::assertSelfUser(api_get_user());
970 * Test the api_get_user() function with a valid user.
974 public function testApiGetUserWithCorrectUser()
976 self::assertOtherUser(api_get_user($this->otherUser['id']));
980 * Test the api_get_user() function with a wrong user ID.
984 public function testApiGetUserWithWrongUser()
986 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
987 self::assertOtherUser(api_get_user($this->wrongUserId));
991 * Test the api_get_user() function with a 0 user ID.
995 public function testApiGetUserWithZeroUser()
997 self::assertSelfUser(api_get_user(0));
1001 * Test the api_item_get_user() function.
1005 public function testApiItemGetUser()
1007 $users = api_item_get_user($this->app, []);
1008 self::assertSelfUser($users[0]);
1012 * Test the api_item_get_user() function with a different item parent.
1016 public function testApiItemGetUserWithDifferentParent()
1018 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1019 self::assertSelfUser($users[0]);
1020 self::assertEquals($users[0], $users[1]);
1024 * Test the Arrays::walkRecursive() function.
1028 public function testApiWalkRecursive()
1033 Arrays::walkRecursive(
1036 // Should we test this with a callback that actually does something?
1044 * Test the Arrays::walkRecursive() function with an array.
1048 public function testApiWalkRecursiveWithArray()
1050 $array = [['item1'], ['item2']];
1053 Arrays::walkRecursive(
1056 // Should we test this with a callback that actually does something?
1064 * Test the BaseApi::reformatXML() function.
1068 public function testApiReformatXml()
1072 self::assertTrue(BaseApi::reformatXML($item, $key));
1073 self::assertEquals('true', $item);
1077 * Test the BaseApi::reformatXML() function with a statusnet_api key.
1081 public function testApiReformatXmlWithStatusnetKey()
1084 $key = 'statusnet_api';
1085 self::assertTrue(BaseApi::reformatXML($item, $key));
1086 self::assertEquals('statusnet:api', $key);
1090 * Test the BaseApi::reformatXML() function with a friendica_api key.
1094 public function testApiReformatXmlWithFriendicaKey()
1097 $key = 'friendica_api';
1098 self::assertTrue(BaseApi::reformatXML($item, $key));
1099 self::assertEquals('friendica:api', $key);
1103 * Test the BaseApi::createXML() function.
1107 public function testApiCreateXml()
1110 '<?xml version="1.0"?>' . "\n" .
1111 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1112 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1113 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1114 ' <data>some_data</data>' . "\n" .
1115 '</root_element>' . "\n",
1116 BaseApi::createXML(['data' => ['some_data']], 'root_element')
1121 * Test the BaseApi::createXML() function without any XML namespace.
1125 public function testApiCreateXmlWithoutNamespaces()
1128 '<?xml version="1.0"?>' . "\n" .
1130 ' <data>some_data</data>' . "\n" .
1132 BaseApi::createXML(['data' => ['some_data']], 'ok')
1137 * Test the BaseApi::formatData() function.
1141 public function testApiFormatData()
1143 $data = ['some_data'];
1144 self::assertEquals($data, BaseApi::formatData('root_element', 'json', $data));
1148 * Test the BaseApi::formatData() function with an XML result.
1152 public function testApiFormatDataWithXml()
1155 '<?xml version="1.0"?>' . "\n" .
1156 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1157 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1158 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1159 ' <data>some_data</data>' . "\n" .
1160 '</root_element>' . "\n",
1161 BaseApi::formatData('root_element', 'xml', ['data' => ['some_data']])
1166 * Test the api_account_verify_credentials() function.
1170 public function testApiAccountVerifyCredentials()
1172 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1176 * Test the api_account_verify_credentials() function without an authenticated user.
1180 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1182 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1183 $_SESSION['authenticated'] = false;
1184 api_account_verify_credentials('json');
1188 * Test the requestdata() function.
1192 public function testRequestdata()
1194 self::assertNull(requestdata('variable_name'));
1198 * Test the requestdata() function with a POST parameter.
1202 public function testRequestdataWithPost()
1204 $_POST['variable_name'] = 'variable_value';
1205 self::assertEquals('variable_value', requestdata('variable_name'));
1209 * Test the requestdata() function with a GET parameter.
1213 public function testRequestdataWithGet()
1215 $_GET['variable_name'] = 'variable_value';
1216 self::assertEquals('variable_value', requestdata('variable_name'));
1220 * Test the api_statuses_mediap() function.
1224 public function testApiStatusesMediap()
1226 DI::args()->setArgc(2);
1234 'tmp_name' => $this->getTempImage(),
1235 'name' => 'spacer.png',
1236 'type' => 'image/png'
1239 $_GET['status'] = '<b>Status content</b>';
1241 $result = api_statuses_mediap('json');
1242 self::assertStatus($result['status']);
1246 * Test the api_statuses_mediap() function without an authenticated user.
1250 public function testApiStatusesMediapWithoutAuthenticatedUser()
1252 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1253 $_SESSION['authenticated'] = false;
1254 api_statuses_mediap('json');
1258 * Test the api_statuses_update() function.
1262 public function testApiStatusesUpdate()
1264 $_GET['status'] = 'Status content #friendica';
1265 $_GET['in_reply_to_status_id'] = -1;
1274 'tmp_name' => $this->getTempImage(),
1275 'name' => 'spacer.png',
1276 'type' => 'image/png'
1280 $result = api_statuses_update('json');
1281 self::assertStatus($result['status']);
1285 * Test the api_statuses_update() function with an HTML status.
1289 public function testApiStatusesUpdateWithHtml()
1291 $_GET['htmlstatus'] = '<b>Status content</b>';
1293 $result = api_statuses_update('json');
1294 self::assertStatus($result['status']);
1298 * Test the api_statuses_update() function without an authenticated user.
1302 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1304 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1305 $_SESSION['authenticated'] = false;
1306 api_statuses_update('json');
1310 * Test the api_statuses_update() function with a parent status.
1314 public function testApiStatusesUpdateWithParent()
1316 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1320 * Test the api_statuses_update() function with a media_ids parameter.
1324 public function testApiStatusesUpdateWithMediaIds()
1326 $this->markTestIncomplete();
1330 * Test the api_statuses_update() function with the throttle limit reached.
1334 public function testApiStatusesUpdateWithDayThrottleReached()
1336 $this->markTestIncomplete();
1340 * Test the api_media_upload() function.
1341 * @runInSeparateProcess
1342 * @preserveGlobalState disabled
1344 public function testApiMediaUpload()
1346 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1351 * Test the api_media_upload() function without an authenticated user.
1355 public function testApiMediaUploadWithoutAuthenticatedUser()
1357 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1358 $_SESSION['authenticated'] = false;
1363 * Test the api_media_upload() function with an invalid uploaded media.
1367 public function testApiMediaUploadWithMedia()
1369 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1373 'tmp_name' => 'tmp_name'
1380 * Test the api_media_upload() function with an valid uploaded media.
1384 public function testApiMediaUploadWithValidMedia()
1392 'tmp_name' => $this->getTempImage(),
1393 'name' => 'spacer.png',
1394 'type' => 'image/png'
1398 DI::args()->setArgc(2);
1400 $result = api_media_upload();
1401 self::assertEquals('image/png', $result['media']['image']['image_type']);
1402 self::assertEquals(1, $result['media']['image']['w']);
1403 self::assertEquals(1, $result['media']['image']['h']);
1404 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1408 * Test the api_status_show() function.
1410 public function testApiStatusShowWithJson()
1412 $result = api_status_show('json', 1);
1413 self::assertStatus($result['status']);
1417 * Test the api_status_show() function with an XML result.
1419 public function testApiStatusShowWithXml()
1421 $result = api_status_show('xml', 1);
1422 self::assertXml($result, 'statuses');
1426 * Test the api_get_last_status() function
1428 public function testApiGetLastStatus()
1430 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1432 self::assertNotNull($item);
1436 * Test the api_users_show() function.
1440 public function testApiUsersShow()
1442 $result = api_users_show('json');
1443 // We can't use assertSelfUser() here because the user object is missing some properties.
1444 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1445 self::assertEquals('DFRN', $result['user']['location']);
1446 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1447 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1448 self::assertEquals('dfrn', $result['user']['network']);
1449 self::assertTrue($result['user']['verified']);
1453 * Test the api_users_show() function with an XML result.
1457 public function testApiUsersShowWithXml()
1459 $result = api_users_show('xml');
1460 self::assertXml($result, 'statuses');
1464 * Test the api_users_search() function.
1468 public function testApiUsersSearch()
1470 $_GET['q'] = 'othercontact';
1471 $result = api_users_search('json');
1472 self::assertOtherUser($result['users'][0]);
1476 * Test the api_users_search() function with an XML result.
1480 public function testApiUsersSearchWithXml()
1482 $_GET['q'] = 'othercontact';
1483 $result = api_users_search('xml');
1484 self::assertXml($result, 'users');
1488 * Test the api_users_search() function without a GET q parameter.
1492 public function testApiUsersSearchWithoutQuery()
1494 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1495 api_users_search('json');
1499 * Test the api_users_lookup() function.
1503 public function testApiUsersLookup()
1505 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1506 api_users_lookup('json');
1510 * Test the api_users_lookup() function with an user ID.
1514 public function testApiUsersLookupWithUserId()
1516 $_REQUEST['user_id'] = $this->otherUser['id'];
1517 $result = api_users_lookup('json');
1518 self::assertOtherUser($result['users'][0]);
1522 * Test the api_search() function.
1526 public function testApiSearch()
1528 $_REQUEST['q'] = 'reply';
1529 $_REQUEST['max_id'] = 10;
1530 $result = api_search('json');
1531 foreach ($result['status'] as $status) {
1532 self::assertStatus($status);
1533 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1538 * Test the api_search() function a count parameter.
1542 public function testApiSearchWithCount()
1544 $_REQUEST['q'] = 'reply';
1545 $_REQUEST['count'] = 20;
1546 $result = api_search('json');
1547 foreach ($result['status'] as $status) {
1548 self::assertStatus($status);
1549 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1554 * Test the api_search() function with an rpp parameter.
1558 public function testApiSearchWithRpp()
1560 $_REQUEST['q'] = 'reply';
1561 $_REQUEST['rpp'] = 20;
1562 $result = api_search('json');
1563 foreach ($result['status'] as $status) {
1564 self::assertStatus($status);
1565 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1570 * Test the api_search() function with an q parameter contains hashtag.
1571 * @doesNotPerformAssertions
1573 public function testApiSearchWithHashtag()
1575 $_REQUEST['q'] = '%23friendica';
1576 $result = api_search('json');
1577 foreach ($result['status'] as $status) {
1578 self::assertStatus($status);
1579 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1584 * Test the api_search() function with an exclude_replies parameter.
1585 * @doesNotPerformAssertions
1587 public function testApiSearchWithExcludeReplies()
1589 $_REQUEST['max_id'] = 10;
1590 $_REQUEST['exclude_replies'] = true;
1591 $_REQUEST['q'] = 'friendica';
1592 $result = api_search('json');
1593 foreach ($result['status'] as $status) {
1594 self::assertStatus($status);
1599 * Test the api_search() function without an authenticated user.
1603 public function testApiSearchWithUnallowedUser()
1605 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1606 $_SESSION['allow_api'] = false;
1607 $_GET['screen_name'] = $this->selfUser['nick'];
1612 * Test the api_search() function without any GET query parameter.
1616 public function testApiSearchWithoutQuery()
1618 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1623 * Test the api_statuses_home_timeline() function.
1627 public function testApiStatusesHomeTimeline()
1629 $_REQUEST['max_id'] = 10;
1630 $_REQUEST['exclude_replies'] = true;
1631 $_REQUEST['conversation_id'] = 1;
1632 $result = api_statuses_home_timeline('json');
1633 self::assertNotEmpty($result['status']);
1634 foreach ($result['status'] as $status) {
1635 self::assertStatus($status);
1640 * Test the api_statuses_home_timeline() function with a negative page parameter.
1644 public function testApiStatusesHomeTimelineWithNegativePage()
1646 $_REQUEST['page'] = -2;
1647 $result = api_statuses_home_timeline('json');
1648 self::assertNotEmpty($result['status']);
1649 foreach ($result['status'] as $status) {
1650 self::assertStatus($status);
1655 * Test the api_statuses_home_timeline() with an unallowed user.
1659 public function testApiStatusesHomeTimelineWithUnallowedUser()
1661 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1662 $_SESSION['allow_api'] = false;
1663 $_GET['screen_name'] = $this->selfUser['nick'];
1664 api_statuses_home_timeline('json');
1668 * Test the api_statuses_home_timeline() function with an RSS result.
1672 public function testApiStatusesHomeTimelineWithRss()
1674 $result = api_statuses_home_timeline('rss');
1675 self::assertXml($result, 'statuses');
1679 * Test the api_statuses_public_timeline() function.
1683 public function testApiStatusesPublicTimeline()
1685 $_REQUEST['max_id'] = 10;
1686 $_REQUEST['conversation_id'] = 1;
1687 $result = api_statuses_public_timeline('json');
1688 self::assertNotEmpty($result['status']);
1689 foreach ($result['status'] as $status) {
1690 self::assertStatus($status);
1695 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1699 public function testApiStatusesPublicTimelineWithExcludeReplies()
1701 $_REQUEST['max_id'] = 10;
1702 $_REQUEST['exclude_replies'] = true;
1703 $result = api_statuses_public_timeline('json');
1704 self::assertNotEmpty($result['status']);
1705 foreach ($result['status'] as $status) {
1706 self::assertStatus($status);
1711 * Test the api_statuses_public_timeline() function with a negative page parameter.
1715 public function testApiStatusesPublicTimelineWithNegativePage()
1717 $_REQUEST['page'] = -2;
1718 $result = api_statuses_public_timeline('json');
1719 self::assertNotEmpty($result['status']);
1720 foreach ($result['status'] as $status) {
1721 self::assertStatus($status);
1726 * Test the api_statuses_public_timeline() function with an unallowed user.
1730 public function testApiStatusesPublicTimelineWithUnallowedUser()
1732 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1733 $_SESSION['allow_api'] = false;
1734 $_GET['screen_name'] = $this->selfUser['nick'];
1735 api_statuses_public_timeline('json');
1739 * Test the api_statuses_public_timeline() function with an RSS result.
1743 public function testApiStatusesPublicTimelineWithRss()
1745 $result = api_statuses_public_timeline('rss');
1746 self::assertXml($result, 'statuses');
1750 * Test the api_statuses_networkpublic_timeline() function.
1754 public function testApiStatusesNetworkpublicTimeline()
1756 $_REQUEST['max_id'] = 10;
1757 $result = api_statuses_networkpublic_timeline('json');
1758 self::assertNotEmpty($result['status']);
1759 foreach ($result['status'] as $status) {
1760 self::assertStatus($status);
1765 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1769 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1771 $_REQUEST['page'] = -2;
1772 $result = api_statuses_networkpublic_timeline('json');
1773 self::assertNotEmpty($result['status']);
1774 foreach ($result['status'] as $status) {
1775 self::assertStatus($status);
1780 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1784 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1786 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1787 $_SESSION['allow_api'] = false;
1788 $_GET['screen_name'] = $this->selfUser['nick'];
1789 api_statuses_networkpublic_timeline('json');
1793 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1797 public function testApiStatusesNetworkpublicTimelineWithRss()
1799 $result = api_statuses_networkpublic_timeline('rss');
1800 self::assertXml($result, 'statuses');
1804 * Test the api_statuses_show() function.
1808 public function testApiStatusesShow()
1810 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1811 api_statuses_show('json');
1815 * Test the api_statuses_show() function with an ID.
1819 public function testApiStatusesShowWithId()
1821 DI::args()->setArgv(['', '', '', 1]);
1822 $result = api_statuses_show('json');
1823 self::assertStatus($result['status']);
1827 * Test the api_statuses_show() function with the conversation parameter.
1831 public function testApiStatusesShowWithConversation()
1833 DI::args()->setArgv(['', '', '', 1]);
1834 $_REQUEST['conversation'] = 1;
1835 $result = api_statuses_show('json');
1836 self::assertNotEmpty($result['status']);
1837 foreach ($result['status'] as $status) {
1838 self::assertStatus($status);
1843 * Test the api_statuses_show() function with an unallowed user.
1847 public function testApiStatusesShowWithUnallowedUser()
1849 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1850 $_SESSION['allow_api'] = false;
1851 $_GET['screen_name'] = $this->selfUser['nick'];
1852 api_statuses_show('json');
1856 * Test the api_conversation_show() function.
1860 public function testApiConversationShow()
1862 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1863 api_conversation_show('json');
1867 * Test the api_conversation_show() function with an ID.
1871 public function testApiConversationShowWithId()
1873 DI::args()->setArgv(['', '', '', 1]);
1874 $_REQUEST['max_id'] = 10;
1875 $_REQUEST['page'] = -2;
1876 $result = api_conversation_show('json');
1877 self::assertNotEmpty($result['status']);
1878 foreach ($result['status'] as $status) {
1879 self::assertStatus($status);
1884 * Test the api_conversation_show() function with an unallowed user.
1888 public function testApiConversationShowWithUnallowedUser()
1890 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1891 $_SESSION['allow_api'] = false;
1892 $_GET['screen_name'] = $this->selfUser['nick'];
1893 api_conversation_show('json');
1897 * Test the api_statuses_repeat() function.
1901 public function testApiStatusesRepeat()
1903 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1904 api_statuses_repeat('json');
1908 * Test the api_statuses_repeat() function without an authenticated user.
1912 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1914 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1915 $_SESSION['authenticated'] = false;
1916 api_statuses_repeat('json');
1920 * Test the api_statuses_repeat() function with an ID.
1924 public function testApiStatusesRepeatWithId()
1926 DI::args()->setArgv(['', '', '', 1]);
1927 $result = api_statuses_repeat('json');
1928 self::assertStatus($result['status']);
1930 // Also test with a shared status
1931 DI::args()->setArgv(['', '', '', 5]);
1932 $result = api_statuses_repeat('json');
1933 self::assertStatus($result['status']);
1937 * Test the api_statuses_destroy() function.
1941 public function testApiStatusesDestroy()
1943 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1944 api_statuses_destroy('json');
1948 * Test the api_statuses_destroy() function without an authenticated user.
1952 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1954 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1955 $_SESSION['authenticated'] = false;
1956 api_statuses_destroy('json');
1960 * Test the api_statuses_destroy() function with an ID.
1964 public function testApiStatusesDestroyWithId()
1966 DI::args()->setArgv(['', '', '', 1]);
1967 $result = api_statuses_destroy('json');
1968 self::assertStatus($result['status']);
1972 * Test the api_statuses_mentions() function.
1976 public function testApiStatusesMentions()
1978 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1979 $_REQUEST['max_id'] = 10;
1980 $result = api_statuses_mentions('json');
1981 self::assertEmpty($result['status']);
1982 // We should test with mentions in the database.
1986 * Test the api_statuses_mentions() function with a negative page parameter.
1990 public function testApiStatusesMentionsWithNegativePage()
1992 $_REQUEST['page'] = -2;
1993 $result = api_statuses_mentions('json');
1994 self::assertEmpty($result['status']);
1998 * Test the api_statuses_mentions() function with an unallowed user.
2002 public function testApiStatusesMentionsWithUnallowedUser()
2004 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2005 $_SESSION['allow_api'] = false;
2006 $_GET['screen_name'] = $this->selfUser['nick'];
2007 api_statuses_mentions('json');
2011 * Test the api_statuses_mentions() function with an RSS result.
2015 public function testApiStatusesMentionsWithRss()
2017 $result = api_statuses_mentions('rss');
2018 self::assertXml($result, 'statuses');
2022 * Test the api_statuses_user_timeline() function.
2026 public function testApiStatusesUserTimeline()
2028 $_REQUEST['max_id'] = 10;
2029 $_REQUEST['exclude_replies'] = true;
2030 $_REQUEST['conversation_id'] = 1;
2031 $result = api_statuses_user_timeline('json');
2032 self::assertNotEmpty($result['status']);
2033 foreach ($result['status'] as $status) {
2034 self::assertStatus($status);
2039 * Test the api_statuses_user_timeline() function with a negative page parameter.
2043 public function testApiStatusesUserTimelineWithNegativePage()
2045 $_REQUEST['page'] = -2;
2046 $result = api_statuses_user_timeline('json');
2047 self::assertNotEmpty($result['status']);
2048 foreach ($result['status'] as $status) {
2049 self::assertStatus($status);
2054 * Test the api_statuses_user_timeline() function with an RSS result.
2058 public function testApiStatusesUserTimelineWithRss()
2060 $result = api_statuses_user_timeline('rss');
2061 self::assertXml($result, 'statuses');
2065 * Test the api_statuses_user_timeline() function with an unallowed user.
2069 public function testApiStatusesUserTimelineWithUnallowedUser()
2071 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2072 $_SESSION['allow_api'] = false;
2073 $_GET['screen_name'] = $this->selfUser['nick'];
2074 api_statuses_user_timeline('json');
2078 * Test the api_favorites_create_destroy() function.
2082 public function testApiFavoritesCreateDestroy()
2084 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2085 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2086 api_favorites_create_destroy('json');
2090 * Test the api_favorites_create_destroy() function with an invalid ID.
2094 public function testApiFavoritesCreateDestroyWithInvalidId()
2096 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2097 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2098 api_favorites_create_destroy('json');
2102 * Test the api_favorites_create_destroy() function with an invalid action.
2106 public function testApiFavoritesCreateDestroyWithInvalidAction()
2108 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2109 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2110 $_REQUEST['id'] = 1;
2111 api_favorites_create_destroy('json');
2115 * Test the api_favorites_create_destroy() function with the create action.
2119 public function testApiFavoritesCreateDestroyWithCreateAction()
2121 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2122 $_REQUEST['id'] = 3;
2123 $result = api_favorites_create_destroy('json');
2124 self::assertStatus($result['status']);
2128 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2132 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2134 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2135 $_REQUEST['id'] = 3;
2136 $result = api_favorites_create_destroy('rss');
2137 self::assertXml($result, 'status');
2141 * Test the api_favorites_create_destroy() function with the destroy action.
2145 public function testApiFavoritesCreateDestroyWithDestroyAction()
2147 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2148 $_REQUEST['id'] = 3;
2149 $result = api_favorites_create_destroy('json');
2150 self::assertStatus($result['status']);
2154 * Test the api_favorites_create_destroy() function without an authenticated user.
2158 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2160 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2161 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2162 $_SESSION['authenticated'] = false;
2163 api_favorites_create_destroy('json');
2167 * Test the api_favorites() function.
2171 public function testApiFavorites()
2173 $_REQUEST['page'] = -1;
2174 $_REQUEST['max_id'] = 10;
2175 $result = api_favorites('json');
2176 foreach ($result['status'] as $status) {
2177 self::assertStatus($status);
2182 * Test the api_favorites() function with an RSS result.
2186 public function testApiFavoritesWithRss()
2188 $result = api_favorites('rss');
2189 self::assertXml($result, 'statuses');
2193 * Test the api_favorites() function with an unallowed user.
2197 public function testApiFavoritesWithUnallowedUser()
2199 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2200 $_SESSION['allow_api'] = false;
2201 $_GET['screen_name'] = $this->selfUser['nick'];
2202 api_favorites('json');
2206 * Test the api_format_messages() function.
2210 public function testApiFormatMessages()
2212 $result = api_format_messages(
2213 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2214 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2215 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2217 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2218 self::assertEquals(1, $result['id']);
2219 self::assertEquals(2, $result['recipient_id']);
2220 self::assertEquals(3, $result['sender_id']);
2221 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2222 self::assertEquals('sender_name', $result['sender_screen_name']);
2226 * Test the api_format_messages() function with HTML.
2230 public function testApiFormatMessagesWithHtmlText()
2232 $_GET['getText'] = 'html';
2233 $result = api_format_messages(
2234 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2235 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2236 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2238 self::assertEquals('item_title', $result['title']);
2239 self::assertEquals('<strong>item_body</strong>', $result['text']);
2243 * Test the api_format_messages() function with plain text.
2247 public function testApiFormatMessagesWithPlainText()
2249 $_GET['getText'] = 'plain';
2250 $result = api_format_messages(
2251 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2252 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2253 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2255 self::assertEquals('item_title', $result['title']);
2256 self::assertEquals('item_body', $result['text']);
2260 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2264 public function testApiFormatMessagesWithoutUserObjects()
2266 $_GET['getUserObjects'] = 'false';
2267 $result = api_format_messages(
2268 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2269 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2270 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2272 self::assertTrue(!isset($result['sender']));
2273 self::assertTrue(!isset($result['recipient']));
2277 * Test the api_convert_item() function.
2281 public function testApiConvertItem()
2283 $result = api_convert_item(
2285 'network' => 'feed',
2286 'title' => 'item_title',
2288 // We need a long string to test that it is correctly cut
2289 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2290 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2291 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2292 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2293 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2294 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2295 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2296 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2297 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2298 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2299 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2300 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2301 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2302 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2303 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2304 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2305 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2306 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2307 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2308 'repellat officia illum quos impedit quam iste esse unde qui ' .
2309 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2310 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2311 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2312 'plink' => 'item_plink'
2315 self::assertStringStartsWith('item_title', $result['text']);
2316 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2320 * Test the api_convert_item() function with an empty item body.
2324 public function testApiConvertItemWithoutBody()
2326 $result = api_convert_item(
2328 'network' => 'feed',
2329 'title' => 'item_title',
2332 'plink' => 'item_plink'
2335 self::assertEquals("item_title", $result['text']);
2336 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2340 * Test the api_convert_item() function with the title in the body.
2344 public function testApiConvertItemWithTitleInBody()
2346 $result = api_convert_item(
2348 'title' => 'item_title',
2349 'body' => 'item_title item_body',
2353 self::assertEquals('item_title item_body', $result['text']);
2354 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2358 * Test the api_get_attachments() function.
2362 public function testApiGetAttachments()
2365 self::assertEmpty(api_get_attachments($body, 0));
2369 * Test the api_get_attachments() function with an img tag.
2373 public function testApiGetAttachmentsWithImage()
2375 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2376 self::assertIsArray(api_get_attachments($body, 0));
2380 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2384 public function testApiGetAttachmentsWithImageAndAndStatus()
2386 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2387 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2388 self::assertIsArray(api_get_attachments($body, 0));
2392 * Test the api_get_entitities() function.
2396 public function testApiGetEntitities()
2399 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2403 * Test the api_get_entitities() function with the include_entities parameter.
2407 public function testApiGetEntititiesWithIncludeEntities()
2409 $_REQUEST['include_entities'] = 'true';
2411 $result = api_get_entitities($text, 'bbcode', 0);
2412 self::assertIsArray($result['hashtags']);
2413 self::assertIsArray($result['symbols']);
2414 self::assertIsArray($result['urls']);
2415 self::assertIsArray($result['user_mentions']);
2419 * Test the api_format_items_embeded_images() function.
2423 public function testApiFormatItemsEmbededImages()
2426 'text ' . DI::baseUrl() . '/display/item_guid',
2427 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2432 * Test the api_contactlink_to_array() function.
2436 public function testApiContactlinkToArray()
2443 api_contactlink_to_array('text')
2448 * Test the api_contactlink_to_array() function with an URL.
2452 public function testApiContactlinkToArrayWithUrl()
2456 'name' => ['link_text'],
2459 api_contactlink_to_array('text <a href="url">link_text</a>')
2464 * Test the api_format_items_activities() function.
2468 public function testApiFormatItemsActivities()
2470 $item = ['uid' => 0, 'uri' => ''];
2471 $result = api_format_items_activities($item);
2472 self::assertArrayHasKey('like', $result);
2473 self::assertArrayHasKey('dislike', $result);
2474 self::assertArrayHasKey('attendyes', $result);
2475 self::assertArrayHasKey('attendno', $result);
2476 self::assertArrayHasKey('attendmaybe', $result);
2480 * Test the api_format_items_activities() function with an XML result.
2484 public function testApiFormatItemsActivitiesWithXml()
2486 $item = ['uid' => 0, 'uri' => ''];
2487 $result = api_format_items_activities($item, 'xml');
2488 self::assertArrayHasKey('friendica:like', $result);
2489 self::assertArrayHasKey('friendica:dislike', $result);
2490 self::assertArrayHasKey('friendica:attendyes', $result);
2491 self::assertArrayHasKey('friendica:attendno', $result);
2492 self::assertArrayHasKey('friendica:attendmaybe', $result);
2496 * Test the api_format_items() function.
2497 * @doesNotPerformAssertions
2499 public function testApiFormatItems()
2503 'item_network' => 'item_network',
2509 'author-network' => Protocol::DFRN,
2510 'author-link' => 'http://localhost/profile/othercontact',
2514 $result = api_format_items($items, ['id' => 0], true);
2515 foreach ($result as $status) {
2516 self::assertStatus($status);
2521 * Test the api_format_items() function with an XML result.
2522 * @doesNotPerformAssertions
2524 public function testApiFormatItemsWithXml()
2532 'author-network' => Protocol::DFRN,
2533 'author-link' => 'http://localhost/profile/othercontact',
2537 $result = api_format_items($items, ['id' => 0], true, 'xml');
2538 foreach ($result as $status) {
2539 self::assertStatus($status);
2544 * Test the api_format_items() function.
2548 public function testApiAccountRateLimitStatus()
2550 // @todo How to test the new API?
2551 // $result = api_account_rate_limit_status('json');
2552 // self::assertEquals(150, $result['hash']['remaining_hits']);
2553 // self::assertEquals(150, $result['hash']['hourly_limit']);
2554 // self::assertIsInt($result['hash']['reset_time_in_seconds']);
2558 * Test the api_format_items() function with an XML result.
2562 public function testApiAccountRateLimitStatusWithXml()
2564 // @todo How to test the new API?
2565 // $result = api_account_rate_limit_status('xml');
2566 // self::assertXml($result, 'hash');
2570 * Test the api_help_test() function.
2574 public function testApiHelpTest()
2576 // @todo How to test the new API?
2577 // $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'json']);
2578 // self::assertEquals(['ok' => 'ok'], $result);
2582 * Test the api_help_test() function with an XML result.
2586 public function testApiHelpTestWithXml()
2588 // @todo How to test the new API?
2589 // $result = api_help_test('xml');
2590 // self::assertXml($result, 'ok');
2594 * Test the api_lists_list() function.
2598 public function testApiListsList()
2600 $result = api_lists_list('json');
2601 self::assertEquals(['lists_list' => []], $result);
2605 * Test the api_lists_ownerships() function.
2609 public function testApiListsOwnerships()
2611 $result = api_lists_ownerships('json');
2612 foreach ($result['lists']['lists'] as $list) {
2613 self::assertList($list);
2618 * Test the api_lists_ownerships() function without an authenticated user.
2622 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2624 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2625 $_SESSION['authenticated'] = false;
2626 api_lists_ownerships('json');
2630 * Test the api_lists_statuses() function.
2634 public function testApiListsStatuses()
2636 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2637 api_lists_statuses('json');
2641 * Test the api_lists_statuses() function with a list ID.
2642 * @doesNotPerformAssertions
2644 public function testApiListsStatusesWithListId()
2646 $_REQUEST['list_id'] = 1;
2647 $_REQUEST['page'] = -1;
2648 $_REQUEST['max_id'] = 10;
2649 $result = api_lists_statuses('json');
2650 foreach ($result['status'] as $status) {
2651 self::assertStatus($status);
2656 * Test the api_lists_statuses() function with a list ID and a RSS result.
2660 public function testApiListsStatusesWithListIdAndRss()
2662 $_REQUEST['list_id'] = 1;
2663 $result = api_lists_statuses('rss');
2664 self::assertXml($result, 'statuses');
2668 * Test the api_lists_statuses() function with an unallowed user.
2672 public function testApiListsStatusesWithUnallowedUser()
2674 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2675 $_SESSION['allow_api'] = false;
2676 $_GET['screen_name'] = $this->selfUser['nick'];
2677 api_lists_statuses('json');
2681 * Test the api_statuses_f() function.
2685 public function testApiStatusesFWithFriends()
2688 $result = api_statuses_f('friends');
2689 self::assertArrayHasKey('user', $result);
2693 * Test the api_statuses_f() function.
2697 public function testApiStatusesFWithFollowers()
2699 $result = api_statuses_f('followers');
2700 self::assertArrayHasKey('user', $result);
2704 * Test the api_statuses_f() function.
2708 public function testApiStatusesFWithBlocks()
2710 $result = api_statuses_f('blocks');
2711 self::assertArrayHasKey('user', $result);
2715 * Test the api_statuses_f() function.
2719 public function testApiStatusesFWithIncoming()
2721 $result = api_statuses_f('incoming');
2722 self::assertArrayHasKey('user', $result);
2726 * Test the api_statuses_f() function an undefined cursor GET variable.
2730 public function testApiStatusesFWithUndefinedCursor()
2732 $_GET['cursor'] = 'undefined';
2733 self::assertFalse(api_statuses_f('friends'));
2737 * Test the api_statuses_friends() function.
2741 public function testApiStatusesFriends()
2743 $result = api_statuses_friends('json');
2744 self::assertArrayHasKey('user', $result);
2748 * Test the api_statuses_friends() function an undefined cursor GET variable.
2752 public function testApiStatusesFriendsWithUndefinedCursor()
2754 $_GET['cursor'] = 'undefined';
2755 self::assertFalse(api_statuses_friends('json'));
2759 * Test the api_statuses_followers() function.
2763 public function testApiStatusesFollowers()
2765 $result = api_statuses_followers('json');
2766 self::assertArrayHasKey('user', $result);
2770 * Test the api_statuses_followers() function an undefined cursor GET variable.
2774 public function testApiStatusesFollowersWithUndefinedCursor()
2776 $_GET['cursor'] = 'undefined';
2777 self::assertFalse(api_statuses_followers('json'));
2781 * Test the api_blocks_list() function.
2785 public function testApiBlocksList()
2787 $result = api_blocks_list('json');
2788 self::assertArrayHasKey('user', $result);
2792 * Test the api_blocks_list() function an undefined cursor GET variable.
2796 public function testApiBlocksListWithUndefinedCursor()
2798 $_GET['cursor'] = 'undefined';
2799 self::assertFalse(api_blocks_list('json'));
2803 * Test the api_friendships_incoming() function.
2807 public function testApiFriendshipsIncoming()
2809 $result = api_friendships_incoming('json');
2810 self::assertArrayHasKey('id', $result);
2814 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2818 public function testApiFriendshipsIncomingWithUndefinedCursor()
2820 $_GET['cursor'] = 'undefined';
2821 self::assertFalse(api_friendships_incoming('json'));
2825 * Test the api_statusnet_config() function.
2829 public function testApiStatusnetConfig()
2831 $result = api_statusnet_config('json');
2832 self::assertEquals('localhost', $result['config']['site']['server']);
2833 self::assertEquals('default', $result['config']['site']['theme']);
2834 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2835 self::assertTrue($result['config']['site']['fancy']);
2836 self::assertEquals('en', $result['config']['site']['language']);
2837 self::assertEquals('UTC', $result['config']['site']['timezone']);
2838 self::assertEquals(200000, $result['config']['site']['textlimit']);
2839 self::assertEquals('false', $result['config']['site']['private']);
2840 self::assertEquals('false', $result['config']['site']['ssl']);
2841 self::assertEquals(30, $result['config']['site']['shorturllength']);
2845 * Test the api_statusnet_version() function.
2849 public function testApiStatusnetVersion()
2851 // @todo How to test the new API?
2852 // $result = api_statusnet_version('json');
2853 // self::assertEquals('0.9.7', $result['version']);
2857 * Test the api_direct_messages_new() function.
2861 public function testApiDirectMessagesNew()
2863 $result = api_direct_messages_new('json');
2864 self::assertNull($result);
2868 * Test the api_direct_messages_new() function without an authenticated user.
2872 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2874 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2875 $_SESSION['authenticated'] = false;
2876 api_direct_messages_new('json');
2880 * Test the api_direct_messages_new() function with an user ID.
2884 public function testApiDirectMessagesNewWithUserId()
2886 $_POST['text'] = 'message_text';
2887 $_POST['user_id'] = $this->otherUser['id'];
2888 $result = api_direct_messages_new('json');
2889 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2893 * Test the api_direct_messages_new() function with a screen name.
2897 public function testApiDirectMessagesNewWithScreenName()
2899 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2900 $_POST['text'] = 'message_text';
2901 $_POST['screen_name'] = $this->friendUser['nick'];
2902 $result = api_direct_messages_new('json');
2903 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2904 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2905 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2909 * Test the api_direct_messages_new() function with a title.
2913 public function testApiDirectMessagesNewWithTitle()
2915 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2916 $_POST['text'] = 'message_text';
2917 $_POST['screen_name'] = $this->friendUser['nick'];
2918 $_REQUEST['title'] = 'message_title';
2919 $result = api_direct_messages_new('json');
2920 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2921 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2922 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2923 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2927 * Test the api_direct_messages_new() function with an RSS result.
2931 public function testApiDirectMessagesNewWithRss()
2933 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2934 $_POST['text'] = 'message_text';
2935 $_POST['screen_name'] = $this->friendUser['nick'];
2936 $result = api_direct_messages_new('rss');
2937 self::assertXml($result, 'direct-messages');
2941 * Test the api_direct_messages_destroy() function.
2945 public function testApiDirectMessagesDestroy()
2947 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2948 api_direct_messages_destroy('json');
2952 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2956 public function testApiDirectMessagesDestroyWithVerbose()
2958 $_GET['friendica_verbose'] = 'true';
2959 $result = api_direct_messages_destroy('json');
2963 'result' => 'error',
2964 'message' => 'message id or parenturi not specified'
2972 * Test the api_direct_messages_destroy() function without an authenticated user.
2976 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2978 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2979 $_SESSION['authenticated'] = false;
2980 api_direct_messages_destroy('json');
2984 * Test the api_direct_messages_destroy() function with a non-zero ID.
2988 public function testApiDirectMessagesDestroyWithId()
2990 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2991 $_REQUEST['id'] = 1;
2992 api_direct_messages_destroy('json');
2996 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
3000 public function testApiDirectMessagesDestroyWithIdAndVerbose()
3002 $_REQUEST['id'] = 1;
3003 $_REQUEST['friendica_parenturi'] = 'parent_uri';
3004 $_GET['friendica_verbose'] = 'true';
3005 $result = api_direct_messages_destroy('json');
3009 'result' => 'error',
3010 'message' => 'message id not in database'
3018 * Test the api_direct_messages_destroy() function with a non-zero ID.
3022 public function testApiDirectMessagesDestroyWithCorrectId()
3024 $this->markTestIncomplete('We need to add a dataset for this.');
3028 * Test the api_direct_messages_box() function.
3032 public function testApiDirectMessagesBoxWithSentbox()
3034 $_REQUEST['page'] = -1;
3035 $_REQUEST['max_id'] = 10;
3036 $result = api_direct_messages_box('json', 'sentbox', 'false');
3037 self::assertArrayHasKey('direct_message', $result);
3041 * Test the api_direct_messages_box() function.
3045 public function testApiDirectMessagesBoxWithConversation()
3047 $result = api_direct_messages_box('json', 'conversation', 'false');
3048 self::assertArrayHasKey('direct_message', $result);
3052 * Test the api_direct_messages_box() function.
3056 public function testApiDirectMessagesBoxWithAll()
3058 $result = api_direct_messages_box('json', 'all', 'false');
3059 self::assertArrayHasKey('direct_message', $result);
3063 * Test the api_direct_messages_box() function.
3067 public function testApiDirectMessagesBoxWithInbox()
3069 $result = api_direct_messages_box('json', 'inbox', 'false');
3070 self::assertArrayHasKey('direct_message', $result);
3074 * Test the api_direct_messages_box() function.
3078 public function testApiDirectMessagesBoxWithVerbose()
3080 $result = api_direct_messages_box('json', 'sentbox', 'true');
3084 'result' => 'error',
3085 'message' => 'no mails available'
3093 * Test the api_direct_messages_box() function with a RSS result.
3097 public function testApiDirectMessagesBoxWithRss()
3099 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3100 self::assertXml($result, 'direct-messages');
3104 * Test the api_direct_messages_box() function without an authenticated user.
3108 public function testApiDirectMessagesBoxWithUnallowedUser()
3110 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3111 $_SESSION['allow_api'] = false;
3112 $_GET['screen_name'] = $this->selfUser['nick'];
3113 api_direct_messages_box('json', 'sentbox', 'false');
3117 * Test the api_direct_messages_sentbox() function.
3121 public function testApiDirectMessagesSentbox()
3123 $result = api_direct_messages_sentbox('json');
3124 self::assertArrayHasKey('direct_message', $result);
3128 * Test the api_direct_messages_inbox() function.
3132 public function testApiDirectMessagesInbox()
3134 $result = api_direct_messages_inbox('json');
3135 self::assertArrayHasKey('direct_message', $result);
3139 * Test the api_direct_messages_all() function.
3143 public function testApiDirectMessagesAll()
3145 $result = api_direct_messages_all('json');
3146 self::assertArrayHasKey('direct_message', $result);
3150 * Test the api_direct_messages_conversation() function.
3154 public function testApiDirectMessagesConversation()
3156 $result = api_direct_messages_conversation('json');
3157 self::assertArrayHasKey('direct_message', $result);
3161 * Test the api_oauth_request_token() function.
3165 public function testApiOauthRequestToken()
3167 $this->markTestIncomplete('exit() kills phpunit as well');
3171 * Test the api_oauth_access_token() function.
3175 public function testApiOauthAccessToken()
3177 $this->markTestIncomplete('exit() kills phpunit as well');
3181 * Test the api_fr_photoalbum_delete() function.
3185 public function testApiFrPhotoalbumDelete()
3187 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3188 // api_fr_photoalbum_delete('json');
3192 * Test the api_fr_photoalbum_delete() function with an album name.
3196 public function testApiFrPhotoalbumDeleteWithAlbum()
3198 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3199 // $_REQUEST['album'] = 'album_name';
3200 // api_fr_photoalbum_delete('json');
3204 * Test the api_fr_photoalbum_delete() function with an album name.
3208 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3210 $this->markTestIncomplete('We need to add a dataset for this.');
3214 * Test the api_fr_photoalbum_delete() function.
3218 public function testApiFrPhotoalbumUpdate()
3220 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3221 // api_fr_photoalbum_update('json');
3225 * Test the api_fr_photoalbum_delete() function with an album name.
3229 public function testApiFrPhotoalbumUpdateWithAlbum()
3231 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3232 // $_REQUEST['album'] = 'album_name';
3233 // api_fr_photoalbum_update('json');
3237 * Test the api_fr_photoalbum_delete() function with an album name.
3241 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3243 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3244 // $_REQUEST['album'] = 'album_name';
3245 // $_REQUEST['album_new'] = 'album_name';
3246 // api_fr_photoalbum_update('json');
3250 * Test the api_fr_photoalbum_update() function without an authenticated user.
3254 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3256 // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3257 // $_SESSION['authenticated'] = false;
3258 // api_fr_photoalbum_update('json');
3262 * Test the api_fr_photoalbum_delete() function with an album name.
3266 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3268 $this->markTestIncomplete('We need to add a dataset for this.');
3272 * Test the api_fr_photos_list() function.
3276 public function testApiFrPhotosList()
3278 $result = api_fr_photos_list('json');
3279 self::assertArrayHasKey('photo', $result);
3283 * Test the api_fr_photos_list() function without an authenticated user.
3287 public function testApiFrPhotosListWithoutAuthenticatedUser()
3289 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3290 $_SESSION['authenticated'] = false;
3291 api_fr_photos_list('json');
3295 * Test the api_fr_photo_create_update() function.
3297 public function testApiFrPhotoCreateUpdate()
3299 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3300 api_fr_photo_create_update('json');
3304 * Test the api_fr_photo_create_update() function without an authenticated user.
3308 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3310 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3311 $_SESSION['authenticated'] = false;
3312 api_fr_photo_create_update('json');
3316 * Test the api_fr_photo_create_update() function with an album name.
3320 public function testApiFrPhotoCreateUpdateWithAlbum()
3322 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3323 $_REQUEST['album'] = 'album_name';
3324 api_fr_photo_create_update('json');
3328 * Test the api_fr_photo_create_update() function with the update mode.
3332 public function testApiFrPhotoCreateUpdateWithUpdate()
3334 $this->markTestIncomplete('We need to create a dataset for this');
3338 * Test the api_fr_photo_create_update() function with an uploaded file.
3342 public function testApiFrPhotoCreateUpdateWithFile()
3344 $this->markTestIncomplete();
3348 * Test the api_fr_photo_delete() function.
3352 public function testApiFrPhotoDelete()
3354 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3355 // api_fr_photo_delete('json');
3359 * Test the api_fr_photo_delete() function without an authenticated user.
3363 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3365 // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3366 // $_SESSION['authenticated'] = false;
3367 // api_fr_photo_delete('json');
3371 * Test the api_fr_photo_delete() function with a photo ID.
3375 public function testApiFrPhotoDeleteWithPhotoId()
3377 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3378 // $_REQUEST['photo_id'] = 1;
3379 // api_fr_photo_delete('json');
3383 * Test the api_fr_photo_delete() function with a correct photo ID.
3387 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3389 $this->markTestIncomplete('We need to create a dataset for this.');
3393 * Test the api_fr_photo_detail() function.
3397 public function testApiFrPhotoDetail()
3399 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3400 api_fr_photo_detail('json');
3404 * Test the api_fr_photo_detail() function without an authenticated user.
3408 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3410 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3411 $_SESSION['authenticated'] = false;
3412 api_fr_photo_detail('json');
3416 * Test the api_fr_photo_detail() function with a photo ID.
3420 public function testApiFrPhotoDetailWithPhotoId()
3422 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3423 $_REQUEST['photo_id'] = 1;
3424 api_fr_photo_detail('json');
3428 * Test the api_fr_photo_detail() function with a correct photo ID.
3432 public function testApiFrPhotoDetailCorrectPhotoId()
3434 $this->markTestIncomplete('We need to create a dataset for this.');
3438 * Test the api_account_update_profile_image() function.
3442 public function testApiAccountUpdateProfileImage()
3444 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3445 api_account_update_profile_image('json');
3449 * Test the api_account_update_profile_image() function without an authenticated user.
3453 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3455 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3456 $_SESSION['authenticated'] = false;
3457 api_account_update_profile_image('json');
3461 * Test the api_account_update_profile_image() function with an uploaded file.
3465 public function testApiAccountUpdateProfileImageWithUpload()
3467 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3468 $this->markTestIncomplete();
3473 * Test the api_account_update_profile() function.
3477 public function testApiAccountUpdateProfile()
3479 $_POST['name'] = 'new_name';
3480 $_POST['description'] = 'new_description';
3481 $result = api_account_update_profile('json');
3482 // We can't use assertSelfUser() here because the user object is missing some properties.
3483 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3484 self::assertEquals('DFRN', $result['user']['location']);
3485 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3486 self::assertEquals('dfrn', $result['user']['network']);
3487 self::assertEquals('new_name', $result['user']['name']);
3488 self::assertEquals('new_description', $result['user']['description']);
3492 * Test the check_acl_input() function.
3496 public function testCheckAclInput()
3498 $result = check_acl_input('<aclstring>');
3499 // Where does this result come from?
3500 self::assertEquals(1, $result);
3504 * Test the check_acl_input() function with an empty ACL string.
3508 public function testCheckAclInputWithEmptyAclString()
3510 $result = check_acl_input(' ');
3511 self::assertFalse($result);
3515 * Test the save_media_to_database() function.
3519 public function testSaveMediaToDatabase()
3521 $this->markTestIncomplete();
3525 * Test the post_photo_item() function.
3529 public function testPostPhotoItem()
3531 $this->markTestIncomplete();
3535 * Test the prepare_photo_data() function.
3539 public function testPreparePhotoData()
3541 $this->markTestIncomplete();
3545 * Test the api_share_as_retweet() function with a valid item.
3549 public function testApiShareAsRetweetWithValidItem()
3551 $this->markTestIncomplete();
3555 * Test the api_in_reply_to() function.
3559 public function testApiInReplyTo()
3561 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3562 self::assertArrayHasKey('status_id', $result);
3563 self::assertArrayHasKey('user_id', $result);
3564 self::assertArrayHasKey('status_id_str', $result);
3565 self::assertArrayHasKey('user_id_str', $result);
3566 self::assertArrayHasKey('screen_name', $result);
3570 * Test the api_in_reply_to() function with a valid item.
3574 public function testApiInReplyToWithValidItem()
3576 $this->markTestIncomplete();
3580 * Test the api_clean_plain_items() function.
3584 public function testApiCleanPlainItems()
3586 $_REQUEST['include_entities'] = 'true';
3587 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3588 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3592 * Test the api_best_nickname() function.
3596 public function testApiBestNickname()
3599 $result = api_best_nickname($contacts);
3600 self::assertNull($result);
3604 * Test the api_best_nickname() function with contacts.
3608 public function testApiBestNicknameWithContacts()
3610 $this->markTestIncomplete();
3614 * Test the api_friendica_group_show() function.
3618 public function testApiFriendicaGroupShow()
3620 $this->markTestIncomplete();
3624 * Test the api_friendica_group_delete() function.
3628 public function testApiFriendicaGroupDelete()
3630 $this->markTestIncomplete();
3634 * Test the api_lists_destroy() function.
3638 public function testApiListsDestroy()
3640 $this->markTestIncomplete();
3644 * Test the group_create() function.
3648 public function testGroupCreate()
3650 $this->markTestIncomplete();
3654 * Test the api_friendica_group_create() function.
3658 public function testApiFriendicaGroupCreate()
3660 $this->markTestIncomplete();
3664 * Test the api_lists_create() function.
3668 public function testApiListsCreate()
3670 $this->markTestIncomplete();
3674 * Test the api_friendica_group_update() function.
3678 public function testApiFriendicaGroupUpdate()
3680 $this->markTestIncomplete();
3684 * Test the api_lists_update() function.
3688 public function testApiListsUpdate()
3690 $this->markTestIncomplete();
3694 * Test the api_friendica_activity() function.
3698 public function testApiFriendicaActivity()
3700 $this->markTestIncomplete();
3704 * Test the api_friendica_notification() function.
3708 public function testApiFriendicaNotification()
3710 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3711 // api_friendica_notification('json');
3715 * Test the api_friendica_notification() function without an authenticated user.
3719 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3721 // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3722 // $_SESSION['authenticated'] = false;
3723 // api_friendica_notification('json');
3727 * Test the api_friendica_notification() function with empty result
3731 public function testApiFriendicaNotificationWithEmptyResult()
3733 // DI::args()->setArgv(['api', 'friendica', 'notification']);
3734 // $_SESSION['uid'] = 41;
3735 // $result = api_friendica_notification('json');
3736 // self::assertEquals(['note' => false], $result);
3740 * Test the api_friendica_notification() function with an XML result.
3744 public function testApiFriendicaNotificationWithXmlResult()
3747 DI::args()->setArgv(['api', 'friendica', 'notification']);
3748 $result = api_friendica_notification('xml');
3749 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3750 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3753 <?xml version="1.0"?>
3755 <note date="$date" date_rel="$dateRel" id="1" iid="4" link="http://localhost/notification/1" msg="A test reply from an item" msg_cache="A test reply from an item" msg_html="A test reply from an item" msg_plain="A test reply from an item" name="Reply to" name_cache="Reply to" otype="item" parent="" photo="http://localhost/" seen="false" timestamp="1577880722" type="8" uid="42" url="http://localhost/display/1" verb="http://activitystrea.ms/schema/1.0/post"/>
3758 self::assertXmlStringEqualsXmlString($assertXml, $result);
3763 * Test the api_friendica_notification() function with an JSON result.
3767 public function testApiFriendicaNotificationWithJsonResult()
3769 // DI::args()->setArgv(['api', 'friendica', 'notification']);
3770 // $result = json_encode(api_friendica_notification('json'));
3771 // self::assertJson($result);
3775 * Test the api_friendica_notification_seen() function.
3779 public function testApiFriendicaNotificationSeen()
3781 $this->markTestIncomplete();
3785 * Test the api_friendica_direct_messages_setseen() function.
3789 public function testApiFriendicaDirectMessagesSetseen()
3791 $this->markTestIncomplete();
3795 * Test the api_friendica_direct_messages_search() function.
3799 public function testApiFriendicaDirectMessagesSearch()
3801 $this->markTestIncomplete();
3805 * Test the api_saved_searches_list() function.
3809 public function testApiSavedSearchesList()
3811 // $result = api_saved_searches_list('json');
3812 // self::assertEquals(1, $result['terms'][0]['id']);
3813 // self::assertEquals(1, $result['terms'][0]['id_str']);
3814 // self::assertEquals('Saved search', $result['terms'][0]['name']);
3815 // self::assertEquals('Saved search', $result['terms'][0]['query']);