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