]> git.mxchange.org Git - friendica.git/blob - tests/legacy/ApiTest.php
Move all (inactive) API endpoint specific tests to new structure
[friendica.git] / tests / legacy / ApiTest.php
1 <?php
2 /**
3  * ApiTest class.
4  */
5
6 namespace Friendica\Test\legacy;
7
8 use Friendica\App;
9 use Friendica\Core\Config\Capability\IManageConfigValues;
10 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
11 use Friendica\Core\Protocol;
12 use Friendica\DI;
13 use Friendica\Model\Post;
14 use Friendica\Module\Api\ApiResponse;
15 use Friendica\Module\BaseApi;
16 use Friendica\Network\HTTPException;
17 use Friendica\Security\BasicAuth;
18 use Friendica\Test\FixtureTest;
19 use Friendica\Util\Arrays;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Temporal;
22 use Monolog\Handler\TestHandler;
23
24 require_once __DIR__ . '/../../include/api.php';
25
26 /**
27  * Tests for the API functions.
28  *
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
31  *
32  * @backupGlobals enabled
33  */
34 class ApiTest extends FixtureTest
35 {
36         /**
37          * @var TestHandler Can handle log-outputs
38          */
39         protected $logOutput;
40
41         /** @var array */
42         protected $selfUser;
43         /** @var array */
44         protected $friendUser;
45         /** @var array */
46         protected $otherUser;
47
48         protected $wrongUserId;
49
50         /** @var App */
51         protected $app;
52
53         /** @var IManageConfigValues */
54         protected $config;
55
56         /**
57          * Create variables used by tests.
58          */
59         protected function setUp() : void
60         {
61                 global $API, $called_api;
62                 $API = [];
63                 $called_api = [];
64
65                 parent::setUp();
66
67                 /** @var IManageConfigValues $config */
68                 $this->config = $this->dice->create(IManageConfigValues::class);
69
70                 $this->config->set('system', 'url', 'http://localhost');
71                 $this->config->set('system', 'hostname', 'localhost');
72                 $this->config->set('system', 'worker_dont_fork', true);
73
74                 // Default config
75                 $this->config->set('config', 'hostname', 'localhost');
76                 $this->config->set('system', 'throttle_limit_day', 100);
77                 $this->config->set('system', 'throttle_limit_week', 100);
78                 $this->config->set('system', 'throttle_limit_month', 100);
79                 $this->config->set('system', 'theme', 'system_theme');
80
81
82                 /** @var App app */
83                 $this->app = DI::app();
84
85                 DI::args()->setArgc(1);
86
87                 // User data that the test database is populated with
88                 $this->selfUser   = [
89                         'id'   => 42,
90                         'name' => 'Self contact',
91                         'nick' => 'selfcontact',
92                         'nurl' => 'http://localhost/profile/selfcontact'
93                 ];
94                 $this->friendUser = [
95                         'id'   => 44,
96                         'name' => 'Friend contact',
97                         'nick' => 'friendcontact',
98                         'nurl' => 'http://localhost/profile/friendcontact'
99                 ];
100                 $this->otherUser  = [
101                         'id'   => 43,
102                         'name' => 'othercontact',
103                         'nick' => 'othercontact',
104                         'nurl' => 'http://localhost/profile/othercontact'
105                 ];
106
107                 // User ID that we know is not in the database
108                 $this->wrongUserId = 666;
109
110                 DI::session()->start();
111
112                 // Most API require login so we force the session
113                 $_SESSION = [
114                         'authenticated' => true,
115                         'uid'           => $this->selfUser['id']
116                 ];
117                 BasicAuth::setCurrentUserID($this->selfUser['id']);
118         }
119
120         /**
121          * Assert that an user array contains expected keys.
122          *
123          * @param array $user User array
124          *
125          * @return void
126          */
127         private function assertSelfUser(array $user)
128         {
129                 self::assertEquals($this->selfUser['id'], $user['uid']);
130                 self::assertEquals($this->selfUser['id'], $user['cid']);
131                 self::assertEquals(1, $user['self']);
132                 self::assertEquals('DFRN', $user['location']);
133                 self::assertEquals($this->selfUser['name'], $user['name']);
134                 self::assertEquals($this->selfUser['nick'], $user['screen_name']);
135                 self::assertEquals('dfrn', $user['network']);
136                 self::assertTrue($user['verified']);
137         }
138
139         /**
140          * Assert that an user array contains expected keys.
141          *
142          * @param array $user User array
143          *
144          * @return void
145          */
146         private function assertOtherUser(array $user = [])
147         {
148                 self::assertEquals($this->otherUser['id'], $user['id']);
149                 self::assertEquals($this->otherUser['id'], $user['id_str']);
150                 self::assertEquals($this->otherUser['name'], $user['name']);
151                 self::assertEquals($this->otherUser['nick'], $user['screen_name']);
152                 self::assertFalse($user['verified']);
153         }
154
155         /**
156          * Assert that a status array contains expected keys.
157          *
158          * @param array $status Status array
159          *
160          * @return void
161          */
162         private function assertStatus(array $status = [])
163         {
164                 self::assertIsString($status['text'] ?? '');
165                 self::assertIsInt($status['id'] ?? '');
166                 // We could probably do more checks here.
167         }
168
169         /**
170          * Assert that a list array contains expected keys.
171          *
172          * @param array $list List array
173          *
174          * @return void
175          */
176         private function assertList(array $list = [])
177         {
178                 self::assertIsString($list['name']);
179                 self::assertIsInt($list['id']);
180                 self::assertIsString('string', $list['id_str']);
181                 self::assertContains($list['mode'], ['public', 'private']);
182                 // We could probably do more checks here.
183         }
184
185         /**
186          * Assert that the string is XML and contain the root element.
187          *
188          * @param string $result       XML string
189          * @param string $root_element Root element name
190          *
191          * @return void
192          */
193         private function assertXml($result = '', $root_element = '')
194         {
195                 self::assertStringStartsWith('<?xml version="1.0"?>', $result);
196                 self::assertStringContainsString('<' . $root_element, $result);
197                 // We could probably do more checks here.
198         }
199
200         /**
201          * Get the path to a temporary empty PNG image.
202          *
203          * @return string Path
204          */
205         private function getTempImage()
206         {
207                 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
208                 file_put_contents(
209                         $tmpFile,
210                         base64_decode(
211                         // Empty 1x1 px PNG image
212                                 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
213                         )
214                 );
215
216                 return $tmpFile;
217         }
218
219         /**
220          * Test the api_user() function.
221          *
222          * @return void
223          */
224         public function testApiUser()
225         {
226                 self::assertEquals($this->selfUser['id'], BaseApi::getCurrentUserID());
227         }
228
229         /**
230          * Test the api_user() function with an unallowed user.
231          *
232          * @return void
233          */
234         public function testApiUserWithUnallowedUser()
235         {
236                 // self::assertEquals(false, api_user());
237         }
238
239         /**
240          * Test the api_source() function.
241          *
242          * @return void
243          */
244         public function testApiSource()
245         {
246                 self::assertEquals('api', BasicAuth::getCurrentApplicationToken()['name']);
247         }
248
249         /**
250          * Test the api_source() function with a Twidere user agent.
251          *
252          * @return void
253          */
254         public function testApiSourceWithTwidere()
255         {
256                 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
257                 self::assertEquals('Twidere', BasicAuth::getCurrentApplicationToken()['name']);
258         }
259
260         /**
261          * Test the api_source() function with a GET parameter.
262          *
263          * @return void
264          */
265         public function testApiSourceWithGet()
266         {
267                 $_REQUEST['source'] = 'source_name';
268                 self::assertEquals('source_name', BasicAuth::getCurrentApplicationToken()['name']);
269         }
270
271         /**
272          * Test the api_date() function.
273          *
274          * @return void
275          */
276         public function testApiDate()
277         {
278                 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
279         }
280
281         /**
282          * Test the api_register_func() function.
283          *
284          * @return void
285          */
286         public function testApiRegisterFunc()
287         {
288                 global $API;
289                 self::assertNull(
290                         api_register_func(
291                                 'api_path',
292                                 function () {
293                                 },
294                                 true,
295                                 'method'
296                         )
297                 );
298                 self::assertTrue(is_callable($API['api_path']['func']));
299         }
300
301         /**
302          * Test the BasicAuth::getCurrentUserID() function without any login.
303          *
304          * @runInSeparateProcess
305          * @preserveGlobalState disabled
306          * @preserveGlobalState disabled
307          */
308         public function testApiLoginWithoutLogin()
309         {
310                 BasicAuth::setCurrentUserID();
311                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
312                 BasicAuth::getCurrentUserID(true);
313         }
314
315         /**
316          * Test the BasicAuth::getCurrentUserID() function with a bad login.
317          *
318          * @runInSeparateProcess
319          * @preserveGlobalState disabled
320          * @preserveGlobalState disabled
321          */
322         public function testApiLoginWithBadLogin()
323         {
324                 BasicAuth::setCurrentUserID();
325                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
326                 $_SERVER['PHP_AUTH_USER'] = 'user@server';
327                 BasicAuth::getCurrentUserID(true);
328         }
329
330         /**
331          * Test the BasicAuth::getCurrentUserID() function with oAuth.
332          *
333          * @return void
334          */
335         public function testApiLoginWithOauth()
336         {
337                 $this->markTestIncomplete('Can we test this easily?');
338         }
339
340         /**
341          * Test the BasicAuth::getCurrentUserID() function with authentication provided by an addon.
342          *
343          * @return void
344          */
345         public function testApiLoginWithAddonAuth()
346         {
347                 $this->markTestIncomplete('Can we test this easily?');
348         }
349
350         /**
351          * Test the BasicAuth::getCurrentUserID() function with a correct login.
352          *
353          * @runInSeparateProcess
354          * @preserveGlobalState disabled
355          * @doesNotPerformAssertions
356          */
357         public function testApiLoginWithCorrectLogin()
358         {
359                 BasicAuth::setCurrentUserID();
360                 $_SERVER['PHP_AUTH_USER'] = 'Test user';
361                 $_SERVER['PHP_AUTH_PW']   = 'password';
362                 BasicAuth::getCurrentUserID(true);
363         }
364
365         /**
366          * Test the BasicAuth::getCurrentUserID() function with a remote user.
367          *
368          * @runInSeparateProcess
369          * @preserveGlobalState disabled
370          */
371         public function testApiLoginWithRemoteUser()
372         {
373                 BasicAuth::setCurrentUserID();
374                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
375                 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
376                 BasicAuth::getCurrentUserID(true);
377         }
378
379         /**
380          * Test the api_call() function.
381          *
382          * @runInSeparateProcess
383          * @preserveGlobalState disabled
384          */
385         public function testApiCall()
386         {
387                 global $API;
388                 $API['api_path']           = [
389                         'method' => 'method',
390                         'func'   => function () {
391                                 return ['data' => ['some_data']];
392                         }
393                 ];
394                 $_SERVER['REQUEST_METHOD'] = 'method';
395                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
396                 $_GET['callback']          = 'callback_name';
397
398                 self::assertEquals(
399                         'callback_name(["some_data"])',
400                         api_call('api_path', 'json')
401                 );
402         }
403
404         /**
405          * Test the api_call() function with the profiled enabled.
406          *
407          * @runInSeparateProcess
408          * @preserveGlobalState disabled
409          */
410         public function testApiCallWithProfiler()
411         {
412                 global $API;
413                 $API['api_path']           = [
414                         'method' => 'method',
415                         'func'   => function () {
416                                 return ['data' => ['some_data']];
417                         }
418                 ];
419
420                 $_SERVER['REQUEST_METHOD'] = 'method';
421                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
422
423                 $this->config->set('system', 'profiler', true);
424                 $this->config->set('rendertime', 'callstack', true);
425                 $this->app->callstack = [
426                         'database'       => ['some_function' => 200],
427                         'database_write' => ['some_function' => 200],
428                         'cache'          => ['some_function' => 200],
429                         'cache_write'    => ['some_function' => 200],
430                         'network'        => ['some_function' => 200]
431                 ];
432
433                 self::assertEquals(
434                         '["some_data"]',
435                         api_call('api_path', 'json')
436                 );
437         }
438
439         /**
440          * Test the api_call() function with a JSON result.
441          *
442          * @runInSeparateProcess
443          * @preserveGlobalState disabled
444          */
445         public function testApiCallWithJson()
446         {
447                 global $API;
448                 $API['api_path']           = [
449                         'method' => 'method',
450                         'func'   => function () {
451                                 return ['data' => ['some_data']];
452                         }
453                 ];
454                 $_SERVER['REQUEST_METHOD'] = 'method';
455                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
456
457                 self::assertEquals(
458                         '["some_data"]',
459                         api_call('api_path.json', 'json')
460                 );
461         }
462
463         /**
464          * Test the api_call() function with an XML result.
465          *
466          * @runInSeparateProcess
467          * @preserveGlobalState disabled
468          */
469         public function testApiCallWithXml()
470         {
471                 global $API;
472                 $API['api_path']           = [
473                         'method' => 'method',
474                         'func'   => function () {
475                                 return 'some_data';
476                         }
477                 ];
478                 $_SERVER['REQUEST_METHOD'] = 'method';
479                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
480
481                 $args = DI::args()->determine($_SERVER, $_GET);
482
483                 self::assertEquals(
484                         'some_data',
485                         api_call('api_path.xml', 'xml')
486                 );
487         }
488
489         /**
490          * Test the api_call() function with an RSS result.
491          *
492          * @runInSeparateProcess
493          * @preserveGlobalState disabled
494          */
495         public function testApiCallWithRss()
496         {
497                 global $API;
498                 $API['api_path']           = [
499                         'method' => 'method',
500                         'func'   => function () {
501                                 return 'some_data';
502                         }
503                 ];
504                 $_SERVER['REQUEST_METHOD'] = 'method';
505                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
506
507                 self::assertEquals(
508                         '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
509                         'some_data',
510                         api_call('api_path.rss', 'rss')
511                 );
512         }
513
514         /**
515          * Test the api_call() function with an Atom result.
516          *
517          * @runInSeparateProcess
518          * @preserveGlobalState disabled
519          */
520         public function testApiCallWithAtom()
521         {
522                 global $API;
523                 $API['api_path']           = [
524                         'method' => 'method',
525                         'func'   => function () {
526                                 return 'some_data';
527                         }
528                 ];
529                 $_SERVER['REQUEST_METHOD'] = 'method';
530                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
531
532                 self::assertEquals(
533                         '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
534                         'some_data',
535                         api_call('api_path.atom', 'atom')
536                 );
537         }
538
539         /**
540          * Test the api_rss_extra() function.
541          *
542          * @return void
543          */
544         public function testApiRssExtra()
545         {
546                 /*
547                 $user_info = ['url' => 'user_url', 'lang' => 'en'];
548                 $result    = api_rss_extra([], $user_info);
549                 self::assertEquals($user_info, $result['$user']);
550                 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
551                 self::assertArrayHasKey('self', $result['$rss']);
552                 self::assertArrayHasKey('base', $result['$rss']);
553                 self::assertArrayHasKey('updated', $result['$rss']);
554                 self::assertArrayHasKey('atom_updated', $result['$rss']);
555                 self::assertArrayHasKey('language', $result['$rss']);
556                 self::assertArrayHasKey('logo', $result['$rss']);
557                 */
558         }
559
560         /**
561          * Test the api_rss_extra() function without any user info.
562          *
563          * @return void
564          */
565         public function testApiRssExtraWithoutUserInfo()
566         {
567                 /*
568                 $result = api_rss_extra([], null);
569                 self::assertIsArray($result['$user']);
570                 self::assertArrayHasKey('alternate', $result['$rss']);
571                 self::assertArrayHasKey('self', $result['$rss']);
572                 self::assertArrayHasKey('base', $result['$rss']);
573                 self::assertArrayHasKey('updated', $result['$rss']);
574                 self::assertArrayHasKey('atom_updated', $result['$rss']);
575                 self::assertArrayHasKey('language', $result['$rss']);
576                 self::assertArrayHasKey('logo', $result['$rss']);
577                 */
578         }
579
580         /**
581          * Test the api_get_user() function.
582          *
583          * @return void
584          */
585         public function testApiGetUser()
586         {
587                 // $user = api_get_user();
588                 // self::assertSelfUser($user);
589                 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
590                 // self::assertEquals('6fdbe8', $user['profile_link_color']);
591                 // self::assertEquals('ededed', $user['profile_background_color']);
592         }
593
594         /**
595          * Test the api_get_user() function with a Frio schema.
596          *
597          * @return void
598          */
599         public function testApiGetUserWithFrioSchema()
600         {
601                 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
602                 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
603                 // $user = api_get_user();
604                 // self::assertSelfUser($user);
605                 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
606                 // self::assertEquals('6fdbe8', $user['profile_link_color']);
607                 // self::assertEquals('ededed', $user['profile_background_color']);
608         }
609
610         /**
611          * Test the api_get_user() function with an empty Frio schema.
612          *
613          * @return void
614          */
615         public function testApiGetUserWithEmptyFrioSchema()
616         {
617                 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
618                 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
619                 // $user = api_get_user();
620                 // self::assertSelfUser($user);
621                 // self::assertEquals('708fa0', $user['profile_sidebar_fill_color']);
622                 // self::assertEquals('6fdbe8', $user['profile_link_color']);
623                 // self::assertEquals('ededed', $user['profile_background_color']);
624         }
625
626         /**
627          * Test the api_get_user() function with a custom Frio schema.
628          *
629          * @return void
630          */
631         public function testApiGetUserWithCustomFrioSchema()
632         {
633                 // $pConfig = $this->dice->create(IManagePersonalConfigValues::class);
634                 // $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
635                 // $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
636                 // $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
637                 // $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
638                 // $user = api_get_user();
639                 // self::assertSelfUser($user);
640                 // self::assertEquals('123456', $user['profile_sidebar_fill_color']);
641                 // self::assertEquals('123456', $user['profile_link_color']);
642                 // self::assertEquals('123456', $user['profile_background_color']);
643         }
644
645         /**
646          * Test the api_get_user() function with an user that is not allowed to use the API.
647          *
648          * @runInSeparateProcess
649          * @preserveGlobalState disabled
650          */
651         public function testApiGetUserWithoutApiUser()
652         {
653                 // api_get_user() with empty parameters is not used anymore
654                 /*
655                 $_SERVER['PHP_AUTH_USER'] = 'Test user';
656                 $_SERVER['PHP_AUTH_PW']   = 'password';
657                 BasicAuth::setCurrentUserID();
658                 self::assertFalse(api_get_user());
659                 */
660         }
661
662         /**
663          * Test the api_get_user() function with an user ID in a GET parameter.
664          *
665          * @return void
666          */
667         public function testApiGetUserWithGetId()
668         {
669                 // self::assertOtherUser(api_get_user());
670         }
671
672         /**
673          * Test the api_get_user() function with a wrong user ID in a GET parameter.
674          *
675          * @return void
676          */
677         public function testApiGetUserWithWrongGetId()
678         {
679                 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
680                 // self::assertOtherUser(api_get_user());
681         }
682
683         /**
684          * Test the api_get_user() function with an user name in a GET parameter.
685          *
686          * @return void
687          */
688         public function testApiGetUserWithGetName()
689         {
690                 // self::assertSelfUser(api_get_user());
691         }
692
693         /**
694          * Test the api_get_user() function with a profile URL in a GET parameter.
695          *
696          * @return void
697          */
698         public function testApiGetUserWithGetUrl()
699         {
700                 // self::assertSelfUser(api_get_user());
701         }
702
703         /**
704          * Test the api_get_user() function with an user ID in the API path.
705          *
706          * @return void
707          */
708         public function testApiGetUserWithNumericCalledApi()
709         {
710                 // global $called_api;
711                 // $called_api         = ['api_path'];
712                 // DI::args()->setArgv(['', $this->otherUser['id'] . '.json']);
713                 // self::assertOtherUser(api_get_user());
714         }
715
716         /**
717          * Test the api_get_user() function with the $called_api global variable.
718          *
719          * @return void
720          */
721         public function testApiGetUserWithCalledApi()
722         {
723                 // global $called_api;
724                 // $called_api = ['api', 'api_path'];
725                 // self::assertSelfUser(api_get_user());
726         }
727
728         /**
729          * Test the Arrays::walkRecursive() function.
730          *
731          * @return void
732          */
733         public function testApiWalkRecursive()
734         {
735                 $array = ['item1'];
736                 self::assertEquals(
737                         $array,
738                         Arrays::walkRecursive(
739                                 $array,
740                                 function () {
741                                         // Should we test this with a callback that actually does something?
742                                         return true;
743                                 }
744                         )
745                 );
746         }
747
748         /**
749          * Test the Arrays::walkRecursive() function with an array.
750          *
751          * @return void
752          */
753         public function testApiWalkRecursiveWithArray()
754         {
755                 $array = [['item1'], ['item2']];
756                 self::assertEquals(
757                         $array,
758                         Arrays::walkRecursive(
759                                 $array,
760                                 function () {
761                                         // Should we test this with a callback that actually does something?
762                                         return true;
763                                 }
764                         )
765                 );
766         }
767
768         /**
769          * Test the BaseApi::reformatXML() function.
770          *
771          * @return void
772          */
773         public function testApiReformatXml()
774         {
775                 $item = true;
776                 $key  = '';
777                 self::assertTrue(ApiResponse::reformatXML($item, $key));
778                 self::assertEquals('true', $item);
779         }
780
781         /**
782          * Test the BaseApi::reformatXML() function with a statusnet_api key.
783          *
784          * @return void
785          */
786         public function testApiReformatXmlWithStatusnetKey()
787         {
788                 $item = '';
789                 $key  = 'statusnet_api';
790                 self::assertTrue(ApiResponse::reformatXML($item, $key));
791                 self::assertEquals('statusnet:api', $key);
792         }
793
794         /**
795          * Test the BaseApi::reformatXML() function with a friendica_api key.
796          *
797          * @return void
798          */
799         public function testApiReformatXmlWithFriendicaKey()
800         {
801                 $item = '';
802                 $key  = 'friendica_api';
803                 self::assertTrue(ApiResponse::reformatXML($item, $key));
804                 self::assertEquals('friendica:api', $key);
805         }
806
807         /**
808          * Test the BaseApi::createXML() function.
809          *
810          * @return void
811          */
812         public function testApiCreateXml()
813         {
814                 self::assertEquals(
815                         '<?xml version="1.0"?>' . "\n" .
816                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
817                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
818                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
819                         '  <data>some_data</data>' . "\n" .
820                         '</root_element>' . "\n",
821                         DI::apiResponse()->createXML(['data' => ['some_data']], 'root_element')
822                 );
823         }
824
825         /**
826          * Test the BaseApi::createXML() function without any XML namespace.
827          *
828          * @return void
829          */
830         public function testApiCreateXmlWithoutNamespaces()
831         {
832                 self::assertEquals(
833                         '<?xml version="1.0"?>' . "\n" .
834                         '<ok>' . "\n" .
835                         '  <data>some_data</data>' . "\n" .
836                         '</ok>' . "\n",
837                         DI::apiResponse()->createXML(['data' => ['some_data']], 'ok')
838                 );
839         }
840
841         /**
842          * Test the BaseApi::formatData() function.
843          *
844          * @return void
845          */
846         public function testApiFormatData()
847         {
848                 $data = ['some_data'];
849                 self::assertEquals($data, DI::apiResponse()->formatData('root_element', 'json', $data));
850         }
851
852         /**
853          * Test the BaseApi::formatData() function with an XML result.
854          *
855          * @return void
856          */
857         public function testApiFormatDataWithXml()
858         {
859                 self::assertEquals(
860                         '<?xml version="1.0"?>' . "\n" .
861                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
862                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
863                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
864                         '  <data>some_data</data>' . "\n" .
865                         '</root_element>' . "\n",
866                         DI::apiResponse()->formatData('root_element', 'xml', ['data' => ['some_data']])
867                 );
868         }
869
870         /**
871          * Test the api_statuses_mediap() function.
872          *
873          * @return void
874          */
875         public function testApiStatusesMediap()
876         {
877                 DI::args()->setArgc(2);
878
879                 $_FILES         = [
880                         'media' => [
881                                 'id'       => 666,
882                                 'size'     => 666,
883                                 'width'    => 666,
884                                 'height'   => 666,
885                                 'tmp_name' => $this->getTempImage(),
886                                 'name'     => 'spacer.png',
887                                 'type'     => 'image/png'
888                         ]
889                 ];
890                 $_GET['status'] = '<b>Status content</b>';
891
892                 $result = api_statuses_mediap('json');
893                 self::assertStatus($result['status']);
894         }
895
896         /**
897          * Test the api_statuses_mediap() function without an authenticated user.
898          *
899          * @return void
900          */
901         public function testApiStatusesMediapWithoutAuthenticatedUser()
902         {
903                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
904                 BasicAuth::setCurrentUserID();
905                 $_SESSION['authenticated'] = false;
906                 api_statuses_mediap('json');
907         }
908
909         /**
910          * Test the api_statuses_update() function.
911          *
912          * @return void
913          */
914         public function testApiStatusesUpdate()
915         {
916                 $_REQUEST['status']                = 'Status content #friendica';
917                 $_REQUEST['in_reply_to_status_id'] = -1;
918                 $_REQUEST['lat']                   = 48;
919                 $_REQUEST['long']                  = 7;
920                 $_FILES                            = [
921                         'media' => [
922                                 'id'       => 666,
923                                 'size'     => 666,
924                                 'width'    => 666,
925                                 'height'   => 666,
926                                 'tmp_name' => $this->getTempImage(),
927                                 'name'     => 'spacer.png',
928                                 'type'     => 'image/png'
929                         ]
930                 ];
931
932                 $result = api_statuses_update('json');
933                 self::assertStatus($result['status']);
934         }
935
936         /**
937          * Test the api_statuses_update() function with an HTML status.
938          *
939          * @return void
940          */
941         public function testApiStatusesUpdateWithHtml()
942         {
943                 $_REQUEST['htmlstatus'] = '<b>Status content</b>';
944
945                 $result = api_statuses_update('json');
946                 self::assertStatus($result['status']);
947         }
948
949         /**
950          * Test the api_statuses_update() function without an authenticated user.
951          *
952          * @return void
953          */
954         public function testApiStatusesUpdateWithoutAuthenticatedUser()
955         {
956                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
957                 BasicAuth::setCurrentUserID();
958                 $_SESSION['authenticated'] = false;
959                 api_statuses_update('json');
960         }
961
962         /**
963          * Test the api_statuses_update() function with a parent status.
964          *
965          * @return void
966          */
967         public function testApiStatusesUpdateWithParent()
968         {
969                 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
970         }
971
972         /**
973          * Test the api_statuses_update() function with a media_ids parameter.
974          *
975          * @return void
976          */
977         public function testApiStatusesUpdateWithMediaIds()
978         {
979                 $this->markTestIncomplete();
980         }
981
982         /**
983          * Test the api_statuses_update() function with the throttle limit reached.
984          *
985          * @return void
986          */
987         public function testApiStatusesUpdateWithDayThrottleReached()
988         {
989                 $this->markTestIncomplete();
990         }
991
992         /**
993          * Test the api_media_upload() function.
994          * @runInSeparateProcess
995          * @preserveGlobalState disabled
996          */
997         public function testApiMediaUpload()
998         {
999                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1000                 api_media_upload();
1001         }
1002
1003         /**
1004          * Test the api_media_upload() function without an authenticated user.
1005          *
1006          * @return void
1007          */
1008         public function testApiMediaUploadWithoutAuthenticatedUser()
1009         {
1010                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1011                 BasicAuth::setCurrentUserID();
1012                 $_SESSION['authenticated'] = false;
1013                 api_media_upload();
1014         }
1015
1016         /**
1017          * Test the api_media_upload() function with an invalid uploaded media.
1018          *
1019          * @return void
1020          */
1021         public function testApiMediaUploadWithMedia()
1022         {
1023                 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1024                 $_FILES = [
1025                         'media' => [
1026                                 'id'       => 666,
1027                                 'tmp_name' => 'tmp_name'
1028                         ]
1029                 ];
1030                 api_media_upload();
1031         }
1032
1033         /**
1034          * Test the api_media_upload() function with an valid uploaded media.
1035          *
1036          * @return void
1037          */
1038         public function testApiMediaUploadWithValidMedia()
1039         {
1040                 $_FILES    = [
1041                         'media' => [
1042                                 'id'       => 666,
1043                                 'size'     => 666,
1044                                 'width'    => 666,
1045                                 'height'   => 666,
1046                                 'tmp_name' => $this->getTempImage(),
1047                                 'name'     => 'spacer.png',
1048                                 'type'     => 'image/png'
1049                         ]
1050                 ];
1051                 $app       = DI::app();
1052                 DI::args()->setArgc(2);
1053
1054                 $result = api_media_upload();
1055                 self::assertEquals('image/png', $result['media']['image']['image_type']);
1056                 self::assertEquals(1, $result['media']['image']['w']);
1057                 self::assertEquals(1, $result['media']['image']['h']);
1058                 self::assertNotEmpty($result['media']['image']['friendica_preview_url']);
1059         }
1060
1061         /**
1062          * Test the api_statuses_repeat() function.
1063          *
1064          * @return void
1065          */
1066         public function testApiStatusesRepeat()
1067         {
1068                 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1069                 api_statuses_repeat('json');
1070         }
1071
1072         /**
1073          * Test the api_statuses_repeat() function without an authenticated user.
1074          *
1075          * @return void
1076          */
1077         public function testApiStatusesRepeatWithoutAuthenticatedUser()
1078         {
1079                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1080                 BasicAuth::setCurrentUserID();
1081                 $_SESSION['authenticated'] = false;
1082                 api_statuses_repeat('json');
1083         }
1084
1085         /**
1086          * Test the api_statuses_repeat() function with an ID.
1087          *
1088          * @return void
1089          */
1090         public function testApiStatusesRepeatWithId()
1091         {
1092                 DI::args()->setArgv(['', '', '', 1]);
1093                 $result = api_statuses_repeat('json');
1094                 self::assertStatus($result['status']);
1095
1096                 // Also test with a shared status
1097                 DI::args()->setArgv(['', '', '', 5]);
1098                 $result = api_statuses_repeat('json');
1099                 self::assertStatus($result['status']);
1100         }
1101
1102         /**
1103          * Test the api_favorites_create_destroy() function.
1104          *
1105          * @return void
1106          */
1107         public function testApiFavoritesCreateDestroy()
1108         {
1109                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1110                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1111                 api_favorites_create_destroy('json');
1112         }
1113
1114         /**
1115          * Test the api_favorites_create_destroy() function with an invalid ID.
1116          *
1117          * @return void
1118          */
1119         public function testApiFavoritesCreateDestroyWithInvalidId()
1120         {
1121                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1122                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1123                 api_favorites_create_destroy('json');
1124         }
1125
1126         /**
1127          * Test the api_favorites_create_destroy() function with an invalid action.
1128          *
1129          * @return void
1130          */
1131         public function testApiFavoritesCreateDestroyWithInvalidAction()
1132         {
1133                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1134                 DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1135                 $_REQUEST['id'] = 1;
1136                 api_favorites_create_destroy('json');
1137         }
1138
1139         /**
1140          * Test the api_favorites_create_destroy() function with the create action.
1141          *
1142          * @return void
1143          */
1144         public function testApiFavoritesCreateDestroyWithCreateAction()
1145         {
1146                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1147                 $_REQUEST['id'] = 3;
1148                 $result         = api_favorites_create_destroy('json');
1149                 self::assertStatus($result['status']);
1150         }
1151
1152         /**
1153          * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1154          *
1155          * @return void
1156          */
1157         public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1158         {
1159                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1160                 $_REQUEST['id'] = 3;
1161                 $result         = api_favorites_create_destroy('rss');
1162                 self::assertXml($result, 'status');
1163         }
1164
1165         /**
1166          * Test the api_favorites_create_destroy() function with the destroy action.
1167          *
1168          * @return void
1169          */
1170         public function testApiFavoritesCreateDestroyWithDestroyAction()
1171         {
1172                 DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1173                 $_REQUEST['id'] = 3;
1174                 $result         = api_favorites_create_destroy('json');
1175                 self::assertStatus($result['status']);
1176         }
1177
1178         /**
1179          * Test the api_favorites_create_destroy() function without an authenticated user.
1180          *
1181          * @return void
1182          */
1183         public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1184         {
1185                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1186                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1187                 BasicAuth::setCurrentUserID();
1188                 $_SESSION['authenticated'] = false;
1189                 api_favorites_create_destroy('json');
1190         }
1191
1192
1193
1194         /**
1195          * Test the api_format_messages() function.
1196          *
1197          * @return void
1198          */
1199         public function testApiFormatMessages()
1200         {
1201                 $result = api_format_messages(
1202                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1203                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1204                         ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1205                 );
1206                 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1207                 self::assertEquals(1, $result['id']);
1208                 self::assertEquals(2, $result['recipient_id']);
1209                 self::assertEquals(3, $result['sender_id']);
1210                 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1211                 self::assertEquals('sender_name', $result['sender_screen_name']);
1212         }
1213
1214         /**
1215          * Test the api_format_messages() function with HTML.
1216          *
1217          * @return void
1218          */
1219         public function testApiFormatMessagesWithHtmlText()
1220         {
1221                 $_GET['getText'] = 'html';
1222                 $result          = api_format_messages(
1223                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1224                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1225                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1226                 );
1227                 self::assertEquals('item_title', $result['title']);
1228                 self::assertEquals('<strong>item_body</strong>', $result['text']);
1229         }
1230
1231         /**
1232          * Test the api_format_messages() function with plain text.
1233          *
1234          * @return void
1235          */
1236         public function testApiFormatMessagesWithPlainText()
1237         {
1238                 $_GET['getText'] = 'plain';
1239                 $result          = api_format_messages(
1240                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1241                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1242                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1243                 );
1244                 self::assertEquals('item_title', $result['title']);
1245                 self::assertEquals('item_body', $result['text']);
1246         }
1247
1248         /**
1249          * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1250          *
1251          * @return void
1252          */
1253         public function testApiFormatMessagesWithoutUserObjects()
1254         {
1255                 $_GET['getUserObjects'] = 'false';
1256                 $result                 = api_format_messages(
1257                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1258                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1259                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1260                 );
1261                 self::assertTrue(!isset($result['sender']));
1262                 self::assertTrue(!isset($result['recipient']));
1263         }
1264
1265         /**
1266          * Test the api_convert_item() function.
1267          *
1268          * @return void
1269          */
1270         public function testApiConvertItem()
1271         {
1272                 /*
1273                 $result = api_convert_item(
1274                         [
1275                                 'network' => 'feed',
1276                                 'title'   => 'item_title',
1277                                 'uri-id'  => 1,
1278                                 // We need a long string to test that it is correctly cut
1279                                 'body'    => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
1280                                                          'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
1281                                                          'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
1282                                                          'laudantium atque commodi alias voluptatem non possimus aperiam ' .
1283                                                          'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
1284                                                          'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
1285                                                          'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
1286                                                          'veritatis nihil distinctio qui eum repellat officia illum quos ' .
1287                                                          'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
1288                                                          'omnis exercitationem quo magnam consequatur maxime aut illum ' .
1289                                                          'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
1290                                                          'temporibus corporis ratione blanditiis perspiciatis impedit ' .
1291                                                          'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
1292                                                          'sunt consequatur inventore dolor officiis pariatur doloremque ' .
1293                                                          'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
1294                                                          'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
1295                                                          'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
1296                                                          'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
1297                                                          'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
1298                                                          'repellat officia illum quos impedit quam iste esse unde qui ' .
1299                                                          'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
1300                                                          'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
1301                                                          'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
1302                                 'plink'   => 'item_plink'
1303                         ]
1304                 );
1305                 self::assertStringStartsWith('item_title', $result['text']);
1306                 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
1307                 */
1308         }
1309
1310         /**
1311          * Test the api_convert_item() function with an empty item body.
1312          *
1313          * @return void
1314          */
1315         public function testApiConvertItemWithoutBody()
1316         {
1317                 /*
1318                 $result = api_convert_item(
1319                         [
1320                                 'network' => 'feed',
1321                                 'title'   => 'item_title',
1322                                 'uri-id'  => -1,
1323                                 'body'    => '',
1324                                 'plink'   => 'item_plink'
1325                         ]
1326                 );
1327                 self::assertEquals("item_title", $result['text']);
1328                 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
1329                 */
1330         }
1331
1332         /**
1333          * Test the api_convert_item() function with the title in the body.
1334          *
1335          * @return void
1336          */
1337         public function testApiConvertItemWithTitleInBody()
1338         {
1339                 /*
1340                 $result = api_convert_item(
1341                         [
1342                                 'title'  => 'item_title',
1343                                 'body'   => 'item_title item_body',
1344                                 'uri-id' => 1,
1345                         ]
1346                 );
1347                 self::assertEquals('item_title item_body', $result['text']);
1348                 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
1349                 */
1350         }
1351
1352         /**
1353          * Test the api_get_attachments() function.
1354          *
1355          * @return void
1356          */
1357         public function testApiGetAttachments()
1358         {
1359                 // $body = 'body';
1360                 // self::assertEmpty(api_get_attachments($body, 0));
1361         }
1362
1363         /**
1364          * Test the api_get_attachments() function with an img tag.
1365          *
1366          * @return void
1367          */
1368         public function testApiGetAttachmentsWithImage()
1369         {
1370                 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
1371                 // self::assertIsArray(api_get_attachments($body, 0));
1372         }
1373
1374         /**
1375          * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
1376          *
1377          * @return void
1378          */
1379         public function testApiGetAttachmentsWithImageAndAndStatus()
1380         {
1381                 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
1382                 // $body                       = '[img]http://via.placeholder.com/1x1.png[/img]';
1383                 // self::assertIsArray(api_get_attachments($body, 0));
1384         }
1385
1386         /**
1387          * Test the api_get_entitities() function.
1388          *
1389          * @return void
1390          */
1391         public function testApiGetEntitities()
1392         {
1393                 // $text = 'text';
1394                 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
1395         }
1396
1397         /**
1398          * Test the api_get_entitities() function with the include_entities parameter.
1399          *
1400          * @return void
1401          */
1402         public function testApiGetEntititiesWithIncludeEntities()
1403         {
1404                 /*
1405                 $_REQUEST['include_entities'] = 'true';
1406                 $text                         = 'text';
1407                 $result                       = api_get_entitities($text, 'bbcode', 0);
1408                 self::assertIsArray($result['hashtags']);
1409                 self::assertIsArray($result['symbols']);
1410                 self::assertIsArray($result['urls']);
1411                 self::assertIsArray($result['user_mentions']);
1412                 */
1413         }
1414
1415         /**
1416          * Test the api_format_items_embeded_images() function.
1417          *
1418          * @return void
1419          */
1420         public function testApiFormatItemsEmbededImages()
1421         {
1422                 /*
1423                 self::assertEquals(
1424                         'text ' . DI::baseUrl() . '/display/item_guid',
1425                         api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
1426                 );
1427                 */
1428         }
1429
1430         /**
1431          * Test the api_format_items_activities() function.
1432          *
1433          * @return void
1434          */
1435         public function testApiFormatItemsActivities()
1436         {
1437                 $item   = ['uid' => 0, 'uri-id' => 1];
1438                 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
1439                 self::assertArrayHasKey('like', $result);
1440                 self::assertArrayHasKey('dislike', $result);
1441                 self::assertArrayHasKey('attendyes', $result);
1442                 self::assertArrayHasKey('attendno', $result);
1443                 self::assertArrayHasKey('attendmaybe', $result);
1444         }
1445
1446         /**
1447          * Test the api_format_items_activities() function with an XML result.
1448          *
1449          * @return void
1450          */
1451         public function testApiFormatItemsActivitiesWithXml()
1452         {
1453                 $item   = ['uid' => 0, 'uri-id' => 1];
1454                 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
1455                 self::assertArrayHasKey('friendica:like', $result);
1456                 self::assertArrayHasKey('friendica:dislike', $result);
1457                 self::assertArrayHasKey('friendica:attendyes', $result);
1458                 self::assertArrayHasKey('friendica:attendno', $result);
1459                 self::assertArrayHasKey('friendica:attendmaybe', $result);
1460         }
1461
1462         /**
1463          * Test the api_format_items() function.
1464          * @doesNotPerformAssertions
1465          */
1466         public function testApiFormatItems()
1467         {
1468                 /*
1469                 $items = Post::selectToArray([], ['uid' => 42]);
1470                 foreach ($items as $item) {
1471                         $status = api_format_item($item);
1472                         self::assertStatus($status);
1473                 }
1474                 */
1475         }
1476
1477         /**
1478          * Test the api_format_items() function with an XML result.
1479          * @doesNotPerformAssertions
1480          */
1481         public function testApiFormatItemsWithXml()
1482         {
1483                 /*
1484                 $items = Post::selectToArray([], ['uid' => 42]);
1485                 foreach ($items as $item) {
1486                         $status = api_format_item($item, 'xml');
1487                         self::assertStatus($status);
1488                 }
1489                 */
1490         }
1491
1492         /**
1493          * Test the api_lists_list() function.
1494          *
1495          * @return void
1496          */
1497         public function testApiListsList()
1498         {
1499                 $result = api_lists_list('json');
1500                 self::assertEquals(['lists_list' => []], $result);
1501         }
1502
1503         /**
1504          * Test the api_lists_ownerships() function.
1505          *
1506          * @return void
1507          */
1508         public function testApiListsOwnerships()
1509         {
1510                 $result = api_lists_ownerships('json');
1511                 foreach ($result['lists']['lists'] as $list) {
1512                         self::assertList($list);
1513                 }
1514         }
1515
1516         /**
1517          * Test the api_lists_ownerships() function without an authenticated user.
1518          *
1519          * @return void
1520          */
1521         public function testApiListsOwnershipsWithoutAuthenticatedUser()
1522         {
1523                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1524                 BasicAuth::setCurrentUserID();
1525                 $_SESSION['authenticated'] = false;
1526                 api_lists_ownerships('json');
1527         }
1528
1529         /**
1530          * Test the api_statuses_f() function.
1531          *
1532          * @return void
1533          */
1534         public function testApiStatusesFWithIncoming()
1535         {
1536                 // $result = api_statuses_f('incoming');
1537                 // self::assertArrayHasKey('user', $result);
1538         }
1539
1540
1541         /**
1542          * Test the api_direct_messages_new() function.
1543          *
1544          * @return void
1545          */
1546         public function testApiDirectMessagesNew()
1547         {
1548                 $result = api_direct_messages_new('json');
1549                 self::assertNull($result);
1550         }
1551
1552         /**
1553          * Test the api_direct_messages_new() function without an authenticated user.
1554          *
1555          * @return void
1556          */
1557         public function testApiDirectMessagesNewWithoutAuthenticatedUser()
1558         {
1559                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1560                 BasicAuth::setCurrentUserID();
1561                 $_SESSION['authenticated'] = false;
1562                 api_direct_messages_new('json');
1563         }
1564
1565         /**
1566          * Test the api_direct_messages_new() function with an user ID.
1567          *
1568          * @return void
1569          */
1570         public function testApiDirectMessagesNewWithUserId()
1571         {
1572                 $_POST['text']       = 'message_text';
1573                 $_REQUEST['user_id'] = $this->otherUser['id'];
1574                 $result           = api_direct_messages_new('json');
1575                 self::assertEquals(['direct_message' => ['error' => -1]], $result);
1576         }
1577
1578         /**
1579          * Test the api_direct_messages_new() function with a screen name.
1580          *
1581          * @return void
1582          */
1583         public function testApiDirectMessagesNewWithScreenName()
1584         {
1585                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1586                 $_POST['text']       = 'message_text';
1587                 $_REQUEST['user_id'] = $this->friendUser['id'];
1588                 $result              = api_direct_messages_new('json');
1589                 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1590                 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1591                 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1592         }
1593
1594         /**
1595          * Test the api_direct_messages_new() function with a title.
1596          *
1597          * @return void
1598          */
1599         public function testApiDirectMessagesNewWithTitle()
1600         {
1601                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1602                 $_POST['text']        = 'message_text';
1603                 $_REQUEST['user_id']  = $this->friendUser['id'];
1604                 $_REQUEST['title']    = 'message_title';
1605                 $result            = api_direct_messages_new('json');
1606                 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1607                 self::assertStringContainsString('message_title', $result['direct_message']['text']);
1608                 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1609                 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1610         }
1611
1612         /**
1613          * Test the api_direct_messages_new() function with an RSS result.
1614          *
1615          * @return void
1616          */
1617         public function testApiDirectMessagesNewWithRss()
1618         {
1619                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1620                 $_POST['text']       = 'message_text';
1621                 $_REQUEST['user_id'] = $this->friendUser['id'];
1622                 $result              = api_direct_messages_new('rss');
1623                 self::assertXml($result, 'direct-messages');
1624         }
1625
1626         /**
1627          * Test the api_direct_messages_destroy() function.
1628          *
1629          * @return void
1630          */
1631         public function testApiDirectMessagesDestroy()
1632         {
1633                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1634                 api_direct_messages_destroy('json');
1635         }
1636
1637         /**
1638          * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
1639          *
1640          * @return void
1641          */
1642         public function testApiDirectMessagesDestroyWithVerbose()
1643         {
1644                 $_GET['friendica_verbose'] = 'true';
1645                 $result                    = api_direct_messages_destroy('json');
1646                 self::assertEquals(
1647                         [
1648                                 '$result' => [
1649                                         'result'  => 'error',
1650                                         'message' => 'message id or parenturi not specified'
1651                                 ]
1652                         ],
1653                         $result
1654                 );
1655         }
1656
1657         /**
1658          * Test the api_direct_messages_destroy() function without an authenticated user.
1659          *
1660          * @return void
1661          */
1662         public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
1663         {
1664                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1665                 BasicAuth::setCurrentUserID();
1666                 $_SESSION['authenticated'] = false;
1667                 api_direct_messages_destroy('json');
1668         }
1669
1670         /**
1671          * Test the api_direct_messages_destroy() function with a non-zero ID.
1672          *
1673          * @return void
1674          */
1675         public function testApiDirectMessagesDestroyWithId()
1676         {
1677                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1678                 $_REQUEST['id'] = 1;
1679                 api_direct_messages_destroy('json');
1680         }
1681
1682         /**
1683          * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
1684          *
1685          * @return void
1686          */
1687         public function testApiDirectMessagesDestroyWithIdAndVerbose()
1688         {
1689                 $_REQUEST['id']                  = 1;
1690                 $_REQUEST['friendica_parenturi'] = 'parent_uri';
1691                 $_GET['friendica_verbose']       = 'true';
1692                 $result                          = api_direct_messages_destroy('json');
1693                 self::assertEquals(
1694                         [
1695                                 '$result' => [
1696                                         'result'  => 'error',
1697                                         'message' => 'message id not in database'
1698                                 ]
1699                         ],
1700                         $result
1701                 );
1702         }
1703
1704         /**
1705          * Test the api_direct_messages_destroy() function with a non-zero ID.
1706          *
1707          * @return void
1708          */
1709         public function testApiDirectMessagesDestroyWithCorrectId()
1710         {
1711                 $this->markTestIncomplete('We need to add a dataset for this.');
1712         }
1713
1714         /**
1715          * Test the api_direct_messages_box() function.
1716          *
1717          * @return void
1718          */
1719         public function testApiDirectMessagesBoxWithSentbox()
1720         {
1721                 $_REQUEST['page']   = -1;
1722                 $_REQUEST['max_id'] = 10;
1723                 $result             = api_direct_messages_box('json', 'sentbox', 'false');
1724                 self::assertArrayHasKey('direct_message', $result);
1725         }
1726
1727         /**
1728          * Test the api_direct_messages_box() function.
1729          *
1730          * @return void
1731          */
1732         public function testApiDirectMessagesBoxWithConversation()
1733         {
1734                 $result = api_direct_messages_box('json', 'conversation', 'false');
1735                 self::assertArrayHasKey('direct_message', $result);
1736         }
1737
1738         /**
1739          * Test the api_direct_messages_box() function.
1740          *
1741          * @return void
1742          */
1743         public function testApiDirectMessagesBoxWithAll()
1744         {
1745                 $result = api_direct_messages_box('json', 'all', 'false');
1746                 self::assertArrayHasKey('direct_message', $result);
1747         }
1748
1749         /**
1750          * Test the api_direct_messages_box() function.
1751          *
1752          * @return void
1753          */
1754         public function testApiDirectMessagesBoxWithInbox()
1755         {
1756                 $result = api_direct_messages_box('json', 'inbox', 'false');
1757                 self::assertArrayHasKey('direct_message', $result);
1758         }
1759
1760         /**
1761          * Test the api_direct_messages_box() function.
1762          *
1763          * @return void
1764          */
1765         public function testApiDirectMessagesBoxWithVerbose()
1766         {
1767                 $result = api_direct_messages_box('json', 'sentbox', 'true');
1768                 self::assertEquals(
1769                         [
1770                                 '$result' => [
1771                                         'result'  => 'error',
1772                                         'message' => 'no mails available'
1773                                 ]
1774                         ],
1775                         $result
1776                 );
1777         }
1778
1779         /**
1780          * Test the api_direct_messages_box() function with a RSS result.
1781          *
1782          * @return void
1783          */
1784         public function testApiDirectMessagesBoxWithRss()
1785         {
1786                 $result = api_direct_messages_box('rss', 'sentbox', 'false');
1787                 self::assertXml($result, 'direct-messages');
1788         }
1789
1790         /**
1791          * Test the api_direct_messages_box() function without an authenticated user.
1792          *
1793          * @return void
1794          */
1795         public function testApiDirectMessagesBoxWithUnallowedUser()
1796         {
1797                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1798                 BasicAuth::setCurrentUserID();
1799                 api_direct_messages_box('json', 'sentbox', 'false');
1800         }
1801
1802         /**
1803          * Test the api_direct_messages_sentbox() function.
1804          *
1805          * @return void
1806          */
1807         public function testApiDirectMessagesSentbox()
1808         {
1809                 $result = api_direct_messages_sentbox('json');
1810                 self::assertArrayHasKey('direct_message', $result);
1811         }
1812
1813         /**
1814          * Test the api_direct_messages_inbox() function.
1815          *
1816          * @return void
1817          */
1818         public function testApiDirectMessagesInbox()
1819         {
1820                 $result = api_direct_messages_inbox('json');
1821                 self::assertArrayHasKey('direct_message', $result);
1822         }
1823
1824         /**
1825          * Test the api_direct_messages_all() function.
1826          *
1827          * @return void
1828          */
1829         public function testApiDirectMessagesAll()
1830         {
1831                 $result = api_direct_messages_all('json');
1832                 self::assertArrayHasKey('direct_message', $result);
1833         }
1834
1835         /**
1836          * Test the api_direct_messages_conversation() function.
1837          *
1838          * @return void
1839          */
1840         public function testApiDirectMessagesConversation()
1841         {
1842                 $result = api_direct_messages_conversation('json');
1843                 self::assertArrayHasKey('direct_message', $result);
1844         }
1845
1846         /**
1847          * Test the api_oauth_request_token() function.
1848          *
1849          * @return void
1850          */
1851         public function testApiOauthRequestToken()
1852         {
1853                 $this->markTestIncomplete('exit() kills phpunit as well');
1854         }
1855
1856         /**
1857          * Test the api_oauth_access_token() function.
1858          *
1859          * @return void
1860          */
1861         public function testApiOauthAccessToken()
1862         {
1863                 $this->markTestIncomplete('exit() kills phpunit as well');
1864         }
1865
1866         /**
1867          * Test the api_fr_photos_list() function.
1868          *
1869          * @return void
1870          */
1871         public function testApiFrPhotosList()
1872         {
1873                 $result = api_fr_photos_list('json');
1874                 self::assertArrayHasKey('photo', $result);
1875         }
1876
1877         /**
1878          * Test the api_fr_photos_list() function without an authenticated user.
1879          *
1880          * @return void
1881          */
1882         public function testApiFrPhotosListWithoutAuthenticatedUser()
1883         {
1884                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1885                 BasicAuth::setCurrentUserID();
1886                 $_SESSION['authenticated'] = false;
1887                 api_fr_photos_list('json');
1888         }
1889
1890         /**
1891          * Test the api_fr_photo_create_update() function.
1892          */
1893         public function testApiFrPhotoCreateUpdate()
1894         {
1895                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1896                 api_fr_photo_create_update('json');
1897         }
1898
1899         /**
1900          * Test the api_fr_photo_create_update() function without an authenticated user.
1901          *
1902          * @return void
1903          */
1904         public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
1905         {
1906                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1907                 BasicAuth::setCurrentUserID();
1908                 $_SESSION['authenticated'] = false;
1909                 api_fr_photo_create_update('json');
1910         }
1911
1912         /**
1913          * Test the api_fr_photo_create_update() function with an album name.
1914          *
1915          * @return void
1916          */
1917         public function testApiFrPhotoCreateUpdateWithAlbum()
1918         {
1919                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1920                 $_REQUEST['album'] = 'album_name';
1921                 api_fr_photo_create_update('json');
1922         }
1923
1924         /**
1925          * Test the api_fr_photo_create_update() function with the update mode.
1926          *
1927          * @return void
1928          */
1929         public function testApiFrPhotoCreateUpdateWithUpdate()
1930         {
1931                 $this->markTestIncomplete('We need to create a dataset for this');
1932         }
1933
1934         /**
1935          * Test the api_fr_photo_create_update() function with an uploaded file.
1936          *
1937          * @return void
1938          */
1939         public function testApiFrPhotoCreateUpdateWithFile()
1940         {
1941                 $this->markTestIncomplete();
1942         }
1943
1944         /**
1945          * Test the api_fr_photo_detail() function.
1946          *
1947          * @return void
1948          */
1949         public function testApiFrPhotoDetail()
1950         {
1951                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1952                 api_fr_photo_detail('json');
1953         }
1954
1955         /**
1956          * Test the api_fr_photo_detail() function without an authenticated user.
1957          *
1958          * @return void
1959          */
1960         public function testApiFrPhotoDetailWithoutAuthenticatedUser()
1961         {
1962                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1963                 BasicAuth::setCurrentUserID();
1964                 $_SESSION['authenticated'] = false;
1965                 api_fr_photo_detail('json');
1966         }
1967
1968         /**
1969          * Test the api_fr_photo_detail() function with a photo ID.
1970          *
1971          * @return void
1972          */
1973         public function testApiFrPhotoDetailWithPhotoId()
1974         {
1975                 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1976                 $_REQUEST['photo_id'] = 1;
1977                 api_fr_photo_detail('json');
1978         }
1979
1980         /**
1981          * Test the api_fr_photo_detail() function with a correct photo ID.
1982          *
1983          * @return void
1984          */
1985         public function testApiFrPhotoDetailCorrectPhotoId()
1986         {
1987                 $this->markTestIncomplete('We need to create a dataset for this.');
1988         }
1989
1990         /**
1991          * Test the api_account_update_profile_image() function.
1992          *
1993          * @return void
1994          */
1995         public function testApiAccountUpdateProfileImage()
1996         {
1997                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1998                 api_account_update_profile_image('json');
1999         }
2000
2001         /**
2002          * Test the api_account_update_profile_image() function without an authenticated user.
2003          *
2004          * @return void
2005          */
2006         public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2007         {
2008                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2009                 BasicAuth::setCurrentUserID();
2010                 $_SESSION['authenticated'] = false;
2011                 api_account_update_profile_image('json');
2012         }
2013
2014         /**
2015          * Test the api_account_update_profile_image() function with an uploaded file.
2016          *
2017          * @return void
2018          */
2019         public function testApiAccountUpdateProfileImageWithUpload()
2020         {
2021                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2022                 $this->markTestIncomplete();
2023         }
2024
2025         /**
2026          * Test the check_acl_input() function.
2027          *
2028          * @return void
2029          */
2030         public function testCheckAclInput()
2031         {
2032                 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
2033                 // Where does this result come from?
2034                 self::assertEquals(1, $result);
2035         }
2036
2037         /**
2038          * Test the check_acl_input() function with an empty ACL string.
2039          *
2040          * @return void
2041          */
2042         public function testCheckAclInputWithEmptyAclString()
2043         {
2044                 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
2045                 self::assertFalse($result);
2046         }
2047
2048         /**
2049          * Test the save_media_to_database() function.
2050          *
2051          * @return void
2052          */
2053         public function testSaveMediaToDatabase()
2054         {
2055                 $this->markTestIncomplete();
2056         }
2057
2058         /**
2059          * Test the post_photo_item() function.
2060          *
2061          * @return void
2062          */
2063         public function testPostPhotoItem()
2064         {
2065                 $this->markTestIncomplete();
2066         }
2067
2068         /**
2069          * Test the prepare_photo_data() function.
2070          *
2071          * @return void
2072          */
2073         public function testPreparePhotoData()
2074         {
2075                 $this->markTestIncomplete();
2076         }
2077
2078         /**
2079          * Test the api_share_as_retweet() function with a valid item.
2080          *
2081          * @return void
2082          */
2083         public function testApiShareAsRetweetWithValidItem()
2084         {
2085                 $this->markTestIncomplete();
2086         }
2087
2088         /**
2089          * Test the api_in_reply_to() function with a valid item.
2090          *
2091          * @return void
2092          */
2093         public function testApiInReplyToWithValidItem()
2094         {
2095                 $this->markTestIncomplete();
2096         }
2097
2098         /**
2099          * Test the api_clean_plain_items() function.
2100          *
2101          * @return void
2102          */
2103         public function testApiCleanPlainItems()
2104         {
2105                 $_REQUEST['include_entities'] = 'true';
2106                 $result                       = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
2107                 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
2108         }
2109
2110         /**
2111          * Test the api_best_nickname() function with contacts.
2112          *
2113          * @return void
2114          */
2115         public function testApiBestNicknameWithContacts()
2116         {
2117                 $this->markTestIncomplete();
2118         }
2119
2120         /**
2121          * Test the api_friendica_group_show() function.
2122          *
2123          * @return void
2124          */
2125         public function testApiFriendicaGroupShow()
2126         {
2127                 $this->markTestIncomplete();
2128         }
2129
2130         /**
2131          * Test the api_friendica_group_delete() function.
2132          *
2133          * @return void
2134          */
2135         public function testApiFriendicaGroupDelete()
2136         {
2137                 $this->markTestIncomplete();
2138         }
2139
2140         /**
2141          * Test the api_lists_destroy() function.
2142          *
2143          * @return void
2144          */
2145         public function testApiListsDestroy()
2146         {
2147                 $this->markTestIncomplete();
2148         }
2149
2150         /**
2151          * Test the group_create() function.
2152          *
2153          * @return void
2154          */
2155         public function testGroupCreate()
2156         {
2157                 $this->markTestIncomplete();
2158         }
2159
2160         /**
2161          * Test the api_friendica_group_create() function.
2162          *
2163          * @return void
2164          */
2165         public function testApiFriendicaGroupCreate()
2166         {
2167                 $this->markTestIncomplete();
2168         }
2169
2170         /**
2171          * Test the api_lists_create() function.
2172          *
2173          * @return void
2174          */
2175         public function testApiListsCreate()
2176         {
2177                 $this->markTestIncomplete();
2178         }
2179
2180         /**
2181          * Test the api_friendica_group_update() function.
2182          *
2183          * @return void
2184          */
2185         public function testApiFriendicaGroupUpdate()
2186         {
2187                 $this->markTestIncomplete();
2188         }
2189
2190         /**
2191          * Test the api_lists_update() function.
2192          *
2193          * @return void
2194          */
2195         public function testApiListsUpdate()
2196         {
2197                 $this->markTestIncomplete();
2198         }
2199
2200         /**
2201          * Test the api_friendica_activity() function.
2202          *
2203          * @return void
2204          */
2205         public function testApiFriendicaActivity()
2206         {
2207                 $this->markTestIncomplete();
2208         }
2209
2210         /**
2211          * Test the api_friendica_notification_seen() function.
2212          *
2213          * @return void
2214          */
2215         public function testApiFriendicaNotificationSeen()
2216         {
2217                 $this->markTestIncomplete();
2218         }
2219
2220         /**
2221          * Test the api_friendica_direct_messages_setseen() function.
2222          *
2223          * @return void
2224          */
2225         public function testApiFriendicaDirectMessagesSetseen()
2226         {
2227                 $this->markTestIncomplete();
2228         }
2229
2230         /**
2231          * Test the api_friendica_direct_messages_search() function.
2232          *
2233          * @return void
2234          */
2235         public function testApiFriendicaDirectMessagesSearch()
2236         {
2237                 $this->markTestIncomplete();
2238         }
2239 }