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