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