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