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