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