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() function.
2496 public function testApiFormatItems()
2500 'item_network' => 'item_network',
2506 'author-network' => Protocol::DFRN,
2507 'author-link' => 'http://localhost/profile/othercontact',
2511 $result = api_format_items($items, ['id' => 0], true);
2512 foreach ($result as $status) {
2513 $this->assertStatus($status);
2518 * Test the api_format_items() function with an XML result.
2522 public function testApiFormatItemsWithXml()
2530 'author-network' => Protocol::DFRN,
2531 'author-link' => 'http://localhost/profile/othercontact',
2535 $result = api_format_items($items, ['id' => 0], true, 'xml');
2536 foreach ($result as $status) {
2537 $this->assertStatus($status);
2542 * Test the api_format_items() function.
2546 public function testApiAccountRateLimitStatus()
2548 $result = api_account_rate_limit_status('json');
2549 $this->assertEquals(150, $result['hash']['remaining_hits']);
2550 $this->assertEquals(150, $result['hash']['hourly_limit']);
2551 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2555 * Test the api_format_items() function with an XML result.
2559 public function testApiAccountRateLimitStatusWithXml()
2561 $result = api_account_rate_limit_status('xml');
2562 $this->assertXml($result, 'hash');
2566 * Test the api_help_test() function.
2570 public function testApiHelpTest()
2572 $result = api_help_test('json');
2573 $this->assertEquals(['ok' => 'ok'], $result);
2577 * Test the api_help_test() function with an XML result.
2581 public function testApiHelpTestWithXml()
2583 $result = api_help_test('xml');
2584 $this->assertXml($result, 'ok');
2588 * Test the api_lists_list() function.
2592 public function testApiListsList()
2594 $result = api_lists_list('json');
2595 $this->assertEquals(['lists_list' => []], $result);
2599 * Test the api_lists_ownerships() function.
2603 public function testApiListsOwnerships()
2605 $result = api_lists_ownerships('json');
2606 foreach ($result['lists']['lists'] as $list) {
2607 $this->assertList($list);
2612 * Test the api_lists_ownerships() function without an authenticated user.
2615 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2617 public function testApiListsOwnershipsWithoutAuthenticatedUser()
2619 $_SESSION['authenticated'] = false;
2620 api_lists_ownerships('json');
2624 * Test the api_lists_statuses() function.
2626 * @expectedException Friendica\Network\HTTPException\BadRequestException
2629 public function testApiListsStatuses()
2631 api_lists_statuses('json');
2635 * Test the api_lists_statuses() function with a list ID.
2639 public function testApiListsStatusesWithListId()
2641 $_REQUEST['list_id'] = 1;
2642 $_REQUEST['page'] = -1;
2643 $_REQUEST['max_id'] = 10;
2644 $result = api_lists_statuses('json');
2645 foreach ($result['status'] as $status) {
2646 $this->assertStatus($status);
2651 * Test the api_lists_statuses() function with a list ID and a RSS result.
2655 public function testApiListsStatusesWithListIdAndRss()
2657 $_REQUEST['list_id'] = 1;
2658 $result = api_lists_statuses('rss');
2659 $this->assertXml($result, 'statuses');
2663 * Test the api_lists_statuses() function with an unallowed user.
2666 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2668 public function testApiListsStatusesWithUnallowedUser()
2670 $_SESSION['allow_api'] = false;
2671 $_GET['screen_name'] = $this->selfUser['nick'];
2672 api_lists_statuses('json');
2676 * Test the api_statuses_f() function.
2680 public function testApiStatusesFWithFriends()
2683 $result = api_statuses_f('friends');
2684 $this->assertArrayHasKey('user', $result);
2688 * Test the api_statuses_f() function.
2692 public function testApiStatusesFWithFollowers()
2694 $result = api_statuses_f('followers');
2695 $this->assertArrayHasKey('user', $result);
2699 * Test the api_statuses_f() function.
2703 public function testApiStatusesFWithBlocks()
2705 $result = api_statuses_f('blocks');
2706 $this->assertArrayHasKey('user', $result);
2710 * Test the api_statuses_f() function.
2714 public function testApiStatusesFWithIncoming()
2716 $result = api_statuses_f('incoming');
2717 $this->assertArrayHasKey('user', $result);
2721 * Test the api_statuses_f() function an undefined cursor GET variable.
2725 public function testApiStatusesFWithUndefinedCursor()
2727 $_GET['cursor'] = 'undefined';
2728 $this->assertFalse(api_statuses_f('friends'));
2732 * Test the api_statuses_friends() function.
2736 public function testApiStatusesFriends()
2738 $result = api_statuses_friends('json');
2739 $this->assertArrayHasKey('user', $result);
2743 * Test the api_statuses_friends() function an undefined cursor GET variable.
2747 public function testApiStatusesFriendsWithUndefinedCursor()
2749 $_GET['cursor'] = 'undefined';
2750 $this->assertFalse(api_statuses_friends('json'));
2754 * Test the api_statuses_followers() function.
2758 public function testApiStatusesFollowers()
2760 $result = api_statuses_followers('json');
2761 $this->assertArrayHasKey('user', $result);
2765 * Test the api_statuses_followers() function an undefined cursor GET variable.
2769 public function testApiStatusesFollowersWithUndefinedCursor()
2771 $_GET['cursor'] = 'undefined';
2772 $this->assertFalse(api_statuses_followers('json'));
2776 * Test the api_blocks_list() function.
2780 public function testApiBlocksList()
2782 $result = api_blocks_list('json');
2783 $this->assertArrayHasKey('user', $result);
2787 * Test the api_blocks_list() function an undefined cursor GET variable.
2791 public function testApiBlocksListWithUndefinedCursor()
2793 $_GET['cursor'] = 'undefined';
2794 $this->assertFalse(api_blocks_list('json'));
2798 * Test the api_friendships_incoming() function.
2802 public function testApiFriendshipsIncoming()
2804 $result = api_friendships_incoming('json');
2805 $this->assertArrayHasKey('id', $result);
2809 * Test the api_friendships_incoming() function an undefined cursor GET variable.
2813 public function testApiFriendshipsIncomingWithUndefinedCursor()
2815 $_GET['cursor'] = 'undefined';
2816 $this->assertFalse(api_friendships_incoming('json'));
2820 * Test the api_statusnet_config() function.
2824 public function testApiStatusnetConfig()
2826 $result = api_statusnet_config('json');
2827 $this->assertEquals('localhost', $result['config']['site']['server']);
2828 $this->assertEquals('default', $result['config']['site']['theme']);
2829 $this->assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2830 $this->assertTrue($result['config']['site']['fancy']);
2831 $this->assertEquals('en', $result['config']['site']['language']);
2832 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2833 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2834 $this->assertEquals('false', $result['config']['site']['private']);
2835 $this->assertEquals('false', $result['config']['site']['ssl']);
2836 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2840 * Test the api_statusnet_version() function.
2844 public function testApiStatusnetVersion()
2846 $result = api_statusnet_version('json');
2847 $this->assertEquals('0.9.7', $result['version']);
2851 * Test the api_ff_ids() function.
2855 public function testApiFfIds()
2857 $result = api_ff_ids('json', Contact::FOLLOWER);
2858 $this->assertEquals(['id' => []], $result);
2862 * Test the api_ff_ids() function with a result.
2866 public function testApiFfIdsWithResult()
2868 $this->markTestIncomplete();
2872 * Test the api_ff_ids() function without an authenticated user.
2875 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2877 public function testApiFfIdsWithoutAuthenticatedUser()
2879 $_SESSION['authenticated'] = false;
2880 api_ff_ids('json', Contact::FOLLOWER);
2884 * Test the api_friends_ids() function.
2888 public function testApiFriendsIds()
2890 $result = api_friends_ids('json');
2891 $this->assertEquals(['id' => []], $result);
2895 * Test the api_followers_ids() function.
2899 public function testApiFollowersIds()
2901 $result = api_followers_ids('json');
2902 $this->assertEquals(['id' => []], $result);
2906 * Test the api_direct_messages_new() function.
2910 public function testApiDirectMessagesNew()
2912 $result = api_direct_messages_new('json');
2913 $this->assertNull($result);
2917 * Test the api_direct_messages_new() function without an authenticated user.
2920 * @expectedException Friendica\Network\HTTPException\ForbiddenException
2922 public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2924 $_SESSION['authenticated'] = false;
2925 api_direct_messages_new('json');
2929 * Test the api_direct_messages_new() function with an user ID.
2933 public function testApiDirectMessagesNewWithUserId()
2935 $_POST['text'] = 'message_text';
2936 $_POST['user_id'] = $this->otherUser['id'];
2937 $result = api_direct_messages_new('json');
2938 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
2942 * Test the api_direct_messages_new() function with a screen name.
2946 public function testApiDirectMessagesNewWithScreenName()
2948 $_POST['text'] = 'message_text';
2949 $_POST['screen_name'] = $this->friendUser['nick'];
2950 $result = api_direct_messages_new('json');
2951 $this->assertContains('message_text', $result['direct_message']['text']);
2952 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2953 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2957 * Test the api_direct_messages_new() function with a title.
2961 public function testApiDirectMessagesNewWithTitle()
2963 $_POST['text'] = 'message_text';
2964 $_POST['screen_name'] = $this->friendUser['nick'];
2965 $_REQUEST['title'] = 'message_title';
2966 $result = api_direct_messages_new('json');
2967 $this->assertContains('message_text', $result['direct_message']['text']);
2968 $this->assertContains('message_title', $result['direct_message']['text']);
2969 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2970 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2974 * Test the api_direct_messages_new() function with an RSS result.
2978 public function testApiDirectMessagesNewWithRss()
2980 $_POST['text'] = 'message_text';
2981 $_POST['screen_name'] = $this->friendUser['nick'];
2982 $result = api_direct_messages_new('rss');
2983 $this->assertXml($result, 'direct-messages');
2987 * Test the api_direct_messages_destroy() function.
2990 * @expectedException Friendica\Network\HTTPException\BadRequestException
2992 public function testApiDirectMessagesDestroy()
2994 api_direct_messages_destroy('json');
2998 * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
3002 public function testApiDirectMessagesDestroyWithVerbose()
3004 $_GET['friendica_verbose'] = 'true';
3005 $result = api_direct_messages_destroy('json');
3006 $this->assertEquals(
3009 'result' => 'error',
3010 'message' => 'message id or parenturi not specified'
3018 * Test the api_direct_messages_destroy() function without an authenticated user.
3021 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3023 public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
3025 $_SESSION['authenticated'] = false;
3026 api_direct_messages_destroy('json');
3030 * Test the api_direct_messages_destroy() function with a non-zero ID.
3033 * @expectedException Friendica\Network\HTTPException\BadRequestException
3035 public function testApiDirectMessagesDestroyWithId()
3037 $_REQUEST['id'] = 1;
3038 api_direct_messages_destroy('json');
3042 * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
3046 public function testApiDirectMessagesDestroyWithIdAndVerbose()
3048 $_REQUEST['id'] = 1;
3049 $_REQUEST['friendica_parenturi'] = 'parent_uri';
3050 $_GET['friendica_verbose'] = 'true';
3051 $result = api_direct_messages_destroy('json');
3052 $this->assertEquals(
3055 'result' => 'error',
3056 'message' => 'message id not in database'
3064 * Test the api_direct_messages_destroy() function with a non-zero ID.
3068 public function testApiDirectMessagesDestroyWithCorrectId()
3070 $this->markTestIncomplete('We need to add a dataset for this.');
3074 * Test the api_direct_messages_box() function.
3078 public function testApiDirectMessagesBoxWithSentbox()
3080 $_REQUEST['page'] = -1;
3081 $_REQUEST['max_id'] = 10;
3082 $result = api_direct_messages_box('json', 'sentbox', 'false');
3083 $this->assertArrayHasKey('direct_message', $result);
3087 * Test the api_direct_messages_box() function.
3091 public function testApiDirectMessagesBoxWithConversation()
3093 $result = api_direct_messages_box('json', 'conversation', 'false');
3094 $this->assertArrayHasKey('direct_message', $result);
3098 * Test the api_direct_messages_box() function.
3102 public function testApiDirectMessagesBoxWithAll()
3104 $result = api_direct_messages_box('json', 'all', 'false');
3105 $this->assertArrayHasKey('direct_message', $result);
3109 * Test the api_direct_messages_box() function.
3113 public function testApiDirectMessagesBoxWithInbox()
3115 $result = api_direct_messages_box('json', 'inbox', 'false');
3116 $this->assertArrayHasKey('direct_message', $result);
3120 * Test the api_direct_messages_box() function.
3124 public function testApiDirectMessagesBoxWithVerbose()
3126 $result = api_direct_messages_box('json', 'sentbox', 'true');
3127 $this->assertEquals(
3130 'result' => 'error',
3131 'message' => 'no mails available'
3139 * Test the api_direct_messages_box() function with a RSS result.
3143 public function testApiDirectMessagesBoxWithRss()
3145 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3146 $this->assertXml($result, 'direct-messages');
3150 * Test the api_direct_messages_box() function without an authenticated user.
3153 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3155 public function testApiDirectMessagesBoxWithUnallowedUser()
3157 $_SESSION['allow_api'] = false;
3158 $_GET['screen_name'] = $this->selfUser['nick'];
3159 api_direct_messages_box('json', 'sentbox', 'false');
3163 * Test the api_direct_messages_sentbox() function.
3167 public function testApiDirectMessagesSentbox()
3169 $result = api_direct_messages_sentbox('json');
3170 $this->assertArrayHasKey('direct_message', $result);
3174 * Test the api_direct_messages_inbox() function.
3178 public function testApiDirectMessagesInbox()
3180 $result = api_direct_messages_inbox('json');
3181 $this->assertArrayHasKey('direct_message', $result);
3185 * Test the api_direct_messages_all() function.
3189 public function testApiDirectMessagesAll()
3191 $result = api_direct_messages_all('json');
3192 $this->assertArrayHasKey('direct_message', $result);
3196 * Test the api_direct_messages_conversation() function.
3200 public function testApiDirectMessagesConversation()
3202 $result = api_direct_messages_conversation('json');
3203 $this->assertArrayHasKey('direct_message', $result);
3207 * Test the api_oauth_request_token() function.
3211 public function testApiOauthRequestToken()
3213 $this->markTestIncomplete('exit() kills phpunit as well');
3217 * Test the api_oauth_access_token() function.
3221 public function testApiOauthAccessToken()
3223 $this->markTestIncomplete('exit() kills phpunit as well');
3227 * Test the api_fr_photoalbum_delete() function.
3230 * @expectedException Friendica\Network\HTTPException\BadRequestException
3232 public function testApiFrPhotoalbumDelete()
3234 api_fr_photoalbum_delete('json');
3238 * Test the api_fr_photoalbum_delete() function with an album name.
3241 * @expectedException Friendica\Network\HTTPException\BadRequestException
3243 public function testApiFrPhotoalbumDeleteWithAlbum()
3245 $_REQUEST['album'] = 'album_name';
3246 api_fr_photoalbum_delete('json');
3250 * Test the api_fr_photoalbum_delete() function with an album name.
3254 public function testApiFrPhotoalbumDeleteWithValidAlbum()
3256 $this->markTestIncomplete('We need to add a dataset for this.');
3260 * Test the api_fr_photoalbum_delete() function.
3263 * @expectedException Friendica\Network\HTTPException\BadRequestException
3265 public function testApiFrPhotoalbumUpdate()
3267 api_fr_photoalbum_update('json');
3271 * Test the api_fr_photoalbum_delete() function with an album name.
3274 * @expectedException Friendica\Network\HTTPException\BadRequestException
3276 public function testApiFrPhotoalbumUpdateWithAlbum()
3278 $_REQUEST['album'] = 'album_name';
3279 api_fr_photoalbum_update('json');
3283 * Test the api_fr_photoalbum_delete() function with an album name.
3286 * @expectedException Friendica\Network\HTTPException\BadRequestException
3288 public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3290 $_REQUEST['album'] = 'album_name';
3291 $_REQUEST['album_new'] = 'album_name';
3292 api_fr_photoalbum_update('json');
3296 * Test the api_fr_photoalbum_update() function without an authenticated user.
3299 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3301 public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3303 $_SESSION['authenticated'] = false;
3304 api_fr_photoalbum_update('json');
3308 * Test the api_fr_photoalbum_delete() function with an album name.
3312 public function testApiFrPhotoalbumUpdateWithValidAlbum()
3314 $this->markTestIncomplete('We need to add a dataset for this.');
3318 * Test the api_fr_photos_list() function.
3322 public function testApiFrPhotosList()
3324 $result = api_fr_photos_list('json');
3325 $this->assertArrayHasKey('photo', $result);
3329 * Test the api_fr_photos_list() function without an authenticated user.
3332 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3334 public function testApiFrPhotosListWithoutAuthenticatedUser()
3336 $_SESSION['authenticated'] = false;
3337 api_fr_photos_list('json');
3341 * Test the api_fr_photo_create_update() function.
3344 * @expectedException Friendica\Network\HTTPException\BadRequestException
3346 public function testApiFrPhotoCreateUpdate()
3348 api_fr_photo_create_update('json');
3352 * Test the api_fr_photo_create_update() function without an authenticated user.
3355 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3357 public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3359 $_SESSION['authenticated'] = false;
3360 api_fr_photo_create_update('json');
3364 * Test the api_fr_photo_create_update() function with an album name.
3367 * @expectedException Friendica\Network\HTTPException\BadRequestException
3369 public function testApiFrPhotoCreateUpdateWithAlbum()
3371 $_REQUEST['album'] = 'album_name';
3372 api_fr_photo_create_update('json');
3376 * Test the api_fr_photo_create_update() function with the update mode.
3380 public function testApiFrPhotoCreateUpdateWithUpdate()
3382 $this->markTestIncomplete('We need to create a dataset for this');
3386 * Test the api_fr_photo_create_update() function with an uploaded file.
3390 public function testApiFrPhotoCreateUpdateWithFile()
3392 $this->markTestIncomplete();
3396 * Test the api_fr_photo_delete() function.
3399 * @expectedException Friendica\Network\HTTPException\BadRequestException
3401 public function testApiFrPhotoDelete()
3403 api_fr_photo_delete('json');
3407 * Test the api_fr_photo_delete() function without an authenticated user.
3410 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3412 public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3414 $_SESSION['authenticated'] = false;
3415 api_fr_photo_delete('json');
3419 * Test the api_fr_photo_delete() function with a photo ID.
3422 * @expectedException Friendica\Network\HTTPException\BadRequestException
3424 public function testApiFrPhotoDeleteWithPhotoId()
3426 $_REQUEST['photo_id'] = 1;
3427 api_fr_photo_delete('json');
3431 * Test the api_fr_photo_delete() function with a correct photo ID.
3435 public function testApiFrPhotoDeleteWithCorrectPhotoId()
3437 $this->markTestIncomplete('We need to create a dataset for this.');
3441 * Test the api_fr_photo_detail() function.
3444 * @expectedException Friendica\Network\HTTPException\BadRequestException
3446 public function testApiFrPhotoDetail()
3448 api_fr_photo_detail('json');
3452 * Test the api_fr_photo_detail() function without an authenticated user.
3455 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3457 public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3459 $_SESSION['authenticated'] = false;
3460 api_fr_photo_detail('json');
3464 * Test the api_fr_photo_detail() function with a photo ID.
3467 * @expectedException Friendica\Network\HTTPException\NotFoundException
3469 public function testApiFrPhotoDetailWithPhotoId()
3471 $_REQUEST['photo_id'] = 1;
3472 api_fr_photo_detail('json');
3476 * Test the api_fr_photo_detail() function with a correct photo ID.
3480 public function testApiFrPhotoDetailCorrectPhotoId()
3482 $this->markTestIncomplete('We need to create a dataset for this.');
3486 * Test the api_account_update_profile_image() function.
3489 * @expectedException Friendica\Network\HTTPException\BadRequestException
3491 public function testApiAccountUpdateProfileImage()
3493 api_account_update_profile_image('json');
3497 * Test the api_account_update_profile_image() function without an authenticated user.
3500 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3502 public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3504 $_SESSION['authenticated'] = false;
3505 api_account_update_profile_image('json');
3509 * Test the api_account_update_profile_image() function with an uploaded file.
3512 * @expectedException Friendica\Network\HTTPException\BadRequestException
3514 public function testApiAccountUpdateProfileImageWithUpload()
3516 $this->markTestIncomplete();
3521 * Test the api_account_update_profile() function.
3525 public function testApiAccountUpdateProfile()
3527 $_POST['name'] = 'new_name';
3528 $_POST['description'] = 'new_description';
3529 $result = api_account_update_profile('json');
3530 // We can't use assertSelfUser() here because the user object is missing some properties.
3531 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3532 $this->assertEquals('DFRN', $result['user']['location']);
3533 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3534 $this->assertEquals('dfrn', $result['user']['network']);
3535 $this->assertEquals('new_name', $result['user']['name']);
3536 $this->assertEquals('new_description', $result['user']['description']);
3540 * Test the check_acl_input() function.
3544 public function testCheckAclInput()
3546 $result = check_acl_input('<aclstring>');
3547 // Where does this result come from?
3548 $this->assertEquals(1, $result);
3552 * Test the check_acl_input() function with an empty ACL string.
3556 public function testCheckAclInputWithEmptyAclString()
3558 $result = check_acl_input(' ');
3559 $this->assertFalse($result);
3563 * Test the save_media_to_database() function.
3567 public function testSaveMediaToDatabase()
3569 $this->markTestIncomplete();
3573 * Test the post_photo_item() function.
3577 public function testPostPhotoItem()
3579 $this->markTestIncomplete();
3583 * Test the prepare_photo_data() function.
3587 public function testPreparePhotoData()
3589 $this->markTestIncomplete();
3593 * Test the api_friendica_remoteauth() function.
3596 * @expectedException Friendica\Network\HTTPException\BadRequestException
3598 public function testApiFriendicaRemoteauth()
3600 api_friendica_remoteauth();
3604 * Test the api_friendica_remoteauth() function with an URL.
3607 * @expectedException Friendica\Network\HTTPException\BadRequestException
3609 public function testApiFriendicaRemoteauthWithUrl()
3611 $_GET['url'] = 'url';
3612 $_GET['c_url'] = 'url';
3613 api_friendica_remoteauth();
3617 * Test the api_friendica_remoteauth() function with a correct URL.
3621 public function testApiFriendicaRemoteauthWithCorrectUrl()
3623 $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
3624 $_GET['url'] = 'url';
3625 $_GET['c_url'] = $this->selfUser['nurl'];
3626 api_friendica_remoteauth();
3630 * Test the api_share_as_retweet() function.
3634 public function testApiShareAsRetweet()
3636 $item = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3637 $result = api_share_as_retweet($item);
3638 $this->assertFalse($result);
3642 * Test the api_share_as_retweet() function with a valid item.
3646 public function testApiShareAsRetweetWithValidItem()
3648 $this->markTestIncomplete();
3652 * Test the api_in_reply_to() function.
3656 public function testApiInReplyTo()
3658 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3659 $this->assertArrayHasKey('status_id', $result);
3660 $this->assertArrayHasKey('user_id', $result);
3661 $this->assertArrayHasKey('status_id_str', $result);
3662 $this->assertArrayHasKey('user_id_str', $result);
3663 $this->assertArrayHasKey('screen_name', $result);
3667 * Test the api_in_reply_to() function with a valid item.
3671 public function testApiInReplyToWithValidItem()
3673 $this->markTestIncomplete();
3677 * Test the api_clean_plain_items() function.
3681 public function testApiCleanPlainItems()
3683 $_REQUEST['include_entities'] = 'true';
3684 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3685 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3689 * Test the api_best_nickname() function.
3693 public function testApiBestNickname()
3696 $result = api_best_nickname($contacts);
3697 $this->assertNull($result);
3701 * Test the api_best_nickname() function with contacts.
3705 public function testApiBestNicknameWithContacts()
3707 $this->markTestIncomplete();
3711 * Test the api_friendica_group_show() function.
3715 public function testApiFriendicaGroupShow()
3717 $this->markTestIncomplete();
3721 * Test the api_friendica_group_delete() function.
3725 public function testApiFriendicaGroupDelete()
3727 $this->markTestIncomplete();
3731 * Test the api_lists_destroy() function.
3735 public function testApiListsDestroy()
3737 $this->markTestIncomplete();
3741 * Test the group_create() function.
3745 public function testGroupCreate()
3747 $this->markTestIncomplete();
3751 * Test the api_friendica_group_create() function.
3755 public function testApiFriendicaGroupCreate()
3757 $this->markTestIncomplete();
3761 * Test the api_lists_create() function.
3765 public function testApiListsCreate()
3767 $this->markTestIncomplete();
3771 * Test the api_friendica_group_update() function.
3775 public function testApiFriendicaGroupUpdate()
3777 $this->markTestIncomplete();
3781 * Test the api_lists_update() function.
3785 public function testApiListsUpdate()
3787 $this->markTestIncomplete();
3791 * Test the api_friendica_activity() function.
3795 public function testApiFriendicaActivity()
3797 $this->markTestIncomplete();
3801 * Test the api_friendica_notification() function.
3804 * @expectedException Friendica\Network\HTTPException\BadRequestException
3806 public function testApiFriendicaNotification()
3808 api_friendica_notification('json');
3812 * Test the api_friendica_notification() function without an authenticated user.
3815 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3817 public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3819 $_SESSION['authenticated'] = false;
3820 api_friendica_notification('json');
3824 * Test the api_friendica_notification() function with empty result
3828 public function testApiFriendicaNotificationWithEmptyResult()
3830 $this->app->argv = ['api', 'friendica', 'notification'];
3831 $this->app->argc = count($this->app->argv);
3832 $_SESSION['uid'] = 41;
3833 $result = api_friendica_notification('json');
3834 $this->assertEquals(['note' => false], $result);
3838 * Test the api_friendica_notification() function with an XML result.
3842 public function testApiFriendicaNotificationWithXmlResult()
3844 $this->app->argv = ['api', 'friendica', 'notification'];
3845 $this->app->argc = count($this->app->argv);
3846 $result = api_friendica_notification('xml');
3848 <?xml version="1.0"?>
3850 <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"/>
3853 $this->assertXmlStringEqualsXmlString($assertXml, $result);
3857 * Test the api_friendica_notification() function with an JSON result.
3861 public function testApiFriendicaNotificationWithJsonResult()
3863 $this->app->argv = ['api', 'friendica', 'notification'];
3864 $this->app->argc = count($this->app->argv);
3865 $result = json_encode(api_friendica_notification('json'));
3866 $this->assertJson($result);
3870 * Test the api_friendica_notification_seen() function.
3874 public function testApiFriendicaNotificationSeen()
3876 $this->markTestIncomplete();
3880 * Test the api_friendica_direct_messages_setseen() function.
3884 public function testApiFriendicaDirectMessagesSetseen()
3886 $this->markTestIncomplete();
3890 * Test the api_friendica_direct_messages_search() function.
3894 public function testApiFriendicaDirectMessagesSearch()
3896 $this->markTestIncomplete();
3900 * Test the api_friendica_profile_show() function.
3904 public function testApiFriendicaProfileShow()
3906 $result = api_friendica_profile_show('json');
3907 // We can't use assertSelfUser() here because the user object is missing some properties.
3908 $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
3909 $this->assertEquals('DFRN', $result['$result']['friendica_owner']['location']);
3910 $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
3911 $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
3912 $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
3913 $this->assertTrue($result['$result']['friendica_owner']['verified']);
3914 $this->assertFalse($result['$result']['multi_profiles']);
3918 * Test the api_friendica_profile_show() function with a profile ID.
3922 public function testApiFriendicaProfileShowWithProfileId()
3924 $this->markTestIncomplete('We need to add a dataset for this.');
3928 * Test the api_friendica_profile_show() function with a wrong profile ID.
3931 * @expectedException Friendica\Network\HTTPException\BadRequestException
3933 public function testApiFriendicaProfileShowWithWrongProfileId()
3935 $_REQUEST['profile_id'] = 666;
3936 api_friendica_profile_show('json');
3940 * Test the api_friendica_profile_show() function without an authenticated user.
3943 * @expectedException Friendica\Network\HTTPException\ForbiddenException
3945 public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
3947 $_SESSION['authenticated'] = false;
3948 api_friendica_profile_show('json');
3952 * Test the api_saved_searches_list() function.
3956 public function testApiSavedSearchesList()
3958 $result = api_saved_searches_list('json');
3959 $this->assertEquals(1, $result['terms'][0]['id']);
3960 $this->assertEquals(1, $result['terms'][0]['id_str']);
3961 $this->assertEquals('Saved search', $result['terms'][0]['name']);
3962 $this->assertEquals('Saved search', $result['terms'][0]['query']);