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 Friendica\Util\Temporal;
23 use Monolog\Handler\TestHandler;
25 require_once __DIR__ . '/../../include/api.php';
28 * Tests for the API functions.
30 * Functions that use header() need to be tested in a separate process.
31 * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
33 class ApiTest extends DatabaseTest
36 * @var TestHandler Can handle log-outputs
43 protected $friendUser;
47 protected $wrongUserId;
59 * Create variables used by tests.
61 protected function setUp()
65 $this->dice = (new Dice())
66 ->addRules(include __DIR__ . '/../../static/dependencies.config.php')
67 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
68 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
69 DI::init($this->dice);
71 /** @var Database $dba */
72 $dba = $this->dice->create(Database::class);
74 /** @var IConfig $config */
75 $this->config = $this->dice->create(IConfig::class);
77 $this->config->set('system', 'url', 'http://localhost');
78 $this->config->set('system', 'hostname', 'localhost');
79 $this->config->set('system', 'worker_dont_fork', true);
82 $this->config->set('config', 'hostname', 'localhost');
83 $this->config->set('system', 'throttle_limit_day', 100);
84 $this->config->set('system', 'throttle_limit_week', 100);
85 $this->config->set('system', 'throttle_limit_month', 100);
86 $this->config->set('system', 'theme', 'system_theme');
88 // Load the API dataset for the whole API
89 $this->loadFixture(__DIR__ . '/../datasets/api.fixture.php', $dba);
92 $this->app = DI::app();
95 $this->app->argv = ['home'];
97 // User data that the test database is populated with
100 'name' => 'Self contact',
101 'nick' => 'selfcontact',
102 'nurl' => 'http://localhost/profile/selfcontact'
104 $this->friendUser = [
106 'name' => 'Friend contact',
107 'nick' => 'friendcontact',
108 'nurl' => 'http://localhost/profile/friendcontact'
112 'name' => 'othercontact',
113 'nick' => 'othercontact',
114 'nurl' => 'http://localhost/profile/othercontact'
117 // User ID that we know is not in the database
118 $this->wrongUserId = 666;
120 DI::session()->start();
122 // Most API require login so we force the session
125 'authenticated' => true,
126 'uid' => $this->selfUser['id']
135 * Assert that an user array contains expected keys.
137 * @param array $user User array
141 private function assertSelfUser(array $user)
143 $this->assertEquals($this->selfUser['id'], $user['uid']);
144 $this->assertEquals($this->selfUser['id'], $user['cid']);
145 $this->assertEquals(1, $user['self']);
146 $this->assertEquals('DFRN', $user['location']);
147 $this->assertEquals($this->selfUser['name'], $user['name']);
148 $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
149 $this->assertEquals('dfrn', $user['network']);
150 $this->assertTrue($user['verified']);
154 * Assert that an user array contains expected keys.
156 * @param array $user User array
160 private function assertOtherUser(array $user)
162 $this->assertEquals($this->otherUser['id'], $user['id']);
163 $this->assertEquals($this->otherUser['id'], $user['id_str']);
164 $this->assertEquals(0, $user['self']);
165 $this->assertEquals($this->otherUser['name'], $user['name']);
166 $this->assertEquals($this->otherUser['nick'], $user['screen_name']);
167 $this->assertFalse($user['verified']);
171 * Assert that a status array contains expected keys.
173 * @param array $status Status array
177 private function assertStatus(array $status)
179 $this->assertInternalType('string', $status['text']);
180 $this->assertInternalType('int', $status['id']);
181 // We could probably do more checks here.
185 * Assert that a list array contains expected keys.
187 * @param array $list List array
191 private function assertList(array $list)
193 $this->assertInternalType('string', $list['name']);
194 $this->assertInternalType('int', $list['id']);
195 $this->assertInternalType('string', $list['id_str']);
196 $this->assertContains($list['mode'], ['public', 'private']);
197 // We could probably do more checks here.
201 * Assert that the string is XML and contain the root element.
203 * @param string $result XML string
204 * @param string $root_element Root element name
208 private function assertXml($result, $root_element)
210 $this->assertStringStartsWith('<?xml version="1.0"?>', $result);
211 $this->assertContains('<' . $root_element, $result);
212 // We could probably do more checks here.
216 * Get the path to a temporary empty PNG image.
218 * @return string Path
220 private function getTempImage()
222 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
226 // Empty 1x1 px PNG image
227 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
235 * Test the api_user() function.
239 public function testApiUser()
241 $this->assertEquals($this->selfUser['id'], api_user());
245 * Test the api_user() function with an unallowed user.
249 public function testApiUserWithUnallowedUser()
251 $_SESSION = ['allow_api' => false];
252 $this->assertEquals(false, api_user());
256 * Test the api_source() function.
260 public function testApiSource()
262 $this->assertEquals('api', api_source());
266 * Test the api_source() function with a Twidere user agent.
270 public function testApiSourceWithTwidere()
272 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
273 $this->assertEquals('Twidere', api_source());
277 * Test the api_source() function with a GET parameter.
281 public function testApiSourceWithGet()
283 $_GET['source'] = 'source_name';
284 $this->assertEquals('source_name', api_source());
288 * Test the api_date() function.
292 public function testApiDate()
294 $this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
298 * Test the api_register_func() function.
302 public function testApiRegisterFunc()
314 $this->assertTrue($API['api_path']['auth']);
315 $this->assertEquals('method', $API['api_path']['method']);
316 $this->assertTrue(is_callable($API['api_path']['func']));
320 * Test the api_login() function without any login.
323 * @runInSeparateProcess
324 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
326 public function testApiLoginWithoutLogin()
328 api_login($this->app);
332 * Test the api_login() function with a bad login.
335 * @runInSeparateProcess
336 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
338 public function testApiLoginWithBadLogin()
340 $_SERVER['PHP_AUTH_USER'] = 'user@server';
341 api_login($this->app);
345 * Test the api_login() function with oAuth.
349 public function testApiLoginWithOauth()
351 $this->markTestIncomplete('Can we test this easily?');
355 * Test the api_login() function with authentication provided by an addon.
359 public function testApiLoginWithAddonAuth()
361 $this->markTestIncomplete('Can we test this easily?');
365 * Test the api_login() function with a correct login.
368 * @runInSeparateProcess
370 public function testApiLoginWithCorrectLogin()
372 $_SERVER['PHP_AUTH_USER'] = 'Test user';
373 $_SERVER['PHP_AUTH_PW'] = 'password';
374 api_login($this->app);
378 * Test the api_login() function with a remote user.
381 * @runInSeparateProcess
382 * @expectedException Friendica\Network\HTTPException\UnauthorizedException
384 public function testApiLoginWithRemoteUser()
386 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
387 api_login($this->app);
391 * Test the api_check_method() function.
395 public function testApiCheckMethod()
397 $this->assertFalse(api_check_method('method'));
401 * Test the api_check_method() function with a correct method.
405 public function testApiCheckMethodWithCorrectMethod()
407 $_SERVER['REQUEST_METHOD'] = 'method';
408 $this->assertTrue(api_check_method('method'));
412 * Test the api_check_method() function with a wildcard.
416 public function testApiCheckMethodWithWildcard()
418 $this->assertTrue(api_check_method('*'));
422 * Test the api_call() function.
425 * @runInSeparateProcess
427 public function testApiCall()
431 'method' => 'method',
432 'func' => function () {
433 return ['data' => ['some_data']];
436 $_SERVER['REQUEST_METHOD'] = 'method';
437 $_SERVER['QUERY_STRING'] = 'q=api_path';
438 $_GET['callback'] = 'callback_name';
440 $args = DI::args()->determine($_SERVER, $_GET);
443 'callback_name(["some_data"])',
444 api_call($this->app, $args)
449 * Test the api_call() function with the profiled enabled.
452 * @runInSeparateProcess
454 public function testApiCallWithProfiler()
458 'method' => 'method',
459 'func' => function () {
460 return ['data' => ['some_data']];
464 $_SERVER['REQUEST_METHOD'] = 'method';
465 $_SERVER['QUERY_STRING'] = 'q=api_path';
467 $args = DI::args()->determine($_SERVER, $_GET);
469 $this->config->set('system', 'profiler', true);
470 $this->config->set('rendertime', 'callstack', true);
471 $this->app->callstack = [
472 'database' => ['some_function' => 200],
473 'database_write' => ['some_function' => 200],
474 'cache' => ['some_function' => 200],
475 'cache_write' => ['some_function' => 200],
476 'network' => ['some_function' => 200]
481 api_call($this->app, $args)
486 * Test the api_call() function without any result.
489 * @runInSeparateProcess
491 public function testApiCallWithNoResult()
495 'method' => 'method',
496 'func' => function () {
500 $_SERVER['REQUEST_METHOD'] = 'method';
501 $_SERVER['QUERY_STRING'] = 'q=api_path';
503 $args = DI::args()->determine($_SERVER, $_GET);
506 '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
507 api_call($this->app, $args)
512 * Test the api_call() function with an unimplemented API.
515 * @runInSeparateProcess
517 public function testApiCallWithUninplementedApi()
520 '{"status":{"error":"Not Implemented","code":"501 Not Implemented","request":""}}',
526 * Test the api_call() function with a JSON result.
529 * @runInSeparateProcess
531 public function testApiCallWithJson()
535 'method' => 'method',
536 'func' => function () {
537 return ['data' => ['some_data']];
540 $_SERVER['REQUEST_METHOD'] = 'method';
541 $_SERVER['QUERY_STRING'] = 'q=api_path.json';
543 $args = DI::args()->determine($_SERVER, $_GET);
547 api_call($this->app, $args)
552 * Test the api_call() function with an XML result.
555 * @runInSeparateProcess
557 public function testApiCallWithXml()
561 'method' => 'method',
562 'func' => function () {
566 $_SERVER['REQUEST_METHOD'] = 'method';
567 $_SERVER['QUERY_STRING'] = 'q=api_path.xml';
569 $args = DI::args()->determine($_SERVER, $_GET);
573 api_call($this->app, $args)
578 * Test the api_call() function with an RSS result.
581 * @runInSeparateProcess
583 public function testApiCallWithRss()
587 'method' => 'method',
588 'func' => function () {
592 $_SERVER['REQUEST_METHOD'] = 'method';
593 $_SERVER['QUERY_STRING'] = 'q=api_path.rss';
595 $args = DI::args()->determine($_SERVER, $_GET);
598 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
600 api_call($this->app, $args)
605 * Test the api_call() function with an Atom result.
608 * @runInSeparateProcess
610 public function testApiCallWithAtom()
614 'method' => 'method',
615 'func' => function () {
619 $_SERVER['REQUEST_METHOD'] = 'method';
620 $_SERVER['QUERY_STRING'] = 'q=api_path.atom';
622 $args = DI::args()->determine($_SERVER, $_GET);
625 '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
627 api_call($this->app, $args)
632 * Test the api_call() function with an unallowed method.
635 * @runInSeparateProcess
637 public function testApiCallWithWrongMethod()
640 $API['api_path'] = ['method' => 'method'];
642 $_SERVER['QUERY_STRING'] = 'q=api_path';
644 $args = DI::args()->determine($_SERVER, $_GET);
647 '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
648 api_call($this->app, $args)
653 * Test the api_call() function with an unauthorized user.
656 * @runInSeparateProcess
658 public function testApiCallWithWrongAuth()
662 'method' => 'method',
665 $_SESSION['authenticated'] = false;
666 $_SERVER['REQUEST_METHOD'] = 'method';
667 $_SERVER['QUERY_STRING'] = 'q=api_path';
669 $args = DI::args()->determine($_SERVER, $_GET);
672 '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
673 api_call($this->app, $args)
678 * Test the api_error() function with a JSON result.
681 * @runInSeparateProcess
683 public function testApiErrorWithJson()
686 '{"status":{"error":"error_message","code":"200 OK","request":""}}',
687 api_error('json', new HTTPException\OKException('error_message'), DI::args())
692 * Test the api_error() function with an XML result.
695 * @runInSeparateProcess
697 public function testApiErrorWithXml()
700 '<?xml version="1.0"?>' . "\n" .
701 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
702 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
703 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
704 ' <error>error_message</error>' . "\n" .
705 ' <code>200 OK</code>' . "\n" .
706 ' <request/>' . "\n" .
708 api_error('xml', new HTTPException\OKException('error_message'), DI::args())
713 * Test the api_error() function with an RSS result.
716 * @runInSeparateProcess
718 public function testApiErrorWithRss()
721 '<?xml version="1.0"?>' . "\n" .
722 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
723 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
724 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
725 ' <error>error_message</error>' . "\n" .
726 ' <code>200 OK</code>' . "\n" .
727 ' <request/>' . "\n" .
729 api_error('rss', new HTTPException\OKException('error_message'), DI::args())
734 * Test the api_error() function with an Atom result.
737 * @runInSeparateProcess
739 public function testApiErrorWithAtom()
742 '<?xml version="1.0"?>' . "\n" .
743 '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
744 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
745 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
746 ' <error>error_message</error>' . "\n" .
747 ' <code>200 OK</code>' . "\n" .
748 ' <request/>' . "\n" .
750 api_error('atom', new HTTPException\OKException('error_message'), DI::args())
755 * Test the api_rss_extra() function.
759 public function testApiRssExtra()
761 $user_info = ['url' => 'user_url', 'lang' => 'en'];
762 $result = api_rss_extra($this->app, [], $user_info);
763 $this->assertEquals($user_info, $result['$user']);
764 $this->assertEquals($user_info['url'], $result['$rss']['alternate']);
765 $this->assertArrayHasKey('self', $result['$rss']);
766 $this->assertArrayHasKey('base', $result['$rss']);
767 $this->assertArrayHasKey('updated', $result['$rss']);
768 $this->assertArrayHasKey('atom_updated', $result['$rss']);
769 $this->assertArrayHasKey('language', $result['$rss']);
770 $this->assertArrayHasKey('logo', $result['$rss']);
774 * Test the api_rss_extra() function without any user info.
778 public function testApiRssExtraWithoutUserInfo()
780 $result = api_rss_extra($this->app, [], null);
781 $this->assertInternalType('array', $result['$user']);
782 $this->assertArrayHasKey('alternate', $result['$rss']);
783 $this->assertArrayHasKey('self', $result['$rss']);
784 $this->assertArrayHasKey('base', $result['$rss']);
785 $this->assertArrayHasKey('updated', $result['$rss']);
786 $this->assertArrayHasKey('atom_updated', $result['$rss']);
787 $this->assertArrayHasKey('language', $result['$rss']);
788 $this->assertArrayHasKey('logo', $result['$rss']);
792 * Test the api_unique_id_to_nurl() function.
796 public function testApiUniqueIdToNurl()
798 $this->assertFalse(api_unique_id_to_nurl($this->wrongUserId));
802 * Test the api_unique_id_to_nurl() function with a correct ID.
806 public function testApiUniqueIdToNurlWithCorrectId()
808 $this->assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
812 * Test the api_get_user() function.
816 public function testApiGetUser()
818 $user = api_get_user($this->app);
819 $this->assertSelfUser($user);
820 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
821 $this->assertEquals('6fdbe8', $user['profile_link_color']);
822 $this->assertEquals('ededed', $user['profile_background_color']);
826 * Test the api_get_user() function with a Frio schema.
830 public function testApiGetUserWithFrioSchema()
832 $pConfig = $this->dice->create(IPConfig::class);
833 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
834 $user = api_get_user($this->app);
835 $this->assertSelfUser($user);
836 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
837 $this->assertEquals('6fdbe8', $user['profile_link_color']);
838 $this->assertEquals('ededed', $user['profile_background_color']);
842 * Test the api_get_user() function with a custom Frio schema.
846 public function testApiGetUserWithCustomFrioSchema()
848 $pConfig = $this->dice->create(IPConfig::class);
849 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
850 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
851 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
852 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
853 $user = api_get_user($this->app);
854 $this->assertSelfUser($user);
855 $this->assertEquals('123456', $user['profile_sidebar_fill_color']);
856 $this->assertEquals('123456', $user['profile_link_color']);
857 $this->assertEquals('123456', $user['profile_background_color']);
861 * Test the api_get_user() function with an empty Frio schema.
865 public function testApiGetUserWithEmptyFrioSchema()
867 $pConfig = $this->dice->create(IPConfig::class);
868 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
869 $user = api_get_user($this->app);
870 $this->assertSelfUser($user);
871 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
872 $this->assertEquals('6fdbe8', $user['profile_link_color']);
873 $this->assertEquals('ededed', $user['profile_background_color']);
877 * Test the api_get_user() function with an user that is not allowed to use the API.
880 * @runInSeparateProcess
882 public function testApiGetUserWithoutApiUser()
884 $_SERVER['PHP_AUTH_USER'] = 'Test user';
885 $_SERVER['PHP_AUTH_PW'] = 'password';
886 $_SESSION['allow_api'] = false;
887 $this->assertFalse(api_get_user($this->app));
891 * Test the api_get_user() function with an user ID in a GET parameter.
895 public function testApiGetUserWithGetId()
897 $_GET['user_id'] = $this->otherUser['id'];
898 $this->assertOtherUser(api_get_user($this->app));
902 * Test the api_get_user() function with a wrong user ID in a GET parameter.
905 * @expectedException Friendica\Network\HTTPException\BadRequestException
907 public function testApiGetUserWithWrongGetId()
909 $_GET['user_id'] = $this->wrongUserId;
910 $this->assertOtherUser(api_get_user($this->app));
914 * Test the api_get_user() function with an user name in a GET parameter.
918 public function testApiGetUserWithGetName()
920 $_GET['screen_name'] = $this->selfUser['nick'];
921 $this->assertSelfUser(api_get_user($this->app));
925 * Test the api_get_user() function with a profile URL in a GET parameter.
929 public function testApiGetUserWithGetUrl()
931 $_GET['profileurl'] = $this->selfUser['nurl'];
932 $this->assertSelfUser(api_get_user($this->app));
936 * Test the api_get_user() function with an user ID in the API path.
940 public function testApiGetUserWithNumericCalledApi()
943 $called_api = ['api_path'];
944 $this->app->argv[1] = $this->otherUser['id'] . '.json';
945 $this->assertOtherUser(api_get_user($this->app));
949 * Test the api_get_user() function with the $called_api global variable.
953 public function testApiGetUserWithCalledApi()
956 $called_api = ['api', 'api_path'];
957 $this->assertSelfUser(api_get_user($this->app));
961 * Test the api_get_user() function with a valid user.
965 public function testApiGetUserWithCorrectUser()
967 $this->assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
971 * Test the api_get_user() function with a wrong user ID.
974 * @expectedException Friendica\Network\HTTPException\BadRequestException
976 public function testApiGetUserWithWrongUser()
978 $this->assertOtherUser(api_get_user($this->app, $this->wrongUserId));
982 * Test the api_get_user() function with a 0 user ID.
986 public function testApiGetUserWithZeroUser()
988 $this->assertSelfUser(api_get_user($this->app, 0));
992 * Test the api_item_get_user() function.
996 public function testApiItemGetUser()
998 $users = api_item_get_user($this->app, []);
999 $this->assertSelfUser($users[0]);
1003 * Test the api_item_get_user() function with a different item parent.
1007 public function testApiItemGetUserWithDifferentParent()
1009 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
1010 $this->assertSelfUser($users[0]);
1011 $this->assertEquals($users[0], $users[1]);
1015 * Test the api_walk_recursive() function.
1019 public function testApiWalkRecursive()
1022 $this->assertEquals(
1027 // Should we test this with a callback that actually does something?
1035 * Test the api_walk_recursive() function with an array.
1039 public function testApiWalkRecursiveWithArray()
1041 $array = [['item1'], ['item2']];
1042 $this->assertEquals(
1047 // Should we test this with a callback that actually does something?
1055 * Test the api_reformat_xml() function.
1059 public function testApiReformatXml()
1063 $this->assertTrue(api_reformat_xml($item, $key));
1064 $this->assertEquals('true', $item);
1068 * Test the api_reformat_xml() function with a statusnet_api key.
1072 public function testApiReformatXmlWithStatusnetKey()
1075 $key = 'statusnet_api';
1076 $this->assertTrue(api_reformat_xml($item, $key));
1077 $this->assertEquals('statusnet:api', $key);
1081 * Test the api_reformat_xml() function with a friendica_api key.
1085 public function testApiReformatXmlWithFriendicaKey()
1088 $key = 'friendica_api';
1089 $this->assertTrue(api_reformat_xml($item, $key));
1090 $this->assertEquals('friendica:api', $key);
1094 * Test the api_create_xml() function.
1098 public function testApiCreateXml()
1100 $this->assertEquals(
1101 '<?xml version="1.0"?>' . "\n" .
1102 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1103 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1104 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1105 ' <data>some_data</data>' . "\n" .
1106 '</root_element>' . "\n",
1107 api_create_xml(['data' => ['some_data']], 'root_element')
1112 * Test the api_create_xml() function without any XML namespace.
1116 public function testApiCreateXmlWithoutNamespaces()
1118 $this->assertEquals(
1119 '<?xml version="1.0"?>' . "\n" .
1121 ' <data>some_data</data>' . "\n" .
1123 api_create_xml(['data' => ['some_data']], 'ok')
1128 * Test the api_format_data() function.
1132 public function testApiFormatData()
1134 $data = ['some_data'];
1135 $this->assertEquals($data, api_format_data('root_element', 'json', $data));
1139 * Test the api_format_data() function with an XML result.
1143 public function testApiFormatDataWithXml()
1145 $this->assertEquals(
1146 '<?xml version="1.0"?>' . "\n" .
1147 '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1148 'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1149 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1150 ' <data>some_data</data>' . "\n" .
1151 '</root_element>' . "\n",
1152 api_format_data('root_element', 'xml', ['data' => ['some_data']])
1157 * Test the api_account_verify_credentials() function.
1161 public function testApiAccountVerifyCredentials()
1163 $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
1167 * Test the api_account_verify_credentials() function without an authenticated user.
1170 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1172 public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1174 $_SESSION['authenticated'] = false;
1175 api_account_verify_credentials('json');
1179 * Test the requestdata() function.
1183 public function testRequestdata()
1185 $this->assertNull(requestdata('variable_name'));
1189 * Test the requestdata() function with a POST parameter.
1193 public function testRequestdataWithPost()
1195 $_POST['variable_name'] = 'variable_value';
1196 $this->assertEquals('variable_value', requestdata('variable_name'));
1200 * Test the requestdata() function with a GET parameter.
1204 public function testRequestdataWithGet()
1206 $_GET['variable_name'] = 'variable_value';
1207 $this->assertEquals('variable_value', requestdata('variable_name'));
1211 * Test the api_statuses_mediap() function.
1215 public function testApiStatusesMediap()
1217 $this->app->argc = 2;
1225 'tmp_name' => $this->getTempImage(),
1226 'name' => 'spacer.png',
1227 'type' => 'image/png'
1230 $_GET['status'] = '<b>Status content</b>';
1232 $result = api_statuses_mediap('json');
1233 $this->assertStatus($result['status']);
1237 * Test the api_statuses_mediap() function without an authenticated user.
1240 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1242 public function testApiStatusesMediapWithoutAuthenticatedUser()
1244 $_SESSION['authenticated'] = false;
1245 api_statuses_mediap('json');
1249 * Test the api_statuses_update() function.
1253 public function testApiStatusesUpdate()
1255 $_GET['status'] = 'Status content #friendica';
1256 $_GET['in_reply_to_status_id'] = -1;
1265 'tmp_name' => $this->getTempImage(),
1266 'name' => 'spacer.png',
1267 'type' => 'image/png'
1271 $result = api_statuses_update('json');
1272 $this->assertStatus($result['status']);
1276 * Test the api_statuses_update() function with an HTML status.
1280 public function testApiStatusesUpdateWithHtml()
1282 $_GET['htmlstatus'] = '<b>Status content</b>';
1284 $result = api_statuses_update('json');
1285 $this->assertStatus($result['status']);
1289 * Test the api_statuses_update() function without an authenticated user.
1292 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1294 public function testApiStatusesUpdateWithoutAuthenticatedUser()
1296 $_SESSION['authenticated'] = false;
1297 api_statuses_update('json');
1301 * Test the api_statuses_update() function with a parent status.
1305 public function testApiStatusesUpdateWithParent()
1307 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1311 * Test the api_statuses_update() function with a media_ids parameter.
1315 public function testApiStatusesUpdateWithMediaIds()
1317 $this->markTestIncomplete();
1321 * Test the api_statuses_update() function with the throttle limit reached.
1325 public function testApiStatusesUpdateWithDayThrottleReached()
1327 $this->markTestIncomplete();
1331 * Test the api_media_upload() function.
1334 * @expectedException Friendica\Network\HTTPException\BadRequestException
1336 public function testApiMediaUpload()
1342 * Test the api_media_upload() function without an authenticated user.
1345 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1347 public function testApiMediaUploadWithoutAuthenticatedUser()
1349 $_SESSION['authenticated'] = false;
1354 * Test the api_media_upload() function with an invalid uploaded media.
1357 * @expectedException Friendica\Network\HTTPException\InternalServerErrorException
1359 public function testApiMediaUploadWithMedia()
1364 'tmp_name' => 'tmp_name'
1371 * Test the api_media_upload() function with an valid uploaded media.
1375 public function testApiMediaUploadWithValidMedia()
1383 'tmp_name' => $this->getTempImage(),
1384 'name' => 'spacer.png',
1385 'type' => 'image/png'
1391 $result = api_media_upload();
1392 $this->assertEquals('image/png', $result['media']['image']['image_type']);
1393 $this->assertEquals(1, $result['media']['image']['w']);
1394 $this->assertEquals(1, $result['media']['image']['h']);
1395 $this->assertNotEmpty($result['media']['image']['friendica_preview_url']);
1399 * Test the api_status_show() function.
1401 public function testApiStatusShowWithJson()
1403 $result = api_status_show('json', 1);
1404 $this->assertStatus($result['status']);
1408 * Test the api_status_show() function with an XML result.
1410 public function testApiStatusShowWithXml()
1412 $result = api_status_show('xml', 1);
1413 $this->assertXml($result, 'statuses');
1417 * Test the api_get_last_status() function
1419 public function testApiGetLastStatus()
1421 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1423 $this->assertNotNull($item);
1427 * Test the api_users_show() function.
1431 public function testApiUsersShow()
1433 $result = api_users_show('json');
1434 // We can't use assertSelfUser() here because the user object is missing some properties.
1435 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
1436 $this->assertEquals('DFRN', $result['user']['location']);
1437 $this->assertEquals($this->selfUser['name'], $result['user']['name']);
1438 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1439 $this->assertEquals('dfrn', $result['user']['network']);
1440 $this->assertTrue($result['user']['verified']);
1444 * Test the api_users_show() function with an XML result.
1448 public function testApiUsersShowWithXml()
1450 $result = api_users_show('xml');
1451 $this->assertXml($result, 'statuses');
1455 * Test the api_users_search() function.
1459 public function testApiUsersSearch()
1461 $_GET['q'] = 'othercontact';
1462 $result = api_users_search('json');
1463 $this->assertOtherUser($result['users'][0]);
1467 * Test the api_users_search() function with an XML result.
1471 public function testApiUsersSearchWithXml()
1473 $_GET['q'] = 'othercontact';
1474 $result = api_users_search('xml');
1475 $this->assertXml($result, 'users');
1479 * Test the api_users_search() function without a GET q parameter.
1482 * @expectedException Friendica\Network\HTTPException\BadRequestException
1484 public function testApiUsersSearchWithoutQuery()
1486 api_users_search('json');
1490 * Test the api_users_lookup() function.
1493 * @expectedException Friendica\Network\HTTPException\NotFoundException
1495 public function testApiUsersLookup()
1497 api_users_lookup('json');
1501 * Test the api_users_lookup() function with an user ID.
1505 public function testApiUsersLookupWithUserId()
1507 $_REQUEST['user_id'] = $this->otherUser['id'];
1508 $result = api_users_lookup('json');
1509 $this->assertOtherUser($result['users'][0]);
1513 * Test the api_search() function.
1517 public function testApiSearch()
1519 $_REQUEST['q'] = 'reply';
1520 $_REQUEST['max_id'] = 10;
1521 $result = api_search('json');
1522 foreach ($result['status'] as $status) {
1523 $this->assertStatus($status);
1524 $this->assertContains('reply', $status['text'], null, true);
1529 * Test the api_search() function a count parameter.
1533 public function testApiSearchWithCount()
1535 $_REQUEST['q'] = 'reply';
1536 $_REQUEST['count'] = 20;
1537 $result = api_search('json');
1538 foreach ($result['status'] as $status) {
1539 $this->assertStatus($status);
1540 $this->assertContains('reply', $status['text'], null, true);
1545 * Test the api_search() function with an rpp parameter.
1549 public function testApiSearchWithRpp()
1551 $_REQUEST['q'] = 'reply';
1552 $_REQUEST['rpp'] = 20;
1553 $result = api_search('json');
1554 foreach ($result['status'] as $status) {
1555 $this->assertStatus($status);
1556 $this->assertContains('reply', $status['text'], null, true);
1561 * Test the api_search() function with an q parameter contains hashtag.
1565 public function testApiSearchWithHashtag()
1567 $_REQUEST['q'] = '%23friendica';
1568 $result = api_search('json');
1569 foreach ($result['status'] as $status) {
1570 $this->assertStatus($status);
1571 $this->assertContains('#friendica', $status['text'], null, true);
1576 * Test the api_search() function with an exclude_replies parameter.
1580 public function testApiSearchWithExcludeReplies()
1582 $_REQUEST['max_id'] = 10;
1583 $_REQUEST['exclude_replies'] = true;
1584 $_REQUEST['q'] = 'friendica';
1585 $result = api_search('json');
1586 foreach ($result['status'] as $status) {
1587 $this->assertStatus($status);
1592 * Test the api_search() function without an authenticated user.
1595 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1597 public function testApiSearchWithUnallowedUser()
1599 $_SESSION['allow_api'] = false;
1600 $_GET['screen_name'] = $this->selfUser['nick'];
1605 * Test the api_search() function without any GET query parameter.
1608 * @expectedException Friendica\Network\HTTPException\BadRequestException
1610 public function testApiSearchWithoutQuery()
1616 * Test the api_statuses_home_timeline() function.
1620 public function testApiStatusesHomeTimeline()
1622 $_REQUEST['max_id'] = 10;
1623 $_REQUEST['exclude_replies'] = true;
1624 $_REQUEST['conversation_id'] = 1;
1625 $result = api_statuses_home_timeline('json');
1626 $this->assertNotEmpty($result['status']);
1627 foreach ($result['status'] as $status) {
1628 $this->assertStatus($status);
1633 * Test the api_statuses_home_timeline() function with a negative page parameter.
1637 public function testApiStatusesHomeTimelineWithNegativePage()
1639 $_REQUEST['page'] = -2;
1640 $result = api_statuses_home_timeline('json');
1641 $this->assertNotEmpty($result['status']);
1642 foreach ($result['status'] as $status) {
1643 $this->assertStatus($status);
1648 * Test the api_statuses_home_timeline() with an unallowed user.
1651 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1653 public function testApiStatusesHomeTimelineWithUnallowedUser()
1655 $_SESSION['allow_api'] = false;
1656 $_GET['screen_name'] = $this->selfUser['nick'];
1657 api_statuses_home_timeline('json');
1661 * Test the api_statuses_home_timeline() function with an RSS result.
1665 public function testApiStatusesHomeTimelineWithRss()
1667 $result = api_statuses_home_timeline('rss');
1668 $this->assertXml($result, 'statuses');
1672 * Test the api_statuses_public_timeline() function.
1676 public function testApiStatusesPublicTimeline()
1678 $_REQUEST['max_id'] = 10;
1679 $_REQUEST['conversation_id'] = 1;
1680 $result = api_statuses_public_timeline('json');
1681 $this->assertNotEmpty($result['status']);
1682 foreach ($result['status'] as $status) {
1683 $this->assertStatus($status);
1688 * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1692 public function testApiStatusesPublicTimelineWithExcludeReplies()
1694 $_REQUEST['max_id'] = 10;
1695 $_REQUEST['exclude_replies'] = true;
1696 $result = api_statuses_public_timeline('json');
1697 $this->assertNotEmpty($result['status']);
1698 foreach ($result['status'] as $status) {
1699 $this->assertStatus($status);
1704 * Test the api_statuses_public_timeline() function with a negative page parameter.
1708 public function testApiStatusesPublicTimelineWithNegativePage()
1710 $_REQUEST['page'] = -2;
1711 $result = api_statuses_public_timeline('json');
1712 $this->assertNotEmpty($result['status']);
1713 foreach ($result['status'] as $status) {
1714 $this->assertStatus($status);
1719 * Test the api_statuses_public_timeline() function with an unallowed user.
1722 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1724 public function testApiStatusesPublicTimelineWithUnallowedUser()
1726 $_SESSION['allow_api'] = false;
1727 $_GET['screen_name'] = $this->selfUser['nick'];
1728 api_statuses_public_timeline('json');
1732 * Test the api_statuses_public_timeline() function with an RSS result.
1736 public function testApiStatusesPublicTimelineWithRss()
1738 $result = api_statuses_public_timeline('rss');
1739 $this->assertXml($result, 'statuses');
1743 * Test the api_statuses_networkpublic_timeline() function.
1747 public function testApiStatusesNetworkpublicTimeline()
1749 $_REQUEST['max_id'] = 10;
1750 $result = api_statuses_networkpublic_timeline('json');
1751 $this->assertNotEmpty($result['status']);
1752 foreach ($result['status'] as $status) {
1753 $this->assertStatus($status);
1758 * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1762 public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1764 $_REQUEST['page'] = -2;
1765 $result = api_statuses_networkpublic_timeline('json');
1766 $this->assertNotEmpty($result['status']);
1767 foreach ($result['status'] as $status) {
1768 $this->assertStatus($status);
1773 * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1776 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1778 public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1780 $_SESSION['allow_api'] = false;
1781 $_GET['screen_name'] = $this->selfUser['nick'];
1782 api_statuses_networkpublic_timeline('json');
1786 * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1790 public function testApiStatusesNetworkpublicTimelineWithRss()
1792 $result = api_statuses_networkpublic_timeline('rss');
1793 $this->assertXml($result, 'statuses');
1797 * Test the api_statuses_show() function.
1800 * @expectedException Friendica\Network\HTTPException\BadRequestException
1802 public function testApiStatusesShow()
1804 api_statuses_show('json');
1808 * Test the api_statuses_show() function with an ID.
1812 public function testApiStatusesShowWithId()
1814 $this->app->argv[3] = 1;
1815 $result = api_statuses_show('json');
1816 $this->assertStatus($result['status']);
1820 * Test the api_statuses_show() function with the conversation parameter.
1824 public function testApiStatusesShowWithConversation()
1826 $this->app->argv[3] = 1;
1827 $_REQUEST['conversation'] = 1;
1828 $result = api_statuses_show('json');
1829 $this->assertNotEmpty($result['status']);
1830 foreach ($result['status'] as $status) {
1831 $this->assertStatus($status);
1836 * Test the api_statuses_show() function with an unallowed user.
1839 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1841 public function testApiStatusesShowWithUnallowedUser()
1843 $_SESSION['allow_api'] = false;
1844 $_GET['screen_name'] = $this->selfUser['nick'];
1845 api_statuses_show('json');
1849 * Test the api_conversation_show() function.
1852 * @expectedException Friendica\Network\HTTPException\BadRequestException
1854 public function testApiConversationShow()
1856 api_conversation_show('json');
1860 * Test the api_conversation_show() function with an ID.
1864 public function testApiConversationShowWithId()
1866 $this->app->argv[3] = 1;
1867 $_REQUEST['max_id'] = 10;
1868 $_REQUEST['page'] = -2;
1869 $result = api_conversation_show('json');
1870 $this->assertNotEmpty($result['status']);
1871 foreach ($result['status'] as $status) {
1872 $this->assertStatus($status);
1877 * Test the api_conversation_show() function with an unallowed user.
1880 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1882 public function testApiConversationShowWithUnallowedUser()
1884 $_SESSION['allow_api'] = false;
1885 $_GET['screen_name'] = $this->selfUser['nick'];
1886 api_conversation_show('json');
1890 * Test the api_statuses_repeat() function.
1893 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1895 public function testApiStatusesRepeat()
1897 api_statuses_repeat('json');
1901 * Test the api_statuses_repeat() function without an authenticated user.
1904 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1906 public function testApiStatusesRepeatWithoutAuthenticatedUser()
1908 $_SESSION['authenticated'] = false;
1909 api_statuses_repeat('json');
1913 * Test the api_statuses_repeat() function with an ID.
1917 public function testApiStatusesRepeatWithId()
1919 $this->app->argv[3] = 1;
1920 $result = api_statuses_repeat('json');
1921 $this->assertStatus($result['status']);
1923 // Also test with a shared status
1924 $this->app->argv[3] = 5;
1925 $result = api_statuses_repeat('json');
1926 $this->assertStatus($result['status']);
1930 * Test the api_statuses_destroy() function.
1933 * @expectedException Friendica\Network\HTTPException\BadRequestException
1935 public function testApiStatusesDestroy()
1937 api_statuses_destroy('json');
1941 * Test the api_statuses_destroy() function without an authenticated user.
1944 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1946 public function testApiStatusesDestroyWithoutAuthenticatedUser()
1948 $_SESSION['authenticated'] = false;
1949 api_statuses_destroy('json');
1953 * Test the api_statuses_destroy() function with an ID.
1957 public function testApiStatusesDestroyWithId()
1959 $this->app->argv[3] = 1;
1960 $result = api_statuses_destroy('json');
1961 $this->assertStatus($result['status']);
1965 * Test the api_statuses_mentions() function.
1969 public function testApiStatusesMentions()
1971 $this->app->user = ['nickname' => $this->selfUser['nick']];
1972 $_REQUEST['max_id'] = 10;
1973 $result = api_statuses_mentions('json');
1974 $this->assertEmpty($result['status']);
1975 // We should test with mentions in the database.
1979 * Test the api_statuses_mentions() function with a negative page parameter.
1983 public function testApiStatusesMentionsWithNegativePage()
1985 $_REQUEST['page'] = -2;
1986 $result = api_statuses_mentions('json');
1987 $this->assertEmpty($result['status']);
1991 * Test the api_statuses_mentions() function with an unallowed user.
1994 * @expectedException Friendica\Network\HTTPException\ForbiddenException
1996 public function testApiStatusesMentionsWithUnallowedUser()
1998 $_SESSION['allow_api'] = false;
1999 $_GET['screen_name'] = $this->selfUser['nick'];
2000 api_statuses_mentions('json');
2004 * Test the api_statuses_mentions() function with an RSS result.
2008 public function testApiStatusesMentionsWithRss()
2010 $result = api_statuses_mentions('rss');
2011 $this->assertXml($result, 'statuses');
2015 * Test the api_statuses_user_timeline() function.
2019 public function testApiStatusesUserTimeline()
2021 $_REQUEST['max_id'] = 10;
2022 $_REQUEST['exclude_replies'] = true;
2023 $_REQUEST['conversation_id'] = 1;
2024 $result = api_statuses_user_timeline('json');
2025 $this->assertNotEmpty($result['status']);
2026 foreach ($result['status'] as $status) {
2027 $this->assertStatus($status);
2032 * Test the api_statuses_user_timeline() function with a negative page parameter.
2036 public function testApiStatusesUserTimelineWithNegativePage()
2038 $_REQUEST['page'] = -2;
2039 $result = api_statuses_user_timeline('json');
2040 $this->assertNotEmpty($result['status']);
2041 foreach ($result['status'] as $status) {
2042 $this->assertStatus($status);
2047 * Test the api_statuses_user_timeline() function with an RSS result.
2051 public function testApiStatusesUserTimelineWithRss()
2053 $result = api_statuses_user_timeline('rss');
2054 $this->assertXml($result, 'statuses');
2058 * Test the api_statuses_user_timeline() function with an unallowed user.
2061 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2063 public function testApiStatusesUserTimelineWithUnallowedUser()
2065 $_SESSION['allow_api'] = false;
2066 $_GET['screen_name'] = $this->selfUser['nick'];
2067 api_statuses_user_timeline('json');
2071 * Test the api_favorites_create_destroy() function.
2074 * @expectedException Friendica\Network\HTTPException\BadRequestException
2076 public function testApiFavoritesCreateDestroy()
2078 $this->app->argv = ['api', '1.1', 'favorites', 'create'];
2079 $this->app->argc = count($this->app->argv);
2080 api_favorites_create_destroy('json');
2084 * Test the api_favorites_create_destroy() function with an invalid ID.
2087 * @expectedException Friendica\Network\HTTPException\BadRequestException
2089 public function testApiFavoritesCreateDestroyWithInvalidId()
2091 $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json'];
2092 $this->app->argc = count($this->app->argv);
2093 api_favorites_create_destroy('json');
2097 * Test the api_favorites_create_destroy() function with an invalid action.
2100 * @expectedException Friendica\Network\HTTPException\BadRequestException
2102 public function testApiFavoritesCreateDestroyWithInvalidAction()
2104 $this->app->argv = ['api', '1.1', 'favorites', 'change.json'];
2105 $this->app->argc = count($this->app->argv);
2106 $_REQUEST['id'] = 1;
2107 api_favorites_create_destroy('json');
2111 * Test the api_favorites_create_destroy() function with the create action.
2115 public function testApiFavoritesCreateDestroyWithCreateAction()
2117 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2118 $this->app->argc = count($this->app->argv);
2119 $_REQUEST['id'] = 3;
2120 $result = api_favorites_create_destroy('json');
2121 $this->assertStatus($result['status']);
2125 * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2129 public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2131 $this->app->argv = ['api', '1.1', 'favorites', 'create.rss'];
2132 $this->app->argc = count($this->app->argv);
2133 $_REQUEST['id'] = 3;
2134 $result = api_favorites_create_destroy('rss');
2135 $this->assertXml($result, 'status');
2139 * Test the api_favorites_create_destroy() function with the destroy action.
2143 public function testApiFavoritesCreateDestroyWithDestroyAction()
2145 $this->app->argv = ['api', '1.1', 'favorites', 'destroy.json'];
2146 $this->app->argc = count($this->app->argv);
2147 $_REQUEST['id'] = 3;
2148 $result = api_favorites_create_destroy('json');
2149 $this->assertStatus($result['status']);
2153 * Test the api_favorites_create_destroy() function without an authenticated user.
2156 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2158 public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2160 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2161 $this->app->argc = count($this->app->argv);
2162 $_SESSION['authenticated'] = false;
2163 api_favorites_create_destroy('json');
2167 * Test the api_favorites() function.
2171 public function testApiFavorites()
2173 $_REQUEST['page'] = -1;
2174 $_REQUEST['max_id'] = 10;
2175 $result = api_favorites('json');
2176 foreach ($result['status'] as $status) {
2177 $this->assertStatus($status);
2182 * Test the api_favorites() function with an RSS result.
2186 public function testApiFavoritesWithRss()
2188 $result = api_favorites('rss');
2189 $this->assertXml($result, 'statuses');
2193 * Test the api_favorites() function with an unallowed user.
2196 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2198 public function testApiFavoritesWithUnallowedUser()
2200 $_SESSION['allow_api'] = false;
2201 $_GET['screen_name'] = $this->selfUser['nick'];
2202 api_favorites('json');
2206 * Test the api_format_messages() function.
2210 public function testApiFormatMessages()
2212 $result = api_format_messages(
2213 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2214 ['id' => 2, 'screen_name' => 'recipient_name'],
2215 ['id' => 3, 'screen_name' => 'sender_name']
2217 $this->assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2218 $this->assertEquals(1, $result['id']);
2219 $this->assertEquals(2, $result['recipient_id']);
2220 $this->assertEquals(3, $result['sender_id']);
2221 $this->assertEquals('recipient_name', $result['recipient_screen_name']);
2222 $this->assertEquals('sender_name', $result['sender_screen_name']);
2226 * Test the api_format_messages() function with HTML.
2230 public function testApiFormatMessagesWithHtmlText()
2232 $_GET['getText'] = 'html';
2233 $result = api_format_messages(
2234 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2235 ['id' => 2, 'screen_name' => 'recipient_name'],
2236 ['id' => 3, 'screen_name' => 'sender_name']
2238 $this->assertEquals('item_title', $result['title']);
2239 $this->assertEquals('<strong>item_body</strong>', $result['text']);
2243 * Test the api_format_messages() function with plain text.
2247 public function testApiFormatMessagesWithPlainText()
2249 $_GET['getText'] = 'plain';
2250 $result = api_format_messages(
2251 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2252 ['id' => 2, 'screen_name' => 'recipient_name'],
2253 ['id' => 3, 'screen_name' => 'sender_name']
2255 $this->assertEquals('item_title', $result['title']);
2256 $this->assertEquals('item_body', $result['text']);
2260 * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2264 public function testApiFormatMessagesWithoutUserObjects()
2266 $_GET['getUserObjects'] = 'false';
2267 $result = api_format_messages(
2268 ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2269 ['id' => 2, 'screen_name' => 'recipient_name'],
2270 ['id' => 3, 'screen_name' => 'sender_name']
2272 $this->assertTrue(!isset($result['sender']));
2273 $this->assertTrue(!isset($result['recipient']));
2277 * Test the api_convert_item() function.
2281 public function testApiConvertItem()
2283 $result = api_convert_item(
2285 'network' => 'feed',
2286 'title' => 'item_title',
2287 // We need a long string to test that it is correctly cut
2288 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2289 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2290 'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2291 'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2292 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2293 'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2294 'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2295 'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2296 'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2297 'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2298 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2299 'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2300 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2301 'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2302 'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2303 'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2304 'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2305 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2306 'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2307 'repellat officia illum quos impedit quam iste esse unde qui ' .
2308 'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2309 'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2310 'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2311 'plink' => 'item_plink'
2314 $this->assertStringStartsWith('item_title', $result['text']);
2315 $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2319 * Test the api_convert_item() function with an empty item body.
2323 public function testApiConvertItemWithoutBody()
2325 $result = api_convert_item(
2327 'network' => 'feed',
2328 'title' => 'item_title',
2330 'plink' => 'item_plink'
2333 $this->assertEquals('item_title', $result['text']);
2334 $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2338 * Test the api_convert_item() function with the title in the body.
2342 public function testApiConvertItemWithTitleInBody()
2344 $result = api_convert_item(
2346 'title' => 'item_title',
2347 'body' => 'item_title item_body'
2350 $this->assertEquals('item_title item_body', $result['text']);
2351 $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2355 * Test the api_get_attachments() function.
2359 public function testApiGetAttachments()
2362 $this->assertEmpty(api_get_attachments($body));
2366 * Test the api_get_attachments() function with an img tag.
2370 public function testApiGetAttachmentsWithImage()
2372 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2373 $this->assertInternalType('array', api_get_attachments($body));
2377 * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2381 public function testApiGetAttachmentsWithImageAndAndStatus()
2383 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2384 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2385 $this->assertInternalType('array', api_get_attachments($body));
2389 * Test the api_get_entitities() function.
2393 public function testApiGetEntitities()
2396 $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
2400 * Test the api_get_entitities() function with the include_entities parameter.
2404 public function testApiGetEntititiesWithIncludeEntities()
2406 $_REQUEST['include_entities'] = 'true';
2408 $result = api_get_entitities($text, 'bbcode');
2409 $this->assertInternalType('array', $result['hashtags']);
2410 $this->assertInternalType('array', $result['symbols']);
2411 $this->assertInternalType('array', $result['urls']);
2412 $this->assertInternalType('array', $result['user_mentions']);
2416 * Test the api_format_items_embeded_images() function.
2420 public function testApiFormatItemsEmbededImages()
2422 $this->assertEquals(
2423 'text ' . DI::baseUrl() . '/display/item_guid',
2424 api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2429 * Test the api_contactlink_to_array() function.
2433 public function testApiContactlinkToArray()
2435 $this->assertEquals(
2440 api_contactlink_to_array('text')
2445 * Test the api_contactlink_to_array() function with an URL.
2449 public function testApiContactlinkToArrayWithUrl()
2451 $this->assertEquals(
2453 'name' => ['link_text'],
2456 api_contactlink_to_array('text <a href="url">link_text</a>')
2461 * Test the api_format_items_activities() function.
2465 public function testApiFormatItemsActivities()
2467 $item = ['uid' => 0, 'uri' => ''];
2468 $result = api_format_items_activities($item);
2469 $this->assertArrayHasKey('like', $result);
2470 $this->assertArrayHasKey('dislike', $result);
2471 $this->assertArrayHasKey('attendyes', $result);
2472 $this->assertArrayHasKey('attendno', $result);
2473 $this->assertArrayHasKey('attendmaybe', $result);
2477 * Test the api_format_items_activities() function with an XML result.
2481 public function testApiFormatItemsActivitiesWithXml()
2483 $item = ['uid' => 0, 'uri' => ''];
2484 $result = api_format_items_activities($item, 'xml');
2485 $this->assertArrayHasKey('friendica:like', $result);
2486 $this->assertArrayHasKey('friendica:dislike', $result);
2487 $this->assertArrayHasKey('friendica:attendyes', $result);
2488 $this->assertArrayHasKey('friendica:attendno', $result);
2489 $this->assertArrayHasKey('friendica:attendmaybe', $result);
2493 * Test the api_format_items_profiles() function.
2497 public function testApiFormatItemsProfiles()
2500 'id' => 'profile_id',
2501 'profile-name' => 'profile_name',
2502 'is-default' => true,
2503 'hide-friends' => true,
2504 'photo' => 'profile_photo',
2505 'thumb' => 'profile_thumb',
2507 'net-publish' => true,
2508 'pdesc' => 'description',
2509 'dob' => 'date_of_birth',
2510 'address' => 'address',
2511 'locality' => 'city',
2512 'region' => 'region',
2513 'postal-code' => 'postal_code',
2514 'country-name' => 'country',
2515 'hometown' => 'hometown',
2516 'gender' => 'gender',
2517 'marital' => 'marital',
2518 'with' => 'marital_with',
2519 'howlong' => 'marital_since',
2520 'sexual' => 'sexual',
2521 'politic' => 'politic',
2522 'religion' => 'religion',
2523 'pub_keywords' => 'public_keywords',
2524 'prv_keywords' => 'private_keywords',
2527 'dislikes' => 'dislikes',
2533 'interest' => 'interest',
2534 'romance' => 'romance',
2536 'education' => 'education',
2537 'contact' => 'social_networks',
2538 'homepage' => 'homepage'
2540 $result = api_format_items_profiles($profile_row);
2541 $this->assertEquals(
2543 'profile_id' => 'profile_id',
2544 'profile_name' => 'profile_name',
2545 'is_default' => true,
2546 'hide_friends' => true,
2547 'profile_photo' => 'profile_photo',
2548 'profile_thumb' => 'profile_thumb',
2550 'net_publish' => true,
2551 'description' => 'description',
2552 'date_of_birth' => 'date_of_birth',
2553 'address' => 'address',
2555 'region' => 'region',
2556 'postal_code' => 'postal_code',
2557 'country' => 'country',
2558 'hometown' => 'hometown',
2559 'gender' => 'gender',
2560 'marital' => 'marital',
2561 'marital_with' => 'marital_with',
2562 'marital_since' => 'marital_since',
2563 'sexual' => 'sexual',
2564 'politic' => 'politic',
2565 'religion' => 'religion',
2566 'public_keywords' => 'public_keywords',
2567 'private_keywords' => 'private_keywords',
2570 'dislikes' => 'dislikes',
2576 'interest' => 'interest',
2577 'romance' => 'romance',
2579 'education' => 'education',
2580 'social_networks' => 'social_networks',
2581 'homepage' => 'homepage',
2589 * Test the api_format_items() function.
2593 public function testApiFormatItems()
2597 'item_network' => 'item_network',
2603 'author-network' => Protocol::DFRN,
2604 'author-link' => 'http://localhost/profile/othercontact',
2608 $result = api_format_items($items, ['id' => 0], true);
2609 foreach ($result as $status) {
2610 $this->assertStatus($status);
2615 * Test the api_format_items() function with an XML result.
2619 public function testApiFormatItemsWithXml()
2627 'author-network' => Protocol::DFRN,
2628 'author-link' => 'http://localhost/profile/othercontact',
2632 $result = api_format_items($items, ['id' => 0], true, 'xml');
2633 foreach ($result as $status) {
2634 $this->assertStatus($status);
2639 * Test the api_format_items() function.
2643 public function testApiAccountRateLimitStatus()
2645 $result = api_account_rate_limit_status('json');
2646 $this->assertEquals(150, $result['hash']['remaining_hits']);
2647 $this->assertEquals(150, $result['hash']['hourly_limit']);
2648 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2652 * Test the api_format_items() function with an XML result.
2656 public function testApiAccountRateLimitStatusWithXml()
2658 $result = api_account_rate_limit_status('xml');
2659 $this->assertXml($result, 'hash');
2663 * Test the api_help_test() function.
2667 public function testApiHelpTest()
2669 $result = api_help_test('json');
2670 $this->assertEquals(['ok' => 'ok'], $result);
2674 * Test the api_help_test() function with an XML result.
2678 public function testApiHelpTestWithXml()
2680 $result = api_help_test('xml');
2681 $this->assertXml($result, 'ok');
2685 * Test the api_lists_list() function.
2689 public function testApiListsList()
2691 $result = api_lists_list('json');
2692 $this->assertEquals(['lists_list' => []], $result);
2696 * Test the api_lists_ownerships() function.
2700 public function testApiListsOwnerships()
2702 $result = api_lists_ownerships('json');
2703 foreach ($result['lists']['lists'] as $list) {
2704 $this->assertList($list);
2709 * Test the api_lists_ownerships() function without an authenticated user.
2712 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2714 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2716 $_SESSION['authenticated'] = false;
2717 api_lists_ownerships('json');
2721 * Test the api_lists_statuses() function.
2723 * @expectedException Friendica\Network\HTTPException\BadRequestException
2726 public function testApiListsStatuses()
2728 api_lists_statuses('json');
2732 * Test the api_lists_statuses() function with a list ID.
2736 public function testApiListsStatusesWithListId()
2738 $_REQUEST['list_id'] = 1;
2739 $_REQUEST['page'] = -1;
2740 $_REQUEST['max_id'] = 10;
2741 $result = api_lists_statuses('json');
2742 foreach ($result['status'] as $status) {
2743 $this->assertStatus($status);
2748 * Test the api_lists_statuses() function with a list ID and a RSS result.
2752 public function testApiListsStatusesWithListIdAndRss()
2754 $_REQUEST['list_id'] = 1;
2755 $result = api_lists_statuses('rss');
2756 $this->assertXml($result, 'statuses');
2760 * Test the api_lists_statuses() function with an unallowed user.
2763 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2765 public function testApiListsStatusesWithUnallowedUser()
2767 $_SESSION['allow_api'] = false;
2768 $_GET['screen_name'] = $this->selfUser['nick'];
2769 api_lists_statuses('json');
2773 * Test the api_statuses_f() function.
2777 public function testApiStatusesFWithFriends()
2780 $result = api_statuses_f('friends');
2781 $this->assertArrayHasKey('user', $result);
2785 * Test the api_statuses_f() function.
2789 public function testApiStatusesFWithFollowers()
2791 $result = api_statuses_f('followers');
2792 $this->assertArrayHasKey('user', $result);
2796 * Test the api_statuses_f() function.
2800 public function testApiStatusesFWithBlocks()
2802 $result = api_statuses_f('blocks');
2803 $this->assertArrayHasKey('user', $result);
2807 * Test the api_statuses_f() function.
2811 public function testApiStatusesFWithIncoming()
2813 $result = api_statuses_f('incoming');
2814 $this->assertArrayHasKey('user', $result);
2818 * Test the api_statuses_f() function an undefined cursor GET variable.
2822 public function testApiStatusesFWithUndefinedCursor()
2824 $_GET['cursor'] = 'undefined';
2825 $this->assertFalse(api_statuses_f('friends'));
2829 * Test the api_statuses_friends() function.
2833 public function testApiStatusesFriends()
2835 $result = api_statuses_friends('json');
2836 $this->assertArrayHasKey('user', $result);
2840 * Test the api_statuses_friends() function an undefined cursor GET variable.
2844 public function testApiStatusesFriendsWithUndefinedCursor()
2846 $_GET['cursor'] = 'undefined';
2847 $this->assertFalse(api_statuses_friends('json'));
2851 * Test the api_statuses_followers() function.
2855 public function testApiStatusesFollowers()
2857 $result = api_statuses_followers('json');
2858 $this->assertArrayHasKey('user', $result);
2862 * Test the api_statuses_followers() function an undefined cursor GET variable.
2866 public function testApiStatusesFollowersWithUndefinedCursor()
2868 $_GET['cursor'] = 'undefined';
2869 $this->assertFalse(api_statuses_followers('json'));
2873 * Test the api_blocks_list() function.
2877 public function testApiBlocksList()
2879 $result = api_blocks_list('json');
2880 $this->assertArrayHasKey('user', $result);
2884 * Test the api_blocks_list() function an undefined cursor GET variable.
2888 public function testApiBlocksListWithUndefinedCursor()
2890 $_GET['cursor'] = 'undefined';
2891 $this->assertFalse(api_blocks_list('json'));
2895 * Test the api_friendships_incoming() function.
2899 public function testApiFriendshipsIncoming()
2901 $result = api_friendships_incoming('json');
2902 $this->assertArrayHasKey('id', $result);
2906 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2910 public function testApiFriendshipsIncomingWithUndefinedCursor()
2912 $_GET['cursor'] = 'undefined';
2913 $this->assertFalse(api_friendships_incoming('json'));
2917 * Test the api_statusnet_config() function.
2921 public function testApiStatusnetConfig()
2923 $result = api_statusnet_config('json');
2924 $this->assertEquals('localhost', $result['config']['site']['server']);
2925 $this->assertEquals('default', $result['config']['site']['theme']);
2926 $this->assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2927 $this->assertTrue($result['config']['site']['fancy']);
2928 $this->assertEquals('en', $result['config']['site']['language']);
2929 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2930 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2931 $this->assertEquals('false', $result['config']['site']['private']);
2932 $this->assertEquals('false', $result['config']['site']['ssl']);
2933 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2937 * Test the api_statusnet_version() function.
2941 public function testApiStatusnetVersion()
2943 $result = api_statusnet_version('json');
2944 $this->assertEquals('0.9.7', $result['version']);
2948 * Test the api_ff_ids() function.
2952 public function testApiFfIds()
2954 $result = api_ff_ids('json', Contact::FOLLOWER);
2955 $this->assertEquals(['id' => []], $result);
2959 * Test the api_ff_ids() function with a result.
2963 public function testApiFfIdsWithResult()
2965 $this->markTestIncomplete();
2969 * Test the api_ff_ids() function without an authenticated user.
2972 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2974 public function testApiFfIdsWithoutAuthenticatedUser()
2976 $_SESSION['authenticated'] = false;
2977 api_ff_ids('json', Contact::FOLLOWER);
2981 * Test the api_friends_ids() function.
2985 public function testApiFriendsIds()
2987 $result = api_friends_ids('json');
2988 $this->assertEquals(['id' => []], $result);
2992 * Test the api_followers_ids() function.
2996 public function testApiFollowersIds()
2998 $result = api_followers_ids('json');
2999 $this->assertEquals(['id' => []], $result);
3003 * Test the api_direct_messages_new() function.
3007 public function testApiDirectMessagesNew()
3009 $result = api_direct_messages_new('json');
3010 $this->assertNull($result);
3014 * Test the api_direct_messages_new() function without an authenticated user.
3017 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3019 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
3021 $_SESSION['authenticated'] = false;
3022 api_direct_messages_new('json');
3026 * Test the api_direct_messages_new() function with an user ID.
3030 public function testApiDirectMessagesNewWithUserId()
3032 $_POST['text'] = 'message_text';
3033 $_POST['user_id'] = $this->otherUser['id'];
3034 $result = api_direct_messages_new('json');
3035 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
3039 * Test the api_direct_messages_new() function with a screen name.
3043 public function testApiDirectMessagesNewWithScreenName()
3045 $_POST['text'] = 'message_text';
3046 $_POST['screen_name'] = $this->friendUser['nick'];
3047 $result = api_direct_messages_new('json');
3048 $this->assertContains('message_text', $result['direct_message']['text']);
3049 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3050 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3054 * Test the api_direct_messages_new() function with a title.
3058 public function testApiDirectMessagesNewWithTitle()
3060 $_POST['text'] = 'message_text';
3061 $_POST['screen_name'] = $this->friendUser['nick'];
3062 $_REQUEST['title'] = 'message_title';
3063 $result = api_direct_messages_new('json');
3064 $this->assertContains('message_text', $result['direct_message']['text']);
3065 $this->assertContains('message_title', $result['direct_message']['text']);
3066 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
3067 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
3071 * Test the api_direct_messages_new() function with an RSS result.
3075 public function testApiDirectMessagesNewWithRss()
3077 $_POST['text'] = 'message_text';
3078 $_POST['screen_name'] = $this->friendUser['nick'];
3079 $result = api_direct_messages_new('rss');
3080 $this->assertXml($result, 'direct-messages');
3084 * Test the api_direct_messages_destroy() function.
3087 * @expectedException Friendica\Network\HTTPException\BadRequestException
3089 public function testApiDirectMessagesDestroy()
3091 api_direct_messages_destroy('json');
3095 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
3099 public function testApiDirectMessagesDestroyWithVerbose()
3101 $_GET['friendica_verbose'] = 'true';
3102 $result = api_direct_messages_destroy('json');
3103 $this->assertEquals(
3106 'result' => 'error',
3107 'message' => 'message id or parenturi not specified'
3115 * Test the api_direct_messages_destroy() function without an authenticated user.
3118 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3120 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
3122 $_SESSION['authenticated'] = false;
3123 api_direct_messages_destroy('json');
3127 * Test the api_direct_messages_destroy() function with a non-zero ID.
3130 * @expectedException Friendica\Network\HTTPException\BadRequestException
3132 public function testApiDirectMessagesDestroyWithId()
3134 $_REQUEST['id'] = 1;
3135 api_direct_messages_destroy('json');
3139 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
3143 public function testApiDirectMessagesDestroyWithIdAndVerbose()
3145 $_REQUEST['id'] = 1;
3146 $_REQUEST['friendica_parenturi'] = 'parent_uri';
3147 $_GET['friendica_verbose'] = 'true';
3148 $result = api_direct_messages_destroy('json');
3149 $this->assertEquals(
3152 'result' => 'error',
3153 'message' => 'message id not in database'
3161 * Test the api_direct_messages_destroy() function with a non-zero ID.
3165 public function testApiDirectMessagesDestroyWithCorrectId()
3167 $this->markTestIncomplete('We need to add a dataset for this.');
3171 * Test the api_direct_messages_box() function.
3175 public function testApiDirectMessagesBoxWithSentbox()
3177 $_REQUEST['page'] = -1;
3178 $_REQUEST['max_id'] = 10;
3179 $result = api_direct_messages_box('json', 'sentbox', 'false');
3180 $this->assertArrayHasKey('direct_message', $result);
3184 * Test the api_direct_messages_box() function.
3188 public function testApiDirectMessagesBoxWithConversation()
3190 $result = api_direct_messages_box('json', 'conversation', 'false');
3191 $this->assertArrayHasKey('direct_message', $result);
3195 * Test the api_direct_messages_box() function.
3199 public function testApiDirectMessagesBoxWithAll()
3201 $result = api_direct_messages_box('json', 'all', 'false');
3202 $this->assertArrayHasKey('direct_message', $result);
3206 * Test the api_direct_messages_box() function.
3210 public function testApiDirectMessagesBoxWithInbox()
3212 $result = api_direct_messages_box('json', 'inbox', 'false');
3213 $this->assertArrayHasKey('direct_message', $result);
3217 * Test the api_direct_messages_box() function.
3221 public function testApiDirectMessagesBoxWithVerbose()
3223 $result = api_direct_messages_box('json', 'sentbox', 'true');
3224 $this->assertEquals(
3227 'result' => 'error',
3228 'message' => 'no mails available'
3236 * Test the api_direct_messages_box() function with a RSS result.
3240 public function testApiDirectMessagesBoxWithRss()
3242 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3243 $this->assertXml($result, 'direct-messages');
3247 * Test the api_direct_messages_box() function without an authenticated user.
3250 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3252 public function testApiDirectMessagesBoxWithUnallowedUser()
3254 $_SESSION['allow_api'] = false;
3255 $_GET['screen_name'] = $this->selfUser['nick'];
3256 api_direct_messages_box('json', 'sentbox', 'false');
3260 * Test the api_direct_messages_sentbox() function.
3264 public function testApiDirectMessagesSentbox()
3266 $result = api_direct_messages_sentbox('json');
3267 $this->assertArrayHasKey('direct_message', $result);
3271 * Test the api_direct_messages_inbox() function.
3275 public function testApiDirectMessagesInbox()
3277 $result = api_direct_messages_inbox('json');
3278 $this->assertArrayHasKey('direct_message', $result);
3282 * Test the api_direct_messages_all() function.
3286 public function testApiDirectMessagesAll()
3288 $result = api_direct_messages_all('json');
3289 $this->assertArrayHasKey('direct_message', $result);
3293 * Test the api_direct_messages_conversation() function.
3297 public function testApiDirectMessagesConversation()
3299 $result = api_direct_messages_conversation('json');
3300 $this->assertArrayHasKey('direct_message', $result);
3304 * Test the api_oauth_request_token() function.
3308 public function testApiOauthRequestToken()
3310 $this->markTestIncomplete('exit() kills phpunit as well');
3314 * Test the api_oauth_access_token() function.
3318 public function testApiOauthAccessToken()
3320 $this->markTestIncomplete('exit() kills phpunit as well');
3324 * Test the api_fr_photoalbum_delete() function.
3327 * @expectedException Friendica\Network\HTTPException\BadRequestException
3329 public function testApiFrPhotoalbumDelete()
3331 api_fr_photoalbum_delete('json');
3335 * Test the api_fr_photoalbum_delete() function with an album name.
3338 * @expectedException Friendica\Network\HTTPException\BadRequestException
3340 public function testApiFrPhotoalbumDeleteWithAlbum()
3342 $_REQUEST['album'] = 'album_name';
3343 api_fr_photoalbum_delete('json');
3347 * Test the api_fr_photoalbum_delete() function with an album name.
3351 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3353 $this->markTestIncomplete('We need to add a dataset for this.');
3357 * Test the api_fr_photoalbum_delete() function.
3360 * @expectedException Friendica\Network\HTTPException\BadRequestException
3362 public function testApiFrPhotoalbumUpdate()
3364 api_fr_photoalbum_update('json');
3368 * Test the api_fr_photoalbum_delete() function with an album name.
3371 * @expectedException Friendica\Network\HTTPException\BadRequestException
3373 public function testApiFrPhotoalbumUpdateWithAlbum()
3375 $_REQUEST['album'] = 'album_name';
3376 api_fr_photoalbum_update('json');
3380 * Test the api_fr_photoalbum_delete() function with an album name.
3383 * @expectedException Friendica\Network\HTTPException\BadRequestException
3385 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3387 $_REQUEST['album'] = 'album_name';
3388 $_REQUEST['album_new'] = 'album_name';
3389 api_fr_photoalbum_update('json');
3393 * Test the api_fr_photoalbum_update() function without an authenticated user.
3396 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3398 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3400 $_SESSION['authenticated'] = false;
3401 api_fr_photoalbum_update('json');
3405 * Test the api_fr_photoalbum_delete() function with an album name.
3409 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3411 $this->markTestIncomplete('We need to add a dataset for this.');
3415 * Test the api_fr_photos_list() function.
3419 public function testApiFrPhotosList()
3421 $result = api_fr_photos_list('json');
3422 $this->assertArrayHasKey('photo', $result);
3426 * Test the api_fr_photos_list() function without an authenticated user.
3429 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3431 public function testApiFrPhotosListWithoutAuthenticatedUser()
3433 $_SESSION['authenticated'] = false;
3434 api_fr_photos_list('json');
3438 * Test the api_fr_photo_create_update() function.
3441 * @expectedException Friendica\Network\HTTPException\BadRequestException
3443 public function testApiFrPhotoCreateUpdate()
3445 api_fr_photo_create_update('json');
3449 * Test the api_fr_photo_create_update() function without an authenticated user.
3452 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3454 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3456 $_SESSION['authenticated'] = false;
3457 api_fr_photo_create_update('json');
3461 * Test the api_fr_photo_create_update() function with an album name.
3464 * @expectedException Friendica\Network\HTTPException\BadRequestException
3466 public function testApiFrPhotoCreateUpdateWithAlbum()
3468 $_REQUEST['album'] = 'album_name';
3469 api_fr_photo_create_update('json');
3473 * Test the api_fr_photo_create_update() function with the update mode.
3477 public function testApiFrPhotoCreateUpdateWithUpdate()
3479 $this->markTestIncomplete('We need to create a dataset for this');
3483 * Test the api_fr_photo_create_update() function with an uploaded file.
3487 public function testApiFrPhotoCreateUpdateWithFile()
3489 $this->markTestIncomplete();
3493 * Test the api_fr_photo_delete() function.
3496 * @expectedException Friendica\Network\HTTPException\BadRequestException
3498 public function testApiFrPhotoDelete()
3500 api_fr_photo_delete('json');
3504 * Test the api_fr_photo_delete() function without an authenticated user.
3507 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3509 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3511 $_SESSION['authenticated'] = false;
3512 api_fr_photo_delete('json');
3516 * Test the api_fr_photo_delete() function with a photo ID.
3519 * @expectedException Friendica\Network\HTTPException\BadRequestException
3521 public function testApiFrPhotoDeleteWithPhotoId()
3523 $_REQUEST['photo_id'] = 1;
3524 api_fr_photo_delete('json');
3528 * Test the api_fr_photo_delete() function with a correct photo ID.
3532 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3534 $this->markTestIncomplete('We need to create a dataset for this.');
3538 * Test the api_fr_photo_detail() function.
3541 * @expectedException Friendica\Network\HTTPException\BadRequestException
3543 public function testApiFrPhotoDetail()
3545 api_fr_photo_detail('json');
3549 * Test the api_fr_photo_detail() function without an authenticated user.
3552 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3554 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3556 $_SESSION['authenticated'] = false;
3557 api_fr_photo_detail('json');
3561 * Test the api_fr_photo_detail() function with a photo ID.
3564 * @expectedException Friendica\Network\HTTPException\NotFoundException
3566 public function testApiFrPhotoDetailWithPhotoId()
3568 $_REQUEST['photo_id'] = 1;
3569 api_fr_photo_detail('json');
3573 * Test the api_fr_photo_detail() function with a correct photo ID.
3577 public function testApiFrPhotoDetailCorrectPhotoId()
3579 $this->markTestIncomplete('We need to create a dataset for this.');
3583 * Test the api_account_update_profile_image() function.
3586 * @expectedException Friendica\Network\HTTPException\BadRequestException
3588 public function testApiAccountUpdateProfileImage()
3590 api_account_update_profile_image('json');
3594 * Test the api_account_update_profile_image() function without an authenticated user.
3597 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3599 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3601 $_SESSION['authenticated'] = false;
3602 api_account_update_profile_image('json');
3606 * Test the api_account_update_profile_image() function with an uploaded file.
3609 * @expectedException Friendica\Network\HTTPException\BadRequestException
3611 public function testApiAccountUpdateProfileImageWithUpload()
3613 $this->markTestIncomplete();
3618 * Test the api_account_update_profile() function.
3622 public function testApiAccountUpdateProfile()
3624 $_POST['name'] = 'new_name';
3625 $_POST['description'] = 'new_description';
3626 $result = api_account_update_profile('json');
3627 // We can't use assertSelfUser() here because the user object is missing some properties.
3628 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3629 $this->assertEquals('DFRN', $result['user']['location']);
3630 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3631 $this->assertEquals('dfrn', $result['user']['network']);
3632 $this->assertEquals('new_name', $result['user']['name']);
3633 $this->assertEquals('new_description', $result['user']['description']);
3637 * Test the check_acl_input() function.
3641 public function testCheckAclInput()
3643 $result = check_acl_input('<aclstring>');
3644 // Where does this result come from?
3645 $this->assertEquals(1, $result);
3649 * Test the check_acl_input() function with an empty ACL string.
3653 public function testCheckAclInputWithEmptyAclString()
3655 $result = check_acl_input(' ');
3656 $this->assertFalse($result);
3660 * Test the save_media_to_database() function.
3664 public function testSaveMediaToDatabase()
3666 $this->markTestIncomplete();
3670 * Test the post_photo_item() function.
3674 public function testPostPhotoItem()
3676 $this->markTestIncomplete();
3680 * Test the prepare_photo_data() function.
3684 public function testPreparePhotoData()
3686 $this->markTestIncomplete();
3690 * Test the api_friendica_remoteauth() function.
3693 * @expectedException Friendica\Network\HTTPException\BadRequestException
3695 public function testApiFriendicaRemoteauth()
3697 api_friendica_remoteauth();
3701 * Test the api_friendica_remoteauth() function with an URL.
3704 * @expectedException Friendica\Network\HTTPException\BadRequestException
3706 public function testApiFriendicaRemoteauthWithUrl()
3708 $_GET['url'] = 'url';
3709 $_GET['c_url'] = 'url';
3710 api_friendica_remoteauth();
3714 * Test the api_friendica_remoteauth() function with a correct URL.
3718 public function testApiFriendicaRemoteauthWithCorrectUrl()
3720 $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
3721 $_GET['url'] = 'url';
3722 $_GET['c_url'] = $this->selfUser['nurl'];
3723 api_friendica_remoteauth();
3727 * Test the api_share_as_retweet() function.
3731 public function testApiShareAsRetweet()
3733 $item = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3734 $result = api_share_as_retweet($item);
3735 $this->assertFalse($result);
3739 * Test the api_share_as_retweet() function with a valid item.
3743 public function testApiShareAsRetweetWithValidItem()
3745 $this->markTestIncomplete();
3749 * Test the api_in_reply_to() function.
3753 public function testApiInReplyTo()
3755 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3756 $this->assertArrayHasKey('status_id', $result);
3757 $this->assertArrayHasKey('user_id', $result);
3758 $this->assertArrayHasKey('status_id_str', $result);
3759 $this->assertArrayHasKey('user_id_str', $result);
3760 $this->assertArrayHasKey('screen_name', $result);
3764 * Test the api_in_reply_to() function with a valid item.
3768 public function testApiInReplyToWithValidItem()
3770 $this->markTestIncomplete();
3774 * Test the api_clean_plain_items() function.
3778 public function testApiCleanPlainItems()
3780 $_REQUEST['include_entities'] = 'true';
3781 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3782 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3786 * Test the api_best_nickname() function.
3790 public function testApiBestNickname()
3793 $result = api_best_nickname($contacts);
3794 $this->assertNull($result);
3798 * Test the api_best_nickname() function with contacts.
3802 public function testApiBestNicknameWithContacts()
3804 $this->markTestIncomplete();
3808 * Test the api_friendica_group_show() function.
3812 public function testApiFriendicaGroupShow()
3814 $this->markTestIncomplete();
3818 * Test the api_friendica_group_delete() function.
3822 public function testApiFriendicaGroupDelete()
3824 $this->markTestIncomplete();
3828 * Test the api_lists_destroy() function.
3832 public function testApiListsDestroy()
3834 $this->markTestIncomplete();
3838 * Test the group_create() function.
3842 public function testGroupCreate()
3844 $this->markTestIncomplete();
3848 * Test the api_friendica_group_create() function.
3852 public function testApiFriendicaGroupCreate()
3854 $this->markTestIncomplete();
3858 * Test the api_lists_create() function.
3862 public function testApiListsCreate()
3864 $this->markTestIncomplete();
3868 * Test the api_friendica_group_update() function.
3872 public function testApiFriendicaGroupUpdate()
3874 $this->markTestIncomplete();
3878 * Test the api_lists_update() function.
3882 public function testApiListsUpdate()
3884 $this->markTestIncomplete();
3888 * Test the api_friendica_activity() function.
3892 public function testApiFriendicaActivity()
3894 $this->markTestIncomplete();
3898 * Test the api_friendica_notification() function.
3901 * @expectedException Friendica\Network\HTTPException\BadRequestException
3903 public function testApiFriendicaNotification()
3905 api_friendica_notification('json');
3909 * Test the api_friendica_notification() function without an authenticated user.
3912 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3914 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3916 $_SESSION['authenticated'] = false;
3917 api_friendica_notification('json');
3921 * Test the api_friendica_notification() function with empty result
3925 public function testApiFriendicaNotificationWithEmptyResult()
3927 $this->app->argv = ['api', 'friendica', 'notification'];
3928 $this->app->argc = count($this->app->argv);
3929 $_SESSION['uid'] = 41;
3930 $result = api_friendica_notification('json');
3931 $this->assertEquals(['note' => false], $result);
3935 * Test the api_friendica_notification() function with an XML result.
3939 public function testApiFriendicaNotificationWithXmlResult()
3941 $this->app->argv = ['api', 'friendica', 'notification'];
3942 $this->app->argc = count($this->app->argv);
3943 $result = api_friendica_notification('xml');
3944 $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02');
3946 <?xml version="1.0"?>
3948 <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="{$dateRel}" msg_html="A test reply from an item" msg_plain="A test reply from an item"/>
3951 $this->assertXmlStringEqualsXmlString($assertXml, $result);
3955 * Test the api_friendica_notification() function with an JSON result.
3959 public function testApiFriendicaNotificationWithJsonResult()
3961 $this->app->argv = ['api', 'friendica', 'notification'];
3962 $this->app->argc = count($this->app->argv);
3963 $result = json_encode(api_friendica_notification('json'));
3964 $this->assertJson($result);
3968 * Test the api_friendica_notification_seen() function.
3972 public function testApiFriendicaNotificationSeen()
3974 $this->markTestIncomplete();
3978 * Test the api_friendica_direct_messages_setseen() function.
3982 public function testApiFriendicaDirectMessagesSetseen()
3984 $this->markTestIncomplete();
3988 * Test the api_friendica_direct_messages_search() function.
3992 public function testApiFriendicaDirectMessagesSearch()
3994 $this->markTestIncomplete();
3998 * Test the api_friendica_profile_show() function.
4002 public function testApiFriendicaProfileShow()
4004 $result = api_friendica_profile_show('json');
4005 // We can't use assertSelfUser() here because the user object is missing some properties.
4006 $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
4007 $this->assertEquals('DFRN', $result['$result']['friendica_owner']['location']);
4008 $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
4009 $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
4010 $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
4011 $this->assertTrue($result['$result']['friendica_owner']['verified']);
4012 $this->assertFalse($result['$result']['multi_profiles']);
4016 * Test the api_friendica_profile_show() function with a profile ID.
4020 public function testApiFriendicaProfileShowWithProfileId()
4022 $this->markTestIncomplete('We need to add a dataset for this.');
4026 * Test the api_friendica_profile_show() function with a wrong profile ID.
4029 * @expectedException Friendica\Network\HTTPException\BadRequestException
4031 public function testApiFriendicaProfileShowWithWrongProfileId()
4033 $_REQUEST['profile_id'] = 666;
4034 api_friendica_profile_show('json');
4038 * Test the api_friendica_profile_show() function without an authenticated user.
4041 * @expectedException Friendica\Network\HTTPException\ForbiddenException
4043 public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
4045 $_SESSION['authenticated'] = false;
4046 api_friendica_profile_show('json');
4050 * Test the api_saved_searches_list() function.
4054 public function testApiSavedSearchesList()
4056 $result = api_saved_searches_list('json');
4057 $this->assertEquals(1, $result['terms'][0]['id']);
4058 $this->assertEquals(1, $result['terms'][0]['id_str']);
4059 $this->assertEquals('Saved search', $result['terms'][0]['name']);
4060 $this->assertEquals('Saved search', $result['terms'][0]['query']);