]> git.mxchange.org Git - friendica.git/blob - tests/legacy/ApiTest.php
Tests deactivated
[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                 DI::args()->setArgc(2);
879
880                 $_FILES         = [
881                         'media' => [
882                                 'id'       => 666,
883                                 'size'     => 666,
884                                 'width'    => 666,
885                                 'height'   => 666,
886                                 'tmp_name' => $this->getTempImage(),
887                                 'name'     => 'spacer.png',
888                                 'type'     => 'image/png'
889                         ]
890                 ];
891                 $_GET['status'] = '<b>Status content</b>';
892
893                 $result = api_statuses_mediap('json');
894                 self::assertStatus($result['status']);
895         }
896
897         /**
898          * Test the api_statuses_mediap() function without an authenticated user.
899          *
900          * @return void
901          */
902         public function testApiStatusesMediapWithoutAuthenticatedUser()
903         {
904                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
905                 BasicAuth::setCurrentUserID();
906                 $_SESSION['authenticated'] = false;
907                 api_statuses_mediap('json');
908         }
909
910         /**
911          * Test the api_statuses_update() function.
912          *
913          * @return void
914          */
915         public function testApiStatusesUpdate()
916         {
917                 $_REQUEST['status']                = 'Status content #friendica';
918                 $_REQUEST['in_reply_to_status_id'] = -1;
919                 $_REQUEST['lat']                   = 48;
920                 $_REQUEST['long']                  = 7;
921                 $_FILES                            = [
922                         'media' => [
923                                 'id'       => 666,
924                                 'size'     => 666,
925                                 'width'    => 666,
926                                 'height'   => 666,
927                                 'tmp_name' => $this->getTempImage(),
928                                 'name'     => 'spacer.png',
929                                 'type'     => 'image/png'
930                         ]
931                 ];
932
933                 $result = api_statuses_update('json');
934                 self::assertStatus($result['status']);
935         }
936
937         /**
938          * Test the api_statuses_update() function with an HTML status.
939          *
940          * @return void
941          */
942         public function testApiStatusesUpdateWithHtml()
943         {
944                 $_REQUEST['htmlstatus'] = '<b>Status content</b>';
945
946                 $result = api_statuses_update('json');
947                 self::assertStatus($result['status']);
948         }
949
950         /**
951          * Test the api_statuses_update() function without an authenticated user.
952          *
953          * @return void
954          */
955         public function testApiStatusesUpdateWithoutAuthenticatedUser()
956         {
957                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
958                 BasicAuth::setCurrentUserID();
959                 $_SESSION['authenticated'] = false;
960                 api_statuses_update('json');
961         }
962
963         /**
964          * Test the api_statuses_update() function with a parent status.
965          *
966          * @return void
967          */
968         public function testApiStatusesUpdateWithParent()
969         {
970                 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
971         }
972
973         /**
974          * Test the api_statuses_update() function with a media_ids parameter.
975          *
976          * @return void
977          */
978         public function testApiStatusesUpdateWithMediaIds()
979         {
980                 $this->markTestIncomplete();
981         }
982
983         /**
984          * Test the api_statuses_update() function with the throttle limit reached.
985          *
986          * @return void
987          */
988         public function testApiStatusesUpdateWithDayThrottleReached()
989         {
990                 $this->markTestIncomplete();
991         }
992
993         /**
994          * Test the \Friendica\Module\Api\Twitter\Media\Upload module.
995          * @runInSeparateProcess
996          * @preserveGlobalState disabled
997          */
998         public function testApiMediaUpload()
999         {
1000                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1001                 (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
1002         }
1003
1004         /**
1005          * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user.
1006          *
1007          * @return void
1008          */
1009         public function testApiMediaUploadWithoutAuthenticatedUser()
1010         {
1011                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1012                 BasicAuth::setCurrentUserID();
1013                 $_SESSION['authenticated'] = false;
1014                 (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
1015         }
1016
1017         /**
1018          * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media.
1019          *
1020          * @return void
1021          */
1022         public function testApiMediaUploadWithMedia()
1023         {
1024                 $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class);
1025                 $_FILES = [
1026                         'media' => [
1027                                 'id'       => 666,
1028                                 'tmp_name' => 'tmp_name'
1029                         ]
1030                 ];
1031                 (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
1032         }
1033
1034         /**
1035          * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media.
1036          *
1037          * @return void
1038          */
1039         public function testApiMediaUploadWithValidMedia()
1040         {
1041                 $_FILES    = [
1042                         'media' => [
1043                                 'id'       => 666,
1044                                 'size'     => 666,
1045                                 'width'    => 666,
1046                                 'height'   => 666,
1047                                 'tmp_name' => $this->getTempImage(),
1048                                 'name'     => 'spacer.png',
1049                                 'type'     => 'image/png'
1050                         ]
1051                 ];
1052
1053                 $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run();
1054                 $media = json_decode($response->getBody(), true);
1055
1056                 self::assertEquals('image/png', $media['image']['image_type']);
1057                 self::assertEquals(1, $media['image']['w']);
1058                 self::assertEquals(1, $media['image']['h']);
1059                 self::assertNotEmpty($media['image']['friendica_preview_url']);
1060         }
1061
1062         /**
1063          * Test the api_statuses_repeat() function.
1064          *
1065          * @return void
1066          */
1067         public function testApiStatusesRepeat()
1068         {
1069                 $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
1070                 api_statuses_repeat('json');
1071         }
1072
1073         /**
1074          * Test the api_statuses_repeat() function without an authenticated user.
1075          *
1076          * @return void
1077          */
1078         public function testApiStatusesRepeatWithoutAuthenticatedUser()
1079         {
1080                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1081                 BasicAuth::setCurrentUserID();
1082                 $_SESSION['authenticated'] = false;
1083                 api_statuses_repeat('json');
1084         }
1085
1086         /**
1087          * Test the api_statuses_repeat() function with an ID.
1088          *
1089          * @return void
1090          */
1091         public function testApiStatusesRepeatWithId()
1092         {
1093                 DI::args()->setArgv(['', '', '', 1]);
1094                 $result = api_statuses_repeat('json');
1095                 self::assertStatus($result['status']);
1096
1097                 // Also test with a shared status
1098                 DI::args()->setArgv(['', '', '', 5]);
1099                 $result = api_statuses_repeat('json');
1100                 self::assertStatus($result['status']);
1101         }
1102
1103         /**
1104          * Test the api_favorites_create_destroy() function.
1105          *
1106          * @return void
1107          */
1108         public function testApiFavoritesCreateDestroy()
1109         {
1110                 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1111                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create']);
1112                 // api_favorites_create_destroy('json');
1113         }
1114
1115         /**
1116          * Test the api_favorites_create_destroy() function with an invalid ID.
1117          *
1118          * @return void
1119          */
1120         public function testApiFavoritesCreateDestroyWithInvalidId()
1121         {
1122                 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1123                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create', '12.json']);
1124                 // api_favorites_create_destroy('json');
1125         }
1126
1127         /**
1128          * Test the api_favorites_create_destroy() function with an invalid action.
1129          *
1130          * @return void
1131          */
1132         public function testApiFavoritesCreateDestroyWithInvalidAction()
1133         {
1134                 // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1135                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'change.json']);
1136                 // $_REQUEST['id'] = 1;
1137                 // api_favorites_create_destroy('json');
1138         }
1139
1140         /**
1141          * Test the api_favorites_create_destroy() function with the create action.
1142          *
1143          * @return void
1144          */
1145         public function testApiFavoritesCreateDestroyWithCreateAction()
1146         {
1147                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1148                 // $_REQUEST['id'] = 3;
1149                 // $result         = api_favorites_create_destroy('json');
1150                 // self::assertStatus($result['status']);
1151         }
1152
1153         /**
1154          * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1155          *
1156          * @return void
1157          */
1158         public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1159         {
1160                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'create.rss']);
1161                 // $_REQUEST['id'] = 3;
1162                 // $result         = api_favorites_create_destroy('rss');
1163                 // self::assertXml($result, 'status');
1164         }
1165
1166         /**
1167          * Test the api_favorites_create_destroy() function with the destroy action.
1168          *
1169          * @return void
1170          */
1171         public function testApiFavoritesCreateDestroyWithDestroyAction()
1172         {
1173                 // DI::args()->setArgv(['api', '1.1', 'favorites', 'destroy.json']);
1174                 // $_REQUEST['id'] = 3;
1175                 // $result         = api_favorites_create_destroy('json');
1176                 // self::assertStatus($result['status']);
1177         }
1178
1179         /**
1180          * Test the api_favorites_create_destroy() function without an authenticated user.
1181          *
1182          * @return void
1183          */
1184         public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1185         {
1186                 /*
1187                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1188                 DI::args()->setArgv(['api', '1.1', 'favorites', 'create.json']);
1189                 BasicAuth::setCurrentUserID();
1190                 $_SESSION['authenticated'] = false;
1191                 api_favorites_create_destroy('json');
1192                 */
1193         }
1194
1195
1196
1197         /**
1198          * Test the api_format_messages() function.
1199          *
1200          * @return void
1201          */
1202         public function testApiFormatMessages()
1203         {
1204                 $result = api_format_messages(
1205                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1206                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1207                         ['id' => 3, 'uri-id' => 2, 'screen_name' => 'sender_name']
1208                 );
1209                 self::assertEquals('item_title' . "\n" . 'item_body', $result['text']);
1210                 self::assertEquals(1, $result['id']);
1211                 self::assertEquals(2, $result['recipient_id']);
1212                 self::assertEquals(3, $result['sender_id']);
1213                 self::assertEquals('recipient_name', $result['recipient_screen_name']);
1214                 self::assertEquals('sender_name', $result['sender_screen_name']);
1215         }
1216
1217         /**
1218          * Test the api_format_messages() function with HTML.
1219          *
1220          * @return void
1221          */
1222         public function testApiFormatMessagesWithHtmlText()
1223         {
1224                 $_GET['getText'] = 'html';
1225                 $result          = api_format_messages(
1226                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1227                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1228                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1229                 );
1230                 self::assertEquals('item_title', $result['title']);
1231                 self::assertEquals('<strong>item_body</strong>', $result['text']);
1232         }
1233
1234         /**
1235          * Test the api_format_messages() function with plain text.
1236          *
1237          * @return void
1238          */
1239         public function testApiFormatMessagesWithPlainText()
1240         {
1241                 $_GET['getText'] = 'plain';
1242                 $result          = api_format_messages(
1243                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1244                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1245                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1246                 );
1247                 self::assertEquals('item_title', $result['title']);
1248                 self::assertEquals('item_body', $result['text']);
1249         }
1250
1251         /**
1252          * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
1253          *
1254          * @return void
1255          */
1256         public function testApiFormatMessagesWithoutUserObjects()
1257         {
1258                 $_GET['getUserObjects'] = 'false';
1259                 $result                 = api_format_messages(
1260                         ['id' => 1, 'uri-id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1261                         ['id' => 2, 'uri-id' => 2, 'screen_name' => 'recipient_name'],
1262                         ['id' => 3, 'uri-id' => 3, 'screen_name' => 'sender_name']
1263                 );
1264                 self::assertTrue(!isset($result['sender']));
1265                 self::assertTrue(!isset($result['recipient']));
1266         }
1267
1268         /**
1269          * Test the api_convert_item() function.
1270          *
1271          * @return void
1272          */
1273         public function testApiConvertItem()
1274         {
1275                 /*
1276                 $result = api_convert_item(
1277                         [
1278                                 'network' => 'feed',
1279                                 'title'   => 'item_title',
1280                                 'uri-id'  => 1,
1281                                 // We need a long string to test that it is correctly cut
1282                                 'body'    => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
1283                                                          'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
1284                                                          'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
1285                                                          'laudantium atque commodi alias voluptatem non possimus aperiam ' .
1286                                                          'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
1287                                                          'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
1288                                                          'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
1289                                                          'veritatis nihil distinctio qui eum repellat officia illum quos ' .
1290                                                          'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
1291                                                          'omnis exercitationem quo magnam consequatur maxime aut illum ' .
1292                                                          'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
1293                                                          'temporibus corporis ratione blanditiis perspiciatis impedit ' .
1294                                                          'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
1295                                                          'sunt consequatur inventore dolor officiis pariatur doloremque ' .
1296                                                          'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
1297                                                          'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
1298                                                          'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
1299                                                          'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
1300                                                          'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
1301                                                          'repellat officia illum quos impedit quam iste esse unde qui ' .
1302                                                          'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
1303                                                          'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
1304                                                          'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
1305                                 'plink'   => 'item_plink'
1306                         ]
1307                 );
1308                 self::assertStringStartsWith('item_title', $result['text']);
1309                 self::assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
1310                 */
1311         }
1312
1313         /**
1314          * Test the api_convert_item() function with an empty item body.
1315          *
1316          * @return void
1317          */
1318         public function testApiConvertItemWithoutBody()
1319         {
1320                 /*
1321                 $result = api_convert_item(
1322                         [
1323                                 'network' => 'feed',
1324                                 'title'   => 'item_title',
1325                                 'uri-id'  => -1,
1326                                 'body'    => '',
1327                                 'plink'   => 'item_plink'
1328                         ]
1329                 );
1330                 self::assertEquals("item_title", $result['text']);
1331                 self::assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
1332                 */
1333         }
1334
1335         /**
1336          * Test the api_convert_item() function with the title in the body.
1337          *
1338          * @return void
1339          */
1340         public function testApiConvertItemWithTitleInBody()
1341         {
1342                 /*
1343                 $result = api_convert_item(
1344                         [
1345                                 'title'  => 'item_title',
1346                                 'body'   => 'item_title item_body',
1347                                 'uri-id' => 1,
1348                         ]
1349                 );
1350                 self::assertEquals('item_title item_body', $result['text']);
1351                 self::assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
1352                 */
1353         }
1354
1355         /**
1356          * Test the api_get_attachments() function.
1357          *
1358          * @return void
1359          */
1360         public function testApiGetAttachments()
1361         {
1362                 // $body = 'body';
1363                 // self::assertEmpty(api_get_attachments($body, 0));
1364         }
1365
1366         /**
1367          * Test the api_get_attachments() function with an img tag.
1368          *
1369          * @return void
1370          */
1371         public function testApiGetAttachmentsWithImage()
1372         {
1373                 // $body = '[img]http://via.placeholder.com/1x1.png[/img]';
1374                 // self::assertIsArray(api_get_attachments($body, 0));
1375         }
1376
1377         /**
1378          * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
1379          *
1380          * @return void
1381          */
1382         public function testApiGetAttachmentsWithImageAndAndStatus()
1383         {
1384                 // $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
1385                 // $body                       = '[img]http://via.placeholder.com/1x1.png[/img]';
1386                 // self::assertIsArray(api_get_attachments($body, 0));
1387         }
1388
1389         /**
1390          * Test the api_get_entitities() function.
1391          *
1392          * @return void
1393          */
1394         public function testApiGetEntitities()
1395         {
1396                 // $text = 'text';
1397                 // self::assertIsArray(api_get_entitities($text, 'bbcode', 0));
1398         }
1399
1400         /**
1401          * Test the api_get_entitities() function with the include_entities parameter.
1402          *
1403          * @return void
1404          */
1405         public function testApiGetEntititiesWithIncludeEntities()
1406         {
1407                 /*
1408                 $_REQUEST['include_entities'] = 'true';
1409                 $text                         = 'text';
1410                 $result                       = api_get_entitities($text, 'bbcode', 0);
1411                 self::assertIsArray($result['hashtags']);
1412                 self::assertIsArray($result['symbols']);
1413                 self::assertIsArray($result['urls']);
1414                 self::assertIsArray($result['user_mentions']);
1415                 */
1416         }
1417
1418         /**
1419          * Test the api_format_items_embeded_images() function.
1420          *
1421          * @return void
1422          */
1423         public function testApiFormatItemsEmbededImages()
1424         {
1425                 /*
1426                 self::assertEquals(
1427                         'text ' . DI::baseUrl() . '/display/item_guid',
1428                         api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
1429                 );
1430                 */
1431         }
1432
1433         /**
1434          * Test the api_format_items_activities() function.
1435          *
1436          * @return void
1437          */
1438         public function testApiFormatItemsActivities()
1439         {
1440                 $item   = ['uid' => 0, 'uri-id' => 1];
1441                 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']);
1442                 self::assertArrayHasKey('like', $result);
1443                 self::assertArrayHasKey('dislike', $result);
1444                 self::assertArrayHasKey('attendyes', $result);
1445                 self::assertArrayHasKey('attendno', $result);
1446                 self::assertArrayHasKey('attendmaybe', $result);
1447         }
1448
1449         /**
1450          * Test the api_format_items_activities() function with an XML result.
1451          *
1452          * @return void
1453          */
1454         public function testApiFormatItemsActivitiesWithXml()
1455         {
1456                 $item   = ['uid' => 0, 'uri-id' => 1];
1457                 $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml');
1458                 self::assertArrayHasKey('friendica:like', $result);
1459                 self::assertArrayHasKey('friendica:dislike', $result);
1460                 self::assertArrayHasKey('friendica:attendyes', $result);
1461                 self::assertArrayHasKey('friendica:attendno', $result);
1462                 self::assertArrayHasKey('friendica:attendmaybe', $result);
1463         }
1464
1465         /**
1466          * Test the api_format_items() function.
1467          * @doesNotPerformAssertions
1468          */
1469         public function testApiFormatItems()
1470         {
1471                 /*
1472                 $items = Post::selectToArray([], ['uid' => 42]);
1473                 foreach ($items as $item) {
1474                         $status = api_format_item($item);
1475                         self::assertStatus($status);
1476                 }
1477                 */
1478         }
1479
1480         /**
1481          * Test the api_format_items() function with an XML result.
1482          * @doesNotPerformAssertions
1483          */
1484         public function testApiFormatItemsWithXml()
1485         {
1486                 /*
1487                 $items = Post::selectToArray([], ['uid' => 42]);
1488                 foreach ($items as $item) {
1489                         $status = api_format_item($item, 'xml');
1490                         self::assertStatus($status);
1491                 }
1492                 */
1493         }
1494
1495         /**
1496          * Test the api_lists_list() function.
1497          *
1498          * @return void
1499          */
1500         public function testApiListsList()
1501         {
1502                 $result = api_lists_list('json');
1503                 self::assertEquals(['lists_list' => []], $result);
1504         }
1505
1506         /**
1507          * Test the api_lists_ownerships() function.
1508          *
1509          * @return void
1510          */
1511         public function testApiListsOwnerships()
1512         {
1513                 $result = api_lists_ownerships('json');
1514                 foreach ($result['lists']['lists'] as $list) {
1515                         self::assertList($list);
1516                 }
1517         }
1518
1519         /**
1520          * Test the api_lists_ownerships() function without an authenticated user.
1521          *
1522          * @return void
1523          */
1524         public function testApiListsOwnershipsWithoutAuthenticatedUser()
1525         {
1526                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1527                 BasicAuth::setCurrentUserID();
1528                 $_SESSION['authenticated'] = false;
1529                 api_lists_ownerships('json');
1530         }
1531
1532         /**
1533          * Test the api_statuses_f() function.
1534          *
1535          * @return void
1536          */
1537         public function testApiStatusesFWithIncoming()
1538         {
1539                 // $result = api_statuses_f('incoming');
1540                 // self::assertArrayHasKey('user', $result);
1541         }
1542
1543
1544         /**
1545          * Test the api_direct_messages_new() function.
1546          *
1547          * @return void
1548          */
1549         public function testApiDirectMessagesNew()
1550         {
1551                 $result = api_direct_messages_new('json');
1552                 self::assertNull($result);
1553         }
1554
1555         /**
1556          * Test the api_direct_messages_new() function without an authenticated user.
1557          *
1558          * @return void
1559          */
1560         public function testApiDirectMessagesNewWithoutAuthenticatedUser()
1561         {
1562                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1563                 BasicAuth::setCurrentUserID();
1564                 $_SESSION['authenticated'] = false;
1565                 api_direct_messages_new('json');
1566         }
1567
1568         /**
1569          * Test the api_direct_messages_new() function with an user ID.
1570          *
1571          * @return void
1572          */
1573         public function testApiDirectMessagesNewWithUserId()
1574         {
1575                 $_POST['text']       = 'message_text';
1576                 $_REQUEST['user_id'] = $this->otherUser['id'];
1577                 $result           = api_direct_messages_new('json');
1578                 self::assertEquals(['direct_message' => ['error' => -1]], $result);
1579         }
1580
1581         /**
1582          * Test the api_direct_messages_new() function with a screen name.
1583          *
1584          * @return void
1585          */
1586         public function testApiDirectMessagesNewWithScreenName()
1587         {
1588                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1589                 $_POST['text']       = 'message_text';
1590                 $_REQUEST['user_id'] = $this->friendUser['id'];
1591                 $result              = api_direct_messages_new('json');
1592                 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1593                 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1594                 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1595         }
1596
1597         /**
1598          * Test the api_direct_messages_new() function with a title.
1599          *
1600          * @return void
1601          */
1602         public function testApiDirectMessagesNewWithTitle()
1603         {
1604                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1605                 $_POST['text']        = 'message_text';
1606                 $_REQUEST['user_id']  = $this->friendUser['id'];
1607                 $_REQUEST['title']    = 'message_title';
1608                 $result            = api_direct_messages_new('json');
1609                 self::assertStringContainsString('message_text', $result['direct_message']['text']);
1610                 self::assertStringContainsString('message_title', $result['direct_message']['text']);
1611                 self::assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
1612                 self::assertEquals(1, $result['direct_message']['friendica_seen']);
1613         }
1614
1615         /**
1616          * Test the api_direct_messages_new() function with an RSS result.
1617          *
1618          * @return void
1619          */
1620         public function testApiDirectMessagesNewWithRss()
1621         {
1622                 $this->app->setLoggedInUserNickname($this->selfUser['nick']);
1623                 $_POST['text']       = 'message_text';
1624                 $_REQUEST['user_id'] = $this->friendUser['id'];
1625                 $result              = api_direct_messages_new('rss');
1626                 self::assertXml($result, 'direct-messages');
1627         }
1628
1629         /**
1630          * Test the api_direct_messages_destroy() function.
1631          *
1632          * @return void
1633          */
1634         public function testApiDirectMessagesDestroy()
1635         {
1636                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1637                 api_direct_messages_destroy('json');
1638         }
1639
1640         /**
1641          * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
1642          *
1643          * @return void
1644          */
1645         public function testApiDirectMessagesDestroyWithVerbose()
1646         {
1647                 $_GET['friendica_verbose'] = 'true';
1648                 $result                    = api_direct_messages_destroy('json');
1649                 self::assertEquals(
1650                         [
1651                                 '$result' => [
1652                                         'result'  => 'error',
1653                                         'message' => 'message id or parenturi not specified'
1654                                 ]
1655                         ],
1656                         $result
1657                 );
1658         }
1659
1660         /**
1661          * Test the api_direct_messages_destroy() function without an authenticated user.
1662          *
1663          * @return void
1664          */
1665         public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
1666         {
1667                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1668                 BasicAuth::setCurrentUserID();
1669                 $_SESSION['authenticated'] = false;
1670                 api_direct_messages_destroy('json');
1671         }
1672
1673         /**
1674          * Test the api_direct_messages_destroy() function with a non-zero ID.
1675          *
1676          * @return void
1677          */
1678         public function testApiDirectMessagesDestroyWithId()
1679         {
1680                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1681                 $_REQUEST['id'] = 1;
1682                 api_direct_messages_destroy('json');
1683         }
1684
1685         /**
1686          * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
1687          *
1688          * @return void
1689          */
1690         public function testApiDirectMessagesDestroyWithIdAndVerbose()
1691         {
1692                 $_REQUEST['id']                  = 1;
1693                 $_REQUEST['friendica_parenturi'] = 'parent_uri';
1694                 $_GET['friendica_verbose']       = 'true';
1695                 $result                          = api_direct_messages_destroy('json');
1696                 self::assertEquals(
1697                         [
1698                                 '$result' => [
1699                                         'result'  => 'error',
1700                                         'message' => 'message id not in database'
1701                                 ]
1702                         ],
1703                         $result
1704                 );
1705         }
1706
1707         /**
1708          * Test the api_direct_messages_destroy() function with a non-zero ID.
1709          *
1710          * @return void
1711          */
1712         public function testApiDirectMessagesDestroyWithCorrectId()
1713         {
1714                 $this->markTestIncomplete('We need to add a dataset for this.');
1715         }
1716
1717         /**
1718          * Test the api_direct_messages_box() function.
1719          *
1720          * @return void
1721          */
1722         public function testApiDirectMessagesBoxWithSentbox()
1723         {
1724                 $_REQUEST['page']   = -1;
1725                 $_REQUEST['max_id'] = 10;
1726                 $result             = api_direct_messages_box('json', 'sentbox', 'false');
1727                 self::assertArrayHasKey('direct_message', $result);
1728         }
1729
1730         /**
1731          * Test the api_direct_messages_box() function.
1732          *
1733          * @return void
1734          */
1735         public function testApiDirectMessagesBoxWithConversation()
1736         {
1737                 $result = api_direct_messages_box('json', 'conversation', 'false');
1738                 self::assertArrayHasKey('direct_message', $result);
1739         }
1740
1741         /**
1742          * Test the api_direct_messages_box() function.
1743          *
1744          * @return void
1745          */
1746         public function testApiDirectMessagesBoxWithAll()
1747         {
1748                 $result = api_direct_messages_box('json', 'all', 'false');
1749                 self::assertArrayHasKey('direct_message', $result);
1750         }
1751
1752         /**
1753          * Test the api_direct_messages_box() function.
1754          *
1755          * @return void
1756          */
1757         public function testApiDirectMessagesBoxWithInbox()
1758         {
1759                 $result = api_direct_messages_box('json', 'inbox', 'false');
1760                 self::assertArrayHasKey('direct_message', $result);
1761         }
1762
1763         /**
1764          * Test the api_direct_messages_box() function.
1765          *
1766          * @return void
1767          */
1768         public function testApiDirectMessagesBoxWithVerbose()
1769         {
1770                 $result = api_direct_messages_box('json', 'sentbox', 'true');
1771                 self::assertEquals(
1772                         [
1773                                 '$result' => [
1774                                         'result'  => 'error',
1775                                         'message' => 'no mails available'
1776                                 ]
1777                         ],
1778                         $result
1779                 );
1780         }
1781
1782         /**
1783          * Test the api_direct_messages_box() function with a RSS result.
1784          *
1785          * @return void
1786          */
1787         public function testApiDirectMessagesBoxWithRss()
1788         {
1789                 $result = api_direct_messages_box('rss', 'sentbox', 'false');
1790                 self::assertXml($result, 'direct-messages');
1791         }
1792
1793         /**
1794          * Test the api_direct_messages_box() function without an authenticated user.
1795          *
1796          * @return void
1797          */
1798         public function testApiDirectMessagesBoxWithUnallowedUser()
1799         {
1800                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1801                 BasicAuth::setCurrentUserID();
1802                 api_direct_messages_box('json', 'sentbox', 'false');
1803         }
1804
1805         /**
1806          * Test the api_direct_messages_sentbox() function.
1807          *
1808          * @return void
1809          */
1810         public function testApiDirectMessagesSentbox()
1811         {
1812                 $result = api_direct_messages_sentbox('json');
1813                 self::assertArrayHasKey('direct_message', $result);
1814         }
1815
1816         /**
1817          * Test the api_direct_messages_inbox() function.
1818          *
1819          * @return void
1820          */
1821         public function testApiDirectMessagesInbox()
1822         {
1823                 $result = api_direct_messages_inbox('json');
1824                 self::assertArrayHasKey('direct_message', $result);
1825         }
1826
1827         /**
1828          * Test the api_direct_messages_all() function.
1829          *
1830          * @return void
1831          */
1832         public function testApiDirectMessagesAll()
1833         {
1834                 $result = api_direct_messages_all('json');
1835                 self::assertArrayHasKey('direct_message', $result);
1836         }
1837
1838         /**
1839          * Test the api_direct_messages_conversation() function.
1840          *
1841          * @return void
1842          */
1843         public function testApiDirectMessagesConversation()
1844         {
1845                 $result = api_direct_messages_conversation('json');
1846                 self::assertArrayHasKey('direct_message', $result);
1847         }
1848
1849         /**
1850          * Test the api_oauth_request_token() function.
1851          *
1852          * @return void
1853          */
1854         public function testApiOauthRequestToken()
1855         {
1856                 $this->markTestIncomplete('exit() kills phpunit as well');
1857         }
1858
1859         /**
1860          * Test the api_oauth_access_token() function.
1861          *
1862          * @return void
1863          */
1864         public function testApiOauthAccessToken()
1865         {
1866                 $this->markTestIncomplete('exit() kills phpunit as well');
1867         }
1868
1869         /**
1870          * Test the api_fr_photos_list() function.
1871          *
1872          * @return void
1873          */
1874         public function testApiFrPhotosList()
1875         {
1876                 $result = api_fr_photos_list('json');
1877                 self::assertArrayHasKey('photo', $result);
1878         }
1879
1880         /**
1881          * Test the api_fr_photos_list() function without an authenticated user.
1882          *
1883          * @return void
1884          */
1885         public function testApiFrPhotosListWithoutAuthenticatedUser()
1886         {
1887                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1888                 BasicAuth::setCurrentUserID();
1889                 $_SESSION['authenticated'] = false;
1890                 api_fr_photos_list('json');
1891         }
1892
1893         /**
1894          * Test the api_fr_photo_create_update() function.
1895          */
1896         public function testApiFrPhotoCreateUpdate()
1897         {
1898                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1899                 api_fr_photo_create_update('json');
1900         }
1901
1902         /**
1903          * Test the api_fr_photo_create_update() function without an authenticated user.
1904          *
1905          * @return void
1906          */
1907         public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
1908         {
1909                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1910                 BasicAuth::setCurrentUserID();
1911                 $_SESSION['authenticated'] = false;
1912                 api_fr_photo_create_update('json');
1913         }
1914
1915         /**
1916          * Test the api_fr_photo_create_update() function with an album name.
1917          *
1918          * @return void
1919          */
1920         public function testApiFrPhotoCreateUpdateWithAlbum()
1921         {
1922                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1923                 $_REQUEST['album'] = 'album_name';
1924                 api_fr_photo_create_update('json');
1925         }
1926
1927         /**
1928          * Test the api_fr_photo_create_update() function with the update mode.
1929          *
1930          * @return void
1931          */
1932         public function testApiFrPhotoCreateUpdateWithUpdate()
1933         {
1934                 $this->markTestIncomplete('We need to create a dataset for this');
1935         }
1936
1937         /**
1938          * Test the api_fr_photo_create_update() function with an uploaded file.
1939          *
1940          * @return void
1941          */
1942         public function testApiFrPhotoCreateUpdateWithFile()
1943         {
1944                 $this->markTestIncomplete();
1945         }
1946
1947         /**
1948          * Test the api_fr_photo_detail() function.
1949          *
1950          * @return void
1951          */
1952         public function testApiFrPhotoDetail()
1953         {
1954                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
1955                 api_fr_photo_detail('json');
1956         }
1957
1958         /**
1959          * Test the api_fr_photo_detail() function without an authenticated user.
1960          *
1961          * @return void
1962          */
1963         public function testApiFrPhotoDetailWithoutAuthenticatedUser()
1964         {
1965                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
1966                 BasicAuth::setCurrentUserID();
1967                 $_SESSION['authenticated'] = false;
1968                 api_fr_photo_detail('json');
1969         }
1970
1971         /**
1972          * Test the api_fr_photo_detail() function with a photo ID.
1973          *
1974          * @return void
1975          */
1976         public function testApiFrPhotoDetailWithPhotoId()
1977         {
1978                 $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class);
1979                 $_REQUEST['photo_id'] = 1;
1980                 api_fr_photo_detail('json');
1981         }
1982
1983         /**
1984          * Test the api_fr_photo_detail() function with a correct photo ID.
1985          *
1986          * @return void
1987          */
1988         public function testApiFrPhotoDetailCorrectPhotoId()
1989         {
1990                 $this->markTestIncomplete('We need to create a dataset for this.');
1991         }
1992
1993         /**
1994          * Test the api_account_update_profile_image() function.
1995          *
1996          * @return void
1997          */
1998         public function testApiAccountUpdateProfileImage()
1999         {
2000                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2001                 api_account_update_profile_image('json');
2002         }
2003
2004         /**
2005          * Test the api_account_update_profile_image() function without an authenticated user.
2006          *
2007          * @return void
2008          */
2009         public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
2010         {
2011                 $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class);
2012                 BasicAuth::setCurrentUserID();
2013                 $_SESSION['authenticated'] = false;
2014                 api_account_update_profile_image('json');
2015         }
2016
2017         /**
2018          * Test the api_account_update_profile_image() function with an uploaded file.
2019          *
2020          * @return void
2021          */
2022         public function testApiAccountUpdateProfileImageWithUpload()
2023         {
2024                 $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
2025                 $this->markTestIncomplete();
2026         }
2027
2028         /**
2029          * Test the check_acl_input() function.
2030          *
2031          * @return void
2032          */
2033         public function testCheckAclInput()
2034         {
2035                 $result = check_acl_input('<aclstring>', BaseApi::getCurrentUserID());
2036                 // Where does this result come from?
2037                 self::assertEquals(1, $result);
2038         }
2039
2040         /**
2041          * Test the check_acl_input() function with an empty ACL string.
2042          *
2043          * @return void
2044          */
2045         public function testCheckAclInputWithEmptyAclString()
2046         {
2047                 $result = check_acl_input(' ', BaseApi::getCurrentUserID());
2048                 self::assertFalse($result);
2049         }
2050
2051         /**
2052          * Test the save_media_to_database() function.
2053          *
2054          * @return void
2055          */
2056         public function testSaveMediaToDatabase()
2057         {
2058                 $this->markTestIncomplete();
2059         }
2060
2061         /**
2062          * Test the post_photo_item() function.
2063          *
2064          * @return void
2065          */
2066         public function testPostPhotoItem()
2067         {
2068                 $this->markTestIncomplete();
2069         }
2070
2071         /**
2072          * Test the prepare_photo_data() function.
2073          *
2074          * @return void
2075          */
2076         public function testPreparePhotoData()
2077         {
2078                 $this->markTestIncomplete();
2079         }
2080
2081         /**
2082          * Test the api_share_as_retweet() function with a valid item.
2083          *
2084          * @return void
2085          */
2086         public function testApiShareAsRetweetWithValidItem()
2087         {
2088                 $this->markTestIncomplete();
2089         }
2090
2091         /**
2092          * Test the api_in_reply_to() function with a valid item.
2093          *
2094          * @return void
2095          */
2096         public function testApiInReplyToWithValidItem()
2097         {
2098                 $this->markTestIncomplete();
2099         }
2100
2101         /**
2102          * Test the api_clean_plain_items() function.
2103          *
2104          * @return void
2105          */
2106         public function testApiCleanPlainItems()
2107         {
2108                 $_REQUEST['include_entities'] = 'true';
2109                 $result                       = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
2110                 self::assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
2111         }
2112
2113         /**
2114          * Test the api_best_nickname() function with contacts.
2115          *
2116          * @return void
2117          */
2118         public function testApiBestNicknameWithContacts()
2119         {
2120                 $this->markTestIncomplete();
2121         }
2122
2123         /**
2124          * Test the api_friendica_group_show() function.
2125          *
2126          * @return void
2127          */
2128         public function testApiFriendicaGroupShow()
2129         {
2130                 $this->markTestIncomplete();
2131         }
2132
2133         /**
2134          * Test the api_friendica_group_delete() function.
2135          *
2136          * @return void
2137          */
2138         public function testApiFriendicaGroupDelete()
2139         {
2140                 $this->markTestIncomplete();
2141         }
2142
2143         /**
2144          * Test the api_lists_destroy() function.
2145          *
2146          * @return void
2147          */
2148         public function testApiListsDestroy()
2149         {
2150                 $this->markTestIncomplete();
2151         }
2152
2153         /**
2154          * Test the group_create() function.
2155          *
2156          * @return void
2157          */
2158         public function testGroupCreate()
2159         {
2160                 $this->markTestIncomplete();
2161         }
2162
2163         /**
2164          * Test the api_friendica_group_create() function.
2165          *
2166          * @return void
2167          */
2168         public function testApiFriendicaGroupCreate()
2169         {
2170                 $this->markTestIncomplete();
2171         }
2172
2173         /**
2174          * Test the api_lists_create() function.
2175          *
2176          * @return void
2177          */
2178         public function testApiListsCreate()
2179         {
2180                 $this->markTestIncomplete();
2181         }
2182
2183         /**
2184          * Test the api_friendica_group_update() function.
2185          *
2186          * @return void
2187          */
2188         public function testApiFriendicaGroupUpdate()
2189         {
2190                 $this->markTestIncomplete();
2191         }
2192
2193         /**
2194          * Test the api_lists_update() function.
2195          *
2196          * @return void
2197          */
2198         public function testApiListsUpdate()
2199         {
2200                 $this->markTestIncomplete();
2201         }
2202
2203         /**
2204          * Test the api_friendica_activity() function.
2205          *
2206          * @return void
2207          */
2208         public function testApiFriendicaActivity()
2209         {
2210                 $this->markTestIncomplete();
2211         }
2212
2213         /**
2214          * Test the api_friendica_notification_seen() function.
2215          *
2216          * @return void
2217          */
2218         public function testApiFriendicaNotificationSeen()
2219         {
2220                 $this->markTestIncomplete();
2221         }
2222
2223         /**
2224          * Test the api_friendica_direct_messages_setseen() function.
2225          *
2226          * @return void
2227          */
2228         public function testApiFriendicaDirectMessagesSetseen()
2229         {
2230                 $this->markTestIncomplete();
2231         }
2232
2233         /**
2234          * Test the api_friendica_direct_messages_search() function.
2235          *
2236          * @return void
2237          */
2238         public function testApiFriendicaDirectMessagesSearch()
2239         {
2240                 $this->markTestIncomplete();
2241         }
2242 }