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