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