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