6 namespace Friendica\Test;
10 use Friendica\Core\Config\IConfiguration;
11 use Friendica\Core\Config\IPConfiguration;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Session;
14 use Friendica\Core\Session\ISession;
15 use Friendica\Core\System;
16 use Friendica\Database\Database;
18 use Friendica\Model\Contact;
19 use Friendica\Network\HTTPException;
20 use Friendica\Test\Util\Database\StaticDatabase;
21 use Monolog\Handler\TestHandler;
23 require_once __DIR__ . '/../../include/api.php';
26 * Tests for the API functions.
28 * Functions that use header() need to be tested in a separate process.
29 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
31 class ApiTest extends DatabaseTest
34 * @var TestHandler Can handle log-outputs
41 protected $friendUser;
45 protected $wrongUserId;
50 /** @var IConfiguration */
57 * Create variables used by tests.
59 protected function setUp()
63 $this->dice = (new Dice())
64 ->addRules(include __DIR__ . '/../../static/dependencies.config.php')
65 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
66 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
67 DI::init($this->dice);
69 /** @var Database $dba */
70 $dba = $this->dice->create(Database::class);
72 /** @var IConfiguration $config */
73 $this->config = $this->dice->create(IConfiguration::class);
75 $this->config->set('system', 'url', 'http://localhost');
76 $this->config->set('system', 'hostname', 'localhost');
77 $this->config->set('system', 'worker_dont_fork', true);
80 $this->config->set('config', 'hostname', 'localhost');
81 $this->config->set('system', 'throttle_limit_day', 100);
82 $this->config->set('system', 'throttle_limit_week', 100);
83 $this->config->set('system', 'throttle_limit_month', 100);
84 $this->config->set('system', 'theme', 'system_theme');
86 // Load the API dataset for the whole API
87 $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba);
90 $this->app = DI::app();
93 $this->app->argv = ['home'];
95 // User data that the test database is populated with
98 'name' => 'Self contact',
99 'nick' => 'selfcontact',
100 'nurl' => 'http://localhost/profile/selfcontact'
102 $this->friendUser = [
104 'name' => 'Friend contact',
105 'nick' => 'friendcontact',
106 'nurl' => 'http://localhost/profile/friendcontact'
110 'name' => 'othercontact',
111 'nick' => 'othercontact',
112 'nurl' => 'http://localhost/profile/othercontact'
115 // User ID that we know is not in the database
116 $this->wrongUserId = 666;
118 DI::session()->start();
120 // Most API require login so we force the session
123 'authenticated' => true,
124 'uid' => $this->selfUser['id']
133 * Assert that an user array contains expected keys.
135 * @param array $user User array
139 private function assertSelfUser(array $user)
141 $this->assertEquals($this->selfUser['id'], $user['uid']);
142 $this->assertEquals($this->selfUser['id'], $user['cid']);
143 $this->assertEquals(1, $user['self']);
144 $this->assertEquals('DFRN', $user['location']);
145 $this->assertEquals($this->selfUser['name'], $user['name']);
146 $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
147 $this->assertEquals('dfrn', $user['network']);
148 $this->assertTrue($user['verified']);
152 * Assert that an user array contains expected keys.
154 * @param array $user User array
158 private function assertOtherUser(array $user)
160 $this->assertEquals($this->otherUser['id'], $user['id']);
161 $this->assertEquals($this->otherUser['id'], $user['id_str']);
162 $this->assertEquals(0, $user['self']);
163 $this->assertEquals($this->otherUser['name'], $user['name']);
164 $this->assertEquals($this->otherUser['nick'], $user['screen_name']);
165 $this->assertFalse($user['verified']);
169 * Assert that a status array contains expected keys.
171 * @param array $status Status array
175 private function assertStatus(array $status)
177 $this->assertInternalType('string', $status['text']);
178 $this->assertInternalType('int', $status['id']);
179 // We could probably do more checks here.
183 * Assert that a list array contains expected keys.
185 * @param array $list List array
189 private function assertList(array $list)
191 $this->assertInternalType('string', $list['name']);
192 $this->assertInternalType('int', $list['id']);
193 $this->assertInternalType('string', $list['id_str']);
194 $this->assertContains($list['mode'], ['public', 'private']);
195 // We could probably do more checks here.
199 * Assert that the string is XML and contain the root element.
201 * @param string $result XML string
202 * @param string $root_element Root element name
206 private function assertXml($result, $root_element)
208 $this->assertStringStartsWith('<?xml version="1.0"?>', $result);
209 $this->assertContains('<' . $root_element, $result);
210 // We could probably do more checks here.
214 * Get the path to a temporary empty PNG image.
216 * @return string Path
218 private function getTempImage()
220 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
224 // Empty 1x1 px PNG image
225 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
233 * Test the api_user() function.
237 public function testApiUser()
239 $this->assertEquals($this->selfUser['id'], api_user());
243 * Test the api_user() function with an unallowed user.
247 public function testApiUserWithUnallowedUser()
249 $_SESSION = ['allow_api' => false];
250 $this->assertEquals(false, api_user());
254 * Test the api_source() function.
258 public function testApiSource()
260 $this->assertEquals('api', api_source());
264 * Test the api_source() function with a Twidere user agent.
268 public function testApiSourceWithTwidere()
270 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
271 $this->assertEquals('Twidere', api_source());
275 * Test the api_source() function with a GET parameter.
279 public function testApiSourceWithGet()
281 $_GET['source'] = 'source_name';
282 $this->assertEquals('source_name', api_source());
286 * Test the api_date() function.
290 public function testApiDate()
292 $this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
296 * Test the api_register_func() function.
300 public function testApiRegisterFunc()
312 $this->assertTrue($API['api_path']['auth']);
313 $this->assertEquals('method', $API['api_path']['method']);
314 $this->assertTrue(is_callable($API['api_path']['func']));
318 * Test the api_login() function without any login.
321 * @runInSeparateProcess
322 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
324 public function testApiLoginWithoutLogin()
326 api_login($this->app);
330 * Test the api_login() function with a bad login.
333 * @runInSeparateProcess
334 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
336 public function testApiLoginWithBadLogin()
338 $_SERVER['PHP_AUTH_USER'] = 'user@server';
339 api_login($this->app);
343 * Test the api_login() function with oAuth.
347 public function testApiLoginWithOauth()
349 $this->markTestIncomplete('Can we test this easily?');
353 * Test the api_login() function with authentication provided by an addon.
357 public function testApiLoginWithAddonAuth()
359 $this->markTestIncomplete('Can we test this easily?');
363 * Test the api_login() function with a correct login.
366 * @runInSeparateProcess
368 public function testApiLoginWithCorrectLogin()
370 $_SERVER['PHP_AUTH_USER'] = 'Test user';
371 $_SERVER['PHP_AUTH_PW'] = 'password';
372 api_login($this->app);
376 * Test the api_login() function with a remote user.
379 * @runInSeparateProcess
380 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
382 public function testApiLoginWithRemoteUser()
384 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
385 api_login($this->app);
389 * Test the api_check_method() function.
393 public function testApiCheckMethod()
395 $this->assertFalse(api_check_method('method'));
399 * Test the api_check_method() function with a correct method.
403 public function testApiCheckMethodWithCorrectMethod()
405 $_SERVER['REQUEST_METHOD'] = 'method';
406 $this->assertTrue(api_check_method('method'));
410 * Test the api_check_method() function with a wildcard.
414 public function testApiCheckMethodWithWildcard()
416 $this->assertTrue(api_check_method('*'));
420 * Test the api_call() function.
423 * @runInSeparateProcess
425 public function testApiCall()
429 'method' => 'method',
430 'func' => function () {
431 return ['data' => ['some_data']];
434 $_SERVER['REQUEST_METHOD'] = 'method';
435 $_SERVER['QUERY_STRING'] = 'q=api_path';
436 $_GET['callback'] = 'callback_name';
438 $args = DI::args()->determine($_SERVER, $_GET);
441 'callback_name(["some_data"])',
442 api_call($this->app, $args)
447 * Test the api_call() function with the profiled enabled.
450 * @runInSeparateProcess
452 public function testApiCallWithProfiler()
456 'method' => 'method',
457 'func' => function () {
458 return ['data' => ['some_data']];
462 $_SERVER['REQUEST_METHOD'] = 'method';
463 $_SERVER['QUERY_STRING'] = 'q=api_path';
465 $args = DI::args()->determine($_SERVER, $_GET);
467 $this->config->set('system', 'profiler', true);
468 $this->config->set('rendertime', 'callstack', true);
469 $this->app->callstack = [
470 'database' => ['some_function' => 200],
471 'database_write' => ['some_function' => 200],
472 'cache' => ['some_function' => 200],
473 'cache_write' => ['some_function' => 200],
474 'network' => ['some_function' => 200]
479 api_call($this->app, $args)
484 * Test the api_call() function without any result.
487 * @runInSeparateProcess
489 public function testApiCallWithNoResult()
493 'method' => 'method',
494 'func' => function () {
498 $_SERVER['REQUEST_METHOD'] = 'method';
499 $_SERVER['QUERY_STRING'] = 'q=api_path';
501 $args = DI::args()->determine($_SERVER, $_GET);
504 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
505 api_call($this->app, $args)
510 * Test the api_call() function with an unimplemented API.
513 * @runInSeparateProcess
515 public function testApiCallWithUninplementedApi()
518 '{"status":{"error":"Not Implemented","code":"501 Not Implemented","request":""}}',
524 * Test the api_call() function with a JSON result.
527 * @runInSeparateProcess
529 public function testApiCallWithJson()
533 'method' => 'method',
534 'func' => function () {
535 return ['data' => ['some_data']];
538 $_SERVER['REQUEST_METHOD'] = 'method';
539 $_SERVER['QUERY_STRING'] = 'q=api_path.json';
541 $args = DI::args()->determine($_SERVER, $_GET);
545 api_call($this->app, $args)
550 * Test the api_call() function with an XML result.
553 * @runInSeparateProcess
555 public function testApiCallWithXml()
559 'method' => 'method',
560 'func' => function () {
564 $_SERVER['REQUEST_METHOD'] = 'method';
565 $_SERVER['QUERY_STRING'] = 'q=api_path.xml';
567 $args = DI::args()->determine($_SERVER, $_GET);
571 api_call($this->app, $args)
576 * Test the api_call() function with an RSS result.
579 * @runInSeparateProcess
581 public function testApiCallWithRss()
585 'method' => 'method',
586 'func' => function () {
590 $_SERVER['REQUEST_METHOD'] = 'method';
591 $_SERVER['QUERY_STRING'] = 'q=api_path.rss';
593 $args = DI::args()->determine($_SERVER, $_GET);
596 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
598 api_call($this->app, $args)
603 * Test the api_call() function with an Atom result.
606 * @runInSeparateProcess
608 public function testApiCallWithAtom()
612 'method' => 'method',
613 'func' => function () {
617 $_SERVER['REQUEST_METHOD'] = 'method';
618 $_SERVER['QUERY_STRING'] = 'q=api_path.atom';
620 $args = DI::args()->determine($_SERVER, $_GET);
623 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
625 api_call($this->app, $args)
630 * Test the api_call() function with an unallowed method.
633 * @runInSeparateProcess
635 public function testApiCallWithWrongMethod()
638 $API['api_path'] = ['method' => 'method'];
640 $_SERVER['QUERY_STRING'] = 'q=api_path';
642 $args = DI::args()->determine($_SERVER, $_GET);
645 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
646 api_call($this->app, $args)
651 * Test the api_call() function with an unauthorized user.
654 * @runInSeparateProcess
656 public function testApiCallWithWrongAuth()
660 'method' => 'method',
663 $_SESSION['authenticated'] = false;
664 $_SERVER['REQUEST_METHOD'] = 'method';
665 $_SERVER['QUERY_STRING'] = 'q=api_path';
667 $args = DI::args()->determine($_SERVER, $_GET);
670 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
671 api_call($this->app, $args)
676 * Test the api_error() function with a JSON result.
679 * @runInSeparateProcess
681 public function testApiErrorWithJson()
684 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
685 api_error('json', new HTTPException\OKException('error_message'), DI::args())
690 * Test the api_error() function with an XML result.
693 * @runInSeparateProcess
695 public function testApiErrorWithXml()
698 '<?xml version="1.0"?>' . "\n" .
699 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
700 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
701 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
702 ' <error>error_message</error>' . "\n" .
703 ' <code>200 OK</code>' . "\n" .
704 ' <request/>' . "\n" .
706 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
711 * Test the api_error() function with an RSS result.
714 * @runInSeparateProcess
716 public function testApiErrorWithRss()
719 '<?xml version="1.0"?>' . "\n" .
720 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
721 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
722 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
723 ' <error>error_message</error>' . "\n" .
724 ' <code>200 OK</code>' . "\n" .
725 ' <request/>' . "\n" .
727 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
732 * Test the api_error() function with an Atom result.
735 * @runInSeparateProcess
737 public function testApiErrorWithAtom()
740 '<?xml version="1.0"?>' . "\n" .
741 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
742 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
743 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
744 ' <error>error_message</error>' . "\n" .
745 ' <code>200 OK</code>' . "\n" .
746 ' <request/>' . "\n" .
748 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
753 * Test the api_rss_extra() function.
757 public function testApiRssExtra()
759 $user_info = ['url' => 'user_url', 'lang' => 'en'];
760 $result = api_rss_extra($this->app, [], $user_info);
761 $this->assertEquals($user_info, $result['$user']);
762 $this->assertEquals($user_info['url'], $result['$rss']['alternate']);
763 $this->assertArrayHasKey('self', $result['$rss']);
764 $this->assertArrayHasKey('base', $result['$rss']);
765 $this->assertArrayHasKey('updated', $result['$rss']);
766 $this->assertArrayHasKey('atom_updated', $result['$rss']);
767 $this->assertArrayHasKey('language', $result['$rss']);
768 $this->assertArrayHasKey('logo', $result['$rss']);
772 * Test the api_rss_extra() function without any user info.
776 public function testApiRssExtraWithoutUserInfo()
778 $result = api_rss_extra($this->app, [], null);
779 $this->assertInternalType('array', $result['$user']);
780 $this->assertArrayHasKey('alternate', $result['$rss']);
781 $this->assertArrayHasKey('self', $result['$rss']);
782 $this->assertArrayHasKey('base', $result['$rss']);
783 $this->assertArrayHasKey('updated', $result['$rss']);
784 $this->assertArrayHasKey('atom_updated', $result['$rss']);
785 $this->assertArrayHasKey('language', $result['$rss']);
786 $this->assertArrayHasKey('logo', $result['$rss']);
790 * Test the api_unique_id_to_nurl() function.
794 public function testApiUniqueIdToNurl()
796 $this->assertFalse(api_unique_id_to_nurl($this->wrongUserId));
800 * Test the api_unique_id_to_nurl() function with a correct ID.
804 public function testApiUniqueIdToNurlWithCorrectId()
806 $this->assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
810 * Test the api_get_user() function.
814 public function testApiGetUser()
816 $user = api_get_user($this->app);
817 $this->assertSelfUser($user);
818 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
819 $this->assertEquals('6fdbe8', $user['profile_link_color']);
820 $this->assertEquals('ededed', $user['profile_background_color']);
824 * Test the api_get_user() function with a Frio schema.
828 public function testApiGetUserWithFrioSchema()
830 $pConfig = $this->dice->create(IPConfiguration::class);
831 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
832 $user = api_get_user($this->app);
833 $this->assertSelfUser($user);
834 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
835 $this->assertEquals('6fdbe8', $user['profile_link_color']);
836 $this->assertEquals('ededed', $user['profile_background_color']);
840 * Test the api_get_user() function with a custom Frio schema.
844 public function testApiGetUserWithCustomFrioSchema()
846 $pConfig = $this->dice->create(IPConfiguration::class);
847 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
848 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
849 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
850 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
851 $user = api_get_user($this->app);
852 $this->assertSelfUser($user);
853 $this->assertEquals('123456', $user['profile_sidebar_fill_color']);
854 $this->assertEquals('123456', $user['profile_link_color']);
855 $this->assertEquals('123456', $user['profile_background_color']);
859 * Test the api_get_user() function with an empty Frio schema.
863 public function testApiGetUserWithEmptyFrioSchema()
865 $pConfig = $this->dice->create(IPConfiguration::class);
866 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
867 $user = api_get_user($this->app);
868 $this->assertSelfUser($user);
869 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
870 $this->assertEquals('6fdbe8', $user['profile_link_color']);
871 $this->assertEquals('ededed', $user['profile_background_color']);
875 * Test the api_get_user() function with an user that is not allowed to use the API.
878 * @runInSeparateProcess
880 public function testApiGetUserWithoutApiUser()
882 $_SERVER['PHP_AUTH_USER'] = 'Test user';
883 $_SERVER['PHP_AUTH_PW'] = 'password';
884 $_SESSION['allow_api'] = false;
885 $this->assertFalse(api_get_user($this->app));
889 * Test the api_get_user() function with an user ID in a GET parameter.
893 public function testApiGetUserWithGetId()
895 $_GET['user_id'] = $this->otherUser['id'];
896 $this->assertOtherUser(api_get_user($this->app));
900 * Test the api_get_user() function with a wrong user ID in a GET parameter.
903 * @expectedException Friendica\Network\HTTPException\BadRequestException
905 public function testApiGetUserWithWrongGetId()
907 $_GET['user_id'] = $this->wrongUserId;
908 $this->assertOtherUser(api_get_user($this->app));
912 * Test the api_get_user() function with an user name in a GET parameter.
916 public function testApiGetUserWithGetName()
918 $_GET['screen_name'] = $this->selfUser['nick'];
919 $this->assertSelfUser(api_get_user($this->app));
923 * Test the api_get_user() function with a profile URL in a GET parameter.
927 public function testApiGetUserWithGetUrl()
929 $_GET['profileurl'] = $this->selfUser['nurl'];
930 $this->assertSelfUser(api_get_user($this->app));
934 * Test the api_get_user() function with an user ID in the API path.
938 public function testApiGetUserWithNumericCalledApi()
941 $called_api = ['api_path'];
942 $this->app->argv[1] = $this->otherUser['id'] . '.json';
943 $this->assertOtherUser(api_get_user($this->app));
947 * Test the api_get_user() function with the $called_api global variable.
951 public function testApiGetUserWithCalledApi()
954 $called_api = ['api', 'api_path'];
955 $this->assertSelfUser(api_get_user($this->app));
959 * Test the api_get_user() function with a valid user.
963 public function testApiGetUserWithCorrectUser()
965 $this->assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
969 * Test the api_get_user() function with a wrong user ID.
972 * @expectedException Friendica\Network\HTTPException\BadRequestException
974 public function testApiGetUserWithWrongUser()
976 $this->assertOtherUser(api_get_user($this->app, $this->wrongUserId));
980 * Test the api_get_user() function with a 0 user ID.
984 public function testApiGetUserWithZeroUser()
986 $this->assertSelfUser(api_get_user($this->app, 0));
990 * Test the api_item_get_user() function.
994 public function testApiItemGetUser()
996 $users = api_item_get_user($this->app, []);
997 $this->assertSelfUser($users[0]);
1001 * Test the api_item_get_user() function with a different item parent.
1005 public function testApiItemGetUserWithDifferentParent()
1007 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1008 $this->assertSelfUser($users[0]);
1009 $this->assertEquals($users[0], $users[1]);
1013 * Test the api_walk_recursive() function.
1017 public function testApiWalkRecursive()
1020 $this->assertEquals(
1025 // Should we test this with a callback that actually does something?
1033 * Test the api_walk_recursive() function with an array.
1037 public function testApiWalkRecursiveWithArray()
1039 $array = [['item1'], ['item2']];
1040 $this->assertEquals(
1045 // Should we test this with a callback that actually does something?
1053 * Test the api_reformat_xml() function.
1057 public function testApiReformatXml()
1061 $this->assertTrue(api_reformat_xml($item, $key));
1062 $this->assertEquals('true', $item);
1066 * Test the api_reformat_xml() function with a statusnet_api key.
1070 public function testApiReformatXmlWithStatusnetKey()
1073 $key = 'statusnet_api';
1074 $this->assertTrue(api_reformat_xml($item, $key));
1075 $this->assertEquals('statusnet:api', $key);
1079 * Test the api_reformat_xml() function with a friendica_api key.
1083 public function testApiReformatXmlWithFriendicaKey()
1086 $key = 'friendica_api';
1087 $this->assertTrue(api_reformat_xml($item, $key));
1088 $this->assertEquals('friendica:api', $key);
1092 * Test the api_create_xml() function.
1096 public function testApiCreateXml()
1098 $this->assertEquals(
1099 '<?xml version="1.0"?>' . "\n" .
1100 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1101 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1102 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1103 ' <data>some_data</data>' . "\n" .
1104 '</root_element>' . "\n",
1105 api_create_xml(['data' => ['some_data']], 'root_element')
1110 * Test the api_create_xml() function without any XML namespace.
1114 public function testApiCreateXmlWithoutNamespaces()
1116 $this->assertEquals(
1117 '<?xml version="1.0"?>' . "\n" .
1119 ' <data>some_data</data>' . "\n" .
1121 api_create_xml(['data' => ['some_data']], 'ok')
1126 * Test the api_format_data() function.
1130 public function testApiFormatData()
1132 $data = ['some_data'];
1133 $this->assertEquals($data, api_format_data('root_element', 'json', $data));
1137 * Test the api_format_data() function with an XML result.
1141 public function testApiFormatDataWithXml()
1143 $this->assertEquals(
1144 '<?xml version="1.0"?>' . "\n" .
1145 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1146 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1147 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1148 ' <data>some_data</data>' . "\n" .
1149 '</root_element>' . "\n",
1150 api_format_data('root_element', 'xml', ['data' => ['some_data']])
1155 * Test the api_account_verify_credentials() function.
1159 public function testApiAccountVerifyCredentials()
1161 $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
1165 * Test the api_account_verify_credentials() function without an authenticated user.
1168 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1170 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1172 $_SESSION['authenticated'] = false;
1173 api_account_verify_credentials('json');
1177 * Test the requestdata() function.
1181 public function testRequestdata()
1183 $this->assertNull(requestdata('variable_name'));
1187 * Test the requestdata() function with a POST parameter.
1191 public function testRequestdataWithPost()
1193 $_POST['variable_name'] = 'variable_value';
1194 $this->assertEquals('variable_value', requestdata('variable_name'));
1198 * Test the requestdata() function with a GET parameter.
1202 public function testRequestdataWithGet()
1204 $_GET['variable_name'] = 'variable_value';
1205 $this->assertEquals('variable_value', requestdata('variable_name'));
1209 * Test the api_statuses_mediap() function.
1213 public function testApiStatusesMediap()
1215 $this->app->argc = 2;
1223 'tmp_name' => $this->getTempImage(),
1224 'name' => 'spacer.png',
1225 'type' => 'image/png'
1228 $_GET['status'] = '<b>Status content</b>';
1230 $result = api_statuses_mediap('json');
1231 $this->assertStatus($result['status']);
1235 * Test the api_statuses_mediap() function without an authenticated user.
1238 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1240 public function testApiStatusesMediapWithoutAuthenticatedUser()
1242 $_SESSION['authenticated'] = false;
1243 api_statuses_mediap('json');
1247 * Test the api_statuses_update() function.
1251 public function testApiStatusesUpdate()
1253 $_GET['status'] = 'Status content #friendica';
1254 $_GET['in_reply_to_status_id'] = -1;
1263 'tmp_name' => $this->getTempImage(),
1264 'name' => 'spacer.png',
1265 'type' => 'image/png'
1269 $result = api_statuses_update('json');
1270 $this->assertStatus($result['status']);
1274 * Test the api_statuses_update() function with an HTML status.
1278 public function testApiStatusesUpdateWithHtml()
1280 $_GET['htmlstatus'] = '<b>Status content</b>';
1282 $result = api_statuses_update('json');
1283 $this->assertStatus($result['status']);
1287 * Test the api_statuses_update() function without an authenticated user.
1290 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1292 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1294 $_SESSION['authenticated'] = false;
1295 api_statuses_update('json');
1299 * Test the api_statuses_update() function with a parent status.
1303 public function testApiStatusesUpdateWithParent()
1305 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1309 * Test the api_statuses_update() function with a media_ids parameter.
1313 public function testApiStatusesUpdateWithMediaIds()
1315 $this->markTestIncomplete();
1319 * Test the api_statuses_update() function with the throttle limit reached.
1323 public function testApiStatusesUpdateWithDayThrottleReached()
1325 $this->markTestIncomplete();
1329 * Test the api_media_upload() function.
1332 * @expectedException Friendica\Network\HTTPException\BadRequestException
1334 public function testApiMediaUpload()
1340 * Test the api_media_upload() function without an authenticated user.
1343 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1345 public function testApiMediaUploadWithoutAuthenticatedUser()
1347 $_SESSION['authenticated'] = false;
1352 * Test the api_media_upload() function with an invalid uploaded media.
1355 * @expectedException Friendica\Network\HTTPException\InternalServerErrorException
1357 public function testApiMediaUploadWithMedia()
1362 'tmp_name' => 'tmp_name'
1369 * Test the api_media_upload() function with an valid uploaded media.
1373 public function testApiMediaUploadWithValidMedia()
1381 'tmp_name' => $this->getTempImage(),
1382 'name' => 'spacer.png',
1383 'type' => 'image/png'
1389 $result = api_media_upload();
1390 $this->assertEquals('image/png', $result['media']['image']['image_type']);
1391 $this->assertEquals(1, $result['media']['image']['w']);
1392 $this->assertEquals(1, $result['media']['image']['h']);
1393 $this->assertNotEmpty($result['media']['image']['friendica_preview_url']);
1397 * Test the api_status_show() function.
1399 public function testApiStatusShowWithJson()
1401 $result = api_status_show('json', 1);
1402 $this->assertStatus($result['status']);
1406 * Test the api_status_show() function with an XML result.
1408 public function testApiStatusShowWithXml()
1410 $result = api_status_show('xml', 1);
1411 $this->assertXml($result, 'statuses');
1415 * Test the api_get_last_status() function
1417 public function testApiGetLastStatus()
1419 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1421 $this->assertNotNull($item);
1425 * Test the api_users_show() function.
1429 public function testApiUsersShow()
1431 $result = api_users_show('json');
1432 // We can't use assertSelfUser() here because the user object is missing some properties.
1433 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
1434 $this->assertEquals('DFRN', $result['user']['location']);
1435 $this->assertEquals($this->selfUser['name'], $result['user']['name']);
1436 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1437 $this->assertEquals('dfrn', $result['user']['network']);
1438 $this->assertTrue($result['user']['verified']);
1442 * Test the api_users_show() function with an XML result.
1446 public function testApiUsersShowWithXml()
1448 $result = api_users_show('xml');
1449 $this->assertXml($result, 'statuses');
1453 * Test the api_users_search() function.
1457 public function testApiUsersSearch()
1459 $_GET['q'] = 'othercontact';
1460 $result = api_users_search('json');
1461 $this->assertOtherUser($result['users'][0]);
1465 * Test the api_users_search() function with an XML result.
1469 public function testApiUsersSearchWithXml()
1471 $_GET['q'] = 'othercontact';
1472 $result = api_users_search('xml');
1473 $this->assertXml($result, 'users');
1477 * Test the api_users_search() function without a GET q parameter.
1480 * @expectedException Friendica\Network\HTTPException\BadRequestException
1482 public function testApiUsersSearchWithoutQuery()
1484 api_users_search('json');
1488 * Test the api_users_lookup() function.
1491 * @expectedException Friendica\Network\HTTPException\NotFoundException
1493 public function testApiUsersLookup()
1495 api_users_lookup('json');
1499 * Test the api_users_lookup() function with an user ID.
1503 public function testApiUsersLookupWithUserId()
1505 $_REQUEST['user_id'] = $this->otherUser['id'];
1506 $result = api_users_lookup('json');
1507 $this->assertOtherUser($result['users'][0]);
1511 * Test the api_search() function.
1515 public function testApiSearch()
1517 $_REQUEST['q'] = 'reply';
1518 $_REQUEST['max_id'] = 10;
1519 $result = api_search('json');
1520 foreach ($result['status'] as $status) {
1521 $this->assertStatus($status);
1522 $this->assertContains('reply', $status['text'], null, true);
1527 * Test the api_search() function a count parameter.
1531 public function testApiSearchWithCount()
1533 $_REQUEST['q'] = 'reply';
1534 $_REQUEST['count'] = 20;
1535 $result = api_search('json');
1536 foreach ($result['status'] as $status) {
1537 $this->assertStatus($status);
1538 $this->assertContains('reply', $status['text'], null, true);
1543 * Test the api_search() function with an rpp parameter.
1547 public function testApiSearchWithRpp()
1549 $_REQUEST['q'] = 'reply';
1550 $_REQUEST['rpp'] = 20;
1551 $result = api_search('json');
1552 foreach ($result['status'] as $status) {
1553 $this->assertStatus($status);
1554 $this->assertContains('reply', $status['text'], null, true);
1559 * Test the api_search() function with an q parameter contains hashtag.
1563 public function testApiSearchWithHashtag()
1565 $_REQUEST['q'] = '%23friendica';
1566 $result = api_search('json');
1567 foreach ($result['status'] as $status) {
1568 $this->assertStatus($status);
1569 $this->assertContains('#friendica', $status['text'], null, true);
1574 * Test the api_search() function with an exclude_replies parameter.
1578 public function testApiSearchWithExcludeReplies()
1580 $_REQUEST['max_id'] = 10;
1581 $_REQUEST['exclude_replies'] = true;
1582 $_REQUEST['q'] = 'friendica';
1583 $result = api_search('json');
1584 foreach ($result['status'] as $status) {
1585 $this->assertStatus($status);
1590 * Test the api_search() function without an authenticated user.
1593 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1595 public function testApiSearchWithUnallowedUser()
1597 $_SESSION['allow_api'] = false;
1598 $_GET['screen_name'] = $this->selfUser['nick'];
1603 * Test the api_search() function without any GET query parameter.
1606 * @expectedException Friendica\Network\HTTPException\BadRequestException
1608 public function testApiSearchWithoutQuery()
1614 * Test the api_statuses_home_timeline() function.
1618 public function testApiStatusesHomeTimeline()
1620 $_REQUEST['max_id'] = 10;
1621 $_REQUEST['exclude_replies'] = true;
1622 $_REQUEST['conversation_id'] = 1;
1623 $result = api_statuses_home_timeline('json');
1624 $this->assertNotEmpty($result['status']);
1625 foreach ($result['status'] as $status) {
1626 $this->assertStatus($status);
1631 * Test the api_statuses_home_timeline() function with a negative page parameter.
1635 public function testApiStatusesHomeTimelineWithNegativePage()
1637 $_REQUEST['page'] = -2;
1638 $result = api_statuses_home_timeline('json');
1639 $this->assertNotEmpty($result['status']);
1640 foreach ($result['status'] as $status) {
1641 $this->assertStatus($status);
1646 * Test the api_statuses_home_timeline() with an unallowed user.
1649 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1651 public function testApiStatusesHomeTimelineWithUnallowedUser()
1653 $_SESSION['allow_api'] = false;
1654 $_GET['screen_name'] = $this->selfUser['nick'];
1655 api_statuses_home_timeline('json');
1659 * Test the api_statuses_home_timeline() function with an RSS result.
1663 public function testApiStatusesHomeTimelineWithRss()
1665 $result = api_statuses_home_timeline('rss');
1666 $this->assertXml($result, 'statuses');
1670 * Test the api_statuses_public_timeline() function.
1674 public function testApiStatusesPublicTimeline()
1676 $_REQUEST['max_id'] = 10;
1677 $_REQUEST['conversation_id'] = 1;
1678 $result = api_statuses_public_timeline('json');
1679 $this->assertNotEmpty($result['status']);
1680 foreach ($result['status'] as $status) {
1681 $this->assertStatus($status);
1686 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1690 public function testApiStatusesPublicTimelineWithExcludeReplies()
1692 $_REQUEST['max_id'] = 10;
1693 $_REQUEST['exclude_replies'] = true;
1694 $result = api_statuses_public_timeline('json');
1695 $this->assertNotEmpty($result['status']);
1696 foreach ($result['status'] as $status) {
1697 $this->assertStatus($status);
1702 * Test the api_statuses_public_timeline() function with a negative page parameter.
1706 public function testApiStatusesPublicTimelineWithNegativePage()
1708 $_REQUEST['page'] = -2;
1709 $result = api_statuses_public_timeline('json');
1710 $this->assertNotEmpty($result['status']);
1711 foreach ($result['status'] as $status) {
1712 $this->assertStatus($status);
1717 * Test the api_statuses_public_timeline() function with an unallowed user.
1720 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1722 public function testApiStatusesPublicTimelineWithUnallowedUser()
1724 $_SESSION['allow_api'] = false;
1725 $_GET['screen_name'] = $this->selfUser['nick'];
1726 api_statuses_public_timeline('json');
1730 * Test the api_statuses_public_timeline() function with an RSS result.
1734 public function testApiStatusesPublicTimelineWithRss()
1736 $result = api_statuses_public_timeline('rss');
1737 $this->assertXml($result, 'statuses');
1741 * Test the api_statuses_networkpublic_timeline() function.
1745 public function testApiStatusesNetworkpublicTimeline()
1747 $_REQUEST['max_id'] = 10;
1748 $result = api_statuses_networkpublic_timeline('json');
1749 $this->assertNotEmpty($result['status']);
1750 foreach ($result['status'] as $status) {
1751 $this->assertStatus($status);
1756 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1760 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1762 $_REQUEST['page'] = -2;
1763 $result = api_statuses_networkpublic_timeline('json');
1764 $this->assertNotEmpty($result['status']);
1765 foreach ($result['status'] as $status) {
1766 $this->assertStatus($status);
1771 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1774 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1776 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1778 $_SESSION['allow_api'] = false;
1779 $_GET['screen_name'] = $this->selfUser['nick'];
1780 api_statuses_networkpublic_timeline('json');
1784 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1788 public function testApiStatusesNetworkpublicTimelineWithRss()
1790 $result = api_statuses_networkpublic_timeline('rss');
1791 $this->assertXml($result, 'statuses');
1795 * Test the api_statuses_show() function.
1798 * @expectedException Friendica\Network\HTTPException\BadRequestException
1800 public function testApiStatusesShow()
1802 api_statuses_show('json');
1806 * Test the api_statuses_show() function with an ID.
1810 public function testApiStatusesShowWithId()
1812 $this->app->argv[3] = 1;
1813 $result = api_statuses_show('json');
1814 $this->assertStatus($result['status']);
1818 * Test the api_statuses_show() function with the conversation parameter.
1822 public function testApiStatusesShowWithConversation()
1824 $this->app->argv[3] = 1;
1825 $_REQUEST['conversation'] = 1;
1826 $result = api_statuses_show('json');
1827 $this->assertNotEmpty($result['status']);
1828 foreach ($result['status'] as $status) {
1829 $this->assertStatus($status);
1834 * Test the api_statuses_show() function with an unallowed user.
1837 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1839 public function testApiStatusesShowWithUnallowedUser()
1841 $_SESSION['allow_api'] = false;
1842 $_GET['screen_name'] = $this->selfUser['nick'];
1843 api_statuses_show('json');
1847 * Test the api_conversation_show() function.
1850 * @expectedException Friendica\Network\HTTPException\BadRequestException
1852 public function testApiConversationShow()
1854 api_conversation_show('json');
1858 * Test the api_conversation_show() function with an ID.
1862 public function testApiConversationShowWithId()
1864 $this->app->argv[3] = 1;
1865 $_REQUEST['max_id'] = 10;
1866 $_REQUEST['page'] = -2;
1867 $result = api_conversation_show('json');
1868 $this->assertNotEmpty($result['status']);
1869 foreach ($result['status'] as $status) {
1870 $this->assertStatus($status);
1875 * Test the api_conversation_show() function with an unallowed user.
1878 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1880 public function testApiConversationShowWithUnallowedUser()
1882 $_SESSION['allow_api'] = false;
1883 $_GET['screen_name'] = $this->selfUser['nick'];
1884 api_conversation_show('json');
1888 * Test the api_statuses_repeat() function.
1891 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1893 public function testApiStatusesRepeat()
1895 api_statuses_repeat('json');
1899 * Test the api_statuses_repeat() function without an authenticated user.
1902 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1904 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1906 $_SESSION['authenticated'] = false;
1907 api_statuses_repeat('json');
1911 * Test the api_statuses_repeat() function with an ID.
1915 public function testApiStatusesRepeatWithId()
1917 $this->app->argv[3] = 1;
1918 $result = api_statuses_repeat('json');
1919 $this->assertStatus($result['status']);
1921 // Also test with a shared status
1922 $this->app->argv[3] = 5;
1923 $result = api_statuses_repeat('json');
1924 $this->assertStatus($result['status']);
1928 * Test the api_statuses_destroy() function.
1931 * @expectedException Friendica\Network\HTTPException\BadRequestException
1933 public function testApiStatusesDestroy()
1935 api_statuses_destroy('json');
1939 * Test the api_statuses_destroy() function without an authenticated user.
1942 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1944 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1946 $_SESSION['authenticated'] = false;
1947 api_statuses_destroy('json');
1951 * Test the api_statuses_destroy() function with an ID.
1955 public function testApiStatusesDestroyWithId()
1957 $this->app->argv[3] = 1;
1958 $result = api_statuses_destroy('json');
1959 $this->assertStatus($result['status']);
1963 * Test the api_statuses_mentions() function.
1967 public function testApiStatusesMentions()
1969 $this->app->user = ['nickname' => $this->selfUser['nick']];
1970 $_REQUEST['max_id'] = 10;
1971 $result = api_statuses_mentions('json');
1972 $this->assertEmpty($result['status']);
1973 // We should test with mentions in the database.
1977 * Test the api_statuses_mentions() function with a negative page parameter.
1981 public function testApiStatusesMentionsWithNegativePage()
1983 $_REQUEST['page'] = -2;
1984 $result = api_statuses_mentions('json');
1985 $this->assertEmpty($result['status']);
1989 * Test the api_statuses_mentions() function with an unallowed user.
1992 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1994 public function testApiStatusesMentionsWithUnallowedUser()
1996 $_SESSION['allow_api'] = false;
1997 $_GET['screen_name'] = $this->selfUser['nick'];
1998 api_statuses_mentions('json');
2002 * Test the api_statuses_mentions() function with an RSS result.
2006 public function testApiStatusesMentionsWithRss()
2008 $result = api_statuses_mentions('rss');
2009 $this->assertXml($result, 'statuses');
2013 * Test the api_statuses_user_timeline() function.
2017 public function testApiStatusesUserTimeline()
2019 $_REQUEST['max_id'] = 10;
2020 $_REQUEST['exclude_replies'] = true;
2021 $_REQUEST['conversation_id'] = 1;
2022 $result = api_statuses_user_timeline('json');
2023 $this->assertNotEmpty($result['status']);
2024 foreach ($result['status'] as $status) {
2025 $this->assertStatus($status);
2030 * Test the api_statuses_user_timeline() function with a negative page parameter.
2034 public function testApiStatusesUserTimelineWithNegativePage()
2036 $_REQUEST['page'] = -2;
2037 $result = api_statuses_user_timeline('json');
2038 $this->assertNotEmpty($result['status']);
2039 foreach ($result['status'] as $status) {
2040 $this->assertStatus($status);
2045 * Test the api_statuses_user_timeline() function with an RSS result.
2049 public function testApiStatusesUserTimelineWithRss()
2051 $result = api_statuses_user_timeline('rss');
2052 $this->assertXml($result, 'statuses');
2056 * Test the api_statuses_user_timeline() function with an unallowed user.
2059 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2061 public function testApiStatusesUserTimelineWithUnallowedUser()
2063 $_SESSION['allow_api'] = false;
2064 $_GET['screen_name'] = $this->selfUser['nick'];
2065 api_statuses_user_timeline('json');
2069 * Test the api_favorites_create_destroy() function.
2072 * @expectedException Friendica\Network\HTTPException\BadRequestException
2074 public function testApiFavoritesCreateDestroy()
2076 $this->app->argv = ['api', '1.1', 'favorites', 'create'];
2077 $this->app->argc = count($this->app->argv);
2078 api_favorites_create_destroy('json');
2082 * Test the api_favorites_create_destroy() function with an invalid ID.
2085 * @expectedException Friendica\Network\HTTPException\BadRequestException
2087 public function testApiFavoritesCreateDestroyWithInvalidId()
2089 $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json'];
2090 $this->app->argc = count($this->app->argv);
2091 api_favorites_create_destroy('json');
2095 * Test the api_favorites_create_destroy() function with an invalid action.
2098 * @expectedException Friendica\Network\HTTPException\BadRequestException
2100 public function testApiFavoritesCreateDestroyWithInvalidAction()
2102 $this->app->argv = ['api', '1.1', 'favorites', 'change.json'];
2103 $this->app->argc = count($this->app->argv);
2104 $_REQUEST['id'] = 1;
2105 api_favorites_create_destroy('json');
2109 * Test the api_favorites_create_destroy() function with the create action.
2113 public function testApiFavoritesCreateDestroyWithCreateAction()
2115 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2116 $this->app->argc = count($this->app->argv);
2117 $_REQUEST['id'] = 3;
2118 $result = api_favorites_create_destroy('json');
2119 $this->assertStatus($result['status']);
2123 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2127 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2129 $this->app->argv = ['api', '1.1', 'favorites', 'create.rss'];
2130 $this->app->argc = count($this->app->argv);
2131 $_REQUEST['id'] = 3;
2132 $result = api_favorites_create_destroy('rss');
2133 $this->assertXml($result, 'status');
2137 * Test the api_favorites_create_destroy() function with the destroy action.
2141 public function testApiFavoritesCreateDestroyWithDestroyAction()
2143 $this->app->argv = ['api', '1.1', 'favorites', 'destroy.json'];
2144 $this->app->argc = count($this->app->argv);
2145 $_REQUEST['id'] = 3;
2146 $result = api_favorites_create_destroy('json');
2147 $this->assertStatus($result['status']);
2151 * Test the api_favorites_create_destroy() function without an authenticated user.
2154 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2156 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2158 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2159 $this->app->argc = count($this->app->argv);
2160 $_SESSION['authenticated'] = false;
2161 api_favorites_create_destroy('json');
2165 * Test the api_favorites() function.
2169 public function testApiFavorites()
2171 $_REQUEST['page'] = -1;
2172 $_REQUEST['max_id'] = 10;
2173 $result = api_favorites('json');
2174 foreach ($result['status'] as $status) {
2175 $this->assertStatus($status);
2180 * Test the api_favorites() function with an RSS result.
2184 public function testApiFavoritesWithRss()
2186 $result = api_favorites('rss');
2187 $this->assertXml($result, 'statuses');
2191 * Test the api_favorites() function with an unallowed user.
2194 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2196 public function testApiFavoritesWithUnallowedUser()
2198 $_SESSION['allow_api'] = false;
2199 $_GET['screen_name'] = $this->selfUser['nick'];
2200 api_favorites('json');
2204 * Test the api_format_messages() function.
2208 public function testApiFormatMessages()
2210 $result = api_format_messages(
2211 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2212 ['id' => 2, 'screen_name' => 'recipient_name'],
2213 ['id' => 3, 'screen_name' => 'sender_name']
2215 $this->assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2216 $this->assertEquals(1, $result['id']);
2217 $this->assertEquals(2, $result['recipient_id']);
2218 $this->assertEquals(3, $result['sender_id']);
2219 $this->assertEquals('recipient_name', $result['recipient_screen_name']);
2220 $this->assertEquals('sender_name', $result['sender_screen_name']);
2224 * Test the api_format_messages() function with HTML.
2228 public function testApiFormatMessagesWithHtmlText()
2230 $_GET['getText'] = 'html';
2231 $result = api_format_messages(
2232 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2233 ['id' => 2, 'screen_name' => 'recipient_name'],
2234 ['id' => 3, 'screen_name' => 'sender_name']
2236 $this->assertEquals('item_title', $result['title']);
2237 $this->assertEquals('<strong>item_body</strong>', $result['text']);
2241 * Test the api_format_messages() function with plain text.
2245 public function testApiFormatMessagesWithPlainText()
2247 $_GET['getText'] = 'plain';
2248 $result = api_format_messages(
2249 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2250 ['id' => 2, 'screen_name' => 'recipient_name'],
2251 ['id' => 3, 'screen_name' => 'sender_name']
2253 $this->assertEquals('item_title', $result['title']);
2254 $this->assertEquals('item_body', $result['text']);
2258 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2262 public function testApiFormatMessagesWithoutUserObjects()
2264 $_GET['getUserObjects'] = 'false';
2265 $result = api_format_messages(
2266 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2267 ['id' => 2, 'screen_name' => 'recipient_name'],
2268 ['id' => 3, 'screen_name' => 'sender_name']
2270 $this->assertTrue(!isset($result['sender']));
2271 $this->assertTrue(!isset($result['recipient']));
2275 * Test the api_convert_item() function.
2279 public function testApiConvertItem()
2281 $result = api_convert_item(
2283 'network' => 'feed',
2284 'title' => 'item_title',
2285 // We need a long string to test that it is correctly cut
2286 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2287 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2288 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2289 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2290 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2291 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2292 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2293 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2294 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2295 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2296 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2297 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2298 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2299 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2300 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2301 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2302 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2303 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2304 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2305 'repellat officia illum quos impedit quam iste esse unde qui ' .
2306 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2307 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2308 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2309 'plink' => 'item_plink'
2312 $this->assertStringStartsWith('item_title', $result['text']);
2313 $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2317 * Test the api_convert_item() function with an empty item body.
2321 public function testApiConvertItemWithoutBody()
2323 $result = api_convert_item(
2325 'network' => 'feed',
2326 'title' => 'item_title',
2328 'plink' => 'item_plink'
2331 $this->assertEquals('item_title', $result['text']);
2332 $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2336 * Test the api_convert_item() function with the title in the body.
2340 public function testApiConvertItemWithTitleInBody()
2342 $result = api_convert_item(
2344 'title' => 'item_title',
2345 'body' => 'item_title item_body'
2348 $this->assertEquals('item_title item_body', $result['text']);
2349 $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2353 * Test the api_get_attachments() function.
2357 public function testApiGetAttachments()
2360 $this->assertEmpty(api_get_attachments($body));
2364 * Test the api_get_attachments() function with an img tag.
2368 public function testApiGetAttachmentsWithImage()
2370 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2371 $this->assertInternalType('array', api_get_attachments($body));
2375 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2379 public function testApiGetAttachmentsWithImageAndAndStatus()
2381 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2382 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2383 $this->assertInternalType('array', api_get_attachments($body));
2387 * Test the api_get_entitities() function.
2391 public function testApiGetEntitities()
2394 $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
2398 * Test the api_get_entitities() function with the include_entities parameter.
2402 public function testApiGetEntititiesWithIncludeEntities()
2404 $_REQUEST['include_entities'] = 'true';
2406 $result = api_get_entitities($text, 'bbcode');
2407 $this->assertInternalType('array', $result['hashtags']);
2408 $this->assertInternalType('array', $result['symbols']);
2409 $this->assertInternalType('array', $result['urls']);
2410 $this->assertInternalType('array', $result['user_mentions']);
2414 * Test the api_format_items_embeded_images() function.
2418 public function testApiFormatItemsEmbededImages()
2420 $this->assertEquals(
2421 'text ' . System::baseUrl() . '/display/item_guid',
2422 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2427 * Test the api_contactlink_to_array() function.
2431 public function testApiContactlinkToArray()
2433 $this->assertEquals(
2438 api_contactlink_to_array('text')
2443 * Test the api_contactlink_to_array() function with an URL.
2447 public function testApiContactlinkToArrayWithUrl()
2449 $this->assertEquals(
2451 'name' => ['link_text'],
2454 api_contactlink_to_array('text <a href="url">link_text</a>')
2459 * Test the api_format_items_activities() function.
2463 public function testApiFormatItemsActivities()
2465 $item = ['uid' => 0, 'uri' => ''];
2466 $result = api_format_items_activities($item);
2467 $this->assertArrayHasKey('like', $result);
2468 $this->assertArrayHasKey('dislike', $result);
2469 $this->assertArrayHasKey('attendyes', $result);
2470 $this->assertArrayHasKey('attendno', $result);
2471 $this->assertArrayHasKey('attendmaybe', $result);
2475 * Test the api_format_items_activities() function with an XML result.
2479 public function testApiFormatItemsActivitiesWithXml()
2481 $item = ['uid' => 0, 'uri' => ''];
2482 $result = api_format_items_activities($item, 'xml');
2483 $this->assertArrayHasKey('friendica:like', $result);
2484 $this->assertArrayHasKey('friendica:dislike', $result);
2485 $this->assertArrayHasKey('friendica:attendyes', $result);
2486 $this->assertArrayHasKey('friendica:attendno', $result);
2487 $this->assertArrayHasKey('friendica:attendmaybe', $result);
2491 * Test the api_format_items_profiles() function.
2495 public function testApiFormatItemsProfiles()
2498 'id' => 'profile_id',
2499 'profile-name' => 'profile_name',
2500 'is-default' => true,
2501 'hide-friends' => true,
2502 'photo' => 'profile_photo',
2503 'thumb' => 'profile_thumb',
2505 'net-publish' => true,
2506 'pdesc' => 'description',
2507 'dob' => 'date_of_birth',
2508 'address' => 'address',
2509 'locality' => 'city',
2510 'region' => 'region',
2511 'postal-code' => 'postal_code',
2512 'country-name' => 'country',
2513 'hometown' => 'hometown',
2514 'gender' => 'gender',
2515 'marital' => 'marital',
2516 'with' => 'marital_with',
2517 'howlong' => 'marital_since',
2518 'sexual' => 'sexual',
2519 'politic' => 'politic',
2520 'religion' => 'religion',
2521 'pub_keywords' => 'public_keywords',
2522 'prv_keywords' => 'private_keywords',
2525 'dislikes' => 'dislikes',
2531 'interest' => 'interest',
2532 'romance' => 'romance',
2534 'education' => 'education',
2535 'contact' => 'social_networks',
2536 'homepage' => 'homepage'
2538 $result = api_format_items_profiles($profile_row);
2539 $this->assertEquals(
2541 'profile_id' => 'profile_id',
2542 'profile_name' => 'profile_name',
2543 'is_default' => true,
2544 'hide_friends' => true,
2545 'profile_photo' => 'profile_photo',
2546 'profile_thumb' => 'profile_thumb',
2548 'net_publish' => true,
2549 'description' => 'description',
2550 'date_of_birth' => 'date_of_birth',
2551 'address' => 'address',
2553 'region' => 'region',
2554 'postal_code' => 'postal_code',
2555 'country' => 'country',
2556 'hometown' => 'hometown',
2557 'gender' => 'gender',
2558 'marital' => 'marital',
2559 'marital_with' => 'marital_with',
2560 'marital_since' => 'marital_since',
2561 'sexual' => 'sexual',
2562 'politic' => 'politic',
2563 'religion' => 'religion',
2564 'public_keywords' => 'public_keywords',
2565 'private_keywords' => 'private_keywords',
2568 'dislikes' => 'dislikes',
2574 'interest' => 'interest',
2575 'romance' => 'romance',
2577 'education' => 'education',
2578 'social_networks' => 'social_networks',
2579 'homepage' => 'homepage',
2587 * Test the api_format_items() function.
2591 public function testApiFormatItems()
2595 'item_network' => 'item_network',
2601 'author-network' => Protocol::DFRN,
2602 'author-link' => 'http://localhost/profile/othercontact',
2606 $result = api_format_items($items, ['id' => 0], true);
2607 foreach ($result as $status) {
2608 $this->assertStatus($status);
2613 * Test the api_format_items() function with an XML result.
2617 public function testApiFormatItemsWithXml()
2625 'author-network' => Protocol::DFRN,
2626 'author-link' => 'http://localhost/profile/othercontact',
2630 $result = api_format_items($items, ['id' => 0], true, 'xml');
2631 foreach ($result as $status) {
2632 $this->assertStatus($status);
2637 * Test the api_format_items() function.
2641 public function testApiAccountRateLimitStatus()
2643 $result = api_account_rate_limit_status('json');
2644 $this->assertEquals(150, $result['hash']['remaining_hits']);
2645 $this->assertEquals(150, $result['hash']['hourly_limit']);
2646 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2650 * Test the api_format_items() function with an XML result.
2654 public function testApiAccountRateLimitStatusWithXml()
2656 $result = api_account_rate_limit_status('xml');
2657 $this->assertXml($result, 'hash');
2661 * Test the api_help_test() function.
2665 public function testApiHelpTest()
2667 $result = api_help_test('json');
2668 $this->assertEquals(['ok' => 'ok'], $result);
2672 * Test the api_help_test() function with an XML result.
2676 public function testApiHelpTestWithXml()
2678 $result = api_help_test('xml');
2679 $this->assertXml($result, 'ok');
2683 * Test the api_lists_list() function.
2687 public function testApiListsList()
2689 $result = api_lists_list('json');
2690 $this->assertEquals(['lists_list' => []], $result);
2694 * Test the api_lists_ownerships() function.
2698 public function testApiListsOwnerships()
2700 $result = api_lists_ownerships('json');
2701 foreach ($result['lists']['lists'] as $list) {
2702 $this->assertList($list);
2707 * Test the api_lists_ownerships() function without an authenticated user.
2710 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2712 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2714 $_SESSION['authenticated'] = false;
2715 api_lists_ownerships('json');
2719 * Test the api_lists_statuses() function.
2721 * @expectedException Friendica\Network\HTTPException\BadRequestException
2724 public function testApiListsStatuses()
2726 api_lists_statuses('json');
2730 * Test the api_lists_statuses() function with a list ID.
2734 public function testApiListsStatusesWithListId()
2736 $_REQUEST['list_id'] = 1;
2737 $_REQUEST['page'] = -1;
2738 $_REQUEST['max_id'] = 10;
2739 $result = api_lists_statuses('json');
2740 foreach ($result['status'] as $status) {
2741 $this->assertStatus($status);
2746 * Test the api_lists_statuses() function with a list ID and a RSS result.
2750 public function testApiListsStatusesWithListIdAndRss()
2752 $_REQUEST['list_id'] = 1;
2753 $result = api_lists_statuses('rss');
2754 $this->assertXml($result, 'statuses');
2758 * Test the api_lists_statuses() function with an unallowed user.
2761 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2763 public function testApiListsStatusesWithUnallowedUser()
2765 $_SESSION['allow_api'] = false;
2766 $_GET['screen_name'] = $this->selfUser['nick'];
2767 api_lists_statuses('json');
2771 * Test the api_statuses_f() function.
2775 public function testApiStatusesFWithFriends()
2778 $result = api_statuses_f('friends');
2779 $this->assertArrayHasKey('user', $result);
2783 * Test the api_statuses_f() function.
2787 public function testApiStatusesFWithFollowers()
2789 $result = api_statuses_f('followers');
2790 $this->assertArrayHasKey('user', $result);
2794 * Test the api_statuses_f() function.
2798 public function testApiStatusesFWithBlocks()
2800 $result = api_statuses_f('blocks');
2801 $this->assertArrayHasKey('user', $result);
2805 * Test the api_statuses_f() function.
2809 public function testApiStatusesFWithIncoming()
2811 $result = api_statuses_f('incoming');
2812 $this->assertArrayHasKey('user', $result);
2816 * Test the api_statuses_f() function an undefined cursor GET variable.
2820 public function testApiStatusesFWithUndefinedCursor()
2822 $_GET['cursor'] = 'undefined';
2823 $this->assertFalse(api_statuses_f('friends'));
2827 * Test the api_statuses_friends() function.
2831 public function testApiStatusesFriends()
2833 $result = api_statuses_friends('json');
2834 $this->assertArrayHasKey('user', $result);
2838 * Test the api_statuses_friends() function an undefined cursor GET variable.
2842 public function testApiStatusesFriendsWithUndefinedCursor()
2844 $_GET['cursor'] = 'undefined';
2845 $this->assertFalse(api_statuses_friends('json'));
2849 * Test the api_statuses_followers() function.
2853 public function testApiStatusesFollowers()
2855 $result = api_statuses_followers('json');
2856 $this->assertArrayHasKey('user', $result);
2860 * Test the api_statuses_followers() function an undefined cursor GET variable.
2864 public function testApiStatusesFollowersWithUndefinedCursor()
2866 $_GET['cursor'] = 'undefined';
2867 $this->assertFalse(api_statuses_followers('json'));
2871 * Test the api_blocks_list() function.
2875 public function testApiBlocksList()
2877 $result = api_blocks_list('json');
2878 $this->assertArrayHasKey('user', $result);
2882 * Test the api_blocks_list() function an undefined cursor GET variable.
2886 public function testApiBlocksListWithUndefinedCursor()
2888 $_GET['cursor'] = 'undefined';
2889 $this->assertFalse(api_blocks_list('json'));
2893 * Test the api_friendships_incoming() function.
2897 public function testApiFriendshipsIncoming()
2899 $result = api_friendships_incoming('json');
2900 $this->assertArrayHasKey('id', $result);
2904 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2908 public function testApiFriendshipsIncomingWithUndefinedCursor()
2910 $_GET['cursor'] = 'undefined';
2911 $this->assertFalse(api_friendships_incoming('json'));
2915 * Test the api_statusnet_config() function.
2919 public function testApiStatusnetConfig()
2921 $result = api_statusnet_config('json');
2922 $this->assertEquals('localhost', $result['config']['site']['server']);
2923 $this->assertEquals('default', $result['config']['site']['theme']);
2924 $this->assertEquals(System::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2925 $this->assertTrue($result['config']['site']['fancy']);
2926 $this->assertEquals('en', $result['config']['site']['language']);
2927 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2928 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2929 $this->assertEquals('false', $result['config']['site']['private']);
2930 $this->assertEquals('false', $result['config']['site']['ssl']);
2931 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2935 * Test the api_statusnet_version() function.
2939 public function testApiStatusnetVersion()
2941 $result = api_statusnet_version('json');
2942 $this->assertEquals('0.9.7', $result['version']);
2946 * Test the api_ff_ids() function.
2950 public function testApiFfIds()
2952 $result = api_ff_ids('json', Contact::FOLLOWER);
2953 $this->assertEquals(['id' => []], $result);
2957 * Test the api_ff_ids() function with a result.
2961 public function testApiFfIdsWithResult()
2963 $this->markTestIncomplete();
2967 * Test the api_ff_ids() function without an authenticated user.
2970 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2972 public function testApiFfIdsWithoutAuthenticatedUser()
2974 $_SESSION['authenticated'] = false;
2975 api_ff_ids('json', Contact::FOLLOWER);
2979 * Test the api_friends_ids() function.
2983 public function testApiFriendsIds()
2985 $result = api_friends_ids('json');
2986 $this->assertEquals(['id' => []], $result);
2990 * Test the api_followers_ids() function.
2994 public function testApiFollowersIds()
2996 $result = api_followers_ids('json');
2997 $this->assertEquals(['id' => []], $result);
3001 * Test the api_direct_messages_new() function.
3005 public function testApiDirectMessagesNew()
3007 $result = api_direct_messages_new('json');
3008 $this->assertNull($result);
3012 * Test the api_direct_messages_new() function without an authenticated user.
3015 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3017 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
3019 $_SESSION['authenticated'] = false;
3020 api_direct_messages_new('json');
3024 * Test the api_direct_messages_new() function with an user ID.
3028 public function testApiDirectMessagesNewWithUserId()
3030 $_POST['text'] = 'message_text';
3031 $_POST['user_id'] = $this->otherUser['id'];
3032 $result = api_direct_messages_new('json');
3033 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
3037 * Test the api_direct_messages_new() function with a screen name.
3041 public function testApiDirectMessagesNewWithScreenName()
3043 $_POST['text'] = 'message_text';
3044 $_POST['screen_name'] = $this->friendUser['nick'];
3045 $result = api_direct_messages_new('json');
3046 $this->assertContains('message_text', $result['direct_message']['text']);
3047 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3048 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3052 * Test the api_direct_messages_new() function with a title.
3056 public function testApiDirectMessagesNewWithTitle()
3058 $_POST['text'] = 'message_text';
3059 $_POST['screen_name'] = $this->friendUser['nick'];
3060 $_REQUEST['title'] = 'message_title';
3061 $result = api_direct_messages_new('json');
3062 $this->assertContains('message_text', $result['direct_message']['text']);
3063 $this->assertContains('message_title', $result['direct_message']['text']);
3064 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3065 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3069 * Test the api_direct_messages_new() function with an RSS result.
3073 public function testApiDirectMessagesNewWithRss()
3075 $_POST['text'] = 'message_text';
3076 $_POST['screen_name'] = $this->friendUser['nick'];
3077 $result = api_direct_messages_new('rss');
3078 $this->assertXml($result, 'direct-messages');
3082 * Test the api_direct_messages_destroy() function.
3085 * @expectedException Friendica\Network\HTTPException\BadRequestException
3087 public function testApiDirectMessagesDestroy()
3089 api_direct_messages_destroy('json');
3093 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
3097 public function testApiDirectMessagesDestroyWithVerbose()
3099 $_GET['friendica_verbose'] = 'true';
3100 $result = api_direct_messages_destroy('json');
3101 $this->assertEquals(
3104 'result' => 'error',
3105 'message' => 'message id or parenturi not specified'
3113 * Test the api_direct_messages_destroy() function without an authenticated user.
3116 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3118 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
3120 $_SESSION['authenticated'] = false;
3121 api_direct_messages_destroy('json');
3125 * Test the api_direct_messages_destroy() function with a non-zero ID.
3128 * @expectedException Friendica\Network\HTTPException\BadRequestException
3130 public function testApiDirectMessagesDestroyWithId()
3132 $_REQUEST['id'] = 1;
3133 api_direct_messages_destroy('json');
3137 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
3141 public function testApiDirectMessagesDestroyWithIdAndVerbose()
3143 $_REQUEST['id'] = 1;
3144 $_REQUEST['friendica_parenturi'] = 'parent_uri';
3145 $_GET['friendica_verbose'] = 'true';
3146 $result = api_direct_messages_destroy('json');
3147 $this->assertEquals(
3150 'result' => 'error',
3151 'message' => 'message id not in database'
3159 * Test the api_direct_messages_destroy() function with a non-zero ID.
3163 public function testApiDirectMessagesDestroyWithCorrectId()
3165 $this->markTestIncomplete('We need to add a dataset for this.');
3169 * Test the api_direct_messages_box() function.
3173 public function testApiDirectMessagesBoxWithSentbox()
3175 $_REQUEST['page'] = -1;
3176 $_REQUEST['max_id'] = 10;
3177 $result = api_direct_messages_box('json', 'sentbox', 'false');
3178 $this->assertArrayHasKey('direct_message', $result);
3182 * Test the api_direct_messages_box() function.
3186 public function testApiDirectMessagesBoxWithConversation()
3188 $result = api_direct_messages_box('json', 'conversation', 'false');
3189 $this->assertArrayHasKey('direct_message', $result);
3193 * Test the api_direct_messages_box() function.
3197 public function testApiDirectMessagesBoxWithAll()
3199 $result = api_direct_messages_box('json', 'all', 'false');
3200 $this->assertArrayHasKey('direct_message', $result);
3204 * Test the api_direct_messages_box() function.
3208 public function testApiDirectMessagesBoxWithInbox()
3210 $result = api_direct_messages_box('json', 'inbox', 'false');
3211 $this->assertArrayHasKey('direct_message', $result);
3215 * Test the api_direct_messages_box() function.
3219 public function testApiDirectMessagesBoxWithVerbose()
3221 $result = api_direct_messages_box('json', 'sentbox', 'true');
3222 $this->assertEquals(
3225 'result' => 'error',
3226 'message' => 'no mails available'
3234 * Test the api_direct_messages_box() function with a RSS result.
3238 public function testApiDirectMessagesBoxWithRss()
3240 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3241 $this->assertXml($result, 'direct-messages');
3245 * Test the api_direct_messages_box() function without an authenticated user.
3248 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3250 public function testApiDirectMessagesBoxWithUnallowedUser()
3252 $_SESSION['allow_api'] = false;
3253 $_GET['screen_name'] = $this->selfUser['nick'];
3254 api_direct_messages_box('json', 'sentbox', 'false');
3258 * Test the api_direct_messages_sentbox() function.
3262 public function testApiDirectMessagesSentbox()
3264 $result = api_direct_messages_sentbox('json');
3265 $this->assertArrayHasKey('direct_message', $result);
3269 * Test the api_direct_messages_inbox() function.
3273 public function testApiDirectMessagesInbox()
3275 $result = api_direct_messages_inbox('json');
3276 $this->assertArrayHasKey('direct_message', $result);
3280 * Test the api_direct_messages_all() function.
3284 public function testApiDirectMessagesAll()
3286 $result = api_direct_messages_all('json');
3287 $this->assertArrayHasKey('direct_message', $result);
3291 * Test the api_direct_messages_conversation() function.
3295 public function testApiDirectMessagesConversation()
3297 $result = api_direct_messages_conversation('json');
3298 $this->assertArrayHasKey('direct_message', $result);
3302 * Test the api_oauth_request_token() function.
3306 public function testApiOauthRequestToken()
3308 $this->markTestIncomplete('killme() kills phpunit as well');
3312 * Test the api_oauth_access_token() function.
3316 public function testApiOauthAccessToken()
3318 $this->markTestIncomplete('killme() kills phpunit as well');
3322 * Test the api_fr_photoalbum_delete() function.
3325 * @expectedException Friendica\Network\HTTPException\BadRequestException
3327 public function testApiFrPhotoalbumDelete()
3329 api_fr_photoalbum_delete('json');
3333 * Test the api_fr_photoalbum_delete() function with an album name.
3336 * @expectedException Friendica\Network\HTTPException\BadRequestException
3338 public function testApiFrPhotoalbumDeleteWithAlbum()
3340 $_REQUEST['album'] = 'album_name';
3341 api_fr_photoalbum_delete('json');
3345 * Test the api_fr_photoalbum_delete() function with an album name.
3349 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3351 $this->markTestIncomplete('We need to add a dataset for this.');
3355 * Test the api_fr_photoalbum_delete() function.
3358 * @expectedException Friendica\Network\HTTPException\BadRequestException
3360 public function testApiFrPhotoalbumUpdate()
3362 api_fr_photoalbum_update('json');
3366 * Test the api_fr_photoalbum_delete() function with an album name.
3369 * @expectedException Friendica\Network\HTTPException\BadRequestException
3371 public function testApiFrPhotoalbumUpdateWithAlbum()
3373 $_REQUEST['album'] = 'album_name';
3374 api_fr_photoalbum_update('json');
3378 * Test the api_fr_photoalbum_delete() function with an album name.
3381 * @expectedException Friendica\Network\HTTPException\BadRequestException
3383 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3385 $_REQUEST['album'] = 'album_name';
3386 $_REQUEST['album_new'] = 'album_name';
3387 api_fr_photoalbum_update('json');
3391 * Test the api_fr_photoalbum_update() function without an authenticated user.
3394 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3396 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3398 $_SESSION['authenticated'] = false;
3399 api_fr_photoalbum_update('json');
3403 * Test the api_fr_photoalbum_delete() function with an album name.
3407 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3409 $this->markTestIncomplete('We need to add a dataset for this.');
3413 * Test the api_fr_photos_list() function.
3417 public function testApiFrPhotosList()
3419 $result = api_fr_photos_list('json');
3420 $this->assertArrayHasKey('photo', $result);
3424 * Test the api_fr_photos_list() function without an authenticated user.
3427 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3429 public function testApiFrPhotosListWithoutAuthenticatedUser()
3431 $_SESSION['authenticated'] = false;
3432 api_fr_photos_list('json');
3436 * Test the api_fr_photo_create_update() function.
3439 * @expectedException Friendica\Network\HTTPException\BadRequestException
3441 public function testApiFrPhotoCreateUpdate()
3443 api_fr_photo_create_update('json');
3447 * Test the api_fr_photo_create_update() function without an authenticated user.
3450 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3452 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3454 $_SESSION['authenticated'] = false;
3455 api_fr_photo_create_update('json');
3459 * Test the api_fr_photo_create_update() function with an album name.
3462 * @expectedException Friendica\Network\HTTPException\BadRequestException
3464 public function testApiFrPhotoCreateUpdateWithAlbum()
3466 $_REQUEST['album'] = 'album_name';
3467 api_fr_photo_create_update('json');
3471 * Test the api_fr_photo_create_update() function with the update mode.
3475 public function testApiFrPhotoCreateUpdateWithUpdate()
3477 $this->markTestIncomplete('We need to create a dataset for this');
3481 * Test the api_fr_photo_create_update() function with an uploaded file.
3485 public function testApiFrPhotoCreateUpdateWithFile()
3487 $this->markTestIncomplete();
3491 * Test the api_fr_photo_delete() function.
3494 * @expectedException Friendica\Network\HTTPException\BadRequestException
3496 public function testApiFrPhotoDelete()
3498 api_fr_photo_delete('json');
3502 * Test the api_fr_photo_delete() function without an authenticated user.
3505 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3507 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3509 $_SESSION['authenticated'] = false;
3510 api_fr_photo_delete('json');
3514 * Test the api_fr_photo_delete() function with a photo ID.
3517 * @expectedException Friendica\Network\HTTPException\BadRequestException
3519 public function testApiFrPhotoDeleteWithPhotoId()
3521 $_REQUEST['photo_id'] = 1;
3522 api_fr_photo_delete('json');
3526 * Test the api_fr_photo_delete() function with a correct photo ID.
3530 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3532 $this->markTestIncomplete('We need to create a dataset for this.');
3536 * Test the api_fr_photo_detail() function.
3539 * @expectedException Friendica\Network\HTTPException\BadRequestException
3541 public function testApiFrPhotoDetail()
3543 api_fr_photo_detail('json');
3547 * Test the api_fr_photo_detail() function without an authenticated user.
3550 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3552 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3554 $_SESSION['authenticated'] = false;
3555 api_fr_photo_detail('json');
3559 * Test the api_fr_photo_detail() function with a photo ID.
3562 * @expectedException Friendica\Network\HTTPException\NotFoundException
3564 public function testApiFrPhotoDetailWithPhotoId()
3566 $_REQUEST['photo_id'] = 1;
3567 api_fr_photo_detail('json');
3571 * Test the api_fr_photo_detail() function with a correct photo ID.
3575 public function testApiFrPhotoDetailCorrectPhotoId()
3577 $this->markTestIncomplete('We need to create a dataset for this.');
3581 * Test the api_account_update_profile_image() function.
3584 * @expectedException Friendica\Network\HTTPException\BadRequestException
3586 public function testApiAccountUpdateProfileImage()
3588 api_account_update_profile_image('json');
3592 * Test the api_account_update_profile_image() function without an authenticated user.
3595 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3597 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3599 $_SESSION['authenticated'] = false;
3600 api_account_update_profile_image('json');
3604 * Test the api_account_update_profile_image() function with an uploaded file.
3607 * @expectedException Friendica\Network\HTTPException\BadRequestException
3609 public function testApiAccountUpdateProfileImageWithUpload()
3611 $this->markTestIncomplete();
3616 * Test the api_account_update_profile() function.
3620 public function testApiAccountUpdateProfile()
3622 $_POST['name'] = 'new_name';
3623 $_POST['description'] = 'new_description';
3624 $result = api_account_update_profile('json');
3625 // We can't use assertSelfUser() here because the user object is missing some properties.
3626 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3627 $this->assertEquals('DFRN', $result['user']['location']);
3628 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3629 $this->assertEquals('dfrn', $result['user']['network']);
3630 $this->assertEquals('new_name', $result['user']['name']);
3631 $this->assertEquals('new_description', $result['user']['description']);
3635 * Test the check_acl_input() function.
3639 public function testCheckAclInput()
3641 $result = check_acl_input('<aclstring>');
3642 // Where does this result come from?
3643 $this->assertEquals(1, $result);
3647 * Test the check_acl_input() function with an empty ACL string.
3651 public function testCheckAclInputWithEmptyAclString()
3653 $result = check_acl_input(' ');
3654 $this->assertFalse($result);
3658 * Test the save_media_to_database() function.
3662 public function testSaveMediaToDatabase()
3664 $this->markTestIncomplete();
3668 * Test the post_photo_item() function.
3672 public function testPostPhotoItem()
3674 $this->markTestIncomplete();
3678 * Test the prepare_photo_data() function.
3682 public function testPreparePhotoData()
3684 $this->markTestIncomplete();
3688 * Test the api_friendica_remoteauth() function.
3691 * @expectedException Friendica\Network\HTTPException\BadRequestException
3693 public function testApiFriendicaRemoteauth()
3695 api_friendica_remoteauth();
3699 * Test the api_friendica_remoteauth() function with an URL.
3702 * @expectedException Friendica\Network\HTTPException\BadRequestException
3704 public function testApiFriendicaRemoteauthWithUrl()
3706 $_GET['url'] = 'url';
3707 $_GET['c_url'] = 'url';
3708 api_friendica_remoteauth();
3712 * Test the api_friendica_remoteauth() function with a correct URL.
3716 public function testApiFriendicaRemoteauthWithCorrectUrl()
3718 $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
3719 $_GET['url'] = 'url';
3720 $_GET['c_url'] = $this->selfUser['nurl'];
3721 api_friendica_remoteauth();
3725 * Test the api_share_as_retweet() function.
3729 public function testApiShareAsRetweet()
3731 $item = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3732 $result = api_share_as_retweet($item);
3733 $this->assertFalse($result);
3737 * Test the api_share_as_retweet() function with a valid item.
3741 public function testApiShareAsRetweetWithValidItem()
3743 $this->markTestIncomplete();
3747 * Test the api_in_reply_to() function.
3751 public function testApiInReplyTo()
3753 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3754 $this->assertArrayHasKey('status_id', $result);
3755 $this->assertArrayHasKey('user_id', $result);
3756 $this->assertArrayHasKey('status_id_str', $result);
3757 $this->assertArrayHasKey('user_id_str', $result);
3758 $this->assertArrayHasKey('screen_name', $result);
3762 * Test the api_in_reply_to() function with a valid item.
3766 public function testApiInReplyToWithValidItem()
3768 $this->markTestIncomplete();
3772 * Test the api_clean_plain_items() function.
3776 public function testApiCleanPlainItems()
3778 $_REQUEST['include_entities'] = 'true';
3779 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3780 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3784 * Test the api_best_nickname() function.
3788 public function testApiBestNickname()
3791 $result = api_best_nickname($contacts);
3792 $this->assertNull($result);
3796 * Test the api_best_nickname() function with contacts.
3800 public function testApiBestNicknameWithContacts()
3802 $this->markTestIncomplete();
3806 * Test the api_friendica_group_show() function.
3810 public function testApiFriendicaGroupShow()
3812 $this->markTestIncomplete();
3816 * Test the api_friendica_group_delete() function.
3820 public function testApiFriendicaGroupDelete()
3822 $this->markTestIncomplete();
3826 * Test the api_lists_destroy() function.
3830 public function testApiListsDestroy()
3832 $this->markTestIncomplete();
3836 * Test the group_create() function.
3840 public function testGroupCreate()
3842 $this->markTestIncomplete();
3846 * Test the api_friendica_group_create() function.
3850 public function testApiFriendicaGroupCreate()
3852 $this->markTestIncomplete();
3856 * Test the api_lists_create() function.
3860 public function testApiListsCreate()
3862 $this->markTestIncomplete();
3866 * Test the api_friendica_group_update() function.
3870 public function testApiFriendicaGroupUpdate()
3872 $this->markTestIncomplete();
3876 * Test the api_lists_update() function.
3880 public function testApiListsUpdate()
3882 $this->markTestIncomplete();
3886 * Test the api_friendica_activity() function.
3890 public function testApiFriendicaActivity()
3892 $this->markTestIncomplete();
3896 * Test the api_friendica_notification() function.
3899 * @expectedException Friendica\Network\HTTPException\BadRequestException
3901 public function testApiFriendicaNotification()
3903 api_friendica_notification('json');
3907 * Test the api_friendica_notification() function without an authenticated user.
3910 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3912 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3914 $_SESSION['authenticated'] = false;
3915 api_friendica_notification('json');
3919 * Test the api_friendica_notification() function with an argument count.
3923 public function testApiFriendicaNotificationWithArgumentCount()
3925 $this->app->argv = ['api', 'friendica', 'notification'];
3926 $this->app->argc = count($this->app->argv);
3927 $result = api_friendica_notification('json');
3928 $this->assertEquals(['note' => false], $result);
3932 * Test the api_friendica_notification() function with an XML result.
3936 public function testApiFriendicaNotificationWithXmlResult()
3938 $this->app->argv = ['api', 'friendica', 'notification'];
3939 $this->app->argc = count($this->app->argv);
3940 $result = api_friendica_notification('xml');
3941 $this->assertXml($result, 'notes');
3945 * Test the api_friendica_notification_seen() function.
3949 public function testApiFriendicaNotificationSeen()
3951 $this->markTestIncomplete();
3955 * Test the api_friendica_direct_messages_setseen() function.
3959 public function testApiFriendicaDirectMessagesSetseen()
3961 $this->markTestIncomplete();
3965 * Test the api_friendica_direct_messages_search() function.
3969 public function testApiFriendicaDirectMessagesSearch()
3971 $this->markTestIncomplete();
3975 * Test the api_friendica_profile_show() function.
3979 public function testApiFriendicaProfileShow()
3981 $result = api_friendica_profile_show('json');
3982 // We can't use assertSelfUser() here because the user object is missing some properties.
3983 $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
3984 $this->assertEquals('DFRN', $result['$result']['friendica_owner']['location']);
3985 $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
3986 $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
3987 $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
3988 $this->assertTrue($result['$result']['friendica_owner']['verified']);
3989 $this->assertFalse($result['$result']['multi_profiles']);
3993 * Test the api_friendica_profile_show() function with a profile ID.
3997 public function testApiFriendicaProfileShowWithProfileId()
3999 $this->markTestIncomplete('We need to add a dataset for this.');
4003 * Test the api_friendica_profile_show() function with a wrong profile ID.
4006 * @expectedException Friendica\Network\HTTPException\BadRequestException
4008 public function testApiFriendicaProfileShowWithWrongProfileId()
4010 $_REQUEST['profile_id'] = 666;
4011 api_friendica_profile_show('json');
4015 * Test the api_friendica_profile_show() function without an authenticated user.
4018 * @expectedException Friendica\Network\HTTPException\ForbiddenException
4020 public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
4022 $_SESSION['authenticated'] = false;
4023 api_friendica_profile_show('json');
4027 * Test the api_saved_searches_list() function.
4031 public function testApiSavedSearchesList()
4033 $result = api_saved_searches_list('json');
4034 $this->assertEquals(1, $result['terms'][0]['id']);
4035 $this->assertEquals(1, $result['terms'][0]['id_str']);
4036 $this->assertEquals('Saved search', $result['terms'][0]['name']);
4037 $this->assertEquals('Saved search', $result['terms'][0]['query']);