6 namespace Friendica\Test\legacy;
9 use Friendica\Core\Config\Capability\IManageConfigValues;
10 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
11 use Friendica\Core\Protocol;
13 use Friendica\Module\BaseApi;
14 use Friendica\Network\HTTPException;
15 use Friendica\Security\BasicAuth;
16 use Friendica\Test\FixtureTest;
17 use Friendica\Util\Arrays;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
20 use Monolog\Handler\TestHandler;
22 require_once __DIR__ . '/../../include/api.php';
25 * Tests for the API functions.
27 * Functions that use header() need to be tested in a separate process.
28 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
30 * @backupGlobals enabled
32 class ApiTest extends FixtureTest
35 * @var TestHandler Can handle log-outputs
42 protected $friendUser;
46 protected $wrongUserId;
51 /** @var IManageConfigValues */
55 * Create variables used by tests.
57 protected function setUp() : void
59 global $API, $called_api;
65 /** @var IManageConfigValues $config */
66 $this->config = $this->dice->create(IManageConfigValues::class);
68 $this->config->set('system', 'url', 'http://localhost');
69 $this->config->set('system', 'hostname', 'localhost');
70 $this->config->set('system', 'worker_dont_fork', true);
73 $this->config->set('config', 'hostname', 'localhost');
74 $this->config->set('system', 'throttle_limit_day', 100);
75 $this->config->set('system', 'throttle_limit_week', 100);
76 $this->config->set('system', 'throttle_limit_month', 100);
77 $this->config->set('system', 'theme', 'system_theme');
81 $this->app = DI::app();
83 DI::args()->setArgc(1);
85 // User data that the test database is populated with
88 'name' => 'Self contact',
89 'nick' => 'selfcontact',
90 'nurl' => 'http://localhost/profile/selfcontact'
94 'name' => 'Friend contact',
95 'nick' => 'friendcontact',
96 'nurl' => 'http://localhost/profile/friendcontact'
100 'name' => 'othercontact',
101 'nick' => 'othercontact',
102 'nurl' => 'http://localhost/profile/othercontact'
105 // User ID that we know is not in the database
106 $this->wrongUserId = 666;
108 DI::session()->start();
110 // Most API require login so we force the session
113 'authenticated' => true,
114 'uid' => $this->selfUser['id']
119 * Assert that an user array contains expected keys.
121 * @param array $user User array
125 private function assertSelfUser(array $user)
127 self::assertEquals($this->selfUser['id'], $user['uid']);
128 self::assertEquals($this->selfUser['id'], $user['cid']);
129 self::assertEquals(1, $user['self']);
130 self::assertEquals('DFRN', $user['location']);
131 self::assertEquals($this->selfUser['name'], $user['name']);
132 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
133 self::assertEquals('dfrn', $user['network']);
134 self::assertTrue($user['verified']);
138 * Assert that an user array contains expected keys.
140 * @param array $user User array
144 private function assertOtherUser(array $user = [])
146 self::assertEquals($this->otherUser['id'], $user['id']);
147 self::assertEquals($this->otherUser['id'], $user['id_str']);
148 self::assertEquals(0, $user['self']);
149 self::assertEquals($this->otherUser['name'], $user['name']);
150 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
151 self::assertFalse($user['verified']);
155 * Assert that a status array contains expected keys.
157 * @param array $status Status array
161 private function assertStatus(array $status = [])
163 self::assertIsString($status['text'] ?? '');
164 self::assertIsInt($status['id'] ?? '');
165 // We could probably do more checks here.
169 * Assert that a list array contains expected keys.
171 * @param array $list List array
175 private function assertList(array $list = [])
177 self::assertIsString($list['name']);
178 self::assertIsInt($list['id']);
179 self::assertIsString('string', $list['id_str']);
180 self::assertContains($list['mode'], ['public', 'private']);
181 // We could probably do more checks here.
185 * Assert that the string is XML and contain the root element.
187 * @param string $result XML string
188 * @param string $root_element Root element name
192 private function assertXml($result = '', $root_element = '')
194 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
195 self::assertStringContainsString('<' . $root_element, $result);
196 // We could probably do more checks here.
200 * Get the path to a temporary empty PNG image.
202 * @return string Path
204 private function getTempImage()
206 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
210 // Empty 1x1 px PNG image
211 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
219 * Test the api_user() function.
223 public function testApiUser()
225 self::assertEquals($this->selfUser['id'], api_user());
229 * Test the api_user() function with an unallowed user.
233 public function testApiUserWithUnallowedUser()
235 $_SESSION = ['allow_api' => false];
236 self::assertEquals(false, api_user());
240 * Test the api_source() function.
244 public function testApiSource()
246 self::assertEquals('api', api_source());
250 * Test the api_source() function with a Twidere user agent.
254 public function testApiSourceWithTwidere()
256 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
257 self::assertEquals('Twidere', api_source());
261 * Test the api_source() function with a GET parameter.
265 public function testApiSourceWithGet()
267 $_GET['source'] = 'source_name';
268 self::assertEquals('source_name', api_source());
272 * Test the api_date() function.
276 public function testApiDate()
278 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
282 * Test the api_register_func() function.
286 public function testApiRegisterFunc()
298 self::assertTrue($API['api_path']['auth']);
299 self::assertEquals('method', $API['api_path']['method']);
300 self::assertTrue(is_callable($API['api_path']['func']));
304 * Test the BasicAuth::getCurrentUserID() function without any login.
306 * @runInSeparateProcess
307 * @preserveGlobalState disabled
308 * @preserveGlobalState disabled
310 public function testApiLoginWithoutLogin()
312 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
313 BasicAuth::getCurrentUserID(true);
317 * Test the BasicAuth::getCurrentUserID() function with a bad login.
319 * @runInSeparateProcess
320 * @preserveGlobalState disabled
321 * @preserveGlobalState disabled
323 public function testApiLoginWithBadLogin()
325 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
326 $_SERVER['PHP_AUTH_USER'] = 'user@server';
327 BasicAuth::getCurrentUserID(true);
331 * Test the BasicAuth::getCurrentUserID() function with oAuth.
335 public function testApiLoginWithOauth()
337 $this->markTestIncomplete('Can we test this easily?');
341 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
345 public function testApiLoginWithAddonAuth()
347 $this->markTestIncomplete('Can we test this easily?');
351 * Test the BasicAuth::getCurrentUserID() function with a correct login.
353 * @runInSeparateProcess
354 * @preserveGlobalState disabled
355 * @doesNotPerformAssertions
357 public function testApiLoginWithCorrectLogin()
359 $_SERVER['PHP_AUTH_USER'] = 'Test user';
360 $_SERVER['PHP_AUTH_PW'] = 'password';
361 BasicAuth::getCurrentUserID(true);
365 * Test the BasicAuth::getCurrentUserID() function with a remote user.
367 * @runInSeparateProcess
368 * @preserveGlobalState disabled
370 public function testApiLoginWithRemoteUser()
372 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
373 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
374 BasicAuth::getCurrentUserID(true);
378 * Test the api_check_method() function.
382 public function testApiCheckMethod()
384 self::assertFalse(api_check_method('method'));
388 * Test the api_check_method() function with a correct method.
392 public function testApiCheckMethodWithCorrectMethod()
394 $_SERVER['REQUEST_METHOD'] = 'method';
395 self::assertTrue(api_check_method('method'));
399 * Test the api_check_method() function with a wildcard.
403 public function testApiCheckMethodWithWildcard()
405 self::assertTrue(api_check_method('*'));
409 * Test the api_call() function.
411 * @runInSeparateProcess
412 * @preserveGlobalState disabled
414 public function testApiCall()
418 'method' => 'method',
419 'func' => function () {
420 return ['data' => ['some_data']];
423 $_SERVER['REQUEST_METHOD'] = 'method';
424 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
425 $_GET['callback'] = 'callback_name';
427 $args = DI::args()->determine($_SERVER, $_GET);
430 'callback_name(["some_data"])',
431 api_call($this->app, $args)
436 * Test the api_call() function with the profiled enabled.
438 * @runInSeparateProcess
439 * @preserveGlobalState disabled
441 public function testApiCallWithProfiler()
445 'method' => 'method',
446 'func' => function () {
447 return ['data' => ['some_data']];
451 $_SERVER['REQUEST_METHOD'] = 'method';
452 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
454 $args = DI::args()->determine($_SERVER, $_GET);
456 $this->config->set('system', 'profiler', true);
457 $this->config->set('rendertime', 'callstack', true);
458 $this->app->callstack = [
459 'database' => ['some_function' => 200],
460 'database_write' => ['some_function' => 200],
461 'cache' => ['some_function' => 200],
462 'cache_write' => ['some_function' => 200],
463 'network' => ['some_function' => 200]
468 api_call($this->app, $args)
473 * Test the api_call() function without any result.
475 * @runInSeparateProcess
476 * @preserveGlobalState disabled
478 public function testApiCallWithNoResult()
480 // @todo How to test the new API?
484 'method' => 'method',
485 'func' => function () {
489 $_SERVER['REQUEST_METHOD'] = 'method';
490 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
492 $args = DI::args()->determine($_SERVER, $_GET);
495 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
496 api_call($this->app, $args)
502 * Test the api_call() function with an unimplemented API.
504 * @runInSeparateProcess
505 * @preserveGlobalState disabled
507 public function testApiCallWithUninplementedApi()
509 // @todo How to test the new API?
512 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
519 * Test the api_call() function with a JSON result.
521 * @runInSeparateProcess
522 * @preserveGlobalState disabled
524 public function testApiCallWithJson()
528 'method' => 'method',
529 'func' => function () {
530 return ['data' => ['some_data']];
533 $_SERVER['REQUEST_METHOD'] = 'method';
534 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
536 $args = DI::args()->determine($_SERVER, $_GET);
540 api_call($this->app, $args)
545 * Test the api_call() function with an XML result.
547 * @runInSeparateProcess
548 * @preserveGlobalState disabled
550 public function testApiCallWithXml()
554 'method' => 'method',
555 'func' => function () {
559 $_SERVER['REQUEST_METHOD'] = 'method';
560 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
562 $args = DI::args()->determine($_SERVER, $_GET);
566 api_call($this->app, $args)
571 * Test the api_call() function with an RSS result.
573 * @runInSeparateProcess
574 * @preserveGlobalState disabled
576 public function testApiCallWithRss()
580 'method' => 'method',
581 'func' => function () {
585 $_SERVER['REQUEST_METHOD'] = 'method';
586 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
588 $args = DI::args()->determine($_SERVER, $_GET);
591 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
593 api_call($this->app, $args)
598 * Test the api_call() function with an Atom result.
600 * @runInSeparateProcess
601 * @preserveGlobalState disabled
603 public function testApiCallWithAtom()
607 'method' => 'method',
608 'func' => function () {
612 $_SERVER['REQUEST_METHOD'] = 'method';
613 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
615 $args = DI::args()->determine($_SERVER, $_GET);
618 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
620 api_call($this->app, $args)
625 * Test the api_call() function with an unallowed method.
627 * @runInSeparateProcess
628 * @preserveGlobalState disabled
630 public function testApiCallWithWrongMethod()
633 $API['api_path'] = ['method' => 'method'];
635 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
637 $args = DI::args()->determine($_SERVER, $_GET);
640 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
641 api_call($this->app, $args)
646 * Test the api_call() function with an unauthorized user.
648 * @runInSeparateProcess
649 * @preserveGlobalState disabled
651 public function testApiCallWithWrongAuth()
655 'method' => 'method',
658 $_SESSION['authenticated'] = false;
659 $_SERVER['REQUEST_METHOD'] = 'method';
660 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
662 $args = DI::args()->determine($_SERVER, $_GET);
665 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
666 api_call($this->app, $args)
671 * Test the api_error() function with a JSON result.
673 * @runInSeparateProcess
674 * @preserveGlobalState disabled
676 public function testApiErrorWithJson()
678 // @todo How to test the new API?
679 // self::assertEquals(
680 // '{"status":{"error":"error_message","code":"200 OK","request":""}}',
681 // api_error('json', new HTTPException\OKException('error_message'), DI::args())
686 * Test the api_error() function with an XML result.
688 * @runInSeparateProcess
689 * @preserveGlobalState disabled
691 public function testApiErrorWithXml()
693 // @todo How to test the new API?
696 '<?xml version="1.0"?>' . "\n" .
697 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
698 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
699 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
700 ' <error>error_message</error>' . "\n" .
701 ' <code>200 OK</code>' . "\n" .
702 ' <request/>' . "\n" .
704 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
710 * Test the api_error() function with an RSS result.
712 * @runInSeparateProcess
713 * @preserveGlobalState disabled
715 public function testApiErrorWithRss()
717 // @todo How to test the new API?
720 '<?xml version="1.0"?>' . "\n" .
721 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
722 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
723 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
724 ' <error>error_message</error>' . "\n" .
725 ' <code>200 OK</code>' . "\n" .
726 ' <request/>' . "\n" .
728 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
734 * Test the api_error() function with an Atom result.
736 * @runInSeparateProcess
737 * @preserveGlobalState disabled
739 public function testApiErrorWithAtom()
741 // @todo How to test the new API?
744 '<?xml version="1.0"?>' . "\n" .
745 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
746 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
747 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
748 ' <error>error_message</error>' . "\n" .
749 ' <code>200 OK</code>' . "\n" .
750 ' <request/>' . "\n" .
752 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
758 * Test the api_rss_extra() function.
762 public function testApiRssExtra()
764 $user_info = ['url' => 'user_url', 'lang' => 'en'];
765 $result = api_rss_extra($this->app, [], $user_info);
766 self::assertEquals($user_info, $result['$user']);
767 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
768 self::assertArrayHasKey('self', $result['$rss']);
769 self::assertArrayHasKey('base', $result['$rss']);
770 self::assertArrayHasKey('updated', $result['$rss']);
771 self::assertArrayHasKey('atom_updated', $result['$rss']);
772 self::assertArrayHasKey('language', $result['$rss']);
773 self::assertArrayHasKey('logo', $result['$rss']);
777 * Test the api_rss_extra() function without any user info.
781 public function testApiRssExtraWithoutUserInfo()
783 $result = api_rss_extra($this->app, [], null);
784 self::assertIsArray($result['$user']);
785 self::assertArrayHasKey('alternate', $result['$rss']);
786 self::assertArrayHasKey('self', $result['$rss']);
787 self::assertArrayHasKey('base', $result['$rss']);
788 self::assertArrayHasKey('updated', $result['$rss']);
789 self::assertArrayHasKey('atom_updated', $result['$rss']);
790 self::assertArrayHasKey('language', $result['$rss']);
791 self::assertArrayHasKey('logo', $result['$rss']);
795 * Test the api_unique_id_to_nurl() function.
799 public function testApiUniqueIdToNurl()
801 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
805 * Test the api_unique_id_to_nurl() function with a correct ID.
809 public function testApiUniqueIdToNurlWithCorrectId()
811 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
815 * Test the api_get_user() function.
819 public function testApiGetUser()
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 a Frio schema.
833 public function testApiGetUserWithFrioSchema()
835 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
836 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
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 an empty Frio schema.
849 public function testApiGetUserWithEmptyFrioSchema()
851 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
852 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
853 $user = api_get_user();
854 self::assertSelfUser($user);
855 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
856 self::assertEquals('6fdbe8', $user['profile_link_color']);
857 self::assertEquals('ededed', $user['profile_background_color']);
861 * Test the api_get_user() function with a custom Frio schema.
865 public function testApiGetUserWithCustomFrioSchema()
867 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
868 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
869 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
870 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
871 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
872 $user = api_get_user();
873 self::assertSelfUser($user);
874 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
875 self::assertEquals('123456', $user['profile_link_color']);
876 self::assertEquals('123456', $user['profile_background_color']);
880 * Test the api_get_user() function with an user that is not allowed to use the API.
882 * @runInSeparateProcess
883 * @preserveGlobalState disabled
885 public function testApiGetUserWithoutApiUser()
887 $_SERVER['PHP_AUTH_USER'] = 'Test user';
888 $_SERVER['PHP_AUTH_PW'] = 'password';
889 $_SESSION['allow_api'] = false;
890 self::assertFalse(api_get_user());
894 * Test the api_get_user() function with an user ID in a GET parameter.
898 public function testApiGetUserWithGetId()
900 $_GET['user_id'] = $this->otherUser['id'];
901 self::assertOtherUser(api_get_user());
905 * Test the api_get_user() function with a wrong user ID in a GET parameter.
909 public function testApiGetUserWithWrongGetId()
911 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
912 $_GET['user_id'] = $this->wrongUserId;
913 self::assertOtherUser(api_get_user());
917 * Test the api_get_user() function with an user name in a GET parameter.
921 public function testApiGetUserWithGetName()
923 $_GET['screen_name'] = $this->selfUser['nick'];
924 self::assertSelfUser(api_get_user());
928 * Test the api_get_user() function with a profile URL in a GET parameter.
932 public function testApiGetUserWithGetUrl()
934 $_GET['profileurl'] = $this->selfUser['nurl'];
935 self::assertSelfUser(api_get_user());
939 * Test the api_get_user() function with an user ID in the API path.
943 public function testApiGetUserWithNumericCalledApi()
946 $called_api = ['api_path'];
947 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
948 self::assertOtherUser(api_get_user());
952 * Test the api_get_user() function with the $called_api global variable.
956 public function testApiGetUserWithCalledApi()
959 $called_api = ['api', 'api_path'];
960 self::assertSelfUser(api_get_user());
964 * Test the api_get_user() function with a valid user.
968 public function testApiGetUserWithCorrectUser()
970 self::assertOtherUser(api_get_user($this->otherUser['id']));
974 * Test the api_get_user() function with a wrong user ID.
978 public function testApiGetUserWithWrongUser()
980 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
981 self::assertOtherUser(api_get_user($this->wrongUserId));
985 * Test the api_get_user() function with a 0 user ID.
989 public function testApiGetUserWithZeroUser()
991 self::assertSelfUser(api_get_user(0));
995 * Test the api_item_get_user() function.
999 public function testApiItemGetUser()
1001 $users = api_item_get_user($this->app, []);
1002 self::assertSelfUser($users[0]);
1006 * Test the api_item_get_user() function with a different item parent.
1010 public function testApiItemGetUserWithDifferentParent()
1012 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1013 self::assertSelfUser($users[0]);
1014 self::assertEquals($users[0], $users[1]);
1018 * Test the Arrays::walkRecursive() function.
1022 public function testApiWalkRecursive()
1027 Arrays::walkRecursive(
1030 // Should we test this with a callback that actually does something?
1038 * Test the Arrays::walkRecursive() function with an array.
1042 public function testApiWalkRecursiveWithArray()
1044 $array = [['item1'], ['item2']];
1047 Arrays::walkRecursive(
1050 // Should we test this with a callback that actually does something?
1058 * Test the BaseApi::reformatXML() function.
1062 public function testApiReformatXml()
1066 self::assertTrue(BaseApi::reformatXML($item, $key));
1067 self::assertEquals('true', $item);
1071 * Test the BaseApi::reformatXML() function with a statusnet_api key.
1075 public function testApiReformatXmlWithStatusnetKey()
1078 $key = 'statusnet_api';
1079 self::assertTrue(BaseApi::reformatXML($item, $key));
1080 self::assertEquals('statusnet:api', $key);
1084 * Test the BaseApi::reformatXML() function with a friendica_api key.
1088 public function testApiReformatXmlWithFriendicaKey()
1091 $key = 'friendica_api';
1092 self::assertTrue(BaseApi::reformatXML($item, $key));
1093 self::assertEquals('friendica:api', $key);
1097 * Test the BaseApi::createXML() function.
1101 public function testApiCreateXml()
1104 '<?xml version="1.0"?>' . "\n" .
1105 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1106 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1107 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1108 ' <data>some_data</data>' . "\n" .
1109 '</root_element>' . "\n",
1110 BaseApi::createXML(['data' => ['some_data']], 'root_element')
1115 * Test the BaseApi::createXML() function without any XML namespace.
1119 public function testApiCreateXmlWithoutNamespaces()
1122 '<?xml version="1.0"?>' . "\n" .
1124 ' <data>some_data</data>' . "\n" .
1126 BaseApi::createXML(['data' => ['some_data']], 'ok')
1131 * Test the BaseApi::formatData() function.
1135 public function testApiFormatData()
1137 $data = ['some_data'];
1138 self::assertEquals($data, BaseApi::formatData('root_element', 'json', $data));
1142 * Test the BaseApi::formatData() function with an XML result.
1146 public function testApiFormatDataWithXml()
1149 '<?xml version="1.0"?>' . "\n" .
1150 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1151 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1152 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1153 ' <data>some_data</data>' . "\n" .
1154 '</root_element>' . "\n",
1155 BaseApi::formatData('root_element', 'xml', ['data' => ['some_data']])
1160 * Test the api_account_verify_credentials() function.
1164 public function testApiAccountVerifyCredentials()
1166 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1170 * Test the api_account_verify_credentials() function without an authenticated user.
1174 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1176 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1177 $_SESSION['authenticated'] = false;
1178 api_account_verify_credentials('json');
1182 * Test the requestdata() function.
1186 public function testRequestdata()
1188 self::assertNull(requestdata('variable_name'));
1192 * Test the requestdata() function with a POST parameter.
1196 public function testRequestdataWithPost()
1198 $_POST['variable_name'] = 'variable_value';
1199 self::assertEquals('variable_value', requestdata('variable_name'));
1203 * Test the requestdata() function with a GET parameter.
1207 public function testRequestdataWithGet()
1209 $_GET['variable_name'] = 'variable_value';
1210 self::assertEquals('variable_value', requestdata('variable_name'));
1214 * Test the api_statuses_mediap() function.
1218 public function testApiStatusesMediap()
1220 DI::args()->setArgc(2);
1228 'tmp_name' => $this->getTempImage(),
1229 'name' => 'spacer.png',
1230 'type' => 'image/png'
1233 $_GET['status'] = '<b>Status content</b>';
1235 $result = api_statuses_mediap('json');
1236 self::assertStatus($result['status']);
1240 * Test the api_statuses_mediap() function without an authenticated user.
1244 public function testApiStatusesMediapWithoutAuthenticatedUser()
1246 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1247 $_SESSION['authenticated'] = false;
1248 api_statuses_mediap('json');
1252 * Test the api_statuses_update() function.
1256 public function testApiStatusesUpdate()
1258 $_GET['status'] = 'Status content #friendica';
1259 $_GET['in_reply_to_status_id'] = -1;
1268 'tmp_name' => $this->getTempImage(),
1269 'name' => 'spacer.png',
1270 'type' => 'image/png'
1274 $result = api_statuses_update('json');
1275 self::assertStatus($result['status']);
1279 * Test the api_statuses_update() function with an HTML status.
1283 public function testApiStatusesUpdateWithHtml()
1285 $_GET['htmlstatus'] = '<b>Status content</b>';
1287 $result = api_statuses_update('json');
1288 self::assertStatus($result['status']);
1292 * Test the api_statuses_update() function without an authenticated user.
1296 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1298 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1299 $_SESSION['authenticated'] = false;
1300 api_statuses_update('json');
1304 * Test the api_statuses_update() function with a parent status.
1308 public function testApiStatusesUpdateWithParent()
1310 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1314 * Test the api_statuses_update() function with a media_ids parameter.
1318 public function testApiStatusesUpdateWithMediaIds()
1320 $this->markTestIncomplete();
1324 * Test the api_statuses_update() function with the throttle limit reached.
1328 public function testApiStatusesUpdateWithDayThrottleReached()
1330 $this->markTestIncomplete();
1334 * Test the api_media_upload() function.
1335 * @runInSeparateProcess
1336 * @preserveGlobalState disabled
1338 public function testApiMediaUpload()
1340 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1345 * Test the api_media_upload() function without an authenticated user.
1349 public function testApiMediaUploadWithoutAuthenticatedUser()
1351 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1352 $_SESSION['authenticated'] = false;
1357 * Test the api_media_upload() function with an invalid uploaded media.
1361 public function testApiMediaUploadWithMedia()
1363 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1367 'tmp_name' => 'tmp_name'
1374 * Test the api_media_upload() function with an valid uploaded media.
1378 public function testApiMediaUploadWithValidMedia()
1386 'tmp_name' => $this->getTempImage(),
1387 'name' => 'spacer.png',
1388 'type' => 'image/png'
1392 DI::args()->setArgc(2);
1394 $result = api_media_upload();
1395 self::assertEquals('image/png', $result['media']['image']['image_type']);
1396 self::assertEquals(1, $result['media']['image']['w']);
1397 self::assertEquals(1, $result['media']['image']['h']);
1398 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1402 * Test the api_status_show() function.
1404 public function testApiStatusShowWithJson()
1406 $result = api_status_show('json', 1);
1407 self::assertStatus($result['status']);
1411 * Test the api_status_show() function with an XML result.
1413 public function testApiStatusShowWithXml()
1415 $result = api_status_show('xml', 1);
1416 self::assertXml($result, 'statuses');
1420 * Test the api_get_last_status() function
1422 public function testApiGetLastStatus()
1424 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1426 self::assertNotNull($item);
1430 * Test the api_users_show() function.
1434 public function testApiUsersShow()
1436 $result = api_users_show('json');
1437 // We can't use assertSelfUser() here because the user object is missing some properties.
1438 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1439 self::assertEquals('DFRN', $result['user']['location']);
1440 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1441 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1442 self::assertEquals('dfrn', $result['user']['network']);
1443 self::assertTrue($result['user']['verified']);
1447 * Test the api_users_show() function with an XML result.
1451 public function testApiUsersShowWithXml()
1453 $result = api_users_show('xml');
1454 self::assertXml($result, 'statuses');
1458 * Test the api_users_search() function.
1462 public function testApiUsersSearch()
1464 $_GET['q'] = 'othercontact';
1465 $result = api_users_search('json');
1466 self::assertOtherUser($result['users'][0]);
1470 * Test the api_users_search() function with an XML result.
1474 public function testApiUsersSearchWithXml()
1476 $_GET['q'] = 'othercontact';
1477 $result = api_users_search('xml');
1478 self::assertXml($result, 'users');
1482 * Test the api_users_search() function without a GET q parameter.
1486 public function testApiUsersSearchWithoutQuery()
1488 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1489 api_users_search('json');
1493 * Test the api_users_lookup() function.
1497 public function testApiUsersLookup()
1499 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1500 api_users_lookup('json');
1504 * Test the api_users_lookup() function with an user ID.
1508 public function testApiUsersLookupWithUserId()
1510 $_REQUEST['user_id'] = $this->otherUser['id'];
1511 $result = api_users_lookup('json');
1512 self::assertOtherUser($result['users'][0]);
1516 * Test the api_search() function.
1520 public function testApiSearch()
1522 $_REQUEST['q'] = 'reply';
1523 $_REQUEST['max_id'] = 10;
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 a count parameter.
1536 public function testApiSearchWithCount()
1538 $_REQUEST['q'] = 'reply';
1539 $_REQUEST['count'] = 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 rpp parameter.
1552 public function testApiSearchWithRpp()
1554 $_REQUEST['q'] = 'reply';
1555 $_REQUEST['rpp'] = 20;
1556 $result = api_search('json');
1557 foreach ($result['status'] as $status) {
1558 self::assertStatus($status);
1559 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1564 * Test the api_search() function with an q parameter contains hashtag.
1565 * @doesNotPerformAssertions
1567 public function testApiSearchWithHashtag()
1569 $_REQUEST['q'] = '%23friendica';
1570 $result = api_search('json');
1571 foreach ($result['status'] as $status) {
1572 self::assertStatus($status);
1573 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1578 * Test the api_search() function with an exclude_replies parameter.
1579 * @doesNotPerformAssertions
1581 public function testApiSearchWithExcludeReplies()
1583 $_REQUEST['max_id'] = 10;
1584 $_REQUEST['exclude_replies'] = true;
1585 $_REQUEST['q'] = 'friendica';
1586 $result = api_search('json');
1587 foreach ($result['status'] as $status) {
1588 self::assertStatus($status);
1593 * Test the api_search() function without an authenticated user.
1597 public function testApiSearchWithUnallowedUser()
1599 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1600 $_SESSION['allow_api'] = false;
1601 $_GET['screen_name'] = $this->selfUser['nick'];
1606 * Test the api_search() function without any GET query parameter.
1610 public function testApiSearchWithoutQuery()
1612 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1617 * Test the api_statuses_home_timeline() function.
1621 public function testApiStatusesHomeTimeline()
1623 $_REQUEST['max_id'] = 10;
1624 $_REQUEST['exclude_replies'] = true;
1625 $_REQUEST['conversation_id'] = 1;
1626 $result = api_statuses_home_timeline('json');
1627 self::assertNotEmpty($result['status']);
1628 foreach ($result['status'] as $status) {
1629 self::assertStatus($status);
1634 * Test the api_statuses_home_timeline() function with a negative page parameter.
1638 public function testApiStatusesHomeTimelineWithNegativePage()
1640 $_REQUEST['page'] = -2;
1641 $result = api_statuses_home_timeline('json');
1642 self::assertNotEmpty($result['status']);
1643 foreach ($result['status'] as $status) {
1644 self::assertStatus($status);
1649 * Test the api_statuses_home_timeline() with an unallowed user.
1653 public function testApiStatusesHomeTimelineWithUnallowedUser()
1655 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1656 $_SESSION['allow_api'] = false;
1657 $_GET['screen_name'] = $this->selfUser['nick'];
1658 api_statuses_home_timeline('json');
1662 * Test the api_statuses_home_timeline() function with an RSS result.
1666 public function testApiStatusesHomeTimelineWithRss()
1668 $result = api_statuses_home_timeline('rss');
1669 self::assertXml($result, 'statuses');
1673 * Test the api_statuses_public_timeline() function.
1677 public function testApiStatusesPublicTimeline()
1679 $_REQUEST['max_id'] = 10;
1680 $_REQUEST['conversation_id'] = 1;
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 the exclude_replies parameter.
1693 public function testApiStatusesPublicTimelineWithExcludeReplies()
1695 $_REQUEST['max_id'] = 10;
1696 $_REQUEST['exclude_replies'] = true;
1697 $result = api_statuses_public_timeline('json');
1698 self::assertNotEmpty($result['status']);
1699 foreach ($result['status'] as $status) {
1700 self::assertStatus($status);
1705 * Test the api_statuses_public_timeline() function with a negative page parameter.
1709 public function testApiStatusesPublicTimelineWithNegativePage()
1711 $_REQUEST['page'] = -2;
1712 $result = api_statuses_public_timeline('json');
1713 self::assertNotEmpty($result['status']);
1714 foreach ($result['status'] as $status) {
1715 self::assertStatus($status);
1720 * Test the api_statuses_public_timeline() function with an unallowed user.
1724 public function testApiStatusesPublicTimelineWithUnallowedUser()
1726 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1727 $_SESSION['allow_api'] = false;
1728 $_GET['screen_name'] = $this->selfUser['nick'];
1729 api_statuses_public_timeline('json');
1733 * Test the api_statuses_public_timeline() function with an RSS result.
1737 public function testApiStatusesPublicTimelineWithRss()
1739 $result = api_statuses_public_timeline('rss');
1740 self::assertXml($result, 'statuses');
1744 * Test the api_statuses_networkpublic_timeline() function.
1748 public function testApiStatusesNetworkpublicTimeline()
1750 $_REQUEST['max_id'] = 10;
1751 $result = api_statuses_networkpublic_timeline('json');
1752 self::assertNotEmpty($result['status']);
1753 foreach ($result['status'] as $status) {
1754 self::assertStatus($status);
1759 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1763 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1765 $_REQUEST['page'] = -2;
1766 $result = api_statuses_networkpublic_timeline('json');
1767 self::assertNotEmpty($result['status']);
1768 foreach ($result['status'] as $status) {
1769 self::assertStatus($status);
1774 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1778 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1780 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1781 $_SESSION['allow_api'] = false;
1782 $_GET['screen_name'] = $this->selfUser['nick'];
1783 api_statuses_networkpublic_timeline('json');
1787 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1791 public function testApiStatusesNetworkpublicTimelineWithRss()
1793 $result = api_statuses_networkpublic_timeline('rss');
1794 self::assertXml($result, 'statuses');
1798 * Test the api_statuses_show() function.
1802 public function testApiStatusesShow()
1804 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1805 api_statuses_show('json');
1809 * Test the api_statuses_show() function with an ID.
1813 public function testApiStatusesShowWithId()
1815 DI::args()->setArgv(['', '', '', 1]);
1816 $result = api_statuses_show('json');
1817 self::assertStatus($result['status']);
1821 * Test the api_statuses_show() function with the conversation parameter.
1825 public function testApiStatusesShowWithConversation()
1827 DI::args()->setArgv(['', '', '', 1]);
1828 $_REQUEST['conversation'] = 1;
1829 $result = api_statuses_show('json');
1830 self::assertNotEmpty($result['status']);
1831 foreach ($result['status'] as $status) {
1832 self::assertStatus($status);
1837 * Test the api_statuses_show() function with an unallowed user.
1841 public function testApiStatusesShowWithUnallowedUser()
1843 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1844 $_SESSION['allow_api'] = false;
1845 $_GET['screen_name'] = $this->selfUser['nick'];
1846 api_statuses_show('json');
1850 * Test the api_conversation_show() function.
1854 public function testApiConversationShow()
1856 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1857 api_conversation_show('json');
1861 * Test the api_conversation_show() function with an ID.
1865 public function testApiConversationShowWithId()
1867 DI::args()->setArgv(['', '', '', 1]);
1868 $_REQUEST['max_id'] = 10;
1869 $_REQUEST['page'] = -2;
1870 $result = api_conversation_show('json');
1871 self::assertNotEmpty($result['status']);
1872 foreach ($result['status'] as $status) {
1873 self::assertStatus($status);
1878 * Test the api_conversation_show() function with an unallowed user.
1882 public function testApiConversationShowWithUnallowedUser()
1884 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1885 $_SESSION['allow_api'] = false;
1886 $_GET['screen_name'] = $this->selfUser['nick'];
1887 api_conversation_show('json');
1891 * Test the api_statuses_repeat() function.
1895 public function testApiStatusesRepeat()
1897 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1898 api_statuses_repeat('json');
1902 * Test the api_statuses_repeat() function without an authenticated user.
1906 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1908 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1909 $_SESSION['authenticated'] = false;
1910 api_statuses_repeat('json');
1914 * Test the api_statuses_repeat() function with an ID.
1918 public function testApiStatusesRepeatWithId()
1920 DI::args()->setArgv(['', '', '', 1]);
1921 $result = api_statuses_repeat('json');
1922 self::assertStatus($result['status']);
1924 // Also test with a shared status
1925 DI::args()->setArgv(['', '', '', 5]);
1926 $result = api_statuses_repeat('json');
1927 self::assertStatus($result['status']);
1931 * Test the api_statuses_destroy() function.
1935 public function testApiStatusesDestroy()
1937 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1938 api_statuses_destroy('json');
1942 * Test the api_statuses_destroy() function without an authenticated user.
1946 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1948 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1949 $_SESSION['authenticated'] = false;
1950 api_statuses_destroy('json');
1954 * Test the api_statuses_destroy() function with an ID.
1958 public function testApiStatusesDestroyWithId()
1960 DI::args()->setArgv(['', '', '', 1]);
1961 $result = api_statuses_destroy('json');
1962 self::assertStatus($result['status']);
1966 * Test the api_statuses_mentions() function.
1970 public function testApiStatusesMentions()
1972 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1973 $_REQUEST['max_id'] = 10;
1974 $result = api_statuses_mentions('json');
1975 self::assertEmpty($result['status']);
1976 // We should test with mentions in the database.
1980 * Test the api_statuses_mentions() function with a negative page parameter.
1984 public function testApiStatusesMentionsWithNegativePage()
1986 $_REQUEST['page'] = -2;
1987 $result = api_statuses_mentions('json');
1988 self::assertEmpty($result['status']);
1992 * Test the api_statuses_mentions() function with an unallowed user.
1996 public function testApiStatusesMentionsWithUnallowedUser()
1998 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1999 $_SESSION['allow_api'] = false;
2000 $_GET['screen_name'] = $this->selfUser['nick'];
2001 api_statuses_mentions('json');
2005 * Test the api_statuses_mentions() function with an RSS result.
2009 public function testApiStatusesMentionsWithRss()
2011 $result = api_statuses_mentions('rss');
2012 self::assertXml($result, 'statuses');
2016 * Test the api_statuses_user_timeline() function.
2020 public function testApiStatusesUserTimeline()
2022 $_REQUEST['max_id'] = 10;
2023 $_REQUEST['exclude_replies'] = true;
2024 $_REQUEST['conversation_id'] = 1;
2025 $result = api_statuses_user_timeline('json');
2026 self::assertNotEmpty($result['status']);
2027 foreach ($result['status'] as $status) {
2028 self::assertStatus($status);
2033 * Test the api_statuses_user_timeline() function with a negative page parameter.
2037 public function testApiStatusesUserTimelineWithNegativePage()
2039 $_REQUEST['page'] = -2;
2040 $result = api_statuses_user_timeline('json');
2041 self::assertNotEmpty($result['status']);
2042 foreach ($result['status'] as $status) {
2043 self::assertStatus($status);
2048 * Test the api_statuses_user_timeline() function with an RSS result.
2052 public function testApiStatusesUserTimelineWithRss()
2054 $result = api_statuses_user_timeline('rss');
2055 self::assertXml($result, 'statuses');
2059 * Test the api_statuses_user_timeline() function with an unallowed user.
2063 public function testApiStatusesUserTimelineWithUnallowedUser()
2065 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2066 $_SESSION['allow_api'] = false;
2067 $_GET['screen_name'] = $this->selfUser['nick'];
2068 api_statuses_user_timeline('json');
2072 * Test the api_favorites_create_destroy() function.
2076 public function testApiFavoritesCreateDestroy()
2078 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2079 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2080 api_favorites_create_destroy('json');
2084 * Test the api_favorites_create_destroy() function with an invalid ID.
2088 public function testApiFavoritesCreateDestroyWithInvalidId()
2090 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2091 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2092 api_favorites_create_destroy('json');
2096 * Test the api_favorites_create_destroy() function with an invalid action.
2100 public function testApiFavoritesCreateDestroyWithInvalidAction()
2102 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2103 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2104 $_REQUEST['id'] = 1;
2105 api_favorites_create_destroy('json');
2109 * Test the api_favorites_create_destroy() function with the create action.
2113 public function testApiFavoritesCreateDestroyWithCreateAction()
2115 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2116 $_REQUEST['id'] = 3;
2117 $result = api_favorites_create_destroy('json');
2118 self::assertStatus($result['status']);
2122 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2126 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2128 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2129 $_REQUEST['id'] = 3;
2130 $result = api_favorites_create_destroy('rss');
2131 self::assertXml($result, 'status');
2135 * Test the api_favorites_create_destroy() function with the destroy action.
2139 public function testApiFavoritesCreateDestroyWithDestroyAction()
2141 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2142 $_REQUEST['id'] = 3;
2143 $result = api_favorites_create_destroy('json');
2144 self::assertStatus($result['status']);
2148 * Test the api_favorites_create_destroy() function without an authenticated user.
2152 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2154 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2155 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2156 $_SESSION['authenticated'] = false;
2157 api_favorites_create_destroy('json');
2161 * Test the api_favorites() function.
2165 public function testApiFavorites()
2167 $_REQUEST['page'] = -1;
2168 $_REQUEST['max_id'] = 10;
2169 $result = api_favorites('json');
2170 foreach ($result['status'] as $status) {
2171 self::assertStatus($status);
2176 * Test the api_favorites() function with an RSS result.
2180 public function testApiFavoritesWithRss()
2182 $result = api_favorites('rss');
2183 self::assertXml($result, 'statuses');
2187 * Test the api_favorites() function with an unallowed user.
2191 public function testApiFavoritesWithUnallowedUser()
2193 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2194 $_SESSION['allow_api'] = false;
2195 $_GET['screen_name'] = $this->selfUser['nick'];
2196 api_favorites('json');
2200 * Test the api_format_messages() function.
2204 public function testApiFormatMessages()
2206 $result = api_format_messages(
2207 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2208 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2209 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2211 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2212 self::assertEquals(1, $result['id']);
2213 self::assertEquals(2, $result['recipient_id']);
2214 self::assertEquals(3, $result['sender_id']);
2215 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2216 self::assertEquals('sender_name', $result['sender_screen_name']);
2220 * Test the api_format_messages() function with HTML.
2224 public function testApiFormatMessagesWithHtmlText()
2226 $_GET['getText'] = 'html';
2227 $result = api_format_messages(
2228 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2229 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2230 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2232 self::assertEquals('item_title', $result['title']);
2233 self::assertEquals('<strong>item_body</strong>', $result['text']);
2237 * Test the api_format_messages() function with plain text.
2241 public function testApiFormatMessagesWithPlainText()
2243 $_GET['getText'] = 'plain';
2244 $result = api_format_messages(
2245 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2246 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2247 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2249 self::assertEquals('item_title', $result['title']);
2250 self::assertEquals('item_body', $result['text']);
2254 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2258 public function testApiFormatMessagesWithoutUserObjects()
2260 $_GET['getUserObjects'] = 'false';
2261 $result = api_format_messages(
2262 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2263 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2264 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2266 self::assertTrue(!isset($result['sender']));
2267 self::assertTrue(!isset($result['recipient']));
2271 * Test the api_convert_item() function.
2275 public function testApiConvertItem()
2277 $result = api_convert_item(
2279 'network' => 'feed',
2280 'title' => 'item_title',
2282 // We need a long string to test that it is correctly cut
2283 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2284 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2285 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2286 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2287 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2288 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2289 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2290 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2291 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2292 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2293 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2294 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2295 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2296 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2297 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2298 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2299 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2300 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2301 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2302 'repellat officia illum quos impedit quam iste esse unde qui ' .
2303 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2304 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2305 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2306 'plink' => 'item_plink'
2309 self::assertStringStartsWith('item_title', $result['text']);
2310 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2314 * Test the api_convert_item() function with an empty item body.
2318 public function testApiConvertItemWithoutBody()
2320 $result = api_convert_item(
2322 'network' => 'feed',
2323 'title' => 'item_title',
2326 'plink' => 'item_plink'
2329 self::assertEquals("item_title", $result['text']);
2330 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2334 * Test the api_convert_item() function with the title in the body.
2338 public function testApiConvertItemWithTitleInBody()
2340 $result = api_convert_item(
2342 'title' => 'item_title',
2343 'body' => 'item_title item_body',
2347 self::assertEquals('item_title item_body', $result['text']);
2348 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2352 * Test the api_get_attachments() function.
2356 public function testApiGetAttachments()
2359 self::assertEmpty(api_get_attachments($body, 0));
2363 * Test the api_get_attachments() function with an img tag.
2367 public function testApiGetAttachmentsWithImage()
2369 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2370 self::assertIsArray(api_get_attachments($body, 0));
2374 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2378 public function testApiGetAttachmentsWithImageAndAndStatus()
2380 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2381 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2382 self::assertIsArray(api_get_attachments($body, 0));
2386 * Test the api_get_entitities() function.
2390 public function testApiGetEntitities()
2393 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2397 * Test the api_get_entitities() function with the include_entities parameter.
2401 public function testApiGetEntititiesWithIncludeEntities()
2403 $_REQUEST['include_entities'] = 'true';
2405 $result = api_get_entitities($text, 'bbcode', 0);
2406 self::assertIsArray($result['hashtags']);
2407 self::assertIsArray($result['symbols']);
2408 self::assertIsArray($result['urls']);
2409 self::assertIsArray($result['user_mentions']);
2413 * Test the api_format_items_embeded_images() function.
2417 public function testApiFormatItemsEmbededImages()
2420 'text ' . DI::baseUrl() . '/display/item_guid',
2421 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2426 * Test the api_contactlink_to_array() function.
2430 public function testApiContactlinkToArray()
2437 api_contactlink_to_array('text')
2442 * Test the api_contactlink_to_array() function with an URL.
2446 public function testApiContactlinkToArrayWithUrl()
2450 'name' => ['link_text'],
2453 api_contactlink_to_array('text <a href="url">link_text</a>')
2458 * Test the api_format_items_activities() function.
2462 public function testApiFormatItemsActivities()
2464 $item = ['uid' => 0, 'uri' => ''];
2465 $result = api_format_items_activities($item);
2466 self::assertArrayHasKey('like', $result);
2467 self::assertArrayHasKey('dislike', $result);
2468 self::assertArrayHasKey('attendyes', $result);
2469 self::assertArrayHasKey('attendno', $result);
2470 self::assertArrayHasKey('attendmaybe', $result);
2474 * Test the api_format_items_activities() function with an XML result.
2478 public function testApiFormatItemsActivitiesWithXml()
2480 $item = ['uid' => 0, 'uri' => ''];
2481 $result = api_format_items_activities($item, 'xml');
2482 self::assertArrayHasKey('friendica:like', $result);
2483 self::assertArrayHasKey('friendica:dislike', $result);
2484 self::assertArrayHasKey('friendica:attendyes', $result);
2485 self::assertArrayHasKey('friendica:attendno', $result);
2486 self::assertArrayHasKey('friendica:attendmaybe', $result);
2490 * Test the api_format_items() function.
2491 * @doesNotPerformAssertions
2493 public function testApiFormatItems()
2497 'item_network' => 'item_network',
2503 'author-network' => Protocol::DFRN,
2504 'author-link' => 'http://localhost/profile/othercontact',
2508 $result = api_format_items($items, ['id' => 0], true);
2509 foreach ($result as $status) {
2510 self::assertStatus($status);
2515 * Test the api_format_items() function with an XML result.
2516 * @doesNotPerformAssertions
2518 public function testApiFormatItemsWithXml()
2526 'author-network' => Protocol::DFRN,
2527 'author-link' => 'http://localhost/profile/othercontact',
2531 $result = api_format_items($items, ['id' => 0], true, 'xml');
2532 foreach ($result as $status) {
2533 self::assertStatus($status);
2538 * Test the api_format_items() function.
2542 public function testApiAccountRateLimitStatus()
2544 // @todo How to test the new API?
2545 // $result = api_account_rate_limit_status('json');
2546 // self::assertEquals(150, $result['hash']['remaining_hits']);
2547 // self::assertEquals(150, $result['hash']['hourly_limit']);
2548 // self::assertIsInt($result['hash']['reset_time_in_seconds']);
2552 * Test the api_format_items() function with an XML result.
2556 public function testApiAccountRateLimitStatusWithXml()
2558 // @todo How to test the new API?
2559 // $result = api_account_rate_limit_status('xml');
2560 // self::assertXml($result, 'hash');
2564 * Test the api_help_test() function.
2568 public function testApiHelpTest()
2570 // @todo How to test the new API?
2571 // $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'json']);
2572 // self::assertEquals(['ok' => 'ok'], $result);
2576 * Test the api_help_test() function with an XML result.
2580 public function testApiHelpTestWithXml()
2582 // @todo How to test the new API?
2583 // $result = api_help_test('xml');
2584 // self::assertXml($result, 'ok');
2588 * Test the api_lists_list() function.
2592 public function testApiListsList()
2594 $result = api_lists_list('json');
2595 self::assertEquals(['lists_list' => []], $result);
2599 * Test the api_lists_ownerships() function.
2603 public function testApiListsOwnerships()
2605 $result = api_lists_ownerships('json');
2606 foreach ($result['lists']['lists'] as $list) {
2607 self::assertList($list);
2612 * Test the api_lists_ownerships() function without an authenticated user.
2616 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2618 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2619 $_SESSION['authenticated'] = false;
2620 api_lists_ownerships('json');
2624 * Test the api_lists_statuses() function.
2628 public function testApiListsStatuses()
2630 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2631 api_lists_statuses('json');
2635 * Test the api_lists_statuses() function with a list ID.
2636 * @doesNotPerformAssertions
2638 public function testApiListsStatusesWithListId()
2640 $_REQUEST['list_id'] = 1;
2641 $_REQUEST['page'] = -1;
2642 $_REQUEST['max_id'] = 10;
2643 $result = api_lists_statuses('json');
2644 foreach ($result['status'] as $status) {
2645 self::assertStatus($status);
2650 * Test the api_lists_statuses() function with a list ID and a RSS result.
2654 public function testApiListsStatusesWithListIdAndRss()
2656 $_REQUEST['list_id'] = 1;
2657 $result = api_lists_statuses('rss');
2658 self::assertXml($result, 'statuses');
2662 * Test the api_lists_statuses() function with an unallowed user.
2666 public function testApiListsStatusesWithUnallowedUser()
2668 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2669 $_SESSION['allow_api'] = false;
2670 $_GET['screen_name'] = $this->selfUser['nick'];
2671 api_lists_statuses('json');
2675 * Test the api_statuses_f() function.
2679 public function testApiStatusesFWithFriends()
2682 $result = api_statuses_f('friends');
2683 self::assertArrayHasKey('user', $result);
2687 * Test the api_statuses_f() function.
2691 public function testApiStatusesFWithFollowers()
2693 $result = api_statuses_f('followers');
2694 self::assertArrayHasKey('user', $result);
2698 * Test the api_statuses_f() function.
2702 public function testApiStatusesFWithBlocks()
2704 $result = api_statuses_f('blocks');
2705 self::assertArrayHasKey('user', $result);
2709 * Test the api_statuses_f() function.
2713 public function testApiStatusesFWithIncoming()
2715 $result = api_statuses_f('incoming');
2716 self::assertArrayHasKey('user', $result);
2720 * Test the api_statuses_f() function an undefined cursor GET variable.
2724 public function testApiStatusesFWithUndefinedCursor()
2726 $_GET['cursor'] = 'undefined';
2727 self::assertFalse(api_statuses_f('friends'));
2731 * Test the api_statuses_friends() function.
2735 public function testApiStatusesFriends()
2737 $result = api_statuses_friends('json');
2738 self::assertArrayHasKey('user', $result);
2742 * Test the api_statuses_friends() function an undefined cursor GET variable.
2746 public function testApiStatusesFriendsWithUndefinedCursor()
2748 $_GET['cursor'] = 'undefined';
2749 self::assertFalse(api_statuses_friends('json'));
2753 * Test the api_statuses_followers() function.
2757 public function testApiStatusesFollowers()
2759 $result = api_statuses_followers('json');
2760 self::assertArrayHasKey('user', $result);
2764 * Test the api_statuses_followers() function an undefined cursor GET variable.
2768 public function testApiStatusesFollowersWithUndefinedCursor()
2770 $_GET['cursor'] = 'undefined';
2771 self::assertFalse(api_statuses_followers('json'));
2775 * Test the api_blocks_list() function.
2779 public function testApiBlocksList()
2781 $result = api_blocks_list('json');
2782 self::assertArrayHasKey('user', $result);
2786 * Test the api_blocks_list() function an undefined cursor GET variable.
2790 public function testApiBlocksListWithUndefinedCursor()
2792 $_GET['cursor'] = 'undefined';
2793 self::assertFalse(api_blocks_list('json'));
2797 * Test the api_friendships_incoming() function.
2801 public function testApiFriendshipsIncoming()
2803 $result = api_friendships_incoming('json');
2804 self::assertArrayHasKey('id', $result);
2808 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2812 public function testApiFriendshipsIncomingWithUndefinedCursor()
2814 $_GET['cursor'] = 'undefined';
2815 self::assertFalse(api_friendships_incoming('json'));
2819 * Test the api_statusnet_config() function.
2823 public function testApiStatusnetConfig()
2825 $result = api_statusnet_config('json');
2826 self::assertEquals('localhost', $result['config']['site']['server']);
2827 self::assertEquals('default', $result['config']['site']['theme']);
2828 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2829 self::assertTrue($result['config']['site']['fancy']);
2830 self::assertEquals('en', $result['config']['site']['language']);
2831 self::assertEquals('UTC', $result['config']['site']['timezone']);
2832 self::assertEquals(200000, $result['config']['site']['textlimit']);
2833 self::assertEquals('false', $result['config']['site']['private']);
2834 self::assertEquals('false', $result['config']['site']['ssl']);
2835 self::assertEquals(30, $result['config']['site']['shorturllength']);
2839 * Test the api_statusnet_version() function.
2843 public function testApiStatusnetVersion()
2845 // @todo How to test the new API?
2846 // $result = api_statusnet_version('json');
2847 // self::assertEquals('0.9.7', $result['version']);
2851 * Test the api_direct_messages_new() function.
2855 public function testApiDirectMessagesNew()
2857 $result = api_direct_messages_new('json');
2858 self::assertNull($result);
2862 * Test the api_direct_messages_new() function without an authenticated user.
2866 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2868 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2869 $_SESSION['authenticated'] = false;
2870 api_direct_messages_new('json');
2874 * Test the api_direct_messages_new() function with an user ID.
2878 public function testApiDirectMessagesNewWithUserId()
2880 $_POST['text'] = 'message_text';
2881 $_POST['user_id'] = $this->otherUser['id'];
2882 $result = api_direct_messages_new('json');
2883 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2887 * Test the api_direct_messages_new() function with a screen name.
2891 public function testApiDirectMessagesNewWithScreenName()
2893 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2894 $_POST['text'] = 'message_text';
2895 $_POST['screen_name'] = $this->friendUser['nick'];
2896 $result = api_direct_messages_new('json');
2897 self::assertStringContainsString('message_text', $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 a title.
2907 public function testApiDirectMessagesNewWithTitle()
2909 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2910 $_POST['text'] = 'message_text';
2911 $_POST['screen_name'] = $this->friendUser['nick'];
2912 $_REQUEST['title'] = 'message_title';
2913 $result = api_direct_messages_new('json');
2914 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2915 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2916 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2917 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2921 * Test the api_direct_messages_new() function with an RSS result.
2925 public function testApiDirectMessagesNewWithRss()
2927 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2928 $_POST['text'] = 'message_text';
2929 $_POST['screen_name'] = $this->friendUser['nick'];
2930 $result = api_direct_messages_new('rss');
2931 self::assertXml($result, 'direct-messages');
2935 * Test the api_direct_messages_destroy() function.
2939 public function testApiDirectMessagesDestroy()
2941 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2942 api_direct_messages_destroy('json');
2946 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2950 public function testApiDirectMessagesDestroyWithVerbose()
2952 $_GET['friendica_verbose'] = 'true';
2953 $result = api_direct_messages_destroy('json');
2957 'result' => 'error',
2958 'message' => 'message id or parenturi not specified'
2966 * Test the api_direct_messages_destroy() function without an authenticated user.
2970 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2972 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2973 $_SESSION['authenticated'] = false;
2974 api_direct_messages_destroy('json');
2978 * Test the api_direct_messages_destroy() function with a non-zero ID.
2982 public function testApiDirectMessagesDestroyWithId()
2984 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2985 $_REQUEST['id'] = 1;
2986 api_direct_messages_destroy('json');
2990 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2994 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2996 $_REQUEST['id'] = 1;
2997 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2998 $_GET['friendica_verbose'] = 'true';
2999 $result = api_direct_messages_destroy('json');
3003 'result' => 'error',
3004 'message' => 'message id not in database'
3012 * Test the api_direct_messages_destroy() function with a non-zero ID.
3016 public function testApiDirectMessagesDestroyWithCorrectId()
3018 $this->markTestIncomplete('We need to add a dataset for this.');
3022 * Test the api_direct_messages_box() function.
3026 public function testApiDirectMessagesBoxWithSentbox()
3028 $_REQUEST['page'] = -1;
3029 $_REQUEST['max_id'] = 10;
3030 $result = api_direct_messages_box('json', 'sentbox', 'false');
3031 self::assertArrayHasKey('direct_message', $result);
3035 * Test the api_direct_messages_box() function.
3039 public function testApiDirectMessagesBoxWithConversation()
3041 $result = api_direct_messages_box('json', 'conversation', 'false');
3042 self::assertArrayHasKey('direct_message', $result);
3046 * Test the api_direct_messages_box() function.
3050 public function testApiDirectMessagesBoxWithAll()
3052 $result = api_direct_messages_box('json', 'all', 'false');
3053 self::assertArrayHasKey('direct_message', $result);
3057 * Test the api_direct_messages_box() function.
3061 public function testApiDirectMessagesBoxWithInbox()
3063 $result = api_direct_messages_box('json', 'inbox', 'false');
3064 self::assertArrayHasKey('direct_message', $result);
3068 * Test the api_direct_messages_box() function.
3072 public function testApiDirectMessagesBoxWithVerbose()
3074 $result = api_direct_messages_box('json', 'sentbox', 'true');
3078 'result' => 'error',
3079 'message' => 'no mails available'
3087 * Test the api_direct_messages_box() function with a RSS result.
3091 public function testApiDirectMessagesBoxWithRss()
3093 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3094 self::assertXml($result, 'direct-messages');
3098 * Test the api_direct_messages_box() function without an authenticated user.
3102 public function testApiDirectMessagesBoxWithUnallowedUser()
3104 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3105 $_SESSION['allow_api'] = false;
3106 $_GET['screen_name'] = $this->selfUser['nick'];
3107 api_direct_messages_box('json', 'sentbox', 'false');
3111 * Test the api_direct_messages_sentbox() function.
3115 public function testApiDirectMessagesSentbox()
3117 $result = api_direct_messages_sentbox('json');
3118 self::assertArrayHasKey('direct_message', $result);
3122 * Test the api_direct_messages_inbox() function.
3126 public function testApiDirectMessagesInbox()
3128 $result = api_direct_messages_inbox('json');
3129 self::assertArrayHasKey('direct_message', $result);
3133 * Test the api_direct_messages_all() function.
3137 public function testApiDirectMessagesAll()
3139 $result = api_direct_messages_all('json');
3140 self::assertArrayHasKey('direct_message', $result);
3144 * Test the api_direct_messages_conversation() function.
3148 public function testApiDirectMessagesConversation()
3150 $result = api_direct_messages_conversation('json');
3151 self::assertArrayHasKey('direct_message', $result);
3155 * Test the api_oauth_request_token() function.
3159 public function testApiOauthRequestToken()
3161 $this->markTestIncomplete('exit() kills phpunit as well');
3165 * Test the api_oauth_access_token() function.
3169 public function testApiOauthAccessToken()
3171 $this->markTestIncomplete('exit() kills phpunit as well');
3175 * Test the api_fr_photoalbum_delete() function.
3179 public function testApiFrPhotoalbumDelete()
3181 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3182 api_fr_photoalbum_delete('json');
3186 * Test the api_fr_photoalbum_delete() function with an album name.
3190 public function testApiFrPhotoalbumDeleteWithAlbum()
3192 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3193 $_REQUEST['album'] = 'album_name';
3194 api_fr_photoalbum_delete('json');
3198 * Test the api_fr_photoalbum_delete() function with an album name.
3202 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3204 $this->markTestIncomplete('We need to add a dataset for this.');
3208 * Test the api_fr_photoalbum_delete() function.
3212 public function testApiFrPhotoalbumUpdate()
3214 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3215 api_fr_photoalbum_update('json');
3219 * Test the api_fr_photoalbum_delete() function with an album name.
3223 public function testApiFrPhotoalbumUpdateWithAlbum()
3225 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3226 $_REQUEST['album'] = 'album_name';
3227 api_fr_photoalbum_update('json');
3231 * Test the api_fr_photoalbum_delete() function with an album name.
3235 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3237 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3238 $_REQUEST['album'] = 'album_name';
3239 $_REQUEST['album_new'] = 'album_name';
3240 api_fr_photoalbum_update('json');
3244 * Test the api_fr_photoalbum_update() function without an authenticated user.
3248 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3250 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3251 $_SESSION['authenticated'] = false;
3252 api_fr_photoalbum_update('json');
3256 * Test the api_fr_photoalbum_delete() function with an album name.
3260 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3262 $this->markTestIncomplete('We need to add a dataset for this.');
3266 * Test the api_fr_photos_list() function.
3270 public function testApiFrPhotosList()
3272 $result = api_fr_photos_list('json');
3273 self::assertArrayHasKey('photo', $result);
3277 * Test the api_fr_photos_list() function without an authenticated user.
3281 public function testApiFrPhotosListWithoutAuthenticatedUser()
3283 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3284 $_SESSION['authenticated'] = false;
3285 api_fr_photos_list('json');
3289 * Test the api_fr_photo_create_update() function.
3291 public function testApiFrPhotoCreateUpdate()
3293 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3294 api_fr_photo_create_update('json');
3298 * Test the api_fr_photo_create_update() function without an authenticated user.
3302 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3304 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3305 $_SESSION['authenticated'] = false;
3306 api_fr_photo_create_update('json');
3310 * Test the api_fr_photo_create_update() function with an album name.
3314 public function testApiFrPhotoCreateUpdateWithAlbum()
3316 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3317 $_REQUEST['album'] = 'album_name';
3318 api_fr_photo_create_update('json');
3322 * Test the api_fr_photo_create_update() function with the update mode.
3326 public function testApiFrPhotoCreateUpdateWithUpdate()
3328 $this->markTestIncomplete('We need to create a dataset for this');
3332 * Test the api_fr_photo_create_update() function with an uploaded file.
3336 public function testApiFrPhotoCreateUpdateWithFile()
3338 $this->markTestIncomplete();
3342 * Test the api_fr_photo_delete() function.
3346 public function testApiFrPhotoDelete()
3348 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3349 api_fr_photo_delete('json');
3353 * Test the api_fr_photo_delete() function without an authenticated user.
3357 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3359 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3360 $_SESSION['authenticated'] = false;
3361 api_fr_photo_delete('json');
3365 * Test the api_fr_photo_delete() function with a photo ID.
3369 public function testApiFrPhotoDeleteWithPhotoId()
3371 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3372 $_REQUEST['photo_id'] = 1;
3373 api_fr_photo_delete('json');
3377 * Test the api_fr_photo_delete() function with a correct photo ID.
3381 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3383 $this->markTestIncomplete('We need to create a dataset for this.');
3387 * Test the api_fr_photo_detail() function.
3391 public function testApiFrPhotoDetail()
3393 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3394 api_fr_photo_detail('json');
3398 * Test the api_fr_photo_detail() function without an authenticated user.
3402 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3404 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3405 $_SESSION['authenticated'] = false;
3406 api_fr_photo_detail('json');
3410 * Test the api_fr_photo_detail() function with a photo ID.
3414 public function testApiFrPhotoDetailWithPhotoId()
3416 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3417 $_REQUEST['photo_id'] = 1;
3418 api_fr_photo_detail('json');
3422 * Test the api_fr_photo_detail() function with a correct photo ID.
3426 public function testApiFrPhotoDetailCorrectPhotoId()
3428 $this->markTestIncomplete('We need to create a dataset for this.');
3432 * Test the api_account_update_profile_image() function.
3436 public function testApiAccountUpdateProfileImage()
3438 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3439 api_account_update_profile_image('json');
3443 * Test the api_account_update_profile_image() function without an authenticated user.
3447 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3449 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3450 $_SESSION['authenticated'] = false;
3451 api_account_update_profile_image('json');
3455 * Test the api_account_update_profile_image() function with an uploaded file.
3459 public function testApiAccountUpdateProfileImageWithUpload()
3461 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3462 $this->markTestIncomplete();
3467 * Test the api_account_update_profile() function.
3471 public function testApiAccountUpdateProfile()
3473 $_POST['name'] = 'new_name';
3474 $_POST['description'] = 'new_description';
3475 $result = api_account_update_profile('json');
3476 // We can't use assertSelfUser() here because the user object is missing some properties.
3477 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3478 self::assertEquals('DFRN', $result['user']['location']);
3479 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3480 self::assertEquals('dfrn', $result['user']['network']);
3481 self::assertEquals('new_name', $result['user']['name']);
3482 self::assertEquals('new_description', $result['user']['description']);
3486 * Test the check_acl_input() function.
3490 public function testCheckAclInput()
3492 $result = check_acl_input('<aclstring>');
3493 // Where does this result come from?
3494 self::assertEquals(1, $result);
3498 * Test the check_acl_input() function with an empty ACL string.
3502 public function testCheckAclInputWithEmptyAclString()
3504 $result = check_acl_input(' ');
3505 self::assertFalse($result);
3509 * Test the save_media_to_database() function.
3513 public function testSaveMediaToDatabase()
3515 $this->markTestIncomplete();
3519 * Test the post_photo_item() function.
3523 public function testPostPhotoItem()
3525 $this->markTestIncomplete();
3529 * Test the prepare_photo_data() function.
3533 public function testPreparePhotoData()
3535 $this->markTestIncomplete();
3539 * Test the api_share_as_retweet() function with a valid item.
3543 public function testApiShareAsRetweetWithValidItem()
3545 $this->markTestIncomplete();
3549 * Test the api_in_reply_to() function.
3553 public function testApiInReplyTo()
3555 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3556 self::assertArrayHasKey('status_id', $result);
3557 self::assertArrayHasKey('user_id', $result);
3558 self::assertArrayHasKey('status_id_str', $result);
3559 self::assertArrayHasKey('user_id_str', $result);
3560 self::assertArrayHasKey('screen_name', $result);
3564 * Test the api_in_reply_to() function with a valid item.
3568 public function testApiInReplyToWithValidItem()
3570 $this->markTestIncomplete();
3574 * Test the api_clean_plain_items() function.
3578 public function testApiCleanPlainItems()
3580 $_REQUEST['include_entities'] = 'true';
3581 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3582 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3586 * Test the api_best_nickname() function.
3590 public function testApiBestNickname()
3593 $result = api_best_nickname($contacts);
3594 self::assertNull($result);
3598 * Test the api_best_nickname() function with contacts.
3602 public function testApiBestNicknameWithContacts()
3604 $this->markTestIncomplete();
3608 * Test the api_friendica_group_show() function.
3612 public function testApiFriendicaGroupShow()
3614 $this->markTestIncomplete();
3618 * Test the api_friendica_group_delete() function.
3622 public function testApiFriendicaGroupDelete()
3624 $this->markTestIncomplete();
3628 * Test the api_lists_destroy() function.
3632 public function testApiListsDestroy()
3634 $this->markTestIncomplete();
3638 * Test the group_create() function.
3642 public function testGroupCreate()
3644 $this->markTestIncomplete();
3648 * Test the api_friendica_group_create() function.
3652 public function testApiFriendicaGroupCreate()
3654 $this->markTestIncomplete();
3658 * Test the api_lists_create() function.
3662 public function testApiListsCreate()
3664 $this->markTestIncomplete();
3668 * Test the api_friendica_group_update() function.
3672 public function testApiFriendicaGroupUpdate()
3674 $this->markTestIncomplete();
3678 * Test the api_lists_update() function.
3682 public function testApiListsUpdate()
3684 $this->markTestIncomplete();
3688 * Test the api_friendica_activity() function.
3692 public function testApiFriendicaActivity()
3694 $this->markTestIncomplete();
3698 * Test the api_friendica_notification() function.
3702 public function testApiFriendicaNotification()
3704 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3705 api_friendica_notification('json');
3709 * Test the api_friendica_notification() function without an authenticated user.
3713 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3715 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3716 $_SESSION['authenticated'] = false;
3717 api_friendica_notification('json');
3721 * Test the api_friendica_notification() function with empty result
3725 public function testApiFriendicaNotificationWithEmptyResult()
3727 DI::args()->setArgv(['api', 'friendica', 'notification']);
3728 $_SESSION['uid'] = 41;
3729 $result = api_friendica_notification('json');
3730 self::assertEquals(['note' => false], $result);
3734 * Test the api_friendica_notification() function with an XML result.
3738 public function testApiFriendicaNotificationWithXmlResult()
3740 DI::args()->setArgv(['api', 'friendica', 'notification']);
3741 $result = api_friendica_notification('xml');
3742 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3743 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3746 <?xml version="1.0"?>
3748 <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"/>
3751 self::assertXmlStringEqualsXmlString($assertXml, $result);
3755 * Test the api_friendica_notification() function with an JSON result.
3759 public function testApiFriendicaNotificationWithJsonResult()
3761 DI::args()->setArgv(['api', 'friendica', 'notification']);
3762 $result = json_encode(api_friendica_notification('json'));
3763 self::assertJson($result);
3767 * Test the api_friendica_notification_seen() function.
3771 public function testApiFriendicaNotificationSeen()
3773 $this->markTestIncomplete();
3777 * Test the api_friendica_direct_messages_setseen() function.
3781 public function testApiFriendicaDirectMessagesSetseen()
3783 $this->markTestIncomplete();
3787 * Test the api_friendica_direct_messages_search() function.
3791 public function testApiFriendicaDirectMessagesSearch()
3793 $this->markTestIncomplete();
3797 * Test the api_saved_searches_list() function.
3801 public function testApiSavedSearchesList()
3803 $result = api_saved_searches_list('json');
3804 self::assertEquals(1, $result['terms'][0]['id']);
3805 self::assertEquals(1, $result['terms'][0]['id_str']);
3806 self::assertEquals('Saved search', $result['terms'][0]['name']);
3807 self::assertEquals('Saved search', $result['terms'][0]['query']);