]> git.mxchange.org Git - friendica.git/blob - tests/ApiTest.php
Fix some undefined variable/index notices 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"?>'.PHP_EOL.
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"?>'.PHP_EOL.
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"?>'.PHP_EOL.
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">'.PHP_EOL.
578                         '  <error>error_message</error>'.PHP_EOL.
579                         '  <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
580                         '  <request/>'.PHP_EOL.
581                         '</status>'.PHP_EOL,
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"?>'.PHP_EOL.
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">'.PHP_EOL.
598                         '  <error>error_message</error>'.PHP_EOL.
599                         '  <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
600                         '  <request/>'.PHP_EOL.
601                         '</status>'.PHP_EOL,
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"?>'.PHP_EOL.
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">'.PHP_EOL.
618                         '  <error>error_message</error>'.PHP_EOL.
619                         '  <code>200 Friendica\Network\HTTP</code>'.PHP_EOL.
620                         '  <request/>'.PHP_EOL.
621                         '</status>'.PHP_EOL,
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_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         /**
858          * Test the api_item_get_user() function.
859          * @return void
860          * @runInSeparateProcess
861          */
862         public function testApiItemGetUser()
863         {
864                 $users = api_item_get_user($this->app, []);
865                 $this->assertSelfUser($users[0]);
866         }
867
868         /**
869          * Test the api_item_get_user() function with a different item parent.
870          * @return void
871          */
872         public function testApiItemGetUserWithDifferentParent()
873         {
874                 $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']);
875                 $this->assertSelfUser($users[0]);
876                 $this->assertEquals($users[0], $users[1]);
877         }
878
879         /**
880          * Test the api_walk_recursive() function.
881          * @return void
882          */
883         public function testApiWalkRecursive()
884         {
885                 $array = ['item1'];
886                 $this->assertEquals(
887                         $array,
888                         api_walk_recursive(
889                                 $array,
890                                 function () {
891                                         // Should we test this with a callback that actually does something?
892                                         return true;
893                                 }
894                         )
895                 );
896         }
897
898         /**
899          * Test the api_walk_recursive() function with an array.
900          * @return void
901          */
902         public function testApiWalkRecursiveWithArray()
903         {
904                 $array = [['item1'], ['item2']];
905                 $this->assertEquals(
906                         $array,
907                         api_walk_recursive(
908                                 $array,
909                                 function () {
910                                         // Should we test this with a callback that actually does something?
911                                         return true;
912                                 }
913                         )
914                 );
915         }
916
917         /**
918          * Test the api_reformat_xml() function.
919          * @return void
920          */
921         public function testApiReformatXml()
922         {
923                 $item = true;
924                 $key = '';
925                 $this->assertTrue(api_reformat_xml($item, $key));
926                 $this->assertEquals('true', $item);
927         }
928
929         /**
930          * Test the api_reformat_xml() function with a statusnet_api key.
931          * @return void
932          */
933         public function testApiReformatXmlWithStatusnetKey()
934         {
935                 $item = '';
936                 $key = 'statusnet_api';
937                 $this->assertTrue(api_reformat_xml($item, $key));
938                 $this->assertEquals('statusnet:api', $key);
939         }
940
941         /**
942          * Test the api_reformat_xml() function with a friendica_api key.
943          * @return void
944          */
945         public function testApiReformatXmlWithFriendicaKey()
946         {
947                 $item = '';
948                 $key = 'friendica_api';
949                 $this->assertTrue(api_reformat_xml($item, $key));
950                 $this->assertEquals('friendica:api', $key);
951         }
952
953         /**
954          * Test the api_create_xml() function.
955          * @return void
956          */
957         public function testApiCreateXml()
958         {
959                 $this->assertEquals(
960                         '<?xml version="1.0"?>'.PHP_EOL.
961                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
962                                 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
963                                 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
964                                 '  <data>some_data</data>'.PHP_EOL.
965                         '</root_element>'.PHP_EOL,
966                         api_create_xml(['data' => ['some_data']], 'root_element')
967                 );
968         }
969
970         /**
971          * Test the api_create_xml() function without any XML namespace.
972          * @return void
973          */
974         public function testApiCreateXmlWithoutNamespaces()
975         {
976                 $this->assertEquals(
977                         '<?xml version="1.0"?>'.PHP_EOL.
978                         '<ok>'.PHP_EOL.
979                                 '  <data>some_data</data>'.PHP_EOL.
980                         '</ok>'.PHP_EOL,
981                         api_create_xml(['data' => ['some_data']], 'ok')
982                 );
983         }
984
985         /**
986          * Test the api_format_data() function.
987          * @return void
988          */
989         public function testApiFormatData()
990         {
991                 $data = ['some_data'];
992                 $this->assertEquals($data, api_format_data('root_element', 'json', $data));
993         }
994
995         /**
996          * Test the api_format_data() function with an XML result.
997          * @return void
998          */
999         public function testApiFormatDataWithXml()
1000         {
1001                 $this->assertEquals(
1002                         '<?xml version="1.0"?>'.PHP_EOL.
1003                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" '.
1004                                 'xmlns:friendica="http://friendi.ca/schema/api/1/" '.
1005                                 'xmlns:georss="http://www.georss.org/georss">'.PHP_EOL.
1006                                 '  <data>some_data</data>'.PHP_EOL.
1007                         '</root_element>'.PHP_EOL,
1008                         api_format_data('root_element', 'xml', ['data' => ['some_data']])
1009                 );
1010         }
1011
1012         /**
1013          * Test the api_account_verify_credentials() function.
1014          * @return void
1015          */
1016         public function testApiAccountVerifyCredentials()
1017         {
1018                 $this->assertArrayHasKey('user', api_account_verify_credentials('json'));
1019         }
1020
1021         /**
1022          * Test the api_account_verify_credentials() function without an authenticated user.
1023          * @return void
1024          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1025          */
1026         public function testApiAccountVerifyCredentialsWithoutAuthenticatedUser()
1027         {
1028                 $_SESSION['authenticated'] = false;
1029                 api_account_verify_credentials('json');
1030         }
1031
1032         /**
1033          * Test the requestdata() function.
1034          * @return void
1035          */
1036         public function testRequestdata()
1037         {
1038                 $this->assertNull(requestdata('variable_name'));
1039         }
1040
1041         /**
1042          * Test the requestdata() function with a POST parameter.
1043          * @return void
1044          */
1045         public function testRequestdataWithPost()
1046         {
1047                 $_POST['variable_name'] = 'variable_value';
1048                 $this->assertEquals('variable_value', requestdata('variable_name'));
1049         }
1050
1051         /**
1052          * Test the requestdata() function with a GET parameter.
1053          * @return void
1054          */
1055         public function testRequestdataWithGet()
1056         {
1057                 $_GET['variable_name'] = 'variable_value';
1058                 $this->assertEquals('variable_value', requestdata('variable_name'));
1059         }
1060
1061         /**
1062          * Test the api_statuses_mediap() function.
1063          * @return void
1064          */
1065         public function testApiStatusesMediap()
1066         {
1067                 $this->app->argc = 2;
1068
1069                 $_FILES = [
1070                         'media' => [
1071                                 'id' => 666,
1072                                 'size' => 666,
1073                                 'width' => 666,
1074                                 'height' => 666,
1075                                 'tmp_name' => $this->getTempImage(),
1076                                 'name' => 'spacer.png',
1077                                 'type' => 'image/png'
1078                         ]
1079                 ];
1080                 $_GET['status'] = '<b>Status content</b>';
1081
1082                 $result = api_statuses_mediap('json');
1083                 $this->assertStatus($result['status']);
1084         }
1085
1086         /**
1087          * Test the api_statuses_mediap() function without an authenticated user.
1088          * @return void
1089          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1090          */
1091         public function testApiStatusesMediapWithoutAuthenticatedUser()
1092         {
1093                 $_SESSION['authenticated'] = false;
1094                 api_statuses_mediap('json');
1095         }
1096
1097         /**
1098          * Test the api_statuses_update() function.
1099          * @return void
1100          */
1101         public function testApiStatusesUpdate()
1102         {
1103                 $_GET['status'] = 'Status content';
1104                 $_GET['in_reply_to_status_id'] = -1;
1105                 $_GET['lat'] = 48;
1106                 $_GET['long'] = 7;
1107                 $_FILES = [
1108                         'media' => [
1109                                 'id' => 666,
1110                                 'size' => 666,
1111                                 'width' => 666,
1112                                 'height' => 666,
1113                                 'tmp_name' => $this->getTempImage(),
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                                 'type' => 'image/png'
1222                         ]
1223                 ];
1224                 $app = get_app();
1225                 $app->argc = 2;
1226
1227                 $result = api_media_upload();
1228                 $this->assertEquals('image/png', $result['media']['image']['image_type']);
1229                 $this->assertEquals(1, $result['media']['image']['w']);
1230                 $this->assertEquals(1, $result['media']['image']['h']);
1231         }
1232
1233         /**
1234          * Test the api_status_show() function.
1235          * @return void
1236          */
1237         public function testApiStatusShow()
1238         {
1239                 $result = api_status_show('json');
1240                 $this->assertStatus($result['status']);
1241         }
1242
1243         /**
1244          * Test the api_status_show() function with an XML result.
1245          * @return void
1246          */
1247         public function testApiStatusShowWithXml()
1248         {
1249                 $result = api_status_show('xml');
1250                 $this->assertXml($result, 'statuses');
1251         }
1252
1253         /**
1254          * Test the api_status_show() function with a raw result.
1255          * @return void
1256          */
1257         public function testApiStatusShowWithRaw()
1258         {
1259                 $this->assertStatus(api_status_show('raw'));
1260         }
1261
1262         /**
1263          * Test the api_users_show() function.
1264          * @return void
1265          */
1266         public function testApiUsersShow()
1267         {
1268                 $result = api_users_show('json');
1269                 // We can't use assertSelfUser() here because the user object is missing some properties.
1270                 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
1271                 $this->assertEquals('Friendica', $result['user']['location']);
1272                 $this->assertEquals($this->selfUser['name'], $result['user']['name']);
1273                 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
1274                 $this->assertEquals('dfrn', $result['user']['network']);
1275                 $this->assertTrue($result['user']['verified']);
1276         }
1277
1278         /**
1279          * Test the api_users_show() function with an XML result.
1280          * @return void
1281          */
1282         public function testApiUsersShowWithXml()
1283         {
1284                 $result = api_users_show('xml');
1285                 $this->assertXml($result, 'statuses');
1286         }
1287
1288         /**
1289          * Test the api_users_search() function.
1290          * @return void
1291          */
1292         public function testApiUsersSearch()
1293         {
1294                 $_GET['q'] = 'othercontact';
1295                 $result = api_users_search('json');
1296                 $this->assertOtherUser($result['users'][0]);
1297         }
1298
1299         /**
1300          * Test the api_users_search() function with an XML result.
1301          * @return void
1302          */
1303         public function testApiUsersSearchWithXml()
1304         {
1305                 $_GET['q'] = 'othercontact';
1306                 $result = api_users_search('xml');
1307                 $this->assertXml($result, 'users');
1308         }
1309
1310         /**
1311          * Test the api_users_search() function without a GET q parameter.
1312          * @return void
1313          * @expectedException Friendica\Network\HTTPException\BadRequestException
1314          */
1315         public function testApiUsersSearchWithoutQuery()
1316         {
1317                 api_users_search('json');
1318         }
1319
1320         /**
1321          * Test the api_users_lookup() function.
1322          * @return void
1323          * @expectedException Friendica\Network\HTTPException\NotFoundException
1324          */
1325         public function testApiUsersLookup()
1326         {
1327                 api_users_lookup('json');
1328         }
1329
1330         /**
1331          * Test the api_users_lookup() function with an user ID.
1332          * @return void
1333          */
1334         public function testApiUsersLookupWithUserId()
1335         {
1336                 $_REQUEST['user_id'] = $this->otherUser['id'];
1337                 $result = api_users_lookup('json');
1338                 $this->assertOtherUser($result['users'][0]);
1339         }
1340
1341         /**
1342          * Test the api_search() function.
1343          * @return void
1344          */
1345         public function testApiSearch()
1346         {
1347                 $_REQUEST['q'] = 'reply';
1348                 $_REQUEST['max_id'] = 10;
1349                 $result = api_search('json');
1350                 foreach ($result['status'] as $status) {
1351                         $this->assertStatus($status);
1352                         $this->assertContains('reply', $status['text'], null, true);
1353                 }
1354         }
1355
1356         /**
1357          * Test the api_search() function a count parameter.
1358          * @return void
1359          */
1360         public function testApiSearchWithCount()
1361         {
1362                 $_REQUEST['q'] = 'reply';
1363                 $_REQUEST['count'] = 20;
1364                 $result = api_search('json');
1365                 foreach ($result['status'] as $status) {
1366                         $this->assertStatus($status);
1367                         $this->assertContains('reply', $status['text'], null, true);
1368                 }
1369         }
1370
1371         /**
1372          * Test the api_search() function with an rpp parameter.
1373          * @return void
1374          */
1375         public function testApiSearchWithRpp()
1376         {
1377                 $_REQUEST['q'] = 'reply';
1378                 $_REQUEST['rpp'] = 20;
1379                 $result = api_search('json');
1380                 foreach ($result['status'] as $status) {
1381                         $this->assertStatus($status);
1382                         $this->assertContains('reply', $status['text'], null, true);
1383                 }
1384         }
1385
1386
1387         /**
1388          * Test the api_search() function without an authenticated user.
1389          * @return void
1390          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1391          */
1392         public function testApiSearchWithUnallowedUser()
1393         {
1394                 $_SESSION['allow_api'] = false;
1395                 $_GET['screen_name'] = $this->selfUser['nick'];
1396                 api_search('json');
1397         }
1398
1399         /**
1400          * Test the api_search() function without any GET query parameter.
1401          * @return void
1402          * @expectedException Friendica\Network\HTTPException\BadRequestException
1403          */
1404         public function testApiSearchWithoutQuery()
1405         {
1406                 api_search('json');
1407         }
1408
1409         /**
1410          * Test the api_statuses_home_timeline() function.
1411          * @return void
1412          */
1413         public function testApiStatusesHomeTimeline()
1414         {
1415                 $_REQUEST['max_id'] = 10;
1416                 $_REQUEST['exclude_replies'] = true;
1417                 $_REQUEST['conversation_id'] = 1;
1418                 $result = api_statuses_home_timeline('json');
1419                 $this->assertNotEmpty($result['status']);
1420                 foreach ($result['status'] as $status) {
1421                         $this->assertStatus($status);
1422                 }
1423         }
1424
1425         /**
1426          * Test the api_statuses_home_timeline() function with a negative page parameter.
1427          * @return void
1428          */
1429         public function testApiStatusesHomeTimelineWithNegativePage()
1430         {
1431                 $_REQUEST['page'] = -2;
1432                 $result = api_statuses_home_timeline('json');
1433                 $this->assertNotEmpty($result['status']);
1434                 foreach ($result['status'] as $status) {
1435                         $this->assertStatus($status);
1436                 }
1437         }
1438
1439         /**
1440          * Test the api_statuses_home_timeline() with an unallowed user.
1441          * @return void
1442          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1443          */
1444         public function testApiStatusesHomeTimelineWithUnallowedUser()
1445         {
1446                 $_SESSION['allow_api'] = false;
1447                 $_GET['screen_name'] = $this->selfUser['nick'];
1448                 api_statuses_home_timeline('json');
1449         }
1450
1451         /**
1452          * Test the api_statuses_home_timeline() function with an RSS result.
1453          * @return void
1454          */
1455         public function testApiStatusesHomeTimelineWithRss()
1456         {
1457                 $result = api_statuses_home_timeline('rss');
1458                 $this->assertXml($result, 'statuses');
1459         }
1460
1461         /**
1462          * Test the api_statuses_public_timeline() function.
1463          * @return void
1464          */
1465         public function testApiStatusesPublicTimeline()
1466         {
1467                 $_REQUEST['max_id'] = 10;
1468                 $_REQUEST['conversation_id'] = 1;
1469                 $result = api_statuses_public_timeline('json');
1470                 $this->assertNotEmpty($result['status']);
1471                 foreach ($result['status'] as $status) {
1472                         $this->assertStatus($status);
1473                 }
1474         }
1475
1476         /**
1477          * Test the api_statuses_public_timeline() function with the exclude_replies parameter.
1478          * @return void
1479          */
1480         public function testApiStatusesPublicTimelineWithExcludeReplies()
1481         {
1482                 $_REQUEST['max_id'] = 10;
1483                 $_REQUEST['exclude_replies'] = true;
1484                 $result = api_statuses_public_timeline('json');
1485                 $this->assertNotEmpty($result['status']);
1486                 foreach ($result['status'] as $status) {
1487                         $this->assertStatus($status);
1488                 }
1489         }
1490
1491         /**
1492          * Test the api_statuses_public_timeline() function with a negative page parameter.
1493          * @return void
1494          */
1495         public function testApiStatusesPublicTimelineWithNegativePage()
1496         {
1497                 $_REQUEST['page'] = -2;
1498                 $result = api_statuses_public_timeline('json');
1499                 $this->assertNotEmpty($result['status']);
1500                 foreach ($result['status'] as $status) {
1501                         $this->assertStatus($status);
1502                 }
1503         }
1504
1505         /**
1506          * Test the api_statuses_public_timeline() function with an unallowed user.
1507          * @return void
1508          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1509          */
1510         public function testApiStatusesPublicTimelineWithUnallowedUser()
1511         {
1512                 $_SESSION['allow_api'] = false;
1513                 $_GET['screen_name'] = $this->selfUser['nick'];
1514                 api_statuses_public_timeline('json');
1515         }
1516
1517         /**
1518          * Test the api_statuses_public_timeline() function with an RSS result.
1519          * @return void
1520          */
1521         public function testApiStatusesPublicTimelineWithRss()
1522         {
1523                 $result = api_statuses_public_timeline('rss');
1524                 $this->assertXml($result, 'statuses');
1525         }
1526
1527         /**
1528          * Test the api_statuses_networkpublic_timeline() function.
1529          * @return void
1530          */
1531         public function testApiStatusesNetworkpublicTimeline()
1532         {
1533                 $_REQUEST['max_id'] = 10;
1534                 $result = api_statuses_networkpublic_timeline('json');
1535                 $this->assertNotEmpty($result['status']);
1536                 foreach ($result['status'] as $status) {
1537                         $this->assertStatus($status);
1538                 }
1539         }
1540
1541         /**
1542          * Test the api_statuses_networkpublic_timeline() function with a negative page parameter.
1543          * @return void
1544          */
1545         public function testApiStatusesNetworkpublicTimelineWithNegativePage()
1546         {
1547                 $_REQUEST['page'] = -2;
1548                 $result = api_statuses_networkpublic_timeline('json');
1549                 $this->assertNotEmpty($result['status']);
1550                 foreach ($result['status'] as $status) {
1551                         $this->assertStatus($status);
1552                 }
1553         }
1554
1555         /**
1556          * Test the api_statuses_networkpublic_timeline() function with an unallowed user.
1557          * @return void
1558          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1559          */
1560         public function testApiStatusesNetworkpublicTimelineWithUnallowedUser()
1561         {
1562                 $_SESSION['allow_api'] = false;
1563                 $_GET['screen_name'] = $this->selfUser['nick'];
1564                 api_statuses_networkpublic_timeline('json');
1565         }
1566
1567         /**
1568          * Test the api_statuses_networkpublic_timeline() function with an RSS result.
1569          * @return void
1570          */
1571         public function testApiStatusesNetworkpublicTimelineWithRss()
1572         {
1573                 $result = api_statuses_networkpublic_timeline('rss');
1574                 $this->assertXml($result, 'statuses');
1575         }
1576
1577         /**
1578          * Test the api_statuses_show() function.
1579          * @return void
1580          * @expectedException Friendica\Network\HTTPException\BadRequestException
1581          */
1582         public function testApiStatusesShow()
1583         {
1584                 api_statuses_show('json');
1585         }
1586
1587         /**
1588          * Test the api_statuses_show() function with an ID.
1589          * @return void
1590          */
1591         public function testApiStatusesShowWithId()
1592         {
1593                 $this->app->argv[3] = 1;
1594                 $result = api_statuses_show('json');
1595                 $this->assertStatus($result['status']);
1596         }
1597
1598         /**
1599          * Test the api_statuses_show() function with the conversation parameter.
1600          * @return void
1601          */
1602         public function testApiStatusesShowWithConversation()
1603         {
1604                 $this->app->argv[3] = 1;
1605                 $_REQUEST['conversation'] = 1;
1606                 $result = api_statuses_show('json');
1607                 $this->assertNotEmpty($result['status']);
1608                 foreach ($result['status'] as $status) {
1609                         $this->assertStatus($status);
1610                 }
1611         }
1612
1613         /**
1614          * Test the api_statuses_show() function with an unallowed user.
1615          * @return void
1616          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1617          */
1618         public function testApiStatusesShowWithUnallowedUser()
1619         {
1620                 $_SESSION['allow_api'] = false;
1621                 $_GET['screen_name'] = $this->selfUser['nick'];
1622                 api_statuses_show('json');
1623         }
1624
1625         /**
1626          * Test the api_conversation_show() function.
1627          * @return void
1628          * @expectedException Friendica\Network\HTTPException\BadRequestException
1629          */
1630         public function testApiConversationShow()
1631         {
1632                 api_conversation_show('json');
1633         }
1634
1635         /**
1636          * Test the api_conversation_show() function with an ID.
1637          * @return void
1638          */
1639         public function testApiConversationShowWithId()
1640         {
1641                 $this->app->argv[3] = 1;
1642                 $_REQUEST['max_id'] = 10;
1643                 $_REQUEST['page'] = -2;
1644                 $result = api_conversation_show('json');
1645                 $this->assertNotEmpty($result['status']);
1646                 foreach ($result['status'] as $status) {
1647                         $this->assertStatus($status);
1648                 }
1649         }
1650
1651         /**
1652          * Test the api_conversation_show() function with an unallowed user.
1653          * @return void
1654          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1655          */
1656         public function testApiConversationShowWithUnallowedUser()
1657         {
1658                 $_SESSION['allow_api'] = false;
1659                 $_GET['screen_name'] = $this->selfUser['nick'];
1660                 api_conversation_show('json');
1661         }
1662
1663         /**
1664          * Test the api_statuses_repeat() function.
1665          * @return void
1666          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1667          */
1668         public function testApiStatusesRepeat()
1669         {
1670                 api_statuses_repeat('json');
1671         }
1672
1673         /**
1674          * Test the api_statuses_repeat() function without an authenticated user.
1675          * @return void
1676          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1677          */
1678         public function testApiStatusesRepeatWithoutAuthenticatedUser()
1679         {
1680                 $_SESSION['authenticated'] = false;
1681                 api_statuses_repeat('json');
1682         }
1683
1684         /**
1685          * Test the api_statuses_repeat() function with an ID.
1686          * @return void
1687          */
1688         public function testApiStatusesRepeatWithId()
1689         {
1690                 $this->app->argv[3] = 1;
1691                 $result = api_statuses_repeat('json');
1692                 $this->assertStatus($result['status']);
1693
1694                 // Also test with a shared status
1695                 $this->app->argv[3] = 5;
1696                 $result = api_statuses_repeat('json');
1697                 $this->assertStatus($result['status']);
1698         }
1699
1700         /**
1701          * Test the api_statuses_destroy() function.
1702          * @return void
1703          * @expectedException Friendica\Network\HTTPException\BadRequestException
1704          */
1705         public function testApiStatusesDestroy()
1706         {
1707                 api_statuses_destroy('json');
1708         }
1709
1710         /**
1711          * Test the api_statuses_destroy() function without an authenticated user.
1712          * @return void
1713          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1714          */
1715         public function testApiStatusesDestroyWithoutAuthenticatedUser()
1716         {
1717                 $_SESSION['authenticated'] = false;
1718                 api_statuses_destroy('json');
1719         }
1720
1721         /**
1722          * Test the api_statuses_destroy() function with an ID.
1723          * @return void
1724          */
1725         public function testApiStatusesDestroyWithId()
1726         {
1727                 $this->app->argv[3] = 1;
1728                 $result = api_statuses_destroy('json');
1729                 $this->assertStatus($result['status']);
1730         }
1731
1732         /**
1733          * Test the api_statuses_mentions() function.
1734          * @return void
1735          */
1736         public function testApiStatusesMentions()
1737         {
1738                 $this->app->user = ['nickname' => $this->selfUser['nick']];
1739                 $_REQUEST['max_id'] = 10;
1740                 $result = api_statuses_mentions('json');
1741                 $this->assertEmpty($result['status']);
1742                 // We should test with mentions in the database.
1743         }
1744
1745         /**
1746          * Test the api_statuses_mentions() function with a negative page parameter.
1747          * @return void
1748          */
1749         public function testApiStatusesMentionsWithNegativePage()
1750         {
1751                 $_REQUEST['page'] = -2;
1752                 $result = api_statuses_mentions('json');
1753                 $this->assertEmpty($result['status']);
1754         }
1755
1756         /**
1757          * Test the api_statuses_mentions() function with an unallowed user.
1758          * @return void
1759          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1760          */
1761         public function testApiStatusesMentionsWithUnallowedUser()
1762         {
1763                 $_SESSION['allow_api'] = false;
1764                 $_GET['screen_name'] = $this->selfUser['nick'];
1765                 api_statuses_mentions('json');
1766         }
1767
1768         /**
1769          * Test the api_statuses_mentions() function with an RSS result.
1770          * @return void
1771          */
1772         public function testApiStatusesMentionsWithRss()
1773         {
1774                 $result = api_statuses_mentions('rss');
1775                 $this->assertXml($result, 'statuses');
1776         }
1777
1778         /**
1779          * Test the api_statuses_user_timeline() function.
1780          * @return void
1781          */
1782         public function testApiStatusesUserTimeline()
1783         {
1784                 $_REQUEST['max_id'] = 10;
1785                 $_REQUEST['exclude_replies'] = true;
1786                 $_REQUEST['conversation_id'] = 1;
1787                 $result = api_statuses_user_timeline('json');
1788                 $this->assertNotEmpty($result['status']);
1789                 foreach ($result['status'] as $status) {
1790                         $this->assertStatus($status);
1791                 }
1792         }
1793
1794         /**
1795          * Test the api_statuses_user_timeline() function with a negative page parameter.
1796          * @return void
1797          */
1798         public function testApiStatusesUserTimelineWithNegativePage()
1799         {
1800                 $_REQUEST['page'] = -2;
1801                 $result = api_statuses_user_timeline('json');
1802                 $this->assertNotEmpty($result['status']);
1803                 foreach ($result['status'] as $status) {
1804                         $this->assertStatus($status);
1805                 }
1806         }
1807
1808         /**
1809          * Test the api_statuses_user_timeline() function with an RSS result.
1810          * @return void
1811          */
1812         public function testApiStatusesUserTimelineWithRss()
1813         {
1814                 $result = api_statuses_user_timeline('rss');
1815                 $this->assertXml($result, 'statuses');
1816         }
1817
1818         /**
1819          * Test the api_statuses_user_timeline() function with an unallowed user.
1820          * @return void
1821          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1822          */
1823         public function testApiStatusesUserTimelineWithUnallowedUser()
1824         {
1825                 $_SESSION['allow_api'] = false;
1826                 $_GET['screen_name'] = $this->selfUser['nick'];
1827                 api_statuses_user_timeline('json');
1828         }
1829
1830         /**
1831          * Test the api_favorites_create_destroy() function.
1832          * @return void
1833          * @expectedException Friendica\Network\HTTPException\BadRequestException
1834          */
1835         public function testApiFavoritesCreateDestroy()
1836         {
1837                 api_favorites_create_destroy('json');
1838         }
1839
1840         /**
1841          * Test the api_favorites_create_destroy() function with an invalid ID.
1842          * @return void
1843          * @expectedException Friendica\Network\HTTPException\BadRequestException
1844          */
1845         public function testApiFavoritesCreateDestroyWithInvalidId()
1846         {
1847                 // This triggers a very specific condition ($action_argv_id + 2)
1848                 $this->app->argv[1] = '1.1';
1849                 $this->app->argc = 5;
1850                 api_favorites_create_destroy('json');
1851         }
1852
1853         /**
1854          * Test the api_favorites_create_destroy() function with an invalid action.
1855          * @return void
1856          * @expectedException Friendica\Network\HTTPException\BadRequestException
1857          */
1858         public function testApiFavoritesCreateDestroyWithInvalidAction()
1859         {
1860                 $this->app->argv[1] = '1.1';
1861                 $this->app->argc = 10;
1862                 $_REQUEST['id'] = 1;
1863                 api_favorites_create_destroy('json');
1864         }
1865
1866         /**
1867          * Test the api_favorites_create_destroy() function with the create action.
1868          * @return void
1869          */
1870         public function testApiFavoritesCreateDestroyWithCreateAction()
1871         {
1872                 $this->app->argv[1] = '1.1';
1873                 $this->app->argv[3] = 'create';
1874                 $this->app->argc = 10;
1875                 $_REQUEST['id'] = 3;
1876                 $result = api_favorites_create_destroy('json');
1877                 $this->assertStatus($result['status']);
1878         }
1879
1880         /**
1881          * Test the api_favorites_create_destroy() function with the create action and an RSS result.
1882          * @return void
1883          */
1884         public function testApiFavoritesCreateDestroyWithCreateActionAndRss()
1885         {
1886                 $this->app->argv[1] = '1.1';
1887                 $this->app->argv[3] = 'create';
1888                 $this->app->argc = 10;
1889                 $_REQUEST['id'] = 3;
1890                 $result = api_favorites_create_destroy('rss');
1891                 $this->assertXml($result, 'status');
1892         }
1893
1894         /**
1895          * Test the api_favorites_create_destroy() function with the destroy action.
1896          * @return void
1897          */
1898         public function testApiFavoritesCreateDestroyWithDestroyAction()
1899         {
1900                 $this->app->argv[1] = '1.1';
1901                 $this->app->argv[3] = 'destroy';
1902                 $this->app->argc = 10;
1903                 $_REQUEST['id'] = 3;
1904                 $result = api_favorites_create_destroy('json');
1905                 $this->assertStatus($result['status']);
1906         }
1907
1908         /**
1909          * Test the api_favorites_create_destroy() function without an authenticated user.
1910          * @return void
1911          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1912          */
1913         public function testApiFavoritesCreateDestroyWithoutAuthenticatedUser()
1914         {
1915                 $_SESSION['authenticated'] = false;
1916                 api_favorites_create_destroy('json');
1917         }
1918
1919         /**
1920          * Test the api_favorites() function.
1921          * @return void
1922          */
1923         public function testApiFavorites()
1924         {
1925                 $_REQUEST['page'] = -1;
1926                 $_REQUEST['max_id'] = 10;
1927                 $result = api_favorites('json');
1928                 foreach ($result['status'] as $status) {
1929                         $this->assertStatus($status);
1930                 }
1931         }
1932
1933         /**
1934          * Test the api_favorites() function with an RSS result.
1935          * @return void
1936          */
1937         public function testApiFavoritesWithRss()
1938         {
1939                 $result = api_favorites('rss');
1940                 $this->assertXml($result, 'statuses');
1941         }
1942
1943         /**
1944          * Test the api_favorites() function with an unallowed user.
1945          * @return void
1946          * @expectedException Friendica\Network\HTTPException\ForbiddenException
1947          */
1948         public function testApiFavoritesWithUnallowedUser()
1949         {
1950                 $_SESSION['allow_api'] = false;
1951                 $_GET['screen_name'] = $this->selfUser['nick'];
1952                 api_favorites('json');
1953         }
1954
1955         /**
1956          * Test the api_format_messages() function.
1957          * @return void
1958          */
1959         public function testApiFormatMessages()
1960         {
1961                 $result = api_format_messages(
1962                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1963                         ['id' => 2, 'screen_name' => 'recipient_name'],
1964                         ['id' => 3, 'screen_name' => 'sender_name']
1965                 );
1966                 $this->assertEquals('item_title'.PHP_EOL.'item_body', $result['text']);
1967                 $this->assertEquals(1, $result['id']);
1968                 $this->assertEquals(2, $result['recipient_id']);
1969                 $this->assertEquals(3, $result['sender_id']);
1970                 $this->assertEquals('recipient_name', $result['recipient_screen_name']);
1971                 $this->assertEquals('sender_name', $result['sender_screen_name']);
1972         }
1973
1974         /**
1975          * Test the api_format_messages() function with HTML.
1976          * @return void
1977          */
1978         public function testApiFormatMessagesWithHtmlText()
1979         {
1980                 $_GET['getText'] = 'html';
1981                 $result = api_format_messages(
1982                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1983                         ['id' => 2, 'screen_name' => 'recipient_name'],
1984                         ['id' => 3, 'screen_name' => 'sender_name']
1985                 );
1986                 $this->assertEquals('item_title', $result['title']);
1987                 $this->assertEquals('<strong>item_body</strong>', $result['text']);
1988         }
1989
1990         /**
1991          * Test the api_format_messages() function with plain text.
1992          * @return void
1993          */
1994         public function testApiFormatMessagesWithPlainText()
1995         {
1996                 $_GET['getText'] = 'plain';
1997                 $result = api_format_messages(
1998                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
1999                         ['id' => 2, 'screen_name' => 'recipient_name'],
2000                         ['id' => 3, 'screen_name' => 'sender_name']
2001                 );
2002                 $this->assertEquals('item_title', $result['title']);
2003                 $this->assertEquals('item_body', $result['text']);
2004         }
2005
2006         /**
2007          * Test the api_format_messages() function with the getUserObjects GET parameter set to false.
2008          * @return void
2009          */
2010         public function testApiFormatMessagesWithoutUserObjects()
2011         {
2012                 $_GET['getUserObjects'] = 'false';
2013                 $result = api_format_messages(
2014                         ['id' => 1, 'title' => 'item_title', 'body' => '[b]item_body[/b]'],
2015                         ['id' => 2, 'screen_name' => 'recipient_name'],
2016                         ['id' => 3, 'screen_name' => 'sender_name']
2017                 );
2018                 $this->assertNull($result['sender']);
2019                 $this->assertNull($result['recipient']);
2020         }
2021
2022         /**
2023          * Test the api_convert_item() function.
2024          * @return void
2025          */
2026         public function testApiConvertItem()
2027         {
2028                 $result = api_convert_item(
2029                         [
2030                                 'network' => 'feed',
2031                                 'title' => 'item_title',
2032                                 // We need a long string to test that it is correctly cut
2033                                 'body' => 'perspiciatis impedit voluptatem quis molestiae ea qui '.
2034                                 'reiciendis dolorum aut ducimus sunt consequatur inventore dolor '.
2035                                 'officiis pariatur doloremque nemo culpa aut quidem qui dolore '.
2036                                 'laudantium atque commodi alias voluptatem non possimus aperiam '.
2037                                 'ipsum rerum consequuntur aut amet fugit quia aliquid praesentium '.
2038                                 'repellendus quibusdam et et inventore mollitia rerum sit autem '.
2039                                 'pariatur maiores ipsum accusantium perferendis vel sit possimus '.
2040                                 'veritatis nihil distinctio qui eum repellat officia illum quos '.
2041                                 'impedit quam iste esse unde qui suscipit aut facilis ut inventore '.
2042                                 'omnis exercitationem quo magnam consequatur maxime aut illum '.
2043                                 'soluta quaerat natus unde aspernatur et sed beatae nihil ullam '.
2044                                 'temporibus corporis ratione blanditiis perspiciatis impedit '.
2045                                 'voluptatem quis molestiae ea qui reiciendis dolorum aut ducimus '.
2046                                 'sunt consequatur inventore dolor officiis pariatur doloremque '.
2047                                 'nemo culpa aut quidem qui dolore laudantium atque commodi alias '.
2048                                 'voluptatem non possimus aperiam ipsum rerum consequuntur aut '.
2049                                 'amet fugit quia aliquid praesentium repellendus quibusdam et et '.
2050                                 'inventore mollitia rerum sit autem pariatur maiores ipsum accusantium '.
2051                                 'perferendis vel sit possimus veritatis nihil distinctio qui eum '.
2052                                 'repellat officia illum quos impedit quam iste esse unde qui '.
2053                                 'suscipit aut facilis ut inventore omnis exercitationem quo magnam '.
2054                                 'consequatur maxime aut illum soluta quaerat natus unde aspernatur '.
2055                                 'et sed beatae nihil ullam temporibus corporis ratione blanditiis'
2056                         ]
2057                 );
2058                 $this->assertStringStartsWith('item_title', $result['text']);
2059                 $this->assertStringStartsWith('<h4>item_title</h4><br>perspiciatis impedit voluptatem', $result['html']);
2060         }
2061
2062         /**
2063          * Test the api_convert_item() function with an empty item body.
2064          * @return void
2065          */
2066         public function testApiConvertItemWithoutBody()
2067         {
2068                 $result = api_convert_item(
2069                         [
2070                                 'network' => 'feed',
2071                                 'title' => 'item_title',
2072                                 'body' => '',
2073                                 'plink' => 'item_plink'
2074                         ]
2075                 );
2076                 $this->assertEquals('item_title', $result['text']);
2077                 $this->assertEquals('<h4>item_title</h4><br>item_plink', $result['html']);
2078         }
2079
2080         /**
2081          * Test the api_convert_item() function with the title in the body.
2082          * @return void
2083          */
2084         public function testApiConvertItemWithTitleInBody()
2085         {
2086                 $result = api_convert_item(
2087                         [
2088                                 'title' => 'item_title',
2089                                 'body' => 'item_title item_body'
2090                         ]
2091                 );
2092                 $this->assertEquals('item_title item_body', $result['text']);
2093                 $this->assertEquals('<h4>item_title</h4><br>item_title item_body', $result['html']);
2094         }
2095
2096         /**
2097          * Test the api_get_attachments() function.
2098          * @return void
2099          */
2100         public function testApiGetAttachments()
2101         {
2102                 $body = 'body';
2103                 $this->assertEmpty(api_get_attachments($body));
2104         }
2105
2106         /**
2107          * Test the api_get_attachments() function with an img tag.
2108          * @return void
2109          */
2110         public function testApiGetAttachmentsWithImage()
2111         {
2112                 $body = '[img]img_url[/img]';
2113                 $this->assertInternalType('array', api_get_attachments($body));
2114         }
2115
2116         /**
2117          * Test the api_get_attachments() function with an img tag and an AndStatus user agent.
2118          * @return void
2119          */
2120         public function testApiGetAttachmentsWithImageAndAndStatus()
2121         {
2122                 $_SERVER['HTTP_USER_AGENT'] = 'AndStatus';
2123                 $body = '[img]img_url[/img]';
2124                 $this->assertInternalType('array', api_get_attachments($body));
2125         }
2126
2127         /**
2128          * Test the api_get_entitities() function.
2129          * @return void
2130          */
2131         public function testApiGetEntitities()
2132         {
2133                 $text = 'text';
2134                 $this->assertInternalType('array', api_get_entitities($text, 'bbcode'));
2135         }
2136
2137         /**
2138          * Test the api_get_entitities() function with the include_entities parameter.
2139          * @return void
2140          */
2141         public function testApiGetEntititiesWithIncludeEntities()
2142         {
2143                 $_REQUEST['include_entities'] = 'true';
2144                 $text = 'text';
2145                 $result = api_get_entitities($text, 'bbcode');
2146                 $this->assertInternalType('array', $result['hashtags']);
2147                 $this->assertInternalType('array', $result['symbols']);
2148                 $this->assertInternalType('array', $result['urls']);
2149                 $this->assertInternalType('array', $result['user_mentions']);
2150         }
2151
2152         /**
2153          * Test the api_format_items_embeded_images() function.
2154          * @return void
2155          */
2156         public function testApiFormatItemsEmbededImages()
2157         {
2158                 $this->assertEquals(
2159                         'text http://localhost/display/item_guid',
2160                         api_format_items_embeded_images(['guid' => 'item_guid'], 'text data:image/foo')
2161                 );
2162         }
2163
2164         /**
2165          * Test the api_contactlink_to_array() function.
2166          * @return void
2167          */
2168         public function testApiContactlinkToArray()
2169         {
2170                 $this->assertEquals(
2171                         [
2172                                 'name' => 'text',
2173                                 'url' => '',
2174                         ],
2175                         api_contactlink_to_array('text')
2176                 );
2177         }
2178
2179         /**
2180          * Test the api_contactlink_to_array() function with an URL.
2181          * @return void
2182          */
2183         public function testApiContactlinkToArrayWithUrl()
2184         {
2185                 $this->assertEquals(
2186                         [
2187                                 'name' => ['link_text'],
2188                                 'url' => ['url'],
2189                         ],
2190                         api_contactlink_to_array('text <a href="url">link_text</a>')
2191                 );
2192         }
2193
2194         /**
2195          * Test the api_format_items_activities() function.
2196          * @return void
2197          */
2198         public function testApiFormatItemsActivities()
2199         {
2200                 $item = [];
2201                 $result = api_format_items_activities($item);
2202                 $this->assertArrayHasKey('like', $result);
2203                 $this->assertArrayHasKey('dislike', $result);
2204                 $this->assertArrayHasKey('attendyes', $result);
2205                 $this->assertArrayHasKey('attendno', $result);
2206                 $this->assertArrayHasKey('attendmaybe', $result);
2207         }
2208
2209         /**
2210          * Test the api_format_items_activities() function with an XML result.
2211          * @return void
2212          */
2213         public function testApiFormatItemsActivitiesWithXml()
2214         {
2215                 $item = [];
2216                 $result = api_format_items_activities($item, 'xml');
2217                 $this->assertArrayHasKey('friendica:like', $result);
2218                 $this->assertArrayHasKey('friendica:dislike', $result);
2219                 $this->assertArrayHasKey('friendica:attendyes', $result);
2220                 $this->assertArrayHasKey('friendica:attendno', $result);
2221                 $this->assertArrayHasKey('friendica:attendmaybe', $result);
2222         }
2223
2224         /**
2225          * Test the api_format_items_profiles() function.
2226          * @return void
2227          */
2228         public function testApiFormatItemsProfiles()
2229         {
2230                 $profile_row = [
2231                         'id' => 'profile_id',
2232                         'profile-name' => 'profile_name',
2233                         'is-default' => true,
2234                         'hide-friends' => true,
2235                         'photo' => 'profile_photo',
2236                         'thumb' => 'profile_thumb',
2237                         'publish' => true,
2238                         'net-publish' => true,
2239                         'pdesc' => 'description',
2240                         'dob' => 'date_of_birth',
2241                         'address' => 'address',
2242                         'locality' => 'city',
2243                         'region' => 'region',
2244                         'postal-code' => 'postal_code',
2245                         'country-name' => 'country',
2246                         'hometown' => 'hometown',
2247                         'gender' => 'gender',
2248                         'marital' => 'marital',
2249                         'with' => 'marital_with',
2250                         'howlong' => 'marital_since',
2251                         'sexual' => 'sexual',
2252                         'politic' => 'politic',
2253                         'religion' => 'religion',
2254                         'pub_keywords' => 'public_keywords',
2255                         'prv_keywords' => 'private_keywords',
2256
2257                         'likes' => 'likes',
2258                         'dislikes' => 'dislikes',
2259                         'about' => 'about',
2260                         'music' => 'music',
2261                         'book' => 'book',
2262                         'tv' => 'tv',
2263                         'film' => 'film',
2264                         'interest' => 'interest',
2265                         'romance' => 'romance',
2266                         'work' => 'work',
2267                         'education' => 'education',
2268                         'contact' => 'social_networks',
2269                         'homepage' => 'homepage'
2270                 ];
2271                 $result = api_format_items_profiles($profile_row);
2272                 $this->assertEquals(
2273                         [
2274                                 'profile_id' => 'profile_id',
2275                                 'profile_name' => 'profile_name',
2276                                 'is_default' => true,
2277                                 'hide_friends' => true,
2278                                 'profile_photo' => 'profile_photo',
2279                                 'profile_thumb' => 'profile_thumb',
2280                                 'publish' => true,
2281                                 'net_publish' => true,
2282                                 'description' => 'description',
2283                                 'date_of_birth' => 'date_of_birth',
2284                                 'address' => 'address',
2285                                 'city' => 'city',
2286                                 'region' => 'region',
2287                                 'postal_code' => 'postal_code',
2288                                 'country' => 'country',
2289                                 'hometown' => 'hometown',
2290                                 'gender' => 'gender',
2291                                 'marital' => 'marital',
2292                                 'marital_with' => 'marital_with',
2293                                 'marital_since' => 'marital_since',
2294                                 'sexual' => 'sexual',
2295                                 'politic' => 'politic',
2296                                 'religion' => 'religion',
2297                                 'public_keywords' => 'public_keywords',
2298                                 'private_keywords' => 'private_keywords',
2299
2300                                 'likes' => 'likes',
2301                                 'dislikes' => 'dislikes',
2302                                 'about' => 'about',
2303                                 'music' => 'music',
2304                                 'book' => 'book',
2305                                 'tv' => 'tv',
2306                                 'film' => 'film',
2307                                 'interest' => 'interest',
2308                                 'romance' => 'romance',
2309                                 'work' => 'work',
2310                                 'education' => 'education',
2311                                 'social_networks' => 'social_networks',
2312                                 'homepage' => 'homepage',
2313                                 'users' => null
2314                         ],
2315                         $result
2316                 );
2317         }
2318
2319         /**
2320          * Test the api_format_items() function.
2321          * @return void
2322          */
2323         public function testApiFormatItems()
2324         {
2325                 $items = [
2326                         [
2327                                 'item_network' => 'item_network',
2328                                 'source' => 'web',
2329                                 'coord' => '5 7'
2330                         ]
2331                 ];
2332                 $result = api_format_items($items, [], true);
2333                 foreach ($result as $status) {
2334                         $this->assertStatus($status);
2335                 }
2336         }
2337
2338         /**
2339          * Test the api_format_items() function with an XML result.
2340          * @return void
2341          */
2342         public function testApiFormatItemsWithXml()
2343         {
2344                 $items = [
2345                         [
2346                                 'coord' => '5 7'
2347                         ]
2348                 ];
2349                 $result = api_format_items($items, [], true, 'xml');
2350                 foreach ($result as $status) {
2351                         $this->assertStatus($status);
2352                 }
2353         }
2354
2355         /**
2356          * Test the api_format_items() function.
2357          * @return void
2358          */
2359         public function testApiAccountRateLimitStatus()
2360         {
2361                 $result = api_account_rate_limit_status('json');
2362                 $this->assertEquals(150, $result['hash']['remaining_hits']);
2363                 $this->assertEquals(150, $result['hash']['hourly_limit']);
2364                 $this->assertInternalType('int', $result['hash']['reset_time_in_seconds']);
2365         }
2366
2367         /**
2368          * Test the api_format_items() function with an XML result.
2369          * @return void
2370          */
2371         public function testApiAccountRateLimitStatusWithXml()
2372         {
2373                 $result = api_account_rate_limit_status('xml');
2374                 $this->assertXml($result, 'hash');
2375         }
2376
2377         /**
2378          * Test the api_help_test() function.
2379          * @return void
2380          */
2381         public function testApiHelpTest()
2382         {
2383                 $result = api_help_test('json');
2384                 $this->assertEquals(['ok' => 'ok'], $result);
2385         }
2386
2387         /**
2388          * Test the api_help_test() function with an XML result.
2389          * @return void
2390          */
2391         public function testApiHelpTestWithXml()
2392         {
2393                 $this->markTestIncomplete('Triggers this error: "key() expects parameter 1 to be array, string given"');
2394                 $result = api_help_test('xml');
2395                 $this->assertXml($result, 'ok');
2396         }
2397
2398         /**
2399          * Test the api_lists_list() function.
2400          * @return void
2401          */
2402         public function testApiListsList()
2403         {
2404                 $result = api_lists_list('json');
2405                 $this->assertEquals(['lists_list' => []], $result);
2406         }
2407
2408         /**
2409          * Test the api_lists_ownerships() function.
2410          * @return void
2411          */
2412         public function testApiListsOwnerships()
2413         {
2414                 $result = api_lists_ownerships('json');
2415                 foreach ($result['lists']['lists'] as $list) {
2416                         $this->assertList($list);
2417                 }
2418         }
2419
2420         /**
2421          * Test the api_lists_ownerships() function without an authenticated user.
2422          * @return void
2423          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2424          */
2425         public function testApiListsOwnershipsWithoutAuthenticatedUser()
2426         {
2427                 $_SESSION['authenticated'] = false;
2428                 api_lists_ownerships('json');
2429         }
2430
2431         /**
2432          * Test the api_lists_statuses() function.
2433          * @expectedException Friendica\Network\HTTPException\BadRequestException
2434          * @return void
2435          */
2436         public function testApiListsStatuses()
2437         {
2438                 api_lists_statuses('json');
2439         }
2440
2441         /**
2442          * Test the api_lists_statuses() function with a list ID.
2443          * @return void
2444          */
2445         public function testApiListsStatusesWithListId()
2446         {
2447                 $_REQUEST['list_id'] = 1;
2448                 $_REQUEST['page'] = -1;
2449                 $_REQUEST['max_id'] = 10;
2450                 $result = api_lists_statuses('json');
2451                 foreach ($result['status'] as $status) {
2452                         $this->assertStatus($status);
2453                 }
2454         }
2455
2456         /**
2457          * Test the api_lists_statuses() function with a list ID and a RSS result.
2458          * @return void
2459          */
2460         public function testApiListsStatusesWithListIdAndRss()
2461         {
2462                 $_REQUEST['list_id'] = 1;
2463                 $result = api_lists_statuses('rss');
2464                 $this->assertXml($result, 'statuses');
2465         }
2466
2467         /**
2468          * Test the api_lists_statuses() function with an unallowed user.
2469          * @return void
2470          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2471          */
2472         public function testApiListsStatusesWithUnallowedUser()
2473         {
2474                 $_SESSION['allow_api'] = false;
2475                 $_GET['screen_name'] = $this->selfUser['nick'];
2476                 api_lists_statuses('json');
2477         }
2478
2479         /**
2480          * Test the api_statuses_f() function.
2481          * @return void
2482          */
2483         public function testApiStatusesFWithFriends()
2484         {
2485                 $_GET['page'] = -1;
2486                 $result = api_statuses_f('friends');
2487                 $this->assertArrayHasKey('user', $result);
2488         }
2489
2490         /**
2491          * Test the api_statuses_f() function.
2492          * @return void
2493          */
2494         public function testApiStatusesFWithFollowers()
2495         {
2496                 $result = api_statuses_f('followers');
2497                 $this->assertArrayHasKey('user', $result);
2498         }
2499
2500         /**
2501          * Test the api_statuses_f() function.
2502          * @return void
2503          */
2504         public function testApiStatusesFWithBlocks()
2505         {
2506                 $result = api_statuses_f('blocks');
2507                 $this->assertArrayHasKey('user', $result);
2508         }
2509
2510         /**
2511          * Test the api_statuses_f() function.
2512          * @return void
2513          */
2514         public function testApiStatusesFWithIncoming()
2515         {
2516                 $result = api_statuses_f('incoming');
2517                 $this->assertArrayHasKey('user', $result);
2518         }
2519
2520         /**
2521          * Test the api_statuses_f() function an undefined cursor GET variable.
2522          * @return void
2523          */
2524         public function testApiStatusesFWithUndefinedCursor()
2525         {
2526                 $_GET['cursor'] = 'undefined';
2527                 $this->assertFalse(api_statuses_f('friends'));
2528         }
2529
2530         /**
2531          * Test the api_statuses_friends() function.
2532          * @return void
2533          */
2534         public function testApiStatusesFriends()
2535         {
2536                 $result = api_statuses_friends('json');
2537                 $this->assertArrayHasKey('user', $result);
2538         }
2539
2540         /**
2541          * Test the api_statuses_friends() function an undefined cursor GET variable.
2542          * @return void
2543          */
2544         public function testApiStatusesFriendsWithUndefinedCursor()
2545         {
2546                 $_GET['cursor'] = 'undefined';
2547                 $this->assertFalse(api_statuses_friends('json'));
2548         }
2549
2550         /**
2551          * Test the api_statuses_followers() function.
2552          * @return void
2553          */
2554         public function testApiStatusesFollowers()
2555         {
2556                 $result = api_statuses_followers('json');
2557                 $this->assertArrayHasKey('user', $result);
2558         }
2559
2560         /**
2561          * Test the api_statuses_followers() function an undefined cursor GET variable.
2562          * @return void
2563          */
2564         public function testApiStatusesFollowersWithUndefinedCursor()
2565         {
2566                 $_GET['cursor'] = 'undefined';
2567                 $this->assertFalse(api_statuses_followers('json'));
2568         }
2569
2570         /**
2571          * Test the api_blocks_list() function.
2572          * @return void
2573          */
2574         public function testApiBlocksList()
2575         {
2576                 $result = api_blocks_list('json');
2577                 $this->assertArrayHasKey('user', $result);
2578         }
2579
2580         /**
2581          * Test the api_blocks_list() function an undefined cursor GET variable.
2582          * @return void
2583          */
2584         public function testApiBlocksListWithUndefinedCursor()
2585         {
2586                 $_GET['cursor'] = 'undefined';
2587                 $this->assertFalse(api_blocks_list('json'));
2588         }
2589
2590         /**
2591          * Test the api_friendships_incoming() function.
2592          * @return void
2593          */
2594         public function testApiFriendshipsIncoming()
2595         {
2596                 $result = api_friendships_incoming('json');
2597                 $this->assertArrayHasKey('id', $result);
2598         }
2599
2600         /**
2601          * Test the api_friendships_incoming() function an undefined cursor GET variable.
2602          * @return void
2603          */
2604         public function testApiFriendshipsIncomingWithUndefinedCursor()
2605         {
2606                 $_GET['cursor'] = 'undefined';
2607                 $this->assertFalse(api_friendships_incoming('json'));
2608         }
2609
2610         /**
2611          * Test the api_statusnet_config() function.
2612          * @return void
2613          */
2614         public function testApiStatusnetConfig()
2615         {
2616                 $result = api_statusnet_config('json');
2617                 $this->assertEquals('localhost', $result['config']['site']['server']);
2618                 $this->assertEquals('default', $result['config']['site']['theme']);
2619                 $this->assertEquals('http://localhost/images/friendica-64.png', $result['config']['site']['logo']);
2620                 $this->assertTrue($result['config']['site']['fancy']);
2621                 $this->assertEquals('en', $result['config']['site']['language']);
2622                 $this->assertEquals('UTC', $result['config']['site']['timezone']);
2623                 $this->assertEquals(200000, $result['config']['site']['textlimit']);
2624                 $this->assertEquals('false', $result['config']['site']['private']);
2625                 $this->assertEquals('false', $result['config']['site']['ssl']);
2626                 $this->assertEquals(30, $result['config']['site']['shorturllength']);
2627         }
2628
2629         /**
2630          * Test the api_statusnet_version() function.
2631          * @return void
2632          */
2633         public function testApiStatusnetVersion()
2634         {
2635                 $result = api_statusnet_version('json');
2636                 $this->assertEquals('0.9.7', $result['version']);
2637         }
2638
2639         /**
2640          * Test the api_ff_ids() function.
2641          * @return void
2642          */
2643         public function testApiFfIds()
2644         {
2645                 $result = api_ff_ids('json');
2646                 $this->assertNull($result);
2647         }
2648
2649         /**
2650          * Test the api_ff_ids() function with a result.
2651          * @return void
2652          */
2653         public function testApiFfIdsWithResult()
2654         {
2655                 $this->markTestIncomplete();
2656         }
2657
2658         /**
2659          * Test the api_ff_ids() function without an authenticated user.
2660          * @return void
2661          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2662          */
2663         public function testApiFfIdsWithoutAuthenticatedUser()
2664         {
2665                 $_SESSION['authenticated'] = false;
2666                 api_ff_ids('json');
2667         }
2668
2669         /**
2670          * Test the api_friends_ids() function.
2671          * @return void
2672          */
2673         public function testApiFriendsIds()
2674         {
2675                 $result = api_friends_ids('json');
2676                 $this->assertNull($result);
2677         }
2678
2679         /**
2680          * Test the api_followers_ids() function.
2681          * @return void
2682          */
2683         public function testApiFollowersIds()
2684         {
2685                 $result = api_followers_ids('json');
2686                 $this->assertNull($result);
2687         }
2688
2689         /**
2690          * Test the api_direct_messages_new() function.
2691          * @return void
2692          */
2693         public function testApiDirectMessagesNew()
2694         {
2695                 $result = api_direct_messages_new('json');
2696                 $this->assertNull($result);
2697         }
2698
2699         /**
2700          * Test the api_direct_messages_new() function without an authenticated user.
2701          * @return void
2702          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2703          */
2704         public function testApiDirectMessagesNewWithoutAuthenticatedUser()
2705         {
2706                 $_SESSION['authenticated'] = false;
2707                 api_direct_messages_new('json');
2708         }
2709
2710         /**
2711          * Test the api_direct_messages_new() function with an user ID.
2712          * @return void
2713          */
2714         public function testApiDirectMessagesNewWithUserId()
2715         {
2716                 $_POST['text'] = 'message_text';
2717                 $_POST['user_id'] = $this->otherUser['id'];
2718                 $result = api_direct_messages_new('json');
2719                 $this->assertEquals(['direct_message' => ['error' => -1]], $result);
2720         }
2721
2722         /**
2723          * Test the api_direct_messages_new() function with a screen name.
2724          * @return void
2725          */
2726         public function testApiDirectMessagesNewWithScreenName()
2727         {
2728                 $_POST['text'] = 'message_text';
2729                 $_POST['screen_name'] = $this->otherUser['nick'];
2730                 $result = api_direct_messages_new('json');
2731                 $this->assertEquals(1, $result['direct_message']['id']);
2732                 $this->assertContains('message_text', $result['direct_message']['text']);
2733                 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2734                 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2735         }
2736
2737         /**
2738          * Test the api_direct_messages_new() function with a title.
2739          * @return void
2740          */
2741         public function testApiDirectMessagesNewWithTitle()
2742         {
2743                 $_POST['text'] = 'message_text';
2744                 $_POST['screen_name'] = $this->otherUser['nick'];
2745                 $_REQUEST['title'] = 'message_title';
2746                 $result = api_direct_messages_new('json');
2747                 $this->assertEquals(1, $result['direct_message']['id']);
2748                 $this->assertContains('message_text', $result['direct_message']['text']);
2749                 $this->assertContains('message_title', $result['direct_message']['text']);
2750                 $this->assertEquals('selfcontact', $result['direct_message']['sender_screen_name']);
2751                 $this->assertEquals(1, $result['direct_message']['friendica_seen']);
2752         }
2753
2754         /**
2755          * Test the api_direct_messages_new() function with an RSS result.
2756          * @return void
2757          */
2758         public function testApiDirectMessagesNewWithRss()
2759         {
2760                 $_POST['text'] = 'message_text';
2761                 $_POST['screen_name'] = $this->otherUser['nick'];
2762                 $result = api_direct_messages_new('rss');
2763                 $this->assertXml($result, 'direct-messages');
2764         }
2765
2766         /**
2767          * Test the api_direct_messages_destroy() function.
2768          * @return void
2769          * @expectedException Friendica\Network\HTTPException\BadRequestException
2770          */
2771         public function testApiDirectMessagesDestroy()
2772         {
2773                 api_direct_messages_destroy('json');
2774         }
2775
2776         /**
2777          * Test the api_direct_messages_destroy() function with the friendica_verbose GET param.
2778          * @return void
2779          */
2780         public function testApiDirectMessagesDestroyWithVerbose()
2781         {
2782                 $_GET['friendica_verbose'] = 'true';
2783                 $result = api_direct_messages_destroy('json');
2784                 $this->assertEquals(
2785                         [
2786                                 '$result' => [
2787                                         'result' => 'error',
2788                                         'message' => 'message id or parenturi not specified'
2789                                 ]
2790                         ],
2791                         $result
2792                 );
2793         }
2794
2795         /**
2796          * Test the api_direct_messages_destroy() function without an authenticated user.
2797          * @return void
2798          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2799          */
2800         public function testApiDirectMessagesDestroyWithoutAuthenticatedUser()
2801         {
2802                 $_SESSION['authenticated'] = false;
2803                 api_direct_messages_destroy('json');
2804         }
2805
2806         /**
2807          * Test the api_direct_messages_destroy() function with a non-zero ID.
2808          * @return void
2809          * @expectedException Friendica\Network\HTTPException\BadRequestException
2810          */
2811         public function testApiDirectMessagesDestroyWithId()
2812         {
2813                 $_REQUEST['id'] = 1;
2814                 api_direct_messages_destroy('json');
2815         }
2816
2817         /**
2818          * Test the api_direct_messages_destroy() with a non-zero ID and the friendica_verbose GET param.
2819          * @return void
2820          */
2821         public function testApiDirectMessagesDestroyWithIdAndVerbose()
2822         {
2823                 $_REQUEST['id'] = 1;
2824                 $_REQUEST['friendica_parenturi'] = 'parent_uri';
2825                 $_GET['friendica_verbose'] = 'true';
2826                 $result = api_direct_messages_destroy('json');
2827                 $this->assertEquals(
2828                         [
2829                                 '$result' => [
2830                                         'result' => 'error',
2831                                         'message' => 'message id not in database'
2832                                 ]
2833                         ],
2834                         $result
2835                 );
2836         }
2837
2838         /**
2839          * Test the api_direct_messages_destroy() function with a non-zero ID.
2840          * @return void
2841          */
2842         public function testApiDirectMessagesDestroyWithCorrectId()
2843         {
2844                 $this->markTestIncomplete('We need to add a dataset for this.');
2845         }
2846
2847         /**
2848          * Test the api_direct_messages_box() function.
2849          * @return void
2850          */
2851         public function testApiDirectMessagesBoxWithSentbox()
2852         {
2853                 $_REQUEST['page'] = -1;
2854                 $_REQUEST['max_id'] = 10;
2855                 $result = api_direct_messages_box('json', 'sentbox', 'false');
2856                 $this->assertArrayHasKey('direct_message', $result);
2857         }
2858
2859         /**
2860          * Test the api_direct_messages_box() function.
2861          * @return void
2862          */
2863         public function testApiDirectMessagesBoxWithConversation()
2864         {
2865                 $result = api_direct_messages_box('json', 'conversation', 'false');
2866                 $this->assertArrayHasKey('direct_message', $result);
2867         }
2868
2869         /**
2870          * Test the api_direct_messages_box() function.
2871          * @return void
2872          */
2873         public function testApiDirectMessagesBoxWithAll()
2874         {
2875                 $result = api_direct_messages_box('json', 'all', 'false');
2876                 $this->assertArrayHasKey('direct_message', $result);
2877         }
2878
2879         /**
2880          * Test the api_direct_messages_box() function.
2881          * @return void
2882          */
2883         public function testApiDirectMessagesBoxWithInbox()
2884         {
2885                 $result = api_direct_messages_box('json', 'inbox', 'false');
2886                 $this->assertArrayHasKey('direct_message', $result);
2887         }
2888
2889         /**
2890          * Test the api_direct_messages_box() function.
2891          * @return void
2892          */
2893         public function testApiDirectMessagesBoxWithVerbose()
2894         {
2895                 $result = api_direct_messages_box('json', 'sentbox', 'true');
2896                 $this->assertEquals(
2897                         [
2898                                 '$result' => [
2899                                         'result' => 'error',
2900                                         'message' => 'no mails available'
2901                                 ]
2902                         ],
2903                         $result
2904                 );
2905         }
2906
2907         /**
2908          * Test the api_direct_messages_box() function with a RSS result.
2909          * @return void
2910          */
2911         public function testApiDirectMessagesBoxWithRss()
2912         {
2913                 $result = api_direct_messages_box('rss', 'sentbox', 'false');
2914                 $this->assertXml($result, 'direct-messages');
2915         }
2916
2917         /**
2918          * Test the api_direct_messages_box() function without an authenticated user.
2919          * @return void
2920          * @expectedException Friendica\Network\HTTPException\ForbiddenException
2921          */
2922         public function testApiDirectMessagesBoxWithUnallowedUser()
2923         {
2924                 $_SESSION['allow_api'] = false;
2925                 $_GET['screen_name'] = $this->selfUser['nick'];
2926                 api_direct_messages_box('json', 'sentbox', 'false');
2927         }
2928
2929         /**
2930          * Test the api_direct_messages_sentbox() function.
2931          * @return void
2932          */
2933         public function testApiDirectMessagesSentbox()
2934         {
2935                 $result = api_direct_messages_sentbox('json');
2936                 $this->assertArrayHasKey('direct_message', $result);
2937         }
2938
2939         /**
2940          * Test the api_direct_messages_inbox() function.
2941          * @return void
2942          */
2943         public function testApiDirectMessagesInbox()
2944         {
2945                 $result = api_direct_messages_inbox('json');
2946                 $this->assertArrayHasKey('direct_message', $result);
2947         }
2948
2949         /**
2950          * Test the api_direct_messages_all() function.
2951          * @return void
2952          */
2953         public function testApiDirectMessagesAll()
2954         {
2955                 $result = api_direct_messages_all('json');
2956                 $this->assertArrayHasKey('direct_message', $result);
2957         }
2958
2959         /**
2960          * Test the api_direct_messages_conversation() function.
2961          * @return void
2962          */
2963         public function testApiDirectMessagesConversation()
2964         {
2965                 $result = api_direct_messages_conversation('json');
2966                 $this->assertArrayHasKey('direct_message', $result);
2967         }
2968
2969         /**
2970          * Test the api_oauth_request_token() function.
2971          * @return void
2972          */
2973         public function testApiOauthRequestToken()
2974         {
2975                 $this->markTestIncomplete('killme() kills phpunit as well');
2976         }
2977
2978         /**
2979          * Test the api_oauth_access_token() function.
2980          * @return void
2981          */
2982         public function testApiOauthAccessToken()
2983         {
2984                 $this->markTestIncomplete('killme() kills phpunit as well');
2985         }
2986
2987         /**
2988          * Test the api_fr_photoalbum_delete() function.
2989          * @return void
2990          * @expectedException Friendica\Network\HTTPException\BadRequestException
2991          */
2992         public function testApiFrPhotoalbumDelete()
2993         {
2994                 api_fr_photoalbum_delete('json');
2995         }
2996
2997         /**
2998          * Test the api_fr_photoalbum_delete() function with an album name.
2999          * @return void
3000          * @expectedException Friendica\Network\HTTPException\BadRequestException
3001          */
3002         public function testApiFrPhotoalbumDeleteWithAlbum()
3003         {
3004                 $_REQUEST['album'] = 'album_name';
3005                 api_fr_photoalbum_delete('json');
3006         }
3007
3008         /**
3009          * Test the api_fr_photoalbum_delete() function with an album name.
3010          * @return void
3011          */
3012         public function testApiFrPhotoalbumDeleteWithValidAlbum()
3013         {
3014                 $this->markTestIncomplete('We need to add a dataset for this.');
3015         }
3016
3017         /**
3018          * Test the api_fr_photoalbum_delete() function.
3019          * @return void
3020          * @expectedException Friendica\Network\HTTPException\BadRequestException
3021          */
3022         public function testApiFrPhotoalbumUpdate()
3023         {
3024                 api_fr_photoalbum_update('json');
3025         }
3026
3027         /**
3028          * Test the api_fr_photoalbum_delete() function with an album name.
3029          * @return void
3030          * @expectedException Friendica\Network\HTTPException\BadRequestException
3031          */
3032         public function testApiFrPhotoalbumUpdateWithAlbum()
3033         {
3034                 $_REQUEST['album'] = 'album_name';
3035                 api_fr_photoalbum_update('json');
3036         }
3037
3038         /**
3039          * Test the api_fr_photoalbum_delete() function with an album name.
3040          * @return void
3041          * @expectedException Friendica\Network\HTTPException\BadRequestException
3042          */
3043         public function testApiFrPhotoalbumUpdateWithAlbumAndNewAlbum()
3044         {
3045                 $_REQUEST['album'] = 'album_name';
3046                 $_REQUEST['album_new'] = 'album_name';
3047                 api_fr_photoalbum_update('json');
3048         }
3049
3050         /**
3051          * Test the api_fr_photoalbum_update() function without an authenticated user.
3052          * @return void
3053          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3054          */
3055         public function testApiFrPhotoalbumUpdateWithoutAuthenticatedUser()
3056         {
3057                 $_SESSION['authenticated'] = false;
3058                 api_fr_photoalbum_update('json');
3059         }
3060
3061         /**
3062          * Test the api_fr_photoalbum_delete() function with an album name.
3063          * @return void
3064          */
3065         public function testApiFrPhotoalbumUpdateWithValidAlbum()
3066         {
3067                 $this->markTestIncomplete('We need to add a dataset for this.');
3068         }
3069
3070         /**
3071          * Test the api_fr_photos_list() function.
3072          * @return void
3073          */
3074         public function testApiFrPhotosList()
3075         {
3076                 $result = api_fr_photos_list('json');
3077                 $this->assertArrayHasKey('photo', $result);
3078         }
3079
3080         /**
3081          * Test the api_fr_photos_list() function without an authenticated user.
3082          * @return void
3083          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3084          */
3085         public function testApiFrPhotosListWithoutAuthenticatedUser()
3086         {
3087                 $_SESSION['authenticated'] = false;
3088                 api_fr_photos_list('json');
3089         }
3090
3091         /**
3092          * Test the api_fr_photo_create_update() function.
3093          * @return void
3094          * @expectedException Friendica\Network\HTTPException\BadRequestException
3095          */
3096         public function testApiFrPhotoCreateUpdate()
3097         {
3098                 api_fr_photo_create_update('json');
3099         }
3100
3101         /**
3102          * Test the api_fr_photo_create_update() function without an authenticated user.
3103          * @return void
3104          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3105          */
3106         public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser()
3107         {
3108                 $_SESSION['authenticated'] = false;
3109                 api_fr_photo_create_update('json');
3110         }
3111
3112         /**
3113          * Test the api_fr_photo_create_update() function with an album name.
3114          * @return void
3115          * @expectedException Friendica\Network\HTTPException\BadRequestException
3116          */
3117         public function testApiFrPhotoCreateUpdateWithAlbum()
3118         {
3119                 $_REQUEST['album'] = 'album_name';
3120                 api_fr_photo_create_update('json');
3121         }
3122
3123         /**
3124          * Test the api_fr_photo_create_update() function with the update mode.
3125          * @return void
3126          */
3127         public function testApiFrPhotoCreateUpdateWithUpdate()
3128         {
3129                 $this->markTestIncomplete('We need to create a dataset for this');
3130         }
3131
3132         /**
3133          * Test the api_fr_photo_create_update() function with an uploaded file.
3134          * @return void
3135          */
3136         public function testApiFrPhotoCreateUpdateWithFile()
3137         {
3138                 $this->markTestIncomplete();
3139         }
3140
3141         /**
3142          * Test the api_fr_photo_delete() function.
3143          * @return void
3144          * @expectedException Friendica\Network\HTTPException\BadRequestException
3145          */
3146         public function testApiFrPhotoDelete()
3147         {
3148                 api_fr_photo_delete('json');
3149         }
3150
3151         /**
3152          * Test the api_fr_photo_delete() function without an authenticated user.
3153          * @return void
3154          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3155          */
3156         public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
3157         {
3158                 $_SESSION['authenticated'] = false;
3159                 api_fr_photo_delete('json');
3160         }
3161
3162         /**
3163          * Test the api_fr_photo_delete() function with a photo ID.
3164          * @return void
3165          * @expectedException Friendica\Network\HTTPException\BadRequestException
3166          */
3167         public function testApiFrPhotoDeleteWithPhotoId()
3168         {
3169                 $_REQUEST['photo_id'] = 1;
3170                 api_fr_photo_delete('json');
3171         }
3172
3173         /**
3174          * Test the api_fr_photo_delete() function with a correct photo ID.
3175          * @return void
3176          */
3177         public function testApiFrPhotoDeleteWithCorrectPhotoId()
3178         {
3179                 $this->markTestIncomplete('We need to create a dataset for this.');
3180         }
3181
3182         /**
3183          * Test the api_fr_photo_detail() function.
3184          * @return void
3185          * @expectedException Friendica\Network\HTTPException\BadRequestException
3186          */
3187         public function testApiFrPhotoDetail()
3188         {
3189                 api_fr_photo_detail('json');
3190         }
3191
3192         /**
3193          * Test the api_fr_photo_detail() function without an authenticated user.
3194          * @return void
3195          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3196          */
3197         public function testApiFrPhotoDetailWithoutAuthenticatedUser()
3198         {
3199                 $_SESSION['authenticated'] = false;
3200                 api_fr_photo_detail('json');
3201         }
3202
3203         /**
3204          * Test the api_fr_photo_detail() function with a photo ID.
3205          * @return void
3206          * @expectedException Friendica\Network\HTTPException\NotFoundException
3207          */
3208         public function testApiFrPhotoDetailWithPhotoId()
3209         {
3210                 $_REQUEST['photo_id'] = 1;
3211                 api_fr_photo_detail('json');
3212         }
3213
3214         /**
3215          * Test the api_fr_photo_detail() function with a correct photo ID.
3216          * @return void
3217          */
3218         public function testApiFrPhotoDetailCorrectPhotoId()
3219         {
3220                 $this->markTestIncomplete('We need to create a dataset for this.');
3221         }
3222
3223         /**
3224          * Test the api_account_update_profile_image() function.
3225          * @return void
3226          * @expectedException Friendica\Network\HTTPException\BadRequestException
3227          */
3228         public function testApiAccountUpdateProfileImage()
3229         {
3230                 api_account_update_profile_image('json');
3231         }
3232
3233         /**
3234          * Test the api_account_update_profile_image() function without an authenticated user.
3235          * @return void
3236          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3237          */
3238         public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser()
3239         {
3240                 $_SESSION['authenticated'] = false;
3241                 api_account_update_profile_image('json');
3242         }
3243
3244         /**
3245          * Test the api_account_update_profile_image() function with an uploaded file.
3246          * @return void
3247          * @expectedException Friendica\Network\HTTPException\BadRequestException
3248          */
3249         public function testApiAccountUpdateProfileImageWithUpload()
3250         {
3251                 $this->markTestIncomplete();
3252         }
3253
3254
3255         /**
3256          * Test the api_account_update_profile() function.
3257          * @return void
3258          */
3259         public function testApiAccountUpdateProfile()
3260         {
3261                 $_POST['name'] = 'new_name';
3262                 $_POST['description'] = 'new_description';
3263                 $result = api_account_update_profile('json');
3264                 // We can't use assertSelfUser() here because the user object is missing some properties.
3265                 $this->assertEquals($this->selfUser['id'], $result['user']['cid']);
3266                 $this->assertEquals('Friendica', $result['user']['location']);
3267                 $this->assertEquals($this->selfUser['nick'], $result['user']['screen_name']);
3268                 $this->assertEquals('dfrn', $result['user']['network']);
3269                 $this->assertEquals('new_name', $result['user']['name']);
3270                 $this->assertEquals('new_description', $result['user']['description']);
3271         }
3272
3273         /**
3274          * Test the check_acl_input() function.
3275          * @return void
3276          */
3277         public function testCheckAclInput()
3278         {
3279                 $result = check_acl_input('<aclstring>');
3280                 // Where does this result come from?
3281                 $this->assertEquals(1, $result);
3282         }
3283
3284         /**
3285          * Test the check_acl_input() function with an empty ACL string.
3286          * @return void
3287          */
3288         public function testCheckAclInputWithEmptyAclString()
3289         {
3290                 $result = check_acl_input(' ');
3291                 $this->assertFalse($result);
3292         }
3293
3294         /**
3295          * Test the save_media_to_database() function.
3296          * @return void
3297          */
3298         public function testSaveMediaToDatabase()
3299         {
3300                 $this->markTestIncomplete();
3301         }
3302
3303         /**
3304          * Test the post_photo_item() function.
3305          * @return void
3306          */
3307         public function testPostPhotoItem()
3308         {
3309                 $this->markTestIncomplete();
3310         }
3311
3312         /**
3313          * Test the prepare_photo_data() function.
3314          * @return void
3315          */
3316         public function testPreparePhotoData()
3317         {
3318                 $this->markTestIncomplete();
3319         }
3320
3321         /**
3322          * Test the api_friendica_remoteauth() function.
3323          * @return void
3324          * @expectedException Friendica\Network\HTTPException\BadRequestException
3325          */
3326         public function testApiFriendicaRemoteauth()
3327         {
3328                 api_friendica_remoteauth();
3329         }
3330
3331         /**
3332          * Test the api_friendica_remoteauth() function with an URL.
3333          * @return void
3334          * @expectedException Friendica\Network\HTTPException\BadRequestException
3335          */
3336         public function testApiFriendicaRemoteauthWithUrl()
3337         {
3338                 $_GET['url'] = 'url';
3339                 $_GET['c_url'] = 'url';
3340                 api_friendica_remoteauth();
3341         }
3342
3343         /**
3344          * Test the api_friendica_remoteauth() function with a correct URL.
3345          * @return void
3346          */
3347         public function testApiFriendicaRemoteauthWithCorrectUrl()
3348         {
3349                 $this->markTestIncomplete("We can't use an assertion here because of goaway().");
3350                 $_GET['url'] = 'url';
3351                 $_GET['c_url'] = $this->selfUser['nurl'];
3352                 api_friendica_remoteauth();
3353         }
3354
3355         /**
3356          * Test the api_share_as_retweet() function.
3357          * @return void
3358          */
3359         public function testApiShareAsRetweet()
3360         {
3361                 $item = [];
3362                 $result = api_share_as_retweet($item);
3363                 $this->assertFalse($result);
3364         }
3365
3366         /**
3367          * Test the api_share_as_retweet() function with a valid item.
3368          * @return void
3369          */
3370         public function testApiShareAsRetweetWithValidItem()
3371         {
3372                 $this->markTestIncomplete();
3373         }
3374
3375         /**
3376          * Test the api_get_nick() function.
3377          * @return void
3378          */
3379         public function testApiGetNick()
3380         {
3381                 $result = api_get_nick($this->otherUser['nurl']);
3382                 $this->assertEquals('othercontact', $result);
3383         }
3384
3385         /**
3386          * Test the api_get_nick() function with a wrong URL.
3387          * @return void
3388          */
3389         public function testApiGetNickWithWrongUrl()
3390         {
3391                 $result = api_get_nick('wrong_url');
3392                 $this->assertFalse($result);
3393         }
3394
3395         /**
3396          * Test the api_in_reply_to() function.
3397          * @return void
3398          */
3399         public function testApiInReplyTo()
3400         {
3401                 $result = api_in_reply_to([]);
3402                 $this->assertArrayHasKey('status_id', $result);
3403                 $this->assertArrayHasKey('user_id', $result);
3404                 $this->assertArrayHasKey('status_id_str', $result);
3405                 $this->assertArrayHasKey('user_id_str', $result);
3406                 $this->assertArrayHasKey('screen_name', $result);
3407         }
3408
3409         /**
3410          * Test the api_in_reply_to() function with a valid item.
3411          * @return void
3412          */
3413         public function testApiInReplyToWithValidItem()
3414         {
3415                 $this->markTestIncomplete();
3416         }
3417
3418         /**
3419          * Test the api_clean_plain_items() function.
3420          * @return void
3421          */
3422         public function testApiCleanPlainItems()
3423         {
3424                 $_REQUEST['include_entities'] = 'true';
3425                 $result = api_clean_plain_items('some_text [url="some_url"]some_text[/url]');
3426                 $this->assertEquals('some_text [url="some_url"]"some_url"[/url]', $result);
3427         }
3428
3429         /**
3430          * Test the api_clean_attachments() function.
3431          * @return void
3432          */
3433         public function testApiCleanAttachments()
3434         {
3435                 $this->markTestIncomplete();
3436         }
3437
3438         /**
3439          * Test the api_best_nickname() function.
3440          * @return void
3441          */
3442         public function testApiBestNickname()
3443         {
3444                 $contacts = [];
3445                 $result = api_best_nickname($contacts);
3446                 $this->assertNull($result);
3447         }
3448
3449         /**
3450          * Test the api_best_nickname() function with contacts.
3451          * @return void
3452          */
3453         public function testApiBestNicknameWithContacts()
3454         {
3455                 $this->markTestIncomplete();
3456         }
3457
3458         /**
3459          * Test the api_friendica_group_show() function.
3460          * @return void
3461          */
3462         public function testApiFriendicaGroupShow()
3463         {
3464                 $this->markTestIncomplete();
3465         }
3466
3467         /**
3468          * Test the api_friendica_group_delete() function.
3469          * @return void
3470          */
3471         public function testApiFriendicaGroupDelete()
3472         {
3473                 $this->markTestIncomplete();
3474         }
3475
3476         /**
3477          * Test the api_lists_destroy() function.
3478          * @return void
3479          */
3480         public function testApiListsDestroy()
3481         {
3482                 $this->markTestIncomplete();
3483         }
3484
3485         /**
3486          * Test the group_create() function.
3487          * @return void
3488          */
3489         public function testGroupCreate()
3490         {
3491                 $this->markTestIncomplete();
3492         }
3493
3494         /**
3495          * Test the api_friendica_group_create() function.
3496          * @return void
3497          */
3498         public function testApiFriendicaGroupCreate()
3499         {
3500                 $this->markTestIncomplete();
3501         }
3502
3503         /**
3504          * Test the api_lists_create() function.
3505          * @return void
3506          */
3507         public function testApiListsCreate()
3508         {
3509                 $this->markTestIncomplete();
3510         }
3511
3512         /**
3513          * Test the api_friendica_group_update() function.
3514          * @return void
3515          */
3516         public function testApiFriendicaGroupUpdate()
3517         {
3518                 $this->markTestIncomplete();
3519         }
3520
3521         /**
3522          * Test the api_lists_update() function.
3523          * @return void
3524          */
3525         public function testApiListsUpdate()
3526         {
3527                 $this->markTestIncomplete();
3528         }
3529
3530         /**
3531          * Test the api_friendica_activity() function.
3532          * @return void
3533          */
3534         public function testApiFriendicaActivity()
3535         {
3536                 $this->markTestIncomplete();
3537         }
3538
3539         /**
3540          * Test the api_friendica_notification() function.
3541          * @return void
3542          * @expectedException Friendica\Network\HTTPException\BadRequestException
3543          */
3544         public function testApiFriendicaNotification()
3545         {
3546                 api_friendica_notification('json');
3547         }
3548
3549         /**
3550          * Test the api_friendica_notification() function without an authenticated user.
3551          * @return void
3552          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3553          */
3554         public function testApiFriendicaNotificationWithoutAuthenticatedUser()
3555         {
3556                 $_SESSION['authenticated'] = false;
3557                 api_friendica_notification('json');
3558         }
3559
3560         /**
3561          * Test the api_friendica_notification() function with an argument count.
3562          * @return void
3563          */
3564         public function testApiFriendicaNotificationWithArgumentCount()
3565         {
3566                 $this->app->argc = 3;
3567                 $result = api_friendica_notification('json');
3568                 $this->assertEquals(['note' => false], $result);
3569         }
3570
3571         /**
3572          * Test the api_friendica_notification() function with an XML result.
3573          * @return void
3574          */
3575         public function testApiFriendicaNotificationWithXmlResult()
3576         {
3577                 $this->markTestIncomplete('Fails with "Invalid argument supplied for foreach()".');
3578                 $this->app->argc = 3;
3579                 $result = api_friendica_notification('xml');
3580                 $this->assertXml($result, 'notes');
3581         }
3582
3583         /**
3584          * Test the api_friendica_notification_seen() function.
3585          * @return void
3586          */
3587         public function testApiFriendicaNotificationSeen()
3588         {
3589                 $this->markTestIncomplete();
3590         }
3591
3592         /**
3593          * Test the api_friendica_direct_messages_setseen() function.
3594          * @return void
3595          */
3596         public function testApiFriendicaDirectMessagesSetseen()
3597         {
3598                 $this->markTestIncomplete();
3599         }
3600
3601         /**
3602          * Test the api_friendica_direct_messages_search() function.
3603          * @return void
3604          */
3605         public function testApiFriendicaDirectMessagesSearch()
3606         {
3607                 $this->markTestIncomplete();
3608         }
3609
3610         /**
3611          * Test the api_friendica_profile_show() function.
3612          * @return void
3613          */
3614         public function testApiFriendicaProfileShow()
3615         {
3616                 $result = api_friendica_profile_show('json');
3617                 // We can't use assertSelfUser() here because the user object is missing some properties.
3618                 $this->assertEquals($this->selfUser['id'], $result['$result']['friendica_owner']['cid']);
3619                 $this->assertEquals('Friendica', $result['$result']['friendica_owner']['location']);
3620                 $this->assertEquals($this->selfUser['name'], $result['$result']['friendica_owner']['name']);
3621                 $this->assertEquals($this->selfUser['nick'], $result['$result']['friendica_owner']['screen_name']);
3622                 $this->assertEquals('dfrn', $result['$result']['friendica_owner']['network']);
3623                 $this->assertTrue($result['$result']['friendica_owner']['verified']);
3624                 $this->assertFalse($result['$result']['multi_profiles']);
3625         }
3626
3627         /**
3628          * Test the api_friendica_profile_show() function with a profile ID.
3629          * @return void
3630          */
3631         public function testApiFriendicaProfileShowWithProfileId()
3632         {
3633                 $this->markTestIncomplete('We need to add a dataset for this.');
3634         }
3635
3636         /**
3637          * Test the api_friendica_profile_show() function with a wrong profile ID.
3638          * @return void
3639          * @expectedException Friendica\Network\HTTPException\BadRequestException
3640          */
3641         public function testApiFriendicaProfileShowWithWrongProfileId()
3642         {
3643                 $_REQUEST['profile_id'] = 666;
3644                 api_friendica_profile_show('json');
3645         }
3646
3647         /**
3648          * Test the api_friendica_profile_show() function without an authenticated user.
3649          * @return void
3650          * @expectedException Friendica\Network\HTTPException\ForbiddenException
3651          */
3652         public function testApiFriendicaProfileShowWithoutAuthenticatedUser()
3653         {
3654                 $_SESSION['authenticated'] = false;
3655                 api_friendica_profile_show('json');
3656         }
3657
3658         /**
3659          * Test the api_saved_searches_list() function.
3660          * @return void
3661          */
3662         public function testApiSavedSearchesList()
3663         {
3664                 $result = api_saved_searches_list('json');
3665                 $this->assertEquals(1, $result['terms'][0]['id']);
3666                 $this->assertEquals(1, $result['terms'][0]['id_str']);
3667                 $this->assertEquals('Saved search', $result['terms'][0]['name']);
3668                 $this->assertEquals('Saved search', $result['terms'][0]['query']);
3669         }
3670 }