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