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