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