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