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\DateTimeFormat;
18 use Friendica\Util\Temporal;
19 use Monolog\Handler\TestHandler;
21 require_once __DIR__ . '/../../include/api.php';
24 * Tests for the API functions.
26 * Functions that use header() need to be tested in a separate process.
27 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
29 * @backupGlobals enabled
31 class ApiTest extends FixtureTest
34 * @var TestHandler Can handle log-outputs
41 protected $friendUser;
45 protected $wrongUserId;
50 /** @var IManageConfigValues */
54 * Create variables used by tests.
56 protected function setUp() : void
58 global $API, $called_api;
64 /** @var IManageConfigValues $config */
65 $this->config = $this->dice->create(IManageConfigValues::class);
67 $this->config->set('system', 'url', 'http://localhost');
68 $this->config->set('system', 'hostname', 'localhost');
69 $this->config->set('system', 'worker_dont_fork', true);
72 $this->config->set('config', 'hostname', 'localhost');
73 $this->config->set('system', 'throttle_limit_day', 100);
74 $this->config->set('system', 'throttle_limit_week', 100);
75 $this->config->set('system', 'throttle_limit_month', 100);
76 $this->config->set('system', 'theme', 'system_theme');
80 $this->app = DI::app();
82 DI::args()->setArgc(1);
84 // User data that the test database is populated with
87 'name' => 'Self contact',
88 'nick' => 'selfcontact',
89 'nurl' => 'http://localhost/profile/selfcontact'
93 'name' => 'Friend contact',
94 'nick' => 'friendcontact',
95 'nurl' => 'http://localhost/profile/friendcontact'
99 'name' => 'othercontact',
100 'nick' => 'othercontact',
101 'nurl' => 'http://localhost/profile/othercontact'
104 // User ID that we know is not in the database
105 $this->wrongUserId = 666;
107 DI::session()->start();
109 // Most API require login so we force the session
112 'authenticated' => true,
113 'uid' => $this->selfUser['id']
118 * Assert that an user array contains expected keys.
120 * @param array $user User array
124 private function assertSelfUser(array $user)
126 self::assertEquals($this->selfUser['id'], $user['uid']);
127 self::assertEquals($this->selfUser['id'], $user['cid']);
128 self::assertEquals(1, $user['self']);
129 self::assertEquals('DFRN', $user['location']);
130 self::assertEquals($this->selfUser['name'], $user['name']);
131 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
132 self::assertEquals('dfrn', $user['network']);
133 self::assertTrue($user['verified']);
137 * Assert that an user array contains expected keys.
139 * @param array $user User array
143 private function assertOtherUser(array $user = [])
145 self::assertEquals($this->otherUser['id'], $user['id']);
146 self::assertEquals($this->otherUser['id'], $user['id_str']);
147 self::assertEquals(0, $user['self']);
148 self::assertEquals($this->otherUser['name'], $user['name']);
149 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
150 self::assertFalse($user['verified']);
154 * Assert that a status array contains expected keys.
156 * @param array $status Status array
160 private function assertStatus(array $status = [])
162 self::assertIsString($status['text'] ?? '');
163 self::assertIsInt($status['id'] ?? '');
164 // We could probably do more checks here.
168 * Assert that a list array contains expected keys.
170 * @param array $list List array
174 private function assertList(array $list = [])
176 self::assertIsString($list['name']);
177 self::assertIsInt($list['id']);
178 self::assertIsString('string', $list['id_str']);
179 self::assertContains($list['mode'], ['public', 'private']);
180 // We could probably do more checks here.
184 * Assert that the string is XML and contain the root element.
186 * @param string $result XML string
187 * @param string $root_element Root element name
191 private function assertXml($result = '', $root_element = '')
193 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
194 self::assertStringContainsString('<' . $root_element, $result);
195 // We could probably do more checks here.
199 * Get the path to a temporary empty PNG image.
201 * @return string Path
203 private function getTempImage()
205 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
209 // Empty 1x1 px PNG image
210 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
218 * Test the api_user() function.
222 public function testApiUser()
224 self::assertEquals($this->selfUser['id'], api_user());
228 * Test the api_user() function with an unallowed user.
232 public function testApiUserWithUnallowedUser()
234 $_SESSION = ['allow_api' => false];
235 self::assertEquals(false, api_user());
239 * Test the api_source() function.
243 public function testApiSource()
245 self::assertEquals('api', api_source());
249 * Test the api_source() function with a Twidere user agent.
253 public function testApiSourceWithTwidere()
255 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
256 self::assertEquals('Twidere', api_source());
260 * Test the api_source() function with a GET parameter.
264 public function testApiSourceWithGet()
266 $_GET['source'] = 'source_name';
267 self::assertEquals('source_name', api_source());
271 * Test the api_date() function.
275 public function testApiDate()
277 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
281 * Test the api_register_func() function.
285 public function testApiRegisterFunc()
297 self::assertTrue($API['api_path']['auth']);
298 self::assertEquals('method', $API['api_path']['method']);
299 self::assertTrue(is_callable($API['api_path']['func']));
303 * Test the BasicAuth::getCurrentUserID() function without any login.
305 * @runInSeparateProcess
306 * @preserveGlobalState disabled
307 * @preserveGlobalState disabled
309 public function testApiLoginWithoutLogin()
311 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
312 BasicAuth::getCurrentUserID(true);
316 * Test the BasicAuth::getCurrentUserID() function with a bad login.
318 * @runInSeparateProcess
319 * @preserveGlobalState disabled
320 * @preserveGlobalState disabled
322 public function testApiLoginWithBadLogin()
324 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
325 $_SERVER['PHP_AUTH_USER'] = 'user@server';
326 BasicAuth::getCurrentUserID(true);
330 * Test the BasicAuth::getCurrentUserID() function with oAuth.
334 public function testApiLoginWithOauth()
336 $this->markTestIncomplete('Can we test this easily?');
340 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
344 public function testApiLoginWithAddonAuth()
346 $this->markTestIncomplete('Can we test this easily?');
350 * Test the BasicAuth::getCurrentUserID() function with a correct login.
352 * @runInSeparateProcess
353 * @preserveGlobalState disabled
354 * @doesNotPerformAssertions
356 public function testApiLoginWithCorrectLogin()
358 $_SERVER['PHP_AUTH_USER'] = 'Test user';
359 $_SERVER['PHP_AUTH_PW'] = 'password';
360 BasicAuth::getCurrentUserID(true);
364 * Test the BasicAuth::getCurrentUserID() function with a remote user.
366 * @runInSeparateProcess
367 * @preserveGlobalState disabled
369 public function testApiLoginWithRemoteUser()
371 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
372 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
373 BasicAuth::getCurrentUserID(true);
377 * Test the api_check_method() function.
381 public function testApiCheckMethod()
383 self::assertFalse(api_check_method('method'));
387 * Test the api_check_method() function with a correct method.
391 public function testApiCheckMethodWithCorrectMethod()
393 $_SERVER['REQUEST_METHOD'] = 'method';
394 self::assertTrue(api_check_method('method'));
398 * Test the api_check_method() function with a wildcard.
402 public function testApiCheckMethodWithWildcard()
404 self::assertTrue(api_check_method('*'));
408 * Test the api_call() function.
410 * @runInSeparateProcess
411 * @preserveGlobalState disabled
413 public function testApiCall()
417 'method' => 'method',
418 'func' => function () {
419 return ['data' => ['some_data']];
422 $_SERVER['REQUEST_METHOD'] = 'method';
423 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
424 $_GET['callback'] = 'callback_name';
426 $args = DI::args()->determine($_SERVER, $_GET);
429 'callback_name(["some_data"])',
430 api_call($this->app, $args)
435 * Test the api_call() function with the profiled enabled.
437 * @runInSeparateProcess
438 * @preserveGlobalState disabled
440 public function testApiCallWithProfiler()
444 'method' => 'method',
445 'func' => function () {
446 return ['data' => ['some_data']];
450 $_SERVER['REQUEST_METHOD'] = 'method';
451 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
453 $args = DI::args()->determine($_SERVER, $_GET);
455 $this->config->set('system', 'profiler', true);
456 $this->config->set('rendertime', 'callstack', true);
457 $this->app->callstack = [
458 'database' => ['some_function' => 200],
459 'database_write' => ['some_function' => 200],
460 'cache' => ['some_function' => 200],
461 'cache_write' => ['some_function' => 200],
462 'network' => ['some_function' => 200]
467 api_call($this->app, $args)
472 * Test the api_call() function without any result.
474 * @runInSeparateProcess
475 * @preserveGlobalState disabled
477 public function testApiCallWithNoResult()
481 'method' => 'method',
482 'func' => function () {
486 $_SERVER['REQUEST_METHOD'] = 'method';
487 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
489 $args = DI::args()->determine($_SERVER, $_GET);
492 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
493 api_call($this->app, $args)
498 * Test the api_call() function with an unimplemented API.
500 * @runInSeparateProcess
501 * @preserveGlobalState disabled
503 public function testApiCallWithUninplementedApi()
506 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
512 * Test the api_call() function with a JSON result.
514 * @runInSeparateProcess
515 * @preserveGlobalState disabled
517 public function testApiCallWithJson()
521 'method' => 'method',
522 'func' => function () {
523 return ['data' => ['some_data']];
526 $_SERVER['REQUEST_METHOD'] = 'method';
527 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
529 $args = DI::args()->determine($_SERVER, $_GET);
533 api_call($this->app, $args)
538 * Test the api_call() function with an XML result.
540 * @runInSeparateProcess
541 * @preserveGlobalState disabled
543 public function testApiCallWithXml()
547 'method' => 'method',
548 'func' => function () {
552 $_SERVER['REQUEST_METHOD'] = 'method';
553 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
555 $args = DI::args()->determine($_SERVER, $_GET);
559 api_call($this->app, $args)
564 * Test the api_call() function with an RSS result.
566 * @runInSeparateProcess
567 * @preserveGlobalState disabled
569 public function testApiCallWithRss()
573 'method' => 'method',
574 'func' => function () {
578 $_SERVER['REQUEST_METHOD'] = 'method';
579 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
581 $args = DI::args()->determine($_SERVER, $_GET);
584 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
586 api_call($this->app, $args)
591 * Test the api_call() function with an Atom result.
593 * @runInSeparateProcess
594 * @preserveGlobalState disabled
596 public function testApiCallWithAtom()
600 'method' => 'method',
601 'func' => function () {
605 $_SERVER['REQUEST_METHOD'] = 'method';
606 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
608 $args = DI::args()->determine($_SERVER, $_GET);
611 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
613 api_call($this->app, $args)
618 * Test the api_call() function with an unallowed method.
620 * @runInSeparateProcess
621 * @preserveGlobalState disabled
623 public function testApiCallWithWrongMethod()
626 $API['api_path'] = ['method' => 'method'];
628 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
630 $args = DI::args()->determine($_SERVER, $_GET);
633 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
634 api_call($this->app, $args)
639 * Test the api_call() function with an unauthorized user.
641 * @runInSeparateProcess
642 * @preserveGlobalState disabled
644 public function testApiCallWithWrongAuth()
648 'method' => 'method',
651 $_SESSION['authenticated'] = false;
652 $_SERVER['REQUEST_METHOD'] = 'method';
653 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
655 $args = DI::args()->determine($_SERVER, $_GET);
658 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
659 api_call($this->app, $args)
664 * Test the api_error() function with a JSON result.
666 * @runInSeparateProcess
667 * @preserveGlobalState disabled
669 public function testApiErrorWithJson()
672 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
673 api_error('json', new HTTPException\OKException('error_message'), DI::args())
678 * Test the api_error() function with an XML result.
680 * @runInSeparateProcess
681 * @preserveGlobalState disabled
683 public function testApiErrorWithXml()
686 '<?xml version="1.0"?>' . "\n" .
687 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
688 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
689 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
690 ' <error>error_message</error>' . "\n" .
691 ' <code>200 OK</code>' . "\n" .
692 ' <request/>' . "\n" .
694 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
699 * Test the api_error() function with an RSS result.
701 * @runInSeparateProcess
702 * @preserveGlobalState disabled
704 public function testApiErrorWithRss()
707 '<?xml version="1.0"?>' . "\n" .
708 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
709 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
710 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
711 ' <error>error_message</error>' . "\n" .
712 ' <code>200 OK</code>' . "\n" .
713 ' <request/>' . "\n" .
715 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
720 * Test the api_error() function with an Atom result.
722 * @runInSeparateProcess
723 * @preserveGlobalState disabled
725 public function testApiErrorWithAtom()
728 '<?xml version="1.0"?>' . "\n" .
729 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
730 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
731 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
732 ' <error>error_message</error>' . "\n" .
733 ' <code>200 OK</code>' . "\n" .
734 ' <request/>' . "\n" .
736 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
741 * Test the api_rss_extra() function.
745 public function testApiRssExtra()
747 $user_info = ['url' => 'user_url', 'lang' => 'en'];
748 $result = api_rss_extra($this->app, [], $user_info);
749 self::assertEquals($user_info, $result['$user']);
750 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
751 self::assertArrayHasKey('self', $result['$rss']);
752 self::assertArrayHasKey('base', $result['$rss']);
753 self::assertArrayHasKey('updated', $result['$rss']);
754 self::assertArrayHasKey('atom_updated', $result['$rss']);
755 self::assertArrayHasKey('language', $result['$rss']);
756 self::assertArrayHasKey('logo', $result['$rss']);
760 * Test the api_rss_extra() function without any user info.
764 public function testApiRssExtraWithoutUserInfo()
766 $result = api_rss_extra($this->app, [], null);
767 self::assertIsArray($result['$user']);
768 self::assertArrayHasKey('alternate', $result['$rss']);
769 self::assertArrayHasKey('self', $result['$rss']);
770 self::assertArrayHasKey('base', $result['$rss']);
771 self::assertArrayHasKey('updated', $result['$rss']);
772 self::assertArrayHasKey('atom_updated', $result['$rss']);
773 self::assertArrayHasKey('language', $result['$rss']);
774 self::assertArrayHasKey('logo', $result['$rss']);
778 * Test the api_unique_id_to_nurl() function.
782 public function testApiUniqueIdToNurl()
784 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
788 * Test the api_unique_id_to_nurl() function with a correct ID.
792 public function testApiUniqueIdToNurlWithCorrectId()
794 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
798 * Test the api_get_user() function.
802 public function testApiGetUser()
804 $user = api_get_user();
805 self::assertSelfUser($user);
806 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
807 self::assertEquals('6fdbe8', $user['profile_link_color']);
808 self::assertEquals('ededed', $user['profile_background_color']);
812 * Test the api_get_user() function with a Frio schema.
816 public function testApiGetUserWithFrioSchema()
818 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
819 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
820 $user = api_get_user();
821 self::assertSelfUser($user);
822 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
823 self::assertEquals('6fdbe8', $user['profile_link_color']);
824 self::assertEquals('ededed', $user['profile_background_color']);
828 * Test the api_get_user() function with an empty Frio schema.
832 public function testApiGetUserWithEmptyFrioSchema()
834 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
835 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
836 $user = api_get_user();
837 self::assertSelfUser($user);
838 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
839 self::assertEquals('6fdbe8', $user['profile_link_color']);
840 self::assertEquals('ededed', $user['profile_background_color']);
844 * Test the api_get_user() function with a custom Frio schema.
848 public function testApiGetUserWithCustomFrioSchema()
850 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
851 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
852 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
853 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
854 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
855 $user = api_get_user();
856 self::assertSelfUser($user);
857 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
858 self::assertEquals('123456', $user['profile_link_color']);
859 self::assertEquals('123456', $user['profile_background_color']);
863 * Test the api_get_user() function with an user that is not allowed to use the API.
865 * @runInSeparateProcess
866 * @preserveGlobalState disabled
868 public function testApiGetUserWithoutApiUser()
870 $_SERVER['PHP_AUTH_USER'] = 'Test user';
871 $_SERVER['PHP_AUTH_PW'] = 'password';
872 $_SESSION['allow_api'] = false;
873 self::assertFalse(api_get_user());
877 * Test the api_get_user() function with an user ID in a GET parameter.
881 public function testApiGetUserWithGetId()
883 $_GET['user_id'] = $this->otherUser['id'];
884 self::assertOtherUser(api_get_user());
888 * Test the api_get_user() function with a wrong user ID in a GET parameter.
892 public function testApiGetUserWithWrongGetId()
894 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
895 $_GET['user_id'] = $this->wrongUserId;
896 self::assertOtherUser(api_get_user());
900 * Test the api_get_user() function with an user name in a GET parameter.
904 public function testApiGetUserWithGetName()
906 $_GET['screen_name'] = $this->selfUser['nick'];
907 self::assertSelfUser(api_get_user());
911 * Test the api_get_user() function with a profile URL in a GET parameter.
915 public function testApiGetUserWithGetUrl()
917 $_GET['profileurl'] = $this->selfUser['nurl'];
918 self::assertSelfUser(api_get_user());
922 * Test the api_get_user() function with an user ID in the API path.
926 public function testApiGetUserWithNumericCalledApi()
929 $called_api = ['api_path'];
930 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
931 self::assertOtherUser(api_get_user());
935 * Test the api_get_user() function with the $called_api global variable.
939 public function testApiGetUserWithCalledApi()
942 $called_api = ['api', 'api_path'];
943 self::assertSelfUser(api_get_user());
947 * Test the api_get_user() function with a valid user.
951 public function testApiGetUserWithCorrectUser()
953 self::assertOtherUser(api_get_user($this->otherUser['id']));
957 * Test the api_get_user() function with a wrong user ID.
961 public function testApiGetUserWithWrongUser()
963 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
964 self::assertOtherUser(api_get_user($this->wrongUserId));
968 * Test the api_get_user() function with a 0 user ID.
972 public function testApiGetUserWithZeroUser()
974 self::assertSelfUser(api_get_user(0));
978 * Test the api_item_get_user() function.
982 public function testApiItemGetUser()
984 $users = api_item_get_user($this->app, []);
985 self::assertSelfUser($users[0]);
989 * Test the api_item_get_user() function with a different item parent.
993 public function testApiItemGetUserWithDifferentParent()
995 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
996 self::assertSelfUser($users[0]);
997 self::assertEquals($users[0], $users[1]);
1001 * Test the BaseApi::walkRecursive() function.
1005 public function testApiWalkRecursive()
1010 BaseApi::walkRecursive(
1013 // Should we test this with a callback that actually does something?
1021 * Test the BaseApi::walkRecursive() function with an array.
1025 public function testApiWalkRecursiveWithArray()
1027 $array = [['item1'], ['item2']];
1030 BaseApi::walkRecursive(
1033 // Should we test this with a callback that actually does something?
1041 * Test the BaseApi::reformatXML() function.
1045 public function testApiReformatXml()
1049 self::assertTrue(BaseApi::reformatXML($item, $key));
1050 self::assertEquals('true', $item);
1054 * Test the BaseApi::reformatXML() function with a statusnet_api key.
1058 public function testApiReformatXmlWithStatusnetKey()
1061 $key = 'statusnet_api';
1062 self::assertTrue(BaseApi::reformatXML($item, $key));
1063 self::assertEquals('statusnet:api', $key);
1067 * Test the BaseApi::reformatXML() function with a friendica_api key.
1071 public function testApiReformatXmlWithFriendicaKey()
1074 $key = 'friendica_api';
1075 self::assertTrue(BaseApi::reformatXML($item, $key));
1076 self::assertEquals('friendica:api', $key);
1080 * Test the BaseApi::createXML() function.
1084 public function testApiCreateXml()
1087 '<?xml version="1.0"?>' . "\n" .
1088 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1089 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1090 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1091 ' <data>some_data</data>' . "\n" .
1092 '</root_element>' . "\n",
1093 BaseApi::createXML(['data' => ['some_data']], 'root_element')
1098 * Test the BaseApi::createXML() function without any XML namespace.
1102 public function testApiCreateXmlWithoutNamespaces()
1105 '<?xml version="1.0"?>' . "\n" .
1107 ' <data>some_data</data>' . "\n" .
1109 BaseApi::createXML(['data' => ['some_data']], 'ok')
1114 * Test the BaseApi::formatData() function.
1118 public function testApiFormatData()
1120 $data = ['some_data'];
1121 self::assertEquals($data, BaseApi::formatData('root_element', 'json', $data));
1125 * Test the BaseApi::formatData() function with an XML result.
1129 public function testApiFormatDataWithXml()
1132 '<?xml version="1.0"?>' . "\n" .
1133 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1134 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1135 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1136 ' <data>some_data</data>' . "\n" .
1137 '</root_element>' . "\n",
1138 BaseApi::formatData('root_element', 'xml', ['data' => ['some_data']])
1143 * Test the api_account_verify_credentials() function.
1147 public function testApiAccountVerifyCredentials()
1149 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1153 * Test the api_account_verify_credentials() function without an authenticated user.
1157 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1159 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1160 $_SESSION['authenticated'] = false;
1161 api_account_verify_credentials('json');
1165 * Test the requestdata() function.
1169 public function testRequestdata()
1171 self::assertNull(requestdata('variable_name'));
1175 * Test the requestdata() function with a POST parameter.
1179 public function testRequestdataWithPost()
1181 $_POST['variable_name'] = 'variable_value';
1182 self::assertEquals('variable_value', requestdata('variable_name'));
1186 * Test the requestdata() function with a GET parameter.
1190 public function testRequestdataWithGet()
1192 $_GET['variable_name'] = 'variable_value';
1193 self::assertEquals('variable_value', requestdata('variable_name'));
1197 * Test the api_statuses_mediap() function.
1201 public function testApiStatusesMediap()
1203 DI::args()->setArgc(2);
1211 'tmp_name' => $this->getTempImage(),
1212 'name' => 'spacer.png',
1213 'type' => 'image/png'
1216 $_GET['status'] = '<b>Status content</b>';
1218 $result = api_statuses_mediap('json');
1219 self::assertStatus($result['status']);
1223 * Test the api_statuses_mediap() function without an authenticated user.
1227 public function testApiStatusesMediapWithoutAuthenticatedUser()
1229 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1230 $_SESSION['authenticated'] = false;
1231 api_statuses_mediap('json');
1235 * Test the api_statuses_update() function.
1239 public function testApiStatusesUpdate()
1241 $_GET['status'] = 'Status content #friendica';
1242 $_GET['in_reply_to_status_id'] = -1;
1251 'tmp_name' => $this->getTempImage(),
1252 'name' => 'spacer.png',
1253 'type' => 'image/png'
1257 $result = api_statuses_update('json');
1258 self::assertStatus($result['status']);
1262 * Test the api_statuses_update() function with an HTML status.
1266 public function testApiStatusesUpdateWithHtml()
1268 $_GET['htmlstatus'] = '<b>Status content</b>';
1270 $result = api_statuses_update('json');
1271 self::assertStatus($result['status']);
1275 * Test the api_statuses_update() function without an authenticated user.
1279 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1281 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1282 $_SESSION['authenticated'] = false;
1283 api_statuses_update('json');
1287 * Test the api_statuses_update() function with a parent status.
1291 public function testApiStatusesUpdateWithParent()
1293 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1297 * Test the api_statuses_update() function with a media_ids parameter.
1301 public function testApiStatusesUpdateWithMediaIds()
1303 $this->markTestIncomplete();
1307 * Test the api_statuses_update() function with the throttle limit reached.
1311 public function testApiStatusesUpdateWithDayThrottleReached()
1313 $this->markTestIncomplete();
1317 * Test the api_media_upload() function.
1318 * @runInSeparateProcess
1319 * @preserveGlobalState disabled
1321 public function testApiMediaUpload()
1323 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1328 * Test the api_media_upload() function without an authenticated user.
1332 public function testApiMediaUploadWithoutAuthenticatedUser()
1334 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1335 $_SESSION['authenticated'] = false;
1340 * Test the api_media_upload() function with an invalid uploaded media.
1344 public function testApiMediaUploadWithMedia()
1346 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1350 'tmp_name' => 'tmp_name'
1357 * Test the api_media_upload() function with an valid uploaded media.
1361 public function testApiMediaUploadWithValidMedia()
1369 'tmp_name' => $this->getTempImage(),
1370 'name' => 'spacer.png',
1371 'type' => 'image/png'
1375 DI::args()->setArgc(2);
1377 $result = api_media_upload();
1378 self::assertEquals('image/png', $result['media']['image']['image_type']);
1379 self::assertEquals(1, $result['media']['image']['w']);
1380 self::assertEquals(1, $result['media']['image']['h']);
1381 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1385 * Test the api_status_show() function.
1387 public function testApiStatusShowWithJson()
1389 $result = api_status_show('json', 1);
1390 self::assertStatus($result['status']);
1394 * Test the api_status_show() function with an XML result.
1396 public function testApiStatusShowWithXml()
1398 $result = api_status_show('xml', 1);
1399 self::assertXml($result, 'statuses');
1403 * Test the api_get_last_status() function
1405 public function testApiGetLastStatus()
1407 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1409 self::assertNotNull($item);
1413 * Test the api_users_show() function.
1417 public function testApiUsersShow()
1419 $result = api_users_show('json');
1420 // We can't use assertSelfUser() here because the user object is missing some properties.
1421 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1422 self::assertEquals('DFRN', $result['user']['location']);
1423 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1424 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1425 self::assertEquals('dfrn', $result['user']['network']);
1426 self::assertTrue($result['user']['verified']);
1430 * Test the api_users_show() function with an XML result.
1434 public function testApiUsersShowWithXml()
1436 $result = api_users_show('xml');
1437 self::assertXml($result, 'statuses');
1441 * Test the api_users_search() function.
1445 public function testApiUsersSearch()
1447 $_GET['q'] = 'othercontact';
1448 $result = api_users_search('json');
1449 self::assertOtherUser($result['users'][0]);
1453 * Test the api_users_search() function with an XML result.
1457 public function testApiUsersSearchWithXml()
1459 $_GET['q'] = 'othercontact';
1460 $result = api_users_search('xml');
1461 self::assertXml($result, 'users');
1465 * Test the api_users_search() function without a GET q parameter.
1469 public function testApiUsersSearchWithoutQuery()
1471 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1472 api_users_search('json');
1476 * Test the api_users_lookup() function.
1480 public function testApiUsersLookup()
1482 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1483 api_users_lookup('json');
1487 * Test the api_users_lookup() function with an user ID.
1491 public function testApiUsersLookupWithUserId()
1493 $_REQUEST['user_id'] = $this->otherUser['id'];
1494 $result = api_users_lookup('json');
1495 self::assertOtherUser($result['users'][0]);
1499 * Test the api_search() function.
1503 public function testApiSearch()
1505 $_REQUEST['q'] = 'reply';
1506 $_REQUEST['max_id'] = 10;
1507 $result = api_search('json');
1508 foreach ($result['status'] as $status) {
1509 self::assertStatus($status);
1510 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1515 * Test the api_search() function a count parameter.
1519 public function testApiSearchWithCount()
1521 $_REQUEST['q'] = 'reply';
1522 $_REQUEST['count'] = 20;
1523 $result = api_search('json');
1524 foreach ($result['status'] as $status) {
1525 self::assertStatus($status);
1526 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1531 * Test the api_search() function with an rpp parameter.
1535 public function testApiSearchWithRpp()
1537 $_REQUEST['q'] = 'reply';
1538 $_REQUEST['rpp'] = 20;
1539 $result = api_search('json');
1540 foreach ($result['status'] as $status) {
1541 self::assertStatus($status);
1542 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1547 * Test the api_search() function with an q parameter contains hashtag.
1548 * @doesNotPerformAssertions
1550 public function testApiSearchWithHashtag()
1552 $_REQUEST['q'] = '%23friendica';
1553 $result = api_search('json');
1554 foreach ($result['status'] as $status) {
1555 self::assertStatus($status);
1556 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1561 * Test the api_search() function with an exclude_replies parameter.
1562 * @doesNotPerformAssertions
1564 public function testApiSearchWithExcludeReplies()
1566 $_REQUEST['max_id'] = 10;
1567 $_REQUEST['exclude_replies'] = true;
1568 $_REQUEST['q'] = 'friendica';
1569 $result = api_search('json');
1570 foreach ($result['status'] as $status) {
1571 self::assertStatus($status);
1576 * Test the api_search() function without an authenticated user.
1580 public function testApiSearchWithUnallowedUser()
1582 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1583 $_SESSION['allow_api'] = false;
1584 $_GET['screen_name'] = $this->selfUser['nick'];
1589 * Test the api_search() function without any GET query parameter.
1593 public function testApiSearchWithoutQuery()
1595 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1600 * Test the api_statuses_home_timeline() function.
1604 public function testApiStatusesHomeTimeline()
1606 $_REQUEST['max_id'] = 10;
1607 $_REQUEST['exclude_replies'] = true;
1608 $_REQUEST['conversation_id'] = 1;
1609 $result = api_statuses_home_timeline('json');
1610 self::assertNotEmpty($result['status']);
1611 foreach ($result['status'] as $status) {
1612 self::assertStatus($status);
1617 * Test the api_statuses_home_timeline() function with a negative page parameter.
1621 public function testApiStatusesHomeTimelineWithNegativePage()
1623 $_REQUEST['page'] = -2;
1624 $result = api_statuses_home_timeline('json');
1625 self::assertNotEmpty($result['status']);
1626 foreach ($result['status'] as $status) {
1627 self::assertStatus($status);
1632 * Test the api_statuses_home_timeline() with an unallowed user.
1636 public function testApiStatusesHomeTimelineWithUnallowedUser()
1638 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1639 $_SESSION['allow_api'] = false;
1640 $_GET['screen_name'] = $this->selfUser['nick'];
1641 api_statuses_home_timeline('json');
1645 * Test the api_statuses_home_timeline() function with an RSS result.
1649 public function testApiStatusesHomeTimelineWithRss()
1651 $result = api_statuses_home_timeline('rss');
1652 self::assertXml($result, 'statuses');
1656 * Test the api_statuses_public_timeline() function.
1660 public function testApiStatusesPublicTimeline()
1662 $_REQUEST['max_id'] = 10;
1663 $_REQUEST['conversation_id'] = 1;
1664 $result = api_statuses_public_timeline('json');
1665 self::assertNotEmpty($result['status']);
1666 foreach ($result['status'] as $status) {
1667 self::assertStatus($status);
1672 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1676 public function testApiStatusesPublicTimelineWithExcludeReplies()
1678 $_REQUEST['max_id'] = 10;
1679 $_REQUEST['exclude_replies'] = true;
1680 $result = api_statuses_public_timeline('json');
1681 self::assertNotEmpty($result['status']);
1682 foreach ($result['status'] as $status) {
1683 self::assertStatus($status);
1688 * Test the api_statuses_public_timeline() function with a negative page parameter.
1692 public function testApiStatusesPublicTimelineWithNegativePage()
1694 $_REQUEST['page'] = -2;
1695 $result = api_statuses_public_timeline('json');
1696 self::assertNotEmpty($result['status']);
1697 foreach ($result['status'] as $status) {
1698 self::assertStatus($status);
1703 * Test the api_statuses_public_timeline() function with an unallowed user.
1707 public function testApiStatusesPublicTimelineWithUnallowedUser()
1709 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1710 $_SESSION['allow_api'] = false;
1711 $_GET['screen_name'] = $this->selfUser['nick'];
1712 api_statuses_public_timeline('json');
1716 * Test the api_statuses_public_timeline() function with an RSS result.
1720 public function testApiStatusesPublicTimelineWithRss()
1722 $result = api_statuses_public_timeline('rss');
1723 self::assertXml($result, 'statuses');
1727 * Test the api_statuses_networkpublic_timeline() function.
1731 public function testApiStatusesNetworkpublicTimeline()
1733 $_REQUEST['max_id'] = 10;
1734 $result = api_statuses_networkpublic_timeline('json');
1735 self::assertNotEmpty($result['status']);
1736 foreach ($result['status'] as $status) {
1737 self::assertStatus($status);
1742 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1746 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1748 $_REQUEST['page'] = -2;
1749 $result = api_statuses_networkpublic_timeline('json');
1750 self::assertNotEmpty($result['status']);
1751 foreach ($result['status'] as $status) {
1752 self::assertStatus($status);
1757 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1761 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1763 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1764 $_SESSION['allow_api'] = false;
1765 $_GET['screen_name'] = $this->selfUser['nick'];
1766 api_statuses_networkpublic_timeline('json');
1770 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1774 public function testApiStatusesNetworkpublicTimelineWithRss()
1776 $result = api_statuses_networkpublic_timeline('rss');
1777 self::assertXml($result, 'statuses');
1781 * Test the api_statuses_show() function.
1785 public function testApiStatusesShow()
1787 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1788 api_statuses_show('json');
1792 * Test the api_statuses_show() function with an ID.
1796 public function testApiStatusesShowWithId()
1798 DI::args()->setArgv(['', '', '', 1]);
1799 $result = api_statuses_show('json');
1800 self::assertStatus($result['status']);
1804 * Test the api_statuses_show() function with the conversation parameter.
1808 public function testApiStatusesShowWithConversation()
1810 DI::args()->setArgv(['', '', '', 1]);
1811 $_REQUEST['conversation'] = 1;
1812 $result = api_statuses_show('json');
1813 self::assertNotEmpty($result['status']);
1814 foreach ($result['status'] as $status) {
1815 self::assertStatus($status);
1820 * Test the api_statuses_show() function with an unallowed user.
1824 public function testApiStatusesShowWithUnallowedUser()
1826 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1827 $_SESSION['allow_api'] = false;
1828 $_GET['screen_name'] = $this->selfUser['nick'];
1829 api_statuses_show('json');
1833 * Test the api_conversation_show() function.
1837 public function testApiConversationShow()
1839 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1840 api_conversation_show('json');
1844 * Test the api_conversation_show() function with an ID.
1848 public function testApiConversationShowWithId()
1850 DI::args()->setArgv(['', '', '', 1]);
1851 $_REQUEST['max_id'] = 10;
1852 $_REQUEST['page'] = -2;
1853 $result = api_conversation_show('json');
1854 self::assertNotEmpty($result['status']);
1855 foreach ($result['status'] as $status) {
1856 self::assertStatus($status);
1861 * Test the api_conversation_show() function with an unallowed user.
1865 public function testApiConversationShowWithUnallowedUser()
1867 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1868 $_SESSION['allow_api'] = false;
1869 $_GET['screen_name'] = $this->selfUser['nick'];
1870 api_conversation_show('json');
1874 * Test the api_statuses_repeat() function.
1878 public function testApiStatusesRepeat()
1880 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1881 api_statuses_repeat('json');
1885 * Test the api_statuses_repeat() function without an authenticated user.
1889 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1891 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1892 $_SESSION['authenticated'] = false;
1893 api_statuses_repeat('json');
1897 * Test the api_statuses_repeat() function with an ID.
1901 public function testApiStatusesRepeatWithId()
1903 DI::args()->setArgv(['', '', '', 1]);
1904 $result = api_statuses_repeat('json');
1905 self::assertStatus($result['status']);
1907 // Also test with a shared status
1908 DI::args()->setArgv(['', '', '', 5]);
1909 $result = api_statuses_repeat('json');
1910 self::assertStatus($result['status']);
1914 * Test the api_statuses_destroy() function.
1918 public function testApiStatusesDestroy()
1920 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1921 api_statuses_destroy('json');
1925 * Test the api_statuses_destroy() function without an authenticated user.
1929 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1931 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1932 $_SESSION['authenticated'] = false;
1933 api_statuses_destroy('json');
1937 * Test the api_statuses_destroy() function with an ID.
1941 public function testApiStatusesDestroyWithId()
1943 DI::args()->setArgv(['', '', '', 1]);
1944 $result = api_statuses_destroy('json');
1945 self::assertStatus($result['status']);
1949 * Test the api_statuses_mentions() function.
1953 public function testApiStatusesMentions()
1955 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1956 $_REQUEST['max_id'] = 10;
1957 $result = api_statuses_mentions('json');
1958 self::assertEmpty($result['status']);
1959 // We should test with mentions in the database.
1963 * Test the api_statuses_mentions() function with a negative page parameter.
1967 public function testApiStatusesMentionsWithNegativePage()
1969 $_REQUEST['page'] = -2;
1970 $result = api_statuses_mentions('json');
1971 self::assertEmpty($result['status']);
1975 * Test the api_statuses_mentions() function with an unallowed user.
1979 public function testApiStatusesMentionsWithUnallowedUser()
1981 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1982 $_SESSION['allow_api'] = false;
1983 $_GET['screen_name'] = $this->selfUser['nick'];
1984 api_statuses_mentions('json');
1988 * Test the api_statuses_mentions() function with an RSS result.
1992 public function testApiStatusesMentionsWithRss()
1994 $result = api_statuses_mentions('rss');
1995 self::assertXml($result, 'statuses');
1999 * Test the api_statuses_user_timeline() function.
2003 public function testApiStatusesUserTimeline()
2005 $_REQUEST['max_id'] = 10;
2006 $_REQUEST['exclude_replies'] = true;
2007 $_REQUEST['conversation_id'] = 1;
2008 $result = api_statuses_user_timeline('json');
2009 self::assertNotEmpty($result['status']);
2010 foreach ($result['status'] as $status) {
2011 self::assertStatus($status);
2016 * Test the api_statuses_user_timeline() function with a negative page parameter.
2020 public function testApiStatusesUserTimelineWithNegativePage()
2022 $_REQUEST['page'] = -2;
2023 $result = api_statuses_user_timeline('json');
2024 self::assertNotEmpty($result['status']);
2025 foreach ($result['status'] as $status) {
2026 self::assertStatus($status);
2031 * Test the api_statuses_user_timeline() function with an RSS result.
2035 public function testApiStatusesUserTimelineWithRss()
2037 $result = api_statuses_user_timeline('rss');
2038 self::assertXml($result, 'statuses');
2042 * Test the api_statuses_user_timeline() function with an unallowed user.
2046 public function testApiStatusesUserTimelineWithUnallowedUser()
2048 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2049 $_SESSION['allow_api'] = false;
2050 $_GET['screen_name'] = $this->selfUser['nick'];
2051 api_statuses_user_timeline('json');
2055 * Test the api_favorites_create_destroy() function.
2059 public function testApiFavoritesCreateDestroy()
2061 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2062 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2063 api_favorites_create_destroy('json');
2067 * Test the api_favorites_create_destroy() function with an invalid ID.
2071 public function testApiFavoritesCreateDestroyWithInvalidId()
2073 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2074 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2075 api_favorites_create_destroy('json');
2079 * Test the api_favorites_create_destroy() function with an invalid action.
2083 public function testApiFavoritesCreateDestroyWithInvalidAction()
2085 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2086 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2087 $_REQUEST['id'] = 1;
2088 api_favorites_create_destroy('json');
2092 * Test the api_favorites_create_destroy() function with the create action.
2096 public function testApiFavoritesCreateDestroyWithCreateAction()
2098 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2099 $_REQUEST['id'] = 3;
2100 $result = api_favorites_create_destroy('json');
2101 self::assertStatus($result['status']);
2105 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2109 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2111 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2112 $_REQUEST['id'] = 3;
2113 $result = api_favorites_create_destroy('rss');
2114 self::assertXml($result, 'status');
2118 * Test the api_favorites_create_destroy() function with the destroy action.
2122 public function testApiFavoritesCreateDestroyWithDestroyAction()
2124 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2125 $_REQUEST['id'] = 3;
2126 $result = api_favorites_create_destroy('json');
2127 self::assertStatus($result['status']);
2131 * Test the api_favorites_create_destroy() function without an authenticated user.
2135 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2137 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2138 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2139 $_SESSION['authenticated'] = false;
2140 api_favorites_create_destroy('json');
2144 * Test the api_favorites() function.
2148 public function testApiFavorites()
2150 $_REQUEST['page'] = -1;
2151 $_REQUEST['max_id'] = 10;
2152 $result = api_favorites('json');
2153 foreach ($result['status'] as $status) {
2154 self::assertStatus($status);
2159 * Test the api_favorites() function with an RSS result.
2163 public function testApiFavoritesWithRss()
2165 $result = api_favorites('rss');
2166 self::assertXml($result, 'statuses');
2170 * Test the api_favorites() function with an unallowed user.
2174 public function testApiFavoritesWithUnallowedUser()
2176 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2177 $_SESSION['allow_api'] = false;
2178 $_GET['screen_name'] = $this->selfUser['nick'];
2179 api_favorites('json');
2183 * Test the api_format_messages() function.
2187 public function testApiFormatMessages()
2189 $result = api_format_messages(
2190 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2191 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2192 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2194 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2195 self::assertEquals(1, $result['id']);
2196 self::assertEquals(2, $result['recipient_id']);
2197 self::assertEquals(3, $result['sender_id']);
2198 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2199 self::assertEquals('sender_name', $result['sender_screen_name']);
2203 * Test the api_format_messages() function with HTML.
2207 public function testApiFormatMessagesWithHtmlText()
2209 $_GET['getText'] = 'html';
2210 $result = api_format_messages(
2211 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2212 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2213 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2215 self::assertEquals('item_title', $result['title']);
2216 self::assertEquals('<strong>item_body</strong>', $result['text']);
2220 * Test the api_format_messages() function with plain text.
2224 public function testApiFormatMessagesWithPlainText()
2226 $_GET['getText'] = 'plain';
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('item_body', $result['text']);
2237 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2241 public function testApiFormatMessagesWithoutUserObjects()
2243 $_GET['getUserObjects'] = 'false';
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::assertTrue(!isset($result['sender']));
2250 self::assertTrue(!isset($result['recipient']));
2254 * Test the api_convert_item() function.
2258 public function testApiConvertItem()
2260 $result = api_convert_item(
2262 'network' => 'feed',
2263 'title' => 'item_title',
2265 // We need a long string to test that it is correctly cut
2266 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2267 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2268 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2269 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2270 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2271 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2272 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2273 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2274 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2275 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2276 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2277 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2278 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2279 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2280 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2281 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2282 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2283 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2284 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2285 'repellat officia illum quos impedit quam iste esse unde qui ' .
2286 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2287 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2288 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2289 'plink' => 'item_plink'
2292 self::assertStringStartsWith('item_title', $result['text']);
2293 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2297 * Test the api_convert_item() function with an empty item body.
2301 public function testApiConvertItemWithoutBody()
2303 $result = api_convert_item(
2305 'network' => 'feed',
2306 'title' => 'item_title',
2309 'plink' => 'item_plink'
2312 self::assertEquals("item_title", $result['text']);
2313 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2317 * Test the api_convert_item() function with the title in the body.
2321 public function testApiConvertItemWithTitleInBody()
2323 $result = api_convert_item(
2325 'title' => 'item_title',
2326 'body' => 'item_title item_body',
2330 self::assertEquals('item_title item_body', $result['text']);
2331 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2335 * Test the api_get_attachments() function.
2339 public function testApiGetAttachments()
2342 self::assertEmpty(api_get_attachments($body, 0));
2346 * Test the api_get_attachments() function with an img tag.
2350 public function testApiGetAttachmentsWithImage()
2352 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2353 self::assertIsArray(api_get_attachments($body, 0));
2357 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2361 public function testApiGetAttachmentsWithImageAndAndStatus()
2363 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2364 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2365 self::assertIsArray(api_get_attachments($body, 0));
2369 * Test the api_get_entitities() function.
2373 public function testApiGetEntitities()
2376 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2380 * Test the api_get_entitities() function with the include_entities parameter.
2384 public function testApiGetEntititiesWithIncludeEntities()
2386 $_REQUEST['include_entities'] = 'true';
2388 $result = api_get_entitities($text, 'bbcode', 0);
2389 self::assertIsArray($result['hashtags']);
2390 self::assertIsArray($result['symbols']);
2391 self::assertIsArray($result['urls']);
2392 self::assertIsArray($result['user_mentions']);
2396 * Test the api_format_items_embeded_images() function.
2400 public function testApiFormatItemsEmbededImages()
2403 'text ' . DI::baseUrl() . '/display/item_guid',
2404 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2409 * Test the api_contactlink_to_array() function.
2413 public function testApiContactlinkToArray()
2420 api_contactlink_to_array('text')
2425 * Test the api_contactlink_to_array() function with an URL.
2429 public function testApiContactlinkToArrayWithUrl()
2433 'name' => ['link_text'],
2436 api_contactlink_to_array('text <a href="url">link_text</a>')
2441 * Test the api_format_items_activities() function.
2445 public function testApiFormatItemsActivities()
2447 $item = ['uid' => 0, 'uri' => ''];
2448 $result = api_format_items_activities($item);
2449 self::assertArrayHasKey('like', $result);
2450 self::assertArrayHasKey('dislike', $result);
2451 self::assertArrayHasKey('attendyes', $result);
2452 self::assertArrayHasKey('attendno', $result);
2453 self::assertArrayHasKey('attendmaybe', $result);
2457 * Test the api_format_items_activities() function with an XML result.
2461 public function testApiFormatItemsActivitiesWithXml()
2463 $item = ['uid' => 0, 'uri' => ''];
2464 $result = api_format_items_activities($item, 'xml');
2465 self::assertArrayHasKey('friendica:like', $result);
2466 self::assertArrayHasKey('friendica:dislike', $result);
2467 self::assertArrayHasKey('friendica:attendyes', $result);
2468 self::assertArrayHasKey('friendica:attendno', $result);
2469 self::assertArrayHasKey('friendica:attendmaybe', $result);
2473 * Test the api_format_items() function.
2474 * @doesNotPerformAssertions
2476 public function testApiFormatItems()
2480 'item_network' => 'item_network',
2486 'author-network' => Protocol::DFRN,
2487 'author-link' => 'http://localhost/profile/othercontact',
2491 $result = api_format_items($items, ['id' => 0], true);
2492 foreach ($result as $status) {
2493 self::assertStatus($status);
2498 * Test the api_format_items() function with an XML result.
2499 * @doesNotPerformAssertions
2501 public function testApiFormatItemsWithXml()
2509 'author-network' => Protocol::DFRN,
2510 'author-link' => 'http://localhost/profile/othercontact',
2514 $result = api_format_items($items, ['id' => 0], true, 'xml');
2515 foreach ($result as $status) {
2516 self::assertStatus($status);
2521 * Test the api_format_items() function.
2525 public function testApiAccountRateLimitStatus()
2527 $result = api_account_rate_limit_status('json');
2528 self::assertEquals(150, $result['hash']['remaining_hits']);
2529 self::assertEquals(150, $result['hash']['hourly_limit']);
2530 self::assertIsInt($result['hash']['reset_time_in_seconds']);
2534 * Test the api_format_items() function with an XML result.
2538 public function testApiAccountRateLimitStatusWithXml()
2540 $result = api_account_rate_limit_status('xml');
2541 self::assertXml($result, 'hash');
2545 * Test the api_help_test() function.
2549 public function testApiHelpTest()
2551 // @todo How to test the new API?
2552 // $result = api_help_test('json');
2553 // self::assertEquals(['ok' => 'ok'], $result);
2557 * Test the api_help_test() function with an XML result.
2561 public function testApiHelpTestWithXml()
2563 // @todo How to test the new API?
2564 // $result = api_help_test('xml');
2565 // self::assertXml($result, 'ok');
2569 * Test the api_lists_list() function.
2573 public function testApiListsList()
2575 $result = api_lists_list('json');
2576 self::assertEquals(['lists_list' => []], $result);
2580 * Test the api_lists_ownerships() function.
2584 public function testApiListsOwnerships()
2586 $result = api_lists_ownerships('json');
2587 foreach ($result['lists']['lists'] as $list) {
2588 self::assertList($list);
2593 * Test the api_lists_ownerships() function without an authenticated user.
2597 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2599 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2600 $_SESSION['authenticated'] = false;
2601 api_lists_ownerships('json');
2605 * Test the api_lists_statuses() function.
2609 public function testApiListsStatuses()
2611 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2612 api_lists_statuses('json');
2616 * Test the api_lists_statuses() function with a list ID.
2617 * @doesNotPerformAssertions
2619 public function testApiListsStatusesWithListId()
2621 $_REQUEST['list_id'] = 1;
2622 $_REQUEST['page'] = -1;
2623 $_REQUEST['max_id'] = 10;
2624 $result = api_lists_statuses('json');
2625 foreach ($result['status'] as $status) {
2626 self::assertStatus($status);
2631 * Test the api_lists_statuses() function with a list ID and a RSS result.
2635 public function testApiListsStatusesWithListIdAndRss()
2637 $_REQUEST['list_id'] = 1;
2638 $result = api_lists_statuses('rss');
2639 self::assertXml($result, 'statuses');
2643 * Test the api_lists_statuses() function with an unallowed user.
2647 public function testApiListsStatusesWithUnallowedUser()
2649 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2650 $_SESSION['allow_api'] = false;
2651 $_GET['screen_name'] = $this->selfUser['nick'];
2652 api_lists_statuses('json');
2656 * Test the api_statuses_f() function.
2660 public function testApiStatusesFWithFriends()
2663 $result = api_statuses_f('friends');
2664 self::assertArrayHasKey('user', $result);
2668 * Test the api_statuses_f() function.
2672 public function testApiStatusesFWithFollowers()
2674 $result = api_statuses_f('followers');
2675 self::assertArrayHasKey('user', $result);
2679 * Test the api_statuses_f() function.
2683 public function testApiStatusesFWithBlocks()
2685 $result = api_statuses_f('blocks');
2686 self::assertArrayHasKey('user', $result);
2690 * Test the api_statuses_f() function.
2694 public function testApiStatusesFWithIncoming()
2696 $result = api_statuses_f('incoming');
2697 self::assertArrayHasKey('user', $result);
2701 * Test the api_statuses_f() function an undefined cursor GET variable.
2705 public function testApiStatusesFWithUndefinedCursor()
2707 $_GET['cursor'] = 'undefined';
2708 self::assertFalse(api_statuses_f('friends'));
2712 * Test the api_statuses_friends() function.
2716 public function testApiStatusesFriends()
2718 $result = api_statuses_friends('json');
2719 self::assertArrayHasKey('user', $result);
2723 * Test the api_statuses_friends() function an undefined cursor GET variable.
2727 public function testApiStatusesFriendsWithUndefinedCursor()
2729 $_GET['cursor'] = 'undefined';
2730 self::assertFalse(api_statuses_friends('json'));
2734 * Test the api_statuses_followers() function.
2738 public function testApiStatusesFollowers()
2740 $result = api_statuses_followers('json');
2741 self::assertArrayHasKey('user', $result);
2745 * Test the api_statuses_followers() function an undefined cursor GET variable.
2749 public function testApiStatusesFollowersWithUndefinedCursor()
2751 $_GET['cursor'] = 'undefined';
2752 self::assertFalse(api_statuses_followers('json'));
2756 * Test the api_blocks_list() function.
2760 public function testApiBlocksList()
2762 $result = api_blocks_list('json');
2763 self::assertArrayHasKey('user', $result);
2767 * Test the api_blocks_list() function an undefined cursor GET variable.
2771 public function testApiBlocksListWithUndefinedCursor()
2773 $_GET['cursor'] = 'undefined';
2774 self::assertFalse(api_blocks_list('json'));
2778 * Test the api_friendships_incoming() function.
2782 public function testApiFriendshipsIncoming()
2784 $result = api_friendships_incoming('json');
2785 self::assertArrayHasKey('id', $result);
2789 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2793 public function testApiFriendshipsIncomingWithUndefinedCursor()
2795 $_GET['cursor'] = 'undefined';
2796 self::assertFalse(api_friendships_incoming('json'));
2800 * Test the api_statusnet_config() function.
2804 public function testApiStatusnetConfig()
2806 $result = api_statusnet_config('json');
2807 self::assertEquals('localhost', $result['config']['site']['server']);
2808 self::assertEquals('default', $result['config']['site']['theme']);
2809 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2810 self::assertTrue($result['config']['site']['fancy']);
2811 self::assertEquals('en', $result['config']['site']['language']);
2812 self::assertEquals('UTC', $result['config']['site']['timezone']);
2813 self::assertEquals(200000, $result['config']['site']['textlimit']);
2814 self::assertEquals('false', $result['config']['site']['private']);
2815 self::assertEquals('false', $result['config']['site']['ssl']);
2816 self::assertEquals(30, $result['config']['site']['shorturllength']);
2820 * Test the api_statusnet_version() function.
2824 public function testApiStatusnetVersion()
2826 // @todo How to test the new API?
2827 // $result = api_statusnet_version('json');
2828 // self::assertEquals('0.9.7', $result['version']);
2832 * Test the api_direct_messages_new() function.
2836 public function testApiDirectMessagesNew()
2838 $result = api_direct_messages_new('json');
2839 self::assertNull($result);
2843 * Test the api_direct_messages_new() function without an authenticated user.
2847 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2849 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2850 $_SESSION['authenticated'] = false;
2851 api_direct_messages_new('json');
2855 * Test the api_direct_messages_new() function with an user ID.
2859 public function testApiDirectMessagesNewWithUserId()
2861 $_POST['text'] = 'message_text';
2862 $_POST['user_id'] = $this->otherUser['id'];
2863 $result = api_direct_messages_new('json');
2864 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2868 * Test the api_direct_messages_new() function with a screen name.
2872 public function testApiDirectMessagesNewWithScreenName()
2874 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2875 $_POST['text'] = 'message_text';
2876 $_POST['screen_name'] = $this->friendUser['nick'];
2877 $result = api_direct_messages_new('json');
2878 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2879 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2880 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2884 * Test the api_direct_messages_new() function with a title.
2888 public function testApiDirectMessagesNewWithTitle()
2890 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2891 $_POST['text'] = 'message_text';
2892 $_POST['screen_name'] = $this->friendUser['nick'];
2893 $_REQUEST['title'] = 'message_title';
2894 $result = api_direct_messages_new('json');
2895 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2896 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2897 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2898 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2902 * Test the api_direct_messages_new() function with an RSS result.
2906 public function testApiDirectMessagesNewWithRss()
2908 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2909 $_POST['text'] = 'message_text';
2910 $_POST['screen_name'] = $this->friendUser['nick'];
2911 $result = api_direct_messages_new('rss');
2912 self::assertXml($result, 'direct-messages');
2916 * Test the api_direct_messages_destroy() function.
2920 public function testApiDirectMessagesDestroy()
2922 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2923 api_direct_messages_destroy('json');
2927 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2931 public function testApiDirectMessagesDestroyWithVerbose()
2933 $_GET['friendica_verbose'] = 'true';
2934 $result = api_direct_messages_destroy('json');
2938 'result' => 'error',
2939 'message' => 'message id or parenturi not specified'
2947 * Test the api_direct_messages_destroy() function without an authenticated user.
2951 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2953 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2954 $_SESSION['authenticated'] = false;
2955 api_direct_messages_destroy('json');
2959 * Test the api_direct_messages_destroy() function with a non-zero ID.
2963 public function testApiDirectMessagesDestroyWithId()
2965 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2966 $_REQUEST['id'] = 1;
2967 api_direct_messages_destroy('json');
2971 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2975 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2977 $_REQUEST['id'] = 1;
2978 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2979 $_GET['friendica_verbose'] = 'true';
2980 $result = api_direct_messages_destroy('json');
2984 'result' => 'error',
2985 'message' => 'message id not in database'
2993 * Test the api_direct_messages_destroy() function with a non-zero ID.
2997 public function testApiDirectMessagesDestroyWithCorrectId()
2999 $this->markTestIncomplete('We need to add a dataset for this.');
3003 * Test the api_direct_messages_box() function.
3007 public function testApiDirectMessagesBoxWithSentbox()
3009 $_REQUEST['page'] = -1;
3010 $_REQUEST['max_id'] = 10;
3011 $result = api_direct_messages_box('json', 'sentbox', 'false');
3012 self::assertArrayHasKey('direct_message', $result);
3016 * Test the api_direct_messages_box() function.
3020 public function testApiDirectMessagesBoxWithConversation()
3022 $result = api_direct_messages_box('json', 'conversation', 'false');
3023 self::assertArrayHasKey('direct_message', $result);
3027 * Test the api_direct_messages_box() function.
3031 public function testApiDirectMessagesBoxWithAll()
3033 $result = api_direct_messages_box('json', 'all', 'false');
3034 self::assertArrayHasKey('direct_message', $result);
3038 * Test the api_direct_messages_box() function.
3042 public function testApiDirectMessagesBoxWithInbox()
3044 $result = api_direct_messages_box('json', 'inbox', 'false');
3045 self::assertArrayHasKey('direct_message', $result);
3049 * Test the api_direct_messages_box() function.
3053 public function testApiDirectMessagesBoxWithVerbose()
3055 $result = api_direct_messages_box('json', 'sentbox', 'true');
3059 'result' => 'error',
3060 'message' => 'no mails available'
3068 * Test the api_direct_messages_box() function with a RSS result.
3072 public function testApiDirectMessagesBoxWithRss()
3074 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3075 self::assertXml($result, 'direct-messages');
3079 * Test the api_direct_messages_box() function without an authenticated user.
3083 public function testApiDirectMessagesBoxWithUnallowedUser()
3085 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3086 $_SESSION['allow_api'] = false;
3087 $_GET['screen_name'] = $this->selfUser['nick'];
3088 api_direct_messages_box('json', 'sentbox', 'false');
3092 * Test the api_direct_messages_sentbox() function.
3096 public function testApiDirectMessagesSentbox()
3098 $result = api_direct_messages_sentbox('json');
3099 self::assertArrayHasKey('direct_message', $result);
3103 * Test the api_direct_messages_inbox() function.
3107 public function testApiDirectMessagesInbox()
3109 $result = api_direct_messages_inbox('json');
3110 self::assertArrayHasKey('direct_message', $result);
3114 * Test the api_direct_messages_all() function.
3118 public function testApiDirectMessagesAll()
3120 $result = api_direct_messages_all('json');
3121 self::assertArrayHasKey('direct_message', $result);
3125 * Test the api_direct_messages_conversation() function.
3129 public function testApiDirectMessagesConversation()
3131 $result = api_direct_messages_conversation('json');
3132 self::assertArrayHasKey('direct_message', $result);
3136 * Test the api_oauth_request_token() function.
3140 public function testApiOauthRequestToken()
3142 $this->markTestIncomplete('exit() kills phpunit as well');
3146 * Test the api_oauth_access_token() function.
3150 public function testApiOauthAccessToken()
3152 $this->markTestIncomplete('exit() kills phpunit as well');
3156 * Test the api_fr_photoalbum_delete() function.
3160 public function testApiFrPhotoalbumDelete()
3162 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3163 api_fr_photoalbum_delete('json');
3167 * Test the api_fr_photoalbum_delete() function with an album name.
3171 public function testApiFrPhotoalbumDeleteWithAlbum()
3173 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3174 $_REQUEST['album'] = 'album_name';
3175 api_fr_photoalbum_delete('json');
3179 * Test the api_fr_photoalbum_delete() function with an album name.
3183 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3185 $this->markTestIncomplete('We need to add a dataset for this.');
3189 * Test the api_fr_photoalbum_delete() function.
3193 public function testApiFrPhotoalbumUpdate()
3195 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3196 api_fr_photoalbum_update('json');
3200 * Test the api_fr_photoalbum_delete() function with an album name.
3204 public function testApiFrPhotoalbumUpdateWithAlbum()
3206 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3207 $_REQUEST['album'] = 'album_name';
3208 api_fr_photoalbum_update('json');
3212 * Test the api_fr_photoalbum_delete() function with an album name.
3216 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3218 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3219 $_REQUEST['album'] = 'album_name';
3220 $_REQUEST['album_new'] = 'album_name';
3221 api_fr_photoalbum_update('json');
3225 * Test the api_fr_photoalbum_update() function without an authenticated user.
3229 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3231 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3232 $_SESSION['authenticated'] = false;
3233 api_fr_photoalbum_update('json');
3237 * Test the api_fr_photoalbum_delete() function with an album name.
3241 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3243 $this->markTestIncomplete('We need to add a dataset for this.');
3247 * Test the api_fr_photos_list() function.
3251 public function testApiFrPhotosList()
3253 $result = api_fr_photos_list('json');
3254 self::assertArrayHasKey('photo', $result);
3258 * Test the api_fr_photos_list() function without an authenticated user.
3262 public function testApiFrPhotosListWithoutAuthenticatedUser()
3264 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3265 $_SESSION['authenticated'] = false;
3266 api_fr_photos_list('json');
3270 * Test the api_fr_photo_create_update() function.
3272 public function testApiFrPhotoCreateUpdate()
3274 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3275 api_fr_photo_create_update('json');
3279 * Test the api_fr_photo_create_update() function without an authenticated user.
3283 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3285 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3286 $_SESSION['authenticated'] = false;
3287 api_fr_photo_create_update('json');
3291 * Test the api_fr_photo_create_update() function with an album name.
3295 public function testApiFrPhotoCreateUpdateWithAlbum()
3297 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3298 $_REQUEST['album'] = 'album_name';
3299 api_fr_photo_create_update('json');
3303 * Test the api_fr_photo_create_update() function with the update mode.
3307 public function testApiFrPhotoCreateUpdateWithUpdate()
3309 $this->markTestIncomplete('We need to create a dataset for this');
3313 * Test the api_fr_photo_create_update() function with an uploaded file.
3317 public function testApiFrPhotoCreateUpdateWithFile()
3319 $this->markTestIncomplete();
3323 * Test the api_fr_photo_delete() function.
3327 public function testApiFrPhotoDelete()
3329 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3330 api_fr_photo_delete('json');
3334 * Test the api_fr_photo_delete() function without an authenticated user.
3338 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3340 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3341 $_SESSION['authenticated'] = false;
3342 api_fr_photo_delete('json');
3346 * Test the api_fr_photo_delete() function with a photo ID.
3350 public function testApiFrPhotoDeleteWithPhotoId()
3352 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3353 $_REQUEST['photo_id'] = 1;
3354 api_fr_photo_delete('json');
3358 * Test the api_fr_photo_delete() function with a correct photo ID.
3362 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3364 $this->markTestIncomplete('We need to create a dataset for this.');
3368 * Test the api_fr_photo_detail() function.
3372 public function testApiFrPhotoDetail()
3374 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3375 api_fr_photo_detail('json');
3379 * Test the api_fr_photo_detail() function without an authenticated user.
3383 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3385 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3386 $_SESSION['authenticated'] = false;
3387 api_fr_photo_detail('json');
3391 * Test the api_fr_photo_detail() function with a photo ID.
3395 public function testApiFrPhotoDetailWithPhotoId()
3397 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3398 $_REQUEST['photo_id'] = 1;
3399 api_fr_photo_detail('json');
3403 * Test the api_fr_photo_detail() function with a correct photo ID.
3407 public function testApiFrPhotoDetailCorrectPhotoId()
3409 $this->markTestIncomplete('We need to create a dataset for this.');
3413 * Test the api_account_update_profile_image() function.
3417 public function testApiAccountUpdateProfileImage()
3419 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3420 api_account_update_profile_image('json');
3424 * Test the api_account_update_profile_image() function without an authenticated user.
3428 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3430 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3431 $_SESSION['authenticated'] = false;
3432 api_account_update_profile_image('json');
3436 * Test the api_account_update_profile_image() function with an uploaded file.
3440 public function testApiAccountUpdateProfileImageWithUpload()
3442 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3443 $this->markTestIncomplete();
3448 * Test the api_account_update_profile() function.
3452 public function testApiAccountUpdateProfile()
3454 $_POST['name'] = 'new_name';
3455 $_POST['description'] = 'new_description';
3456 $result = api_account_update_profile('json');
3457 // We can't use assertSelfUser() here because the user object is missing some properties.
3458 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3459 self::assertEquals('DFRN', $result['user']['location']);
3460 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3461 self::assertEquals('dfrn', $result['user']['network']);
3462 self::assertEquals('new_name', $result['user']['name']);
3463 self::assertEquals('new_description', $result['user']['description']);
3467 * Test the check_acl_input() function.
3471 public function testCheckAclInput()
3473 $result = check_acl_input('<aclstring>');
3474 // Where does this result come from?
3475 self::assertEquals(1, $result);
3479 * Test the check_acl_input() function with an empty ACL string.
3483 public function testCheckAclInputWithEmptyAclString()
3485 $result = check_acl_input(' ');
3486 self::assertFalse($result);
3490 * Test the save_media_to_database() function.
3494 public function testSaveMediaToDatabase()
3496 $this->markTestIncomplete();
3500 * Test the post_photo_item() function.
3504 public function testPostPhotoItem()
3506 $this->markTestIncomplete();
3510 * Test the prepare_photo_data() function.
3514 public function testPreparePhotoData()
3516 $this->markTestIncomplete();
3520 * Test the api_share_as_retweet() function with a valid item.
3524 public function testApiShareAsRetweetWithValidItem()
3526 $this->markTestIncomplete();
3530 * Test the api_in_reply_to() function.
3534 public function testApiInReplyTo()
3536 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3537 self::assertArrayHasKey('status_id', $result);
3538 self::assertArrayHasKey('user_id', $result);
3539 self::assertArrayHasKey('status_id_str', $result);
3540 self::assertArrayHasKey('user_id_str', $result);
3541 self::assertArrayHasKey('screen_name', $result);
3545 * Test the api_in_reply_to() function with a valid item.
3549 public function testApiInReplyToWithValidItem()
3551 $this->markTestIncomplete();
3555 * Test the api_clean_plain_items() function.
3559 public function testApiCleanPlainItems()
3561 $_REQUEST['include_entities'] = 'true';
3562 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3563 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3567 * Test the api_best_nickname() function.
3571 public function testApiBestNickname()
3574 $result = api_best_nickname($contacts);
3575 self::assertNull($result);
3579 * Test the api_best_nickname() function with contacts.
3583 public function testApiBestNicknameWithContacts()
3585 $this->markTestIncomplete();
3589 * Test the api_friendica_group_show() function.
3593 public function testApiFriendicaGroupShow()
3595 $this->markTestIncomplete();
3599 * Test the api_friendica_group_delete() function.
3603 public function testApiFriendicaGroupDelete()
3605 $this->markTestIncomplete();
3609 * Test the api_lists_destroy() function.
3613 public function testApiListsDestroy()
3615 $this->markTestIncomplete();
3619 * Test the group_create() function.
3623 public function testGroupCreate()
3625 $this->markTestIncomplete();
3629 * Test the api_friendica_group_create() function.
3633 public function testApiFriendicaGroupCreate()
3635 $this->markTestIncomplete();
3639 * Test the api_lists_create() function.
3643 public function testApiListsCreate()
3645 $this->markTestIncomplete();
3649 * Test the api_friendica_group_update() function.
3653 public function testApiFriendicaGroupUpdate()
3655 $this->markTestIncomplete();
3659 * Test the api_lists_update() function.
3663 public function testApiListsUpdate()
3665 $this->markTestIncomplete();
3669 * Test the api_friendica_activity() function.
3673 public function testApiFriendicaActivity()
3675 $this->markTestIncomplete();
3679 * Test the api_friendica_notification() function.
3683 public function testApiFriendicaNotification()
3685 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3686 api_friendica_notification('json');
3690 * Test the api_friendica_notification() function without an authenticated user.
3694 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3696 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3697 $_SESSION['authenticated'] = false;
3698 api_friendica_notification('json');
3702 * Test the api_friendica_notification() function with empty result
3706 public function testApiFriendicaNotificationWithEmptyResult()
3708 DI::args()->setArgv(['api', 'friendica', 'notification']);
3709 $_SESSION['uid'] = 41;
3710 $result = api_friendica_notification('json');
3711 self::assertEquals(['note' => false], $result);
3715 * Test the api_friendica_notification() function with an XML result.
3719 public function testApiFriendicaNotificationWithXmlResult()
3721 DI::args()->setArgv(['api', 'friendica', 'notification']);
3722 $result = api_friendica_notification('xml');
3723 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3724 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3727 <?xml version="1.0"?>
3729 <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"/>
3732 self::assertXmlStringEqualsXmlString($assertXml, $result);
3736 * Test the api_friendica_notification() function with an JSON result.
3740 public function testApiFriendicaNotificationWithJsonResult()
3742 DI::args()->setArgv(['api', 'friendica', 'notification']);
3743 $result = json_encode(api_friendica_notification('json'));
3744 self::assertJson($result);
3748 * Test the api_friendica_notification_seen() function.
3752 public function testApiFriendicaNotificationSeen()
3754 $this->markTestIncomplete();
3758 * Test the api_friendica_direct_messages_setseen() function.
3762 public function testApiFriendicaDirectMessagesSetseen()
3764 $this->markTestIncomplete();
3768 * Test the api_friendica_direct_messages_search() function.
3772 public function testApiFriendicaDirectMessagesSearch()
3774 $this->markTestIncomplete();
3778 * Test the api_saved_searches_list() function.
3782 public function testApiSavedSearchesList()
3784 $result = api_saved_searches_list('json');
3785 self::assertEquals(1, $result['terms'][0]['id']);
3786 self::assertEquals(1, $result['terms'][0]['id_str']);
3787 self::assertEquals('Saved search', $result['terms'][0]['name']);
3788 self::assertEquals('Saved search', $result['terms'][0]['query']);