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