]> git.mxchange.org Git - friendica.git/blob - tests/legacy/ApiTest.php
Stop using q as a page parameter
[friendica.git] / tests / legacy / ApiTest.php
1 <?php
2 /**
3  * ApiTest class.
4  */
5
6 namespace Friendica\Test\legacy;
7
8 use Friendica\App;
9 use Friendica\Core\Config\IConfig;
10 use Friendica\Core\PConfig\IPConfig;
11 use Friendica\Core\Protocol;
12 use Friendica\DI;
13 use Friendica\Network\HTTPException;
14 use Friendica\Test\FixtureTest;
15 use Friendica\Util\Temporal;
16 use Monolog\Handler\TestHandler;
17
18 require_once __DIR__ . '/../../include/api.php';
19
20 /**
21  * Tests for the API functions.
22  *
23  * Functions that use header() need to be tested in a separate process.
24  * @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
25  */
26 class ApiTest extends FixtureTest
27 {
28         /**
29          * @var TestHandler Can handle log-outputs
30          */
31         protected $logOutput;
32
33         /** @var array */
34         protected $selfUser;
35         /** @var array */
36         protected $friendUser;
37         /** @var array */
38         protected $otherUser;
39
40         protected $wrongUserId;
41
42         /** @var App */
43         protected $app;
44
45         /** @var IConfig */
46         protected $config;
47
48         /**
49          * Create variables used by tests.
50          */
51         protected function setUp()
52         {
53                 global $API, $called_api;
54                 $API = [];
55                 $called_api = [];
56
57                 parent::setUp();
58
59                 /** @var IConfig $config */
60                 $this->config = $this->dice->create(IConfig::class);
61
62                 $this->config->set('system', 'url', 'http://localhost');
63                 $this->config->set('system', 'hostname', 'localhost');
64                 $this->config->set('system', 'worker_dont_fork', true);
65
66                 // Default config
67                 $this->config->set('config', 'hostname', 'localhost');
68                 $this->config->set('system', 'throttle_limit_day', 100);
69                 $this->config->set('system', 'throttle_limit_week', 100);
70                 $this->config->set('system', 'throttle_limit_month', 100);
71                 $this->config->set('system', 'theme', 'system_theme');
72
73
74                 /** @var App app */
75                 $this->app = DI::app();
76
77                 $this->app->argc = 1;
78                 $this->app->argv = ['home'];
79
80                 // User data that the test database is populated with
81                 $this->selfUser   = [
82                         'id'   => 42,
83                         'name' => 'Self contact',
84                         'nick' => 'selfcontact',
85                         'nurl' => 'http://localhost/profile/selfcontact'
86                 ];
87                 $this->friendUser = [
88                         'id'   => 44,
89                         'name' => 'Friend contact',
90                         'nick' => 'friendcontact',
91                         'nurl' => 'http://localhost/profile/friendcontact'
92                 ];
93                 $this->otherUser  = [
94                         'id'   => 43,
95                         'name' => 'othercontact',
96                         'nick' => 'othercontact',
97                         'nurl' => 'http://localhost/profile/othercontact'
98                 ];
99
100                 // User ID that we know is not in the database
101                 $this->wrongUserId = 666;
102
103                 DI::session()->start();
104
105                 // Most API require login so we force the session
106                 $_SESSION = [
107                         'allow_api'     => true,
108                         'authenticated' => true,
109                         'uid'           => $this->selfUser['id']
110                 ];
111
112                 $_POST   = [];
113                 $_GET    = [];
114                 $_SERVER = [];
115         }
116
117         /**
118          * Assert that an user array contains expected keys.
119          *
120          * @param array $user User array
121          *
122          * @return void
123          */
124         private function assertSelfUser(array $user)
125         {
126                 $this->assertEquals($this->selfUser['id'], $user['uid']);
127                 $this->assertEquals($this->selfUser['id'], $user['cid']);
128                 $this->assertEquals(1, $user['self']);
129                 $this->assertEquals('DFRN', $user['location']);
130                 $this->assertEquals($this->selfUser['name'], $user['name']);
131                 $this->assertEquals($this->selfUser['nick'], $user['screen_name']);
132                 $this->assertEquals('dfrn', $user['network']);
133                 $this->assertTrue($user['verified']);
134         }
135
136         /**
137          * Assert that an user array contains expected keys.
138          *
139          * @param array $user User array
140          *
141          * @return void
142          */
143         private function assertOtherUser(array $user)
144         {
145                 $this->assertEquals($this->otherUser['id'], $user['id']);
146                 $this->assertEquals($this->otherUser['id'], $user['id_str']);
147                 $this->assertEquals(0, $user['self']);
148                 $this->assertEquals($this->otherUser['name'], $user['name']);
149                 $this->assertEquals($this->otherUser['nick'], $user['screen_name']);
150                 $this->assertFalse($user['verified']);
151         }
152
153         /**
154          * Assert that a status array contains expected keys.
155          *
156          * @param array $status Status array
157          *
158          * @return void
159          */
160         private function assertStatus(array $status)
161         {
162                 $this->assertInternalType('string', $status['text']);
163                 $this->assertInternalType('int', $status['id']);
164                 // We could probably do more checks here.
165         }
166
167         /**
168          * Assert that a list array contains expected keys.
169          *
170          * @param array $list List array
171          *
172          * @return void
173          */
174         private function assertList(array $list)
175         {
176                 $this->assertInternalType('string', $list['name']);
177                 $this->assertInternalType('int', $list['id']);
178                 $this->assertInternalType('string', $list['id_str']);
179                 $this->assertContains($list['mode'], ['public', 'private']);
180                 // We could probably do more checks here.
181         }
182
183         /**
184          * Assert that the string is XML and contain the root element.
185          *
186          * @param string $result       XML string
187          * @param string $root_element Root element name
188          *
189          * @return void
190          */
191         private function assertXml($result, $root_element)
192         {
193                 $this->assertStringStartsWith('<?xml version="1.0"?>', $result);
194                 $this->assertContains('<' . $root_element, $result);
195                 // We could probably do more checks here.
196         }
197
198         /**
199          * Get the path to a temporary empty PNG image.
200          *
201          * @return string Path
202          */
203         private function getTempImage()
204         {
205                 $tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
206                 file_put_contents(
207                         $tmpFile,
208                         base64_decode(
209                         // Empty 1x1 px PNG image
210                                 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
211                         )
212                 );
213
214                 return $tmpFile;
215         }
216
217         /**
218          * Test the api_user() function.
219          *
220          * @return void
221          */
222         public function testApiUser()
223         {
224                 $this->assertEquals($this->selfUser['id'], api_user());
225         }
226
227         /**
228          * Test the api_user() function with an unallowed user.
229          *
230          * @return void
231          */
232         public function testApiUserWithUnallowedUser()
233         {
234                 $_SESSION = ['allow_api' => false];
235                 $this->assertEquals(false, api_user());
236         }
237
238         /**
239          * Test the api_source() function.
240          *
241          * @return void
242          */
243         public function testApiSource()
244         {
245                 $this->assertEquals('api', api_source());
246         }
247
248         /**
249          * Test the api_source() function with a Twidere user agent.
250          *
251          * @return void
252          */
253         public function testApiSourceWithTwidere()
254         {
255                 $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
256                 $this->assertEquals('Twidere', api_source());
257         }
258
259         /**
260          * Test the api_source() function with a GET parameter.
261          *
262          * @return void
263          */
264         public function testApiSourceWithGet()
265         {
266                 $_GET['source'] = 'source_name';
267                 $this->assertEquals('source_name', api_source());
268         }
269
270         /**
271          * Test the api_date() function.
272          *
273          * @return void
274          */
275         public function testApiDate()
276         {
277                 $this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
278         }
279
280         /**
281          * Test the api_register_func() function.
282          *
283          * @return void
284          */
285         public function testApiRegisterFunc()
286         {
287                 global $API;
288                 $this->assertNull(
289                         api_register_func(
290                                 'api_path',
291                                 function () {
292                                 },
293                                 true,
294                                 'method'
295                         )
296                 );
297                 $this->assertTrue($API['api_path']['auth']);
298                 $this->assertEquals('method', $API['api_path']['method']);
299                 $this->assertTrue(is_callable($API['api_path']['func']));
300         }
301
302         /**
303          * Test the api_login() function without any login.
304          *
305          * @return void
306          * @runInSeparateProcess
307          * @expectedException Friendica\Network\HTTPException\UnauthorizedException
308          */
309         public function testApiLoginWithoutLogin()
310         {
311                 api_login($this->app);
312         }
313
314         /**
315          * Test the api_login() function with a bad login.
316          *
317          * @return void
318          * @runInSeparateProcess
319          * @expectedException Friendica\Network\HTTPException\UnauthorizedException
320          */
321         public function testApiLoginWithBadLogin()
322         {
323                 $_SERVER['PHP_AUTH_USER'] = 'user@server';
324                 api_login($this->app);
325         }
326
327         /**
328          * Test the api_login() function with oAuth.
329          *
330          * @return void
331          */
332         public function testApiLoginWithOauth()
333         {
334                 $this->markTestIncomplete('Can we test this easily?');
335         }
336
337         /**
338          * Test the api_login() function with authentication provided by an addon.
339          *
340          * @return void
341          */
342         public function testApiLoginWithAddonAuth()
343         {
344                 $this->markTestIncomplete('Can we test this easily?');
345         }
346
347         /**
348          * Test the api_login() function with a correct login.
349          *
350          * @return void
351          * @runInSeparateProcess
352          */
353         public function testApiLoginWithCorrectLogin()
354         {
355                 $_SERVER['PHP_AUTH_USER'] = 'Test user';
356                 $_SERVER['PHP_AUTH_PW']   = 'password';
357                 api_login($this->app);
358         }
359
360         /**
361          * Test the api_login() function with a remote user.
362          *
363          * @return void
364          * @runInSeparateProcess
365          * @expectedException Friendica\Network\HTTPException\UnauthorizedException
366          */
367         public function testApiLoginWithRemoteUser()
368         {
369                 $_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
370                 api_login($this->app);
371         }
372
373         /**
374          * Test the api_check_method() function.
375          *
376          * @return void
377          */
378         public function testApiCheckMethod()
379         {
380                 $this->assertFalse(api_check_method('method'));
381         }
382
383         /**
384          * Test the api_check_method() function with a correct method.
385          *
386          * @return void
387          */
388         public function testApiCheckMethodWithCorrectMethod()
389         {
390                 $_SERVER['REQUEST_METHOD'] = 'method';
391                 $this->assertTrue(api_check_method('method'));
392         }
393
394         /**
395          * Test the api_check_method() function with a wildcard.
396          *
397          * @return void
398          */
399         public function testApiCheckMethodWithWildcard()
400         {
401                 $this->assertTrue(api_check_method('*'));
402         }
403
404         /**
405          * Test the api_call() function.
406          *
407          * @return void
408          * @runInSeparateProcess
409          */
410         public function testApiCall()
411         {
412                 global $API;
413                 $API['api_path']           = [
414                         'method' => 'method',
415                         'func'   => function () {
416                                 return ['data' => ['some_data']];
417                         }
418                 ];
419                 $_SERVER['REQUEST_METHOD'] = 'method';
420                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
421                 $_GET['callback']          = 'callback_name';
422
423                 $args = DI::args()->determine($_SERVER, $_GET);
424
425                 $this->assertEquals(
426                         'callback_name(["some_data"])',
427                         api_call($this->app, $args)
428                 );
429         }
430
431         /**
432          * Test the api_call() function with the profiled enabled.
433          *
434          * @return void
435          * @runInSeparateProcess
436          */
437         public function testApiCallWithProfiler()
438         {
439                 global $API;
440                 $API['api_path']           = [
441                         'method' => 'method',
442                         'func'   => function () {
443                                 return ['data' => ['some_data']];
444                         }
445                 ];
446
447                 $_SERVER['REQUEST_METHOD'] = 'method';
448                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
449
450                 $args = DI::args()->determine($_SERVER, $_GET);
451
452                 $this->config->set('system', 'profiler', true);
453                 $this->config->set('rendertime', 'callstack', true);
454                 $this->app->callstack = [
455                         'database'       => ['some_function' => 200],
456                         'database_write' => ['some_function' => 200],
457                         'cache'          => ['some_function' => 200],
458                         'cache_write'    => ['some_function' => 200],
459                         'network'        => ['some_function' => 200]
460                 ];
461
462                 $this->assertEquals(
463                         '["some_data"]',
464                         api_call($this->app, $args)
465                 );
466         }
467
468         /**
469          * Test the api_call() function without any result.
470          *
471          * @return void
472          * @runInSeparateProcess
473          */
474         public function testApiCallWithNoResult()
475         {
476                 global $API;
477                 $API['api_path']           = [
478                         'method' => 'method',
479                         'func'   => function () {
480                                 return false;
481                         }
482                 ];
483                 $_SERVER['REQUEST_METHOD'] = 'method';
484                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
485
486                 $args = DI::args()->determine($_SERVER, $_GET);
487
488                 $this->assertEquals(
489                         '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}',
490                         api_call($this->app, $args)
491                 );
492         }
493
494         /**
495          * Test the api_call() function with an unimplemented API.
496          *
497          * @return void
498          * @runInSeparateProcess
499          */
500         public function testApiCallWithUninplementedApi()
501         {
502                 $this->assertEquals(
503                         '{"status":{"error":"Not Implemented","code":"501 Not Implemented","request":""}}',
504                         api_call($this->app)
505                 );
506         }
507
508         /**
509          * Test the api_call() function with a JSON result.
510          *
511          * @return void
512          * @runInSeparateProcess
513          */
514         public function testApiCallWithJson()
515         {
516                 global $API;
517                 $API['api_path']           = [
518                         'method' => 'method',
519                         'func'   => function () {
520                                 return ['data' => ['some_data']];
521                         }
522                 ];
523                 $_SERVER['REQUEST_METHOD'] = 'method';
524                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.json';
525
526                 $args = DI::args()->determine($_SERVER, $_GET);
527
528                 $this->assertEquals(
529                         '["some_data"]',
530                         api_call($this->app, $args)
531                 );
532         }
533
534         /**
535          * Test the api_call() function with an XML result.
536          *
537          * @return void
538          * @runInSeparateProcess
539          */
540         public function testApiCallWithXml()
541         {
542                 global $API;
543                 $API['api_path']           = [
544                         'method' => 'method',
545                         'func'   => function () {
546                                 return 'some_data';
547                         }
548                 ];
549                 $_SERVER['REQUEST_METHOD'] = 'method';
550                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml';
551
552                 $args = DI::args()->determine($_SERVER, $_GET);
553
554                 $this->assertEquals(
555                         'some_data',
556                         api_call($this->app, $args)
557                 );
558         }
559
560         /**
561          * Test the api_call() function with an RSS result.
562          *
563          * @return void
564          * @runInSeparateProcess
565          */
566         public function testApiCallWithRss()
567         {
568                 global $API;
569                 $API['api_path']           = [
570                         'method' => 'method',
571                         'func'   => function () {
572                                 return 'some_data';
573                         }
574                 ];
575                 $_SERVER['REQUEST_METHOD'] = 'method';
576                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss';
577
578                 $args = DI::args()->determine($_SERVER, $_GET);
579
580                 $this->assertEquals(
581                         '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
582                         'some_data',
583                         api_call($this->app, $args)
584                 );
585         }
586
587         /**
588          * Test the api_call() function with an Atom result.
589          *
590          * @return void
591          * @runInSeparateProcess
592          */
593         public function testApiCallWithAtom()
594         {
595                 global $API;
596                 $API['api_path']           = [
597                         'method' => 'method',
598                         'func'   => function () {
599                                 return 'some_data';
600                         }
601                 ];
602                 $_SERVER['REQUEST_METHOD'] = 'method';
603                 $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom';
604
605                 $args = DI::args()->determine($_SERVER, $_GET);
606
607                 $this->assertEquals(
608                         '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
609                         'some_data',
610                         api_call($this->app, $args)
611                 );
612         }
613
614         /**
615          * Test the api_call() function with an unallowed method.
616          *
617          * @return void
618          * @runInSeparateProcess
619          */
620         public function testApiCallWithWrongMethod()
621         {
622                 global $API;
623                 $API['api_path'] = ['method' => 'method'];
624
625                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
626
627                 $args = DI::args()->determine($_SERVER, $_GET);
628
629                 $this->assertEquals(
630                         '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
631                         api_call($this->app, $args)
632                 );
633         }
634
635         /**
636          * Test the api_call() function with an unauthorized user.
637          *
638          * @return void
639          * @runInSeparateProcess
640          */
641         public function testApiCallWithWrongAuth()
642         {
643                 global $API;
644                 $API['api_path']           = [
645                         'method' => 'method',
646                         'auth'   => true
647                 ];
648                 $_SESSION['authenticated'] = false;
649                 $_SERVER['REQUEST_METHOD'] = 'method';
650                 $_SERVER['QUERY_STRING'] = 'pagename=api_path';
651
652                 $args = DI::args()->determine($_SERVER, $_GET);
653
654                 $this->assertEquals(
655                         '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}',
656                         api_call($this->app, $args)
657                 );
658         }
659
660         /**
661          * Test the api_error() function with a JSON result.
662          *
663          * @return void
664          * @runInSeparateProcess
665          */
666         public function testApiErrorWithJson()
667         {
668                 $this->assertEquals(
669                         '{"status":{"error":"error_message","code":"200 OK","request":""}}',
670                         api_error('json', new HTTPException\OKException('error_message'), DI::args())
671                 );
672         }
673
674         /**
675          * Test the api_error() function with an XML result.
676          *
677          * @return void
678          * @runInSeparateProcess
679          */
680         public function testApiErrorWithXml()
681         {
682                 $this->assertEquals(
683                         '<?xml version="1.0"?>' . "\n" .
684                         '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
685                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
686                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
687                         '  <error>error_message</error>' . "\n" .
688                         '  <code>200 OK</code>' . "\n" .
689                         '  <request/>' . "\n" .
690                         '</status>' . "\n",
691                         api_error('xml', new HTTPException\OKException('error_message'), DI::args())
692                 );
693         }
694
695         /**
696          * Test the api_error() function with an RSS result.
697          *
698          * @return void
699          * @runInSeparateProcess
700          */
701         public function testApiErrorWithRss()
702         {
703                 $this->assertEquals(
704                         '<?xml version="1.0"?>' . "\n" .
705                         '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
706                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
707                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
708                         '  <error>error_message</error>' . "\n" .
709                         '  <code>200 OK</code>' . "\n" .
710                         '  <request/>' . "\n" .
711                         '</status>' . "\n",
712                         api_error('rss', new HTTPException\OKException('error_message'), DI::args())
713                 );
714         }
715
716         /**
717          * Test the api_error() function with an Atom result.
718          *
719          * @return void
720          * @runInSeparateProcess
721          */
722         public function testApiErrorWithAtom()
723         {
724                 $this->assertEquals(
725                         '<?xml version="1.0"?>' . "\n" .
726                         '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
727                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
728                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
729                         '  <error>error_message</error>' . "\n" .
730                         '  <code>200 OK</code>' . "\n" .
731                         '  <request/>' . "\n" .
732                         '</status>' . "\n",
733                         api_error('atom', new HTTPException\OKException('error_message'), DI::args())
734                 );
735         }
736
737         /**
738          * Test the api_rss_extra() function.
739          *
740          * @return void
741          */
742         public function testApiRssExtra()
743         {
744                 $user_info = ['url' => 'user_url', 'lang' => 'en'];
745                 $result    = api_rss_extra($this->app, [], $user_info);
746                 $this->assertEquals($user_info, $result['$user']);
747                 $this->assertEquals($user_info['url'], $result['$rss']['alternate']);
748                 $this->assertArrayHasKey('self', $result['$rss']);
749                 $this->assertArrayHasKey('base', $result['$rss']);
750                 $this->assertArrayHasKey('updated', $result['$rss']);
751                 $this->assertArrayHasKey('atom_updated', $result['$rss']);
752                 $this->assertArrayHasKey('language', $result['$rss']);
753                 $this->assertArrayHasKey('logo', $result['$rss']);
754         }
755
756         /**
757          * Test the api_rss_extra() function without any user info.
758          *
759          * @return void
760          */
761         public function testApiRssExtraWithoutUserInfo()
762         {
763                 $result = api_rss_extra($this->app, [], null);
764                 $this->assertInternalType('array', $result['$user']);
765                 $this->assertArrayHasKey('alternate', $result['$rss']);
766                 $this->assertArrayHasKey('self', $result['$rss']);
767                 $this->assertArrayHasKey('base', $result['$rss']);
768                 $this->assertArrayHasKey('updated', $result['$rss']);
769                 $this->assertArrayHasKey('atom_updated', $result['$rss']);
770                 $this->assertArrayHasKey('language', $result['$rss']);
771                 $this->assertArrayHasKey('logo', $result['$rss']);
772         }
773
774         /**
775          * Test the api_unique_id_to_nurl() function.
776          *
777          * @return void
778          */
779         public function testApiUniqueIdToNurl()
780         {
781                 $this->assertFalse(api_unique_id_to_nurl($this->wrongUserId));
782         }
783
784         /**
785          * Test the api_unique_id_to_nurl() function with a correct ID.
786          *
787          * @return void
788          */
789         public function testApiUniqueIdToNurlWithCorrectId()
790         {
791                 $this->assertEquals($this->otherUser['nurl'], api_unique_id_to_nurl($this->otherUser['id']));
792         }
793
794         /**
795          * Test the api_get_user() function.
796          *
797          * @return void
798          */
799         public function testApiGetUser()
800         {
801                 $user = api_get_user($this->app);
802                 $this->assertSelfUser($user);
803                 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
804                 $this->assertEquals('6fdbe8', $user['profile_link_color']);
805                 $this->assertEquals('ededed', $user['profile_background_color']);
806         }
807
808         /**
809          * Test the api_get_user() function with a Frio schema.
810          *
811          * @return void
812          */
813         public function testApiGetUserWithFrioSchema()
814         {
815                 $pConfig = $this->dice->create(IPConfig::class);
816                 $pConfig->set($this->selfUser['id'], 'frio', 'schema', 'red');
817                 $user = api_get_user($this->app);
818                 $this->assertSelfUser($user);
819                 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
820                 $this->assertEquals('6fdbe8', $user['profile_link_color']);
821                 $this->assertEquals('ededed', $user['profile_background_color']);
822         }
823
824         /**
825          * Test the api_get_user() function with an empty Frio schema.
826          *
827          * @return void
828          */
829         public function testApiGetUserWithEmptyFrioSchema()
830         {
831                 $pConfig = $this->dice->create(IPConfig::class);
832                 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
833                 $user = api_get_user($this->app);
834                 $this->assertSelfUser($user);
835                 $this->assertEquals('708fa0', $user['profile_sidebar_fill_color']);
836                 $this->assertEquals('6fdbe8', $user['profile_link_color']);
837                 $this->assertEquals('ededed', $user['profile_background_color']);
838         }
839
840         /**
841          * Test the api_get_user() function with a custom Frio schema.
842          *
843          * @return void
844          */
845         public function testApiGetUserWithCustomFrioSchema()
846         {
847                 $pConfig = $this->dice->create(IPConfig::class);
848                 $pConfig->set($this->selfUser['id'], 'frio', 'schema', '---');
849                 $pConfig->set($this->selfUser['id'], 'frio', 'nav_bg', '#123456');
850                 $pConfig->set($this->selfUser['id'], 'frio', 'link_color', '#123456');
851                 $pConfig->set($this->selfUser['id'], 'frio', 'background_color', '#123456');
852                 $user = api_get_user($this->app);
853                 $this->assertSelfUser($user);
854                 $this->assertEquals('123456', $user['profile_sidebar_fill_color']);
855                 $this->assertEquals('123456', $user['profile_link_color']);
856                 $this->assertEquals('123456', $user['profile_background_color']);
857         }
858
859         /**
860          * Test the api_get_user() function with an user that is not allowed to use the API.
861          *
862          * @return void
863          * @runInSeparateProcess
864          */
865         public function testApiGetUserWithoutApiUser()
866         {
867                 $_SERVER['PHP_AUTH_USER'] = 'Test user';
868                 $_SERVER['PHP_AUTH_PW']   = 'password';
869                 $_SESSION['allow_api']    = false;
870                 $this->assertFalse(api_get_user($this->app));
871         }
872
873         /**
874          * Test the api_get_user() function with an user ID in a GET parameter.
875          *
876          * @return void
877          */
878         public function testApiGetUserWithGetId()
879         {
880                 $_GET['user_id'] = $this->otherUser['id'];
881                 $this->assertOtherUser(api_get_user($this->app));
882         }
883
884         /**
885          * Test the api_get_user() function with a wrong user ID in a GET parameter.
886          *
887          * @return void
888          * @expectedException Friendica\Network\HTTPException\BadRequestException
889          */
890         public function testApiGetUserWithWrongGetId()
891         {
892                 $_GET['user_id'] = $this->wrongUserId;
893                 $this->assertOtherUser(api_get_user($this->app));
894         }
895
896         /**
897          * Test the api_get_user() function with an user name in a GET parameter.
898          *
899          * @return void
900          */
901         public function testApiGetUserWithGetName()
902         {
903                 $_GET['screen_name'] = $this->selfUser['nick'];
904                 $this->assertSelfUser(api_get_user($this->app));
905         }
906
907         /**
908          * Test the api_get_user() function with a profile URL in a GET parameter.
909          *
910          * @return void
911          */
912         public function testApiGetUserWithGetUrl()
913         {
914                 $_GET['profileurl'] = $this->selfUser['nurl'];
915                 $this->assertSelfUser(api_get_user($this->app));
916         }
917
918         /**
919          * Test the api_get_user() function with an user ID in the API path.
920          *
921          * @return void
922          */
923         public function testApiGetUserWithNumericCalledApi()
924         {
925                 global $called_api;
926                 $called_api         = ['api_path'];
927                 $this->app->argv[1] = $this->otherUser['id'] . '.json';
928                 $this->assertOtherUser(api_get_user($this->app));
929         }
930
931         /**
932          * Test the api_get_user() function with the $called_api global variable.
933          *
934          * @return void
935          */
936         public function testApiGetUserWithCalledApi()
937         {
938                 global $called_api;
939                 $called_api = ['api', 'api_path'];
940                 $this->assertSelfUser(api_get_user($this->app));
941         }
942
943         /**
944          * Test the api_get_user() function with a valid user.
945          *
946          * @return void
947          */
948         public function testApiGetUserWithCorrectUser()
949         {
950                 $this->assertOtherUser(api_get_user($this->app, $this->otherUser['id']));
951         }
952
953         /**
954          * Test the api_get_user() function with a wrong user ID.
955          *
956          * @return void
957          * @expectedException Friendica\Network\HTTPException\BadRequestException
958          */
959         public function testApiGetUserWithWrongUser()
960         {
961                 $this->assertOtherUser(api_get_user($this->app, $this->wrongUserId));
962         }
963
964         /**
965          * Test the api_get_user() function with a 0 user ID.
966          *
967          * @return void
968          */
969         public function testApiGetUserWithZeroUser()
970         {
971                 $this->assertSelfUser(api_get_user($this->app, 0));
972         }
973
974         /**
975          * Test the api_item_get_user() function.
976          *
977          * @return void
978          */
979         public function testApiItemGetUser()
980         {
981                 $users = api_item_get_user($this->app, []);
982                 $this->assertSelfUser($users[0]);
983         }
984
985         /**
986          * Test the api_item_get_user() function with a different item parent.
987          *
988          * @return void
989          */
990         public function testApiItemGetUserWithDifferentParent()
991         {
992                 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
993                 $this->assertSelfUser($users[0]);
994                 $this->assertEquals($users[0], $users[1]);
995         }
996
997         /**
998          * Test the api_walk_recursive() function.
999          *
1000          * @return void
1001          */
1002         public function testApiWalkRecursive()
1003         {
1004                 $array = ['item1'];
1005                 $this->assertEquals(
1006                         $array,
1007                         api_walk_recursive(
1008                                 $array,
1009                                 function () {
1010                                         // Should we test this with a callback that actually does something?
1011                                         return true;
1012                                 }
1013                         )
1014                 );
1015         }
1016
1017         /**
1018          * Test the api_walk_recursive() function with an array.
1019          *
1020          * @return void
1021          */
1022         public function testApiWalkRecursiveWithArray()
1023         {
1024                 $array = [['item1'], ['item2']];
1025                 $this->assertEquals(
1026                         $array,
1027                         api_walk_recursive(
1028                                 $array,
1029                                 function () {
1030                                         // Should we test this with a callback that actually does something?
1031                                         return true;
1032                                 }
1033                         )
1034                 );
1035         }
1036
1037         /**
1038          * Test the api_reformat_xml() function.
1039          *
1040          * @return void
1041          */
1042         public function testApiReformatXml()
1043         {
1044                 $item = true;
1045                 $key  = '';
1046                 $this->assertTrue(api_reformat_xml($item, $key));
1047                 $this->assertEquals('true', $item);
1048         }
1049
1050         /**
1051          * Test the api_reformat_xml() function with a statusnet_api key.
1052          *
1053          * @return void
1054          */
1055         public function testApiReformatXmlWithStatusnetKey()
1056         {
1057                 $item = '';
1058                 $key  = 'statusnet_api';
1059                 $this->assertTrue(api_reformat_xml($item, $key));
1060                 $this->assertEquals('statusnet:api', $key);
1061         }
1062
1063         /**
1064          * Test the api_reformat_xml() function with a friendica_api key.
1065          *
1066          * @return void
1067          */
1068         public function testApiReformatXmlWithFriendicaKey()
1069         {
1070                 $item = '';
1071                 $key  = 'friendica_api';
1072                 $this->assertTrue(api_reformat_xml($item, $key));
1073                 $this->assertEquals('friendica:api', $key);
1074         }
1075
1076         /**
1077          * Test the api_create_xml() function.
1078          *
1079          * @return void
1080          */
1081         public function testApiCreateXml()
1082         {
1083                 $this->assertEquals(
1084                         '<?xml version="1.0"?>' . "\n" .
1085                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1086                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1087                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1088                         '  <data>some_data</data>' . "\n" .
1089                         '</root_element>' . "\n",
1090                         api_create_xml(['data' => ['some_data']], 'root_element')
1091                 );
1092         }
1093
1094         /**
1095          * Test the api_create_xml() function without any XML namespace.
1096          *
1097          * @return void
1098          */
1099         public function testApiCreateXmlWithoutNamespaces()
1100         {
1101                 $this->assertEquals(
1102                         '<?xml version="1.0"?>' . "\n" .
1103                         '<ok>' . "\n" .
1104                         '  <data>some_data</data>' . "\n" .
1105                         '</ok>' . "\n",
1106                         api_create_xml(['data' => ['some_data']], 'ok')
1107                 );
1108         }
1109
1110         /**
1111          * Test the api_format_data() function.
1112          *
1113          * @return void
1114          */
1115         public function testApiFormatData()
1116         {
1117                 $data = ['some_data'];
1118                 $this->assertEquals($data, api_format_data('root_element', 'json', $data));
1119         }
1120
1121         /**
1122          * Test the api_format_data() function with an XML result.
1123          *
1124          * @return void
1125          */
1126         public function testApiFormatDataWithXml()
1127         {
1128                 $this->assertEquals(
1129                         '<?xml version="1.0"?>' . "\n" .
1130                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
1131                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
1132                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
1133                         '  <data>some_data</data>' . "\n" .
1134                         '</root_element>' . "\n",
1135                         api_format_data('root_element', 'xml', ['data' => ['some_data']])
1136                 );
1137         }
1138
1139         /**
1140          * Test the api_account_verify_credentials() function.
1141          *
1142          * @return void
1143          */
1144         public function testApiAccountVerifyCredentials()
1145         {
1146                 $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
1147         }
1148
1149         /**
1150          * Test the api_account_verify_credentials() function without an authenticated user.
1151          *
1152          * @return void
1153          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1154          */
1155         public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1156         {
1157                 $_SESSION['authenticated'] = false;
1158                 api_account_verify_credentials('json');
1159         }
1160
1161         /**
1162          * Test the requestdata() function.
1163          *
1164          * @return void
1165          */
1166         public function testRequestdata()
1167         {
1168                 $this->assertNull(requestdata('variable_name'));
1169         }
1170
1171         /**
1172          * Test the requestdata() function with a POST parameter.
1173          *
1174          * @return void
1175          */
1176         public function testRequestdataWithPost()
1177         {
1178                 $_POST['variable_name'] = 'variable_value';
1179                 $this->assertEquals('variable_value', requestdata('variable_name'));
1180         }
1181
1182         /**
1183          * Test the requestdata() function with a GET parameter.
1184          *
1185          * @return void
1186          */
1187         public function testRequestdataWithGet()
1188         {
1189                 $_GET['variable_name'] = 'variable_value';
1190                 $this->assertEquals('variable_value', requestdata('variable_name'));
1191         }
1192
1193         /**
1194          * Test the api_statuses_mediap() function.
1195          *
1196          * @return void
1197          */
1198         public function testApiStatusesMediap()
1199         {
1200                 $this->app->argc = 2;
1201
1202                 $_FILES         = [
1203                         'media' => [
1204                                 'id'       => 666,
1205                                 'size'     => 666,
1206                                 'width'    => 666,
1207                                 'height'   => 666,
1208                                 'tmp_name' => $this->getTempImage(),
1209                                 'name'     => 'spacer.png',
1210                                 'type'     => 'image/png'
1211                         ]
1212                 ];
1213                 $_GET['status'] = '<b>Status content</b>';
1214
1215                 $result = api_statuses_mediap('json');
1216                 $this->assertStatus($result['status']);
1217         }
1218
1219         /**
1220          * Test the api_statuses_mediap() function without an authenticated user.
1221          *
1222          * @return void
1223          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1224          */
1225         public function testApiStatusesMediapWithoutAuthenticatedUser()
1226         {
1227                 $_SESSION['authenticated'] = false;
1228                 api_statuses_mediap('json');
1229         }
1230
1231         /**
1232          * Test the api_statuses_update() function.
1233          *
1234          * @return void
1235          */
1236         public function testApiStatusesUpdate()
1237         {
1238                 $_GET['status']                = 'Status content #friendica';
1239                 $_GET['in_reply_to_status_id'] = -1;
1240                 $_GET['lat']                   = 48;
1241                 $_GET['long']                  = 7;
1242                 $_FILES                        = [
1243                         'media' => [
1244                                 'id'       => 666,
1245                                 'size'     => 666,
1246                                 'width'    => 666,
1247                                 'height'   => 666,
1248                                 'tmp_name' => $this->getTempImage(),
1249                                 'name'     => 'spacer.png',
1250                                 'type'     => 'image/png'
1251                         ]
1252                 ];
1253
1254                 $result = api_statuses_update('json');
1255                 $this->assertStatus($result['status']);
1256         }
1257
1258         /**
1259          * Test the api_statuses_update() function with an HTML status.
1260          *
1261          * @return void
1262          */
1263         public function testApiStatusesUpdateWithHtml()
1264         {
1265                 $_GET['htmlstatus'] = '<b>Status content</b>';
1266
1267                 $result = api_statuses_update('json');
1268                 $this->assertStatus($result['status']);
1269         }
1270
1271         /**
1272          * Test the api_statuses_update() function without an authenticated user.
1273          *
1274          * @return void
1275          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1276          */
1277         public function testApiStatusesUpdateWithoutAuthenticatedUser()
1278         {
1279                 $_SESSION['authenticated'] = false;
1280                 api_statuses_update('json');
1281         }
1282
1283         /**
1284          * Test the api_statuses_update() function with a parent status.
1285          *
1286          * @return void
1287          */
1288         public function testApiStatusesUpdateWithParent()
1289         {
1290                 $this->markTestIncomplete('This triggers an exit() somewhere and kills PHPUnit.');
1291         }
1292
1293         /**
1294          * Test the api_statuses_update() function with a media_ids parameter.
1295          *
1296          * @return void
1297          */
1298         public function testApiStatusesUpdateWithMediaIds()
1299         {
1300                 $this->markTestIncomplete();
1301         }
1302
1303         /**
1304          * Test the api_statuses_update() function with the throttle limit reached.
1305          *
1306          * @return void
1307          */
1308         public function testApiStatusesUpdateWithDayThrottleReached()
1309         {
1310                 $this->markTestIncomplete();
1311         }
1312
1313         /**
1314          * Test the api_media_upload() function.
1315          *
1316          * @return void
1317          * @expectedException Friendica\Network\HTTPException\BadRequestException
1318          */
1319         public function testApiMediaUpload()
1320         {
1321                 api_media_upload();
1322         }
1323
1324         /**
1325          * Test the api_media_upload() function without an authenticated user.
1326          *
1327          * @return void
1328          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1329          */
1330         public function testApiMediaUploadWithoutAuthenticatedUser()
1331         {
1332                 $_SESSION['authenticated'] = false;
1333                 api_media_upload();
1334         }
1335
1336         /**
1337          * Test the api_media_upload() function with an invalid uploaded media.
1338          *
1339          * @return void
1340          * @expectedException Friendica\Network\HTTPException\InternalServerErrorException
1341          */
1342         public function testApiMediaUploadWithMedia()
1343         {
1344                 $_FILES = [
1345                         'media' => [
1346                                 'id'       => 666,
1347                                 'tmp_name' => 'tmp_name'
1348                         ]
1349                 ];
1350                 api_media_upload();
1351         }
1352
1353         /**
1354          * Test the api_media_upload() function with an valid uploaded media.
1355          *
1356          * @return void
1357          */
1358         public function testApiMediaUploadWithValidMedia()
1359         {
1360                 $_FILES    = [
1361                         'media' => [
1362                                 'id'       => 666,
1363                                 'size'     => 666,
1364                                 'width'    => 666,
1365                                 'height'   => 666,
1366                                 'tmp_name' => $this->getTempImage(),
1367                                 'name'     => 'spacer.png',
1368                                 'type'     => 'image/png'
1369                         ]
1370                 ];
1371                 $app       = DI::app();
1372                 $app->argc = 2;
1373
1374                 $result = api_media_upload();
1375                 $this->assertEquals('image/png', $result['media']['image']['image_type']);
1376                 $this->assertEquals(1, $result['media']['image']['w']);
1377                 $this->assertEquals(1, $result['media']['image']['h']);
1378                 $this->assertNotEmpty($result['media']['image']['friendica_preview_url']);
1379         }
1380
1381         /**
1382          * Test the api_status_show() function.
1383          */
1384         public function testApiStatusShowWithJson()
1385         {
1386                 $result = api_status_show('json', 1);
1387                 $this->assertStatus($result['status']);
1388         }
1389
1390         /**
1391          * Test the api_status_show() function with an XML result.
1392          */
1393         public function testApiStatusShowWithXml()
1394         {
1395                 $result = api_status_show('xml', 1);
1396                 $this->assertXml($result, 'statuses');
1397         }
1398
1399         /**
1400          * Test the api_get_last_status() function
1401          */
1402         public function testApiGetLastStatus()
1403         {
1404                 $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']);
1405
1406                 $this->assertNotNull($item);
1407         }
1408
1409         /**
1410          * Test the api_users_show() function.
1411          *
1412          * @return void
1413          */
1414         public function testApiUsersShow()
1415         {
1416                 $result = api_users_show('json');
1417                 // We can't use assertSelfUser() here because the user object is missing some properties.
1418                 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
1419                 $this->assertEquals('DFRN', $result['user']['location']);
1420                 $this->assertEquals($this->selfUser['name'], $result['user']['name']);
1421                 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1422                 $this->assertEquals('dfrn', $result['user']['network']);
1423                 $this->assertTrue($result['user']['verified']);
1424         }
1425
1426         /**
1427          * Test the api_users_show() function with an XML result.
1428          *
1429          * @return void
1430          */
1431         public function testApiUsersShowWithXml()
1432         {
1433                 $result = api_users_show('xml');
1434                 $this->assertXml($result, 'statuses');
1435         }
1436
1437         /**
1438          * Test the api_users_search() function.
1439          *
1440          * @return void
1441          */
1442         public function testApiUsersSearch()
1443         {
1444                 $_GET['q'] = 'othercontact';
1445                 $result    = api_users_search('json');
1446                 $this->assertOtherUser($result['users'][0]);
1447         }
1448
1449         /**
1450          * Test the api_users_search() function with an XML result.
1451          *
1452          * @return void
1453          */
1454         public function testApiUsersSearchWithXml()
1455         {
1456                 $_GET['q'] = 'othercontact';
1457                 $result    = api_users_search('xml');
1458                 $this->assertXml($result, 'users');
1459         }
1460
1461         /**
1462          * Test the api_users_search() function without a GET q parameter.
1463          *
1464          * @return void
1465          * @expectedException Friendica\Network\HTTPException\BadRequestException
1466          */
1467         public function testApiUsersSearchWithoutQuery()
1468         {
1469                 api_users_search('json');
1470         }
1471
1472         /**
1473          * Test the api_users_lookup() function.
1474          *
1475          * @return void
1476          * @expectedException Friendica\Network\HTTPException\NotFoundException
1477          */
1478         public function testApiUsersLookup()
1479         {
1480                 api_users_lookup('json');
1481         }
1482
1483         /**
1484          * Test the api_users_lookup() function with an user ID.
1485          *
1486          * @return void
1487          */
1488         public function testApiUsersLookupWithUserId()
1489         {
1490                 $_REQUEST['user_id'] = $this->otherUser['id'];
1491                 $result              = api_users_lookup('json');
1492                 $this->assertOtherUser($result['users'][0]);
1493         }
1494
1495         /**
1496          * Test the api_search() function.
1497          *
1498          * @return void
1499          */
1500         public function testApiSearch()
1501         {
1502                 $_REQUEST['q']      = 'reply';
1503                 $_REQUEST['max_id'] = 10;
1504                 $result             = api_search('json');
1505                 foreach ($result['status'] as $status) {
1506                         $this->assertStatus($status);
1507                         $this->assertContains('reply', $status['text'], null, true);
1508                 }
1509         }
1510
1511         /**
1512          * Test the api_search() function a count parameter.
1513          *
1514          * @return void
1515          */
1516         public function testApiSearchWithCount()
1517         {
1518                 $_REQUEST['q']     = 'reply';
1519                 $_REQUEST['count'] = 20;
1520                 $result            = api_search('json');
1521                 foreach ($result['status'] as $status) {
1522                         $this->assertStatus($status);
1523                         $this->assertContains('reply', $status['text'], null, true);
1524                 }
1525         }
1526
1527         /**
1528          * Test the api_search() function with an rpp parameter.
1529          *
1530          * @return void
1531          */
1532         public function testApiSearchWithRpp()
1533         {
1534                 $_REQUEST['q']   = 'reply';
1535                 $_REQUEST['rpp'] = 20;
1536                 $result          = api_search('json');
1537                 foreach ($result['status'] as $status) {
1538                         $this->assertStatus($status);
1539                         $this->assertContains('reply', $status['text'], null, true);
1540                 }
1541         }
1542
1543         /**
1544          * Test the api_search() function with an q parameter contains hashtag.
1545          *
1546          * @return void
1547          */
1548         public function testApiSearchWithHashtag()
1549         {
1550                 $_REQUEST['q'] = '%23friendica';
1551                 $result        = api_search('json');
1552                 foreach ($result['status'] as $status) {
1553                         $this->assertStatus($status);
1554                         $this->assertContains('#friendica', $status['text'], null, true);
1555                 }
1556         }
1557
1558         /**
1559          * Test the api_search() function with an exclude_replies parameter.
1560          *
1561          * @return void
1562          */
1563         public function testApiSearchWithExcludeReplies()
1564         {
1565                 $_REQUEST['max_id']          = 10;
1566                 $_REQUEST['exclude_replies'] = true;
1567                 $_REQUEST['q']               = 'friendica';
1568                 $result                      = api_search('json');
1569                 foreach ($result['status'] as $status) {
1570                         $this->assertStatus($status);
1571                 }
1572         }
1573
1574         /**
1575          * Test the api_search() function without an authenticated user.
1576          *
1577          * @return void
1578          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1579          */
1580         public function testApiSearchWithUnallowedUser()
1581         {
1582                 $_SESSION['allow_api'] = false;
1583                 $_GET['screen_name']   = $this->selfUser['nick'];
1584                 api_search('json');
1585         }
1586
1587         /**
1588          * Test the api_search() function without any GET query parameter.
1589          *
1590          * @return void
1591          * @expectedException Friendica\Network\HTTPException\BadRequestException
1592          */
1593         public function testApiSearchWithoutQuery()
1594         {
1595                 api_search('json');
1596         }
1597
1598         /**
1599          * Test the api_statuses_home_timeline() function.
1600          *
1601          * @return void
1602          */
1603         public function testApiStatusesHomeTimeline()
1604         {
1605                 $_REQUEST['max_id']          = 10;
1606                 $_REQUEST['exclude_replies'] = true;
1607                 $_REQUEST['conversation_id'] = 1;
1608                 $result                      = api_statuses_home_timeline('json');
1609                 $this->assertNotEmpty($result['status']);
1610                 foreach ($result['status'] as $status) {
1611                         $this->assertStatus($status);
1612                 }
1613         }
1614
1615         /**
1616          * Test the api_statuses_home_timeline() function with a negative page parameter.
1617          *
1618          * @return void
1619          */
1620         public function testApiStatusesHomeTimelineWithNegativePage()
1621         {
1622                 $_REQUEST['page'] = -2;
1623                 $result           = api_statuses_home_timeline('json');
1624                 $this->assertNotEmpty($result['status']);
1625                 foreach ($result['status'] as $status) {
1626                         $this->assertStatus($status);
1627                 }
1628         }
1629
1630         /**
1631          * Test the api_statuses_home_timeline() with an unallowed user.
1632          *
1633          * @return void
1634          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1635          */
1636         public function testApiStatusesHomeTimelineWithUnallowedUser()
1637         {
1638                 $_SESSION['allow_api'] = false;
1639                 $_GET['screen_name']   = $this->selfUser['nick'];
1640                 api_statuses_home_timeline('json');
1641         }
1642
1643         /**
1644          * Test the api_statuses_home_timeline() function with an RSS result.
1645          *
1646          * @return void
1647          */
1648         public function testApiStatusesHomeTimelineWithRss()
1649         {
1650                 $result = api_statuses_home_timeline('rss');
1651                 $this->assertXml($result, 'statuses');
1652         }
1653
1654         /**
1655          * Test the api_statuses_public_timeline() function.
1656          *
1657          * @return void
1658          */
1659         public function testApiStatusesPublicTimeline()
1660         {
1661                 $_REQUEST['max_id']          = 10;
1662                 $_REQUEST['conversation_id'] = 1;
1663                 $result                      = api_statuses_public_timeline('json');
1664                 $this->assertNotEmpty($result['status']);
1665                 foreach ($result['status'] as $status) {
1666                         $this->assertStatus($status);
1667                 }
1668         }
1669
1670         /**
1671          * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1672          *
1673          * @return void
1674          */
1675         public function testApiStatusesPublicTimelineWithExcludeReplies()
1676         {
1677                 $_REQUEST['max_id']          = 10;
1678                 $_REQUEST['exclude_replies'] = true;
1679                 $result                      = api_statuses_public_timeline('json');
1680                 $this->assertNotEmpty($result['status']);
1681                 foreach ($result['status'] as $status) {
1682                         $this->assertStatus($status);
1683                 }
1684         }
1685
1686         /**
1687          * Test the api_statuses_public_timeline() function with a negative page parameter.
1688          *
1689          * @return void
1690          */
1691         public function testApiStatusesPublicTimelineWithNegativePage()
1692         {
1693                 $_REQUEST['page'] = -2;
1694                 $result           = api_statuses_public_timeline('json');
1695                 $this->assertNotEmpty($result['status']);
1696                 foreach ($result['status'] as $status) {
1697                         $this->assertStatus($status);
1698                 }
1699         }
1700
1701         /**
1702          * Test the api_statuses_public_timeline() function with an unallowed user.
1703          *
1704          * @return void
1705          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1706          */
1707         public function testApiStatusesPublicTimelineWithUnallowedUser()
1708         {
1709                 $_SESSION['allow_api'] = false;
1710                 $_GET['screen_name']   = $this->selfUser['nick'];
1711                 api_statuses_public_timeline('json');
1712         }
1713
1714         /**
1715          * Test the api_statuses_public_timeline() function with an RSS result.
1716          *
1717          * @return void
1718          */
1719         public function testApiStatusesPublicTimelineWithRss()
1720         {
1721                 $result = api_statuses_public_timeline('rss');
1722                 $this->assertXml($result, 'statuses');
1723         }
1724
1725         /**
1726          * Test the api_statuses_networkpublic_timeline() function.
1727          *
1728          * @return void
1729          */
1730         public function testApiStatusesNetworkpublicTimeline()
1731         {
1732                 $_REQUEST['max_id'] = 10;
1733                 $result             = api_statuses_networkpublic_timeline('json');
1734                 $this->assertNotEmpty($result['status']);
1735                 foreach ($result['status'] as $status) {
1736                         $this->assertStatus($status);
1737                 }
1738         }
1739
1740         /**
1741          * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1742          *
1743          * @return void
1744          */
1745         public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1746         {
1747                 $_REQUEST['page'] = -2;
1748                 $result           = api_statuses_networkpublic_timeline('json');
1749                 $this->assertNotEmpty($result['status']);
1750                 foreach ($result['status'] as $status) {
1751                         $this->assertStatus($status);
1752                 }
1753         }
1754
1755         /**
1756          * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1757          *
1758          * @return void
1759          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1760          */
1761         public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1762         {
1763                 $_SESSION['allow_api'] = false;
1764                 $_GET['screen_name']   = $this->selfUser['nick'];
1765                 api_statuses_networkpublic_timeline('json');
1766         }
1767
1768         /**
1769          * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1770          *
1771          * @return void
1772          */
1773         public function testApiStatusesNetworkpublicTimelineWithRss()
1774         {
1775                 $result = api_statuses_networkpublic_timeline('rss');
1776                 $this->assertXml($result, 'statuses');
1777         }
1778
1779         /**
1780          * Test the api_statuses_show() function.
1781          *
1782          * @return void
1783          * @expectedException Friendica\Network\HTTPException\BadRequestException
1784          */
1785         public function testApiStatusesShow()
1786         {
1787                 api_statuses_show('json');
1788         }
1789
1790         /**
1791          * Test the api_statuses_show() function with an ID.
1792          *
1793          * @return void
1794          */
1795         public function testApiStatusesShowWithId()
1796         {
1797                 $this->app->argv[3] = 1;
1798                 $result             = api_statuses_show('json');
1799                 $this->assertStatus($result['status']);
1800         }
1801
1802         /**
1803          * Test the api_statuses_show() function with the conversation parameter.
1804          *
1805          * @return void
1806          */
1807         public function testApiStatusesShowWithConversation()
1808         {
1809                 $this->app->argv[3]       = 1;
1810                 $_REQUEST['conversation'] = 1;
1811                 $result                   = api_statuses_show('json');
1812                 $this->assertNotEmpty($result['status']);
1813                 foreach ($result['status'] as $status) {
1814                         $this->assertStatus($status);
1815                 }
1816         }
1817
1818         /**
1819          * Test the api_statuses_show() function with an unallowed user.
1820          *
1821          * @return void
1822          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1823          */
1824         public function testApiStatusesShowWithUnallowedUser()
1825         {
1826                 $_SESSION['allow_api'] = false;
1827                 $_GET['screen_name']   = $this->selfUser['nick'];
1828                 api_statuses_show('json');
1829         }
1830
1831         /**
1832          * Test the api_conversation_show() function.
1833          *
1834          * @return void
1835          * @expectedException Friendica\Network\HTTPException\BadRequestException
1836          */
1837         public function testApiConversationShow()
1838         {
1839                 api_conversation_show('json');
1840         }
1841
1842         /**
1843          * Test the api_conversation_show() function with an ID.
1844          *
1845          * @return void
1846          */
1847         public function testApiConversationShowWithId()
1848         {
1849                 $this->app->argv[3] = 1;
1850                 $_REQUEST['max_id'] = 10;
1851                 $_REQUEST['page']   = -2;
1852                 $result             = api_conversation_show('json');
1853                 $this->assertNotEmpty($result['status']);
1854                 foreach ($result['status'] as $status) {
1855                         $this->assertStatus($status);
1856                 }
1857         }
1858
1859         /**
1860          * Test the api_conversation_show() function with an unallowed user.
1861          *
1862          * @return void
1863          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1864          */
1865         public function testApiConversationShowWithUnallowedUser()
1866         {
1867                 $_SESSION['allow_api'] = false;
1868                 $_GET['screen_name']   = $this->selfUser['nick'];
1869                 api_conversation_show('json');
1870         }
1871
1872         /**
1873          * Test the api_statuses_repeat() function.
1874          *
1875          * @return void
1876          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1877          */
1878         public function testApiStatusesRepeat()
1879         {
1880                 api_statuses_repeat('json');
1881         }
1882
1883         /**
1884          * Test the api_statuses_repeat() function without an authenticated user.
1885          *
1886          * @return void
1887          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1888          */
1889         public function testApiStatusesRepeatWithoutAuthenticatedUser()
1890         {
1891                 $_SESSION['authenticated'] = false;
1892                 api_statuses_repeat('json');
1893         }
1894
1895         /**
1896          * Test the api_statuses_repeat() function with an ID.
1897          *
1898          * @return void
1899          */
1900         public function testApiStatusesRepeatWithId()
1901         {
1902                 $this->app->argv[3] = 1;
1903                 $result             = api_statuses_repeat('json');
1904                 $this->assertStatus($result['status']);
1905
1906                 // Also test with a shared status
1907                 $this->app->argv[3] = 5;
1908                 $result             = api_statuses_repeat('json');
1909                 $this->assertStatus($result['status']);
1910         }
1911
1912         /**
1913          * Test the api_statuses_destroy() function.
1914          *
1915          * @return void
1916          * @expectedException Friendica\Network\HTTPException\BadRequestException
1917          */
1918         public function testApiStatusesDestroy()
1919         {
1920                 api_statuses_destroy('json');
1921         }
1922
1923         /**
1924          * Test the api_statuses_destroy() function without an authenticated user.
1925          *
1926          * @return void
1927          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1928          */
1929         public function testApiStatusesDestroyWithoutAuthenticatedUser()
1930         {
1931                 $_SESSION['authenticated'] = false;
1932                 api_statuses_destroy('json');
1933         }
1934
1935         /**
1936          * Test the api_statuses_destroy() function with an ID.
1937          *
1938          * @return void
1939          */
1940         public function testApiStatusesDestroyWithId()
1941         {
1942                 $this->app->argv[3] = 1;
1943                 $result             = api_statuses_destroy('json');
1944                 $this->assertStatus($result['status']);
1945         }
1946
1947         /**
1948          * Test the api_statuses_mentions() function.
1949          *
1950          * @return void
1951          */
1952         public function testApiStatusesMentions()
1953         {
1954                 $this->app->user    = ['nickname' => $this->selfUser['nick']];
1955                 $_REQUEST['max_id'] = 10;
1956                 $result             = api_statuses_mentions('json');
1957                 $this->assertEmpty($result['status']);
1958                 // We should test with mentions in the database.
1959         }
1960
1961         /**
1962          * Test the api_statuses_mentions() function with a negative page parameter.
1963          *
1964          * @return void
1965          */
1966         public function testApiStatusesMentionsWithNegativePage()
1967         {
1968                 $_REQUEST['page'] = -2;
1969                 $result           = api_statuses_mentions('json');
1970                 $this->assertEmpty($result['status']);
1971         }
1972
1973         /**
1974          * Test the api_statuses_mentions() function with an unallowed user.
1975          *
1976          * @return void
1977          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1978          */
1979         public function testApiStatusesMentionsWithUnallowedUser()
1980         {
1981                 $_SESSION['allow_api'] = false;
1982                 $_GET['screen_name']   = $this->selfUser['nick'];
1983                 api_statuses_mentions('json');
1984         }
1985
1986         /**
1987          * Test the api_statuses_mentions() function with an RSS result.
1988          *
1989          * @return void
1990          */
1991         public function testApiStatusesMentionsWithRss()
1992         {
1993                 $result = api_statuses_mentions('rss');
1994                 $this->assertXml($result, 'statuses');
1995         }
1996
1997         /**
1998          * Test the api_statuses_user_timeline() function.
1999          *
2000          * @return void
2001          */
2002         public function testApiStatusesUserTimeline()
2003         {
2004                 $_REQUEST['max_id']          = 10;
2005                 $_REQUEST['exclude_replies'] = true;
2006                 $_REQUEST['conversation_id'] = 1;
2007                 $result                      = api_statuses_user_timeline('json');
2008                 $this->assertNotEmpty($result['status']);
2009                 foreach ($result['status'] as $status) {
2010                         $this->assertStatus($status);
2011                 }
2012         }
2013
2014         /**
2015          * Test the api_statuses_user_timeline() function with a negative page parameter.
2016          *
2017          * @return void
2018          */
2019         public function testApiStatusesUserTimelineWithNegativePage()
2020         {
2021                 $_REQUEST['page'] = -2;
2022                 $result           = api_statuses_user_timeline('json');
2023                 $this->assertNotEmpty($result['status']);
2024                 foreach ($result['status'] as $status) {
2025                         $this->assertStatus($status);
2026                 }
2027         }
2028
2029         /**
2030          * Test the api_statuses_user_timeline() function with an RSS result.
2031          *
2032          * @return void
2033          */
2034         public function testApiStatusesUserTimelineWithRss()
2035         {
2036                 $result = api_statuses_user_timeline('rss');
2037                 $this->assertXml($result, 'statuses');
2038         }
2039
2040         /**
2041          * Test the api_statuses_user_timeline() function with an unallowed user.
2042          *
2043          * @return void
2044          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2045          */
2046         public function testApiStatusesUserTimelineWithUnallowedUser()
2047         {
2048                 $_SESSION['allow_api'] = false;
2049                 $_GET['screen_name']   = $this->selfUser['nick'];
2050                 api_statuses_user_timeline('json');
2051         }
2052
2053         /**
2054          * Test the api_favorites_create_destroy() function.
2055          *
2056          * @return void
2057          * @expectedException Friendica\Network\HTTPException\BadRequestException
2058          */
2059         public function testApiFavoritesCreateDestroy()
2060         {
2061                 $this->app->argv = ['api', '1.1', 'favorites', 'create'];
2062                 $this->app->argc = count($this->app->argv);
2063                 api_favorites_create_destroy('json');
2064         }
2065
2066         /**
2067          * Test the api_favorites_create_destroy() function with an invalid ID.
2068          *
2069          * @return void
2070          * @expectedException Friendica\Network\HTTPException\BadRequestException
2071          */
2072         public function testApiFavoritesCreateDestroyWithInvalidId()
2073         {
2074                 $this->app->argv = ['api', '1.1', 'favorites', 'create', '12.json'];
2075                 $this->app->argc = count($this->app->argv);
2076                 api_favorites_create_destroy('json');
2077         }
2078
2079         /**
2080          * Test the api_favorites_create_destroy() function with an invalid action.
2081          *
2082          * @return void
2083          * @expectedException Friendica\Network\HTTPException\BadRequestException
2084          */
2085         public function testApiFavoritesCreateDestroyWithInvalidAction()
2086         {
2087                 $this->app->argv = ['api', '1.1', 'favorites', 'change.json'];
2088                 $this->app->argc = count($this->app->argv);
2089                 $_REQUEST['id']  = 1;
2090                 api_favorites_create_destroy('json');
2091         }
2092
2093         /**
2094          * Test the api_favorites_create_destroy() function with the create action.
2095          *
2096          * @return void
2097          */
2098         public function testApiFavoritesCreateDestroyWithCreateAction()
2099         {
2100                 $this->app->argv = ['api', '1.1', 'favorites', 'create.json'];
2101                 $this->app->argc = count($this->app->argv);
2102                 $_REQUEST['id']  = 3;
2103                 $result          = api_favorites_create_destroy('json');
2104                 $this->assertStatus($result['status']);
2105         }
2106
2107         /**
2108          * Test the api_favorites_create_destroy() function with the create action and an RSS result.
2109          *
2110          * @return void
2111          */
2112         public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
2113         {
2114                 $this->app->argv = ['api', '1.1', 'favorites', 'create.rss'];
2115                 $this->app->argc = count($this->app->argv);
2116                 $_REQUEST['id']  = 3;
2117                 $result          = api_favorites_create_destroy('rss');
2118                 $this->assertXml($result, 'status');
2119         }
2120
2121         /**
2122          * Test the api_favorites_create_destroy() function with the destroy action.
2123          *
2124          * @return void
2125          */
2126         public function testApiFavoritesCreateDestroyWithDestroyAction()
2127         {
2128                 $this->app->argv = ['api', '1.1', 'favorites', 'destroy.json'];
2129                 $this->app->argc = count($this->app->argv);
2130                 $_REQUEST['id']  = 3;
2131                 $result          = api_favorites_create_destroy('json');
2132                 $this->assertStatus($result['status']);
2133         }
2134
2135         /**
2136          * Test the api_favorites_create_destroy() function without an authenticated user.
2137          *
2138          * @return void
2139          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2140          */
2141         public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
2142         {
2143                 $this->app->argv           = ['api', '1.1', 'favorites', 'create.json'];
2144                 $this->app->argc           = count($this->app->argv);
2145                 $_SESSION['authenticated'] = false;
2146                 api_favorites_create_destroy('json');
2147         }
2148
2149         /**
2150          * Test the api_favorites() function.
2151          *
2152          * @return void
2153          */
2154         public function testApiFavorites()
2155         {
2156                 $_REQUEST['page']   = -1;
2157                 $_REQUEST['max_id'] = 10;
2158                 $result             = api_favorites('json');
2159                 foreach ($result['status'] as $status) {
2160                         $this->assertStatus($status);
2161                 }
2162         }
2163
2164         /**
2165          * Test the api_favorites() function with an RSS result.
2166          *
2167          * @return void
2168          */
2169         public function testApiFavoritesWithRss()
2170         {
2171                 $result = api_favorites('rss');
2172                 $this->assertXml($result, 'statuses');
2173         }
2174
2175         /**
2176          * Test the api_favorites() function with an unallowed user.
2177          *
2178          * @return void
2179          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2180          */
2181         public function testApiFavoritesWithUnallowedUser()
2182         {
2183                 $_SESSION['allow_api'] = false;
2184                 $_GET['screen_name']   = $this->selfUser['nick'];
2185                 api_favorites('json');
2186         }
2187
2188         /**
2189          * Test the api_format_messages() function.
2190          *
2191          * @return void
2192          */
2193         public function testApiFormatMessages()
2194         {
2195                 $result = api_format_messages(
2196                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2197                         ['id' => 2, 'screen_name' => 'recipient_name'],
2198                         ['id' => 3, 'screen_name' => 'sender_name']
2199                 );
2200                 $this->assertEquals('item_title' . "\n" . 'item_body', $result['text']);
2201                 $this->assertEquals(1, $result['id']);
2202                 $this->assertEquals(2, $result['recipient_id']);
2203                 $this->assertEquals(3, $result['sender_id']);
2204                 $this->assertEquals('recipient_name', $result['recipient_screen_name']);
2205                 $this->assertEquals('sender_name', $result['sender_screen_name']);
2206         }
2207
2208         /**
2209          * Test the api_format_messages() function with HTML.
2210          *
2211          * @return void
2212          */
2213         public function testApiFormatMessagesWithHtmlText()
2214         {
2215                 $_GET['getText'] = 'html';
2216                 $result          = api_format_messages(
2217                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2218                         ['id' => 2, 'screen_name' => 'recipient_name'],
2219                         ['id' => 3, 'screen_name' => 'sender_name']
2220                 );
2221                 $this->assertEquals('item_title', $result['title']);
2222                 $this->assertEquals('<strong>item_body</strong>', $result['text']);
2223         }
2224
2225         /**
2226          * Test the api_format_messages() function with plain text.
2227          *
2228          * @return void
2229          */
2230         public function testApiFormatMessagesWithPlainText()
2231         {
2232                 $_GET['getText'] = 'plain';
2233                 $result          = api_format_messages(
2234                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2235                         ['id' => 2, 'screen_name' => 'recipient_name'],
2236                         ['id' => 3, 'screen_name' => 'sender_name']
2237                 );
2238                 $this->assertEquals('item_title', $result['title']);
2239                 $this->assertEquals('item_body', $result['text']);
2240         }
2241
2242         /**
2243          * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2244          *
2245          * @return void
2246          */
2247         public function testApiFormatMessagesWithoutUserObjects()
2248         {
2249                 $_GET['getUserObjects'] = 'false';
2250                 $result                 = api_format_messages(
2251                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2252                         ['id' => 2, 'screen_name' => 'recipient_name'],
2253                         ['id' => 3, 'screen_name' => 'sender_name']
2254                 );
2255                 $this->assertTrue(!isset($result['sender']));
2256                 $this->assertTrue(!isset($result['recipient']));
2257         }
2258
2259         /**
2260          * Test the api_convert_item() function.
2261          *
2262          * @return void
2263          */
2264         public function testApiConvertItem()
2265         {
2266                 $result = api_convert_item(
2267                         [
2268                                 'network' => 'feed',
2269                                 'title'   => 'item_title',
2270                                 // We need a long string to test that it is correctly cut
2271                                 'body'    => 'perspiciatis impedit voluptatem quis molestiae ea qui ' .
2272                                              'reiciendis dolorum aut ducimus sunt consequatur inventore dolor ' .
2273                                              'officiis pariatur doloremque nemo culpa aut quidem qui dolore ' .
2274                                              'laudantium atque commodi alias voluptatem non possimus aperiam ' .
2275                                              'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium ' .
2276                                              'repellendus quibusdam et et inventore mollitia rerum sit autem ' .
2277                                              'pariatur maiores ipsum accusantium perferendis vel sit possimus ' .
2278                                              'veritatis nihil distinctio qui eum repellat officia illum quos ' .
2279                                              'impedit quam iste esse unde qui suscipit aut facilis ut inventore ' .
2280                                              'omnis exercitationem quo magnam consequatur maxime aut illum ' .
2281                                              'soluta quaerat natus unde aspernatur et sed beatae nihil ullam ' .
2282                                              'temporibus corporis ratione blanditiis perspiciatis impedit ' .
2283                                              'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus ' .
2284                                              'sunt consequatur inventore dolor officiis pariatur doloremque ' .
2285                                              'nemo culpa aut quidem qui dolore laudantium atque commodi alias ' .
2286                                              'voluptatem non possimus aperiam ipsum rerum consequuntur aut ' .
2287                                              'amet fugit quia aliquid praesentium repellendus quibusdam et et ' .
2288                                              'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium ' .
2289                                              'perferendis vel sit possimus veritatis nihil distinctio qui eum ' .
2290                                              'repellat officia illum quos impedit quam iste esse unde qui ' .
2291                                              'suscipit aut facilis ut inventore omnis exercitationem quo magnam ' .
2292                                              'consequatur maxime aut illum soluta quaerat natus unde aspernatur ' .
2293                                              'et sed beatae nihil ullam temporibus corporis ratione blanditiis',
2294                                 'plink'   => 'item_plink'
2295                         ]
2296                 );
2297                 $this->assertStringStartsWith('item_title', $result['text']);
2298                 $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2299         }
2300
2301         /**
2302          * Test the api_convert_item() function with an empty item body.
2303          *
2304          * @return void
2305          */
2306         public function testApiConvertItemWithoutBody()
2307         {
2308                 $result = api_convert_item(
2309                         [
2310                                 'network' => 'feed',
2311                                 'title'   => 'item_title',
2312                                 'body'    => '',
2313                                 'plink'   => 'item_plink'
2314                         ]
2315                 );
2316                 $this->assertEquals('item_title', $result['text']);
2317                 $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2318         }
2319
2320         /**
2321          * Test the api_convert_item() function with the title in the body.
2322          *
2323          * @return void
2324          */
2325         public function testApiConvertItemWithTitleInBody()
2326         {
2327                 $result = api_convert_item(
2328                         [
2329                                 'title' => 'item_title',
2330                                 'body'  => 'item_title item_body'
2331                         ]
2332                 );
2333                 $this->assertEquals('item_title item_body', $result['text']);
2334                 $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2335         }
2336
2337         /**
2338          * Test the api_get_attachments() function.
2339          *
2340          * @return void
2341          */
2342         public function testApiGetAttachments()
2343         {
2344                 $body = 'body';
2345                 $this->assertEmpty(api_get_attachments($body));
2346         }
2347
2348         /**
2349          * Test the api_get_attachments() function with an img tag.
2350          *
2351          * @return void
2352          */
2353         public function testApiGetAttachmentsWithImage()
2354         {
2355                 $body = '[img]http://via.placeholder.com/1x1.png[/img]';
2356                 $this->assertInternalType('array', api_get_attachments($body));
2357         }
2358
2359         /**
2360          * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2361          *
2362          * @return void
2363          */
2364         public function testApiGetAttachmentsWithImageAndAndStatus()
2365         {
2366                 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2367                 $body                       = '[img]http://via.placeholder.com/1x1.png[/img]';
2368                 $this->assertInternalType('array', api_get_attachments($body));
2369         }
2370
2371         /**
2372          * Test the api_get_entitities() function.
2373          *
2374          * @return void
2375          */
2376         public function testApiGetEntitities()
2377         {
2378                 $text = 'text';
2379                 $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
2380         }
2381
2382         /**
2383          * Test the api_get_entitities() function with the include_entities parameter.
2384          *
2385          * @return void
2386          */
2387         public function testApiGetEntititiesWithIncludeEntities()
2388         {
2389                 $_REQUEST['include_entities'] = 'true';
2390                 $text                         = 'text';
2391                 $result                       = api_get_entitities($text, 'bbcode');
2392                 $this->assertInternalType('array', $result['hashtags']);
2393                 $this->assertInternalType('array', $result['symbols']);
2394                 $this->assertInternalType('array', $result['urls']);
2395                 $this->assertInternalType('array', $result['user_mentions']);
2396         }
2397
2398         /**
2399          * Test the api_format_items_embeded_images() function.
2400          *
2401          * @return void
2402          */
2403         public function testApiFormatItemsEmbededImages()
2404         {
2405                 $this->assertEquals(
2406                         'text ' . DI::baseUrl() . '/display/item_guid',
2407                         api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2408                 );
2409         }
2410
2411         /**
2412          * Test the api_contactlink_to_array() function.
2413          *
2414          * @return void
2415          */
2416         public function testApiContactlinkToArray()
2417         {
2418                 $this->assertEquals(
2419                         [
2420                                 'name' => 'text',
2421                                 'url'  => '',
2422                         ],
2423                         api_contactlink_to_array('text')
2424                 );
2425         }
2426
2427         /**
2428          * Test the api_contactlink_to_array() function with an URL.
2429          *
2430          * @return void
2431          */
2432         public function testApiContactlinkToArrayWithUrl()
2433         {
2434                 $this->assertEquals(
2435                         [
2436                                 'name' => ['link_text'],
2437                                 'url'  => ['url'],
2438                         ],
2439                         api_contactlink_to_array('text <a href="url">link_text</a>')
2440                 );
2441         }
2442
2443         /**
2444          * Test the api_format_items_activities() function.
2445          *
2446          * @return void
2447          */
2448         public function testApiFormatItemsActivities()
2449         {
2450                 $item   = ['uid' => 0, 'uri' => ''];
2451                 $result = api_format_items_activities($item);
2452                 $this->assertArrayHasKey('like', $result);
2453                 $this->assertArrayHasKey('dislike', $result);
2454                 $this->assertArrayHasKey('attendyes', $result);
2455                 $this->assertArrayHasKey('attendno', $result);
2456                 $this->assertArrayHasKey('attendmaybe', $result);
2457         }
2458
2459         /**
2460          * Test the api_format_items_activities() function with an XML result.
2461          *
2462          * @return void
2463          */
2464         public function testApiFormatItemsActivitiesWithXml()
2465         {
2466                 $item   = ['uid' => 0, 'uri' => ''];
2467                 $result = api_format_items_activities($item, 'xml');
2468                 $this->assertArrayHasKey('friendica:like', $result);
2469                 $this->assertArrayHasKey('friendica:dislike', $result);
2470                 $this->assertArrayHasKey('friendica:attendyes', $result);
2471                 $this->assertArrayHasKey('friendica:attendno', $result);
2472                 $this->assertArrayHasKey('friendica:attendmaybe', $result);
2473         }
2474
2475         /**
2476          * Test the api_format_items() function.
2477          *
2478          * @return void
2479          */
2480         public function testApiFormatItems()
2481         {
2482                 $items  = [
2483                         [
2484                                 'item_network'   => 'item_network',
2485                                 'source'         => 'web',
2486                                 'coord'          => '5 7',
2487                                 'body'           => '',
2488                                 'verb'           => '',
2489                                 'author-id'      => 43,
2490                                 'author-network' => Protocol::DFRN,
2491                                 'author-link'    => 'http://localhost/profile/othercontact',
2492                                 'plink'          => '',
2493                         ]
2494                 ];
2495                 $result = api_format_items($items, ['id' => 0], true);
2496                 foreach ($result as $status) {
2497                         $this->assertStatus($status);
2498                 }
2499         }
2500
2501         /**
2502          * Test the api_format_items() function with an XML result.
2503          *
2504          * @return void
2505          */
2506         public function testApiFormatItemsWithXml()
2507         {
2508                 $items  = [
2509                         [
2510                                 'coord'          => '5 7',
2511                                 'body'           => '',
2512                                 'verb'           => '',
2513                                 'author-id'      => 43,
2514                                 'author-network' => Protocol::DFRN,
2515                                 'author-link'    => 'http://localhost/profile/othercontact',
2516                                 'plink'          => '',
2517                         ]
2518                 ];
2519                 $result = api_format_items($items, ['id' => 0], true, 'xml');
2520                 foreach ($result as $status) {
2521                         $this->assertStatus($status);
2522                 }
2523         }
2524
2525         /**
2526          * Test the api_format_items() function.
2527          *
2528          * @return void
2529          */
2530         public function testApiAccountRateLimitStatus()
2531         {
2532                 $result = api_account_rate_limit_status('json');
2533                 $this->assertEquals(150, $result['hash']['remaining_hits']);
2534                 $this->assertEquals(150, $result['hash']['hourly_limit']);
2535                 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2536         }
2537
2538         /**
2539          * Test the api_format_items() function with an XML result.
2540          *
2541          * @return void
2542          */
2543         public function testApiAccountRateLimitStatusWithXml()
2544         {
2545                 $result = api_account_rate_limit_status('xml');
2546                 $this->assertXml($result, 'hash');
2547         }
2548
2549         /**
2550          * Test the api_help_test() function.
2551          *
2552          * @return void
2553          */
2554         public function testApiHelpTest()
2555         {
2556                 $result = api_help_test('json');
2557                 $this->assertEquals(['ok' => 'ok'], $result);
2558         }
2559
2560         /**
2561          * Test the api_help_test() function with an XML result.
2562          *
2563          * @return void
2564          */
2565         public function testApiHelpTestWithXml()
2566         {
2567                 $result = api_help_test('xml');
2568                 $this->assertXml($result, 'ok');
2569         }
2570
2571         /**
2572          * Test the api_lists_list() function.
2573          *
2574          * @return void
2575          */
2576         public function testApiListsList()
2577         {
2578                 $result = api_lists_list('json');
2579                 $this->assertEquals(['lists_list' => []], $result);
2580         }
2581
2582         /**
2583          * Test the api_lists_ownerships() function.
2584          *
2585          * @return void
2586          */
2587         public function testApiListsOwnerships()
2588         {
2589                 $result = api_lists_ownerships('json');
2590                 foreach ($result['lists']['lists'] as $list) {
2591                         $this->assertList($list);
2592                 }
2593         }
2594
2595         /**
2596          * Test the api_lists_ownerships() function without an authenticated user.
2597          *
2598          * @return void
2599          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2600          */
2601         public function testApiListsOwnershipsWithoutAuthenticatedUser()
2602         {
2603                 $_SESSION['authenticated'] = false;
2604                 api_lists_ownerships('json');
2605         }
2606
2607         /**
2608          * Test the api_lists_statuses() function.
2609          *
2610          * @expectedException Friendica\Network\HTTPException\BadRequestException
2611          * @return void
2612          */
2613         public function testApiListsStatuses()
2614         {
2615                 api_lists_statuses('json');
2616         }
2617
2618         /**
2619          * Test the api_lists_statuses() function with a list ID.
2620          *
2621          * @return void
2622          */
2623         public function testApiListsStatusesWithListId()
2624         {
2625                 $_REQUEST['list_id'] = 1;
2626                 $_REQUEST['page']    = -1;
2627                 $_REQUEST['max_id']  = 10;
2628                 $result              = api_lists_statuses('json');
2629                 foreach ($result['status'] as $status) {
2630                         $this->assertStatus($status);
2631                 }
2632         }
2633
2634         /**
2635          * Test the api_lists_statuses() function with a list ID and a RSS result.
2636          *
2637          * @return void
2638          */
2639         public function testApiListsStatusesWithListIdAndRss()
2640         {
2641                 $_REQUEST['list_id'] = 1;
2642                 $result              = api_lists_statuses('rss');
2643                 $this->assertXml($result, 'statuses');
2644         }
2645
2646         /**
2647          * Test the api_lists_statuses() function with an unallowed user.
2648          *
2649          * @return void
2650          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2651          */
2652         public function testApiListsStatusesWithUnallowedUser()
2653         {
2654                 $_SESSION['allow_api'] = false;
2655                 $_GET['screen_name']   = $this->selfUser['nick'];
2656                 api_lists_statuses('json');
2657         }
2658
2659         /**
2660          * Test the api_statuses_f() function.
2661          *
2662          * @return void
2663          */
2664         public function testApiStatusesFWithFriends()
2665         {
2666                 $_GET['page'] = -1;
2667                 $result       = api_statuses_f('friends');
2668                 $this->assertArrayHasKey('user', $result);
2669         }
2670
2671         /**
2672          * Test the api_statuses_f() function.
2673          *
2674          * @return void
2675          */
2676         public function testApiStatusesFWithFollowers()
2677         {
2678                 $result = api_statuses_f('followers');
2679                 $this->assertArrayHasKey('user', $result);
2680         }
2681
2682         /**
2683          * Test the api_statuses_f() function.
2684          *
2685          * @return void
2686          */
2687         public function testApiStatusesFWithBlocks()
2688         {
2689                 $result = api_statuses_f('blocks');
2690                 $this->assertArrayHasKey('user', $result);
2691         }
2692
2693         /**
2694          * Test the api_statuses_f() function.
2695          *
2696          * @return void
2697          */
2698         public function testApiStatusesFWithIncoming()
2699         {
2700                 $result = api_statuses_f('incoming');
2701                 $this->assertArrayHasKey('user', $result);
2702         }
2703
2704         /**
2705          * Test the api_statuses_f() function an undefined cursor GET variable.
2706          *
2707          * @return void
2708          */
2709         public function testApiStatusesFWithUndefinedCursor()
2710         {
2711                 $_GET['cursor'] = 'undefined';
2712                 $this->assertFalse(api_statuses_f('friends'));
2713         }
2714
2715         /**
2716          * Test the api_statuses_friends() function.
2717          *
2718          * @return void
2719          */
2720         public function testApiStatusesFriends()
2721         {
2722                 $result = api_statuses_friends('json');
2723                 $this->assertArrayHasKey('user', $result);
2724         }
2725
2726         /**
2727          * Test the api_statuses_friends() function an undefined cursor GET variable.
2728          *
2729          * @return void
2730          */
2731         public function testApiStatusesFriendsWithUndefinedCursor()
2732         {
2733                 $_GET['cursor'] = 'undefined';
2734                 $this->assertFalse(api_statuses_friends('json'));
2735         }
2736
2737         /**
2738          * Test the api_statuses_followers() function.
2739          *
2740          * @return void
2741          */
2742         public function testApiStatusesFollowers()
2743         {
2744                 $result = api_statuses_followers('json');
2745                 $this->assertArrayHasKey('user', $result);
2746         }
2747
2748         /**
2749          * Test the api_statuses_followers() function an undefined cursor GET variable.
2750          *
2751          * @return void
2752          */
2753         public function testApiStatusesFollowersWithUndefinedCursor()
2754         {
2755                 $_GET['cursor'] = 'undefined';
2756                 $this->assertFalse(api_statuses_followers('json'));
2757         }
2758
2759         /**
2760          * Test the api_blocks_list() function.
2761          *
2762          * @return void
2763          */
2764         public function testApiBlocksList()
2765         {
2766                 $result = api_blocks_list('json');
2767                 $this->assertArrayHasKey('user', $result);
2768         }
2769
2770         /**
2771          * Test the api_blocks_list() function an undefined cursor GET variable.
2772          *
2773          * @return void
2774          */
2775         public function testApiBlocksListWithUndefinedCursor()
2776         {
2777                 $_GET['cursor'] = 'undefined';
2778                 $this->assertFalse(api_blocks_list('json'));
2779         }
2780
2781         /**
2782          * Test the api_friendships_incoming() function.
2783          *
2784          * @return void
2785          */
2786         public function testApiFriendshipsIncoming()
2787         {
2788                 $result = api_friendships_incoming('json');
2789                 $this->assertArrayHasKey('id', $result);
2790         }
2791
2792         /**
2793          * Test the api_friendships_incoming() function an undefined cursor GET variable.
2794          *
2795          * @return void
2796          */
2797         public function testApiFriendshipsIncomingWithUndefinedCursor()
2798         {
2799                 $_GET['cursor'] = 'undefined';
2800                 $this->assertFalse(api_friendships_incoming('json'));
2801         }
2802
2803         /**
2804          * Test the api_statusnet_config() function.
2805          *
2806          * @return void
2807          */
2808         public function testApiStatusnetConfig()
2809         {
2810                 $result = api_statusnet_config('json');
2811                 $this->assertEquals('localhost', $result['config']['site']['server']);
2812                 $this->assertEquals('default', $result['config']['site']['theme']);
2813                 $this->assertEquals(DI::baseUrl() . '/images/friendica-64.png', $result['config']['site']['logo']);
2814                 $this->assertTrue($result['config']['site']['fancy']);
2815                 $this->assertEquals('en', $result['config']['site']['language']);
2816                 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2817                 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2818                 $this->assertEquals('false', $result['config']['site']['private']);
2819                 $this->assertEquals('false', $result['config']['site']['ssl']);
2820                 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2821         }
2822
2823         /**
2824          * Test the api_statusnet_version() function.
2825          *
2826          * @return void
2827          */
2828         public function testApiStatusnetVersion()
2829         {
2830                 $result = api_statusnet_version('json');
2831                 $this->assertEquals('0.9.7', $result['version']);
2832         }
2833
2834         /**
2835          * Test the api_direct_messages_new() function.
2836          *
2837          * @return void
2838          */
2839         public function testApiDirectMessagesNew()
2840         {
2841                 $result = api_direct_messages_new('json');
2842                 $this->assertNull($result);
2843         }
2844
2845         /**
2846          * Test the api_direct_messages_new() function without an authenticated user.
2847          *
2848          * @return void
2849          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2850          */
2851         public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2852         {
2853                 $_SESSION['authenticated'] = false;
2854                 api_direct_messages_new('json');
2855         }
2856
2857         /**
2858          * Test the api_direct_messages_new() function with an user ID.
2859          *
2860          * @return void
2861          */
2862         public function testApiDirectMessagesNewWithUserId()
2863         {
2864                 $_POST['text']    = 'message_text';
2865                 $_POST['user_id'] = $this->otherUser['id'];
2866                 $result           = api_direct_messages_new('json');
2867                 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
2868         }
2869
2870         /**
2871          * Test the api_direct_messages_new() function with a screen name.
2872          *
2873          * @return void
2874          */
2875         public function testApiDirectMessagesNewWithScreenName()
2876         {
2877                 $this->app->user    = ['nickname' => $this->selfUser['nick']];
2878                 $_POST['text']        = 'message_text';
2879                 $_POST['screen_name'] = $this->friendUser['nick'];
2880                 $result               = api_direct_messages_new('json');
2881                 $this->assertContains('message_text', $result['direct_message']['text']);
2882                 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2883                 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2884         }
2885
2886         /**
2887          * Test the api_direct_messages_new() function with a title.
2888          *
2889          * @return void
2890          */
2891         public function testApiDirectMessagesNewWithTitle()
2892         {
2893                 $this->app->user    = ['nickname' => $this->selfUser['nick']];
2894                 $_POST['text']        = 'message_text';
2895                 $_POST['screen_name'] = $this->friendUser['nick'];
2896                 $_REQUEST['title']    = 'message_title';
2897                 $result               = api_direct_messages_new('json');
2898                 $this->assertContains('message_text', $result['direct_message']['text']);
2899                 $this->assertContains('message_title', $result['direct_message']['text']);
2900                 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2901                 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2902         }
2903
2904         /**
2905          * Test the api_direct_messages_new() function with an RSS result.
2906          *
2907          * @return void
2908          */
2909         public function testApiDirectMessagesNewWithRss()
2910         {
2911                 $this->app->user    = ['nickname' => $this->selfUser['nick']];
2912                 $_POST['text']        = 'message_text';
2913                 $_POST['screen_name'] = $this->friendUser['nick'];
2914                 $result               = api_direct_messages_new('rss');
2915                 $this->assertXml($result, 'direct-messages');
2916         }
2917
2918         /**
2919          * Test the api_direct_messages_destroy() function.
2920          *
2921          * @return void
2922          * @expectedException Friendica\Network\HTTPException\BadRequestException
2923          */
2924         public function testApiDirectMessagesDestroy()
2925         {
2926                 api_direct_messages_destroy('json');
2927         }
2928
2929         /**
2930          * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2931          *
2932          * @return void
2933          */
2934         public function testApiDirectMessagesDestroyWithVerbose()
2935         {
2936                 $_GET['friendica_verbose'] = 'true';
2937                 $result                    = api_direct_messages_destroy('json');
2938                 $this->assertEquals(
2939                         [
2940                                 '$result' => [
2941                                         'result'  => 'error',
2942                                         'message' => 'message id or parenturi not specified'
2943                                 ]
2944                         ],
2945                         $result
2946                 );
2947         }
2948
2949         /**
2950          * Test the api_direct_messages_destroy() function without an authenticated user.
2951          *
2952          * @return void
2953          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2954          */
2955         public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2956         {
2957                 $_SESSION['authenticated'] = false;
2958                 api_direct_messages_destroy('json');
2959         }
2960
2961         /**
2962          * Test the api_direct_messages_destroy() function with a non-zero ID.
2963          *
2964          * @return void
2965          * @expectedException Friendica\Network\HTTPException\BadRequestException
2966          */
2967         public function testApiDirectMessagesDestroyWithId()
2968         {
2969                 $_REQUEST['id'] = 1;
2970                 api_direct_messages_destroy('json');
2971         }
2972
2973         /**
2974          * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2975          *
2976          * @return void
2977          */
2978         public function testApiDirectMessagesDestroyWithIdAndVerbose()
2979         {
2980                 $_REQUEST['id']                  = 1;
2981                 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2982                 $_GET['friendica_verbose']       = 'true';
2983                 $result                          = api_direct_messages_destroy('json');
2984                 $this->assertEquals(
2985                         [
2986                                 '$result' => [
2987                                         'result'  => 'error',
2988                                         'message' => 'message id not in database'
2989                                 ]
2990                         ],
2991                         $result
2992                 );
2993         }
2994
2995         /**
2996          * Test the api_direct_messages_destroy() function with a non-zero ID.
2997          *
2998          * @return void
2999          */
3000         public function testApiDirectMessagesDestroyWithCorrectId()
3001         {
3002                 $this->markTestIncomplete('We need to add a dataset for this.');
3003         }
3004
3005         /**
3006          * Test the api_direct_messages_box() function.
3007          *
3008          * @return void
3009          */
3010         public function testApiDirectMessagesBoxWithSentbox()
3011         {
3012                 $_REQUEST['page']   = -1;
3013                 $_REQUEST['max_id'] = 10;
3014                 $result             = api_direct_messages_box('json', 'sentbox', 'false');
3015                 $this->assertArrayHasKey('direct_message', $result);
3016         }
3017
3018         /**
3019          * Test the api_direct_messages_box() function.
3020          *
3021          * @return void
3022          */
3023         public function testApiDirectMessagesBoxWithConversation()
3024         {
3025                 $result = api_direct_messages_box('json', 'conversation', 'false');
3026                 $this->assertArrayHasKey('direct_message', $result);
3027         }
3028
3029         /**
3030          * Test the api_direct_messages_box() function.
3031          *
3032          * @return void
3033          */
3034         public function testApiDirectMessagesBoxWithAll()
3035         {
3036                 $result = api_direct_messages_box('json', 'all', 'false');
3037                 $this->assertArrayHasKey('direct_message', $result);
3038         }
3039
3040         /**
3041          * Test the api_direct_messages_box() function.
3042          *
3043          * @return void
3044          */
3045         public function testApiDirectMessagesBoxWithInbox()
3046         {
3047                 $result = api_direct_messages_box('json', 'inbox', 'false');
3048                 $this->assertArrayHasKey('direct_message', $result);
3049         }
3050
3051         /**
3052          * Test the api_direct_messages_box() function.
3053          *
3054          * @return void
3055          */
3056         public function testApiDirectMessagesBoxWithVerbose()
3057         {
3058                 $result = api_direct_messages_box('json', 'sentbox', 'true');
3059                 $this->assertEquals(
3060                         [
3061                                 '$result' => [
3062                                         'result'  => 'error',
3063                                         'message' => 'no mails available'
3064                                 ]
3065                         ],
3066                         $result
3067                 );
3068         }
3069
3070         /**
3071          * Test the api_direct_messages_box() function with a RSS result.
3072          *
3073          * @return void
3074          */
3075         public function testApiDirectMessagesBoxWithRss()
3076         {
3077                 $result = api_direct_messages_box('rss', 'sentbox', 'false');
3078                 $this->assertXml($result, 'direct-messages');
3079         }
3080
3081         /**
3082          * Test the api_direct_messages_box() function without an authenticated user.
3083          *
3084          * @return void
3085          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3086          */
3087         public function testApiDirectMessagesBoxWithUnallowedUser()
3088         {
3089                 $_SESSION['allow_api'] = false;
3090                 $_GET['screen_name']   = $this->selfUser['nick'];
3091                 api_direct_messages_box('json', 'sentbox', 'false');
3092         }
3093
3094         /**
3095          * Test the api_direct_messages_sentbox() function.
3096          *
3097          * @return void
3098          */
3099         public function testApiDirectMessagesSentbox()
3100         {
3101                 $result = api_direct_messages_sentbox('json');
3102                 $this->assertArrayHasKey('direct_message', $result);
3103         }
3104
3105         /**
3106          * Test the api_direct_messages_inbox() function.
3107          *
3108          * @return void
3109          */
3110         public function testApiDirectMessagesInbox()
3111         {
3112                 $result = api_direct_messages_inbox('json');
3113                 $this->assertArrayHasKey('direct_message', $result);
3114         }
3115
3116         /**
3117          * Test the api_direct_messages_all() function.
3118          *
3119          * @return void
3120          */
3121         public function testApiDirectMessagesAll()
3122         {
3123                 $result = api_direct_messages_all('json');
3124                 $this->assertArrayHasKey('direct_message', $result);
3125         }
3126
3127         /**
3128          * Test the api_direct_messages_conversation() function.
3129          *
3130          * @return void
3131          */
3132         public function testApiDirectMessagesConversation()
3133         {
3134                 $result = api_direct_messages_conversation('json');
3135                 $this->assertArrayHasKey('direct_message', $result);
3136         }
3137
3138         /**
3139          * Test the api_oauth_request_token() function.
3140          *
3141          * @return void
3142          */
3143         public function testApiOauthRequestToken()
3144         {
3145                 $this->markTestIncomplete('exit() kills phpunit as well');
3146         }
3147
3148         /**
3149          * Test the api_oauth_access_token() function.
3150          *
3151          * @return void
3152          */
3153         public function testApiOauthAccessToken()
3154         {
3155                 $this->markTestIncomplete('exit() kills phpunit as well');
3156         }
3157
3158         /**
3159          * Test the api_fr_photoalbum_delete() function.
3160          *
3161          * @return void
3162          * @expectedException Friendica\Network\HTTPException\BadRequestException
3163          */
3164         public function testApiFrPhotoalbumDelete()
3165         {
3166                 api_fr_photoalbum_delete('json');
3167         }
3168
3169         /**
3170          * Test the api_fr_photoalbum_delete() function with an album name.
3171          *
3172          * @return void
3173          * @expectedException Friendica\Network\HTTPException\BadRequestException
3174          */
3175         public function testApiFrPhotoalbumDeleteWithAlbum()
3176         {
3177                 $_REQUEST['album'] = 'album_name';
3178                 api_fr_photoalbum_delete('json');
3179         }
3180
3181         /**
3182          * Test the api_fr_photoalbum_delete() function with an album name.
3183          *
3184          * @return void
3185          */
3186         public function testApiFrPhotoalbumDeleteWithValidAlbum()
3187         {
3188                 $this->markTestIncomplete('We need to add a dataset for this.');
3189         }
3190
3191         /**
3192          * Test the api_fr_photoalbum_delete() function.
3193          *
3194          * @return void
3195          * @expectedException Friendica\Network\HTTPException\BadRequestException
3196          */
3197         public function testApiFrPhotoalbumUpdate()
3198         {
3199                 api_fr_photoalbum_update('json');
3200         }
3201
3202         /**
3203          * Test the api_fr_photoalbum_delete() function with an album name.
3204          *
3205          * @return void
3206          * @expectedException Friendica\Network\HTTPException\BadRequestException
3207          */
3208         public function testApiFrPhotoalbumUpdateWithAlbum()
3209         {
3210                 $_REQUEST['album'] = 'album_name';
3211                 api_fr_photoalbum_update('json');
3212         }
3213
3214         /**
3215          * Test the api_fr_photoalbum_delete() function with an album name.
3216          *
3217          * @return void
3218          * @expectedException Friendica\Network\HTTPException\BadRequestException
3219          */
3220         public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3221         {
3222                 $_REQUEST['album']     = 'album_name';
3223                 $_REQUEST['album_new'] = 'album_name';
3224                 api_fr_photoalbum_update('json');
3225         }
3226
3227         /**
3228          * Test the api_fr_photoalbum_update() function without an authenticated user.
3229          *
3230          * @return void
3231          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3232          */
3233         public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3234         {
3235                 $_SESSION['authenticated'] = false;
3236                 api_fr_photoalbum_update('json');
3237         }
3238
3239         /**
3240          * Test the api_fr_photoalbum_delete() function with an album name.
3241          *
3242          * @return void
3243          */
3244         public function testApiFrPhotoalbumUpdateWithValidAlbum()
3245         {
3246                 $this->markTestIncomplete('We need to add a dataset for this.');
3247         }
3248
3249         /**
3250          * Test the api_fr_photos_list() function.
3251          *
3252          * @return void
3253          */
3254         public function testApiFrPhotosList()
3255         {
3256                 $result = api_fr_photos_list('json');
3257                 $this->assertArrayHasKey('photo', $result);
3258         }
3259
3260         /**
3261          * Test the api_fr_photos_list() function without an authenticated user.
3262          *
3263          * @return void
3264          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3265          */
3266         public function testApiFrPhotosListWithoutAuthenticatedUser()
3267         {
3268                 $_SESSION['authenticated'] = false;
3269                 api_fr_photos_list('json');
3270         }
3271
3272         /**
3273          * Test the api_fr_photo_create_update() function.
3274          *
3275          * @return void
3276          * @expectedException Friendica\Network\HTTPException\BadRequestException
3277          */
3278         public function testApiFrPhotoCreateUpdate()
3279         {
3280                 api_fr_photo_create_update('json');
3281         }
3282
3283         /**
3284          * Test the api_fr_photo_create_update() function without an authenticated user.
3285          *
3286          * @return void
3287          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3288          */
3289         public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3290         {
3291                 $_SESSION['authenticated'] = false;
3292                 api_fr_photo_create_update('json');
3293         }
3294
3295         /**
3296          * Test the api_fr_photo_create_update() function with an album name.
3297          *
3298          * @return void
3299          * @expectedException Friendica\Network\HTTPException\BadRequestException
3300          */
3301         public function testApiFrPhotoCreateUpdateWithAlbum()
3302         {
3303                 $_REQUEST['album'] = 'album_name';
3304                 api_fr_photo_create_update('json');
3305         }
3306
3307         /**
3308          * Test the api_fr_photo_create_update() function with the update mode.
3309          *
3310          * @return void
3311          */
3312         public function testApiFrPhotoCreateUpdateWithUpdate()
3313         {
3314                 $this->markTestIncomplete('We need to create a dataset for this');
3315         }
3316
3317         /**
3318          * Test the api_fr_photo_create_update() function with an uploaded file.
3319          *
3320          * @return void
3321          */
3322         public function testApiFrPhotoCreateUpdateWithFile()
3323         {
3324                 $this->markTestIncomplete();
3325         }
3326
3327         /**
3328          * Test the api_fr_photo_delete() function.
3329          *
3330          * @return void
3331          * @expectedException Friendica\Network\HTTPException\BadRequestException
3332          */
3333         public function testApiFrPhotoDelete()
3334         {
3335                 api_fr_photo_delete('json');
3336         }
3337
3338         /**
3339          * Test the api_fr_photo_delete() function without an authenticated user.
3340          *
3341          * @return void
3342          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3343          */
3344         public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3345         {
3346                 $_SESSION['authenticated'] = false;
3347                 api_fr_photo_delete('json');
3348         }
3349
3350         /**
3351          * Test the api_fr_photo_delete() function with a photo ID.
3352          *
3353          * @return void
3354          * @expectedException Friendica\Network\HTTPException\BadRequestException
3355          */
3356         public function testApiFrPhotoDeleteWithPhotoId()
3357         {
3358                 $_REQUEST['photo_id'] = 1;
3359                 api_fr_photo_delete('json');
3360         }
3361
3362         /**
3363          * Test the api_fr_photo_delete() function with a correct photo ID.
3364          *
3365          * @return void
3366          */
3367         public function testApiFrPhotoDeleteWithCorrectPhotoId()
3368         {
3369                 $this->markTestIncomplete('We need to create a dataset for this.');
3370         }
3371
3372         /**
3373          * Test the api_fr_photo_detail() function.
3374          *
3375          * @return void
3376          * @expectedException Friendica\Network\HTTPException\BadRequestException
3377          */
3378         public function testApiFrPhotoDetail()
3379         {
3380                 api_fr_photo_detail('json');
3381         }
3382
3383         /**
3384          * Test the api_fr_photo_detail() function without an authenticated user.
3385          *
3386          * @return void
3387          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3388          */
3389         public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3390         {
3391                 $_SESSION['authenticated'] = false;
3392                 api_fr_photo_detail('json');
3393         }
3394
3395         /**
3396          * Test the api_fr_photo_detail() function with a photo ID.
3397          *
3398          * @return void
3399          * @expectedException Friendica\Network\HTTPException\NotFoundException
3400          */
3401         public function testApiFrPhotoDetailWithPhotoId()
3402         {
3403                 $_REQUEST['photo_id'] = 1;
3404                 api_fr_photo_detail('json');
3405         }
3406
3407         /**
3408          * Test the api_fr_photo_detail() function with a correct photo ID.
3409          *
3410          * @return void
3411          */
3412         public function testApiFrPhotoDetailCorrectPhotoId()
3413         {
3414                 $this->markTestIncomplete('We need to create a dataset for this.');
3415         }
3416
3417         /**
3418          * Test the api_account_update_profile_image() function.
3419          *
3420          * @return void
3421          * @expectedException Friendica\Network\HTTPException\BadRequestException
3422          */
3423         public function testApiAccountUpdateProfileImage()
3424         {
3425                 api_account_update_profile_image('json');
3426         }
3427
3428         /**
3429          * Test the api_account_update_profile_image() function without an authenticated user.
3430          *
3431          * @return void
3432          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3433          */
3434         public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3435         {
3436                 $_SESSION['authenticated'] = false;
3437                 api_account_update_profile_image('json');
3438         }
3439
3440         /**
3441          * Test the api_account_update_profile_image() function with an uploaded file.
3442          *
3443          * @return void
3444          * @expectedException Friendica\Network\HTTPException\BadRequestException
3445          */
3446         public function testApiAccountUpdateProfileImageWithUpload()
3447         {
3448                 $this->markTestIncomplete();
3449         }
3450
3451
3452         /**
3453          * Test the api_account_update_profile() function.
3454          *
3455          * @return void
3456          */
3457         public function testApiAccountUpdateProfile()
3458         {
3459                 $_POST['name']        = 'new_name';
3460                 $_POST['description'] = 'new_description';
3461                 $result               = api_account_update_profile('json');
3462                 // We can't use assertSelfUser() here because the user object is missing some properties.
3463                 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3464                 $this->assertEquals('DFRN', $result['user']['location']);
3465                 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3466                 $this->assertEquals('dfrn', $result['user']['network']);
3467                 $this->assertEquals('new_name', $result['user']['name']);
3468                 $this->assertEquals('new_description', $result['user']['description']);
3469         }
3470
3471         /**
3472          * Test the check_acl_input() function.
3473          *
3474          * @return void
3475          */
3476         public function testCheckAclInput()
3477         {
3478                 $result = check_acl_input('<aclstring>');
3479                 // Where does this result come from?
3480                 $this->assertEquals(1, $result);
3481         }
3482
3483         /**
3484          * Test the check_acl_input() function with an empty ACL string.
3485          *
3486          * @return void
3487          */
3488         public function testCheckAclInputWithEmptyAclString()
3489         {
3490                 $result = check_acl_input(' ');
3491                 $this->assertFalse($result);
3492         }
3493
3494         /**
3495          * Test the save_media_to_database() function.
3496          *
3497          * @return void
3498          */
3499         public function testSaveMediaToDatabase()
3500         {
3501                 $this->markTestIncomplete();
3502         }
3503
3504         /**
3505          * Test the post_photo_item() function.
3506          *
3507          * @return void
3508          */
3509         public function testPostPhotoItem()
3510         {
3511                 $this->markTestIncomplete();
3512         }
3513
3514         /**
3515          * Test the prepare_photo_data() function.
3516          *
3517          * @return void
3518          */
3519         public function testPreparePhotoData()
3520         {
3521                 $this->markTestIncomplete();
3522         }
3523
3524         /**
3525          * Test the api_friendica_remoteauth() function.
3526          *
3527          * @return void
3528          * @expectedException Friendica\Network\HTTPException\BadRequestException
3529          */
3530         public function testApiFriendicaRemoteauth()
3531         {
3532                 api_friendica_remoteauth();
3533         }
3534
3535         /**
3536          * Test the api_friendica_remoteauth() function with an URL.
3537          *
3538          * @return void
3539          * @expectedException Friendica\Network\HTTPException\BadRequestException
3540          */
3541         public function testApiFriendicaRemoteauthWithUrl()
3542         {
3543                 $_GET['url']   = 'url';
3544                 $_GET['c_url'] = 'url';
3545                 api_friendica_remoteauth();
3546         }
3547
3548         /**
3549          * Test the api_friendica_remoteauth() function with a correct URL.
3550          *
3551          * @return void
3552          */
3553         public function testApiFriendicaRemoteauthWithCorrectUrl()
3554         {
3555                 $this->markTestIncomplete("We can't use an assertion here because of App->redirect().");
3556                 $_GET['url']   = 'url';
3557                 $_GET['c_url'] = $this->selfUser['nurl'];
3558                 api_friendica_remoteauth();
3559         }
3560
3561         /**
3562          * Test the api_share_as_retweet() function.
3563          *
3564          * @return void
3565          */
3566         public function testApiShareAsRetweet()
3567         {
3568                 $item   = ['body' => '', 'author-id' => 1, 'owner-id' => 1];
3569                 $result = api_share_as_retweet($item);
3570                 $this->assertFalse($result);
3571         }
3572
3573         /**
3574          * Test the api_share_as_retweet() function with a valid item.
3575          *
3576          * @return void
3577          */
3578         public function testApiShareAsRetweetWithValidItem()
3579         {
3580                 $this->markTestIncomplete();
3581         }
3582
3583         /**
3584          * Test the api_in_reply_to() function.
3585          *
3586          * @return void
3587          */
3588         public function testApiInReplyTo()
3589         {
3590                 $result = api_in_reply_to(['id' => 0, 'parent' => 0, 'uri' => '', 'thr-parent' => '']);
3591                 $this->assertArrayHasKey('status_id', $result);
3592                 $this->assertArrayHasKey('user_id', $result);
3593                 $this->assertArrayHasKey('status_id_str', $result);
3594                 $this->assertArrayHasKey('user_id_str', $result);
3595                 $this->assertArrayHasKey('screen_name', $result);
3596         }
3597
3598         /**
3599          * Test the api_in_reply_to() function with a valid item.
3600          *
3601          * @return void
3602          */
3603         public function testApiInReplyToWithValidItem()
3604         {
3605                 $this->markTestIncomplete();
3606         }
3607
3608         /**
3609          * Test the api_clean_plain_items() function.
3610          *
3611          * @return void
3612          */
3613         public function testApiCleanPlainItems()
3614         {
3615                 $_REQUEST['include_entities'] = 'true';
3616                 $result                       = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3617                 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3618         }
3619
3620         /**
3621          * Test the api_best_nickname() function.
3622          *
3623          * @return void
3624          */
3625         public function testApiBestNickname()
3626         {
3627                 $contacts = [];
3628                 $result   = api_best_nickname($contacts);
3629                 $this->assertNull($result);
3630         }
3631
3632         /**
3633          * Test the api_best_nickname() function with contacts.
3634          *
3635          * @return void
3636          */
3637         public function testApiBestNicknameWithContacts()
3638         {
3639                 $this->markTestIncomplete();
3640         }
3641
3642         /**
3643          * Test the api_friendica_group_show() function.
3644          *
3645          * @return void
3646          */
3647         public function testApiFriendicaGroupShow()
3648         {
3649                 $this->markTestIncomplete();
3650         }
3651
3652         /**
3653          * Test the api_friendica_group_delete() function.
3654          *
3655          * @return void
3656          */
3657         public function testApiFriendicaGroupDelete()
3658         {
3659                 $this->markTestIncomplete();
3660         }
3661
3662         /**
3663          * Test the api_lists_destroy() function.
3664          *
3665          * @return void
3666          */
3667         public function testApiListsDestroy()
3668         {
3669                 $this->markTestIncomplete();
3670         }
3671
3672         /**
3673          * Test the group_create() function.
3674          *
3675          * @return void
3676          */
3677         public function testGroupCreate()
3678         {
3679                 $this->markTestIncomplete();
3680         }
3681
3682         /**
3683          * Test the api_friendica_group_create() function.
3684          *
3685          * @return void
3686          */
3687         public function testApiFriendicaGroupCreate()
3688         {
3689                 $this->markTestIncomplete();
3690         }
3691
3692         /**
3693          * Test the api_lists_create() function.
3694          *
3695          * @return void
3696          */
3697         public function testApiListsCreate()
3698         {
3699                 $this->markTestIncomplete();
3700         }
3701
3702         /**
3703          * Test the api_friendica_group_update() function.
3704          *
3705          * @return void
3706          */
3707         public function testApiFriendicaGroupUpdate()
3708         {
3709                 $this->markTestIncomplete();
3710         }
3711
3712         /**
3713          * Test the api_lists_update() function.
3714          *
3715          * @return void
3716          */
3717         public function testApiListsUpdate()
3718         {
3719                 $this->markTestIncomplete();
3720         }
3721
3722         /**
3723          * Test the api_friendica_activity() function.
3724          *
3725          * @return void
3726          */
3727         public function testApiFriendicaActivity()
3728         {
3729                 $this->markTestIncomplete();
3730         }
3731
3732         /**
3733          * Test the api_friendica_notification() function.
3734          *
3735          * @return void
3736          * @expectedException Friendica\Network\HTTPException\BadRequestException
3737          */
3738         public function testApiFriendicaNotification()
3739         {
3740                 api_friendica_notification('json');
3741         }
3742
3743         /**
3744          * Test the api_friendica_notification() function without an authenticated user.
3745          *
3746          * @return void
3747          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3748          */
3749         public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3750         {
3751                 $_SESSION['authenticated'] = false;
3752                 api_friendica_notification('json');
3753         }
3754
3755         /**
3756          * Test the api_friendica_notification() function with empty result
3757          *
3758          * @return void
3759          */
3760         public function testApiFriendicaNotificationWithEmptyResult()
3761         {
3762                 $this->app->argv = ['api', 'friendica', 'notification'];
3763                 $this->app->argc = count($this->app->argv);
3764                 $_SESSION['uid'] = 41;
3765                 $result          = api_friendica_notification('json');
3766                 $this->assertEquals(['note' => false], $result);
3767         }
3768
3769         /**
3770          * Test the api_friendica_notification() function with an XML result.
3771          *
3772          * @return void
3773          */
3774         public function testApiFriendicaNotificationWithXmlResult()
3775         {
3776                 $this->app->argv = ['api', 'friendica', 'notification'];
3777                 $this->app->argc = count($this->app->argv);
3778                 $result          = api_friendica_notification('xml');
3779                 $dateRel = Temporal::getRelativeDate('2020-01-01 12:12:02');
3780                 $assertXml=<<<XML
3781 <?xml version="1.0"?>
3782 <notes>
3783   <note id="1" hash="" type="8" name="Reply to" url="http://localhost/display/1" photo="http://localhost/" date="2020-01-01 12:12:02" msg="A test reply from an item" uid="42" uri-id="" link="http://localhost/notification/1" iid="4" parent="0" parent-uri-id="" seen="0" verb="" otype="item" name_cache="Reply to" msg_cache="A test reply from an item" timestamp="1577880722" date_rel="{$dateRel}" msg_html="A test reply from an item" msg_plain="A test reply from an item"/>
3784 </notes>
3785 XML;
3786                 $this->assertXmlStringEqualsXmlString($assertXml, $result);
3787         }
3788
3789         /**
3790          * Test the api_friendica_notification() function with an JSON result.
3791          *
3792          * @return void
3793          */
3794         public function testApiFriendicaNotificationWithJsonResult()
3795         {
3796                 $this->app->argv = ['api', 'friendica', 'notification'];
3797                 $this->app->argc = count($this->app->argv);
3798                 $result          = json_encode(api_friendica_notification('json'));
3799                 $this->assertJson($result);
3800         }
3801
3802         /**
3803          * Test the api_friendica_notification_seen() function.
3804          *
3805          * @return void
3806          */
3807         public function testApiFriendicaNotificationSeen()
3808         {
3809                 $this->markTestIncomplete();
3810         }
3811
3812         /**
3813          * Test the api_friendica_direct_messages_setseen() function.
3814          *
3815          * @return void
3816          */
3817         public function testApiFriendicaDirectMessagesSetseen()
3818         {
3819                 $this->markTestIncomplete();
3820         }
3821
3822         /**
3823          * Test the api_friendica_direct_messages_search() function.
3824          *
3825          * @return void
3826          */
3827         public function testApiFriendicaDirectMessagesSearch()
3828         {
3829                 $this->markTestIncomplete();
3830         }
3831
3832         /**
3833          * Test the api_saved_searches_list() function.
3834          *
3835          * @return void
3836          */
3837         public function testApiSavedSearchesList()
3838         {
3839                 $result = api_saved_searches_list('json');
3840                 $this->assertEquals(1, $result['terms'][0]['id']);
3841                 $this->assertEquals(1, $result['terms'][0]['id_str']);
3842                 $this->assertEquals('Saved search', $result['terms'][0]['name']);
3843                 $this->assertEquals('Saved search', $result['terms'][0]['query']);
3844         }
3845 }