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