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