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