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