]> git.mxchange.org Git - friendica.git/blob - tests/src/Module/Api/ApiResponseTest.php
spelling: forums
[friendica.git] / tests / src / Module / Api / ApiResponseTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Module\Api;
23
24 use Friendica\App\Arguments;
25 use Friendica\App\BaseURL;
26 use Friendica\Core\L10n;
27 use Friendica\Factory\Api\Twitter\User;
28 use Friendica\Module\Api\ApiResponse;
29 use Friendica\Test\MockedTest;
30 use Psr\Log\NullLogger;
31
32 class ApiResponseTest extends MockedTest
33 {
34         public function testErrorWithJson()
35         {
36                 $l10n = \Mockery::mock(L10n::class);
37                 $args = \Mockery::mock(Arguments::class);
38                 $args->shouldReceive('getQueryString')->andReturn('');
39                 $baseUrl     = \Mockery::mock(BaseURL::class);
40                 $twitterUser = \Mockery::mock(User::class);
41
42                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
43                 $response->error(200, 'OK', 'error_message', 'json');
44
45                 self::assertEquals('{"error":"error_message","code":"200 OK","request":""}', $response->getContent());
46         }
47
48         public function testErrorWithXml()
49         {
50                 $l10n = \Mockery::mock(L10n::class);
51                 $args = \Mockery::mock(Arguments::class);
52                 $args->shouldReceive('getQueryString')->andReturn('');
53                 $baseUrl     = \Mockery::mock(BaseURL::class);
54                 $twitterUser = \Mockery::mock(User::class);
55
56                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
57                 $response->error(200, 'OK', 'error_message', 'xml');
58
59                 self::assertEquals(['Content-type' => 'text/xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
60                 self::assertEquals('<?xml version="1.0"?>' . "\n" .
61                                                    '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
62                                                    'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
63                                                    'xmlns:georss="http://www.georss.org/georss">' . "\n" .
64                                                    '  <error>error_message</error>' . "\n" .
65                                                    '  <code>200 OK</code>' . "\n" .
66                                                    '  <request/>' . "\n" .
67                                                    '</status>' . "\n",
68                         $response->getContent());
69         }
70
71         public function testErrorWithRss()
72         {
73                 $l10n = \Mockery::mock(L10n::class);
74                 $args = \Mockery::mock(Arguments::class);
75                 $args->shouldReceive('getQueryString')->andReturn('');
76                 $baseUrl     = \Mockery::mock(BaseURL::class);
77                 $twitterUser = \Mockery::mock(User::class);
78
79                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
80                 $response->error(200, 'OK', 'error_message', 'rss');
81
82                 self::assertEquals(['Content-type' => 'application/rss+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
83                 self::assertEquals(
84                         '<?xml version="1.0"?>' . "\n" .
85                         '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
86                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
87                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
88                         '  <error>error_message</error>' . "\n" .
89                         '  <code>200 OK</code>' . "\n" .
90                         '  <request/>' . "\n" .
91                         '</status>' . "\n",
92                         $response->getContent());
93         }
94
95         public function testErrorWithAtom()
96         {
97                 $l10n = \Mockery::mock(L10n::class);
98                 $args = \Mockery::mock(Arguments::class);
99                 $args->shouldReceive('getQueryString')->andReturn('');
100                 $baseUrl     = \Mockery::mock(BaseURL::class);
101                 $twitterUser = \Mockery::mock(User::class);
102
103                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
104                 $response->error(200, 'OK', 'error_message', 'atom');
105
106                 self::assertEquals(['Content-type' => 'application/atom+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
107                 self::assertEquals(
108                         '<?xml version="1.0"?>' . "\n" .
109                         '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
110                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
111                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
112                         '  <error>error_message</error>' . "\n" .
113                         '  <code>200 OK</code>' . "\n" .
114                         '  <request/>' . "\n" .
115                         '</status>' . "\n",
116                         $response->getContent());
117         }
118
119         public function testUnsupported()
120         {
121                 $l10n = \Mockery::mock(L10n::class);
122                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
123                         return $args;
124                 });
125                 $args = \Mockery::mock(Arguments::class);
126                 $args->shouldReceive('getQueryString')->andReturn('');
127                 $baseUrl     = \Mockery::mock(BaseURL::class);
128                 $twitterUser = \Mockery::mock(User::class);
129
130                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
131                 $response->unsupported();
132
133                 self::assertEquals('{"error":"API endpoint %s %s is not implemented but might be in the future.","code":"501 Not Implemented","request":""}', $response->getContent());
134         }
135
136         /**
137          * Test the BaseApi::reformatXML() function.
138          *
139          * @return void
140          */
141         public function testApiReformatXml()
142         {
143                 $item = true;
144                 $key  = '';
145                 self::assertTrue(ApiResponse::reformatXML($item, $key));
146                 self::assertEquals('true', $item);
147         }
148
149         /**
150          * Test the BaseApi::reformatXML() function with a statusnet_api key.
151          *
152          * @return void
153          */
154         public function testApiReformatXmlWithStatusnetKey()
155         {
156                 $item = '';
157                 $key  = 'statusnet_api';
158                 self::assertTrue(ApiResponse::reformatXML($item, $key));
159                 self::assertEquals('statusnet:api', $key);
160         }
161
162         /**
163          * Test the BaseApi::reformatXML() function with a friendica_api key.
164          *
165          * @return void
166          */
167         public function testApiReformatXmlWithFriendicaKey()
168         {
169                 $item = '';
170                 $key  = 'friendica_api';
171                 self::assertTrue(ApiResponse::reformatXML($item, $key));
172                 self::assertEquals('friendica:api', $key);
173         }
174
175         /**
176          * Test the BaseApi::createXML() function.
177          *
178          * @return void
179          */
180         public function testApiCreateXml()
181         {
182                 $l10n = \Mockery::mock(L10n::class);
183                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
184                         return $args;
185                 });
186                 $args = \Mockery::mock(Arguments::class);
187                 $args->shouldReceive('getQueryString')->andReturn('');
188                 $baseUrl     = \Mockery::mock(BaseURL::class);
189                 $twitterUser = \Mockery::mock(User::class);
190
191                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
192
193                 self::assertEquals(
194                         '<?xml version="1.0"?>' . "\n" .
195                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
196                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
197                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
198                         '  <data>some_data</data>' . "\n" .
199                         '</root_element>' . "\n",
200                         $response->createXML(['data' => ['some_data']], 'root_element')
201                 );
202         }
203
204         /**
205          * Test the BaseApi::createXML() function without any XML namespace.
206          *
207          * @return void
208          */
209         public function testApiCreateXmlWithoutNamespaces()
210         {
211                 $l10n = \Mockery::mock(L10n::class);
212                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
213                         return $args;
214                 });
215                 $args = \Mockery::mock(Arguments::class);
216                 $args->shouldReceive('getQueryString')->andReturn('');
217                 $baseUrl     = \Mockery::mock(BaseURL::class);
218                 $twitterUser = \Mockery::mock(User::class);
219
220                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
221
222                 self::assertEquals(
223                         '<?xml version="1.0"?>' . "\n" .
224                         '<ok>' . "\n" .
225                         '  <data>some_data</data>' . "\n" .
226                         '</ok>' . "\n",
227                         $response->createXML(['data' => ['some_data']], 'ok')
228                 );
229         }
230
231         /**
232          * Test the BaseApi::formatData() function.
233          *
234          * @return void
235          */
236         public function testApiFormatData()
237         {
238                 $l10n = \Mockery::mock(L10n::class);
239                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
240                         return $args;
241                 });
242                 $args = \Mockery::mock(Arguments::class);
243                 $args->shouldReceive('getQueryString')->andReturn('');
244                 $baseUrl     = \Mockery::mock(BaseURL::class);
245                 $twitterUser = \Mockery::mock(User::class);
246
247                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
248
249                 $data = ['some_data'];
250                 self::assertEquals($data, $response->formatData('root_element', 'json', $data));
251         }
252
253         /**
254          * Test the BaseApi::formatData() function with an XML result.
255          *
256          * @return void
257          */
258         public function testApiFormatDataWithXml()
259         {
260                 $l10n = \Mockery::mock(L10n::class);
261                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
262                         return $args;
263                 });
264                 $args = \Mockery::mock(Arguments::class);
265                 $args->shouldReceive('getQueryString')->andReturn('');
266                 $baseUrl     = \Mockery::mock(BaseURL::class);
267                 $twitterUser = \Mockery::mock(User::class);
268
269                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
270
271                 self::assertEquals(
272                         '<?xml version="1.0"?>' . "\n" .
273                         '<root_element xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
274                         'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
275                         'xmlns:georss="http://www.georss.org/georss">' . "\n" .
276                         '  <data>some_data</data>' . "\n" .
277                         '</root_element>' . "\n",
278                         $response->formatData('root_element', 'xml', ['data' => ['some_data']])
279                 );
280         }
281
282         /**
283          * Test the api_rss_extra() function.
284          *
285          * @return void
286          */
287         public function testApiRssExtra()
288         {
289                 self::markTestIncomplete('Cannot mock it yet.');
290
291                 /*
292                 $user_info = ['url' => 'user_url', 'lang' => 'en'];
293                 $userMock = \Mockery::mock(\Friendica\Object\Api\Twitter\User::class);
294                 $userMock->shouldReceive('toArray')->andReturn($user_info);
295
296                 $l10n = \Mockery::mock(L10n::class);
297                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) {
298                         return $args;
299                 });
300                 $args = \Mockery::mock(Arguments::class);
301                 $args->shouldReceive('getQueryString')->andReturn('');
302                 $baseUrl = \Mockery::mock(BaseURL::class);
303                 $baseUrl->shouldReceive('__toString')->andReturn('https://friendica.local');
304                 $twitterUser = \Mockery::mock(User::class);
305                 $twitterUser->shouldReceive('createFromContactId')->with(1)->andReturn($userMock);
306
307                 $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
308
309                 $result = $response->formatData('root_element', 'rss', ['data' => ['some_data']], 1);
310
311                 print_r($result);
312
313                 self::assertEquals($user_info, $result['$user']);
314                 self::assertEquals($user_info['url'], $result['$rss']['alternate']);
315                 self::assertArrayHasKey('self', $result['$rss']);
316                 self::assertArrayHasKey('base', $result['$rss']);
317                 self::assertArrayHasKey('updated', $result['$rss']);
318                 self::assertArrayHasKey('atom_updated', $result['$rss']);
319                 self::assertArrayHasKey('language', $result['$rss']);
320                 self::assertArrayHasKey('logo', $result['$rss']);
321                 */
322         }
323
324         /**
325          * Test the api_rss_extra() function without any user info.
326          *
327          * @return void
328          */
329         public function testApiRssExtraWithoutUserInfo()
330         {
331                 self::markTestIncomplete('Cannot mock it yet.');
332
333                 /*
334                 $result = api_rss_extra([], null);
335                 self::assertIsArray($result['$user']);
336                 self::assertArrayHasKey('alternate', $result['$rss']);
337                 self::assertArrayHasKey('self', $result['$rss']);
338                 self::assertArrayHasKey('base', $result['$rss']);
339                 self::assertArrayHasKey('updated', $result['$rss']);
340                 self::assertArrayHasKey('atom_updated', $result['$rss']);
341                 self::assertArrayHasKey('language', $result['$rss']);
342                 self::assertArrayHasKey('logo', $result['$rss']);
343                 */
344         }
345 }