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