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 $result = api_account_rate_limit_status('json');
2529 self::assertEquals(150, $result['hash']['remaining_hits']);
2530 self::assertEquals(150, $result['hash']['hourly_limit']);
2531 self::assertIsInt($result['hash']['reset_time_in_seconds']);
2535 * Test the api_format_items() function with an XML result.
2539 public function testApiAccountRateLimitStatusWithXml()
2541 $result = api_account_rate_limit_status('xml');
2542 self::assertXml($result, 'hash');
2546 * Test the api_help_test() function.
2550 public function testApiHelpTest()
2552 // @todo How to test the new API?
2553 $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'xml']);
2554 self::assertEquals(['ok' => 'ok'], $result);
2558 * Test the api_help_test() function with an XML result.
2562 public function testApiHelpTestWithXml()
2564 // @todo How to test the new API?
2565 // $result = api_help_test('xml');
2566 // self::assertXml($result, 'ok');
2570 * Test the api_lists_list() function.
2574 public function testApiListsList()
2576 $result = api_lists_list('json');
2577 self::assertEquals(['lists_list' => []], $result);
2581 * Test the api_lists_ownerships() function.
2585 public function testApiListsOwnerships()
2587 $result = api_lists_ownerships('json');
2588 foreach ($result['lists']['lists'] as $list) {
2589 self::assertList($list);
2594 * Test the api_lists_ownerships() function without an authenticated user.
2598 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2600 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2601 $_SESSION['authenticated'] = false;
2602 api_lists_ownerships('json');
2606 * Test the api_lists_statuses() function.
2610 public function testApiListsStatuses()
2612 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2613 api_lists_statuses('json');
2617 * Test the api_lists_statuses() function with a list ID.
2618 * @doesNotPerformAssertions
2620 public function testApiListsStatusesWithListId()
2622 $_REQUEST['list_id'] = 1;
2623 $_REQUEST['page'] = -1;
2624 $_REQUEST['max_id'] = 10;
2625 $result = api_lists_statuses('json');
2626 foreach ($result['status'] as $status) {
2627 self::assertStatus($status);
2632 * Test the api_lists_statuses() function with a list ID and a RSS result.
2636 public function testApiListsStatusesWithListIdAndRss()
2638 $_REQUEST['list_id'] = 1;
2639 $result = api_lists_statuses('rss');
2640 self::assertXml($result, 'statuses');
2644 * Test the api_lists_statuses() function with an unallowed user.
2648 public function testApiListsStatusesWithUnallowedUser()
2650 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2651 $_SESSION['allow_api'] = false;
2652 $_GET['screen_name'] = $this->selfUser['nick'];
2653 api_lists_statuses('json');
2657 * Test the api_statuses_f() function.
2661 public function testApiStatusesFWithFriends()
2664 $result = api_statuses_f('friends');
2665 self::assertArrayHasKey('user', $result);
2669 * Test the api_statuses_f() function.
2673 public function testApiStatusesFWithFollowers()
2675 $result = api_statuses_f('followers');
2676 self::assertArrayHasKey('user', $result);
2680 * Test the api_statuses_f() function.
2684 public function testApiStatusesFWithBlocks()
2686 $result = api_statuses_f('blocks');
2687 self::assertArrayHasKey('user', $result);
2691 * Test the api_statuses_f() function.
2695 public function testApiStatusesFWithIncoming()
2697 $result = api_statuses_f('incoming');
2698 self::assertArrayHasKey('user', $result);
2702 * Test the api_statuses_f() function an undefined cursor GET variable.
2706 public function testApiStatusesFWithUndefinedCursor()
2708 $_GET['cursor'] = 'undefined';
2709 self::assertFalse(api_statuses_f('friends'));
2713 * Test the api_statuses_friends() function.
2717 public function testApiStatusesFriends()
2719 $result = api_statuses_friends('json');
2720 self::assertArrayHasKey('user', $result);
2724 * Test the api_statuses_friends() function an undefined cursor GET variable.
2728 public function testApiStatusesFriendsWithUndefinedCursor()
2730 $_GET['cursor'] = 'undefined';
2731 self::assertFalse(api_statuses_friends('json'));
2735 * Test the api_statuses_followers() function.
2739 public function testApiStatusesFollowers()
2741 $result = api_statuses_followers('json');
2742 self::assertArrayHasKey('user', $result);
2746 * Test the api_statuses_followers() function an undefined cursor GET variable.
2750 public function testApiStatusesFollowersWithUndefinedCursor()
2752 $_GET['cursor'] = 'undefined';
2753 self::assertFalse(api_statuses_followers('json'));
2757 * Test the api_blocks_list() function.
2761 public function testApiBlocksList()
2763 $result = api_blocks_list('json');
2764 self::assertArrayHasKey('user', $result);
2768 * Test the api_blocks_list() function an undefined cursor GET variable.
2772 public function testApiBlocksListWithUndefinedCursor()
2774 $_GET['cursor'] = 'undefined';
2775 self::assertFalse(api_blocks_list('json'));
2779 * Test the api_friendships_incoming() function.
2783 public function testApiFriendshipsIncoming()
2785 $result = api_friendships_incoming('json');
2786 self::assertArrayHasKey('id', $result);
2790 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2794 public function testApiFriendshipsIncomingWithUndefinedCursor()
2796 $_GET['cursor'] = 'undefined';
2797 self::assertFalse(api_friendships_incoming('json'));
2801 * Test the api_statusnet_config() function.
2805 public function testApiStatusnetConfig()
2807 $result = api_statusnet_config('json');
2808 self::assertEquals('localhost', $result['config']['site']['server']);
2809 self::assertEquals('default', $result['config']['site']['theme']);
2810 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2811 self::assertTrue($result['config']['site']['fancy']);
2812 self::assertEquals('en', $result['config']['site']['language']);
2813 self::assertEquals('UTC', $result['config']['site']['timezone']);
2814 self::assertEquals(200000, $result['config']['site']['textlimit']);
2815 self::assertEquals('false', $result['config']['site']['private']);
2816 self::assertEquals('false', $result['config']['site']['ssl']);
2817 self::assertEquals(30, $result['config']['site']['shorturllength']);
2821 * Test the api_statusnet_version() function.
2825 public function testApiStatusnetVersion()
2827 // @todo How to test the new API?
2828 // $result = api_statusnet_version('json');
2829 // self::assertEquals('0.9.7', $result['version']);
2833 * Test the api_direct_messages_new() function.
2837 public function testApiDirectMessagesNew()
2839 $result = api_direct_messages_new('json');
2840 self::assertNull($result);
2844 * Test the api_direct_messages_new() function without an authenticated user.
2848 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2850 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2851 $_SESSION['authenticated'] = false;
2852 api_direct_messages_new('json');
2856 * Test the api_direct_messages_new() function with an user ID.
2860 public function testApiDirectMessagesNewWithUserId()
2862 $_POST['text'] = 'message_text';
2863 $_POST['user_id'] = $this->otherUser['id'];
2864 $result = api_direct_messages_new('json');
2865 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2869 * Test the api_direct_messages_new() function with a screen name.
2873 public function testApiDirectMessagesNewWithScreenName()
2875 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2876 $_POST['text'] = 'message_text';
2877 $_POST['screen_name'] = $this->friendUser['nick'];
2878 $result = api_direct_messages_new('json');
2879 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2880 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2881 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2885 * Test the api_direct_messages_new() function with a title.
2889 public function testApiDirectMessagesNewWithTitle()
2891 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2892 $_POST['text'] = 'message_text';
2893 $_POST['screen_name'] = $this->friendUser['nick'];
2894 $_REQUEST['title'] = 'message_title';
2895 $result = api_direct_messages_new('json');
2896 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2897 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2898 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2899 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2903 * Test the api_direct_messages_new() function with an RSS result.
2907 public function testApiDirectMessagesNewWithRss()
2909 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2910 $_POST['text'] = 'message_text';
2911 $_POST['screen_name'] = $this->friendUser['nick'];
2912 $result = api_direct_messages_new('rss');
2913 self::assertXml($result, 'direct-messages');
2917 * Test the api_direct_messages_destroy() function.
2921 public function testApiDirectMessagesDestroy()
2923 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2924 api_direct_messages_destroy('json');
2928 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2932 public function testApiDirectMessagesDestroyWithVerbose()
2934 $_GET['friendica_verbose'] = 'true';
2935 $result = api_direct_messages_destroy('json');
2939 'result' => 'error',
2940 'message' => 'message id or parenturi not specified'
2948 * Test the api_direct_messages_destroy() function without an authenticated user.
2952 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2954 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2955 $_SESSION['authenticated'] = false;
2956 api_direct_messages_destroy('json');
2960 * Test the api_direct_messages_destroy() function with a non-zero ID.
2964 public function testApiDirectMessagesDestroyWithId()
2966 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2967 $_REQUEST['id'] = 1;
2968 api_direct_messages_destroy('json');
2972 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2976 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2978 $_REQUEST['id'] = 1;
2979 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2980 $_GET['friendica_verbose'] = 'true';
2981 $result = api_direct_messages_destroy('json');
2985 'result' => 'error',
2986 'message' => 'message id not in database'
2994 * Test the api_direct_messages_destroy() function with a non-zero ID.
2998 public function testApiDirectMessagesDestroyWithCorrectId()
3000 $this->markTestIncomplete('We need to add a dataset for this.');
3004 * Test the api_direct_messages_box() function.
3008 public function testApiDirectMessagesBoxWithSentbox()
3010 $_REQUEST['page'] = -1;
3011 $_REQUEST['max_id'] = 10;
3012 $result = api_direct_messages_box('json', 'sentbox', 'false');
3013 self::assertArrayHasKey('direct_message', $result);
3017 * Test the api_direct_messages_box() function.
3021 public function testApiDirectMessagesBoxWithConversation()
3023 $result = api_direct_messages_box('json', 'conversation', 'false');
3024 self::assertArrayHasKey('direct_message', $result);
3028 * Test the api_direct_messages_box() function.
3032 public function testApiDirectMessagesBoxWithAll()
3034 $result = api_direct_messages_box('json', 'all', 'false');
3035 self::assertArrayHasKey('direct_message', $result);
3039 * Test the api_direct_messages_box() function.
3043 public function testApiDirectMessagesBoxWithInbox()
3045 $result = api_direct_messages_box('json', 'inbox', 'false');
3046 self::assertArrayHasKey('direct_message', $result);
3050 * Test the api_direct_messages_box() function.
3054 public function testApiDirectMessagesBoxWithVerbose()
3056 $result = api_direct_messages_box('json', 'sentbox', 'true');
3060 'result' => 'error',
3061 'message' => 'no mails available'
3069 * Test the api_direct_messages_box() function with a RSS result.
3073 public function testApiDirectMessagesBoxWithRss()
3075 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3076 self::assertXml($result, 'direct-messages');
3080 * Test the api_direct_messages_box() function without an authenticated user.
3084 public function testApiDirectMessagesBoxWithUnallowedUser()
3086 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3087 $_SESSION['allow_api'] = false;
3088 $_GET['screen_name'] = $this->selfUser['nick'];
3089 api_direct_messages_box('json', 'sentbox', 'false');
3093 * Test the api_direct_messages_sentbox() function.
3097 public function testApiDirectMessagesSentbox()
3099 $result = api_direct_messages_sentbox('json');
3100 self::assertArrayHasKey('direct_message', $result);
3104 * Test the api_direct_messages_inbox() function.
3108 public function testApiDirectMessagesInbox()
3110 $result = api_direct_messages_inbox('json');
3111 self::assertArrayHasKey('direct_message', $result);
3115 * Test the api_direct_messages_all() function.
3119 public function testApiDirectMessagesAll()
3121 $result = api_direct_messages_all('json');
3122 self::assertArrayHasKey('direct_message', $result);
3126 * Test the api_direct_messages_conversation() function.
3130 public function testApiDirectMessagesConversation()
3132 $result = api_direct_messages_conversation('json');
3133 self::assertArrayHasKey('direct_message', $result);
3137 * Test the api_oauth_request_token() function.
3141 public function testApiOauthRequestToken()
3143 $this->markTestIncomplete('exit() kills phpunit as well');
3147 * Test the api_oauth_access_token() function.
3151 public function testApiOauthAccessToken()
3153 $this->markTestIncomplete('exit() kills phpunit as well');
3157 * Test the api_fr_photoalbum_delete() function.
3161 public function testApiFrPhotoalbumDelete()
3163 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3164 api_fr_photoalbum_delete('json');
3168 * Test the api_fr_photoalbum_delete() function with an album name.
3172 public function testApiFrPhotoalbumDeleteWithAlbum()
3174 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3175 $_REQUEST['album'] = 'album_name';
3176 api_fr_photoalbum_delete('json');
3180 * Test the api_fr_photoalbum_delete() function with an album name.
3184 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3186 $this->markTestIncomplete('We need to add a dataset for this.');
3190 * Test the api_fr_photoalbum_delete() function.
3194 public function testApiFrPhotoalbumUpdate()
3196 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3197 api_fr_photoalbum_update('json');
3201 * Test the api_fr_photoalbum_delete() function with an album name.
3205 public function testApiFrPhotoalbumUpdateWithAlbum()
3207 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3208 $_REQUEST['album'] = 'album_name';
3209 api_fr_photoalbum_update('json');
3213 * Test the api_fr_photoalbum_delete() function with an album name.
3217 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3219 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3220 $_REQUEST['album'] = 'album_name';
3221 $_REQUEST['album_new'] = 'album_name';
3222 api_fr_photoalbum_update('json');
3226 * Test the api_fr_photoalbum_update() function without an authenticated user.
3230 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3232 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3233 $_SESSION['authenticated'] = false;
3234 api_fr_photoalbum_update('json');
3238 * Test the api_fr_photoalbum_delete() function with an album name.
3242 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3244 $this->markTestIncomplete('We need to add a dataset for this.');
3248 * Test the api_fr_photos_list() function.
3252 public function testApiFrPhotosList()
3254 $result = api_fr_photos_list('json');
3255 self::assertArrayHasKey('photo', $result);
3259 * Test the api_fr_photos_list() function without an authenticated user.
3263 public function testApiFrPhotosListWithoutAuthenticatedUser()
3265 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3266 $_SESSION['authenticated'] = false;
3267 api_fr_photos_list('json');
3271 * Test the api_fr_photo_create_update() function.
3273 public function testApiFrPhotoCreateUpdate()
3275 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3276 api_fr_photo_create_update('json');
3280 * Test the api_fr_photo_create_update() function without an authenticated user.
3284 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3286 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3287 $_SESSION['authenticated'] = false;
3288 api_fr_photo_create_update('json');
3292 * Test the api_fr_photo_create_update() function with an album name.
3296 public function testApiFrPhotoCreateUpdateWithAlbum()
3298 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3299 $_REQUEST['album'] = 'album_name';
3300 api_fr_photo_create_update('json');
3304 * Test the api_fr_photo_create_update() function with the update mode.
3308 public function testApiFrPhotoCreateUpdateWithUpdate()
3310 $this->markTestIncomplete('We need to create a dataset for this');
3314 * Test the api_fr_photo_create_update() function with an uploaded file.
3318 public function testApiFrPhotoCreateUpdateWithFile()
3320 $this->markTestIncomplete();
3324 * Test the api_fr_photo_delete() function.
3328 public function testApiFrPhotoDelete()
3330 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3331 api_fr_photo_delete('json');
3335 * Test the api_fr_photo_delete() function without an authenticated user.
3339 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3341 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3342 $_SESSION['authenticated'] = false;
3343 api_fr_photo_delete('json');
3347 * Test the api_fr_photo_delete() function with a photo ID.
3351 public function testApiFrPhotoDeleteWithPhotoId()
3353 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3354 $_REQUEST['photo_id'] = 1;
3355 api_fr_photo_delete('json');
3359 * Test the api_fr_photo_delete() function with a correct photo ID.
3363 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3365 $this->markTestIncomplete('We need to create a dataset for this.');
3369 * Test the api_fr_photo_detail() function.
3373 public function testApiFrPhotoDetail()
3375 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3376 api_fr_photo_detail('json');
3380 * Test the api_fr_photo_detail() function without an authenticated user.
3384 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3386 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3387 $_SESSION['authenticated'] = false;
3388 api_fr_photo_detail('json');
3392 * Test the api_fr_photo_detail() function with a photo ID.
3396 public function testApiFrPhotoDetailWithPhotoId()
3398 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3399 $_REQUEST['photo_id'] = 1;
3400 api_fr_photo_detail('json');
3404 * Test the api_fr_photo_detail() function with a correct photo ID.
3408 public function testApiFrPhotoDetailCorrectPhotoId()
3410 $this->markTestIncomplete('We need to create a dataset for this.');
3414 * Test the api_account_update_profile_image() function.
3418 public function testApiAccountUpdateProfileImage()
3420 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3421 api_account_update_profile_image('json');
3425 * Test the api_account_update_profile_image() function without an authenticated user.
3429 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3431 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3432 $_SESSION['authenticated'] = false;
3433 api_account_update_profile_image('json');
3437 * Test the api_account_update_profile_image() function with an uploaded file.
3441 public function testApiAccountUpdateProfileImageWithUpload()
3443 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3444 $this->markTestIncomplete();
3449 * Test the api_account_update_profile() function.
3453 public function testApiAccountUpdateProfile()
3455 $_POST['name'] = 'new_name';
3456 $_POST['description'] = 'new_description';
3457 $result = api_account_update_profile('json');
3458 // We can't use assertSelfUser() here because the user object is missing some properties.
3459 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3460 self::assertEquals('DFRN', $result['user']['location']);
3461 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3462 self::assertEquals('dfrn', $result['user']['network']);
3463 self::assertEquals('new_name', $result['user']['name']);
3464 self::assertEquals('new_description', $result['user']['description']);
3468 * Test the check_acl_input() function.
3472 public function testCheckAclInput()
3474 $result = check_acl_input('<aclstring>');
3475 // Where does this result come from?
3476 self::assertEquals(1, $result);
3480 * Test the check_acl_input() function with an empty ACL string.
3484 public function testCheckAclInputWithEmptyAclString()
3486 $result = check_acl_input(' ');
3487 self::assertFalse($result);
3491 * Test the save_media_to_database() function.
3495 public function testSaveMediaToDatabase()
3497 $this->markTestIncomplete();
3501 * Test the post_photo_item() function.
3505 public function testPostPhotoItem()
3507 $this->markTestIncomplete();
3511 * Test the prepare_photo_data() function.
3515 public function testPreparePhotoData()
3517 $this->markTestIncomplete();
3521 * Test the api_share_as_retweet() function with a valid item.
3525 public function testApiShareAsRetweetWithValidItem()
3527 $this->markTestIncomplete();
3531 * Test the api_in_reply_to() function.
3535 public function testApiInReplyTo()
3537 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3538 self::assertArrayHasKey('status_id', $result);
3539 self::assertArrayHasKey('user_id', $result);
3540 self::assertArrayHasKey('status_id_str', $result);
3541 self::assertArrayHasKey('user_id_str', $result);
3542 self::assertArrayHasKey('screen_name', $result);
3546 * Test the api_in_reply_to() function with a valid item.
3550 public function testApiInReplyToWithValidItem()
3552 $this->markTestIncomplete();
3556 * Test the api_clean_plain_items() function.
3560 public function testApiCleanPlainItems()
3562 $_REQUEST['include_entities'] = 'true';
3563 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3564 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3568 * Test the api_best_nickname() function.
3572 public function testApiBestNickname()
3575 $result = api_best_nickname($contacts);
3576 self::assertNull($result);
3580 * Test the api_best_nickname() function with contacts.
3584 public function testApiBestNicknameWithContacts()
3586 $this->markTestIncomplete();
3590 * Test the api_friendica_group_show() function.
3594 public function testApiFriendicaGroupShow()
3596 $this->markTestIncomplete();
3600 * Test the api_friendica_group_delete() function.
3604 public function testApiFriendicaGroupDelete()
3606 $this->markTestIncomplete();
3610 * Test the api_lists_destroy() function.
3614 public function testApiListsDestroy()
3616 $this->markTestIncomplete();
3620 * Test the group_create() function.
3624 public function testGroupCreate()
3626 $this->markTestIncomplete();
3630 * Test the api_friendica_group_create() function.
3634 public function testApiFriendicaGroupCreate()
3636 $this->markTestIncomplete();
3640 * Test the api_lists_create() function.
3644 public function testApiListsCreate()
3646 $this->markTestIncomplete();
3650 * Test the api_friendica_group_update() function.
3654 public function testApiFriendicaGroupUpdate()
3656 $this->markTestIncomplete();
3660 * Test the api_lists_update() function.
3664 public function testApiListsUpdate()
3666 $this->markTestIncomplete();
3670 * Test the api_friendica_activity() function.
3674 public function testApiFriendicaActivity()
3676 $this->markTestIncomplete();
3680 * Test the api_friendica_notification() function.
3684 public function testApiFriendicaNotification()
3686 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3687 api_friendica_notification('json');
3691 * Test the api_friendica_notification() function without an authenticated user.
3695 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3697 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3698 $_SESSION['authenticated'] = false;
3699 api_friendica_notification('json');
3703 * Test the api_friendica_notification() function with empty result
3707 public function testApiFriendicaNotificationWithEmptyResult()
3709 DI::args()->setArgv(['api', 'friendica', 'notification']);
3710 $_SESSION['uid'] = 41;
3711 $result = api_friendica_notification('json');
3712 self::assertEquals(['note' => false], $result);
3716 * Test the api_friendica_notification() function with an XML result.
3720 public function testApiFriendicaNotificationWithXmlResult()
3722 DI::args()->setArgv(['api', 'friendica', 'notification']);
3723 $result = api_friendica_notification('xml');
3724 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3725 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3728 <?xml version="1.0"?>
3730 <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"/>
3733 self::assertXmlStringEqualsXmlString($assertXml, $result);
3737 * Test the api_friendica_notification() function with an JSON result.
3741 public function testApiFriendicaNotificationWithJsonResult()
3743 DI::args()->setArgv(['api', 'friendica', 'notification']);
3744 $result = json_encode(api_friendica_notification('json'));
3745 self::assertJson($result);
3749 * Test the api_friendica_notification_seen() function.
3753 public function testApiFriendicaNotificationSeen()
3755 $this->markTestIncomplete();
3759 * Test the api_friendica_direct_messages_setseen() function.
3763 public function testApiFriendicaDirectMessagesSetseen()
3765 $this->markTestIncomplete();
3769 * Test the api_friendica_direct_messages_search() function.
3773 public function testApiFriendicaDirectMessagesSearch()
3775 $this->markTestIncomplete();
3779 * Test the api_saved_searches_list() function.
3783 public function testApiSavedSearchesList()
3785 $result = api_saved_searches_list('json');
3786 self::assertEquals(1, $result['terms'][0]['id']);
3787 self::assertEquals(1, $result['terms'][0]['id_str']);
3788 self::assertEquals('Saved search', $result['terms'][0]['name']);
3789 self::assertEquals('Saved search', $result['terms'][0]['query']);