]> git.mxchange.org Git - friendica.git/commitdiff
Migrate API\error tests and fix ApiResponse for RSS/atom
authorPhilipp <admin@philipp.info>
Fri, 12 Nov 2021 21:29:58 +0000 (22:29 +0100)
committerPhilipp <admin@philipp.info>
Fri, 12 Nov 2021 21:29:58 +0000 (22:29 +0100)
src/Module/Api/ApiResponse.php
tests/legacy/ApiTest.php
tests/src/Module/Api/ApiResponseTest.php [new file with mode: 0644]

index d0d03da9943a84d20b02682e3f04321f622f183b..c0ce7c756b335b35c2ce3bc505c601c5ff735a26 100644 (file)
@@ -202,11 +202,9 @@ class ApiResponse
                                break;
                        case 'rss':
                                $this->setHeader('Content-Type: application/rss+xml');
-                               $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                break;
                        case 'atom':
                                $this->setHeader('Content-Type: application/atom+xml');
-                               $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                break;
                }
 
index 9bcf2e11649daf0cbc8316bcf793d4b9da59ae8f..73ea1b332726f32d908318ebb5803cdfa31044a7 100644 (file)
@@ -622,30 +622,6 @@ class ApiTest extends FixtureTest
                );
        }
 
-       /**
-        * Test the api_call() function with an unallowed method.
-        *
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiCallWithWrongMethod()
-       {
-               // Shouldn't be needed anymore due to the router?
-               /*
-               global $API;
-               $API['api_path'] = ['method' => 'method'];
-
-               $_SERVER['QUERY_STRING'] = 'pagename=api_path';
-
-               $args = DI::args()->determine($_SERVER, $_GET);
-
-               self::assertEquals(
-                       '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}',
-                       api_call($this->app, $args)
-               );
-               */
-       }
-
        /**
         * Test the api_call() function with an unauthorized user.
         *
@@ -674,92 +650,6 @@ class ApiTest extends FixtureTest
                */
        }
 
-       /**
-        * Test the api_error() function with a JSON result.
-        *
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiErrorWithJson()
-       {
-               // @todo How to test the new API?
-               // self::assertEquals(
-               //      '{"status":{"error":"error_message","code":"200 OK","request":""}}',
-               //      api_error('json', new HTTPException\OKException('error_message'), DI::args())
-               // );
-       }
-
-       /**
-        * Test the api_error() function with an XML result.
-        *
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiErrorWithXml()
-       {
-               // @todo How to test the new API?
-               /*
-               self::assertEquals(
-                       '<?xml version="1.0"?>' . "\n" .
-                       '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
-                       'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
-                       'xmlns:georss="http://www.georss.org/georss">' . "\n" .
-                       '  <error>error_message</error>' . "\n" .
-                       '  <code>200 OK</code>' . "\n" .
-                       '  <request/>' . "\n" .
-                       '</status>' . "\n",
-                       api_error('xml', new HTTPException\OKException('error_message'), DI::args())
-               );
-               */
-       }
-
-       /**
-        * Test the api_error() function with an RSS result.
-        *
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiErrorWithRss()
-       {
-               // @todo How to test the new API?
-               /*
-               self::assertEquals(
-                       '<?xml version="1.0"?>' . "\n" .
-                       '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
-                       'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
-                       'xmlns:georss="http://www.georss.org/georss">' . "\n" .
-                       '  <error>error_message</error>' . "\n" .
-                       '  <code>200 OK</code>' . "\n" .
-                       '  <request/>' . "\n" .
-                       '</status>' . "\n",
-                       api_error('rss', new HTTPException\OKException('error_message'), DI::args())
-               );
-               */
-       }
-
-       /**
-        * Test the api_error() function with an Atom result.
-        *
-        * @runInSeparateProcess
-        * @preserveGlobalState disabled
-        */
-       public function testApiErrorWithAtom()
-       {
-               // @todo How to test the new API?
-               /*
-               self::assertEquals(
-                       '<?xml version="1.0"?>' . "\n" .
-                       '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
-                       'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
-                       'xmlns:georss="http://www.georss.org/georss">' . "\n" .
-                       '  <error>error_message</error>' . "\n" .
-                       '  <code>200 OK</code>' . "\n" .
-                       '  <request/>' . "\n" .
-                       '</status>' . "\n",
-                       api_error('atom', new HTTPException\OKException('error_message'), DI::args())
-               );
-               */
-       }
 
        /**
         * Test the api_rss_extra() function.
diff --git a/tests/src/Module/Api/ApiResponseTest.php b/tests/src/Module/Api/ApiResponseTest.php
new file mode 100644 (file)
index 0000000..7499e9a
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+
+namespace Friendica\Test\src\Module\Api;
+
+use Friendica\App\Arguments;
+use Friendica\Core\L10n;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\ApiResponseDouble;
+use Psr\Log\NullLogger;
+
+class ApiResponseTest extends MockedTest
+{
+       protected function tearDown(): void
+       {
+               ApiResponseDouble::reset();
+
+               parent::tearDown();
+       }
+
+       public function testErrorWithJson()
+       {
+               $l10n = \Mockery::mock(L10n::class);
+               $args = \Mockery::mock(Arguments::class);
+               $args->shouldReceive('getQueryString')->andReturn('');
+
+               $response = new ApiResponseDouble($l10n, $args, new NullLogger());
+               $response->error(200, 'OK', 'error_message', 'json');
+
+               self::assertEquals('{"error":"error_message","code":"200 OK","request":""}', ApiResponseDouble::getOutput());
+       }
+
+       public function testErrorWithXml()
+       {
+               $l10n = \Mockery::mock(L10n::class);
+               $args = \Mockery::mock(Arguments::class);
+               $args->shouldReceive('getQueryString')->andReturn('');
+
+               $response = new ApiResponseDouble($l10n, $args, new NullLogger());
+               $response->error(200, 'OK', 'error_message', 'xml');
+
+               self::assertEquals('<?xml version="1.0"?>' . "\n" .
+                                                  '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
+                                                  'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
+                                                  'xmlns:georss="http://www.georss.org/georss">' . "\n" .
+                                                  '  <error>error_message</error>' . "\n" .
+                                                  '  <code>200 OK</code>' . "\n" .
+                                                  '  <request/>' . "\n" .
+                                                  '</status>' . "\n",
+                       ApiResponseDouble::getOutput());
+       }
+
+       public function testErrorWithRss()
+       {
+               $l10n = \Mockery::mock(L10n::class);
+               $args = \Mockery::mock(Arguments::class);
+               $args->shouldReceive('getQueryString')->andReturn('');
+
+               $response = new ApiResponseDouble($l10n, $args, new NullLogger());
+               $response->error(200, 'OK', 'error_message', 'rss');
+
+               self::assertEquals(
+                       '<?xml version="1.0"?>' . "\n" .
+                       '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
+                       'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
+                       'xmlns:georss="http://www.georss.org/georss">' . "\n" .
+                       '  <error>error_message</error>' . "\n" .
+                       '  <code>200 OK</code>' . "\n" .
+                       '  <request/>' . "\n" .
+                       '</status>' . "\n",
+                       ApiResponseDouble::getOutput());
+       }
+
+       public function testErrorWithAtom()
+       {
+               $l10n = \Mockery::mock(L10n::class);
+               $args = \Mockery::mock(Arguments::class);
+               $args->shouldReceive('getQueryString')->andReturn('');
+
+               $response = new ApiResponseDouble($l10n, $args, new NullLogger());
+               $response->error(200, 'OK', 'error_message', 'atom');
+
+               self::assertEquals(
+                       '<?xml version="1.0"?>' . "\n" .
+                       '<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
+                       'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
+                       'xmlns:georss="http://www.georss.org/georss">' . "\n" .
+                       '  <error>error_message</error>' . "\n" .
+                       '  <code>200 OK</code>' . "\n" .
+                       '  <request/>' . "\n" .
+                       '</status>' . "\n",
+                       ApiResponseDouble::getOutput());
+       }
+}