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