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