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