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