6 namespace Friendica\Test\legacy;
9 use Friendica\Core\Config\Capability\IManageConfigValues;
10 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
11 use Friendica\Core\Protocol;
13 use Friendica\Module\BaseApi;
14 use Friendica\Network\HTTPException;
15 use Friendica\Security\BasicAuth;
16 use Friendica\Test\FixtureTest;
17 use Friendica\Util\Arrays;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Temporal;
20 use Monolog\Handler\TestHandler;
22 require_once __DIR__ . '/../../include/api.php';
25 * Tests for the API functions.
27 * Functions that use header() need to be tested in a separate process.
28 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
30 * @backupGlobals enabled
32 class ApiTest extends FixtureTest
35 * @var TestHandler Can handle log-outputs
42 protected $friendUser;
46 protected $wrongUserId;
51 /** @var IManageConfigValues */
55 * Create variables used by tests.
57 protected function setUp() : void
59 global $API, $called_api;
65 /** @var IManageConfigValues $config */
66 $this->config = $this->dice->create(IManageConfigValues::class);
68 $this->config->set('system', 'url', 'http://localhost');
69 $this->config->set('system', 'hostname', 'localhost');
70 $this->config->set('system', 'worker_dont_fork', true);
73 $this->config->set('config', 'hostname', 'localhost');
74 $this->config->set('system', 'throttle_limit_day', 100);
75 $this->config->set('system', 'throttle_limit_week', 100);
76 $this->config->set('system', 'throttle_limit_month', 100);
77 $this->config->set('system', 'theme', 'system_theme');
81 $this->app = DI::app();
83 DI::args()->setArgc(1);
85 // User data that the test database is populated with
88 'name' => 'Self contact',
89 'nick' => 'selfcontact',
90 'nurl' => 'http://localhost/profile/selfcontact'
94 'name' => 'Friend contact',
95 'nick' => 'friendcontact',
96 'nurl' => 'http://localhost/profile/friendcontact'
100 'name' => 'othercontact',
101 'nick' => 'othercontact',
102 'nurl' => 'http://localhost/profile/othercontact'
105 // User ID that we know is not in the database
106 $this->wrongUserId = 666;
108 DI::session()->start();
110 // Most API require login so we force the session
113 'authenticated' => true,
114 'uid' => $this->selfUser['id']
119 * Assert that an user array contains expected keys.
121 * @param array $user User array
125 private function assertSelfUser(array $user)
127 self::assertEquals($this->selfUser['id'], $user['uid']);
128 self::assertEquals($this->selfUser['id'], $user['cid']);
129 self::assertEquals(1, $user['self']);
130 self::assertEquals('DFRN', $user['location']);
131 self::assertEquals($this->selfUser['name'], $user['name']);
132 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
133 self::assertEquals('dfrn', $user['network']);
134 self::assertTrue($user['verified']);
138 * Assert that an user array contains expected keys.
140 * @param array $user User array
144 private function assertOtherUser(array $user = [])
146 self::assertEquals($this->otherUser['id'], $user['id']);
147 self::assertEquals($this->otherUser['id'], $user['id_str']);
148 self::assertEquals(0, $user['self']);
149 self::assertEquals($this->otherUser['name'], $user['name']);
150 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
151 self::assertFalse($user['verified']);
155 * Assert that a status array contains expected keys.
157 * @param array $status Status array
161 private function assertStatus(array $status = [])
163 self::assertIsString($status['text'] ?? '');
164 self::assertIsInt($status['id'] ?? '');
165 // We could probably do more checks here.
169 * Assert that a list array contains expected keys.
171 * @param array $list List array
175 private function assertList(array $list = [])
177 self::assertIsString($list['name']);
178 self::assertIsInt($list['id']);
179 self::assertIsString('string', $list['id_str']);
180 self::assertContains($list['mode'], ['public', 'private']);
181 // We could probably do more checks here.
185 * Assert that the string is XML and contain the root element.
187 * @param string $result XML string
188 * @param string $root_element Root element name
192 private function assertXml($result = '', $root_element = '')
194 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
195 self::assertStringContainsString('<' . $root_element, $result);
196 // We could probably do more checks here.
200 * Get the path to a temporary empty PNG image.
202 * @return string Path
204 private function getTempImage()
206 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
210 // Empty 1x1 px PNG image
211 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
219 * Test the api_user() function.
223 public function testApiUser()
225 self::assertEquals($this->selfUser['id'], api_user());
229 * Test the api_user() function with an unallowed user.
233 public function testApiUserWithUnallowedUser()
235 $_SESSION = ['allow_api' => false];
236 self::assertEquals(false, api_user());
240 * Test the api_source() function.
244 public function testApiSource()
246 self::assertEquals('api', api_source());
250 * Test the api_source() function with a Twidere user agent.
254 public function testApiSourceWithTwidere()
256 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
257 self::assertEquals('Twidere', api_source());
261 * Test the api_source() function with a GET parameter.
265 public function testApiSourceWithGet()
267 $_GET['source'] = 'source_name';
268 self::assertEquals('source_name', api_source());
272 * Test the api_date() function.
276 public function testApiDate()
278 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
282 * Test the api_register_func() function.
286 public function testApiRegisterFunc()
298 self::assertTrue($API['api_path']['auth']);
299 self::assertEquals('method', $API['api_path']['method']);
300 self::assertTrue(is_callable($API['api_path']['func']));
304 * Test the BasicAuth::getCurrentUserID() function without any login.
306 * @runInSeparateProcess
307 * @preserveGlobalState disabled
308 * @preserveGlobalState disabled
310 public function testApiLoginWithoutLogin()
312 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
313 BasicAuth::getCurrentUserID(true);
317 * Test the BasicAuth::getCurrentUserID() function with a bad login.
319 * @runInSeparateProcess
320 * @preserveGlobalState disabled
321 * @preserveGlobalState disabled
323 public function testApiLoginWithBadLogin()
325 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
326 $_SERVER['PHP_AUTH_USER'] = 'user@server';
327 BasicAuth::getCurrentUserID(true);
331 * Test the BasicAuth::getCurrentUserID() function with oAuth.
335 public function testApiLoginWithOauth()
337 $this->markTestIncomplete('Can we test this easily?');
341 * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
345 public function testApiLoginWithAddonAuth()
347 $this->markTestIncomplete('Can we test this easily?');
351 * Test the BasicAuth::getCurrentUserID() function with a correct login.
353 * @runInSeparateProcess
354 * @preserveGlobalState disabled
355 * @doesNotPerformAssertions
357 public function testApiLoginWithCorrectLogin()
359 $_SERVER['PHP_AUTH_USER'] = 'Test user';
360 $_SERVER['PHP_AUTH_PW'] = 'password';
361 BasicAuth::getCurrentUserID(true);
365 * Test the BasicAuth::getCurrentUserID() function with a remote user.
367 * @runInSeparateProcess
368 * @preserveGlobalState disabled
370 public function testApiLoginWithRemoteUser()
372 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
373 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
374 BasicAuth::getCurrentUserID(true);
378 * Test the api_check_method() function.
382 public function testApiCheckMethod()
384 self::assertFalse(api_check_method('method'));
388 * Test the api_check_method() function with a correct method.
392 public function testApiCheckMethodWithCorrectMethod()
394 $_SERVER['REQUEST_METHOD'] = 'method';
395 self::assertTrue(api_check_method('method'));
399 * Test the api_check_method() function with a wildcard.
403 public function testApiCheckMethodWithWildcard()
405 self::assertTrue(api_check_method('*'));
409 * Test the api_call() function.
411 * @runInSeparateProcess
412 * @preserveGlobalState disabled
414 public function testApiCall()
418 'method' => 'method',
419 'func' => function () {
420 return ['data' => ['some_data']];
423 $_SERVER['REQUEST_METHOD'] = 'method';
424 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
425 $_GET['callback'] = 'callback_name';
427 $args = DI::args()->determine($_SERVER, $_GET);
430 'callback_name(["some_data"])',
431 api_call($this->app, $args)
436 * Test the api_call() function with the profiled enabled.
438 * @runInSeparateProcess
439 * @preserveGlobalState disabled
441 public function testApiCallWithProfiler()
445 'method' => 'method',
446 'func' => function () {
447 return ['data' => ['some_data']];
451 $_SERVER['REQUEST_METHOD'] = 'method';
452 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
454 $args = DI::args()->determine($_SERVER, $_GET);
456 $this->config->set('system', 'profiler', true);
457 $this->config->set('rendertime', 'callstack', true);
458 $this->app->callstack = [
459 'database' => ['some_function' => 200],
460 'database_write' => ['some_function' => 200],
461 'cache' => ['some_function' => 200],
462 'cache_write' => ['some_function' => 200],
463 'network' => ['some_function' => 200]
468 api_call($this->app, $args)
473 * Test the api_call() function without any result.
475 * @runInSeparateProcess
476 * @preserveGlobalState disabled
478 public function testApiCallWithNoResult()
482 'method' => 'method',
483 'func' => function () {
487 $_SERVER['REQUEST_METHOD'] = 'method';
488 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
490 $args = DI::args()->determine($_SERVER, $_GET);
493 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
494 api_call($this->app, $args)
499 * Test the api_call() function with an unimplemented API.
501 * @runInSeparateProcess
502 * @preserveGlobalState disabled
504 public function testApiCallWithUninplementedApi()
507 '{"status":{"error":"Not Found","code":"404 Not Found","request":""}}',
513 * Test the api_call() function with a JSON result.
515 * @runInSeparateProcess
516 * @preserveGlobalState disabled
518 public function testApiCallWithJson()
522 'method' => 'method',
523 'func' => function () {
524 return ['data' => ['some_data']];
527 $_SERVER['REQUEST_METHOD'] = 'method';
528 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
530 $args = DI::args()->determine($_SERVER, $_GET);
534 api_call($this->app, $args)
539 * Test the api_call() function with an XML result.
541 * @runInSeparateProcess
542 * @preserveGlobalState disabled
544 public function testApiCallWithXml()
548 'method' => 'method',
549 'func' => function () {
553 $_SERVER['REQUEST_METHOD'] = 'method';
554 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
556 $args = DI::args()->determine($_SERVER, $_GET);
560 api_call($this->app, $args)
565 * Test the api_call() function with an RSS result.
567 * @runInSeparateProcess
568 * @preserveGlobalState disabled
570 public function testApiCallWithRss()
574 'method' => 'method',
575 'func' => function () {
579 $_SERVER['REQUEST_METHOD'] = 'method';
580 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
582 $args = DI::args()->determine($_SERVER, $_GET);
585 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
587 api_call($this->app, $args)
592 * Test the api_call() function with an Atom result.
594 * @runInSeparateProcess
595 * @preserveGlobalState disabled
597 public function testApiCallWithAtom()
601 'method' => 'method',
602 'func' => function () {
606 $_SERVER['REQUEST_METHOD'] = 'method';
607 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
609 $args = DI::args()->determine($_SERVER, $_GET);
612 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
614 api_call($this->app, $args)
619 * Test the api_call() function with an unallowed method.
621 * @runInSeparateProcess
622 * @preserveGlobalState disabled
624 public function testApiCallWithWrongMethod()
627 $API['api_path'] = ['method' => 'method'];
629 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
631 $args = DI::args()->determine($_SERVER, $_GET);
634 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
635 api_call($this->app, $args)
640 * Test the api_call() function with an unauthorized user.
642 * @runInSeparateProcess
643 * @preserveGlobalState disabled
645 public function testApiCallWithWrongAuth()
649 'method' => 'method',
652 $_SESSION['authenticated'] = false;
653 $_SERVER['REQUEST_METHOD'] = 'method';
654 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
656 $args = DI::args()->determine($_SERVER, $_GET);
659 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
660 api_call($this->app, $args)
665 * Test the api_error() function with a JSON result.
667 * @runInSeparateProcess
668 * @preserveGlobalState disabled
670 public function testApiErrorWithJson()
672 // @todo How to test the new API?
673 // self::assertEquals(
674 // '{"status":{"error":"error_message","code":"200 OK","request":""}}',
675 // api_error('json', new HTTPException\OKException('error_message'), DI::args())
680 * Test the api_error() function with an XML result.
682 * @runInSeparateProcess
683 * @preserveGlobalState disabled
685 public function testApiErrorWithXml()
687 // @todo How to test the new API?
690 '<?xml version="1.0"?>' . "\n" .
691 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
692 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
693 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
694 ' <error>error_message</error>' . "\n" .
695 ' <code>200 OK</code>' . "\n" .
696 ' <request/>' . "\n" .
698 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
704 * Test the api_error() function with an RSS result.
706 * @runInSeparateProcess
707 * @preserveGlobalState disabled
709 public function testApiErrorWithRss()
711 // @todo How to test the new API?
714 '<?xml version="1.0"?>' . "\n" .
715 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
716 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
717 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
718 ' <error>error_message</error>' . "\n" .
719 ' <code>200 OK</code>' . "\n" .
720 ' <request/>' . "\n" .
722 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
728 * Test the api_error() function with an Atom result.
730 * @runInSeparateProcess
731 * @preserveGlobalState disabled
733 public function testApiErrorWithAtom()
735 // @todo How to test the new API?
738 '<?xml version="1.0"?>' . "\n" .
739 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
740 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
741 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
742 ' <error>error_message</error>' . "\n" .
743 ' <code>200 OK</code>' . "\n" .
744 ' <request/>' . "\n" .
746 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
752 * Test the api_rss_extra() function.
756 public function testApiRssExtra()
758 $user_info = ['url' => 'user_url', 'lang' => 'en'];
759 $result = api_rss_extra($this->app, [], $user_info);
760 self::assertEquals($user_info, $result['$user']);
761 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
762 self::assertArrayHasKey('self', $result['$rss']);
763 self::assertArrayHasKey('base', $result['$rss']);
764 self::assertArrayHasKey('updated', $result['$rss']);
765 self::assertArrayHasKey('atom_updated', $result['$rss']);
766 self::assertArrayHasKey('language', $result['$rss']);
767 self::assertArrayHasKey('logo', $result['$rss']);
771 * Test the api_rss_extra() function without any user info.
775 public function testApiRssExtraWithoutUserInfo()
777 $result = api_rss_extra($this->app, [], null);
778 self::assertIsArray($result['$user']);
779 self::assertArrayHasKey('alternate', $result['$rss']);
780 self::assertArrayHasKey('self', $result['$rss']);
781 self::assertArrayHasKey('base', $result['$rss']);
782 self::assertArrayHasKey('updated', $result['$rss']);
783 self::assertArrayHasKey('atom_updated', $result['$rss']);
784 self::assertArrayHasKey('language', $result['$rss']);
785 self::assertArrayHasKey('logo', $result['$rss']);
789 * Test the api_unique_id_to_nurl() function.
793 public function testApiUniqueIdToNurl()
795 self::assertFalse(api_unique_id_to_nurl($this->wrongUserId));
799 * Test the api_unique_id_to_nurl() function with a correct ID.
803 public function testApiUniqueIdToNurlWithCorrectId()
805 self::assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
809 * Test the api_get_user() function.
813 public function testApiGetUser()
815 $user = api_get_user();
816 self::assertSelfUser($user);
817 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
818 self::assertEquals('6fdbe8', $user['profile_link_color']);
819 self::assertEquals('ededed', $user['profile_background_color']);
823 * Test the api_get_user() function with a Frio schema.
827 public function testApiGetUserWithFrioSchema()
829 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
830 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
831 $user = api_get_user();
832 self::assertSelfUser($user);
833 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
834 self::assertEquals('6fdbe8', $user['profile_link_color']);
835 self::assertEquals('ededed', $user['profile_background_color']);
839 * Test the api_get_user() function with an empty Frio schema.
843 public function testApiGetUserWithEmptyFrioSchema()
845 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
846 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
847 $user = api_get_user();
848 self::assertSelfUser($user);
849 self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
850 self::assertEquals('6fdbe8', $user['profile_link_color']);
851 self::assertEquals('ededed', $user['profile_background_color']);
855 * Test the api_get_user() function with a custom Frio schema.
859 public function testApiGetUserWithCustomFrioSchema()
861 $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
862 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
863 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
864 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
865 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
866 $user = api_get_user();
867 self::assertSelfUser($user);
868 self::assertEquals('123456', $user['profile_sidebar_fill_color']);
869 self::assertEquals('123456', $user['profile_link_color']);
870 self::assertEquals('123456', $user['profile_background_color']);
874 * Test the api_get_user() function with an user that is not allowed to use the API.
876 * @runInSeparateProcess
877 * @preserveGlobalState disabled
879 public function testApiGetUserWithoutApiUser()
881 $_SERVER['PHP_AUTH_USER'] = 'Test user';
882 $_SERVER['PHP_AUTH_PW'] = 'password';
883 $_SESSION['allow_api'] = false;
884 self::assertFalse(api_get_user());
888 * Test the api_get_user() function with an user ID in a GET parameter.
892 public function testApiGetUserWithGetId()
894 $_GET['user_id'] = $this->otherUser['id'];
895 self::assertOtherUser(api_get_user());
899 * Test the api_get_user() function with a wrong user ID in a GET parameter.
903 public function testApiGetUserWithWrongGetId()
905 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
906 $_GET['user_id'] = $this->wrongUserId;
907 self::assertOtherUser(api_get_user());
911 * Test the api_get_user() function with an user name in a GET parameter.
915 public function testApiGetUserWithGetName()
917 $_GET['screen_name'] = $this->selfUser['nick'];
918 self::assertSelfUser(api_get_user());
922 * Test the api_get_user() function with a profile URL in a GET parameter.
926 public function testApiGetUserWithGetUrl()
928 $_GET['profileurl'] = $this->selfUser['nurl'];
929 self::assertSelfUser(api_get_user());
933 * Test the api_get_user() function with an user ID in the API path.
937 public function testApiGetUserWithNumericCalledApi()
940 $called_api = ['api_path'];
941 DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
942 self::assertOtherUser(api_get_user());
946 * Test the api_get_user() function with the $called_api global variable.
950 public function testApiGetUserWithCalledApi()
953 $called_api = ['api', 'api_path'];
954 self::assertSelfUser(api_get_user());
958 * Test the api_get_user() function with a valid user.
962 public function testApiGetUserWithCorrectUser()
964 self::assertOtherUser(api_get_user($this->otherUser['id']));
968 * Test the api_get_user() function with a wrong user ID.
972 public function testApiGetUserWithWrongUser()
974 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
975 self::assertOtherUser(api_get_user($this->wrongUserId));
979 * Test the api_get_user() function with a 0 user ID.
983 public function testApiGetUserWithZeroUser()
985 self::assertSelfUser(api_get_user(0));
989 * Test the api_item_get_user() function.
993 public function testApiItemGetUser()
995 $users = api_item_get_user($this->app, []);
996 self::assertSelfUser($users[0]);
1000 * Test the api_item_get_user() function with a different item parent.
1004 public function testApiItemGetUserWithDifferentParent()
1006 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1007 self::assertSelfUser($users[0]);
1008 self::assertEquals($users[0], $users[1]);
1012 * Test the Arrays::walkRecursive() function.
1016 public function testApiWalkRecursive()
1021 Arrays::walkRecursive(
1024 // Should we test this with a callback that actually does something?
1032 * Test the Arrays::walkRecursive() function with an array.
1036 public function testApiWalkRecursiveWithArray()
1038 $array = [['item1'], ['item2']];
1041 Arrays::walkRecursive(
1044 // Should we test this with a callback that actually does something?
1052 * Test the BaseApi::reformatXML() function.
1056 public function testApiReformatXml()
1060 self::assertTrue(BaseApi::reformatXML($item, $key));
1061 self::assertEquals('true', $item);
1065 * Test the BaseApi::reformatXML() function with a statusnet_api key.
1069 public function testApiReformatXmlWithStatusnetKey()
1072 $key = 'statusnet_api';
1073 self::assertTrue(BaseApi::reformatXML($item, $key));
1074 self::assertEquals('statusnet:api', $key);
1078 * Test the BaseApi::reformatXML() function with a friendica_api key.
1082 public function testApiReformatXmlWithFriendicaKey()
1085 $key = 'friendica_api';
1086 self::assertTrue(BaseApi::reformatXML($item, $key));
1087 self::assertEquals('friendica:api', $key);
1091 * Test the BaseApi::createXML() function.
1095 public function testApiCreateXml()
1098 '<?xml version="1.0"?>' . "\n" .
1099 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1100 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1101 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1102 ' <data>some_data</data>' . "\n" .
1103 '</root_element>' . "\n",
1104 BaseApi::createXML(['data' => ['some_data']], 'root_element')
1109 * Test the BaseApi::createXML() function without any XML namespace.
1113 public function testApiCreateXmlWithoutNamespaces()
1116 '<?xml version="1.0"?>' . "\n" .
1118 ' <data>some_data</data>' . "\n" .
1120 BaseApi::createXML(['data' => ['some_data']], 'ok')
1125 * Test the BaseApi::formatData() function.
1129 public function testApiFormatData()
1131 $data = ['some_data'];
1132 self::assertEquals($data, BaseApi::formatData('root_element', 'json', $data));
1136 * Test the BaseApi::formatData() function with an XML result.
1140 public function testApiFormatDataWithXml()
1143 '<?xml version="1.0"?>' . "\n" .
1144 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1145 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1146 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1147 ' <data>some_data</data>' . "\n" .
1148 '</root_element>' . "\n",
1149 BaseApi::formatData('root_element', 'xml', ['data' => ['some_data']])
1154 * Test the api_account_verify_credentials() function.
1158 public function testApiAccountVerifyCredentials()
1160 self::assertArrayHasKey('user', api_account_verify_credentials('json'));
1164 * Test the api_account_verify_credentials() function without an authenticated user.
1168 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1170 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1171 $_SESSION['authenticated'] = false;
1172 api_account_verify_credentials('json');
1176 * Test the requestdata() function.
1180 public function testRequestdata()
1182 self::assertNull(requestdata('variable_name'));
1186 * Test the requestdata() function with a POST parameter.
1190 public function testRequestdataWithPost()
1192 $_POST['variable_name'] = 'variable_value';
1193 self::assertEquals('variable_value', requestdata('variable_name'));
1197 * Test the requestdata() function with a GET parameter.
1201 public function testRequestdataWithGet()
1203 $_GET['variable_name'] = 'variable_value';
1204 self::assertEquals('variable_value', requestdata('variable_name'));
1208 * Test the api_statuses_mediap() function.
1212 public function testApiStatusesMediap()
1214 DI::args()->setArgc(2);
1222 'tmp_name' => $this->getTempImage(),
1223 'name' => 'spacer.png',
1224 'type' => 'image/png'
1227 $_GET['status'] = '<b>Status content</b>';
1229 $result = api_statuses_mediap('json');
1230 self::assertStatus($result['status']);
1234 * Test the api_statuses_mediap() function without an authenticated user.
1238 public function testApiStatusesMediapWithoutAuthenticatedUser()
1240 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1241 $_SESSION['authenticated'] = false;
1242 api_statuses_mediap('json');
1246 * Test the api_statuses_update() function.
1250 public function testApiStatusesUpdate()
1252 $_GET['status'] = 'Status content #friendica';
1253 $_GET['in_reply_to_status_id'] = -1;
1262 'tmp_name' => $this->getTempImage(),
1263 'name' => 'spacer.png',
1264 'type' => 'image/png'
1268 $result = api_statuses_update('json');
1269 self::assertStatus($result['status']);
1273 * Test the api_statuses_update() function with an HTML status.
1277 public function testApiStatusesUpdateWithHtml()
1279 $_GET['htmlstatus'] = '<b>Status content</b>';
1281 $result = api_statuses_update('json');
1282 self::assertStatus($result['status']);
1286 * Test the api_statuses_update() function without an authenticated user.
1290 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1292 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1293 $_SESSION['authenticated'] = false;
1294 api_statuses_update('json');
1298 * Test the api_statuses_update() function with a parent status.
1302 public function testApiStatusesUpdateWithParent()
1304 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1308 * Test the api_statuses_update() function with a media_ids parameter.
1312 public function testApiStatusesUpdateWithMediaIds()
1314 $this->markTestIncomplete();
1318 * Test the api_statuses_update() function with the throttle limit reached.
1322 public function testApiStatusesUpdateWithDayThrottleReached()
1324 $this->markTestIncomplete();
1328 * Test the api_media_upload() function.
1329 * @runInSeparateProcess
1330 * @preserveGlobalState disabled
1332 public function testApiMediaUpload()
1334 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1339 * Test the api_media_upload() function without an authenticated user.
1343 public function testApiMediaUploadWithoutAuthenticatedUser()
1345 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1346 $_SESSION['authenticated'] = false;
1351 * Test the api_media_upload() function with an invalid uploaded media.
1355 public function testApiMediaUploadWithMedia()
1357 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1361 'tmp_name' => 'tmp_name'
1368 * Test the api_media_upload() function with an valid uploaded media.
1372 public function testApiMediaUploadWithValidMedia()
1380 'tmp_name' => $this->getTempImage(),
1381 'name' => 'spacer.png',
1382 'type' => 'image/png'
1386 DI::args()->setArgc(2);
1388 $result = api_media_upload();
1389 self::assertEquals('image/png', $result['media']['image']['image_type']);
1390 self::assertEquals(1, $result['media']['image']['w']);
1391 self::assertEquals(1, $result['media']['image']['h']);
1392 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1396 * Test the api_status_show() function.
1398 public function testApiStatusShowWithJson()
1400 $result = api_status_show('json', 1);
1401 self::assertStatus($result['status']);
1405 * Test the api_status_show() function with an XML result.
1407 public function testApiStatusShowWithXml()
1409 $result = api_status_show('xml', 1);
1410 self::assertXml($result, 'statuses');
1414 * Test the api_get_last_status() function
1416 public function testApiGetLastStatus()
1418 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1420 self::assertNotNull($item);
1424 * Test the api_users_show() function.
1428 public function testApiUsersShow()
1430 $result = api_users_show('json');
1431 // We can't use assertSelfUser() here because the user object is missing some properties.
1432 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
1433 self::assertEquals('DFRN', $result['user']['location']);
1434 self::assertEquals($this->selfUser['name'], $result['user']['name']);
1435 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1436 self::assertEquals('dfrn', $result['user']['network']);
1437 self::assertTrue($result['user']['verified']);
1441 * Test the api_users_show() function with an XML result.
1445 public function testApiUsersShowWithXml()
1447 $result = api_users_show('xml');
1448 self::assertXml($result, 'statuses');
1452 * Test the api_users_search() function.
1456 public function testApiUsersSearch()
1458 $_GET['q'] = 'othercontact';
1459 $result = api_users_search('json');
1460 self::assertOtherUser($result['users'][0]);
1464 * Test the api_users_search() function with an XML result.
1468 public function testApiUsersSearchWithXml()
1470 $_GET['q'] = 'othercontact';
1471 $result = api_users_search('xml');
1472 self::assertXml($result, 'users');
1476 * Test the api_users_search() function without a GET q parameter.
1480 public function testApiUsersSearchWithoutQuery()
1482 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1483 api_users_search('json');
1487 * Test the api_users_lookup() function.
1491 public function testApiUsersLookup()
1493 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1494 api_users_lookup('json');
1498 * Test the api_users_lookup() function with an user ID.
1502 public function testApiUsersLookupWithUserId()
1504 $_REQUEST['user_id'] = $this->otherUser['id'];
1505 $result = api_users_lookup('json');
1506 self::assertOtherUser($result['users'][0]);
1510 * Test the api_search() function.
1514 public function testApiSearch()
1516 $_REQUEST['q'] = 'reply';
1517 $_REQUEST['max_id'] = 10;
1518 $result = api_search('json');
1519 foreach ($result['status'] as $status) {
1520 self::assertStatus($status);
1521 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1526 * Test the api_search() function a count parameter.
1530 public function testApiSearchWithCount()
1532 $_REQUEST['q'] = 'reply';
1533 $_REQUEST['count'] = 20;
1534 $result = api_search('json');
1535 foreach ($result['status'] as $status) {
1536 self::assertStatus($status);
1537 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1542 * Test the api_search() function with an rpp parameter.
1546 public function testApiSearchWithRpp()
1548 $_REQUEST['q'] = 'reply';
1549 $_REQUEST['rpp'] = 20;
1550 $result = api_search('json');
1551 foreach ($result['status'] as $status) {
1552 self::assertStatus($status);
1553 self::assertStringContainsStringIgnoringCase('reply', $status['text'], '', true);
1558 * Test the api_search() function with an q parameter contains hashtag.
1559 * @doesNotPerformAssertions
1561 public function testApiSearchWithHashtag()
1563 $_REQUEST['q'] = '%23friendica';
1564 $result = api_search('json');
1565 foreach ($result['status'] as $status) {
1566 self::assertStatus($status);
1567 self::assertStringContainsStringIgnoringCase('#friendica', $status['text'], '', true);
1572 * Test the api_search() function with an exclude_replies parameter.
1573 * @doesNotPerformAssertions
1575 public function testApiSearchWithExcludeReplies()
1577 $_REQUEST['max_id'] = 10;
1578 $_REQUEST['exclude_replies'] = true;
1579 $_REQUEST['q'] = 'friendica';
1580 $result = api_search('json');
1581 foreach ($result['status'] as $status) {
1582 self::assertStatus($status);
1587 * Test the api_search() function without an authenticated user.
1591 public function testApiSearchWithUnallowedUser()
1593 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1594 $_SESSION['allow_api'] = false;
1595 $_GET['screen_name'] = $this->selfUser['nick'];
1600 * Test the api_search() function without any GET query parameter.
1604 public function testApiSearchWithoutQuery()
1606 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1611 * Test the api_statuses_home_timeline() function.
1615 public function testApiStatusesHomeTimeline()
1617 $_REQUEST['max_id'] = 10;
1618 $_REQUEST['exclude_replies'] = true;
1619 $_REQUEST['conversation_id'] = 1;
1620 $result = api_statuses_home_timeline('json');
1621 self::assertNotEmpty($result['status']);
1622 foreach ($result['status'] as $status) {
1623 self::assertStatus($status);
1628 * Test the api_statuses_home_timeline() function with a negative page parameter.
1632 public function testApiStatusesHomeTimelineWithNegativePage()
1634 $_REQUEST['page'] = -2;
1635 $result = api_statuses_home_timeline('json');
1636 self::assertNotEmpty($result['status']);
1637 foreach ($result['status'] as $status) {
1638 self::assertStatus($status);
1643 * Test the api_statuses_home_timeline() with an unallowed user.
1647 public function testApiStatusesHomeTimelineWithUnallowedUser()
1649 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1650 $_SESSION['allow_api'] = false;
1651 $_GET['screen_name'] = $this->selfUser['nick'];
1652 api_statuses_home_timeline('json');
1656 * Test the api_statuses_home_timeline() function with an RSS result.
1660 public function testApiStatusesHomeTimelineWithRss()
1662 $result = api_statuses_home_timeline('rss');
1663 self::assertXml($result, 'statuses');
1667 * Test the api_statuses_public_timeline() function.
1671 public function testApiStatusesPublicTimeline()
1673 $_REQUEST['max_id'] = 10;
1674 $_REQUEST['conversation_id'] = 1;
1675 $result = api_statuses_public_timeline('json');
1676 self::assertNotEmpty($result['status']);
1677 foreach ($result['status'] as $status) {
1678 self::assertStatus($status);
1683 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1687 public function testApiStatusesPublicTimelineWithExcludeReplies()
1689 $_REQUEST['max_id'] = 10;
1690 $_REQUEST['exclude_replies'] = true;
1691 $result = api_statuses_public_timeline('json');
1692 self::assertNotEmpty($result['status']);
1693 foreach ($result['status'] as $status) {
1694 self::assertStatus($status);
1699 * Test the api_statuses_public_timeline() function with a negative page parameter.
1703 public function testApiStatusesPublicTimelineWithNegativePage()
1705 $_REQUEST['page'] = -2;
1706 $result = api_statuses_public_timeline('json');
1707 self::assertNotEmpty($result['status']);
1708 foreach ($result['status'] as $status) {
1709 self::assertStatus($status);
1714 * Test the api_statuses_public_timeline() function with an unallowed user.
1718 public function testApiStatusesPublicTimelineWithUnallowedUser()
1720 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1721 $_SESSION['allow_api'] = false;
1722 $_GET['screen_name'] = $this->selfUser['nick'];
1723 api_statuses_public_timeline('json');
1727 * Test the api_statuses_public_timeline() function with an RSS result.
1731 public function testApiStatusesPublicTimelineWithRss()
1733 $result = api_statuses_public_timeline('rss');
1734 self::assertXml($result, 'statuses');
1738 * Test the api_statuses_networkpublic_timeline() function.
1742 public function testApiStatusesNetworkpublicTimeline()
1744 $_REQUEST['max_id'] = 10;
1745 $result = api_statuses_networkpublic_timeline('json');
1746 self::assertNotEmpty($result['status']);
1747 foreach ($result['status'] as $status) {
1748 self::assertStatus($status);
1753 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1757 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1759 $_REQUEST['page'] = -2;
1760 $result = api_statuses_networkpublic_timeline('json');
1761 self::assertNotEmpty($result['status']);
1762 foreach ($result['status'] as $status) {
1763 self::assertStatus($status);
1768 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1772 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1774 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1775 $_SESSION['allow_api'] = false;
1776 $_GET['screen_name'] = $this->selfUser['nick'];
1777 api_statuses_networkpublic_timeline('json');
1781 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1785 public function testApiStatusesNetworkpublicTimelineWithRss()
1787 $result = api_statuses_networkpublic_timeline('rss');
1788 self::assertXml($result, 'statuses');
1792 * Test the api_statuses_show() function.
1796 public function testApiStatusesShow()
1798 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1799 api_statuses_show('json');
1803 * Test the api_statuses_show() function with an ID.
1807 public function testApiStatusesShowWithId()
1809 DI::args()->setArgv(['', '', '', 1]);
1810 $result = api_statuses_show('json');
1811 self::assertStatus($result['status']);
1815 * Test the api_statuses_show() function with the conversation parameter.
1819 public function testApiStatusesShowWithConversation()
1821 DI::args()->setArgv(['', '', '', 1]);
1822 $_REQUEST['conversation'] = 1;
1823 $result = api_statuses_show('json');
1824 self::assertNotEmpty($result['status']);
1825 foreach ($result['status'] as $status) {
1826 self::assertStatus($status);
1831 * Test the api_statuses_show() function with an unallowed user.
1835 public function testApiStatusesShowWithUnallowedUser()
1837 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1838 $_SESSION['allow_api'] = false;
1839 $_GET['screen_name'] = $this->selfUser['nick'];
1840 api_statuses_show('json');
1844 * Test the api_conversation_show() function.
1848 public function testApiConversationShow()
1850 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1851 api_conversation_show('json');
1855 * Test the api_conversation_show() function with an ID.
1859 public function testApiConversationShowWithId()
1861 DI::args()->setArgv(['', '', '', 1]);
1862 $_REQUEST['max_id'] = 10;
1863 $_REQUEST['page'] = -2;
1864 $result = api_conversation_show('json');
1865 self::assertNotEmpty($result['status']);
1866 foreach ($result['status'] as $status) {
1867 self::assertStatus($status);
1872 * Test the api_conversation_show() function with an unallowed user.
1876 public function testApiConversationShowWithUnallowedUser()
1878 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1879 $_SESSION['allow_api'] = false;
1880 $_GET['screen_name'] = $this->selfUser['nick'];
1881 api_conversation_show('json');
1885 * Test the api_statuses_repeat() function.
1889 public function testApiStatusesRepeat()
1891 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1892 api_statuses_repeat('json');
1896 * Test the api_statuses_repeat() function without an authenticated user.
1900 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1902 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1903 $_SESSION['authenticated'] = false;
1904 api_statuses_repeat('json');
1908 * Test the api_statuses_repeat() function with an ID.
1912 public function testApiStatusesRepeatWithId()
1914 DI::args()->setArgv(['', '', '', 1]);
1915 $result = api_statuses_repeat('json');
1916 self::assertStatus($result['status']);
1918 // Also test with a shared status
1919 DI::args()->setArgv(['', '', '', 5]);
1920 $result = api_statuses_repeat('json');
1921 self::assertStatus($result['status']);
1925 * Test the api_statuses_destroy() function.
1929 public function testApiStatusesDestroy()
1931 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1932 api_statuses_destroy('json');
1936 * Test the api_statuses_destroy() function without an authenticated user.
1940 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1942 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1943 $_SESSION['authenticated'] = false;
1944 api_statuses_destroy('json');
1948 * Test the api_statuses_destroy() function with an ID.
1952 public function testApiStatusesDestroyWithId()
1954 DI::args()->setArgv(['', '', '', 1]);
1955 $result = api_statuses_destroy('json');
1956 self::assertStatus($result['status']);
1960 * Test the api_statuses_mentions() function.
1964 public function testApiStatusesMentions()
1966 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1967 $_REQUEST['max_id'] = 10;
1968 $result = api_statuses_mentions('json');
1969 self::assertEmpty($result['status']);
1970 // We should test with mentions in the database.
1974 * Test the api_statuses_mentions() function with a negative page parameter.
1978 public function testApiStatusesMentionsWithNegativePage()
1980 $_REQUEST['page'] = -2;
1981 $result = api_statuses_mentions('json');
1982 self::assertEmpty($result['status']);
1986 * Test the api_statuses_mentions() function with an unallowed user.
1990 public function testApiStatusesMentionsWithUnallowedUser()
1992 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1993 $_SESSION['allow_api'] = false;
1994 $_GET['screen_name'] = $this->selfUser['nick'];
1995 api_statuses_mentions('json');
1999 * Test the api_statuses_mentions() function with an RSS result.
2003 public function testApiStatusesMentionsWithRss()
2005 $result = api_statuses_mentions('rss');
2006 self::assertXml($result, 'statuses');
2010 * Test the api_statuses_user_timeline() function.
2014 public function testApiStatusesUserTimeline()
2016 $_REQUEST['max_id'] = 10;
2017 $_REQUEST['exclude_replies'] = true;
2018 $_REQUEST['conversation_id'] = 1;
2019 $result = api_statuses_user_timeline('json');
2020 self::assertNotEmpty($result['status']);
2021 foreach ($result['status'] as $status) {
2022 self::assertStatus($status);
2027 * Test the api_statuses_user_timeline() function with a negative page parameter.
2031 public function testApiStatusesUserTimelineWithNegativePage()
2033 $_REQUEST['page'] = -2;
2034 $result = api_statuses_user_timeline('json');
2035 self::assertNotEmpty($result['status']);
2036 foreach ($result['status'] as $status) {
2037 self::assertStatus($status);
2042 * Test the api_statuses_user_timeline() function with an RSS result.
2046 public function testApiStatusesUserTimelineWithRss()
2048 $result = api_statuses_user_timeline('rss');
2049 self::assertXml($result, 'statuses');
2053 * Test the api_statuses_user_timeline() function with an unallowed user.
2057 public function testApiStatusesUserTimelineWithUnallowedUser()
2059 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2060 $_SESSION['allow_api'] = false;
2061 $_GET['screen_name'] = $this->selfUser['nick'];
2062 api_statuses_user_timeline('json');
2066 * Test the api_favorites_create_destroy() function.
2070 public function testApiFavoritesCreateDestroy()
2072 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2073 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
2074 api_favorites_create_destroy('json');
2078 * Test the api_favorites_create_destroy() function with an invalid ID.
2082 public function testApiFavoritesCreateDestroyWithInvalidId()
2084 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2085 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
2086 api_favorites_create_destroy('json');
2090 * Test the api_favorites_create_destroy() function with an invalid action.
2094 public function testApiFavoritesCreateDestroyWithInvalidAction()
2096 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2097 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
2098 $_REQUEST['id'] = 1;
2099 api_favorites_create_destroy('json');
2103 * Test the api_favorites_create_destroy() function with the create action.
2107 public function testApiFavoritesCreateDestroyWithCreateAction()
2109 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2110 $_REQUEST['id'] = 3;
2111 $result = api_favorites_create_destroy('json');
2112 self::assertStatus($result['status']);
2116 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2120 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2122 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
2123 $_REQUEST['id'] = 3;
2124 $result = api_favorites_create_destroy('rss');
2125 self::assertXml($result, 'status');
2129 * Test the api_favorites_create_destroy() function with the destroy action.
2133 public function testApiFavoritesCreateDestroyWithDestroyAction()
2135 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
2136 $_REQUEST['id'] = 3;
2137 $result = api_favorites_create_destroy('json');
2138 self::assertStatus($result['status']);
2142 * Test the api_favorites_create_destroy() function without an authenticated user.
2146 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2148 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2149 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
2150 $_SESSION['authenticated'] = false;
2151 api_favorites_create_destroy('json');
2155 * Test the api_favorites() function.
2159 public function testApiFavorites()
2161 $_REQUEST['page'] = -1;
2162 $_REQUEST['max_id'] = 10;
2163 $result = api_favorites('json');
2164 foreach ($result['status'] as $status) {
2165 self::assertStatus($status);
2170 * Test the api_favorites() function with an RSS result.
2174 public function testApiFavoritesWithRss()
2176 $result = api_favorites('rss');
2177 self::assertXml($result, 'statuses');
2181 * Test the api_favorites() function with an unallowed user.
2185 public function testApiFavoritesWithUnallowedUser()
2187 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2188 $_SESSION['allow_api'] = false;
2189 $_GET['screen_name'] = $this->selfUser['nick'];
2190 api_favorites('json');
2194 * Test the api_format_messages() function.
2198 public function testApiFormatMessages()
2200 $result = api_format_messages(
2201 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2202 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2203 ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
2205 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2206 self::assertEquals(1, $result['id']);
2207 self::assertEquals(2, $result['recipient_id']);
2208 self::assertEquals(3, $result['sender_id']);
2209 self::assertEquals('recipient_name', $result['recipient_screen_name']);
2210 self::assertEquals('sender_name', $result['sender_screen_name']);
2214 * Test the api_format_messages() function with HTML.
2218 public function testApiFormatMessagesWithHtmlText()
2220 $_GET['getText'] = 'html';
2221 $result = api_format_messages(
2222 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2223 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2224 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2226 self::assertEquals('item_title', $result['title']);
2227 self::assertEquals('<strong>item_body</strong>', $result['text']);
2231 * Test the api_format_messages() function with plain text.
2235 public function testApiFormatMessagesWithPlainText()
2237 $_GET['getText'] = 'plain';
2238 $result = api_format_messages(
2239 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2240 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2241 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2243 self::assertEquals('item_title', $result['title']);
2244 self::assertEquals('item_body', $result['text']);
2248 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2252 public function testApiFormatMessagesWithoutUserObjects()
2254 $_GET['getUserObjects'] = 'false';
2255 $result = api_format_messages(
2256 ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2257 ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
2258 ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
2260 self::assertTrue(!isset($result['sender']));
2261 self::assertTrue(!isset($result['recipient']));
2265 * Test the api_convert_item() function.
2269 public function testApiConvertItem()
2271 $result = api_convert_item(
2273 'network' => 'feed',
2274 'title' => 'item_title',
2276 // We need a long string to test that it is correctly cut
2277 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2278 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2279 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2280 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2281 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2282 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2283 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2284 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2285 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2286 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2287 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2288 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2289 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2290 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2291 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2292 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2293 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2294 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2295 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2296 'repellat officia illum quos impedit quam iste esse unde qui ' .
2297 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2298 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2299 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2300 'plink' => 'item_plink'
2303 self::assertStringStartsWith('item_title', $result['text']);
2304 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2308 * Test the api_convert_item() function with an empty item body.
2312 public function testApiConvertItemWithoutBody()
2314 $result = api_convert_item(
2316 'network' => 'feed',
2317 'title' => 'item_title',
2320 'plink' => 'item_plink'
2323 self::assertEquals("item_title", $result['text']);
2324 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2328 * Test the api_convert_item() function with the title in the body.
2332 public function testApiConvertItemWithTitleInBody()
2334 $result = api_convert_item(
2336 'title' => 'item_title',
2337 'body' => 'item_title item_body',
2341 self::assertEquals('item_title item_body', $result['text']);
2342 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2346 * Test the api_get_attachments() function.
2350 public function testApiGetAttachments()
2353 self::assertEmpty(api_get_attachments($body, 0));
2357 * Test the api_get_attachments() function with an img tag.
2361 public function testApiGetAttachmentsWithImage()
2363 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2364 self::assertIsArray(api_get_attachments($body, 0));
2368 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2372 public function testApiGetAttachmentsWithImageAndAndStatus()
2374 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2375 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2376 self::assertIsArray(api_get_attachments($body, 0));
2380 * Test the api_get_entitities() function.
2384 public function testApiGetEntitities()
2387 self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
2391 * Test the api_get_entitities() function with the include_entities parameter.
2395 public function testApiGetEntititiesWithIncludeEntities()
2397 $_REQUEST['include_entities'] = 'true';
2399 $result = api_get_entitities($text, 'bbcode', 0);
2400 self::assertIsArray($result['hashtags']);
2401 self::assertIsArray($result['symbols']);
2402 self::assertIsArray($result['urls']);
2403 self::assertIsArray($result['user_mentions']);
2407 * Test the api_format_items_embeded_images() function.
2411 public function testApiFormatItemsEmbededImages()
2414 'text ' . DI::baseUrl() . '/display/item_guid',
2415 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2420 * Test the api_contactlink_to_array() function.
2424 public function testApiContactlinkToArray()
2431 api_contactlink_to_array('text')
2436 * Test the api_contactlink_to_array() function with an URL.
2440 public function testApiContactlinkToArrayWithUrl()
2444 'name' => ['link_text'],
2447 api_contactlink_to_array('text <a href="url">link_text</a>')
2452 * Test the api_format_items_activities() function.
2456 public function testApiFormatItemsActivities()
2458 $item = ['uid' => 0, 'uri' => ''];
2459 $result = api_format_items_activities($item);
2460 self::assertArrayHasKey('like', $result);
2461 self::assertArrayHasKey('dislike', $result);
2462 self::assertArrayHasKey('attendyes', $result);
2463 self::assertArrayHasKey('attendno', $result);
2464 self::assertArrayHasKey('attendmaybe', $result);
2468 * Test the api_format_items_activities() function with an XML result.
2472 public function testApiFormatItemsActivitiesWithXml()
2474 $item = ['uid' => 0, 'uri' => ''];
2475 $result = api_format_items_activities($item, 'xml');
2476 self::assertArrayHasKey('friendica:like', $result);
2477 self::assertArrayHasKey('friendica:dislike', $result);
2478 self::assertArrayHasKey('friendica:attendyes', $result);
2479 self::assertArrayHasKey('friendica:attendno', $result);
2480 self::assertArrayHasKey('friendica:attendmaybe', $result);
2484 * Test the api_format_items() function.
2485 * @doesNotPerformAssertions
2487 public function testApiFormatItems()
2491 'item_network' => 'item_network',
2497 'author-network' => Protocol::DFRN,
2498 'author-link' => 'http://localhost/profile/othercontact',
2502 $result = api_format_items($items, ['id' => 0], true);
2503 foreach ($result as $status) {
2504 self::assertStatus($status);
2509 * Test the api_format_items() function with an XML result.
2510 * @doesNotPerformAssertions
2512 public function testApiFormatItemsWithXml()
2520 'author-network' => Protocol::DFRN,
2521 'author-link' => 'http://localhost/profile/othercontact',
2525 $result = api_format_items($items, ['id' => 0], true, 'xml');
2526 foreach ($result as $status) {
2527 self::assertStatus($status);
2532 * Test the api_format_items() function.
2536 public function testApiAccountRateLimitStatus()
2538 // @todo How to test the new API?
2539 // $result = api_account_rate_limit_status('json');
2540 // self::assertEquals(150, $result['hash']['remaining_hits']);
2541 // self::assertEquals(150, $result['hash']['hourly_limit']);
2542 // self::assertIsInt($result['hash']['reset_time_in_seconds']);
2546 * Test the api_format_items() function with an XML result.
2550 public function testApiAccountRateLimitStatusWithXml()
2552 // @todo How to test the new API?
2553 // $result = api_account_rate_limit_status('xml');
2554 // self::assertXml($result, 'hash');
2558 * Test the api_help_test() function.
2562 public function testApiHelpTest()
2564 // @todo How to test the new API?
2565 // $result = \Friendica\Module\Api\Friendica\Help\Test::rawcontent(['extension' => 'json']);
2566 // self::assertEquals(['ok' => 'ok'], $result);
2570 * Test the api_help_test() function with an XML result.
2574 public function testApiHelpTestWithXml()
2576 // @todo How to test the new API?
2577 // $result = api_help_test('xml');
2578 // self::assertXml($result, 'ok');
2582 * Test the api_lists_list() function.
2586 public function testApiListsList()
2588 $result = api_lists_list('json');
2589 self::assertEquals(['lists_list' => []], $result);
2593 * Test the api_lists_ownerships() function.
2597 public function testApiListsOwnerships()
2599 $result = api_lists_ownerships('json');
2600 foreach ($result['lists']['lists'] as $list) {
2601 self::assertList($list);
2606 * Test the api_lists_ownerships() function without an authenticated user.
2610 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2612 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2613 $_SESSION['authenticated'] = false;
2614 api_lists_ownerships('json');
2618 * Test the api_lists_statuses() function.
2622 public function testApiListsStatuses()
2624 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2625 api_lists_statuses('json');
2629 * Test the api_lists_statuses() function with a list ID.
2630 * @doesNotPerformAssertions
2632 public function testApiListsStatusesWithListId()
2634 $_REQUEST['list_id'] = 1;
2635 $_REQUEST['page'] = -1;
2636 $_REQUEST['max_id'] = 10;
2637 $result = api_lists_statuses('json');
2638 foreach ($result['status'] as $status) {
2639 self::assertStatus($status);
2644 * Test the api_lists_statuses() function with a list ID and a RSS result.
2648 public function testApiListsStatusesWithListIdAndRss()
2650 $_REQUEST['list_id'] = 1;
2651 $result = api_lists_statuses('rss');
2652 self::assertXml($result, 'statuses');
2656 * Test the api_lists_statuses() function with an unallowed user.
2660 public function testApiListsStatusesWithUnallowedUser()
2662 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2663 $_SESSION['allow_api'] = false;
2664 $_GET['screen_name'] = $this->selfUser['nick'];
2665 api_lists_statuses('json');
2669 * Test the api_statuses_f() function.
2673 public function testApiStatusesFWithFriends()
2676 $result = api_statuses_f('friends');
2677 self::assertArrayHasKey('user', $result);
2681 * Test the api_statuses_f() function.
2685 public function testApiStatusesFWithFollowers()
2687 $result = api_statuses_f('followers');
2688 self::assertArrayHasKey('user', $result);
2692 * Test the api_statuses_f() function.
2696 public function testApiStatusesFWithBlocks()
2698 $result = api_statuses_f('blocks');
2699 self::assertArrayHasKey('user', $result);
2703 * Test the api_statuses_f() function.
2707 public function testApiStatusesFWithIncoming()
2709 $result = api_statuses_f('incoming');
2710 self::assertArrayHasKey('user', $result);
2714 * Test the api_statuses_f() function an undefined cursor GET variable.
2718 public function testApiStatusesFWithUndefinedCursor()
2720 $_GET['cursor'] = 'undefined';
2721 self::assertFalse(api_statuses_f('friends'));
2725 * Test the api_statuses_friends() function.
2729 public function testApiStatusesFriends()
2731 $result = api_statuses_friends('json');
2732 self::assertArrayHasKey('user', $result);
2736 * Test the api_statuses_friends() function an undefined cursor GET variable.
2740 public function testApiStatusesFriendsWithUndefinedCursor()
2742 $_GET['cursor'] = 'undefined';
2743 self::assertFalse(api_statuses_friends('json'));
2747 * Test the api_statuses_followers() function.
2751 public function testApiStatusesFollowers()
2753 $result = api_statuses_followers('json');
2754 self::assertArrayHasKey('user', $result);
2758 * Test the api_statuses_followers() function an undefined cursor GET variable.
2762 public function testApiStatusesFollowersWithUndefinedCursor()
2764 $_GET['cursor'] = 'undefined';
2765 self::assertFalse(api_statuses_followers('json'));
2769 * Test the api_blocks_list() function.
2773 public function testApiBlocksList()
2775 $result = api_blocks_list('json');
2776 self::assertArrayHasKey('user', $result);
2780 * Test the api_blocks_list() function an undefined cursor GET variable.
2784 public function testApiBlocksListWithUndefinedCursor()
2786 $_GET['cursor'] = 'undefined';
2787 self::assertFalse(api_blocks_list('json'));
2791 * Test the api_friendships_incoming() function.
2795 public function testApiFriendshipsIncoming()
2797 $result = api_friendships_incoming('json');
2798 self::assertArrayHasKey('id', $result);
2802 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2806 public function testApiFriendshipsIncomingWithUndefinedCursor()
2808 $_GET['cursor'] = 'undefined';
2809 self::assertFalse(api_friendships_incoming('json'));
2813 * Test the api_statusnet_config() function.
2817 public function testApiStatusnetConfig()
2819 $result = api_statusnet_config('json');
2820 self::assertEquals('localhost', $result['config']['site']['server']);
2821 self::assertEquals('default', $result['config']['site']['theme']);
2822 self::assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2823 self::assertTrue($result['config']['site']['fancy']);
2824 self::assertEquals('en', $result['config']['site']['language']);
2825 self::assertEquals('UTC', $result['config']['site']['timezone']);
2826 self::assertEquals(200000, $result['config']['site']['textlimit']);
2827 self::assertEquals('false', $result['config']['site']['private']);
2828 self::assertEquals('false', $result['config']['site']['ssl']);
2829 self::assertEquals(30, $result['config']['site']['shorturllength']);
2833 * Test the api_statusnet_version() function.
2837 public function testApiStatusnetVersion()
2839 // @todo How to test the new API?
2840 // $result = api_statusnet_version('json');
2841 // self::assertEquals('0.9.7', $result['version']);
2845 * Test the api_direct_messages_new() function.
2849 public function testApiDirectMessagesNew()
2851 $result = api_direct_messages_new('json');
2852 self::assertNull($result);
2856 * Test the api_direct_messages_new() function without an authenticated user.
2860 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2862 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2863 $_SESSION['authenticated'] = false;
2864 api_direct_messages_new('json');
2868 * Test the api_direct_messages_new() function with an user ID.
2872 public function testApiDirectMessagesNewWithUserId()
2874 $_POST['text'] = 'message_text';
2875 $_POST['user_id'] = $this->otherUser['id'];
2876 $result = api_direct_messages_new('json');
2877 self::assertEquals(['direct_message' => ['error' => -1]], $result);
2881 * Test the api_direct_messages_new() function with a screen name.
2885 public function testApiDirectMessagesNewWithScreenName()
2887 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2888 $_POST['text'] = 'message_text';
2889 $_POST['screen_name'] = $this->friendUser['nick'];
2890 $result = api_direct_messages_new('json');
2891 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2892 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2893 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2897 * Test the api_direct_messages_new() function with a title.
2901 public function testApiDirectMessagesNewWithTitle()
2903 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2904 $_POST['text'] = 'message_text';
2905 $_POST['screen_name'] = $this->friendUser['nick'];
2906 $_REQUEST['title'] = 'message_title';
2907 $result = api_direct_messages_new('json');
2908 self::assertStringContainsString('message_text', $result['direct_message']['text']);
2909 self::assertStringContainsString('message_title', $result['direct_message']['text']);
2910 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2911 self::assertEquals(1, $result['direct_message']['friendica_seen']);
2915 * Test the api_direct_messages_new() function with an RSS result.
2919 public function testApiDirectMessagesNewWithRss()
2921 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
2922 $_POST['text'] = 'message_text';
2923 $_POST['screen_name'] = $this->friendUser['nick'];
2924 $result = api_direct_messages_new('rss');
2925 self::assertXml($result, 'direct-messages');
2929 * Test the api_direct_messages_destroy() function.
2933 public function testApiDirectMessagesDestroy()
2935 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2936 api_direct_messages_destroy('json');
2940 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2944 public function testApiDirectMessagesDestroyWithVerbose()
2946 $_GET['friendica_verbose'] = 'true';
2947 $result = api_direct_messages_destroy('json');
2951 'result' => 'error',
2952 'message' => 'message id or parenturi not specified'
2960 * Test the api_direct_messages_destroy() function without an authenticated user.
2964 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2966 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
2967 $_SESSION['authenticated'] = false;
2968 api_direct_messages_destroy('json');
2972 * Test the api_direct_messages_destroy() function with a non-zero ID.
2976 public function testApiDirectMessagesDestroyWithId()
2978 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2979 $_REQUEST['id'] = 1;
2980 api_direct_messages_destroy('json');
2984 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2988 public function testApiDirectMessagesDestroyWithIdAndVerbose()
2990 $_REQUEST['id'] = 1;
2991 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2992 $_GET['friendica_verbose'] = 'true';
2993 $result = api_direct_messages_destroy('json');
2997 'result' => 'error',
2998 'message' => 'message id not in database'
3006 * Test the api_direct_messages_destroy() function with a non-zero ID.
3010 public function testApiDirectMessagesDestroyWithCorrectId()
3012 $this->markTestIncomplete('We need to add a dataset for this.');
3016 * Test the api_direct_messages_box() function.
3020 public function testApiDirectMessagesBoxWithSentbox()
3022 $_REQUEST['page'] = -1;
3023 $_REQUEST['max_id'] = 10;
3024 $result = api_direct_messages_box('json', 'sentbox', 'false');
3025 self::assertArrayHasKey('direct_message', $result);
3029 * Test the api_direct_messages_box() function.
3033 public function testApiDirectMessagesBoxWithConversation()
3035 $result = api_direct_messages_box('json', 'conversation', 'false');
3036 self::assertArrayHasKey('direct_message', $result);
3040 * Test the api_direct_messages_box() function.
3044 public function testApiDirectMessagesBoxWithAll()
3046 $result = api_direct_messages_box('json', 'all', 'false');
3047 self::assertArrayHasKey('direct_message', $result);
3051 * Test the api_direct_messages_box() function.
3055 public function testApiDirectMessagesBoxWithInbox()
3057 $result = api_direct_messages_box('json', 'inbox', 'false');
3058 self::assertArrayHasKey('direct_message', $result);
3062 * Test the api_direct_messages_box() function.
3066 public function testApiDirectMessagesBoxWithVerbose()
3068 $result = api_direct_messages_box('json', 'sentbox', 'true');
3072 'result' => 'error',
3073 'message' => 'no mails available'
3081 * Test the api_direct_messages_box() function with a RSS result.
3085 public function testApiDirectMessagesBoxWithRss()
3087 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3088 self::assertXml($result, 'direct-messages');
3092 * Test the api_direct_messages_box() function without an authenticated user.
3096 public function testApiDirectMessagesBoxWithUnallowedUser()
3098 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3099 $_SESSION['allow_api'] = false;
3100 $_GET['screen_name'] = $this->selfUser['nick'];
3101 api_direct_messages_box('json', 'sentbox', 'false');
3105 * Test the api_direct_messages_sentbox() function.
3109 public function testApiDirectMessagesSentbox()
3111 $result = api_direct_messages_sentbox('json');
3112 self::assertArrayHasKey('direct_message', $result);
3116 * Test the api_direct_messages_inbox() function.
3120 public function testApiDirectMessagesInbox()
3122 $result = api_direct_messages_inbox('json');
3123 self::assertArrayHasKey('direct_message', $result);
3127 * Test the api_direct_messages_all() function.
3131 public function testApiDirectMessagesAll()
3133 $result = api_direct_messages_all('json');
3134 self::assertArrayHasKey('direct_message', $result);
3138 * Test the api_direct_messages_conversation() function.
3142 public function testApiDirectMessagesConversation()
3144 $result = api_direct_messages_conversation('json');
3145 self::assertArrayHasKey('direct_message', $result);
3149 * Test the api_oauth_request_token() function.
3153 public function testApiOauthRequestToken()
3155 $this->markTestIncomplete('exit() kills phpunit as well');
3159 * Test the api_oauth_access_token() function.
3163 public function testApiOauthAccessToken()
3165 $this->markTestIncomplete('exit() kills phpunit as well');
3169 * Test the api_fr_photoalbum_delete() function.
3173 public function testApiFrPhotoalbumDelete()
3175 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3176 api_fr_photoalbum_delete('json');
3180 * Test the api_fr_photoalbum_delete() function with an album name.
3184 public function testApiFrPhotoalbumDeleteWithAlbum()
3186 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3187 $_REQUEST['album'] = 'album_name';
3188 api_fr_photoalbum_delete('json');
3192 * Test the api_fr_photoalbum_delete() function with an album name.
3196 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3198 $this->markTestIncomplete('We need to add a dataset for this.');
3202 * Test the api_fr_photoalbum_delete() function.
3206 public function testApiFrPhotoalbumUpdate()
3208 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3209 api_fr_photoalbum_update('json');
3213 * Test the api_fr_photoalbum_delete() function with an album name.
3217 public function testApiFrPhotoalbumUpdateWithAlbum()
3219 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3220 $_REQUEST['album'] = 'album_name';
3221 api_fr_photoalbum_update('json');
3225 * Test the api_fr_photoalbum_delete() function with an album name.
3229 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3231 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3232 $_REQUEST['album'] = 'album_name';
3233 $_REQUEST['album_new'] = 'album_name';
3234 api_fr_photoalbum_update('json');
3238 * Test the api_fr_photoalbum_update() function without an authenticated user.
3242 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3244 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3245 $_SESSION['authenticated'] = false;
3246 api_fr_photoalbum_update('json');
3250 * Test the api_fr_photoalbum_delete() function with an album name.
3254 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3256 $this->markTestIncomplete('We need to add a dataset for this.');
3260 * Test the api_fr_photos_list() function.
3264 public function testApiFrPhotosList()
3266 $result = api_fr_photos_list('json');
3267 self::assertArrayHasKey('photo', $result);
3271 * Test the api_fr_photos_list() function without an authenticated user.
3275 public function testApiFrPhotosListWithoutAuthenticatedUser()
3277 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3278 $_SESSION['authenticated'] = false;
3279 api_fr_photos_list('json');
3283 * Test the api_fr_photo_create_update() function.
3285 public function testApiFrPhotoCreateUpdate()
3287 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3288 api_fr_photo_create_update('json');
3292 * Test the api_fr_photo_create_update() function without an authenticated user.
3296 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3298 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3299 $_SESSION['authenticated'] = false;
3300 api_fr_photo_create_update('json');
3304 * Test the api_fr_photo_create_update() function with an album name.
3308 public function testApiFrPhotoCreateUpdateWithAlbum()
3310 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3311 $_REQUEST['album'] = 'album_name';
3312 api_fr_photo_create_update('json');
3316 * Test the api_fr_photo_create_update() function with the update mode.
3320 public function testApiFrPhotoCreateUpdateWithUpdate()
3322 $this->markTestIncomplete('We need to create a dataset for this');
3326 * Test the api_fr_photo_create_update() function with an uploaded file.
3330 public function testApiFrPhotoCreateUpdateWithFile()
3332 $this->markTestIncomplete();
3336 * Test the api_fr_photo_delete() function.
3340 public function testApiFrPhotoDelete()
3342 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3343 api_fr_photo_delete('json');
3347 * Test the api_fr_photo_delete() function without an authenticated user.
3351 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3353 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3354 $_SESSION['authenticated'] = false;
3355 api_fr_photo_delete('json');
3359 * Test the api_fr_photo_delete() function with a photo ID.
3363 public function testApiFrPhotoDeleteWithPhotoId()
3365 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3366 $_REQUEST['photo_id'] = 1;
3367 api_fr_photo_delete('json');
3371 * Test the api_fr_photo_delete() function with a correct photo ID.
3375 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3377 $this->markTestIncomplete('We need to create a dataset for this.');
3381 * Test the api_fr_photo_detail() function.
3385 public function testApiFrPhotoDetail()
3387 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3388 api_fr_photo_detail('json');
3392 * Test the api_fr_photo_detail() function without an authenticated user.
3396 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3398 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3399 $_SESSION['authenticated'] = false;
3400 api_fr_photo_detail('json');
3404 * Test the api_fr_photo_detail() function with a photo ID.
3408 public function testApiFrPhotoDetailWithPhotoId()
3410 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
3411 $_REQUEST['photo_id'] = 1;
3412 api_fr_photo_detail('json');
3416 * Test the api_fr_photo_detail() function with a correct photo ID.
3420 public function testApiFrPhotoDetailCorrectPhotoId()
3422 $this->markTestIncomplete('We need to create a dataset for this.');
3426 * Test the api_account_update_profile_image() function.
3430 public function testApiAccountUpdateProfileImage()
3432 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3433 api_account_update_profile_image('json');
3437 * Test the api_account_update_profile_image() function without an authenticated user.
3441 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3443 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3444 $_SESSION['authenticated'] = false;
3445 api_account_update_profile_image('json');
3449 * Test the api_account_update_profile_image() function with an uploaded file.
3453 public function testApiAccountUpdateProfileImageWithUpload()
3455 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3456 $this->markTestIncomplete();
3461 * Test the api_account_update_profile() function.
3465 public function testApiAccountUpdateProfile()
3467 $_POST['name'] = 'new_name';
3468 $_POST['description'] = 'new_description';
3469 $result = api_account_update_profile('json');
3470 // We can't use assertSelfUser() here because the user object is missing some properties.
3471 self::assertEquals($this->selfUser['id'], $result['user']['cid']);
3472 self::assertEquals('DFRN', $result['user']['location']);
3473 self::assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3474 self::assertEquals('dfrn', $result['user']['network']);
3475 self::assertEquals('new_name', $result['user']['name']);
3476 self::assertEquals('new_description', $result['user']['description']);
3480 * Test the check_acl_input() function.
3484 public function testCheckAclInput()
3486 $result = check_acl_input('<aclstring>');
3487 // Where does this result come from?
3488 self::assertEquals(1, $result);
3492 * Test the check_acl_input() function with an empty ACL string.
3496 public function testCheckAclInputWithEmptyAclString()
3498 $result = check_acl_input(' ');
3499 self::assertFalse($result);
3503 * Test the save_media_to_database() function.
3507 public function testSaveMediaToDatabase()
3509 $this->markTestIncomplete();
3513 * Test the post_photo_item() function.
3517 public function testPostPhotoItem()
3519 $this->markTestIncomplete();
3523 * Test the prepare_photo_data() function.
3527 public function testPreparePhotoData()
3529 $this->markTestIncomplete();
3533 * Test the api_share_as_retweet() function with a valid item.
3537 public function testApiShareAsRetweetWithValidItem()
3539 $this->markTestIncomplete();
3543 * Test the api_in_reply_to() function.
3547 public function testApiInReplyTo()
3549 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3550 self::assertArrayHasKey('status_id', $result);
3551 self::assertArrayHasKey('user_id', $result);
3552 self::assertArrayHasKey('status_id_str', $result);
3553 self::assertArrayHasKey('user_id_str', $result);
3554 self::assertArrayHasKey('screen_name', $result);
3558 * Test the api_in_reply_to() function with a valid item.
3562 public function testApiInReplyToWithValidItem()
3564 $this->markTestIncomplete();
3568 * Test the api_clean_plain_items() function.
3572 public function testApiCleanPlainItems()
3574 $_REQUEST['include_entities'] = 'true';
3575 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3576 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3580 * Test the api_best_nickname() function.
3584 public function testApiBestNickname()
3587 $result = api_best_nickname($contacts);
3588 self::assertNull($result);
3592 * Test the api_best_nickname() function with contacts.
3596 public function testApiBestNicknameWithContacts()
3598 $this->markTestIncomplete();
3602 * Test the api_friendica_group_show() function.
3606 public function testApiFriendicaGroupShow()
3608 $this->markTestIncomplete();
3612 * Test the api_friendica_group_delete() function.
3616 public function testApiFriendicaGroupDelete()
3618 $this->markTestIncomplete();
3622 * Test the api_lists_destroy() function.
3626 public function testApiListsDestroy()
3628 $this->markTestIncomplete();
3632 * Test the group_create() function.
3636 public function testGroupCreate()
3638 $this->markTestIncomplete();
3642 * Test the api_friendica_group_create() function.
3646 public function testApiFriendicaGroupCreate()
3648 $this->markTestIncomplete();
3652 * Test the api_lists_create() function.
3656 public function testApiListsCreate()
3658 $this->markTestIncomplete();
3662 * Test the api_friendica_group_update() function.
3666 public function testApiFriendicaGroupUpdate()
3668 $this->markTestIncomplete();
3672 * Test the api_lists_update() function.
3676 public function testApiListsUpdate()
3678 $this->markTestIncomplete();
3682 * Test the api_friendica_activity() function.
3686 public function testApiFriendicaActivity()
3688 $this->markTestIncomplete();
3692 * Test the api_friendica_notification() function.
3696 public function testApiFriendicaNotification()
3698 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
3699 api_friendica_notification('json');
3703 * Test the api_friendica_notification() function without an authenticated user.
3707 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3709 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
3710 $_SESSION['authenticated'] = false;
3711 api_friendica_notification('json');
3715 * Test the api_friendica_notification() function with empty result
3719 public function testApiFriendicaNotificationWithEmptyResult()
3721 DI::args()->setArgv(['api', 'friendica', 'notification']);
3722 $_SESSION['uid'] = 41;
3723 $result = api_friendica_notification('json');
3724 self::assertEquals(['note' => false], $result);
3728 * Test the api_friendica_notification() function with an XML result.
3732 public function testApiFriendicaNotificationWithXmlResult()
3734 DI::args()->setArgv(['api', 'friendica', 'notification']);
3735 $result = api_friendica_notification('xml');
3736 $date = DateTimeFormat::local('2020-01-01 12:12:02');
3737 $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02');
3740 <?xml version="1.0"?>
3742 <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"/>
3745 self::assertXmlStringEqualsXmlString($assertXml, $result);
3749 * Test the api_friendica_notification() function with an JSON result.
3753 public function testApiFriendicaNotificationWithJsonResult()
3755 DI::args()->setArgv(['api', 'friendica', 'notification']);
3756 $result = json_encode(api_friendica_notification('json'));
3757 self::assertJson($result);
3761 * Test the api_friendica_notification_seen() function.
3765 public function testApiFriendicaNotificationSeen()
3767 $this->markTestIncomplete();
3771 * Test the api_friendica_direct_messages_setseen() function.
3775 public function testApiFriendicaDirectMessagesSetseen()
3777 $this->markTestIncomplete();
3781 * Test the api_friendica_direct_messages_search() function.
3785 public function testApiFriendicaDirectMessagesSearch()
3787 $this->markTestIncomplete();
3791 * Test the api_saved_searches_list() function.
3795 public function testApiSavedSearchesList()
3797 $result = api_saved_searches_list('json');
3798 self::assertEquals(1, $result['terms'][0]['id']);
3799 self::assertEquals(1, $result['terms'][0]['id_str']);
3800 self::assertEquals('Saved search', $result['terms'][0]['name']);
3801 self::assertEquals('Saved search', $result['terms'][0]['query']);