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