6 namespace Friendica\Test\legacy;
9 use Friendica\Core\Config\IConfig;
10 use Friendica\Core\PConfig\IPConfig;
11 use Friendica\Core\Protocol;
13 use Friendica\Network\HTTPException;
14 use Friendica\Test\FixtureTest;
15 use Friendica\Util\Temporal;
16 use Monolog\Handler\TestHandler;
18 require_once __DIR__ . '/../../include/api.php';
21 * Tests for the API functions.
23 * Functions that use header() need to be tested in a separate process.
24 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
26 * @backupGlobals enabled
28 class ApiTest extends FixtureTest
31 * @var TestHandler Can handle log-outputs
38 protected $friendUser;
42 protected $wrongUserId;
51 * Create variables used by tests.
53 protected function setUp() : void
55 global $API, $called_api;
61 /** @var IConfig $config */
62 $this->config = $this->dice->create(IConfig::class);
64 $this->config->set('system', 'url', 'http://localhost');
65 $this->config->set('system', 'hostname', 'localhost');
66 $this->config->set('system', 'worker_dont_fork', true);
69 $this->config->set('config', 'hostname', 'localhost');
70 $this->config->set('system', 'throttle_limit_day', 100);
71 $this->config->set('system', 'throttle_limit_week', 100);
72 $this->config->set('system', 'throttle_limit_month', 100);
73 $this->config->set('system', 'theme', 'system_theme');
77 $this->app = DI::app();
79 DI::args()->setArgc(1);
81 // User data that the test database is populated with
84 'name' => 'Self contact',
85 'nick' => 'selfcontact',
86 'nurl' => 'http://localhost/profile/selfcontact'
90 'name' => 'Friend contact',
91 'nick' => 'friendcontact',
92 'nurl' => 'http://localhost/profile/friendcontact'
96 'name' => 'othercontact',
97 'nick' => 'othercontact',
98 'nurl' => 'http://localhost/profile/othercontact'
101 // User ID that we know is not in the database
102 $this->wrongUserId = 666;
104 DI::session()->start();
106 // Most API require login so we force the session
109 'authenticated' => true,
110 'uid' => $this->selfUser['id']
115 * Assert that an user array contains expected keys.
117 * @param array $user User array
121 private function assertSelfUser(array $user)
123 self::assertEquals($this->selfUser['id'], $user['uid']);
124 self::assertEquals($this->selfUser['id'], $user['cid']);
125 self::assertEquals(1, $user['self']);
126 self::assertEquals('DFRN', $user['location']);
127 self::assertEquals($this->selfUser['name'], $user['name']);
128 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
129 self::assertEquals('dfrn', $user['network']);
130 self::assertTrue($user['verified']);
134 * Assert that an user array contains expected keys.
136 * @param array $user User array
140 private function assertOtherUser(array $user = [])
142 self::assertEquals($this->otherUser['id'], $user['id']);
143 self::assertEquals($this->otherUser['id'], $user['id_str']);
144 self::assertEquals(0, $user['self']);
145 self::assertEquals($this->otherUser['name'], $user['name']);
146 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
147 self::assertFalse($user['verified']);
151 * Assert that a status array contains expected keys.
153 * @param array $status Status array
157 private function assertStatus(array $status = [])
159 self::assertIsString($status['text'] ?? '');
160 self::assertIsInt($status['id'] ?? '');
161 // We could probably do more checks here.
165 * Assert that a list array contains expected keys.
167 * @param array $list List array
171 private function assertList(array $list = [])
173 self::assertIsString($list['name']);
174 self::assertIsInt($list['id']);
175 self::assertIsString('string', $list['id_str']);
176 self::assertContains($list['mode'], ['public', 'private']);
177 // We could probably do more checks here.
181 * Assert that the string is XML and contain the root element.
183 * @param string $result XML string
184 * @param string $root_element Root element name
188 private function assertXml($result = '', $root_element = '')
190 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
191 self::assertStringContainsString('<' . $root_element, $result);
192 // We could probably do more checks here.
196 * Get the path to a temporary empty PNG image.
198 * @return string Path
200 private function getTempImage()
202 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
206 // Empty 1x1 px PNG image
207 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
215 * Test the api_user() function.
219 public function testApiUser()
221 self::assertEquals($this->selfUser['id'], api_user());
225 * Test the api_user() function with an unallowed user.
229 public function testApiUserWithUnallowedUser()
231 $_SESSION = ['allow_api' => false];
232 self::assertEquals(false, api_user());
236 * Test the api_source() function.
240 public function testApiSource()
242 self::assertEquals('api', api_source());
246 * Test the api_source() function with a Twidere user agent.
250 public function testApiSourceWithTwidere()
252 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
253 self::assertEquals('Twidere', api_source());
257 * Test the api_source() function with a GET parameter.
261 public function testApiSourceWithGet()
263 $_GET['source'] = 'source_name';
264 self::assertEquals('source_name', api_source());
268 * Test the api_date() function.
272 public function testApiDate()
274 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
278 * Test the api_register_func() function.
282 public function testApiRegisterFunc()
294 self::assertTrue($API['api_path']['auth']);
295 self::assertEquals('method', $API['api_path']['method']);
296 self::assertTrue(is_callable($API['api_path']['func']));
300 * Test the api_login() function without any login.
302 * @runInSeparateProcess
303 * @preserveGlobalState disabled
304 * @preserveGlobalState disabled
306 public function testApiLoginWithoutLogin()
308 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
309 api_login($this->app);
313 * Test the api_login() function with a bad login.
315 * @runInSeparateProcess
316 * @preserveGlobalState disabled
317 * @preserveGlobalState disabled
319 public function testApiLoginWithBadLogin()
321 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
322 $_SERVER['PHP_AUTH_USER'] = 'user@server';
323 api_login($this->app);
327 * Test the api_login() function with oAuth.
331 public function testApiLoginWithOauth()
333 $this->markTestIncomplete('Can we test this easily?');
337 * Test the api_login() function with authentication provided by an addon.
341 public function testApiLoginWithAddonAuth()
343 $this->markTestIncomplete('Can we test this easily?');
347 * Test the api_login() function with a correct login.
349 * @runInSeparateProcess
350 * @preserveGlobalState disabled
351 * @doesNotPerformAssertions
353 public function testApiLoginWithCorrectLogin()
355 $_SERVER['PHP_AUTH_USER'] = 'Test user';
356 $_SERVER['PHP_AUTH_PW'] = 'password';
357 api_login($this->app);
361 * Test the api_login() function with a remote user.
363 * @runInSeparateProcess
364 * @preserveGlobalState disabled
366 public function testApiLoginWithRemoteUser()
368 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
369 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
370 api_login($this->app);
374 * Test the api_check_method() function.
378 public function testApiCheckMethod()
380 self::assertFalse(api_check_method('method'));
384 * Test the api_check_method() function with a correct method.
388 public function testApiCheckMethodWithCorrectMethod()
390 $_SERVER['REQUEST_METHOD'] = 'method';
391 self::assertTrue(api_check_method('method'));
395 * Test the api_check_method() function with a wildcard.
399 public function testApiCheckMethodWithWildcard()
401 self::assertTrue(api_check_method('*'));
405 * Test the api_call() function.
407 * @runInSeparateProcess
408 * @preserveGlobalState disabled
410 public function testApiCall()
414 'method' => 'method',
415 'func' => function () {
416 return ['data' => ['some_data']];
419 $_SERVER['REQUEST_METHOD'] = 'method';
420 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
421 $_GET['callback'] = 'callback_name';
423 $args = DI::args()->determine($_SERVER, $_GET);
426 'callback_name(["some_data"])',
427 api_call($this->app, $args)
432 * Test the api_call() function with the profiled enabled.
434 * @runInSeparateProcess
435 * @preserveGlobalState disabled
437 public function testApiCallWithProfiler()
441 'method' => 'method',
442 'func' => function () {
443 return ['data' => ['some_data']];
447 $_SERVER['REQUEST_METHOD'] = 'method';
448 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
450 $args = DI::args()->determine($_SERVER, $_GET);
452 $this->config->set('system', 'profiler', true);
453 $this->config->set('rendertime', 'callstack', true);
454 $this->app->callstack = [
455 'database' => ['some_function' => 200],
456 'database_write' => ['some_function' => 200],
457 'cache' => ['some_function' => 200],
458 'cache_write' => ['some_function' => 200],
459 'network' => ['some_function' => 200]
464 api_call($this->app, $args)
469 * Test the api_call() function without any result.
471 * @runInSeparateProcess
472 * @preserveGlobalState disabled
474 public function testApiCallWithNoResult()
478 'method' => 'method',
479 'func' => function () {
483 $_SERVER['REQUEST_METHOD'] = 'method';
484 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
486 $args = DI::args()->determine($_SERVER, $_GET);
489 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
490 api_call($this->app, $args)
495 * Test the api_call() function with an unimplemented API.
497 * @runInSeparateProcess
498 * @preserveGlobalState disabled
500 public function testApiCallWithUninplementedApi()
503 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
509 * Test the api_call() function with a JSON result.
511 * @runInSeparateProcess
512 * @preserveGlobalState disabled
514 public function testApiCallWithJson()
518 'method' => 'method',
519 'func' => function () {
520 return ['data' => ['some_data']];
523 $_SERVER['REQUEST_METHOD'] = 'method';
524 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
526 $args = DI::args()->determine($_SERVER, $_GET);
530 api_call($this->app, $args)
535 * Test the api_call() function with an XML result.
537 * @runInSeparateProcess
538 * @preserveGlobalState disabled
540 public function testApiCallWithXml()
544 'method' => 'method',
545 'func' => function () {
549 $_SERVER['REQUEST_METHOD'] = 'method';
550 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
552 $args = DI::args()->determine($_SERVER, $_GET);
556 api_call($this->app, $args)
561 * Test the api_call() function with an RSS result.
563 * @runInSeparateProcess
564 * @preserveGlobalState disabled
566 public function testApiCallWithRss()
570 'method' => 'method',
571 'func' => function () {
575 $_SERVER['REQUEST_METHOD'] = 'method';
576 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
578 $args = DI::args()->determine($_SERVER, $_GET);
581 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
583 api_call($this->app, $args)
588 * Test the api_call() function with an Atom result.
590 * @runInSeparateProcess
591 * @preserveGlobalState disabled
593 public function testApiCallWithAtom()
597 'method' => 'method',
598 'func' => function () {
602 $_SERVER['REQUEST_METHOD'] = 'method';
603 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
605 $args = DI::args()->determine($_SERVER, $_GET);
608 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
610 api_call($this->app, $args)
615 * Test the api_call() function with an unallowed method.
617 * @runInSeparateProcess
618 * @preserveGlobalState disabled
620 public function testApiCallWithWrongMethod()
623 $API['api_path'] = ['method' => 'method'];
625 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
627 $args = DI::args()->determine($_SERVER, $_GET);
630 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
631 api_call($this->app, $args)
636 * Test the api_call() function with an unauthorized user.
638 * @runInSeparateProcess
639 * @preserveGlobalState disabled
641 public function testApiCallWithWrongAuth()
645 'method' => 'method',
648 $_SESSION['authenticated'] = false;
649 $_SERVER['REQUEST_METHOD'] = 'method';
650 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
652 $args = DI::args()->determine($_SERVER, $_GET);
655 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
656 api_call($this->app, $args)
661 * Test the api_error() function with a JSON result.
663 * @runInSeparateProcess
664 * @preserveGlobalState disabled
666 public function testApiErrorWithJson()
669 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
670 api_error('json', new HTTPException\OKException('error_message'), DI::args())
675 * Test the api_error() function with an XML result.
677 * @runInSeparateProcess
678 * @preserveGlobalState disabled
680 public function testApiErrorWithXml()
683 '<?xml version="1.0"?>' . "\n" .
684 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
685 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
686 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
687 ' <error>error_message</error>' . "\n" .
688 ' <code>200 OK</code>' . "\n" .
689 ' <request/>' . "\n" .
691 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
696 * Test the api_error() function with an RSS result.
698 * @runInSeparateProcess
699 * @preserveGlobalState disabled
701 public function testApiErrorWithRss()
704 '<?xml version="1.0"?>' . "\n" .
705 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
706 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
707 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
708 ' <error>error_message</error>' . "\n" .
709 ' <code>200 OK</code>' . "\n" .
710 ' <request/>' . "\n" .
712 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
717 * Test the api_error() function with an Atom result.
719 * @runInSeparateProcess
720 * @preserveGlobalState disabled
722 public function testApiErrorWithAtom()
725 '<?xml version="1.0"?>' . "\n" .
726 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
727 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
728 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
729 ' <error>error_message</error>' . "\n" .
730 ' <code>200 OK</code>' . "\n" .
731 ' <request/>' . "\n" .
733 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
738 * Test the api_rss_extra() function.
742 public function testApiRssExtra()
744 $user_info = ['url' => 'user_url', 'lang' => 'en'];
745 $result = api_rss_extra($this->app, [], $user_info);
746 self::assertEquals($user_info, $result['$user']);
747 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
748 self::assertArrayHasKey('self', $result['$rss']);
749 self::assertArrayHasKey('base', $result['$rss']);
750 self::assertArrayHasKey('updated', $result['$rss']);
751 self::assertArrayHasKey('atom_updated', $result['$rss']);
752 self::assertArrayHasKey('language', $result['$rss']);
753 self::assertArrayHasKey('logo', $result['$rss']);
757 * Test the api_rss_extra() function without any user info.
761 public function testApiRssExtraWithoutUserInfo()
763 $result = api_rss_extra($this->app, [], null);
764 self::assertIsArray($result['$user']);
765 self::assertArrayHasKey('alternate', $result['$rss']);
766 self::assertArrayHasKey('self', $result['$rss']);
767 self::assertArrayHasKey('base', $result['$rss']);
768 self::assertArrayHasKey('updated', $result['$rss']);
769 self::assertArrayHasKey('atom_updated', $result['$rss']);
770 self::assertArrayHasKey('language', $result['$rss']);
771 self::assertArrayHasKey('logo', $result['$rss']);
775 * Test the api_unique_id_to_nurl() function.
779 public function testApiUniqueIdToNurl()
781 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
785 * Test the api_unique_id_to_nurl() function with a correct ID.
789 public function testApiUniqueIdToNurlWithCorrectId()
791 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
795 * Test the api_get_user() function.
799 public function testApiGetUser()
801 $user = api_get_user($this->app);
802 self::assertSelfUser($user);
803 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
804 self::assertEquals('6fdbe8', $user['profile_link_color']);
805 self::assertEquals('ededed', $user['profile_background_color']);
809 * Test the api_get_user() function with a Frio schema.
813 public function testApiGetUserWithFrioSchema()
815 $pConfig = $this->dice->create(IPConfig::class);
816 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
817 $user = api_get_user($this->app);
818 self::assertSelfUser($user);
819 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
820 self::assertEquals('6fdbe8', $user['profile_link_color']);
821 self::assertEquals('ededed', $user['profile_background_color']);
825 * Test the api_get_user() function with an empty Frio schema.
829 public function testApiGetUserWithEmptyFrioSchema()
831 $pConfig = $this->dice->create(IPConfig::class);
832 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
833 $user = api_get_user($this->app);
834 self::assertSelfUser($user);
835 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
836 self::assertEquals('6fdbe8', $user['profile_link_color']);
837 self::assertEquals('ededed', $user['profile_background_color']);
841 * Test the api_get_user() function with a custom Frio schema.
845 public function testApiGetUserWithCustomFrioSchema()
847 $pConfig = $this->dice->create(IPConfig::class);
848 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
849 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
850 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
851 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
852 $user = api_get_user($this->app);
853 self::assertSelfUser($user);
854 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
855 self::assertEquals('123456', $user['profile_link_color']);
856 self::assertEquals('123456', $user['profile_background_color']);
860 * Test the api_get_user() function with an user that is not allowed to use the API.
862 * @runInSeparateProcess
863 * @preserveGlobalState disabled
865 public function testApiGetUserWithoutApiUser()
867 $_SERVER['PHP_AUTH_USER'] = 'Test user';
868 $_SERVER['PHP_AUTH_PW'] = 'password';
869 $_SESSION['allow_api'] = false;
870 self::assertFalse(api_get_user($this->app));
874 * Test the api_get_user() function with an user ID in a GET parameter.
878 public function testApiGetUserWithGetId()
880 $_GET['user_id'] = $this->otherUser['id'];
881 self::assertOtherUser(api_get_user($this->app));
885 * Test the api_get_user() function with a wrong user ID in a GET parameter.
889 public function testApiGetUserWithWrongGetId()
891 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
892 $_GET['user_id'] = $this->wrongUserId;
893 self::assertOtherUser(api_get_user($this->app));
897 * Test the api_get_user() function with an user name in a GET parameter.
901 public function testApiGetUserWithGetName()
903 $_GET['screen_name'] = $this->selfUser['nick'];
904 self::assertSelfUser(api_get_user($this->app));
908 * Test the api_get_user() function with a profile URL in a GET parameter.
912 public function testApiGetUserWithGetUrl()
914 $_GET['profileurl'] = $this->selfUser['nurl'];
915 self::assertSelfUser(api_get_user($this->app));
919 * Test the api_get_user() function with an user ID in the API path.
923 public function testApiGetUserWithNumericCalledApi()
926 $called_api = ['api_path'];
927 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
928 self::assertOtherUser(api_get_user($this->app));
932 * Test the api_get_user() function with the $called_api global variable.
936 public function testApiGetUserWithCalledApi()
939 $called_api = ['api', 'api_path'];
940 self::assertSelfUser(api_get_user($this->app));
944 * Test the api_get_user() function with a valid user.
948 public function testApiGetUserWithCorrectUser()
950 self::assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
954 * Test the api_get_user() function with a wrong user ID.
958 public function testApiGetUserWithWrongUser()
960 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
961 self::assertOtherUser(api_get_user($this->app, $this->wrongUserId));
965 * Test the api_get_user() function with a 0 user ID.
969 public function testApiGetUserWithZeroUser()
971 self::assertSelfUser(api_get_user($this->app, 0));
975 * Test the api_item_get_user() function.
979 public function testApiItemGetUser()
981 $users = api_item_get_user($this->app, []);
982 self::assertSelfUser($users[0]);
986 * Test the api_item_get_user() function with a different item parent.
990 public function testApiItemGetUserWithDifferentParent()
992 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
993 self::assertSelfUser($users[0]);
994 self::assertEquals($users[0], $users[1]);
998 * Test the api_walk_recursive() function.
1002 public function testApiWalkRecursive()
1010 // Should we test this with a callback that actually does something?
1018 * Test the api_walk_recursive() function with an array.
1022 public function testApiWalkRecursiveWithArray()
1024 $array = [['item1'], ['item2']];
1030 // Should we test this with a callback that actually does something?
1038 * Test the api_reformat_xml() function.
1042 public function testApiReformatXml()
1046 self::assertTrue(api_reformat_xml($item, $key));
1047 self::assertEquals('true', $item);
1051 * Test the api_reformat_xml() function with a statusnet_api key.
1055 public function testApiReformatXmlWithStatusnetKey()
1058 $key = 'statusnet_api';
1059 self::assertTrue(api_reformat_xml($item, $key));
1060 self::assertEquals('statusnet:api', $key);
1064 * Test the api_reformat_xml() function with a friendica_api key.
1068 public function testApiReformatXmlWithFriendicaKey()
1071 $key = 'friendica_api';
1072 self::assertTrue(api_reformat_xml($item, $key));
1073 self::assertEquals('friendica:api', $key);
1077 * Test the api_create_xml() function.
1081 public function testApiCreateXml()
1084 '<?xml version="1.0"?>' . "\n" .
1085 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1086 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1087 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1088 ' <data>some_data</data>' . "\n" .
1089 '</root_element>' . "\n",
1090 api_create_xml(['data' => ['some_data']], 'root_element')
1095 * Test the api_create_xml() function without any XML namespace.
1099 public function testApiCreateXmlWithoutNamespaces()
1102 '<?xml version="1.0"?>' . "\n" .
1104 ' <data>some_data</data>' . "\n" .
1106 api_create_xml(['data' => ['some_data']], 'ok')
1111 * Test the api_format_data() function.
1115 public function testApiFormatData()
1117 $data = ['some_data'];
1118 self::assertEquals($data, api_format_data('root_element', 'json', $data));
1122 * Test the api_format_data() function with an XML result.
1126 public function testApiFormatDataWithXml()
1129 '<?xml version="1.0"?>' . "\n" .
1130 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1131 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1132 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1133 ' <data>some_data</data>' . "\n" .
1134 '</root_element>' . "\n",
1135 api_format_data('root_element', 'xml', ['data' => ['some_data']])
1140 * Test the api_account_verify_credentials() function.
1144 public function testApiAccountVerifyCredentials()
1146 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1150 * Test the api_account_verify_credentials() function without an authenticated user.
1154 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1156 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1157 $_SESSION['authenticated'] = false;
1158 api_account_verify_credentials('json');
1162 * Test the requestdata() function.
1166 public function testRequestdata()
1168 self::assertNull(requestdata('variable_name'));
1172 * Test the requestdata() function with a POST parameter.
1176 public function testRequestdataWithPost()
1178 $_POST['variable_name'] = 'variable_value';
1179 self::assertEquals('variable_value', requestdata('variable_name'));
1183 * Test the requestdata() function with a GET parameter.
1187 public function testRequestdataWithGet()
1189 $_GET['variable_name'] = 'variable_value';
1190 self::assertEquals('variable_value', requestdata('variable_name'));
1194 * Test the api_statuses_mediap() function.
1198 public function testApiStatusesMediap()
1200 DI::args()->setArgc(2);
1208 'tmp_name' => $this->getTempImage(),
1209 'name' => 'spacer.png',
1210 'type' => 'image/png'
1213 $_GET['status'] = '<b>Status content</b>';
1215 $result = api_statuses_mediap('json');
1216 self::assertStatus($result['status']);
1220 * Test the api_statuses_mediap() function without an authenticated user.
1224 public function testApiStatusesMediapWithoutAuthenticatedUser()
1226 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1227 $_SESSION['authenticated'] = false;
1228 api_statuses_mediap('json');
1232 * Test the api_statuses_update() function.
1236 public function testApiStatusesUpdate()
1238 $_GET['status'] = 'Status content #friendica';
1239 $_GET['in_reply_to_status_id'] = -1;
1248 'tmp_name' => $this->getTempImage(),
1249 'name' => 'spacer.png',
1250 'type' => 'image/png'
1254 $result = api_statuses_update('json');
1255 self::assertStatus($result['status']);
1259 * Test the api_statuses_update() function with an HTML status.
1263 public function testApiStatusesUpdateWithHtml()
1265 $_GET['htmlstatus'] = '<b>Status content</b>';
1267 $result = api_statuses_update('json');
1268 self::assertStatus($result['status']);
1272 * Test the api_statuses_update() function without an authenticated user.
1276 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1278 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1279 $_SESSION['authenticated'] = false;
1280 api_statuses_update('json');
1284 * Test the api_statuses_update() function with a parent status.
1288 public function testApiStatusesUpdateWithParent()
1290 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1294 * Test the api_statuses_update() function with a media_ids parameter.
1298 public function testApiStatusesUpdateWithMediaIds()
1300 $this->markTestIncomplete();
1304 * Test the api_statuses_update() function with the throttle limit reached.
1308 public function testApiStatusesUpdateWithDayThrottleReached()
1310 $this->markTestIncomplete();
1314 * Test the api_media_upload() function.
1315 * @runInSeparateProcess
1316 * @preserveGlobalState disabled
1318 public function testApiMediaUpload()
1320 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1325 * Test the api_media_upload() function without an authenticated user.
1329 public function testApiMediaUploadWithoutAuthenticatedUser()
1331 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1332 $_SESSION['authenticated'] = false;
1337 * Test the api_media_upload() function with an invalid uploaded media.
1341 public function testApiMediaUploadWithMedia()
1343 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1347 'tmp_name' => 'tmp_name'
1354 * Test the api_media_upload() function with an valid uploaded media.
1358 public function testApiMediaUploadWithValidMedia()
1366 'tmp_name' => $this->getTempImage(),
1367 'name' => 'spacer.png',
1368 'type' => 'image/png'
1372 DI::args()->setArgc(2);
1374 $result = api_media_upload();
1375 self::assertEquals('image/png', $result['media']['image']['image_type']);
1376 self::assertEquals(1, $result['media']['image']['w']);
1377 self::assertEquals(1, $result['media']['image']['h']);
1378 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1382 * Test the api_status_show() function.
1384 public function testApiStatusShowWithJson()
1386 $result = api_status_show('json', 1);
1387 self::assertStatus($result['status']);
1391 * Test the api_status_show() function with an XML result.
1393 public function testApiStatusShowWithXml()
1395 $result = api_status_show('xml', 1);
1396 self::assertXml($result, 'statuses');
1400 * Test the api_get_last_status() function
1402 public function testApiGetLastStatus()
1404 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1406 self::assertNotNull($item);
1410 * Test the api_users_show() function.
1414 public function testApiUsersShow()
1416 $result = api_users_show('json');
1417 // We can't use assertSelfUser() here because the user object is missing some properties.
1418 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1419 self::assertEquals('DFRN', $result['user']['location']);
1420 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1421 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1422 self::assertEquals('dfrn', $result['user']['network']);
1423 self::assertTrue($result['user']['verified']);
1427 * Test the api_users_show() function with an XML result.
1431 public function testApiUsersShowWithXml()
1433 $result = api_users_show('xml');
1434 self::assertXml($result, 'statuses');
1438 * Test the api_users_search() function.
1442 public function testApiUsersSearch()
1444 $_GET['q'] = 'othercontact';
1445 $result = api_users_search('json');
1446 self::assertOtherUser($result['users'][0]);
1450 * Test the api_users_search() function with an XML result.
1454 public function testApiUsersSearchWithXml()
1456 $_GET['q'] = 'othercontact';
1457 $result = api_users_search('xml');
1458 self::assertXml($result, 'users');
1462 * Test the api_users_search() function without a GET q parameter.
1466 public function testApiUsersSearchWithoutQuery()
1468 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1469 api_users_search('json');
1473 * Test the api_users_lookup() function.
1477 public function testApiUsersLookup()
1479 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1480 api_users_lookup('json');
1484 * Test the api_users_lookup() function with an user ID.
1488 public function testApiUsersLookupWithUserId()
1490 $_REQUEST['user_id'] = $this->otherUser['id'];
1491 $result = api_users_lookup('json');
1492 self::assertOtherUser($result['users'][0]);
1496 * Test the api_search() function.
1500 public function testApiSearch()
1502 $_REQUEST['q'] = 'reply';
1503 $_REQUEST['max_id'] = 10;
1504 $result = api_search('json');
1505 foreach ($result['status'] as $status) {
1506 self::assertStatus($status);
1507 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1512 * Test the api_search() function a count parameter.
1516 public function testApiSearchWithCount()
1518 $_REQUEST['q'] = 'reply';
1519 $_REQUEST['count'] = 20;
1520 $result = api_search('json');
1521 foreach ($result['status'] as $status) {
1522 self::assertStatus($status);
1523 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1528 * Test the api_search() function with an rpp parameter.
1532 public function testApiSearchWithRpp()
1534 $_REQUEST['q'] = 'reply';
1535 $_REQUEST['rpp'] = 20;
1536 $result = api_search('json');
1537 foreach ($result['status'] as $status) {
1538 self::assertStatus($status);
1539 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1544 * Test the api_search() function with an q parameter contains hashtag.
1545 * @doesNotPerformAssertions
1547 public function testApiSearchWithHashtag()
1549 $_REQUEST['q'] = '%23friendica';
1550 $result = api_search('json');
1551 foreach ($result['status'] as $status) {
1552 self::assertStatus($status);
1553 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1558 * Test the api_search() function with an exclude_replies parameter.
1559 * @doesNotPerformAssertions
1561 public function testApiSearchWithExcludeReplies()
1563 $_REQUEST['max_id'] = 10;
1564 $_REQUEST['exclude_replies'] = true;
1565 $_REQUEST['q'] = 'friendica';
1566 $result = api_search('json');
1567 foreach ($result['status'] as $status) {
1568 self::assertStatus($status);
1573 * Test the api_search() function without an authenticated user.
1577 public function testApiSearchWithUnallowedUser()
1579 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1580 $_SESSION['allow_api'] = false;
1581 $_GET['screen_name'] = $this->selfUser['nick'];
1586 * Test the api_search() function without any GET query parameter.
1590 public function testApiSearchWithoutQuery()
1592 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1597 * Test the api_statuses_home_timeline() function.
1601 public function testApiStatusesHomeTimeline()
1603 $_REQUEST['max_id'] = 10;
1604 $_REQUEST['exclude_replies'] = true;
1605 $_REQUEST['conversation_id'] = 1;
1606 $result = api_statuses_home_timeline('json');
1607 self::assertNotEmpty($result['status']);
1608 foreach ($result['status'] as $status) {
1609 self::assertStatus($status);
1614 * Test the api_statuses_home_timeline() function with a negative page parameter.
1618 public function testApiStatusesHomeTimelineWithNegativePage()
1620 $_REQUEST['page'] = -2;
1621 $result = api_statuses_home_timeline('json');
1622 self::assertNotEmpty($result['status']);
1623 foreach ($result['status'] as $status) {
1624 self::assertStatus($status);
1629 * Test the api_statuses_home_timeline() with an unallowed user.
1633 public function testApiStatusesHomeTimelineWithUnallowedUser()
1635 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1636 $_SESSION['allow_api'] = false;
1637 $_GET['screen_name'] = $this->selfUser['nick'];
1638 api_statuses_home_timeline('json');
1642 * Test the api_statuses_home_timeline() function with an RSS result.
1646 public function testApiStatusesHomeTimelineWithRss()
1648 $result = api_statuses_home_timeline('rss');
1649 self::assertXml($result, 'statuses');
1653 * Test the api_statuses_public_timeline() function.
1657 public function testApiStatusesPublicTimeline()
1659 $_REQUEST['max_id'] = 10;
1660 $_REQUEST['conversation_id'] = 1;
1661 $result = api_statuses_public_timeline('json');
1662 self::assertNotEmpty($result['status']);
1663 foreach ($result['status'] as $status) {
1664 self::assertStatus($status);
1669 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1673 public function testApiStatusesPublicTimelineWithExcludeReplies()
1675 $_REQUEST['max_id'] = 10;
1676 $_REQUEST['exclude_replies'] = true;
1677 $result = api_statuses_public_timeline('json');
1678 self::assertNotEmpty($result['status']);
1679 foreach ($result['status'] as $status) {
1680 self::assertStatus($status);
1685 * Test the api_statuses_public_timeline() function with a negative page parameter.
1689 public function testApiStatusesPublicTimelineWithNegativePage()
1691 $_REQUEST['page'] = -2;
1692 $result = api_statuses_public_timeline('json');
1693 self::assertNotEmpty($result['status']);
1694 foreach ($result['status'] as $status) {
1695 self::assertStatus($status);
1700 * Test the api_statuses_public_timeline() function with an unallowed user.
1704 public function testApiStatusesPublicTimelineWithUnallowedUser()
1706 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1707 $_SESSION['allow_api'] = false;
1708 $_GET['screen_name'] = $this->selfUser['nick'];
1709 api_statuses_public_timeline('json');
1713 * Test the api_statuses_public_timeline() function with an RSS result.
1717 public function testApiStatusesPublicTimelineWithRss()
1719 $result = api_statuses_public_timeline('rss');
1720 self::assertXml($result, 'statuses');
1724 * Test the api_statuses_networkpublic_timeline() function.
1728 public function testApiStatusesNetworkpublicTimeline()
1730 $_REQUEST['max_id'] = 10;
1731 $result = api_statuses_networkpublic_timeline('json');
1732 self::assertNotEmpty($result['status']);
1733 foreach ($result['status'] as $status) {
1734 self::assertStatus($status);
1739 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1743 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1745 $_REQUEST['page'] = -2;
1746 $result = api_statuses_networkpublic_timeline('json');
1747 self::assertNotEmpty($result['status']);
1748 foreach ($result['status'] as $status) {
1749 self::assertStatus($status);
1754 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1758 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1760 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1761 $_SESSION['allow_api'] = false;
1762 $_GET['screen_name'] = $this->selfUser['nick'];
1763 api_statuses_networkpublic_timeline('json');
1767 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1771 public function testApiStatusesNetworkpublicTimelineWithRss()
1773 $result = api_statuses_networkpublic_timeline('rss');
1774 self::assertXml($result, 'statuses');
1778 * Test the api_statuses_show() function.
1782 public function testApiStatusesShow()
1784 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1785 api_statuses_show('json');
1789 * Test the api_statuses_show() function with an ID.
1793 public function testApiStatusesShowWithId()
1795 DI::args()->setArgv(['', '', '', 1]);
1796 $result = api_statuses_show('json');
1797 self::assertStatus($result['status']);
1801 * Test the api_statuses_show() function with the conversation parameter.
1805 public function testApiStatusesShowWithConversation()
1807 DI::args()->setArgv(['', '', '', 1]);
1808 $_REQUEST['conversation'] = 1;
1809 $result = api_statuses_show('json');
1810 self::assertNotEmpty($result['status']);
1811 foreach ($result['status'] as $status) {
1812 self::assertStatus($status);
1817 * Test the api_statuses_show() function with an unallowed user.
1821 public function testApiStatusesShowWithUnallowedUser()
1823 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1824 $_SESSION['allow_api'] = false;
1825 $_GET['screen_name'] = $this->selfUser['nick'];
1826 api_statuses_show('json');
1830 * Test the api_conversation_show() function.
1834 public function testApiConversationShow()
1836 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1837 api_conversation_show('json');
1841 * Test the api_conversation_show() function with an ID.
1845 public function testApiConversationShowWithId()
1847 DI::args()->setArgv(['', '', '', 1]);
1848 $_REQUEST['max_id'] = 10;
1849 $_REQUEST['page'] = -2;
1850 $result = api_conversation_show('json');
1851 self::assertNotEmpty($result['status']);
1852 foreach ($result['status'] as $status) {
1853 self::assertStatus($status);
1858 * Test the api_conversation_show() function with an unallowed user.
1862 public function testApiConversationShowWithUnallowedUser()
1864 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1865 $_SESSION['allow_api'] = false;
1866 $_GET['screen_name'] = $this->selfUser['nick'];
1867 api_conversation_show('json');
1871 * Test the api_statuses_repeat() function.
1875 public function testApiStatusesRepeat()
1877 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1878 api_statuses_repeat('json');
1882 * Test the api_statuses_repeat() function without an authenticated user.
1886 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1888 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1889 $_SESSION['authenticated'] = false;
1890 api_statuses_repeat('json');
1894 * Test the api_statuses_repeat() function with an ID.
1898 public function testApiStatusesRepeatWithId()
1900 DI::args()->setArgv(['', '', '', 1]);
1901 $result = api_statuses_repeat('json');
1902 self::assertStatus($result['status']);
1904 // Also test with a shared status
1905 DI::args()->setArgv(['', '', '', 5]);
1906 $result = api_statuses_repeat('json');
1907 self::assertStatus($result['status']);
1911 * Test the api_statuses_destroy() function.
1915 public function testApiStatusesDestroy()
1917 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1918 api_statuses_destroy('json');
1922 * Test the api_statuses_destroy() function without an authenticated user.
1926 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1928 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1929 $_SESSION['authenticated'] = false;
1930 api_statuses_destroy('json');
1934 * Test the api_statuses_destroy() function with an ID.
1938 public function testApiStatusesDestroyWithId()
1940 DI::args()->setArgv(['', '', '', 1]);
1941 $result = api_statuses_destroy('json');
1942 self::assertStatus($result['status']);
1946 * Test the api_statuses_mentions() function.
1950 public function testApiStatusesMentions()
1952 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1953 $_REQUEST['max_id'] = 10;
1954 $result = api_statuses_mentions('json');
1955 self::assertEmpty($result['status']);
1956 // We should test with mentions in the database.
1960 * Test the api_statuses_mentions() function with a negative page parameter.
1964 public function testApiStatusesMentionsWithNegativePage()
1966 $_REQUEST['page'] = -2;
1967 $result = api_statuses_mentions('json');
1968 self::assertEmpty($result['status']);
1972 * Test the api_statuses_mentions() function with an unallowed user.
1976 public function testApiStatusesMentionsWithUnallowedUser()
1978 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1979 $_SESSION['allow_api'] = false;
1980 $_GET['screen_name'] = $this->selfUser['nick'];
1981 api_statuses_mentions('json');
1985 * Test the api_statuses_mentions() function with an RSS result.
1989 public function testApiStatusesMentionsWithRss()
1991 $result = api_statuses_mentions('rss');
1992 self::assertXml($result, 'statuses');
1996 * Test the api_statuses_user_timeline() function.
2000 public function testApiStatusesUserTimeline()
2002 $_REQUEST['max_id'] = 10;
2003 $_REQUEST['exclude_replies'] = true;
2004 $_REQUEST['conversation_id'] = 1;
2005 $result = api_statuses_user_timeline('json');
2006 self::assertNotEmpty($result['status']);
2007 foreach ($result['status'] as $status) {
2008 self::assertStatus($status);
2013 * Test the api_statuses_user_timeline() function with a negative page parameter.
2017 public function testApiStatusesUserTimelineWithNegativePage()
2019 $_REQUEST['page'] = -2;
2020 $result = api_statuses_user_timeline('json');
2021 self::assertNotEmpty($result['status']);
2022 foreach ($result['status'] as $status) {
2023 self::assertStatus($status);
2028 * Test the api_statuses_user_timeline() function with an RSS result.
2032 public function testApiStatusesUserTimelineWithRss()
2034 $result = api_statuses_user_timeline('rss');
2035 self::assertXml($result, 'statuses');
2039 * Test the api_statuses_user_timeline() function with an unallowed user.
2043 public function testApiStatusesUserTimelineWithUnallowedUser()
2045 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2046 $_SESSION['allow_api'] = false;
2047 $_GET['screen_name'] = $this->selfUser['nick'];
2048 api_statuses_user_timeline('json');
2052 * Test the api_favorites_create_destroy() function.
2056 public function testApiFavoritesCreateDestroy()
2058 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2059 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2060 api_favorites_create_destroy('json');
2064 * Test the api_favorites_create_destroy() function with an invalid ID.
2068 public function testApiFavoritesCreateDestroyWithInvalidId()
2070 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2071 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2072 api_favorites_create_destroy('json');
2076 * Test the api_favorites_create_destroy() function with an invalid action.
2080 public function testApiFavoritesCreateDestroyWithInvalidAction()
2082 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2083 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2084 $_REQUEST['id'] = 1;
2085 api_favorites_create_destroy('json');
2089 * Test the api_favorites_create_destroy() function with the create action.
2093 public function testApiFavoritesCreateDestroyWithCreateAction()
2095 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2096 $_REQUEST['id'] = 3;
2097 $result = api_favorites_create_destroy('json');
2098 self::assertStatus($result['status']);
2102 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2106 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2108 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2109 $_REQUEST['id'] = 3;
2110 $result = api_favorites_create_destroy('rss');
2111 self::assertXml($result, 'status');
2115 * Test the api_favorites_create_destroy() function with the destroy action.
2119 public function testApiFavoritesCreateDestroyWithDestroyAction()
2121 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2122 $_REQUEST['id'] = 3;
2123 $result = api_favorites_create_destroy('json');
2124 self::assertStatus($result['status']);
2128 * Test the api_favorites_create_destroy() function without an authenticated user.
2132 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2134 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2135 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2136 $_SESSION['authenticated'] = false;
2137 api_favorites_create_destroy('json');
2141 * Test the api_favorites() function.
2145 public function testApiFavorites()
2147 $_REQUEST['page'] = -1;
2148 $_REQUEST['max_id'] = 10;
2149 $result = api_favorites('json');
2150 foreach ($result['status'] as $status) {
2151 self::assertStatus($status);
2156 * Test the api_favorites() function with an RSS result.
2160 public function testApiFavoritesWithRss()
2162 $result = api_favorites('rss');
2163 self::assertXml($result, 'statuses');
2167 * Test the api_favorites() function with an unallowed user.
2171 public function testApiFavoritesWithUnallowedUser()
2173 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2174 $_SESSION['allow_api'] = false;
2175 $_GET['screen_name'] = $this->selfUser['nick'];
2176 api_favorites('json');
2180 * Test the api_format_messages() function.
2184 public function testApiFormatMessages()
2186 $result = api_format_messages(
2187 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2188 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2189 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2191 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2192 self::assertEquals(1, $result['id']);
2193 self::assertEquals(2, $result['recipient_id']);
2194 self::assertEquals(3, $result['sender_id']);
2195 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2196 self::assertEquals('sender_name', $result['sender_screen_name']);
2200 * Test the api_format_messages() function with HTML.
2204 public function testApiFormatMessagesWithHtmlText()
2206 $_GET['getText'] = 'html';
2207 $result = api_format_messages(
2208 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2209 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2210 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2212 self::assertEquals('item_title', $result['title']);
2213 self::assertEquals('<strong>item_body</strong>', $result['text']);
2217 * Test the api_format_messages() function with plain text.
2221 public function testApiFormatMessagesWithPlainText()
2223 $_GET['getText'] = 'plain';
2224 $result = api_format_messages(
2225 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2226 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2227 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2229 self::assertEquals('item_title', $result['title']);
2230 self::assertEquals('item_body', $result['text']);
2234 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2238 public function testApiFormatMessagesWithoutUserObjects()
2240 $_GET['getUserObjects'] = 'false';
2241 $result = api_format_messages(
2242 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2243 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2244 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2246 self::assertTrue(!isset($result['sender']));
2247 self::assertTrue(!isset($result['recipient']));
2251 * Test the api_convert_item() function.
2255 public function testApiConvertItem()
2257 $result = api_convert_item(
2259 'network' => 'feed',
2260 'title' => 'item_title',
2262 // We need a long string to test that it is correctly cut
2263 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2264 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2265 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2266 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2267 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2268 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2269 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2270 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2271 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2272 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2273 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2274 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2275 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2276 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2277 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2278 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2279 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2280 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2281 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2282 'repellat officia illum quos impedit quam iste esse unde qui ' .
2283 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2284 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2285 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2286 'plink' => 'item_plink'
2289 self::assertStringStartsWith('item_title', $result['text']);
2290 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2294 * Test the api_convert_item() function with an empty item body.
2298 public function testApiConvertItemWithoutBody()
2300 $result = api_convert_item(
2302 'network' => 'feed',
2303 'title' => 'item_title',
2306 'plink' => 'item_plink'
2309 self::assertEquals("item_title", $result['text']);
2310 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2314 * Test the api_convert_item() function with the title in the body.
2318 public function testApiConvertItemWithTitleInBody()
2320 $result = api_convert_item(
2322 'title' => 'item_title',
2323 'body' => 'item_title item_body',
2327 self::assertEquals('item_title item_body', $result['text']);
2328 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2332 * Test the api_get_attachments() function.
2336 public function testApiGetAttachments()
2339 self::assertEmpty(api_get_attachments($body, 0));
2343 * Test the api_get_attachments() function with an img tag.
2347 public function testApiGetAttachmentsWithImage()
2349 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2350 self::assertIsArray(api_get_attachments($body, 0));
2354 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2358 public function testApiGetAttachmentsWithImageAndAndStatus()
2360 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2361 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2362 self::assertIsArray(api_get_attachments($body, 0));
2366 * Test the api_get_entitities() function.
2370 public function testApiGetEntitities()
2373 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2377 * Test the api_get_entitities() function with the include_entities parameter.
2381 public function testApiGetEntititiesWithIncludeEntities()
2383 $_REQUEST['include_entities'] = 'true';
2385 $result = api_get_entitities($text, 'bbcode', 0);
2386 self::assertIsArray($result['hashtags']);
2387 self::assertIsArray($result['symbols']);
2388 self::assertIsArray($result['urls']);
2389 self::assertIsArray($result['user_mentions']);
2393 * Test the api_format_items_embeded_images() function.
2397 public function testApiFormatItemsEmbededImages()
2400 'text ' . DI::baseUrl() . '/display/item_guid',
2401 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2406 * Test the api_contactlink_to_array() function.
2410 public function testApiContactlinkToArray()
2417 api_contactlink_to_array('text')
2422 * Test the api_contactlink_to_array() function with an URL.
2426 public function testApiContactlinkToArrayWithUrl()
2430 'name' => ['link_text'],
2433 api_contactlink_to_array('text <a href="url">link_text</a>')
2438 * Test the api_format_items_activities() function.
2442 public function testApiFormatItemsActivities()
2444 $item = ['uid' => 0, 'uri' => ''];
2445 $result = api_format_items_activities($item);
2446 self::assertArrayHasKey('like', $result);
2447 self::assertArrayHasKey('dislike', $result);
2448 self::assertArrayHasKey('attendyes', $result);
2449 self::assertArrayHasKey('attendno', $result);
2450 self::assertArrayHasKey('attendmaybe', $result);
2454 * Test the api_format_items_activities() function with an XML result.
2458 public function testApiFormatItemsActivitiesWithXml()
2460 $item = ['uid' => 0, 'uri' => ''];
2461 $result = api_format_items_activities($item, 'xml');
2462 self::assertArrayHasKey('friendica:like', $result);
2463 self::assertArrayHasKey('friendica:dislike', $result);
2464 self::assertArrayHasKey('friendica:attendyes', $result);
2465 self::assertArrayHasKey('friendica:attendno', $result);
2466 self::assertArrayHasKey('friendica:attendmaybe', $result);
2470 * Test the api_format_items() function.
2471 * @doesNotPerformAssertions
2473 public function testApiFormatItems()
2477 'item_network' => 'item_network',
2483 'author-network' => Protocol::DFRN,
2484 'author-link' => 'http://localhost/profile/othercontact',
2488 $result = api_format_items($items, ['id' => 0], true);
2489 foreach ($result as $status) {
2490 self::assertStatus($status);
2495 * Test the api_format_items() function with an XML result.
2496 * @doesNotPerformAssertions
2498 public function testApiFormatItemsWithXml()
2506 'author-network' => Protocol::DFRN,
2507 'author-link' => 'http://localhost/profile/othercontact',
2511 $result = api_format_items($items, ['id' => 0], true, 'xml');
2512 foreach ($result as $status) {
2513 self::assertStatus($status);
2518 * Test the api_format_items() function.
2522 public function testApiAccountRateLimitStatus()
2524 $result = api_account_rate_limit_status('json');
2525 self::assertEquals(150, $result['hash']['remaining_hits']);
2526 self::assertEquals(150, $result['hash']['hourly_limit']);
2527 self::assertIsInt($result['hash']['reset_time_in_seconds']);
2531 * Test the api_format_items() function with an XML result.
2535 public function testApiAccountRateLimitStatusWithXml()
2537 $result = api_account_rate_limit_status('xml');
2538 self::assertXml($result, 'hash');
2542 * Test the api_help_test() function.
2546 public function testApiHelpTest()
2548 $result = api_help_test('json');
2549 self::assertEquals(['ok' => 'ok'], $result);
2553 * Test the api_help_test() function with an XML result.
2557 public function testApiHelpTestWithXml()
2559 $result = api_help_test('xml');
2560 self::assertXml($result, 'ok');
2564 * Test the api_lists_list() function.
2568 public function testApiListsList()
2570 $result = api_lists_list('json');
2571 self::assertEquals(['lists_list' => []], $result);
2575 * Test the api_lists_ownerships() function.
2579 public function testApiListsOwnerships()
2581 $result = api_lists_ownerships('json');
2582 foreach ($result['lists']['lists'] as $list) {
2583 self::assertList($list);
2588 * Test the api_lists_ownerships() function without an authenticated user.
2592 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2594 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2595 $_SESSION['authenticated'] = false;
2596 api_lists_ownerships('json');
2600 * Test the api_lists_statuses() function.
2604 public function testApiListsStatuses()
2606 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2607 api_lists_statuses('json');
2611 * Test the api_lists_statuses() function with a list ID.
2612 * @doesNotPerformAssertions
2614 public function testApiListsStatusesWithListId()
2616 $_REQUEST['list_id'] = 1;
2617 $_REQUEST['page'] = -1;
2618 $_REQUEST['max_id'] = 10;
2619 $result = api_lists_statuses('json');
2620 foreach ($result['status'] as $status) {
2621 self::assertStatus($status);
2626 * Test the api_lists_statuses() function with a list ID and a RSS result.
2630 public function testApiListsStatusesWithListIdAndRss()
2632 $_REQUEST['list_id'] = 1;
2633 $result = api_lists_statuses('rss');
2634 self::assertXml($result, 'statuses');
2638 * Test the api_lists_statuses() function with an unallowed user.
2642 public function testApiListsStatusesWithUnallowedUser()
2644 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2645 $_SESSION['allow_api'] = false;
2646 $_GET['screen_name'] = $this->selfUser['nick'];
2647 api_lists_statuses('json');
2651 * Test the api_statuses_f() function.
2655 public function testApiStatusesFWithFriends()
2658 $result = api_statuses_f('friends');
2659 self::assertArrayHasKey('user', $result);
2663 * Test the api_statuses_f() function.
2667 public function testApiStatusesFWithFollowers()
2669 $result = api_statuses_f('followers');
2670 self::assertArrayHasKey('user', $result);
2674 * Test the api_statuses_f() function.
2678 public function testApiStatusesFWithBlocks()
2680 $result = api_statuses_f('blocks');
2681 self::assertArrayHasKey('user', $result);
2685 * Test the api_statuses_f() function.
2689 public function testApiStatusesFWithIncoming()
2691 $result = api_statuses_f('incoming');
2692 self::assertArrayHasKey('user', $result);
2696 * Test the api_statuses_f() function an undefined cursor GET variable.
2700 public function testApiStatusesFWithUndefinedCursor()
2702 $_GET['cursor'] = 'undefined';
2703 self::assertFalse(api_statuses_f('friends'));
2707 * Test the api_statuses_friends() function.
2711 public function testApiStatusesFriends()
2713 $result = api_statuses_friends('json');
2714 self::assertArrayHasKey('user', $result);
2718 * Test the api_statuses_friends() function an undefined cursor GET variable.
2722 public function testApiStatusesFriendsWithUndefinedCursor()
2724 $_GET['cursor'] = 'undefined';
2725 self::assertFalse(api_statuses_friends('json'));
2729 * Test the api_statuses_followers() function.
2733 public function testApiStatusesFollowers()
2735 $result = api_statuses_followers('json');
2736 self::assertArrayHasKey('user', $result);
2740 * Test the api_statuses_followers() function an undefined cursor GET variable.
2744 public function testApiStatusesFollowersWithUndefinedCursor()
2746 $_GET['cursor'] = 'undefined';
2747 self::assertFalse(api_statuses_followers('json'));
2751 * Test the api_blocks_list() function.
2755 public function testApiBlocksList()
2757 $result = api_blocks_list('json');
2758 self::assertArrayHasKey('user', $result);
2762 * Test the api_blocks_list() function an undefined cursor GET variable.
2766 public function testApiBlocksListWithUndefinedCursor()
2768 $_GET['cursor'] = 'undefined';
2769 self::assertFalse(api_blocks_list('json'));
2773 * Test the api_friendships_incoming() function.
2777 public function testApiFriendshipsIncoming()
2779 $result = api_friendships_incoming('json');
2780 self::assertArrayHasKey('id', $result);
2784 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2788 public function testApiFriendshipsIncomingWithUndefinedCursor()
2790 $_GET['cursor'] = 'undefined';
2791 self::assertFalse(api_friendships_incoming('json'));
2795 * Test the api_statusnet_config() function.
2799 public function testApiStatusnetConfig()
2801 $result = api_statusnet_config('json');
2802 self::assertEquals('localhost', $result['config']['site']['server']);
2803 self::assertEquals('default', $result['config']['site']['theme']);
2804 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2805 self::assertTrue($result['config']['site']['fancy']);
2806 self::assertEquals('en', $result['config']['site']['language']);
2807 self::assertEquals('UTC', $result['config']['site']['timezone']);
2808 self::assertEquals(200000, $result['config']['site']['textlimit']);
2809 self::assertEquals('false', $result['config']['site']['private']);
2810 self::assertEquals('false', $result['config']['site']['ssl']);
2811 self::assertEquals(30, $result['config']['site']['shorturllength']);
2815 * Test the api_statusnet_version() function.
2819 public function testApiStatusnetVersion()
2821 $result = api_statusnet_version('json');
2822 self::assertEquals('0.9.7', $result['version']);
2826 * Test the api_direct_messages_new() function.
2830 public function testApiDirectMessagesNew()
2832 $result = api_direct_messages_new('json');
2833 self::assertNull($result);
2837 * Test the api_direct_messages_new() function without an authenticated user.
2841 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2843 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2844 $_SESSION['authenticated'] = false;
2845 api_direct_messages_new('json');
2849 * Test the api_direct_messages_new() function with an user ID.
2853 public function testApiDirectMessagesNewWithUserId()
2855 $_POST['text'] = 'message_text';
2856 $_POST['user_id'] = $this->otherUser['id'];
2857 $result = api_direct_messages_new('json');
2858 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2862 * Test the api_direct_messages_new() function with a screen name.
2866 public function testApiDirectMessagesNewWithScreenName()
2868 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2869 $_POST['text'] = 'message_text';
2870 $_POST['screen_name'] = $this->friendUser['nick'];
2871 $result = api_direct_messages_new('json');
2872 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2873 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2874 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2878 * Test the api_direct_messages_new() function with a title.
2882 public function testApiDirectMessagesNewWithTitle()
2884 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2885 $_POST['text'] = 'message_text';
2886 $_POST['screen_name'] = $this->friendUser['nick'];
2887 $_REQUEST['title'] = 'message_title';
2888 $result = api_direct_messages_new('json');
2889 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2890 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2891 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2892 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2896 * Test the api_direct_messages_new() function with an RSS result.
2900 public function testApiDirectMessagesNewWithRss()
2902 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2903 $_POST['text'] = 'message_text';
2904 $_POST['screen_name'] = $this->friendUser['nick'];
2905 $result = api_direct_messages_new('rss');
2906 self::assertXml($result, 'direct-messages');
2910 * Test the api_direct_messages_destroy() function.
2914 public function testApiDirectMessagesDestroy()
2916 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2917 api_direct_messages_destroy('json');
2921 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2925 public function testApiDirectMessagesDestroyWithVerbose()
2927 $_GET['friendica_verbose'] = 'true';
2928 $result = api_direct_messages_destroy('json');
2932 'result' => 'error',
2933 'message' => 'message id or parenturi not specified'
2941 * Test the api_direct_messages_destroy() function without an authenticated user.
2945 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2947 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2948 $_SESSION['authenticated'] = false;
2949 api_direct_messages_destroy('json');
2953 * Test the api_direct_messages_destroy() function with a non-zero ID.
2957 public function testApiDirectMessagesDestroyWithId()
2959 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2960 $_REQUEST['id'] = 1;
2961 api_direct_messages_destroy('json');
2965 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2969 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2971 $_REQUEST['id'] = 1;
2972 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2973 $_GET['friendica_verbose'] = 'true';
2974 $result = api_direct_messages_destroy('json');
2978 'result' => 'error',
2979 'message' => 'message id not in database'
2987 * Test the api_direct_messages_destroy() function with a non-zero ID.
2991 public function testApiDirectMessagesDestroyWithCorrectId()
2993 $this->markTestIncomplete('We need to add a dataset for this.');
2997 * Test the api_direct_messages_box() function.
3001 public function testApiDirectMessagesBoxWithSentbox()
3003 $_REQUEST['page'] = -1;
3004 $_REQUEST['max_id'] = 10;
3005 $result = api_direct_messages_box('json', 'sentbox', 'false');
3006 self::assertArrayHasKey('direct_message', $result);
3010 * Test the api_direct_messages_box() function.
3014 public function testApiDirectMessagesBoxWithConversation()
3016 $result = api_direct_messages_box('json', 'conversation', 'false');
3017 self::assertArrayHasKey('direct_message', $result);
3021 * Test the api_direct_messages_box() function.
3025 public function testApiDirectMessagesBoxWithAll()
3027 $result = api_direct_messages_box('json', 'all', 'false');
3028 self::assertArrayHasKey('direct_message', $result);
3032 * Test the api_direct_messages_box() function.
3036 public function testApiDirectMessagesBoxWithInbox()
3038 $result = api_direct_messages_box('json', 'inbox', 'false');
3039 self::assertArrayHasKey('direct_message', $result);
3043 * Test the api_direct_messages_box() function.
3047 public function testApiDirectMessagesBoxWithVerbose()
3049 $result = api_direct_messages_box('json', 'sentbox', 'true');
3053 'result' => 'error',
3054 'message' => 'no mails available'
3062 * Test the api_direct_messages_box() function with a RSS result.
3066 public function testApiDirectMessagesBoxWithRss()
3068 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3069 self::assertXml($result, 'direct-messages');
3073 * Test the api_direct_messages_box() function without an authenticated user.
3077 public function testApiDirectMessagesBoxWithUnallowedUser()
3079 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3080 $_SESSION['allow_api'] = false;
3081 $_GET['screen_name'] = $this->selfUser['nick'];
3082 api_direct_messages_box('json', 'sentbox', 'false');
3086 * Test the api_direct_messages_sentbox() function.
3090 public function testApiDirectMessagesSentbox()
3092 $result = api_direct_messages_sentbox('json');
3093 self::assertArrayHasKey('direct_message', $result);
3097 * Test the api_direct_messages_inbox() function.
3101 public function testApiDirectMessagesInbox()
3103 $result = api_direct_messages_inbox('json');
3104 self::assertArrayHasKey('direct_message', $result);
3108 * Test the api_direct_messages_all() function.
3112 public function testApiDirectMessagesAll()
3114 $result = api_direct_messages_all('json');
3115 self::assertArrayHasKey('direct_message', $result);
3119 * Test the api_direct_messages_conversation() function.
3123 public function testApiDirectMessagesConversation()
3125 $result = api_direct_messages_conversation('json');
3126 self::assertArrayHasKey('direct_message', $result);
3130 * Test the api_oauth_request_token() function.
3134 public function testApiOauthRequestToken()
3136 $this->markTestIncomplete('exit() kills phpunit as well');
3140 * Test the api_oauth_access_token() function.
3144 public function testApiOauthAccessToken()
3146 $this->markTestIncomplete('exit() kills phpunit as well');
3150 * Test the api_fr_photoalbum_delete() function.
3154 public function testApiFrPhotoalbumDelete()
3156 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3157 api_fr_photoalbum_delete('json');
3161 * Test the api_fr_photoalbum_delete() function with an album name.
3165 public function testApiFrPhotoalbumDeleteWithAlbum()
3167 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3168 $_REQUEST['album'] = 'album_name';
3169 api_fr_photoalbum_delete('json');
3173 * Test the api_fr_photoalbum_delete() function with an album name.
3177 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3179 $this->markTestIncomplete('We need to add a dataset for this.');
3183 * Test the api_fr_photoalbum_delete() function.
3187 public function testApiFrPhotoalbumUpdate()
3189 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3190 api_fr_photoalbum_update('json');
3194 * Test the api_fr_photoalbum_delete() function with an album name.
3198 public function testApiFrPhotoalbumUpdateWithAlbum()
3200 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3201 $_REQUEST['album'] = 'album_name';
3202 api_fr_photoalbum_update('json');
3206 * Test the api_fr_photoalbum_delete() function with an album name.
3210 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3212 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3213 $_REQUEST['album'] = 'album_name';
3214 $_REQUEST['album_new'] = 'album_name';
3215 api_fr_photoalbum_update('json');
3219 * Test the api_fr_photoalbum_update() function without an authenticated user.
3223 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3225 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3226 $_SESSION['authenticated'] = false;
3227 api_fr_photoalbum_update('json');
3231 * Test the api_fr_photoalbum_delete() function with an album name.
3235 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3237 $this->markTestIncomplete('We need to add a dataset for this.');
3241 * Test the api_fr_photos_list() function.
3245 public function testApiFrPhotosList()
3247 $result = api_fr_photos_list('json');
3248 self::assertArrayHasKey('photo', $result);
3252 * Test the api_fr_photos_list() function without an authenticated user.
3256 public function testApiFrPhotosListWithoutAuthenticatedUser()
3258 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3259 $_SESSION['authenticated'] = false;
3260 api_fr_photos_list('json');
3264 * Test the api_fr_photo_create_update() function.
3266 public function testApiFrPhotoCreateUpdate()
3268 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3269 api_fr_photo_create_update('json');
3273 * Test the api_fr_photo_create_update() function without an authenticated user.
3277 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3279 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3280 $_SESSION['authenticated'] = false;
3281 api_fr_photo_create_update('json');
3285 * Test the api_fr_photo_create_update() function with an album name.
3289 public function testApiFrPhotoCreateUpdateWithAlbum()
3291 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3292 $_REQUEST['album'] = 'album_name';
3293 api_fr_photo_create_update('json');
3297 * Test the api_fr_photo_create_update() function with the update mode.
3301 public function testApiFrPhotoCreateUpdateWithUpdate()
3303 $this->markTestIncomplete('We need to create a dataset for this');
3307 * Test the api_fr_photo_create_update() function with an uploaded file.
3311 public function testApiFrPhotoCreateUpdateWithFile()
3313 $this->markTestIncomplete();
3317 * Test the api_fr_photo_delete() function.
3321 public function testApiFrPhotoDelete()
3323 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3324 api_fr_photo_delete('json');
3328 * Test the api_fr_photo_delete() function without an authenticated user.
3332 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3334 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3335 $_SESSION['authenticated'] = false;
3336 api_fr_photo_delete('json');
3340 * Test the api_fr_photo_delete() function with a photo ID.
3344 public function testApiFrPhotoDeleteWithPhotoId()
3346 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3347 $_REQUEST['photo_id'] = 1;
3348 api_fr_photo_delete('json');
3352 * Test the api_fr_photo_delete() function with a correct photo ID.
3356 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3358 $this->markTestIncomplete('We need to create a dataset for this.');
3362 * Test the api_fr_photo_detail() function.
3366 public function testApiFrPhotoDetail()
3368 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3369 api_fr_photo_detail('json');
3373 * Test the api_fr_photo_detail() function without an authenticated user.
3377 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3379 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3380 $_SESSION['authenticated'] = false;
3381 api_fr_photo_detail('json');
3385 * Test the api_fr_photo_detail() function with a photo ID.
3389 public function testApiFrPhotoDetailWithPhotoId()
3391 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3392 $_REQUEST['photo_id'] = 1;
3393 api_fr_photo_detail('json');
3397 * Test the api_fr_photo_detail() function with a correct photo ID.
3401 public function testApiFrPhotoDetailCorrectPhotoId()
3403 $this->markTestIncomplete('We need to create a dataset for this.');
3407 * Test the api_account_update_profile_image() function.
3411 public function testApiAccountUpdateProfileImage()
3413 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3414 api_account_update_profile_image('json');
3418 * Test the api_account_update_profile_image() function without an authenticated user.
3422 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3424 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3425 $_SESSION['authenticated'] = false;
3426 api_account_update_profile_image('json');
3430 * Test the api_account_update_profile_image() function with an uploaded file.
3434 public function testApiAccountUpdateProfileImageWithUpload()
3436 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3437 $this->markTestIncomplete();
3442 * Test the api_account_update_profile() function.
3446 public function testApiAccountUpdateProfile()
3448 $_POST['name'] = 'new_name';
3449 $_POST['description'] = 'new_description';
3450 $result = api_account_update_profile('json');
3451 // We can't use assertSelfUser() here because the user object is missing some properties.
3452 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3453 self::assertEquals('DFRN', $result['user']['location']);
3454 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3455 self::assertEquals('dfrn', $result['user']['network']);
3456 self::assertEquals('new_name', $result['user']['name']);
3457 self::assertEquals('new_description', $result['user']['description']);
3461 * Test the check_acl_input() function.
3465 public function testCheckAclInput()
3467 $result = check_acl_input('<aclstring>');
3468 // Where does this result come from?
3469 self::assertEquals(1, $result);
3473 * Test the check_acl_input() function with an empty ACL string.
3477 public function testCheckAclInputWithEmptyAclString()
3479 $result = check_acl_input(' ');
3480 self::assertFalse($result);
3484 * Test the save_media_to_database() function.
3488 public function testSaveMediaToDatabase()
3490 $this->markTestIncomplete();
3494 * Test the post_photo_item() function.
3498 public function testPostPhotoItem()
3500 $this->markTestIncomplete();
3504 * Test the prepare_photo_data() function.
3508 public function testPreparePhotoData()
3510 $this->markTestIncomplete();
3514 * Test the api_share_as_retweet() function.
3518 public function testApiShareAsRetweet()
3520 $item = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3521 $result = api_share_as_retweet($item);
3522 self::assertFalse($result);
3526 * Test the api_share_as_retweet() function with a valid item.
3530 public function testApiShareAsRetweetWithValidItem()
3532 $this->markTestIncomplete();
3536 * Test the api_in_reply_to() function.
3540 public function testApiInReplyTo()
3542 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3543 self::assertArrayHasKey('status_id', $result);
3544 self::assertArrayHasKey('user_id', $result);
3545 self::assertArrayHasKey('status_id_str', $result);
3546 self::assertArrayHasKey('user_id_str', $result);
3547 self::assertArrayHasKey('screen_name', $result);
3551 * Test the api_in_reply_to() function with a valid item.
3555 public function testApiInReplyToWithValidItem()
3557 $this->markTestIncomplete();
3561 * Test the api_clean_plain_items() function.
3565 public function testApiCleanPlainItems()
3567 $_REQUEST['include_entities'] = 'true';
3568 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3569 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3573 * Test the api_best_nickname() function.
3577 public function testApiBestNickname()
3580 $result = api_best_nickname($contacts);
3581 self::assertNull($result);
3585 * Test the api_best_nickname() function with contacts.
3589 public function testApiBestNicknameWithContacts()
3591 $this->markTestIncomplete();
3595 * Test the api_friendica_group_show() function.
3599 public function testApiFriendicaGroupShow()
3601 $this->markTestIncomplete();
3605 * Test the api_friendica_group_delete() function.
3609 public function testApiFriendicaGroupDelete()
3611 $this->markTestIncomplete();
3615 * Test the api_lists_destroy() function.
3619 public function testApiListsDestroy()
3621 $this->markTestIncomplete();
3625 * Test the group_create() function.
3629 public function testGroupCreate()
3631 $this->markTestIncomplete();
3635 * Test the api_friendica_group_create() function.
3639 public function testApiFriendicaGroupCreate()
3641 $this->markTestIncomplete();
3645 * Test the api_lists_create() function.
3649 public function testApiListsCreate()
3651 $this->markTestIncomplete();
3655 * Test the api_friendica_group_update() function.
3659 public function testApiFriendicaGroupUpdate()
3661 $this->markTestIncomplete();
3665 * Test the api_lists_update() function.
3669 public function testApiListsUpdate()
3671 $this->markTestIncomplete();
3675 * Test the api_friendica_activity() function.
3679 public function testApiFriendicaActivity()
3681 $this->markTestIncomplete();
3685 * Test the api_friendica_notification() function.
3689 public function testApiFriendicaNotification()
3691 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3692 api_friendica_notification('json');
3696 * Test the api_friendica_notification() function without an authenticated user.
3700 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3702 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3703 $_SESSION['authenticated'] = false;
3704 api_friendica_notification('json');
3708 * Test the api_friendica_notification() function with empty result
3712 public function testApiFriendicaNotificationWithEmptyResult()
3714 DI::args()->setArgv(['api', 'friendica', 'notification']);
3715 $_SESSION['uid'] = 41;
3716 $result = api_friendica_notification('json');
3717 self::assertEquals(['note' => false], $result);
3721 * Test the api_friendica_notification() function with an XML result.
3725 public function testApiFriendicaNotificationWithXmlResult()
3727 DI::args()->setArgv(['api', 'friendica', 'notification']);
3728 $result = api_friendica_notification('xml');
3729 $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02');
3731 <?xml version="1.0"?>
3733 <note id="1" hash="" type="8" name="Reply to" url="http://localhost/display/1" photo="http://localhost/" date="2020-01-01 12:12:02" msg="A test reply from an item" uid="42" uri-id="" link="http://localhost/notification/1" iid="4" parent="" parent-uri-id="" seen="0" verb="" otype="item" name_cache="Reply to" msg_cache="A test reply from an item" timestamp="1577880722" date_rel="{$dateRel}" msg_html="A test reply from an item" msg_plain="A test reply from an item"/>
3736 self::assertXmlStringEqualsXmlString($assertXml, $result);
3740 * Test the api_friendica_notification() function with an JSON result.
3744 public function testApiFriendicaNotificationWithJsonResult()
3746 DI::args()->setArgv(['api', 'friendica', 'notification']);
3747 $result = json_encode(api_friendica_notification('json'));
3748 self::assertJson($result);
3752 * Test the api_friendica_notification_seen() function.
3756 public function testApiFriendicaNotificationSeen()
3758 $this->markTestIncomplete();
3762 * Test the api_friendica_direct_messages_setseen() function.
3766 public function testApiFriendicaDirectMessagesSetseen()
3768 $this->markTestIncomplete();
3772 * Test the api_friendica_direct_messages_search() function.
3776 public function testApiFriendicaDirectMessagesSearch()
3778 $this->markTestIncomplete();
3782 * Test the api_saved_searches_list() function.
3786 public function testApiSavedSearchesList()
3788 $result = api_saved_searches_list('json');
3789 self::assertEquals(1, $result['terms'][0]['id']);
3790 self::assertEquals(1, $result['terms'][0]['id_str']);
3791 self::assertEquals('Saved search', $result['terms'][0]['name']);
3792 self::assertEquals('Saved search', $result['terms'][0]['query']);