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()
482 'method' => 'method',
483 'func' => function () {
487 $_SERVER['REQUEST_METHOD'] = 'method';
488 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
490 $args = DI::args()->determine($_SERVER, $_GET);
493 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
494 api_call($this->app, $args)
499 * Test the api_call() function with an unimplemented API.
501 * @runInSeparateProcess
502 * @preserveGlobalState disabled
504 public function testApiCallWithUninplementedApi()
507 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
513 * Test the api_call() function with a JSON result.
515 * @runInSeparateProcess
516 * @preserveGlobalState disabled
518 public function testApiCallWithJson()
522 'method' => 'method',
523 'func' => function () {
524 return ['data' => ['some_data']];
527 $_SERVER['REQUEST_METHOD'] = 'method';
528 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
530 $args = DI::args()->determine($_SERVER, $_GET);
534 api_call($this->app, $args)
539 * Test the api_call() function with an XML result.
541 * @runInSeparateProcess
542 * @preserveGlobalState disabled
544 public function testApiCallWithXml()
548 'method' => 'method',
549 'func' => function () {
553 $_SERVER['REQUEST_METHOD'] = 'method';
554 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
556 $args = DI::args()->determine($_SERVER, $_GET);
560 api_call($this->app, $args)
565 * Test the api_call() function with an RSS result.
567 * @runInSeparateProcess
568 * @preserveGlobalState disabled
570 public function testApiCallWithRss()
574 'method' => 'method',
575 'func' => function () {
579 $_SERVER['REQUEST_METHOD'] = 'method';
580 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
582 $args = DI::args()->determine($_SERVER, $_GET);
585 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
587 api_call($this->app, $args)
592 * Test the api_call() function with an Atom result.
594 * @runInSeparateProcess
595 * @preserveGlobalState disabled
597 public function testApiCallWithAtom()
601 'method' => 'method',
602 'func' => function () {
606 $_SERVER['REQUEST_METHOD'] = 'method';
607 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
609 $args = DI::args()->determine($_SERVER, $_GET);
612 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
614 api_call($this->app, $args)
619 * Test the api_call() function with an unallowed method.
621 * @runInSeparateProcess
622 * @preserveGlobalState disabled
624 public function testApiCallWithWrongMethod()
627 $API['api_path'] = ['method' => 'method'];
629 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
631 $args = DI::args()->determine($_SERVER, $_GET);
634 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
635 api_call($this->app, $args)
640 * Test the api_call() function with an unauthorized user.
642 * @runInSeparateProcess
643 * @preserveGlobalState disabled
645 public function testApiCallWithWrongAuth()
649 'method' => 'method',
652 $_SESSION['authenticated'] = false;
653 $_SERVER['REQUEST_METHOD'] = 'method';
654 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
656 $args = DI::args()->determine($_SERVER, $_GET);
659 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
660 api_call($this->app, $args)
665 * Test the api_error() function with a JSON result.
667 * @runInSeparateProcess
668 * @preserveGlobalState disabled
670 public function testApiErrorWithJson()
673 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
674 api_error('json', new HTTPException\OKException('error_message'), DI::args())
679 * Test the api_error() function with an XML result.
681 * @runInSeparateProcess
682 * @preserveGlobalState disabled
684 public function testApiErrorWithXml()
687 '<?xml version="1.0"?>' . "\n" .
688 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
689 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
690 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
691 ' <error>error_message</error>' . "\n" .
692 ' <code>200 OK</code>' . "\n" .
693 ' <request/>' . "\n" .
695 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
700 * Test the api_error() function with an RSS result.
702 * @runInSeparateProcess
703 * @preserveGlobalState disabled
705 public function testApiErrorWithRss()
708 '<?xml version="1.0"?>' . "\n" .
709 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
710 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
711 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
712 ' <error>error_message</error>' . "\n" .
713 ' <code>200 OK</code>' . "\n" .
714 ' <request/>' . "\n" .
716 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
721 * Test the api_error() function with an Atom result.
723 * @runInSeparateProcess
724 * @preserveGlobalState disabled
726 public function testApiErrorWithAtom()
729 '<?xml version="1.0"?>' . "\n" .
730 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
731 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
732 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
733 ' <error>error_message</error>' . "\n" .
734 ' <code>200 OK</code>' . "\n" .
735 ' <request/>' . "\n" .
737 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
742 * Test the api_rss_extra() function.
746 public function testApiRssExtra()
748 $user_info = ['url' => 'user_url', 'lang' => 'en'];
749 $result = api_rss_extra($this->app, [], $user_info);
750 self::assertEquals($user_info, $result['$user']);
751 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
752 self::assertArrayHasKey('self', $result['$rss']);
753 self::assertArrayHasKey('base', $result['$rss']);
754 self::assertArrayHasKey('updated', $result['$rss']);
755 self::assertArrayHasKey('atom_updated', $result['$rss']);
756 self::assertArrayHasKey('language', $result['$rss']);
757 self::assertArrayHasKey('logo', $result['$rss']);
761 * Test the api_rss_extra() function without any user info.
765 public function testApiRssExtraWithoutUserInfo()
767 $result = api_rss_extra($this->app, [], null);
768 self::assertIsArray($result['$user']);
769 self::assertArrayHasKey('alternate', $result['$rss']);
770 self::assertArrayHasKey('self', $result['$rss']);
771 self::assertArrayHasKey('base', $result['$rss']);
772 self::assertArrayHasKey('updated', $result['$rss']);
773 self::assertArrayHasKey('atom_updated', $result['$rss']);
774 self::assertArrayHasKey('language', $result['$rss']);
775 self::assertArrayHasKey('logo', $result['$rss']);
779 * Test the api_unique_id_to_nurl() function.
783 public function testApiUniqueIdToNurl()
785 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
789 * Test the api_unique_id_to_nurl() function with a correct ID.
793 public function testApiUniqueIdToNurlWithCorrectId()
795 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
799 * Test the api_get_user() function.
803 public function testApiGetUser()
805 $user = api_get_user();
806 self::assertSelfUser($user);
807 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
808 self::assertEquals('6fdbe8', $user['profile_link_color']);
809 self::assertEquals('ededed', $user['profile_background_color']);
813 * Test the api_get_user() function with a Frio schema.
817 public function testApiGetUserWithFrioSchema()
819 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
820 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
821 $user = api_get_user();
822 self::assertSelfUser($user);
823 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
824 self::assertEquals('6fdbe8', $user['profile_link_color']);
825 self::assertEquals('ededed', $user['profile_background_color']);
829 * Test the api_get_user() function with an empty Frio schema.
833 public function testApiGetUserWithEmptyFrioSchema()
835 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
836 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
837 $user = api_get_user();
838 self::assertSelfUser($user);
839 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
840 self::assertEquals('6fdbe8', $user['profile_link_color']);
841 self::assertEquals('ededed', $user['profile_background_color']);
845 * Test the api_get_user() function with a custom Frio schema.
849 public function testApiGetUserWithCustomFrioSchema()
851 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
852 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
853 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
854 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
855 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
856 $user = api_get_user();
857 self::assertSelfUser($user);
858 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
859 self::assertEquals('123456', $user['profile_link_color']);
860 self::assertEquals('123456', $user['profile_background_color']);
864 * Test the api_get_user() function with an user that is not allowed to use the API.
866 * @runInSeparateProcess
867 * @preserveGlobalState disabled
869 public function testApiGetUserWithoutApiUser()
871 $_SERVER['PHP_AUTH_USER'] = 'Test user';
872 $_SERVER['PHP_AUTH_PW'] = 'password';
873 $_SESSION['allow_api'] = false;
874 self::assertFalse(api_get_user());
878 * Test the api_get_user() function with an user ID in a GET parameter.
882 public function testApiGetUserWithGetId()
884 $_GET['user_id'] = $this->otherUser['id'];
885 self::assertOtherUser(api_get_user());
889 * Test the api_get_user() function with a wrong user ID in a GET parameter.
893 public function testApiGetUserWithWrongGetId()
895 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
896 $_GET['user_id'] = $this->wrongUserId;
897 self::assertOtherUser(api_get_user());
901 * Test the api_get_user() function with an user name in a GET parameter.
905 public function testApiGetUserWithGetName()
907 $_GET['screen_name'] = $this->selfUser['nick'];
908 self::assertSelfUser(api_get_user());
912 * Test the api_get_user() function with a profile URL in a GET parameter.
916 public function testApiGetUserWithGetUrl()
918 $_GET['profileurl'] = $this->selfUser['nurl'];
919 self::assertSelfUser(api_get_user());
923 * Test the api_get_user() function with an user ID in the API path.
927 public function testApiGetUserWithNumericCalledApi()
930 $called_api = ['api_path'];
931 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
932 self::assertOtherUser(api_get_user());
936 * Test the api_get_user() function with the $called_api global variable.
940 public function testApiGetUserWithCalledApi()
943 $called_api = ['api', 'api_path'];
944 self::assertSelfUser(api_get_user());
948 * Test the api_get_user() function with a valid user.
952 public function testApiGetUserWithCorrectUser()
954 self::assertOtherUser(api_get_user($this->otherUser['id']));
958 * Test the api_get_user() function with a wrong user ID.
962 public function testApiGetUserWithWrongUser()
964 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
965 self::assertOtherUser(api_get_user($this->wrongUserId));
969 * Test the api_get_user() function with a 0 user ID.
973 public function testApiGetUserWithZeroUser()
975 self::assertSelfUser(api_get_user(0));
979 * Test the api_item_get_user() function.
983 public function testApiItemGetUser()
985 $users = api_item_get_user($this->app, []);
986 self::assertSelfUser($users[0]);
990 * Test the api_item_get_user() function with a different item parent.
994 public function testApiItemGetUserWithDifferentParent()
996 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
997 self::assertSelfUser($users[0]);
998 self::assertEquals($users[0], $users[1]);
1002 * Test the Arrays::walkRecursive() function.
1006 public function testApiWalkRecursive()
1011 Arrays::walkRecursive(
1014 // Should we test this with a callback that actually does something?
1022 * Test the Arrays::walkRecursive() function with an array.
1026 public function testApiWalkRecursiveWithArray()
1028 $array = [['item1'], ['item2']];
1031 Arrays::walkRecursive(
1034 // Should we test this with a callback that actually does something?
1042 * Test the BaseApi::reformatXML() function.
1046 public function testApiReformatXml()
1050 self::assertTrue(BaseApi::reformatXML($item, $key));
1051 self::assertEquals('true', $item);
1055 * Test the BaseApi::reformatXML() function with a statusnet_api key.
1059 public function testApiReformatXmlWithStatusnetKey()
1062 $key = 'statusnet_api';
1063 self::assertTrue(BaseApi::reformatXML($item, $key));
1064 self::assertEquals('statusnet:api', $key);
1068 * Test the BaseApi::reformatXML() function with a friendica_api key.
1072 public function testApiReformatXmlWithFriendicaKey()
1075 $key = 'friendica_api';
1076 self::assertTrue(BaseApi::reformatXML($item, $key));
1077 self::assertEquals('friendica:api', $key);
1081 * Test the BaseApi::createXML() function.
1085 public function testApiCreateXml()
1088 '<?xml version="1.0"?>' . "\n" .
1089 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1090 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1091 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1092 ' <data>some_data</data>' . "\n" .
1093 '</root_element>' . "\n",
1094 BaseApi::createXML(['data' => ['some_data']], 'root_element')
1099 * Test the BaseApi::createXML() function without any XML namespace.
1103 public function testApiCreateXmlWithoutNamespaces()
1106 '<?xml version="1.0"?>' . "\n" .
1108 ' <data>some_data</data>' . "\n" .
1110 BaseApi::createXML(['data' => ['some_data']], 'ok')
1115 * Test the BaseApi::formatData() function.
1119 public function testApiFormatData()
1121 $data = ['some_data'];
1122 self::assertEquals($data, BaseApi::formatData('root_element', 'json', $data));
1126 * Test the BaseApi::formatData() function with an XML result.
1130 public function testApiFormatDataWithXml()
1133 '<?xml version="1.0"?>' . "\n" .
1134 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1135 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1136 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1137 ' <data>some_data</data>' . "\n" .
1138 '</root_element>' . "\n",
1139 BaseApi::formatData('root_element', 'xml', ['data' => ['some_data']])
1144 * Test the api_account_verify_credentials() function.
1148 public function testApiAccountVerifyCredentials()
1150 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1154 * Test the api_account_verify_credentials() function without an authenticated user.
1158 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1160 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1161 $_SESSION['authenticated'] = false;
1162 api_account_verify_credentials('json');
1166 * Test the requestdata() function.
1170 public function testRequestdata()
1172 self::assertNull(requestdata('variable_name'));
1176 * Test the requestdata() function with a POST parameter.
1180 public function testRequestdataWithPost()
1182 $_POST['variable_name'] = 'variable_value';
1183 self::assertEquals('variable_value', requestdata('variable_name'));
1187 * Test the requestdata() function with a GET parameter.
1191 public function testRequestdataWithGet()
1193 $_GET['variable_name'] = 'variable_value';
1194 self::assertEquals('variable_value', requestdata('variable_name'));
1198 * Test the api_statuses_mediap() function.
1202 public function testApiStatusesMediap()
1204 DI::args()->setArgc(2);
1212 'tmp_name' => $this->getTempImage(),
1213 'name' => 'spacer.png',
1214 'type' => 'image/png'
1217 $_GET['status'] = '<b>Status content</b>';
1219 $result = api_statuses_mediap('json');
1220 self::assertStatus($result['status']);
1224 * Test the api_statuses_mediap() function without an authenticated user.
1228 public function testApiStatusesMediapWithoutAuthenticatedUser()
1230 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1231 $_SESSION['authenticated'] = false;
1232 api_statuses_mediap('json');
1236 * Test the api_statuses_update() function.
1240 public function testApiStatusesUpdate()
1242 $_GET['status'] = 'Status content #friendica';
1243 $_GET['in_reply_to_status_id'] = -1;
1252 'tmp_name' => $this->getTempImage(),
1253 'name' => 'spacer.png',
1254 'type' => 'image/png'
1258 $result = api_statuses_update('json');
1259 self::assertStatus($result['status']);
1263 * Test the api_statuses_update() function with an HTML status.
1267 public function testApiStatusesUpdateWithHtml()
1269 $_GET['htmlstatus'] = '<b>Status content</b>';
1271 $result = api_statuses_update('json');
1272 self::assertStatus($result['status']);
1276 * Test the api_statuses_update() function without an authenticated user.
1280 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1282 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1283 $_SESSION['authenticated'] = false;
1284 api_statuses_update('json');
1288 * Test the api_statuses_update() function with a parent status.
1292 public function testApiStatusesUpdateWithParent()
1294 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1298 * Test the api_statuses_update() function with a media_ids parameter.
1302 public function testApiStatusesUpdateWithMediaIds()
1304 $this->markTestIncomplete();
1308 * Test the api_statuses_update() function with the throttle limit reached.
1312 public function testApiStatusesUpdateWithDayThrottleReached()
1314 $this->markTestIncomplete();
1318 * Test the api_media_upload() function.
1319 * @runInSeparateProcess
1320 * @preserveGlobalState disabled
1322 public function testApiMediaUpload()
1324 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1329 * Test the api_media_upload() function without an authenticated user.
1333 public function testApiMediaUploadWithoutAuthenticatedUser()
1335 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1336 $_SESSION['authenticated'] = false;
1341 * Test the api_media_upload() function with an invalid uploaded media.
1345 public function testApiMediaUploadWithMedia()
1347 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1351 'tmp_name' => 'tmp_name'
1358 * Test the api_media_upload() function with an valid uploaded media.
1362 public function testApiMediaUploadWithValidMedia()
1370 'tmp_name' => $this->getTempImage(),
1371 'name' => 'spacer.png',
1372 'type' => 'image/png'
1376 DI::args()->setArgc(2);
1378 $result = api_media_upload();
1379 self::assertEquals('image/png', $result['media']['image']['image_type']);
1380 self::assertEquals(1, $result['media']['image']['w']);
1381 self::assertEquals(1, $result['media']['image']['h']);
1382 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1386 * Test the api_status_show() function.
1388 public function testApiStatusShowWithJson()
1390 $result = api_status_show('json', 1);
1391 self::assertStatus($result['status']);
1395 * Test the api_status_show() function with an XML result.
1397 public function testApiStatusShowWithXml()
1399 $result = api_status_show('xml', 1);
1400 self::assertXml($result, 'statuses');
1404 * Test the api_get_last_status() function
1406 public function testApiGetLastStatus()
1408 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1410 self::assertNotNull($item);
1414 * Test the api_users_show() function.
1418 public function testApiUsersShow()
1420 $result = api_users_show('json');
1421 // We can't use assertSelfUser() here because the user object is missing some properties.
1422 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1423 self::assertEquals('DFRN', $result['user']['location']);
1424 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1425 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1426 self::assertEquals('dfrn', $result['user']['network']);
1427 self::assertTrue($result['user']['verified']);
1431 * Test the api_users_show() function with an XML result.
1435 public function testApiUsersShowWithXml()
1437 $result = api_users_show('xml');
1438 self::assertXml($result, 'statuses');
1442 * Test the api_users_search() function.
1446 public function testApiUsersSearch()
1448 $_GET['q'] = 'othercontact';
1449 $result = api_users_search('json');
1450 self::assertOtherUser($result['users'][0]);
1454 * Test the api_users_search() function with an XML result.
1458 public function testApiUsersSearchWithXml()
1460 $_GET['q'] = 'othercontact';
1461 $result = api_users_search('xml');
1462 self::assertXml($result, 'users');
1466 * Test the api_users_search() function without a GET q parameter.
1470 public function testApiUsersSearchWithoutQuery()
1472 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1473 api_users_search('json');
1477 * Test the api_users_lookup() function.
1481 public function testApiUsersLookup()
1483 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1484 api_users_lookup('json');
1488 * Test the api_users_lookup() function with an user ID.
1492 public function testApiUsersLookupWithUserId()
1494 $_REQUEST['user_id'] = $this->otherUser['id'];
1495 $result = api_users_lookup('json');
1496 self::assertOtherUser($result['users'][0]);
1500 * Test the api_search() function.
1504 public function testApiSearch()
1506 $_REQUEST['q'] = 'reply';
1507 $_REQUEST['max_id'] = 10;
1508 $result = api_search('json');
1509 foreach ($result['status'] as $status) {
1510 self::assertStatus($status);
1511 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1516 * Test the api_search() function a count parameter.
1520 public function testApiSearchWithCount()
1522 $_REQUEST['q'] = 'reply';
1523 $_REQUEST['count'] = 20;
1524 $result = api_search('json');
1525 foreach ($result['status'] as $status) {
1526 self::assertStatus($status);
1527 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1532 * Test the api_search() function with an rpp parameter.
1536 public function testApiSearchWithRpp()
1538 $_REQUEST['q'] = 'reply';
1539 $_REQUEST['rpp'] = 20;
1540 $result = api_search('json');
1541 foreach ($result['status'] as $status) {
1542 self::assertStatus($status);
1543 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1548 * Test the api_search() function with an q parameter contains hashtag.
1549 * @doesNotPerformAssertions
1551 public function testApiSearchWithHashtag()
1553 $_REQUEST['q'] = '%23friendica';
1554 $result = api_search('json');
1555 foreach ($result['status'] as $status) {
1556 self::assertStatus($status);
1557 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1562 * Test the api_search() function with an exclude_replies parameter.
1563 * @doesNotPerformAssertions
1565 public function testApiSearchWithExcludeReplies()
1567 $_REQUEST['max_id'] = 10;
1568 $_REQUEST['exclude_replies'] = true;
1569 $_REQUEST['q'] = 'friendica';
1570 $result = api_search('json');
1571 foreach ($result['status'] as $status) {
1572 self::assertStatus($status);
1577 * Test the api_search() function without an authenticated user.
1581 public function testApiSearchWithUnallowedUser()
1583 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1584 $_SESSION['allow_api'] = false;
1585 $_GET['screen_name'] = $this->selfUser['nick'];
1590 * Test the api_search() function without any GET query parameter.
1594 public function testApiSearchWithoutQuery()
1596 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1601 * Test the api_statuses_home_timeline() function.
1605 public function testApiStatusesHomeTimeline()
1607 $_REQUEST['max_id'] = 10;
1608 $_REQUEST['exclude_replies'] = true;
1609 $_REQUEST['conversation_id'] = 1;
1610 $result = api_statuses_home_timeline('json');
1611 self::assertNotEmpty($result['status']);
1612 foreach ($result['status'] as $status) {
1613 self::assertStatus($status);
1618 * Test the api_statuses_home_timeline() function with a negative page parameter.
1622 public function testApiStatusesHomeTimelineWithNegativePage()
1624 $_REQUEST['page'] = -2;
1625 $result = api_statuses_home_timeline('json');
1626 self::assertNotEmpty($result['status']);
1627 foreach ($result['status'] as $status) {
1628 self::assertStatus($status);
1633 * Test the api_statuses_home_timeline() with an unallowed user.
1637 public function testApiStatusesHomeTimelineWithUnallowedUser()
1639 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1640 $_SESSION['allow_api'] = false;
1641 $_GET['screen_name'] = $this->selfUser['nick'];
1642 api_statuses_home_timeline('json');
1646 * Test the api_statuses_home_timeline() function with an RSS result.
1650 public function testApiStatusesHomeTimelineWithRss()
1652 $result = api_statuses_home_timeline('rss');
1653 self::assertXml($result, 'statuses');
1657 * Test the api_statuses_public_timeline() function.
1661 public function testApiStatusesPublicTimeline()
1663 $_REQUEST['max_id'] = 10;
1664 $_REQUEST['conversation_id'] = 1;
1665 $result = api_statuses_public_timeline('json');
1666 self::assertNotEmpty($result['status']);
1667 foreach ($result['status'] as $status) {
1668 self::assertStatus($status);
1673 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1677 public function testApiStatusesPublicTimelineWithExcludeReplies()
1679 $_REQUEST['max_id'] = 10;
1680 $_REQUEST['exclude_replies'] = true;
1681 $result = api_statuses_public_timeline('json');
1682 self::assertNotEmpty($result['status']);
1683 foreach ($result['status'] as $status) {
1684 self::assertStatus($status);
1689 * Test the api_statuses_public_timeline() function with a negative page parameter.
1693 public function testApiStatusesPublicTimelineWithNegativePage()
1695 $_REQUEST['page'] = -2;
1696 $result = api_statuses_public_timeline('json');
1697 self::assertNotEmpty($result['status']);
1698 foreach ($result['status'] as $status) {
1699 self::assertStatus($status);
1704 * Test the api_statuses_public_timeline() function with an unallowed user.
1708 public function testApiStatusesPublicTimelineWithUnallowedUser()
1710 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1711 $_SESSION['allow_api'] = false;
1712 $_GET['screen_name'] = $this->selfUser['nick'];
1713 api_statuses_public_timeline('json');
1717 * Test the api_statuses_public_timeline() function with an RSS result.
1721 public function testApiStatusesPublicTimelineWithRss()
1723 $result = api_statuses_public_timeline('rss');
1724 self::assertXml($result, 'statuses');
1728 * Test the api_statuses_networkpublic_timeline() function.
1732 public function testApiStatusesNetworkpublicTimeline()
1734 $_REQUEST['max_id'] = 10;
1735 $result = api_statuses_networkpublic_timeline('json');
1736 self::assertNotEmpty($result['status']);
1737 foreach ($result['status'] as $status) {
1738 self::assertStatus($status);
1743 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1747 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1749 $_REQUEST['page'] = -2;
1750 $result = api_statuses_networkpublic_timeline('json');
1751 self::assertNotEmpty($result['status']);
1752 foreach ($result['status'] as $status) {
1753 self::assertStatus($status);
1758 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1762 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1764 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1765 $_SESSION['allow_api'] = false;
1766 $_GET['screen_name'] = $this->selfUser['nick'];
1767 api_statuses_networkpublic_timeline('json');
1771 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1775 public function testApiStatusesNetworkpublicTimelineWithRss()
1777 $result = api_statuses_networkpublic_timeline('rss');
1778 self::assertXml($result, 'statuses');
1782 * Test the api_statuses_show() function.
1786 public function testApiStatusesShow()
1788 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1789 api_statuses_show('json');
1793 * Test the api_statuses_show() function with an ID.
1797 public function testApiStatusesShowWithId()
1799 DI::args()->setArgv(['', '', '', 1]);
1800 $result = api_statuses_show('json');
1801 self::assertStatus($result['status']);
1805 * Test the api_statuses_show() function with the conversation parameter.
1809 public function testApiStatusesShowWithConversation()
1811 DI::args()->setArgv(['', '', '', 1]);
1812 $_REQUEST['conversation'] = 1;
1813 $result = api_statuses_show('json');
1814 self::assertNotEmpty($result['status']);
1815 foreach ($result['status'] as $status) {
1816 self::assertStatus($status);
1821 * Test the api_statuses_show() function with an unallowed user.
1825 public function testApiStatusesShowWithUnallowedUser()
1827 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1828 $_SESSION['allow_api'] = false;
1829 $_GET['screen_name'] = $this->selfUser['nick'];
1830 api_statuses_show('json');
1834 * Test the api_conversation_show() function.
1838 public function testApiConversationShow()
1840 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1841 api_conversation_show('json');
1845 * Test the api_conversation_show() function with an ID.
1849 public function testApiConversationShowWithId()
1851 DI::args()->setArgv(['', '', '', 1]);
1852 $_REQUEST['max_id'] = 10;
1853 $_REQUEST['page'] = -2;
1854 $result = api_conversation_show('json');
1855 self::assertNotEmpty($result['status']);
1856 foreach ($result['status'] as $status) {
1857 self::assertStatus($status);
1862 * Test the api_conversation_show() function with an unallowed user.
1866 public function testApiConversationShowWithUnallowedUser()
1868 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1869 $_SESSION['allow_api'] = false;
1870 $_GET['screen_name'] = $this->selfUser['nick'];
1871 api_conversation_show('json');
1875 * Test the api_statuses_repeat() function.
1879 public function testApiStatusesRepeat()
1881 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1882 api_statuses_repeat('json');
1886 * Test the api_statuses_repeat() function without an authenticated user.
1890 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1892 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1893 $_SESSION['authenticated'] = false;
1894 api_statuses_repeat('json');
1898 * Test the api_statuses_repeat() function with an ID.
1902 public function testApiStatusesRepeatWithId()
1904 DI::args()->setArgv(['', '', '', 1]);
1905 $result = api_statuses_repeat('json');
1906 self::assertStatus($result['status']);
1908 // Also test with a shared status
1909 DI::args()->setArgv(['', '', '', 5]);
1910 $result = api_statuses_repeat('json');
1911 self::assertStatus($result['status']);
1915 * Test the api_statuses_destroy() function.
1919 public function testApiStatusesDestroy()
1921 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1922 api_statuses_destroy('json');
1926 * Test the api_statuses_destroy() function without an authenticated user.
1930 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1932 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1933 $_SESSION['authenticated'] = false;
1934 api_statuses_destroy('json');
1938 * Test the api_statuses_destroy() function with an ID.
1942 public function testApiStatusesDestroyWithId()
1944 DI::args()->setArgv(['', '', '', 1]);
1945 $result = api_statuses_destroy('json');
1946 self::assertStatus($result['status']);
1950 * Test the api_statuses_mentions() function.
1954 public function testApiStatusesMentions()
1956 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1957 $_REQUEST['max_id'] = 10;
1958 $result = api_statuses_mentions('json');
1959 self::assertEmpty($result['status']);
1960 // We should test with mentions in the database.
1964 * Test the api_statuses_mentions() function with a negative page parameter.
1968 public function testApiStatusesMentionsWithNegativePage()
1970 $_REQUEST['page'] = -2;
1971 $result = api_statuses_mentions('json');
1972 self::assertEmpty($result['status']);
1976 * Test the api_statuses_mentions() function with an unallowed user.
1980 public function testApiStatusesMentionsWithUnallowedUser()
1982 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1983 $_SESSION['allow_api'] = false;
1984 $_GET['screen_name'] = $this->selfUser['nick'];
1985 api_statuses_mentions('json');
1989 * Test the api_statuses_mentions() function with an RSS result.
1993 public function testApiStatusesMentionsWithRss()
1995 $result = api_statuses_mentions('rss');
1996 self::assertXml($result, 'statuses');
2000 * Test the api_statuses_user_timeline() function.
2004 public function testApiStatusesUserTimeline()
2006 $_REQUEST['max_id'] = 10;
2007 $_REQUEST['exclude_replies'] = true;
2008 $_REQUEST['conversation_id'] = 1;
2009 $result = api_statuses_user_timeline('json');
2010 self::assertNotEmpty($result['status']);
2011 foreach ($result['status'] as $status) {
2012 self::assertStatus($status);
2017 * Test the api_statuses_user_timeline() function with a negative page parameter.
2021 public function testApiStatusesUserTimelineWithNegativePage()
2023 $_REQUEST['page'] = -2;
2024 $result = api_statuses_user_timeline('json');
2025 self::assertNotEmpty($result['status']);
2026 foreach ($result['status'] as $status) {
2027 self::assertStatus($status);
2032 * Test the api_statuses_user_timeline() function with an RSS result.
2036 public function testApiStatusesUserTimelineWithRss()
2038 $result = api_statuses_user_timeline('rss');
2039 self::assertXml($result, 'statuses');
2043 * Test the api_statuses_user_timeline() function with an unallowed user.
2047 public function testApiStatusesUserTimelineWithUnallowedUser()
2049 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2050 $_SESSION['allow_api'] = false;
2051 $_GET['screen_name'] = $this->selfUser['nick'];
2052 api_statuses_user_timeline('json');
2056 * Test the api_favorites_create_destroy() function.
2060 public function testApiFavoritesCreateDestroy()
2062 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2063 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2064 api_favorites_create_destroy('json');
2068 * Test the api_favorites_create_destroy() function with an invalid ID.
2072 public function testApiFavoritesCreateDestroyWithInvalidId()
2074 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2075 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2076 api_favorites_create_destroy('json');
2080 * Test the api_favorites_create_destroy() function with an invalid action.
2084 public function testApiFavoritesCreateDestroyWithInvalidAction()
2086 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2087 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2088 $_REQUEST['id'] = 1;
2089 api_favorites_create_destroy('json');
2093 * Test the api_favorites_create_destroy() function with the create action.
2097 public function testApiFavoritesCreateDestroyWithCreateAction()
2099 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2100 $_REQUEST['id'] = 3;
2101 $result = api_favorites_create_destroy('json');
2102 self::assertStatus($result['status']);
2106 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2110 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2112 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2113 $_REQUEST['id'] = 3;
2114 $result = api_favorites_create_destroy('rss');
2115 self::assertXml($result, 'status');
2119 * Test the api_favorites_create_destroy() function with the destroy action.
2123 public function testApiFavoritesCreateDestroyWithDestroyAction()
2125 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2126 $_REQUEST['id'] = 3;
2127 $result = api_favorites_create_destroy('json');
2128 self::assertStatus($result['status']);
2132 * Test the api_favorites_create_destroy() function without an authenticated user.
2136 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2138 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2139 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2140 $_SESSION['authenticated'] = false;
2141 api_favorites_create_destroy('json');
2145 * Test the api_favorites() function.
2149 public function testApiFavorites()
2151 $_REQUEST['page'] = -1;
2152 $_REQUEST['max_id'] = 10;
2153 $result = api_favorites('json');
2154 foreach ($result['status'] as $status) {
2155 self::assertStatus($status);
2160 * Test the api_favorites() function with an RSS result.
2164 public function testApiFavoritesWithRss()
2166 $result = api_favorites('rss');
2167 self::assertXml($result, 'statuses');
2171 * Test the api_favorites() function with an unallowed user.
2175 public function testApiFavoritesWithUnallowedUser()
2177 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2178 $_SESSION['allow_api'] = false;
2179 $_GET['screen_name'] = $this->selfUser['nick'];
2180 api_favorites('json');
2184 * Test the api_format_messages() function.
2188 public function testApiFormatMessages()
2190 $result = api_format_messages(
2191 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2192 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2193 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2195 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2196 self::assertEquals(1, $result['id']);
2197 self::assertEquals(2, $result['recipient_id']);
2198 self::assertEquals(3, $result['sender_id']);
2199 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2200 self::assertEquals('sender_name', $result['sender_screen_name']);
2204 * Test the api_format_messages() function with HTML.
2208 public function testApiFormatMessagesWithHtmlText()
2210 $_GET['getText'] = 'html';
2211 $result = api_format_messages(
2212 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2213 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2214 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2216 self::assertEquals('item_title', $result['title']);
2217 self::assertEquals('<strong>item_body</strong>', $result['text']);
2221 * Test the api_format_messages() function with plain text.
2225 public function testApiFormatMessagesWithPlainText()
2227 $_GET['getText'] = 'plain';
2228 $result = api_format_messages(
2229 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2230 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2231 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2233 self::assertEquals('item_title', $result['title']);
2234 self::assertEquals('item_body', $result['text']);
2238 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2242 public function testApiFormatMessagesWithoutUserObjects()
2244 $_GET['getUserObjects'] = 'false';
2245 $result = api_format_messages(
2246 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2247 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2248 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2250 self::assertTrue(!isset($result['sender']));
2251 self::assertTrue(!isset($result['recipient']));
2255 * Test the api_convert_item() function.
2259 public function testApiConvertItem()
2261 $result = api_convert_item(
2263 'network' => 'feed',
2264 'title' => 'item_title',
2266 // We need a long string to test that it is correctly cut
2267 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2268 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2269 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2270 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2271 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2272 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2273 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2274 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2275 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2276 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2277 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2278 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2279 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2280 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2281 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2282 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2283 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2284 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2285 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2286 'repellat officia illum quos impedit quam iste esse unde qui ' .
2287 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2288 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2289 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2290 'plink' => 'item_plink'
2293 self::assertStringStartsWith('item_title', $result['text']);
2294 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2298 * Test the api_convert_item() function with an empty item body.
2302 public function testApiConvertItemWithoutBody()
2304 $result = api_convert_item(
2306 'network' => 'feed',
2307 'title' => 'item_title',
2310 'plink' => 'item_plink'
2313 self::assertEquals("item_title", $result['text']);
2314 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2318 * Test the api_convert_item() function with the title in the body.
2322 public function testApiConvertItemWithTitleInBody()
2324 $result = api_convert_item(
2326 'title' => 'item_title',
2327 'body' => 'item_title item_body',
2331 self::assertEquals('item_title item_body', $result['text']);
2332 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2336 * Test the api_get_attachments() function.
2340 public function testApiGetAttachments()
2343 self::assertEmpty(api_get_attachments($body, 0));
2347 * Test the api_get_attachments() function with an img tag.
2351 public function testApiGetAttachmentsWithImage()
2353 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2354 self::assertIsArray(api_get_attachments($body, 0));
2358 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2362 public function testApiGetAttachmentsWithImageAndAndStatus()
2364 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2365 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2366 self::assertIsArray(api_get_attachments($body, 0));
2370 * Test the api_get_entitities() function.
2374 public function testApiGetEntitities()
2377 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2381 * Test the api_get_entitities() function with the include_entities parameter.
2385 public function testApiGetEntititiesWithIncludeEntities()
2387 $_REQUEST['include_entities'] = 'true';
2389 $result = api_get_entitities($text, 'bbcode', 0);
2390 self::assertIsArray($result['hashtags']);
2391 self::assertIsArray($result['symbols']);
2392 self::assertIsArray($result['urls']);
2393 self::assertIsArray($result['user_mentions']);
2397 * Test the api_format_items_embeded_images() function.
2401 public function testApiFormatItemsEmbededImages()
2404 'text ' . DI::baseUrl() . '/display/item_guid',
2405 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2410 * Test the api_contactlink_to_array() function.
2414 public function testApiContactlinkToArray()
2421 api_contactlink_to_array('text')
2426 * Test the api_contactlink_to_array() function with an URL.
2430 public function testApiContactlinkToArrayWithUrl()
2434 'name' => ['link_text'],
2437 api_contactlink_to_array('text <a href="url">link_text</a>')
2442 * Test the api_format_items_activities() function.
2446 public function testApiFormatItemsActivities()
2448 $item = ['uid' => 0, 'uri' => ''];
2449 $result = api_format_items_activities($item);
2450 self::assertArrayHasKey('like', $result);
2451 self::assertArrayHasKey('dislike', $result);
2452 self::assertArrayHasKey('attendyes', $result);
2453 self::assertArrayHasKey('attendno', $result);
2454 self::assertArrayHasKey('attendmaybe', $result);
2458 * Test the api_format_items_activities() function with an XML result.
2462 public function testApiFormatItemsActivitiesWithXml()
2464 $item = ['uid' => 0, 'uri' => ''];
2465 $result = api_format_items_activities($item, 'xml');
2466 self::assertArrayHasKey('friendica:like', $result);
2467 self::assertArrayHasKey('friendica:dislike', $result);
2468 self::assertArrayHasKey('friendica:attendyes', $result);
2469 self::assertArrayHasKey('friendica:attendno', $result);
2470 self::assertArrayHasKey('friendica:attendmaybe', $result);
2474 * Test the api_format_items() function.
2475 * @doesNotPerformAssertions
2477 public function testApiFormatItems()
2481 'item_network' => 'item_network',
2487 'author-network' => Protocol::DFRN,
2488 'author-link' => 'http://localhost/profile/othercontact',
2492 $result = api_format_items($items, ['id' => 0], true);
2493 foreach ($result as $status) {
2494 self::assertStatus($status);
2499 * Test the api_format_items() function with an XML result.
2500 * @doesNotPerformAssertions
2502 public function testApiFormatItemsWithXml()
2510 'author-network' => Protocol::DFRN,
2511 'author-link' => 'http://localhost/profile/othercontact',
2515 $result = api_format_items($items, ['id' => 0], true, 'xml');
2516 foreach ($result as $status) {
2517 self::assertStatus($status);
2522 * Test the api_format_items() function.
2526 public function testApiAccountRateLimitStatus()
2528 // @todo How to test the new API?
2529 // $result = api_account_rate_limit_status('json');
2530 // self::assertEquals(150, $result['hash']['remaining_hits']);
2531 // self::assertEquals(150, $result['hash']['hourly_limit']);
2532 // self::assertIsInt($result['hash']['reset_time_in_seconds']);
2536 * Test the api_format_items() function with an XML result.
2540 public function testApiAccountRateLimitStatusWithXml()
2542 // @todo How to test the new API?
2543 // $result = api_account_rate_limit_status('xml');
2544 // self::assertXml($result, 'hash');
2548 * Test the api_help_test() function.
2552 public function testApiHelpTest()
2554 // @todo How to test the new API?
2555 // $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'json']);
2556 // self::assertEquals(['ok' => 'ok'], $result);
2560 * Test the api_help_test() function with an XML result.
2564 public function testApiHelpTestWithXml()
2566 // @todo How to test the new API?
2567 // $result = api_help_test('xml');
2568 // self::assertXml($result, 'ok');
2572 * Test the api_lists_list() function.
2576 public function testApiListsList()
2578 $result = api_lists_list('json');
2579 self::assertEquals(['lists_list' => []], $result);
2583 * Test the api_lists_ownerships() function.
2587 public function testApiListsOwnerships()
2589 $result = api_lists_ownerships('json');
2590 foreach ($result['lists']['lists'] as $list) {
2591 self::assertList($list);
2596 * Test the api_lists_ownerships() function without an authenticated user.
2600 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2602 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2603 $_SESSION['authenticated'] = false;
2604 api_lists_ownerships('json');
2608 * Test the api_lists_statuses() function.
2612 public function testApiListsStatuses()
2614 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2615 api_lists_statuses('json');
2619 * Test the api_lists_statuses() function with a list ID.
2620 * @doesNotPerformAssertions
2622 public function testApiListsStatusesWithListId()
2624 $_REQUEST['list_id'] = 1;
2625 $_REQUEST['page'] = -1;
2626 $_REQUEST['max_id'] = 10;
2627 $result = api_lists_statuses('json');
2628 foreach ($result['status'] as $status) {
2629 self::assertStatus($status);
2634 * Test the api_lists_statuses() function with a list ID and a RSS result.
2638 public function testApiListsStatusesWithListIdAndRss()
2640 $_REQUEST['list_id'] = 1;
2641 $result = api_lists_statuses('rss');
2642 self::assertXml($result, 'statuses');
2646 * Test the api_lists_statuses() function with an unallowed user.
2650 public function testApiListsStatusesWithUnallowedUser()
2652 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2653 $_SESSION['allow_api'] = false;
2654 $_GET['screen_name'] = $this->selfUser['nick'];
2655 api_lists_statuses('json');
2659 * Test the api_statuses_f() function.
2663 public function testApiStatusesFWithFriends()
2666 $result = api_statuses_f('friends');
2667 self::assertArrayHasKey('user', $result);
2671 * Test the api_statuses_f() function.
2675 public function testApiStatusesFWithFollowers()
2677 $result = api_statuses_f('followers');
2678 self::assertArrayHasKey('user', $result);
2682 * Test the api_statuses_f() function.
2686 public function testApiStatusesFWithBlocks()
2688 $result = api_statuses_f('blocks');
2689 self::assertArrayHasKey('user', $result);
2693 * Test the api_statuses_f() function.
2697 public function testApiStatusesFWithIncoming()
2699 $result = api_statuses_f('incoming');
2700 self::assertArrayHasKey('user', $result);
2704 * Test the api_statuses_f() function an undefined cursor GET variable.
2708 public function testApiStatusesFWithUndefinedCursor()
2710 $_GET['cursor'] = 'undefined';
2711 self::assertFalse(api_statuses_f('friends'));
2715 * Test the api_statuses_friends() function.
2719 public function testApiStatusesFriends()
2721 $result = api_statuses_friends('json');
2722 self::assertArrayHasKey('user', $result);
2726 * Test the api_statuses_friends() function an undefined cursor GET variable.
2730 public function testApiStatusesFriendsWithUndefinedCursor()
2732 $_GET['cursor'] = 'undefined';
2733 self::assertFalse(api_statuses_friends('json'));
2737 * Test the api_statuses_followers() function.
2741 public function testApiStatusesFollowers()
2743 $result = api_statuses_followers('json');
2744 self::assertArrayHasKey('user', $result);
2748 * Test the api_statuses_followers() function an undefined cursor GET variable.
2752 public function testApiStatusesFollowersWithUndefinedCursor()
2754 $_GET['cursor'] = 'undefined';
2755 self::assertFalse(api_statuses_followers('json'));
2759 * Test the api_blocks_list() function.
2763 public function testApiBlocksList()
2765 $result = api_blocks_list('json');
2766 self::assertArrayHasKey('user', $result);
2770 * Test the api_blocks_list() function an undefined cursor GET variable.
2774 public function testApiBlocksListWithUndefinedCursor()
2776 $_GET['cursor'] = 'undefined';
2777 self::assertFalse(api_blocks_list('json'));
2781 * Test the api_friendships_incoming() function.
2785 public function testApiFriendshipsIncoming()
2787 $result = api_friendships_incoming('json');
2788 self::assertArrayHasKey('id', $result);
2792 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2796 public function testApiFriendshipsIncomingWithUndefinedCursor()
2798 $_GET['cursor'] = 'undefined';
2799 self::assertFalse(api_friendships_incoming('json'));
2803 * Test the api_statusnet_config() function.
2807 public function testApiStatusnetConfig()
2809 $result = api_statusnet_config('json');
2810 self::assertEquals('localhost', $result['config']['site']['server']);
2811 self::assertEquals('default', $result['config']['site']['theme']);
2812 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2813 self::assertTrue($result['config']['site']['fancy']);
2814 self::assertEquals('en', $result['config']['site']['language']);
2815 self::assertEquals('UTC', $result['config']['site']['timezone']);
2816 self::assertEquals(200000, $result['config']['site']['textlimit']);
2817 self::assertEquals('false', $result['config']['site']['private']);
2818 self::assertEquals('false', $result['config']['site']['ssl']);
2819 self::assertEquals(30, $result['config']['site']['shorturllength']);
2823 * Test the api_statusnet_version() function.
2827 public function testApiStatusnetVersion()
2829 // @todo How to test the new API?
2830 // $result = api_statusnet_version('json');
2831 // self::assertEquals('0.9.7', $result['version']);
2835 * Test the api_direct_messages_new() function.
2839 public function testApiDirectMessagesNew()
2841 $result = api_direct_messages_new('json');
2842 self::assertNull($result);
2846 * Test the api_direct_messages_new() function without an authenticated user.
2850 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2852 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2853 $_SESSION['authenticated'] = false;
2854 api_direct_messages_new('json');
2858 * Test the api_direct_messages_new() function with an user ID.
2862 public function testApiDirectMessagesNewWithUserId()
2864 $_POST['text'] = 'message_text';
2865 $_POST['user_id'] = $this->otherUser['id'];
2866 $result = api_direct_messages_new('json');
2867 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2871 * Test the api_direct_messages_new() function with a screen name.
2875 public function testApiDirectMessagesNewWithScreenName()
2877 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2878 $_POST['text'] = 'message_text';
2879 $_POST['screen_name'] = $this->friendUser['nick'];
2880 $result = api_direct_messages_new('json');
2881 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2882 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2883 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2887 * Test the api_direct_messages_new() function with a title.
2891 public function testApiDirectMessagesNewWithTitle()
2893 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2894 $_POST['text'] = 'message_text';
2895 $_POST['screen_name'] = $this->friendUser['nick'];
2896 $_REQUEST['title'] = 'message_title';
2897 $result = api_direct_messages_new('json');
2898 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2899 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2900 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2901 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2905 * Test the api_direct_messages_new() function with an RSS result.
2909 public function testApiDirectMessagesNewWithRss()
2911 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2912 $_POST['text'] = 'message_text';
2913 $_POST['screen_name'] = $this->friendUser['nick'];
2914 $result = api_direct_messages_new('rss');
2915 self::assertXml($result, 'direct-messages');
2919 * Test the api_direct_messages_destroy() function.
2923 public function testApiDirectMessagesDestroy()
2925 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2926 api_direct_messages_destroy('json');
2930 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2934 public function testApiDirectMessagesDestroyWithVerbose()
2936 $_GET['friendica_verbose'] = 'true';
2937 $result = api_direct_messages_destroy('json');
2941 'result' => 'error',
2942 'message' => 'message id or parenturi not specified'
2950 * Test the api_direct_messages_destroy() function without an authenticated user.
2954 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2956 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2957 $_SESSION['authenticated'] = false;
2958 api_direct_messages_destroy('json');
2962 * Test the api_direct_messages_destroy() function with a non-zero ID.
2966 public function testApiDirectMessagesDestroyWithId()
2968 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2969 $_REQUEST['id'] = 1;
2970 api_direct_messages_destroy('json');
2974 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2978 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2980 $_REQUEST['id'] = 1;
2981 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2982 $_GET['friendica_verbose'] = 'true';
2983 $result = api_direct_messages_destroy('json');
2987 'result' => 'error',
2988 'message' => 'message id not in database'
2996 * Test the api_direct_messages_destroy() function with a non-zero ID.
3000 public function testApiDirectMessagesDestroyWithCorrectId()
3002 $this->markTestIncomplete('We need to add a dataset for this.');
3006 * Test the api_direct_messages_box() function.
3010 public function testApiDirectMessagesBoxWithSentbox()
3012 $_REQUEST['page'] = -1;
3013 $_REQUEST['max_id'] = 10;
3014 $result = api_direct_messages_box('json', 'sentbox', 'false');
3015 self::assertArrayHasKey('direct_message', $result);
3019 * Test the api_direct_messages_box() function.
3023 public function testApiDirectMessagesBoxWithConversation()
3025 $result = api_direct_messages_box('json', 'conversation', 'false');
3026 self::assertArrayHasKey('direct_message', $result);
3030 * Test the api_direct_messages_box() function.
3034 public function testApiDirectMessagesBoxWithAll()
3036 $result = api_direct_messages_box('json', 'all', 'false');
3037 self::assertArrayHasKey('direct_message', $result);
3041 * Test the api_direct_messages_box() function.
3045 public function testApiDirectMessagesBoxWithInbox()
3047 $result = api_direct_messages_box('json', 'inbox', 'false');
3048 self::assertArrayHasKey('direct_message', $result);
3052 * Test the api_direct_messages_box() function.
3056 public function testApiDirectMessagesBoxWithVerbose()
3058 $result = api_direct_messages_box('json', 'sentbox', 'true');
3062 'result' => 'error',
3063 'message' => 'no mails available'
3071 * Test the api_direct_messages_box() function with a RSS result.
3075 public function testApiDirectMessagesBoxWithRss()
3077 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3078 self::assertXml($result, 'direct-messages');
3082 * Test the api_direct_messages_box() function without an authenticated user.
3086 public function testApiDirectMessagesBoxWithUnallowedUser()
3088 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3089 $_SESSION['allow_api'] = false;
3090 $_GET['screen_name'] = $this->selfUser['nick'];
3091 api_direct_messages_box('json', 'sentbox', 'false');
3095 * Test the api_direct_messages_sentbox() function.
3099 public function testApiDirectMessagesSentbox()
3101 $result = api_direct_messages_sentbox('json');
3102 self::assertArrayHasKey('direct_message', $result);
3106 * Test the api_direct_messages_inbox() function.
3110 public function testApiDirectMessagesInbox()
3112 $result = api_direct_messages_inbox('json');
3113 self::assertArrayHasKey('direct_message', $result);
3117 * Test the api_direct_messages_all() function.
3121 public function testApiDirectMessagesAll()
3123 $result = api_direct_messages_all('json');
3124 self::assertArrayHasKey('direct_message', $result);
3128 * Test the api_direct_messages_conversation() function.
3132 public function testApiDirectMessagesConversation()
3134 $result = api_direct_messages_conversation('json');
3135 self::assertArrayHasKey('direct_message', $result);
3139 * Test the api_oauth_request_token() function.
3143 public function testApiOauthRequestToken()
3145 $this->markTestIncomplete('exit() kills phpunit as well');
3149 * Test the api_oauth_access_token() function.
3153 public function testApiOauthAccessToken()
3155 $this->markTestIncomplete('exit() kills phpunit as well');
3159 * Test the api_fr_photoalbum_delete() function.
3163 public function testApiFrPhotoalbumDelete()
3165 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3166 api_fr_photoalbum_delete('json');
3170 * Test the api_fr_photoalbum_delete() function with an album name.
3174 public function testApiFrPhotoalbumDeleteWithAlbum()
3176 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3177 $_REQUEST['album'] = 'album_name';
3178 api_fr_photoalbum_delete('json');
3182 * Test the api_fr_photoalbum_delete() function with an album name.
3186 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3188 $this->markTestIncomplete('We need to add a dataset for this.');
3192 * Test the api_fr_photoalbum_delete() function.
3196 public function testApiFrPhotoalbumUpdate()
3198 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3199 api_fr_photoalbum_update('json');
3203 * Test the api_fr_photoalbum_delete() function with an album name.
3207 public function testApiFrPhotoalbumUpdateWithAlbum()
3209 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3210 $_REQUEST['album'] = 'album_name';
3211 api_fr_photoalbum_update('json');
3215 * Test the api_fr_photoalbum_delete() function with an album name.
3219 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3221 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3222 $_REQUEST['album'] = 'album_name';
3223 $_REQUEST['album_new'] = 'album_name';
3224 api_fr_photoalbum_update('json');
3228 * Test the api_fr_photoalbum_update() function without an authenticated user.
3232 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3234 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3235 $_SESSION['authenticated'] = false;
3236 api_fr_photoalbum_update('json');
3240 * Test the api_fr_photoalbum_delete() function with an album name.
3244 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3246 $this->markTestIncomplete('We need to add a dataset for this.');
3250 * Test the api_fr_photos_list() function.
3254 public function testApiFrPhotosList()
3256 $result = api_fr_photos_list('json');
3257 self::assertArrayHasKey('photo', $result);
3261 * Test the api_fr_photos_list() function without an authenticated user.
3265 public function testApiFrPhotosListWithoutAuthenticatedUser()
3267 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3268 $_SESSION['authenticated'] = false;
3269 api_fr_photos_list('json');
3273 * Test the api_fr_photo_create_update() function.
3275 public function testApiFrPhotoCreateUpdate()
3277 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3278 api_fr_photo_create_update('json');
3282 * Test the api_fr_photo_create_update() function without an authenticated user.
3286 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3288 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3289 $_SESSION['authenticated'] = false;
3290 api_fr_photo_create_update('json');
3294 * Test the api_fr_photo_create_update() function with an album name.
3298 public function testApiFrPhotoCreateUpdateWithAlbum()
3300 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3301 $_REQUEST['album'] = 'album_name';
3302 api_fr_photo_create_update('json');
3306 * Test the api_fr_photo_create_update() function with the update mode.
3310 public function testApiFrPhotoCreateUpdateWithUpdate()
3312 $this->markTestIncomplete('We need to create a dataset for this');
3316 * Test the api_fr_photo_create_update() function with an uploaded file.
3320 public function testApiFrPhotoCreateUpdateWithFile()
3322 $this->markTestIncomplete();
3326 * Test the api_fr_photo_delete() function.
3330 public function testApiFrPhotoDelete()
3332 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3333 api_fr_photo_delete('json');
3337 * Test the api_fr_photo_delete() function without an authenticated user.
3341 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3343 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3344 $_SESSION['authenticated'] = false;
3345 api_fr_photo_delete('json');
3349 * Test the api_fr_photo_delete() function with a photo ID.
3353 public function testApiFrPhotoDeleteWithPhotoId()
3355 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3356 $_REQUEST['photo_id'] = 1;
3357 api_fr_photo_delete('json');
3361 * Test the api_fr_photo_delete() function with a correct photo ID.
3365 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3367 $this->markTestIncomplete('We need to create a dataset for this.');
3371 * Test the api_fr_photo_detail() function.
3375 public function testApiFrPhotoDetail()
3377 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3378 api_fr_photo_detail('json');
3382 * Test the api_fr_photo_detail() function without an authenticated user.
3386 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3388 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3389 $_SESSION['authenticated'] = false;
3390 api_fr_photo_detail('json');
3394 * Test the api_fr_photo_detail() function with a photo ID.
3398 public function testApiFrPhotoDetailWithPhotoId()
3400 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3401 $_REQUEST['photo_id'] = 1;
3402 api_fr_photo_detail('json');
3406 * Test the api_fr_photo_detail() function with a correct photo ID.
3410 public function testApiFrPhotoDetailCorrectPhotoId()
3412 $this->markTestIncomplete('We need to create a dataset for this.');
3416 * Test the api_account_update_profile_image() function.
3420 public function testApiAccountUpdateProfileImage()
3422 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3423 api_account_update_profile_image('json');
3427 * Test the api_account_update_profile_image() function without an authenticated user.
3431 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3433 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3434 $_SESSION['authenticated'] = false;
3435 api_account_update_profile_image('json');
3439 * Test the api_account_update_profile_image() function with an uploaded file.
3443 public function testApiAccountUpdateProfileImageWithUpload()
3445 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3446 $this->markTestIncomplete();
3451 * Test the api_account_update_profile() function.
3455 public function testApiAccountUpdateProfile()
3457 $_POST['name'] = 'new_name';
3458 $_POST['description'] = 'new_description';
3459 $result = api_account_update_profile('json');
3460 // We can't use assertSelfUser() here because the user object is missing some properties.
3461 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3462 self::assertEquals('DFRN', $result['user']['location']);
3463 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3464 self::assertEquals('dfrn', $result['user']['network']);
3465 self::assertEquals('new_name', $result['user']['name']);
3466 self::assertEquals('new_description', $result['user']['description']);
3470 * Test the check_acl_input() function.
3474 public function testCheckAclInput()
3476 $result = check_acl_input('<aclstring>');
3477 // Where does this result come from?
3478 self::assertEquals(1, $result);
3482 * Test the check_acl_input() function with an empty ACL string.
3486 public function testCheckAclInputWithEmptyAclString()
3488 $result = check_acl_input(' ');
3489 self::assertFalse($result);
3493 * Test the save_media_to_database() function.
3497 public function testSaveMediaToDatabase()
3499 $this->markTestIncomplete();
3503 * Test the post_photo_item() function.
3507 public function testPostPhotoItem()
3509 $this->markTestIncomplete();
3513 * Test the prepare_photo_data() function.
3517 public function testPreparePhotoData()
3519 $this->markTestIncomplete();
3523 * Test the api_share_as_retweet() function with a valid item.
3527 public function testApiShareAsRetweetWithValidItem()
3529 $this->markTestIncomplete();
3533 * Test the api_in_reply_to() function.
3537 public function testApiInReplyTo()
3539 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3540 self::assertArrayHasKey('status_id', $result);
3541 self::assertArrayHasKey('user_id', $result);
3542 self::assertArrayHasKey('status_id_str', $result);
3543 self::assertArrayHasKey('user_id_str', $result);
3544 self::assertArrayHasKey('screen_name', $result);
3548 * Test the api_in_reply_to() function with a valid item.
3552 public function testApiInReplyToWithValidItem()
3554 $this->markTestIncomplete();
3558 * Test the api_clean_plain_items() function.
3562 public function testApiCleanPlainItems()
3564 $_REQUEST['include_entities'] = 'true';
3565 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3566 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3570 * Test the api_best_nickname() function.
3574 public function testApiBestNickname()
3577 $result = api_best_nickname($contacts);
3578 self::assertNull($result);
3582 * Test the api_best_nickname() function with contacts.
3586 public function testApiBestNicknameWithContacts()
3588 $this->markTestIncomplete();
3592 * Test the api_friendica_group_show() function.
3596 public function testApiFriendicaGroupShow()
3598 $this->markTestIncomplete();
3602 * Test the api_friendica_group_delete() function.
3606 public function testApiFriendicaGroupDelete()
3608 $this->markTestIncomplete();
3612 * Test the api_lists_destroy() function.
3616 public function testApiListsDestroy()
3618 $this->markTestIncomplete();
3622 * Test the group_create() function.
3626 public function testGroupCreate()
3628 $this->markTestIncomplete();
3632 * Test the api_friendica_group_create() function.
3636 public function testApiFriendicaGroupCreate()
3638 $this->markTestIncomplete();
3642 * Test the api_lists_create() function.
3646 public function testApiListsCreate()
3648 $this->markTestIncomplete();
3652 * Test the api_friendica_group_update() function.
3656 public function testApiFriendicaGroupUpdate()
3658 $this->markTestIncomplete();
3662 * Test the api_lists_update() function.
3666 public function testApiListsUpdate()
3668 $this->markTestIncomplete();
3672 * Test the api_friendica_activity() function.
3676 public function testApiFriendicaActivity()
3678 $this->markTestIncomplete();
3682 * Test the api_friendica_notification() function.
3686 public function testApiFriendicaNotification()
3688 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3689 api_friendica_notification('json');
3693 * Test the api_friendica_notification() function without an authenticated user.
3697 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3699 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3700 $_SESSION['authenticated'] = false;
3701 api_friendica_notification('json');
3705 * Test the api_friendica_notification() function with empty result
3709 public function testApiFriendicaNotificationWithEmptyResult()
3711 DI::args()->setArgv(['api', 'friendica', 'notification']);
3712 $_SESSION['uid'] = 41;
3713 $result = api_friendica_notification('json');
3714 self::assertEquals(['note' => false], $result);
3718 * Test the api_friendica_notification() function with an XML result.
3722 public function testApiFriendicaNotificationWithXmlResult()
3724 DI::args()->setArgv(['api', 'friendica', 'notification']);
3725 $result = api_friendica_notification('xml');
3726 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3727 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3730 <?xml version="1.0"?>
3732 <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"/>
3735 self::assertXmlStringEqualsXmlString($assertXml, $result);
3739 * Test the api_friendica_notification() function with an JSON result.
3743 public function testApiFriendicaNotificationWithJsonResult()
3745 DI::args()->setArgv(['api', 'friendica', 'notification']);
3746 $result = json_encode(api_friendica_notification('json'));
3747 self::assertJson($result);
3751 * Test the api_friendica_notification_seen() function.
3755 public function testApiFriendicaNotificationSeen()
3757 $this->markTestIncomplete();
3761 * Test the api_friendica_direct_messages_setseen() function.
3765 public function testApiFriendicaDirectMessagesSetseen()
3767 $this->markTestIncomplete();
3771 * Test the api_friendica_direct_messages_search() function.
3775 public function testApiFriendicaDirectMessagesSearch()
3777 $this->markTestIncomplete();
3781 * Test the api_saved_searches_list() function.
3785 public function testApiSavedSearchesList()
3787 $result = api_saved_searches_list('json');
3788 self::assertEquals(1, $result['terms'][0]['id']);
3789 self::assertEquals(1, $result['terms'][0]['id_str']);
3790 self::assertEquals('Saved search', $result['terms'][0]['name']);
3791 self::assertEquals('Saved search', $result['terms'][0]['query']);