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