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