6 namespace Friendica\Test;
10 use Friendica\Core\Config\IConfig;
11 use Friendica\Core\PConfig\IPConfig;
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;
17 use Friendica\Database\DBA;
19 use Friendica\Model\Contact;
20 use Friendica\Network\HTTPException;
21 use Friendica\Test\Util\Database\StaticDatabase;
22 use Monolog\Handler\TestHandler;
24 require_once __DIR__ . '/../../include/api.php';
27 * Tests for the API functions.
29 * Functions that use header() need to be tested in a separate process.
30 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
32 class ApiTest extends DatabaseTest
35 * @var TestHandler Can handle log-outputs
42 protected $friendUser;
46 protected $wrongUserId;
58 * Create variables used by tests.
60 protected function setUp()
64 $this->dice = (new Dice())
65 ->addRules(include __DIR__ . '/../../static/dependencies.config.php')
66 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
67 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
68 DI::init($this->dice);
70 /** @var Database $dba */
71 $dba = $this->dice->create(Database::class);
73 /** @var IConfig $config */
74 $this->config = $this->dice->create(IConfig::class);
76 $this->config->set('system', 'url', 'http://localhost');
77 $this->config->set('system', 'hostname', 'localhost');
78 $this->config->set('system', 'worker_dont_fork', true);
81 $this->config->set('config', 'hostname', 'localhost');
82 $this->config->set('system', 'throttle_limit_day', 100);
83 $this->config->set('system', 'throttle_limit_week', 100);
84 $this->config->set('system', 'throttle_limit_month', 100);
85 $this->config->set('system', 'theme', 'system_theme');
87 // Load the API dataset for the whole API
88 $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba);
91 $this->app = DI::app();
94 $this->app->argv = ['home'];
96 // User data that the test database is populated with
99 'name' => 'Self contact',
100 'nick' => 'selfcontact',
101 'nurl' => 'http://localhost/profile/selfcontact'
103 $this->friendUser = [
105 'name' => 'Friend contact',
106 'nick' => 'friendcontact',
107 'nurl' => 'http://localhost/profile/friendcontact'
111 'name' => 'othercontact',
112 'nick' => 'othercontact',
113 'nurl' => 'http://localhost/profile/othercontact'
116 // User ID that we know is not in the database
117 $this->wrongUserId = 666;
119 DI::session()->start();
121 // Most API require login so we force the session
124 'authenticated' => true,
125 'uid' => $this->selfUser['id']
134 * Assert that an user array contains expected keys.
136 * @param array $user User array
140 private function assertSelfUser(array $user)
142 $this->assertEquals($this->selfUser['id'], $user['uid']);
143 $this->assertEquals($this->selfUser['id'], $user['cid']);
144 $this->assertEquals(1, $user['self']);
145 $this->assertEquals('DFRN', $user['location']);
146 $this->assertEquals($this->selfUser['name'], $user['name']);
147 $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
148 $this->assertEquals('dfrn', $user['network']);
149 $this->assertTrue($user['verified']);
153 * Assert that an user array contains expected keys.
155 * @param array $user User array
159 private function assertOtherUser(array $user)
161 $this->assertEquals($this->otherUser['id'], $user['id']);
162 $this->assertEquals($this->otherUser['id'], $user['id_str']);
163 $this->assertEquals(0, $user['self']);
164 $this->assertEquals($this->otherUser['name'], $user['name']);
165 $this->assertEquals($this->otherUser['nick'], $user['screen_name']);
166 $this->assertFalse($user['verified']);
170 * Assert that a status array contains expected keys.
172 * @param array $status Status array
176 private function assertStatus(array $status)
178 $this->assertInternalType('string', $status['text']);
179 $this->assertInternalType('int', $status['id']);
180 // We could probably do more checks here.
184 * Assert that a list array contains expected keys.
186 * @param array $list List array
190 private function assertList(array $list)
192 $this->assertInternalType('string', $list['name']);
193 $this->assertInternalType('int', $list['id']);
194 $this->assertInternalType('string', $list['id_str']);
195 $this->assertContains($list['mode'], ['public', 'private']);
196 // We could probably do more checks here.
200 * Assert that the string is XML and contain the root element.
202 * @param string $result XML string
203 * @param string $root_element Root element name
207 private function assertXml($result, $root_element)
209 $this->assertStringStartsWith('<?xml version="1.0"?>', $result);
210 $this->assertContains('<' . $root_element, $result);
211 // We could probably do more checks here.
215 * Get the path to a temporary empty PNG image.
217 * @return string Path
219 private function getTempImage()
221 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
225 // Empty 1x1 px PNG image
226 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
234 * Test the api_user() function.
238 public function testApiUser()
240 $this->assertEquals($this->selfUser['id'], api_user());
244 * Test the api_user() function with an unallowed user.
248 public function testApiUserWithUnallowedUser()
250 $_SESSION = ['allow_api' => false];
251 $this->assertEquals(false, api_user());
255 * Test the api_source() function.
259 public function testApiSource()
261 $this->assertEquals('api', api_source());
265 * Test the api_source() function with a Twidere user agent.
269 public function testApiSourceWithTwidere()
271 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
272 $this->assertEquals('Twidere', api_source());
276 * Test the api_source() function with a GET parameter.
280 public function testApiSourceWithGet()
282 $_GET['source'] = 'source_name';
283 $this->assertEquals('source_name', api_source());
287 * Test the api_date() function.
291 public function testApiDate()
293 $this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
297 * Test the api_register_func() function.
301 public function testApiRegisterFunc()
313 $this->assertTrue($API['api_path']['auth']);
314 $this->assertEquals('method', $API['api_path']['method']);
315 $this->assertTrue(is_callable($API['api_path']['func']));
319 * Test the api_login() function without any login.
322 * @runInSeparateProcess
323 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
325 public function testApiLoginWithoutLogin()
327 api_login($this->app);
331 * Test the api_login() function with a bad login.
334 * @runInSeparateProcess
335 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
337 public function testApiLoginWithBadLogin()
339 $_SERVER['PHP_AUTH_USER'] = 'user@server';
340 api_login($this->app);
344 * Test the api_login() function with oAuth.
348 public function testApiLoginWithOauth()
350 $this->markTestIncomplete('Can we test this easily?');
354 * Test the api_login() function with authentication provided by an addon.
358 public function testApiLoginWithAddonAuth()
360 $this->markTestIncomplete('Can we test this easily?');
364 * Test the api_login() function with a correct login.
367 * @runInSeparateProcess
369 public function testApiLoginWithCorrectLogin()
371 $_SERVER['PHP_AUTH_USER'] = 'Test user';
372 $_SERVER['PHP_AUTH_PW'] = 'password';
373 api_login($this->app);
377 * Test the api_login() function with a remote user.
380 * @runInSeparateProcess
381 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
383 public function testApiLoginWithRemoteUser()
385 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
386 api_login($this->app);
390 * Test the api_check_method() function.
394 public function testApiCheckMethod()
396 $this->assertFalse(api_check_method('method'));
400 * Test the api_check_method() function with a correct method.
404 public function testApiCheckMethodWithCorrectMethod()
406 $_SERVER['REQUEST_METHOD'] = 'method';
407 $this->assertTrue(api_check_method('method'));
411 * Test the api_check_method() function with a wildcard.
415 public function testApiCheckMethodWithWildcard()
417 $this->assertTrue(api_check_method('*'));
421 * Test the api_call() function.
424 * @runInSeparateProcess
426 public function testApiCall()
430 'method' => 'method',
431 'func' => function () {
432 return ['data' => ['some_data']];
435 $_SERVER['REQUEST_METHOD'] = 'method';
436 $_SERVER['QUERY_STRING'] = 'q=api_path';
437 $_GET['callback'] = 'callback_name';
439 $args = DI::args()->determine($_SERVER, $_GET);
442 'callback_name(["some_data"])',
443 api_call($this->app, $args)
448 * Test the api_call() function with the profiled enabled.
451 * @runInSeparateProcess
453 public function testApiCallWithProfiler()
457 'method' => 'method',
458 'func' => function () {
459 return ['data' => ['some_data']];
463 $_SERVER['REQUEST_METHOD'] = 'method';
464 $_SERVER['QUERY_STRING'] = 'q=api_path';
466 $args = DI::args()->determine($_SERVER, $_GET);
468 $this->config->set('system', 'profiler', true);
469 $this->config->set('rendertime', 'callstack', true);
470 $this->app->callstack = [
471 'database' => ['some_function' => 200],
472 'database_write' => ['some_function' => 200],
473 'cache' => ['some_function' => 200],
474 'cache_write' => ['some_function' => 200],
475 'network' => ['some_function' => 200]
480 api_call($this->app, $args)
485 * Test the api_call() function without any result.
488 * @runInSeparateProcess
490 public function testApiCallWithNoResult()
494 'method' => 'method',
495 'func' => function () {
499 $_SERVER['REQUEST_METHOD'] = 'method';
500 $_SERVER['QUERY_STRING'] = 'q=api_path';
502 $args = DI::args()->determine($_SERVER, $_GET);
505 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
506 api_call($this->app, $args)
511 * Test the api_call() function with an unimplemented API.
514 * @runInSeparateProcess
516 public function testApiCallWithUninplementedApi()
519 '{"status":{"error":"Not Implemented","code":"501 Not Implemented","request":""}}',
525 * Test the api_call() function with a JSON result.
528 * @runInSeparateProcess
530 public function testApiCallWithJson()
534 'method' => 'method',
535 'func' => function () {
536 return ['data' => ['some_data']];
539 $_SERVER['REQUEST_METHOD'] = 'method';
540 $_SERVER['QUERY_STRING'] = 'q=api_path.json';
542 $args = DI::args()->determine($_SERVER, $_GET);
546 api_call($this->app, $args)
551 * Test the api_call() function with an XML result.
554 * @runInSeparateProcess
556 public function testApiCallWithXml()
560 'method' => 'method',
561 'func' => function () {
565 $_SERVER['REQUEST_METHOD'] = 'method';
566 $_SERVER['QUERY_STRING'] = 'q=api_path.xml';
568 $args = DI::args()->determine($_SERVER, $_GET);
572 api_call($this->app, $args)
577 * Test the api_call() function with an RSS result.
580 * @runInSeparateProcess
582 public function testApiCallWithRss()
586 'method' => 'method',
587 'func' => function () {
591 $_SERVER['REQUEST_METHOD'] = 'method';
592 $_SERVER['QUERY_STRING'] = 'q=api_path.rss';
594 $args = DI::args()->determine($_SERVER, $_GET);
597 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
599 api_call($this->app, $args)
604 * Test the api_call() function with an Atom result.
607 * @runInSeparateProcess
609 public function testApiCallWithAtom()
613 'method' => 'method',
614 'func' => function () {
618 $_SERVER['REQUEST_METHOD'] = 'method';
619 $_SERVER['QUERY_STRING'] = 'q=api_path.atom';
621 $args = DI::args()->determine($_SERVER, $_GET);
624 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
626 api_call($this->app, $args)
631 * Test the api_call() function with an unallowed method.
634 * @runInSeparateProcess
636 public function testApiCallWithWrongMethod()
639 $API['api_path'] = ['method' => 'method'];
641 $_SERVER['QUERY_STRING'] = 'q=api_path';
643 $args = DI::args()->determine($_SERVER, $_GET);
646 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
647 api_call($this->app, $args)
652 * Test the api_call() function with an unauthorized user.
655 * @runInSeparateProcess
657 public function testApiCallWithWrongAuth()
661 'method' => 'method',
664 $_SESSION['authenticated'] = false;
665 $_SERVER['REQUEST_METHOD'] = 'method';
666 $_SERVER['QUERY_STRING'] = 'q=api_path';
668 $args = DI::args()->determine($_SERVER, $_GET);
671 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
672 api_call($this->app, $args)
677 * Test the api_error() function with a JSON result.
680 * @runInSeparateProcess
682 public function testApiErrorWithJson()
685 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
686 api_error('json', new HTTPException\OKException('error_message'), DI::args())
691 * Test the api_error() function with an XML result.
694 * @runInSeparateProcess
696 public function testApiErrorWithXml()
699 '<?xml version="1.0"?>' . "\n" .
700 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
701 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
702 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
703 ' <error>error_message</error>' . "\n" .
704 ' <code>200 OK</code>' . "\n" .
705 ' <request/>' . "\n" .
707 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
712 * Test the api_error() function with an RSS result.
715 * @runInSeparateProcess
717 public function testApiErrorWithRss()
720 '<?xml version="1.0"?>' . "\n" .
721 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
722 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
723 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
724 ' <error>error_message</error>' . "\n" .
725 ' <code>200 OK</code>' . "\n" .
726 ' <request/>' . "\n" .
728 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
733 * Test the api_error() function with an Atom result.
736 * @runInSeparateProcess
738 public function testApiErrorWithAtom()
741 '<?xml version="1.0"?>' . "\n" .
742 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
743 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
744 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
745 ' <error>error_message</error>' . "\n" .
746 ' <code>200 OK</code>' . "\n" .
747 ' <request/>' . "\n" .
749 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
754 * Test the api_rss_extra() function.
758 public function testApiRssExtra()
760 $user_info = ['url' => 'user_url', 'lang' => 'en'];
761 $result = api_rss_extra($this->app, [], $user_info);
762 $this->assertEquals($user_info, $result['$user']);
763 $this->assertEquals($user_info['url'], $result['$rss']['alternate']);
764 $this->assertArrayHasKey('self', $result['$rss']);
765 $this->assertArrayHasKey('base', $result['$rss']);
766 $this->assertArrayHasKey('updated', $result['$rss']);
767 $this->assertArrayHasKey('atom_updated', $result['$rss']);
768 $this->assertArrayHasKey('language', $result['$rss']);
769 $this->assertArrayHasKey('logo', $result['$rss']);
773 * Test the api_rss_extra() function without any user info.
777 public function testApiRssExtraWithoutUserInfo()
779 $result = api_rss_extra($this->app, [], null);
780 $this->assertInternalType('array', $result['$user']);
781 $this->assertArrayHasKey('alternate', $result['$rss']);
782 $this->assertArrayHasKey('self', $result['$rss']);
783 $this->assertArrayHasKey('base', $result['$rss']);
784 $this->assertArrayHasKey('updated', $result['$rss']);
785 $this->assertArrayHasKey('atom_updated', $result['$rss']);
786 $this->assertArrayHasKey('language', $result['$rss']);
787 $this->assertArrayHasKey('logo', $result['$rss']);
791 * Test the api_unique_id_to_nurl() function.
795 public function testApiUniqueIdToNurl()
797 $this->assertFalse(api_unique_id_to_nurl($this->wrongUserId));
801 * Test the api_unique_id_to_nurl() function with a correct ID.
805 public function testApiUniqueIdToNurlWithCorrectId()
807 $this->assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
811 * Test the api_get_user() function.
815 public function testApiGetUser()
817 $user = api_get_user($this->app);
818 $this->assertSelfUser($user);
819 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
820 $this->assertEquals('6fdbe8', $user['profile_link_color']);
821 $this->assertEquals('ededed', $user['profile_background_color']);
825 * Test the api_get_user() function with a Frio schema.
829 public function testApiGetUserWithFrioSchema()
831 $pConfig = $this->dice->create(IPConfig::class);
832 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
833 $user = api_get_user($this->app);
834 $this->assertSelfUser($user);
835 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
836 $this->assertEquals('6fdbe8', $user['profile_link_color']);
837 $this->assertEquals('ededed', $user['profile_background_color']);
841 * Test the api_get_user() function with a custom Frio schema.
845 public function testApiGetUserWithCustomFrioSchema()
847 $pConfig = $this->dice->create(IPConfig::class);
848 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
849 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
850 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
851 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
852 $user = api_get_user($this->app);
853 $this->assertSelfUser($user);
854 $this->assertEquals('123456', $user['profile_sidebar_fill_color']);
855 $this->assertEquals('123456', $user['profile_link_color']);
856 $this->assertEquals('123456', $user['profile_background_color']);
860 * Test the api_get_user() function with an empty Frio schema.
864 public function testApiGetUserWithEmptyFrioSchema()
866 $pConfig = $this->dice->create(IPConfig::class);
867 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
868 $user = api_get_user($this->app);
869 $this->assertSelfUser($user);
870 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
871 $this->assertEquals('6fdbe8', $user['profile_link_color']);
872 $this->assertEquals('ededed', $user['profile_background_color']);
876 * Test the api_get_user() function with an user that is not allowed to use the API.
879 * @runInSeparateProcess
881 public function testApiGetUserWithoutApiUser()
883 $_SERVER['PHP_AUTH_USER'] = 'Test user';
884 $_SERVER['PHP_AUTH_PW'] = 'password';
885 $_SESSION['allow_api'] = false;
886 $this->assertFalse(api_get_user($this->app));
890 * Test the api_get_user() function with an user ID in a GET parameter.
894 public function testApiGetUserWithGetId()
896 $_GET['user_id'] = $this->otherUser['id'];
897 $this->assertOtherUser(api_get_user($this->app));
901 * Test the api_get_user() function with a wrong user ID in a GET parameter.
904 * @expectedException Friendica\Network\HTTPException\BadRequestException
906 public function testApiGetUserWithWrongGetId()
908 $_GET['user_id'] = $this->wrongUserId;
909 $this->assertOtherUser(api_get_user($this->app));
913 * Test the api_get_user() function with an user name in a GET parameter.
917 public function testApiGetUserWithGetName()
919 $_GET['screen_name'] = $this->selfUser['nick'];
920 $this->assertSelfUser(api_get_user($this->app));
924 * Test the api_get_user() function with a profile URL in a GET parameter.
928 public function testApiGetUserWithGetUrl()
930 $_GET['profileurl'] = $this->selfUser['nurl'];
931 $this->assertSelfUser(api_get_user($this->app));
935 * Test the api_get_user() function with an user ID in the API path.
939 public function testApiGetUserWithNumericCalledApi()
942 $called_api = ['api_path'];
943 $this->app->argv[1] = $this->otherUser['id'] . '.json';
944 $this->assertOtherUser(api_get_user($this->app));
948 * Test the api_get_user() function with the $called_api global variable.
952 public function testApiGetUserWithCalledApi()
955 $called_api = ['api', 'api_path'];
956 $this->assertSelfUser(api_get_user($this->app));
960 * Test the api_get_user() function with a valid user.
964 public function testApiGetUserWithCorrectUser()
966 $this->assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
970 * Test the api_get_user() function with a wrong user ID.
973 * @expectedException Friendica\Network\HTTPException\BadRequestException
975 public function testApiGetUserWithWrongUser()
977 $this->assertOtherUser(api_get_user($this->app, $this->wrongUserId));
981 * Test the api_get_user() function with a 0 user ID.
985 public function testApiGetUserWithZeroUser()
987 $this->assertSelfUser(api_get_user($this->app, 0));
991 * Test the api_item_get_user() function.
995 public function testApiItemGetUser()
997 $users = api_item_get_user($this->app, []);
998 $this->assertSelfUser($users[0]);
1002 * Test the api_item_get_user() function with a different item parent.
1006 public function testApiItemGetUserWithDifferentParent()
1008 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1009 $this->assertSelfUser($users[0]);
1010 $this->assertEquals($users[0], $users[1]);
1014 * Test the api_walk_recursive() function.
1018 public function testApiWalkRecursive()
1021 $this->assertEquals(
1026 // Should we test this with a callback that actually does something?
1034 * Test the api_walk_recursive() function with an array.
1038 public function testApiWalkRecursiveWithArray()
1040 $array = [['item1'], ['item2']];
1041 $this->assertEquals(
1046 // Should we test this with a callback that actually does something?
1054 * Test the api_reformat_xml() function.
1058 public function testApiReformatXml()
1062 $this->assertTrue(api_reformat_xml($item, $key));
1063 $this->assertEquals('true', $item);
1067 * Test the api_reformat_xml() function with a statusnet_api key.
1071 public function testApiReformatXmlWithStatusnetKey()
1074 $key = 'statusnet_api';
1075 $this->assertTrue(api_reformat_xml($item, $key));
1076 $this->assertEquals('statusnet:api', $key);
1080 * Test the api_reformat_xml() function with a friendica_api key.
1084 public function testApiReformatXmlWithFriendicaKey()
1087 $key = 'friendica_api';
1088 $this->assertTrue(api_reformat_xml($item, $key));
1089 $this->assertEquals('friendica:api', $key);
1093 * Test the api_create_xml() function.
1097 public function testApiCreateXml()
1099 $this->assertEquals(
1100 '<?xml version="1.0"?>' . "\n" .
1101 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1102 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1103 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1104 ' <data>some_data</data>' . "\n" .
1105 '</root_element>' . "\n",
1106 api_create_xml(['data' => ['some_data']], 'root_element')
1111 * Test the api_create_xml() function without any XML namespace.
1115 public function testApiCreateXmlWithoutNamespaces()
1117 $this->assertEquals(
1118 '<?xml version="1.0"?>' . "\n" .
1120 ' <data>some_data</data>' . "\n" .
1122 api_create_xml(['data' => ['some_data']], 'ok')
1127 * Test the api_format_data() function.
1131 public function testApiFormatData()
1133 $data = ['some_data'];
1134 $this->assertEquals($data, api_format_data('root_element', 'json', $data));
1138 * Test the api_format_data() function with an XML result.
1142 public function testApiFormatDataWithXml()
1144 $this->assertEquals(
1145 '<?xml version="1.0"?>' . "\n" .
1146 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1147 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1148 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1149 ' <data>some_data</data>' . "\n" .
1150 '</root_element>' . "\n",
1151 api_format_data('root_element', 'xml', ['data' => ['some_data']])
1156 * Test the api_account_verify_credentials() function.
1160 public function testApiAccountVerifyCredentials()
1162 $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
1166 * Test the api_account_verify_credentials() function without an authenticated user.
1169 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1171 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1173 $_SESSION['authenticated'] = false;
1174 api_account_verify_credentials('json');
1178 * Test the requestdata() function.
1182 public function testRequestdata()
1184 $this->assertNull(requestdata('variable_name'));
1188 * Test the requestdata() function with a POST parameter.
1192 public function testRequestdataWithPost()
1194 $_POST['variable_name'] = 'variable_value';
1195 $this->assertEquals('variable_value', requestdata('variable_name'));
1199 * Test the requestdata() function with a GET parameter.
1203 public function testRequestdataWithGet()
1205 $_GET['variable_name'] = 'variable_value';
1206 $this->assertEquals('variable_value', requestdata('variable_name'));
1210 * Test the api_statuses_mediap() function.
1214 public function testApiStatusesMediap()
1216 $this->app->argc = 2;
1224 'tmp_name' => $this->getTempImage(),
1225 'name' => 'spacer.png',
1226 'type' => 'image/png'
1229 $_GET['status'] = '<b>Status content</b>';
1231 $result = api_statuses_mediap('json');
1232 $this->assertStatus($result['status']);
1236 * Test the api_statuses_mediap() function without an authenticated user.
1239 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1241 public function testApiStatusesMediapWithoutAuthenticatedUser()
1243 $_SESSION['authenticated'] = false;
1244 api_statuses_mediap('json');
1248 * Test the api_statuses_update() function.
1252 public function testApiStatusesUpdate()
1254 $_GET['status'] = 'Status content #friendica';
1255 $_GET['in_reply_to_status_id'] = -1;
1264 'tmp_name' => $this->getTempImage(),
1265 'name' => 'spacer.png',
1266 'type' => 'image/png'
1270 $result = api_statuses_update('json');
1271 $this->assertStatus($result['status']);
1275 * Test the api_statuses_update() function with an HTML status.
1279 public function testApiStatusesUpdateWithHtml()
1281 $_GET['htmlstatus'] = '<b>Status content</b>';
1283 $result = api_statuses_update('json');
1284 $this->assertStatus($result['status']);
1288 * Test the api_statuses_update() function without an authenticated user.
1291 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1293 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1295 $_SESSION['authenticated'] = false;
1296 api_statuses_update('json');
1300 * Test the api_statuses_update() function with a parent status.
1304 public function testApiStatusesUpdateWithParent()
1306 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1310 * Test the api_statuses_update() function with a media_ids parameter.
1314 public function testApiStatusesUpdateWithMediaIds()
1316 $this->markTestIncomplete();
1320 * Test the api_statuses_update() function with the throttle limit reached.
1324 public function testApiStatusesUpdateWithDayThrottleReached()
1326 $this->markTestIncomplete();
1330 * Test the api_media_upload() function.
1333 * @expectedException Friendica\Network\HTTPException\BadRequestException
1335 public function testApiMediaUpload()
1341 * Test the api_media_upload() function without an authenticated user.
1344 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1346 public function testApiMediaUploadWithoutAuthenticatedUser()
1348 $_SESSION['authenticated'] = false;
1353 * Test the api_media_upload() function with an invalid uploaded media.
1356 * @expectedException Friendica\Network\HTTPException\InternalServerErrorException
1358 public function testApiMediaUploadWithMedia()
1363 'tmp_name' => 'tmp_name'
1370 * Test the api_media_upload() function with an valid uploaded media.
1374 public function testApiMediaUploadWithValidMedia()
1382 'tmp_name' => $this->getTempImage(),
1383 'name' => 'spacer.png',
1384 'type' => 'image/png'
1390 $result = api_media_upload();
1391 $this->assertEquals('image/png', $result['media']['image']['image_type']);
1392 $this->assertEquals(1, $result['media']['image']['w']);
1393 $this->assertEquals(1, $result['media']['image']['h']);
1394 $this->assertNotEmpty($result['media']['image']['friendica_preview_url']);
1398 * Test the api_status_show() function.
1400 public function testApiStatusShowWithJson()
1402 $result = api_status_show('json', 1);
1403 $this->assertStatus($result['status']);
1407 * Test the api_status_show() function with an XML result.
1409 public function testApiStatusShowWithXml()
1411 $result = api_status_show('xml', 1);
1412 $this->assertXml($result, 'statuses');
1416 * Test the api_get_last_status() function
1418 public function testApiGetLastStatus()
1420 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1422 $this->assertNotNull($item);
1426 * Test the api_users_show() function.
1430 public function testApiUsersShow()
1432 $result = api_users_show('json');
1433 // We can't use assertSelfUser() here because the user object is missing some properties.
1434 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
1435 $this->assertEquals('DFRN', $result['user']['location']);
1436 $this->assertEquals($this->selfUser['name'], $result['user']['name']);
1437 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1438 $this->assertEquals('dfrn', $result['user']['network']);
1439 $this->assertTrue($result['user']['verified']);
1443 * Test the api_users_show() function with an XML result.
1447 public function testApiUsersShowWithXml()
1449 $result = api_users_show('xml');
1450 $this->assertXml($result, 'statuses');
1454 * Test the api_users_search() function.
1458 public function testApiUsersSearch()
1460 $_GET['q'] = 'othercontact';
1461 $result = api_users_search('json');
1462 $this->assertOtherUser($result['users'][0]);
1466 * Test the api_users_search() function with an XML result.
1470 public function testApiUsersSearchWithXml()
1472 $_GET['q'] = 'othercontact';
1473 $result = api_users_search('xml');
1474 $this->assertXml($result, 'users');
1478 * Test the api_users_search() function without a GET q parameter.
1481 * @expectedException Friendica\Network\HTTPException\BadRequestException
1483 public function testApiUsersSearchWithoutQuery()
1485 api_users_search('json');
1489 * Test the api_users_lookup() function.
1492 * @expectedException Friendica\Network\HTTPException\NotFoundException
1494 public function testApiUsersLookup()
1496 api_users_lookup('json');
1500 * Test the api_users_lookup() function with an user ID.
1504 public function testApiUsersLookupWithUserId()
1506 $_REQUEST['user_id'] = $this->otherUser['id'];
1507 $result = api_users_lookup('json');
1508 $this->assertOtherUser($result['users'][0]);
1512 * Test the api_search() function.
1516 public function testApiSearch()
1518 $_REQUEST['q'] = 'reply';
1519 $_REQUEST['max_id'] = 10;
1520 $result = api_search('json');
1521 foreach ($result['status'] as $status) {
1522 $this->assertStatus($status);
1523 $this->assertContains('reply', $status['text'], null, true);
1528 * Test the api_search() function a count parameter.
1532 public function testApiSearchWithCount()
1534 $_REQUEST['q'] = 'reply';
1535 $_REQUEST['count'] = 20;
1536 $result = api_search('json');
1537 foreach ($result['status'] as $status) {
1538 $this->assertStatus($status);
1539 $this->assertContains('reply', $status['text'], null, true);
1544 * Test the api_search() function with an rpp parameter.
1548 public function testApiSearchWithRpp()
1550 $_REQUEST['q'] = 'reply';
1551 $_REQUEST['rpp'] = 20;
1552 $result = api_search('json');
1553 foreach ($result['status'] as $status) {
1554 $this->assertStatus($status);
1555 $this->assertContains('reply', $status['text'], null, true);
1560 * Test the api_search() function with an q parameter contains hashtag.
1564 public function testApiSearchWithHashtag()
1566 $_REQUEST['q'] = '%23friendica';
1567 $result = api_search('json');
1568 foreach ($result['status'] as $status) {
1569 $this->assertStatus($status);
1570 $this->assertContains('#friendica', $status['text'], null, true);
1575 * Test the api_search() function with an exclude_replies parameter.
1579 public function testApiSearchWithExcludeReplies()
1581 $_REQUEST['max_id'] = 10;
1582 $_REQUEST['exclude_replies'] = true;
1583 $_REQUEST['q'] = 'friendica';
1584 $result = api_search('json');
1585 foreach ($result['status'] as $status) {
1586 $this->assertStatus($status);
1591 * Test the api_search() function without an authenticated user.
1594 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1596 public function testApiSearchWithUnallowedUser()
1598 $_SESSION['allow_api'] = false;
1599 $_GET['screen_name'] = $this->selfUser['nick'];
1604 * Test the api_search() function without any GET query parameter.
1607 * @expectedException Friendica\Network\HTTPException\BadRequestException
1609 public function testApiSearchWithoutQuery()
1615 * Test the api_statuses_home_timeline() function.
1619 public function testApiStatusesHomeTimeline()
1621 $_REQUEST['max_id'] = 10;
1622 $_REQUEST['exclude_replies'] = true;
1623 $_REQUEST['conversation_id'] = 1;
1624 $result = api_statuses_home_timeline('json');
1625 $this->assertNotEmpty($result['status']);
1626 foreach ($result['status'] as $status) {
1627 $this->assertStatus($status);
1632 * Test the api_statuses_home_timeline() function with a negative page parameter.
1636 public function testApiStatusesHomeTimelineWithNegativePage()
1638 $_REQUEST['page'] = -2;
1639 $result = api_statuses_home_timeline('json');
1640 $this->assertNotEmpty($result['status']);
1641 foreach ($result['status'] as $status) {
1642 $this->assertStatus($status);
1647 * Test the api_statuses_home_timeline() with an unallowed user.
1650 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1652 public function testApiStatusesHomeTimelineWithUnallowedUser()
1654 $_SESSION['allow_api'] = false;
1655 $_GET['screen_name'] = $this->selfUser['nick'];
1656 api_statuses_home_timeline('json');
1660 * Test the api_statuses_home_timeline() function with an RSS result.
1664 public function testApiStatusesHomeTimelineWithRss()
1666 $result = api_statuses_home_timeline('rss');
1667 $this->assertXml($result, 'statuses');
1671 * Test the api_statuses_public_timeline() function.
1675 public function testApiStatusesPublicTimeline()
1677 $_REQUEST['max_id'] = 10;
1678 $_REQUEST['conversation_id'] = 1;
1679 $result = api_statuses_public_timeline('json');
1680 $this->assertNotEmpty($result['status']);
1681 foreach ($result['status'] as $status) {
1682 $this->assertStatus($status);
1687 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1691 public function testApiStatusesPublicTimelineWithExcludeReplies()
1693 $_REQUEST['max_id'] = 10;
1694 $_REQUEST['exclude_replies'] = true;
1695 $result = api_statuses_public_timeline('json');
1696 $this->assertNotEmpty($result['status']);
1697 foreach ($result['status'] as $status) {
1698 $this->assertStatus($status);
1703 * Test the api_statuses_public_timeline() function with a negative page parameter.
1707 public function testApiStatusesPublicTimelineWithNegativePage()
1709 $_REQUEST['page'] = -2;
1710 $result = api_statuses_public_timeline('json');
1711 $this->assertNotEmpty($result['status']);
1712 foreach ($result['status'] as $status) {
1713 $this->assertStatus($status);
1718 * Test the api_statuses_public_timeline() function with an unallowed user.
1721 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1723 public function testApiStatusesPublicTimelineWithUnallowedUser()
1725 $_SESSION['allow_api'] = false;
1726 $_GET['screen_name'] = $this->selfUser['nick'];
1727 api_statuses_public_timeline('json');
1731 * Test the api_statuses_public_timeline() function with an RSS result.
1735 public function testApiStatusesPublicTimelineWithRss()
1737 $result = api_statuses_public_timeline('rss');
1738 $this->assertXml($result, 'statuses');
1742 * Test the api_statuses_networkpublic_timeline() function.
1746 public function testApiStatusesNetworkpublicTimeline()
1748 $_REQUEST['max_id'] = 10;
1749 $result = api_statuses_networkpublic_timeline('json');
1750 $this->assertNotEmpty($result['status']);
1751 foreach ($result['status'] as $status) {
1752 $this->assertStatus($status);
1757 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1761 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1763 $_REQUEST['page'] = -2;
1764 $result = api_statuses_networkpublic_timeline('json');
1765 $this->assertNotEmpty($result['status']);
1766 foreach ($result['status'] as $status) {
1767 $this->assertStatus($status);
1772 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1775 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1777 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1779 $_SESSION['allow_api'] = false;
1780 $_GET['screen_name'] = $this->selfUser['nick'];
1781 api_statuses_networkpublic_timeline('json');
1785 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1789 public function testApiStatusesNetworkpublicTimelineWithRss()
1791 $result = api_statuses_networkpublic_timeline('rss');
1792 $this->assertXml($result, 'statuses');
1796 * Test the api_statuses_show() function.
1799 * @expectedException Friendica\Network\HTTPException\BadRequestException
1801 public function testApiStatusesShow()
1803 api_statuses_show('json');
1807 * Test the api_statuses_show() function with an ID.
1811 public function testApiStatusesShowWithId()
1813 $this->app->argv[3] = 1;
1814 $result = api_statuses_show('json');
1815 $this->assertStatus($result['status']);
1819 * Test the api_statuses_show() function with the conversation parameter.
1823 public function testApiStatusesShowWithConversation()
1825 $this->app->argv[3] = 1;
1826 $_REQUEST['conversation'] = 1;
1827 $result = api_statuses_show('json');
1828 $this->assertNotEmpty($result['status']);
1829 foreach ($result['status'] as $status) {
1830 $this->assertStatus($status);
1835 * Test the api_statuses_show() function with an unallowed user.
1838 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1840 public function testApiStatusesShowWithUnallowedUser()
1842 $_SESSION['allow_api'] = false;
1843 $_GET['screen_name'] = $this->selfUser['nick'];
1844 api_statuses_show('json');
1848 * Test the api_conversation_show() function.
1851 * @expectedException Friendica\Network\HTTPException\BadRequestException
1853 public function testApiConversationShow()
1855 api_conversation_show('json');
1859 * Test the api_conversation_show() function with an ID.
1863 public function testApiConversationShowWithId()
1865 $this->app->argv[3] = 1;
1866 $_REQUEST['max_id'] = 10;
1867 $_REQUEST['page'] = -2;
1868 $result = api_conversation_show('json');
1869 $this->assertNotEmpty($result['status']);
1870 foreach ($result['status'] as $status) {
1871 $this->assertStatus($status);
1876 * Test the api_conversation_show() function with an unallowed user.
1879 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1881 public function testApiConversationShowWithUnallowedUser()
1883 $_SESSION['allow_api'] = false;
1884 $_GET['screen_name'] = $this->selfUser['nick'];
1885 api_conversation_show('json');
1889 * Test the api_statuses_repeat() function.
1892 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1894 public function testApiStatusesRepeat()
1896 api_statuses_repeat('json');
1900 * Test the api_statuses_repeat() function without an authenticated user.
1903 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1905 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1907 $_SESSION['authenticated'] = false;
1908 api_statuses_repeat('json');
1912 * Test the api_statuses_repeat() function with an ID.
1916 public function testApiStatusesRepeatWithId()
1918 $this->app->argv[3] = 1;
1919 $result = api_statuses_repeat('json');
1920 $this->assertStatus($result['status']);
1922 // Also test with a shared status
1923 $this->app->argv[3] = 5;
1924 $result = api_statuses_repeat('json');
1925 $this->assertStatus($result['status']);
1929 * Test the api_statuses_destroy() function.
1932 * @expectedException Friendica\Network\HTTPException\BadRequestException
1934 public function testApiStatusesDestroy()
1936 api_statuses_destroy('json');
1940 * Test the api_statuses_destroy() function without an authenticated user.
1943 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1945 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1947 $_SESSION['authenticated'] = false;
1948 api_statuses_destroy('json');
1952 * Test the api_statuses_destroy() function with an ID.
1956 public function testApiStatusesDestroyWithId()
1958 $this->app->argv[3] = 1;
1959 $result = api_statuses_destroy('json');
1960 $this->assertStatus($result['status']);
1964 * Test the api_statuses_mentions() function.
1968 public function testApiStatusesMentions()
1970 $this->app->user = ['nickname' => $this->selfUser['nick']];
1971 $_REQUEST['max_id'] = 10;
1972 $result = api_statuses_mentions('json');
1973 $this->assertEmpty($result['status']);
1974 // We should test with mentions in the database.
1978 * Test the api_statuses_mentions() function with a negative page parameter.
1982 public function testApiStatusesMentionsWithNegativePage()
1984 $_REQUEST['page'] = -2;
1985 $result = api_statuses_mentions('json');
1986 $this->assertEmpty($result['status']);
1990 * Test the api_statuses_mentions() function with an unallowed user.
1993 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1995 public function testApiStatusesMentionsWithUnallowedUser()
1997 $_SESSION['allow_api'] = false;
1998 $_GET['screen_name'] = $this->selfUser['nick'];
1999 api_statuses_mentions('json');
2003 * Test the api_statuses_mentions() function with an RSS result.
2007 public function testApiStatusesMentionsWithRss()
2009 $result = api_statuses_mentions('rss');
2010 $this->assertXml($result, 'statuses');
2014 * Test the api_statuses_user_timeline() function.
2018 public function testApiStatusesUserTimeline()
2020 $_REQUEST['max_id'] = 10;
2021 $_REQUEST['exclude_replies'] = true;
2022 $_REQUEST['conversation_id'] = 1;
2023 $result = api_statuses_user_timeline('json');
2024 $this->assertNotEmpty($result['status']);
2025 foreach ($result['status'] as $status) {
2026 $this->assertStatus($status);
2031 * Test the api_statuses_user_timeline() function with a negative page parameter.
2035 public function testApiStatusesUserTimelineWithNegativePage()
2037 $_REQUEST['page'] = -2;
2038 $result = api_statuses_user_timeline('json');
2039 $this->assertNotEmpty($result['status']);
2040 foreach ($result['status'] as $status) {
2041 $this->assertStatus($status);
2046 * Test the api_statuses_user_timeline() function with an RSS result.
2050 public function testApiStatusesUserTimelineWithRss()
2052 $result = api_statuses_user_timeline('rss');
2053 $this->assertXml($result, 'statuses');
2057 * Test the api_statuses_user_timeline() function with an unallowed user.
2060 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2062 public function testApiStatusesUserTimelineWithUnallowedUser()
2064 $_SESSION['allow_api'] = false;
2065 $_GET['screen_name'] = $this->selfUser['nick'];
2066 api_statuses_user_timeline('json');
2070 * Test the api_favorites_create_destroy() function.
2073 * @expectedException Friendica\Network\HTTPException\BadRequestException
2075 public function testApiFavoritesCreateDestroy()
2077 $this->app->argv = ['api', '1.1', 'favorites', 'create'];
2078 $this->app->argc = count($this->app->argv);
2079 api_favorites_create_destroy('json');
2083 * Test the api_favorites_create_destroy() function with an invalid ID.
2086 * @expectedException Friendica\Network\HTTPException\BadRequestException
2088 public function testApiFavoritesCreateDestroyWithInvalidId()
2090 $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json'];
2091 $this->app->argc = count($this->app->argv);
2092 api_favorites_create_destroy('json');
2096 * Test the api_favorites_create_destroy() function with an invalid action.
2099 * @expectedException Friendica\Network\HTTPException\BadRequestException
2101 public function testApiFavoritesCreateDestroyWithInvalidAction()
2103 $this->app->argv = ['api', '1.1', 'favorites', 'change.json'];
2104 $this->app->argc = count($this->app->argv);
2105 $_REQUEST['id'] = 1;
2106 api_favorites_create_destroy('json');
2110 * Test the api_favorites_create_destroy() function with the create action.
2114 public function testApiFavoritesCreateDestroyWithCreateAction()
2116 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2117 $this->app->argc = count($this->app->argv);
2118 $_REQUEST['id'] = 3;
2119 $result = api_favorites_create_destroy('json');
2120 $this->assertStatus($result['status']);
2124 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2128 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2130 $this->app->argv = ['api', '1.1', 'favorites', 'create.rss'];
2131 $this->app->argc = count($this->app->argv);
2132 $_REQUEST['id'] = 3;
2133 $result = api_favorites_create_destroy('rss');
2134 $this->assertXml($result, 'status');
2138 * Test the api_favorites_create_destroy() function with the destroy action.
2142 public function testApiFavoritesCreateDestroyWithDestroyAction()
2144 $this->app->argv = ['api', '1.1', 'favorites', 'destroy.json'];
2145 $this->app->argc = count($this->app->argv);
2146 $_REQUEST['id'] = 3;
2147 $result = api_favorites_create_destroy('json');
2148 $this->assertStatus($result['status']);
2152 * Test the api_favorites_create_destroy() function without an authenticated user.
2155 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2157 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2159 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2160 $this->app->argc = count($this->app->argv);
2161 $_SESSION['authenticated'] = false;
2162 api_favorites_create_destroy('json');
2166 * Test the api_favorites() function.
2170 public function testApiFavorites()
2172 $_REQUEST['page'] = -1;
2173 $_REQUEST['max_id'] = 10;
2174 $result = api_favorites('json');
2175 foreach ($result['status'] as $status) {
2176 $this->assertStatus($status);
2181 * Test the api_favorites() function with an RSS result.
2185 public function testApiFavoritesWithRss()
2187 $result = api_favorites('rss');
2188 $this->assertXml($result, 'statuses');
2192 * Test the api_favorites() function with an unallowed user.
2195 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2197 public function testApiFavoritesWithUnallowedUser()
2199 $_SESSION['allow_api'] = false;
2200 $_GET['screen_name'] = $this->selfUser['nick'];
2201 api_favorites('json');
2205 * Test the api_format_messages() function.
2209 public function testApiFormatMessages()
2211 $result = api_format_messages(
2212 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2213 ['id' => 2, 'screen_name' => 'recipient_name'],
2214 ['id' => 3, 'screen_name' => 'sender_name']
2216 $this->assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2217 $this->assertEquals(1, $result['id']);
2218 $this->assertEquals(2, $result['recipient_id']);
2219 $this->assertEquals(3, $result['sender_id']);
2220 $this->assertEquals('recipient_name', $result['recipient_screen_name']);
2221 $this->assertEquals('sender_name', $result['sender_screen_name']);
2225 * Test the api_format_messages() function with HTML.
2229 public function testApiFormatMessagesWithHtmlText()
2231 $_GET['getText'] = 'html';
2232 $result = api_format_messages(
2233 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2234 ['id' => 2, 'screen_name' => 'recipient_name'],
2235 ['id' => 3, 'screen_name' => 'sender_name']
2237 $this->assertEquals('item_title', $result['title']);
2238 $this->assertEquals('<strong>item_body</strong>', $result['text']);
2242 * Test the api_format_messages() function with plain text.
2246 public function testApiFormatMessagesWithPlainText()
2248 $_GET['getText'] = 'plain';
2249 $result = api_format_messages(
2250 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2251 ['id' => 2, 'screen_name' => 'recipient_name'],
2252 ['id' => 3, 'screen_name' => 'sender_name']
2254 $this->assertEquals('item_title', $result['title']);
2255 $this->assertEquals('item_body', $result['text']);
2259 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2263 public function testApiFormatMessagesWithoutUserObjects()
2265 $_GET['getUserObjects'] = 'false';
2266 $result = api_format_messages(
2267 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2268 ['id' => 2, 'screen_name' => 'recipient_name'],
2269 ['id' => 3, 'screen_name' => 'sender_name']
2271 $this->assertTrue(!isset($result['sender']));
2272 $this->assertTrue(!isset($result['recipient']));
2276 * Test the api_convert_item() function.
2280 public function testApiConvertItem()
2282 $result = api_convert_item(
2284 'network' => 'feed',
2285 'title' => 'item_title',
2286 // We need a long string to test that it is correctly cut
2287 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2288 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2289 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2290 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2291 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2292 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2293 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2294 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2295 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2296 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2297 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2298 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2299 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2300 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2301 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2302 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2303 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2304 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2305 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2306 'repellat officia illum quos impedit quam iste esse unde qui ' .
2307 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2308 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2309 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2310 'plink' => 'item_plink'
2313 $this->assertStringStartsWith('item_title', $result['text']);
2314 $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2318 * Test the api_convert_item() function with an empty item body.
2322 public function testApiConvertItemWithoutBody()
2324 $result = api_convert_item(
2326 'network' => 'feed',
2327 'title' => 'item_title',
2329 'plink' => 'item_plink'
2332 $this->assertEquals('item_title', $result['text']);
2333 $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2337 * Test the api_convert_item() function with the title in the body.
2341 public function testApiConvertItemWithTitleInBody()
2343 $result = api_convert_item(
2345 'title' => 'item_title',
2346 'body' => 'item_title item_body'
2349 $this->assertEquals('item_title item_body', $result['text']);
2350 $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2354 * Test the api_get_attachments() function.
2358 public function testApiGetAttachments()
2361 $this->assertEmpty(api_get_attachments($body));
2365 * Test the api_get_attachments() function with an img tag.
2369 public function testApiGetAttachmentsWithImage()
2371 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2372 $this->assertInternalType('array', api_get_attachments($body));
2376 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2380 public function testApiGetAttachmentsWithImageAndAndStatus()
2382 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2383 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2384 $this->assertInternalType('array', api_get_attachments($body));
2388 * Test the api_get_entitities() function.
2392 public function testApiGetEntitities()
2395 $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
2399 * Test the api_get_entitities() function with the include_entities parameter.
2403 public function testApiGetEntititiesWithIncludeEntities()
2405 $_REQUEST['include_entities'] = 'true';
2407 $result = api_get_entitities($text, 'bbcode');
2408 $this->assertInternalType('array', $result['hashtags']);
2409 $this->assertInternalType('array', $result['symbols']);
2410 $this->assertInternalType('array', $result['urls']);
2411 $this->assertInternalType('array', $result['user_mentions']);
2415 * Test the api_format_items_embeded_images() function.
2419 public function testApiFormatItemsEmbededImages()
2421 $this->assertEquals(
2422 'text ' . DI::baseUrl() . '/display/item_guid',
2423 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2428 * Test the api_contactlink_to_array() function.
2432 public function testApiContactlinkToArray()
2434 $this->assertEquals(
2439 api_contactlink_to_array('text')
2444 * Test the api_contactlink_to_array() function with an URL.
2448 public function testApiContactlinkToArrayWithUrl()
2450 $this->assertEquals(
2452 'name' => ['link_text'],
2455 api_contactlink_to_array('text <a href="url">link_text</a>')
2460 * Test the api_format_items_activities() function.
2464 public function testApiFormatItemsActivities()
2466 $item = ['uid' => 0, 'uri' => ''];
2467 $result = api_format_items_activities($item);
2468 $this->assertArrayHasKey('like', $result);
2469 $this->assertArrayHasKey('dislike', $result);
2470 $this->assertArrayHasKey('attendyes', $result);
2471 $this->assertArrayHasKey('attendno', $result);
2472 $this->assertArrayHasKey('attendmaybe', $result);
2476 * Test the api_format_items_activities() function with an XML result.
2480 public function testApiFormatItemsActivitiesWithXml()
2482 $item = ['uid' => 0, 'uri' => ''];
2483 $result = api_format_items_activities($item, 'xml');
2484 $this->assertArrayHasKey('friendica:like', $result);
2485 $this->assertArrayHasKey('friendica:dislike', $result);
2486 $this->assertArrayHasKey('friendica:attendyes', $result);
2487 $this->assertArrayHasKey('friendica:attendno', $result);
2488 $this->assertArrayHasKey('friendica:attendmaybe', $result);
2492 * Test the api_format_items_profiles() function.
2496 public function testApiFormatItemsProfiles()
2499 'id' => 'profile_id',
2500 'profile-name' => 'profile_name',
2501 'is-default' => true,
2502 'hide-friends' => true,
2503 'photo' => 'profile_photo',
2504 'thumb' => 'profile_thumb',
2506 'net-publish' => true,
2507 'pdesc' => 'description',
2508 'dob' => 'date_of_birth',
2509 'address' => 'address',
2510 'locality' => 'city',
2511 'region' => 'region',
2512 'postal-code' => 'postal_code',
2513 'country-name' => 'country',
2514 'hometown' => 'hometown',
2515 'gender' => 'gender',
2516 'marital' => 'marital',
2517 'with' => 'marital_with',
2518 'howlong' => 'marital_since',
2519 'sexual' => 'sexual',
2520 'politic' => 'politic',
2521 'religion' => 'religion',
2522 'pub_keywords' => 'public_keywords',
2523 'prv_keywords' => 'private_keywords',
2526 'dislikes' => 'dislikes',
2532 'interest' => 'interest',
2533 'romance' => 'romance',
2535 'education' => 'education',
2536 'contact' => 'social_networks',
2537 'homepage' => 'homepage'
2539 $result = api_format_items_profiles($profile_row);
2540 $this->assertEquals(
2542 'profile_id' => 'profile_id',
2543 'profile_name' => 'profile_name',
2544 'is_default' => true,
2545 'hide_friends' => true,
2546 'profile_photo' => 'profile_photo',
2547 'profile_thumb' => 'profile_thumb',
2549 'net_publish' => true,
2550 'description' => 'description',
2551 'date_of_birth' => 'date_of_birth',
2552 'address' => 'address',
2554 'region' => 'region',
2555 'postal_code' => 'postal_code',
2556 'country' => 'country',
2557 'hometown' => 'hometown',
2558 'gender' => 'gender',
2559 'marital' => 'marital',
2560 'marital_with' => 'marital_with',
2561 'marital_since' => 'marital_since',
2562 'sexual' => 'sexual',
2563 'politic' => 'politic',
2564 'religion' => 'religion',
2565 'public_keywords' => 'public_keywords',
2566 'private_keywords' => 'private_keywords',
2569 'dislikes' => 'dislikes',
2575 'interest' => 'interest',
2576 'romance' => 'romance',
2578 'education' => 'education',
2579 'social_networks' => 'social_networks',
2580 'homepage' => 'homepage',
2588 * Test the api_format_items() function.
2592 public function testApiFormatItems()
2596 'item_network' => 'item_network',
2602 'author-network' => Protocol::DFRN,
2603 'author-link' => 'http://localhost/profile/othercontact',
2607 $result = api_format_items($items, ['id' => 0], true);
2608 foreach ($result as $status) {
2609 $this->assertStatus($status);
2614 * Test the api_format_items() function with an XML result.
2618 public function testApiFormatItemsWithXml()
2626 'author-network' => Protocol::DFRN,
2627 'author-link' => 'http://localhost/profile/othercontact',
2631 $result = api_format_items($items, ['id' => 0], true, 'xml');
2632 foreach ($result as $status) {
2633 $this->assertStatus($status);
2638 * Test the api_format_items() function.
2642 public function testApiAccountRateLimitStatus()
2644 $result = api_account_rate_limit_status('json');
2645 $this->assertEquals(150, $result['hash']['remaining_hits']);
2646 $this->assertEquals(150, $result['hash']['hourly_limit']);
2647 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2651 * Test the api_format_items() function with an XML result.
2655 public function testApiAccountRateLimitStatusWithXml()
2657 $result = api_account_rate_limit_status('xml');
2658 $this->assertXml($result, 'hash');
2662 * Test the api_help_test() function.
2666 public function testApiHelpTest()
2668 $result = api_help_test('json');
2669 $this->assertEquals(['ok' => 'ok'], $result);
2673 * Test the api_help_test() function with an XML result.
2677 public function testApiHelpTestWithXml()
2679 $result = api_help_test('xml');
2680 $this->assertXml($result, 'ok');
2684 * Test the api_lists_list() function.
2688 public function testApiListsList()
2690 $result = api_lists_list('json');
2691 $this->assertEquals(['lists_list' => []], $result);
2695 * Test the api_lists_ownerships() function.
2699 public function testApiListsOwnerships()
2701 $result = api_lists_ownerships('json');
2702 foreach ($result['lists']['lists'] as $list) {
2703 $this->assertList($list);
2708 * Test the api_lists_ownerships() function without an authenticated user.
2711 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2713 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2715 $_SESSION['authenticated'] = false;
2716 api_lists_ownerships('json');
2720 * Test the api_lists_statuses() function.
2722 * @expectedException Friendica\Network\HTTPException\BadRequestException
2725 public function testApiListsStatuses()
2727 api_lists_statuses('json');
2731 * Test the api_lists_statuses() function with a list ID.
2735 public function testApiListsStatusesWithListId()
2737 $_REQUEST['list_id'] = 1;
2738 $_REQUEST['page'] = -1;
2739 $_REQUEST['max_id'] = 10;
2740 $result = api_lists_statuses('json');
2741 foreach ($result['status'] as $status) {
2742 $this->assertStatus($status);
2747 * Test the api_lists_statuses() function with a list ID and a RSS result.
2751 public function testApiListsStatusesWithListIdAndRss()
2753 $_REQUEST['list_id'] = 1;
2754 $result = api_lists_statuses('rss');
2755 $this->assertXml($result, 'statuses');
2759 * Test the api_lists_statuses() function with an unallowed user.
2762 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2764 public function testApiListsStatusesWithUnallowedUser()
2766 $_SESSION['allow_api'] = false;
2767 $_GET['screen_name'] = $this->selfUser['nick'];
2768 api_lists_statuses('json');
2772 * Test the api_statuses_f() function.
2776 public function testApiStatusesFWithFriends()
2779 $result = api_statuses_f('friends');
2780 $this->assertArrayHasKey('user', $result);
2784 * Test the api_statuses_f() function.
2788 public function testApiStatusesFWithFollowers()
2790 $result = api_statuses_f('followers');
2791 $this->assertArrayHasKey('user', $result);
2795 * Test the api_statuses_f() function.
2799 public function testApiStatusesFWithBlocks()
2801 $result = api_statuses_f('blocks');
2802 $this->assertArrayHasKey('user', $result);
2806 * Test the api_statuses_f() function.
2810 public function testApiStatusesFWithIncoming()
2812 $result = api_statuses_f('incoming');
2813 $this->assertArrayHasKey('user', $result);
2817 * Test the api_statuses_f() function an undefined cursor GET variable.
2821 public function testApiStatusesFWithUndefinedCursor()
2823 $_GET['cursor'] = 'undefined';
2824 $this->assertFalse(api_statuses_f('friends'));
2828 * Test the api_statuses_friends() function.
2832 public function testApiStatusesFriends()
2834 $result = api_statuses_friends('json');
2835 $this->assertArrayHasKey('user', $result);
2839 * Test the api_statuses_friends() function an undefined cursor GET variable.
2843 public function testApiStatusesFriendsWithUndefinedCursor()
2845 $_GET['cursor'] = 'undefined';
2846 $this->assertFalse(api_statuses_friends('json'));
2850 * Test the api_statuses_followers() function.
2854 public function testApiStatusesFollowers()
2856 $result = api_statuses_followers('json');
2857 $this->assertArrayHasKey('user', $result);
2861 * Test the api_statuses_followers() function an undefined cursor GET variable.
2865 public function testApiStatusesFollowersWithUndefinedCursor()
2867 $_GET['cursor'] = 'undefined';
2868 $this->assertFalse(api_statuses_followers('json'));
2872 * Test the api_blocks_list() function.
2876 public function testApiBlocksList()
2878 $result = api_blocks_list('json');
2879 $this->assertArrayHasKey('user', $result);
2883 * Test the api_blocks_list() function an undefined cursor GET variable.
2887 public function testApiBlocksListWithUndefinedCursor()
2889 $_GET['cursor'] = 'undefined';
2890 $this->assertFalse(api_blocks_list('json'));
2894 * Test the api_friendships_incoming() function.
2898 public function testApiFriendshipsIncoming()
2900 $result = api_friendships_incoming('json');
2901 $this->assertArrayHasKey('id', $result);
2905 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2909 public function testApiFriendshipsIncomingWithUndefinedCursor()
2911 $_GET['cursor'] = 'undefined';
2912 $this->assertFalse(api_friendships_incoming('json'));
2916 * Test the api_statusnet_config() function.
2920 public function testApiStatusnetConfig()
2922 $result = api_statusnet_config('json');
2923 $this->assertEquals('localhost', $result['config']['site']['server']);
2924 $this->assertEquals('default', $result['config']['site']['theme']);
2925 $this->assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2926 $this->assertTrue($result['config']['site']['fancy']);
2927 $this->assertEquals('en', $result['config']['site']['language']);
2928 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2929 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2930 $this->assertEquals('false', $result['config']['site']['private']);
2931 $this->assertEquals('false', $result['config']['site']['ssl']);
2932 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2936 * Test the api_statusnet_version() function.
2940 public function testApiStatusnetVersion()
2942 $result = api_statusnet_version('json');
2943 $this->assertEquals('0.9.7', $result['version']);
2947 * Test the api_ff_ids() function.
2951 public function testApiFfIds()
2953 $result = api_ff_ids('json', Contact::FOLLOWER);
2954 $this->assertEquals(['id' => []], $result);
2958 * Test the api_ff_ids() function with a result.
2962 public function testApiFfIdsWithResult()
2964 $this->markTestIncomplete();
2968 * Test the api_ff_ids() function without an authenticated user.
2971 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2973 public function testApiFfIdsWithoutAuthenticatedUser()
2975 $_SESSION['authenticated'] = false;
2976 api_ff_ids('json', Contact::FOLLOWER);
2980 * Test the api_friends_ids() function.
2984 public function testApiFriendsIds()
2986 $result = api_friends_ids('json');
2987 $this->assertEquals(['id' => []], $result);
2991 * Test the api_followers_ids() function.
2995 public function testApiFollowersIds()
2997 $result = api_followers_ids('json');
2998 $this->assertEquals(['id' => []], $result);
3002 * Test the api_direct_messages_new() function.
3006 public function testApiDirectMessagesNew()
3008 $result = api_direct_messages_new('json');
3009 $this->assertNull($result);
3013 * Test the api_direct_messages_new() function without an authenticated user.
3016 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3018 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
3020 $_SESSION['authenticated'] = false;
3021 api_direct_messages_new('json');
3025 * Test the api_direct_messages_new() function with an user ID.
3029 public function testApiDirectMessagesNewWithUserId()
3031 $_POST['text'] = 'message_text';
3032 $_POST['user_id'] = $this->otherUser['id'];
3033 $result = api_direct_messages_new('json');
3034 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
3038 * Test the api_direct_messages_new() function with a screen name.
3042 public function testApiDirectMessagesNewWithScreenName()
3044 $_POST['text'] = 'message_text';
3045 $_POST['screen_name'] = $this->friendUser['nick'];
3046 $result = api_direct_messages_new('json');
3047 $this->assertContains('message_text', $result['direct_message']['text']);
3048 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3049 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3053 * Test the api_direct_messages_new() function with a title.
3057 public function testApiDirectMessagesNewWithTitle()
3059 $_POST['text'] = 'message_text';
3060 $_POST['screen_name'] = $this->friendUser['nick'];
3061 $_REQUEST['title'] = 'message_title';
3062 $result = api_direct_messages_new('json');
3063 $this->assertContains('message_text', $result['direct_message']['text']);
3064 $this->assertContains('message_title', $result['direct_message']['text']);
3065 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3066 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3070 * Test the api_direct_messages_new() function with an RSS result.
3074 public function testApiDirectMessagesNewWithRss()
3076 $_POST['text'] = 'message_text';
3077 $_POST['screen_name'] = $this->friendUser['nick'];
3078 $result = api_direct_messages_new('rss');
3079 $this->assertXml($result, 'direct-messages');
3083 * Test the api_direct_messages_destroy() function.
3086 * @expectedException Friendica\Network\HTTPException\BadRequestException
3088 public function testApiDirectMessagesDestroy()
3090 api_direct_messages_destroy('json');
3094 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
3098 public function testApiDirectMessagesDestroyWithVerbose()
3100 $_GET['friendica_verbose'] = 'true';
3101 $result = api_direct_messages_destroy('json');
3102 $this->assertEquals(
3105 'result' => 'error',
3106 'message' => 'message id or parenturi not specified'
3114 * Test the api_direct_messages_destroy() function without an authenticated user.
3117 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3119 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
3121 $_SESSION['authenticated'] = false;
3122 api_direct_messages_destroy('json');
3126 * Test the api_direct_messages_destroy() function with a non-zero ID.
3129 * @expectedException Friendica\Network\HTTPException\BadRequestException
3131 public function testApiDirectMessagesDestroyWithId()
3133 $_REQUEST['id'] = 1;
3134 api_direct_messages_destroy('json');
3138 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
3142 public function testApiDirectMessagesDestroyWithIdAndVerbose()
3144 $_REQUEST['id'] = 1;
3145 $_REQUEST['friendica_parenturi'] = 'parent_uri';
3146 $_GET['friendica_verbose'] = 'true';
3147 $result = api_direct_messages_destroy('json');
3148 $this->assertEquals(
3151 'result' => 'error',
3152 'message' => 'message id not in database'
3160 * Test the api_direct_messages_destroy() function with a non-zero ID.
3164 public function testApiDirectMessagesDestroyWithCorrectId()
3166 $this->markTestIncomplete('We need to add a dataset for this.');
3170 * Test the api_direct_messages_box() function.
3174 public function testApiDirectMessagesBoxWithSentbox()
3176 $_REQUEST['page'] = -1;
3177 $_REQUEST['max_id'] = 10;
3178 $result = api_direct_messages_box('json', 'sentbox', 'false');
3179 $this->assertArrayHasKey('direct_message', $result);
3183 * Test the api_direct_messages_box() function.
3187 public function testApiDirectMessagesBoxWithConversation()
3189 $result = api_direct_messages_box('json', 'conversation', 'false');
3190 $this->assertArrayHasKey('direct_message', $result);
3194 * Test the api_direct_messages_box() function.
3198 public function testApiDirectMessagesBoxWithAll()
3200 $result = api_direct_messages_box('json', 'all', 'false');
3201 $this->assertArrayHasKey('direct_message', $result);
3205 * Test the api_direct_messages_box() function.
3209 public function testApiDirectMessagesBoxWithInbox()
3211 $result = api_direct_messages_box('json', 'inbox', 'false');
3212 $this->assertArrayHasKey('direct_message', $result);
3216 * Test the api_direct_messages_box() function.
3220 public function testApiDirectMessagesBoxWithVerbose()
3222 $result = api_direct_messages_box('json', 'sentbox', 'true');
3223 $this->assertEquals(
3226 'result' => 'error',
3227 'message' => 'no mails available'
3235 * Test the api_direct_messages_box() function with a RSS result.
3239 public function testApiDirectMessagesBoxWithRss()
3241 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3242 $this->assertXml($result, 'direct-messages');
3246 * Test the api_direct_messages_box() function without an authenticated user.
3249 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3251 public function testApiDirectMessagesBoxWithUnallowedUser()
3253 $_SESSION['allow_api'] = false;
3254 $_GET['screen_name'] = $this->selfUser['nick'];
3255 api_direct_messages_box('json', 'sentbox', 'false');
3259 * Test the api_direct_messages_sentbox() function.
3263 public function testApiDirectMessagesSentbox()
3265 $result = api_direct_messages_sentbox('json');
3266 $this->assertArrayHasKey('direct_message', $result);
3270 * Test the api_direct_messages_inbox() function.
3274 public function testApiDirectMessagesInbox()
3276 $result = api_direct_messages_inbox('json');
3277 $this->assertArrayHasKey('direct_message', $result);
3281 * Test the api_direct_messages_all() function.
3285 public function testApiDirectMessagesAll()
3287 $result = api_direct_messages_all('json');
3288 $this->assertArrayHasKey('direct_message', $result);
3292 * Test the api_direct_messages_conversation() function.
3296 public function testApiDirectMessagesConversation()
3298 $result = api_direct_messages_conversation('json');
3299 $this->assertArrayHasKey('direct_message', $result);
3303 * Test the api_oauth_request_token() function.
3307 public function testApiOauthRequestToken()
3309 $this->markTestIncomplete('exit() kills phpunit as well');
3313 * Test the api_oauth_access_token() function.
3317 public function testApiOauthAccessToken()
3319 $this->markTestIncomplete('exit() kills phpunit as well');
3323 * Test the api_fr_photoalbum_delete() function.
3326 * @expectedException Friendica\Network\HTTPException\BadRequestException
3328 public function testApiFrPhotoalbumDelete()
3330 api_fr_photoalbum_delete('json');
3334 * Test the api_fr_photoalbum_delete() function with an album name.
3337 * @expectedException Friendica\Network\HTTPException\BadRequestException
3339 public function testApiFrPhotoalbumDeleteWithAlbum()
3341 $_REQUEST['album'] = 'album_name';
3342 api_fr_photoalbum_delete('json');
3346 * Test the api_fr_photoalbum_delete() function with an album name.
3350 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3352 $this->markTestIncomplete('We need to add a dataset for this.');
3356 * Test the api_fr_photoalbum_delete() function.
3359 * @expectedException Friendica\Network\HTTPException\BadRequestException
3361 public function testApiFrPhotoalbumUpdate()
3363 api_fr_photoalbum_update('json');
3367 * Test the api_fr_photoalbum_delete() function with an album name.
3370 * @expectedException Friendica\Network\HTTPException\BadRequestException
3372 public function testApiFrPhotoalbumUpdateWithAlbum()
3374 $_REQUEST['album'] = 'album_name';
3375 api_fr_photoalbum_update('json');
3379 * Test the api_fr_photoalbum_delete() function with an album name.
3382 * @expectedException Friendica\Network\HTTPException\BadRequestException
3384 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3386 $_REQUEST['album'] = 'album_name';
3387 $_REQUEST['album_new'] = 'album_name';
3388 api_fr_photoalbum_update('json');
3392 * Test the api_fr_photoalbum_update() function without an authenticated user.
3395 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3397 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3399 $_SESSION['authenticated'] = false;
3400 api_fr_photoalbum_update('json');
3404 * Test the api_fr_photoalbum_delete() function with an album name.
3408 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3410 $this->markTestIncomplete('We need to add a dataset for this.');
3414 * Test the api_fr_photos_list() function.
3418 public function testApiFrPhotosList()
3420 $result = api_fr_photos_list('json');
3421 $this->assertArrayHasKey('photo', $result);
3425 * Test the api_fr_photos_list() function without an authenticated user.
3428 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3430 public function testApiFrPhotosListWithoutAuthenticatedUser()
3432 $_SESSION['authenticated'] = false;
3433 api_fr_photos_list('json');
3437 * Test the api_fr_photo_create_update() function.
3440 * @expectedException Friendica\Network\HTTPException\BadRequestException
3442 public function testApiFrPhotoCreateUpdate()
3444 api_fr_photo_create_update('json');
3448 * Test the api_fr_photo_create_update() function without an authenticated user.
3451 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3453 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3455 $_SESSION['authenticated'] = false;
3456 api_fr_photo_create_update('json');
3460 * Test the api_fr_photo_create_update() function with an album name.
3463 * @expectedException Friendica\Network\HTTPException\BadRequestException
3465 public function testApiFrPhotoCreateUpdateWithAlbum()
3467 $_REQUEST['album'] = 'album_name';
3468 api_fr_photo_create_update('json');
3472 * Test the api_fr_photo_create_update() function with the update mode.
3476 public function testApiFrPhotoCreateUpdateWithUpdate()
3478 $this->markTestIncomplete('We need to create a dataset for this');
3482 * Test the api_fr_photo_create_update() function with an uploaded file.
3486 public function testApiFrPhotoCreateUpdateWithFile()
3488 $this->markTestIncomplete();
3492 * Test the api_fr_photo_delete() function.
3495 * @expectedException Friendica\Network\HTTPException\BadRequestException
3497 public function testApiFrPhotoDelete()
3499 api_fr_photo_delete('json');
3503 * Test the api_fr_photo_delete() function without an authenticated user.
3506 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3508 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3510 $_SESSION['authenticated'] = false;
3511 api_fr_photo_delete('json');
3515 * Test the api_fr_photo_delete() function with a photo ID.
3518 * @expectedException Friendica\Network\HTTPException\BadRequestException
3520 public function testApiFrPhotoDeleteWithPhotoId()
3522 $_REQUEST['photo_id'] = 1;
3523 api_fr_photo_delete('json');
3527 * Test the api_fr_photo_delete() function with a correct photo ID.
3531 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3533 $this->markTestIncomplete('We need to create a dataset for this.');
3537 * Test the api_fr_photo_detail() function.
3540 * @expectedException Friendica\Network\HTTPException\BadRequestException
3542 public function testApiFrPhotoDetail()
3544 api_fr_photo_detail('json');
3548 * Test the api_fr_photo_detail() function without an authenticated user.
3551 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3553 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3555 $_SESSION['authenticated'] = false;
3556 api_fr_photo_detail('json');
3560 * Test the api_fr_photo_detail() function with a photo ID.
3563 * @expectedException Friendica\Network\HTTPException\NotFoundException
3565 public function testApiFrPhotoDetailWithPhotoId()
3567 $_REQUEST['photo_id'] = 1;
3568 api_fr_photo_detail('json');
3572 * Test the api_fr_photo_detail() function with a correct photo ID.
3576 public function testApiFrPhotoDetailCorrectPhotoId()
3578 $this->markTestIncomplete('We need to create a dataset for this.');
3582 * Test the api_account_update_profile_image() function.
3585 * @expectedException Friendica\Network\HTTPException\BadRequestException
3587 public function testApiAccountUpdateProfileImage()
3589 api_account_update_profile_image('json');
3593 * Test the api_account_update_profile_image() function without an authenticated user.
3596 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3598 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3600 $_SESSION['authenticated'] = false;
3601 api_account_update_profile_image('json');
3605 * Test the api_account_update_profile_image() function with an uploaded file.
3608 * @expectedException Friendica\Network\HTTPException\BadRequestException
3610 public function testApiAccountUpdateProfileImageWithUpload()
3612 $this->markTestIncomplete();
3617 * Test the api_account_update_profile() function.
3621 public function testApiAccountUpdateProfile()
3623 $_POST['name'] = 'new_name';
3624 $_POST['description'] = 'new_description';
3625 $result = api_account_update_profile('json');
3626 // We can't use assertSelfUser() here because the user object is missing some properties.
3627 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3628 $this->assertEquals('DFRN', $result['user']['location']);
3629 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3630 $this->assertEquals('dfrn', $result['user']['network']);
3631 $this->assertEquals('new_name', $result['user']['name']);
3632 $this->assertEquals('new_description', $result['user']['description']);
3636 * Test the check_acl_input() function.
3640 public function testCheckAclInput()
3642 $result = check_acl_input('<aclstring>');
3643 // Where does this result come from?
3644 $this->assertEquals(1, $result);
3648 * Test the check_acl_input() function with an empty ACL string.
3652 public function testCheckAclInputWithEmptyAclString()
3654 $result = check_acl_input(' ');
3655 $this->assertFalse($result);
3659 * Test the save_media_to_database() function.
3663 public function testSaveMediaToDatabase()
3665 $this->markTestIncomplete();
3669 * Test the post_photo_item() function.
3673 public function testPostPhotoItem()
3675 $this->markTestIncomplete();
3679 * Test the prepare_photo_data() function.
3683 public function testPreparePhotoData()
3685 $this->markTestIncomplete();
3689 * Test the api_friendica_remoteauth() function.
3692 * @expectedException Friendica\Network\HTTPException\BadRequestException
3694 public function testApiFriendicaRemoteauth()
3696 api_friendica_remoteauth();
3700 * Test the api_friendica_remoteauth() function with an URL.
3703 * @expectedException Friendica\Network\HTTPException\BadRequestException
3705 public function testApiFriendicaRemoteauthWithUrl()
3707 $_GET['url'] = 'url';
3708 $_GET['c_url'] = 'url';
3709 api_friendica_remoteauth();
3713 * Test the api_friendica_remoteauth() function with a correct URL.
3717 public function testApiFriendicaRemoteauthWithCorrectUrl()
3719 $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
3720 $_GET['url'] = 'url';
3721 $_GET['c_url'] = $this->selfUser['nurl'];
3722 api_friendica_remoteauth();
3726 * Test the api_share_as_retweet() function.
3730 public function testApiShareAsRetweet()
3732 $item = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3733 $result = api_share_as_retweet($item);
3734 $this->assertFalse($result);
3738 * Test the api_share_as_retweet() function with a valid item.
3742 public function testApiShareAsRetweetWithValidItem()
3744 $this->markTestIncomplete();
3748 * Test the api_in_reply_to() function.
3752 public function testApiInReplyTo()
3754 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3755 $this->assertArrayHasKey('status_id', $result);
3756 $this->assertArrayHasKey('user_id', $result);
3757 $this->assertArrayHasKey('status_id_str', $result);
3758 $this->assertArrayHasKey('user_id_str', $result);
3759 $this->assertArrayHasKey('screen_name', $result);
3763 * Test the api_in_reply_to() function with a valid item.
3767 public function testApiInReplyToWithValidItem()
3769 $this->markTestIncomplete();
3773 * Test the api_clean_plain_items() function.
3777 public function testApiCleanPlainItems()
3779 $_REQUEST['include_entities'] = 'true';
3780 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3781 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3785 * Test the api_best_nickname() function.
3789 public function testApiBestNickname()
3792 $result = api_best_nickname($contacts);
3793 $this->assertNull($result);
3797 * Test the api_best_nickname() function with contacts.
3801 public function testApiBestNicknameWithContacts()
3803 $this->markTestIncomplete();
3807 * Test the api_friendica_group_show() function.
3811 public function testApiFriendicaGroupShow()
3813 $this->markTestIncomplete();
3817 * Test the api_friendica_group_delete() function.
3821 public function testApiFriendicaGroupDelete()
3823 $this->markTestIncomplete();
3827 * Test the api_lists_destroy() function.
3831 public function testApiListsDestroy()
3833 $this->markTestIncomplete();
3837 * Test the group_create() function.
3841 public function testGroupCreate()
3843 $this->markTestIncomplete();
3847 * Test the api_friendica_group_create() function.
3851 public function testApiFriendicaGroupCreate()
3853 $this->markTestIncomplete();
3857 * Test the api_lists_create() function.
3861 public function testApiListsCreate()
3863 $this->markTestIncomplete();
3867 * Test the api_friendica_group_update() function.
3871 public function testApiFriendicaGroupUpdate()
3873 $this->markTestIncomplete();
3877 * Test the api_lists_update() function.
3881 public function testApiListsUpdate()
3883 $this->markTestIncomplete();
3887 * Test the api_friendica_activity() function.
3891 public function testApiFriendicaActivity()
3893 $this->markTestIncomplete();
3897 * Test the api_friendica_notification() function.
3900 * @expectedException Friendica\Network\HTTPException\BadRequestException
3902 public function testApiFriendicaNotification()
3904 api_friendica_notification('json');
3908 * Test the api_friendica_notification() function without an authenticated user.
3911 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3913 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3915 $_SESSION['authenticated'] = false;
3916 api_friendica_notification('json');
3920 * Test the api_friendica_notification() function with empty result
3924 public function testApiFriendicaNotificationWithEmptyResult()
3926 $this->app->argv = ['api', 'friendica', 'notification'];
3927 $this->app->argc = count($this->app->argv);
3928 $_SESSION['uid'] = 41;
3929 $result = api_friendica_notification('json');
3930 $this->assertEquals(['note' => false], $result);
3934 * Test the api_friendica_notification() function with an XML result.
3938 public function testApiFriendicaNotificationWithXmlResult()
3940 $this->app->argv = ['api', 'friendica', 'notification'];
3941 $this->app->argc = count($this->app->argv);
3942 $result = api_friendica_notification('xml');
3944 <?xml version="1.0"?>
3946 <note id="1" hash="" type="8" name="Reply to" url="http://localhost/display/1" photo="http://localhost/" date="2020-01-01 12:12:02" msg="A test reply from an item" uid="42" link="http://localhost/notification/1" iid="4" parent="0" seen="0" verb="" otype="item" name_cache="" msg_cache="A test reply from an item" timestamp="1577880722" date_rel="4 weeks ago" msg_html="A test reply from an item" msg_plain="A test reply from an item"/>
3949 $this->assertXmlStringEqualsXmlString($assertXml, $result);
3953 * Test the api_friendica_notification() function with an JSON result.
3957 public function testApiFriendicaNotificationWithJsonResult()
3959 $this->app->argv = ['api', 'friendica', 'notification'];
3960 $this->app->argc = count($this->app->argv);
3961 $result = json_encode(api_friendica_notification('json'));
3962 $this->assertJson($result);
3966 * Test the api_friendica_notification_seen() function.
3970 public function testApiFriendicaNotificationSeen()
3972 $this->markTestIncomplete();
3976 * Test the api_friendica_direct_messages_setseen() function.
3980 public function testApiFriendicaDirectMessagesSetseen()
3982 $this->markTestIncomplete();
3986 * Test the api_friendica_direct_messages_search() function.
3990 public function testApiFriendicaDirectMessagesSearch()
3992 $this->markTestIncomplete();
3996 * Test the api_friendica_profile_show() function.
4000 public function testApiFriendicaProfileShow()
4002 $result = api_friendica_profile_show('json');
4003 // We can't use assertSelfUser() here because the user object is missing some properties.
4004 $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
4005 $this->assertEquals('DFRN', $result['$result']['friendica_owner']['location']);
4006 $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
4007 $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
4008 $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
4009 $this->assertTrue($result['$result']['friendica_owner']['verified']);
4010 $this->assertFalse($result['$result']['multi_profiles']);
4014 * Test the api_friendica_profile_show() function with a profile ID.
4018 public function testApiFriendicaProfileShowWithProfileId()
4020 $this->markTestIncomplete('We need to add a dataset for this.');
4024 * Test the api_friendica_profile_show() function with a wrong profile ID.
4027 * @expectedException Friendica\Network\HTTPException\BadRequestException
4029 public function testApiFriendicaProfileShowWithWrongProfileId()
4031 $_REQUEST['profile_id'] = 666;
4032 api_friendica_profile_show('json');
4036 * Test the api_friendica_profile_show() function without an authenticated user.
4039 * @expectedException Friendica\Network\HTTPException\ForbiddenException
4041 public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
4043 $_SESSION['authenticated'] = false;
4044 api_friendica_profile_show('json');
4048 * Test the api_saved_searches_list() function.
4052 public function testApiSavedSearchesList()
4054 $result = api_saved_searches_list('json');
4055 $this->assertEquals(1, $result['terms'][0]['id']);
4056 $this->assertEquals(1, $result['terms'][0]['id_str']);
4057 $this->assertEquals('Saved search', $result['terms'][0]['name']);
4058 $this->assertEquals('Saved search', $result['terms'][0]['query']);