From 6725f05ab203ea6a1425f7b023dc61b017da1ee2 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 12 Nov 2021 21:35:21 +0100 Subject: [PATCH] Moved API\Notification tests --- src/Module/Api/ApiResponse.php | 36 ++++++++-- tests/Util/ApiResponseDouble.php | 54 ++++++++++++++ tests/legacy/ApiTest.php | 71 ------------------- tests/src/Module/Api/ApiTest.php | 12 +++- .../Module/Api/Friendica/NotificationTest.php | 60 ++++++++++++++++ .../Module/Api/Friendica/Photo/DeleteTest.php | 4 +- .../Api/Friendica/Photoalbum/DeleteTest.php | 4 +- .../Api/Friendica/Photoalbum/UpdateTest.php | 6 +- 8 files changed, 161 insertions(+), 86 deletions(-) create mode 100644 tests/Util/ApiResponseDouble.php create mode 100644 tests/src/Module/Api/Friendica/NotificationTest.php diff --git a/src/Module/Api/ApiResponse.php b/src/Module/Api/ApiResponse.php index 82801f2e60..d0d03da994 100644 --- a/src/Module/Api/ApiResponse.php +++ b/src/Module/Api/ApiResponse.php @@ -35,6 +35,29 @@ class ApiResponse $this->logger = $logger; } + /** + * Sets header directly + * mainly used to override it for tests + * + * @param string $header + */ + protected function setHeader(string $header) + { + header($header); + } + + /** + * Prints output directly to the caller + * mainly used to override it for tests + * + * @param string $output + */ + protected function printOutput(string $output) + { + echo $output; + exit; + } + /** * Creates the XML from a JSON style array * @@ -143,7 +166,7 @@ class ApiResponse 'request' => $this->args->getQueryString() ]; - header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description); + $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description); $this->exit('status', ['status' => $error], $format); } @@ -165,10 +188,10 @@ class ApiResponse switch ($format) { case 'xml': - header('Content-Type: text/xml'); + $this->setHeader('Content-Type: text/xml'); break; case 'json': - header('Content-Type: application/json'); + $this->setHeader('Content-Type: application/json'); if (!empty($return)) { $json = json_encode(end($return)); if (!empty($_GET['callback'])) { @@ -178,17 +201,16 @@ class ApiResponse } break; case 'rss': - header('Content-Type: application/rss+xml'); + $this->setHeader('Content-Type: application/rss+xml'); $return = '' . "\n" . $return; break; case 'atom': - header('Content-Type: application/atom+xml'); + $this->setHeader('Content-Type: application/atom+xml'); $return = '' . "\n" . $return; break; } - echo $return; - exit; + $this->printOutput($return); } /** diff --git a/tests/Util/ApiResponseDouble.php b/tests/Util/ApiResponseDouble.php new file mode 100644 index 0000000000..c931f2bec4 --- /dev/null +++ b/tests/Util/ApiResponseDouble.php @@ -0,0 +1,54 @@ +markTestIncomplete(); } - /** - * Test the api_friendica_notification() function. - * - * @return void - */ - public function testApiFriendicaNotification() - { - // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - // api_friendica_notification('json'); - } - - /** - * Test the api_friendica_notification() function without an authenticated user. - * - * @return void - */ - public function testApiFriendicaNotificationWithoutAuthenticatedUser() - { - // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class); - // $_SESSION['authenticated'] = false; - // api_friendica_notification('json'); - } - - /** - * Test the api_friendica_notification() function with empty result - * - * @return void - */ - public function testApiFriendicaNotificationWithEmptyResult() - { - // DI::args()->setArgv(['api', 'friendica', 'notification']); - // $_SESSION['uid'] = 41; - // $result = api_friendica_notification('json'); - // self::assertEquals(['note' => false], $result); - } - - /** - * Test the api_friendica_notification() function with an XML result. - * - * @return void - */ - public function testApiFriendicaNotificationWithXmlResult() - { - /* - DI::args()->setArgv(['api', 'friendica', 'notification']); - $result = api_friendica_notification('xml'); - $date = DateTimeFormat::local('2020-01-01 12:12:02'); - $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02'); - - $assertXml=<< - - - -XML; - self::assertXmlStringEqualsXmlString($assertXml, $result); - */ - } - - /** - * Test the api_friendica_notification() function with an JSON result. - * - * @return void - */ - public function testApiFriendicaNotificationWithJsonResult() - { - // DI::args()->setArgv(['api', 'friendica', 'notification']); - // $result = json_encode(api_friendica_notification('json')); - // self::assertJson($result); - } - /** * Test the api_friendica_notification_seen() function. * diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php index b2c38c31e6..95d04041f9 100644 --- a/tests/src/Module/Api/ApiTest.php +++ b/tests/src/Module/Api/ApiTest.php @@ -7,8 +7,10 @@ use Friendica\Core\Addon; use Friendica\Core\Hook; use Friendica\Database\Database; use Friendica\DI; +use Friendica\Module\Api\ApiResponse; use Friendica\Security\Authentication; use Friendica\Test\FixtureTest; +use Friendica\Test\Util\ApiResponseDouble; use Friendica\Test\Util\AuthenticationDouble; class ApiTest extends FixtureTest @@ -18,12 +20,20 @@ class ApiTest extends FixtureTest parent::setUp(); // TODO: Change the autogenerated stub $this->dice = $this->dice - ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]); + ->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true]) + ->addRule(ApiResponse::class, ['instanceOf' => ApiResponseDouble::class, 'shared' => true]); DI::init($this->dice); $this->installAuthTest(); } + protected function tearDown(): void + { + ApiResponseDouble::reset(); + + parent::tearDown(); + } + /** * installs auththest. * diff --git a/tests/src/Module/Api/Friendica/NotificationTest.php b/tests/src/Module/Api/Friendica/NotificationTest.php new file mode 100644 index 0000000000..d9451d0bf9 --- /dev/null +++ b/tests/src/Module/Api/Friendica/NotificationTest.php @@ -0,0 +1,60 @@ +expectException(BadRequestException::class); + DI::session()->set('uid', ''); + + Notification::rawContent(); + } + + public function testWithoutAuthenticatedUser() + { + self::markTestIncomplete('Needs BasicAuth as dynamic method for overriding first'); + + $this->expectException(BadRequestException::class); + DI::session()->set('uid', 41); + + Notification::rawContent(); + } + + public function testWithXmlResult() + { + $date = DateTimeFormat::local('2020-01-01 12:12:02'); + $dateRel = Temporal::getRelativeDate('2020-01-01 07:12:02'); + + $assertXml = << + + + +XML; + + Notification::rawContent(['extension' => 'xml']); + + self::assertXmlStringEqualsXmlString($assertXml, ApiResponseDouble::getOutput()); + } + + public function testWithJsonResult() + { + Notification::rawContent(['parameter' => 'json']); + + $result = json_encode(ApiResponseDouble::getOutput()); + + self::assertJson($result); + } +} diff --git a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php index a47c87e98a..5023cd6c16 100644 --- a/tests/src/Module/Api/Friendica/Photo/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photo/DeleteTest.php @@ -10,7 +10,7 @@ class DeleteTest extends ApiTest { public function testEmpty() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Delete::rawContent(); } @@ -21,7 +21,7 @@ class DeleteTest extends ApiTest public function testWrong() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Delete::rawContent(['photo_id' => 1]); } diff --git a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php index ed9cd11578..a8f07d5aa1 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/DeleteTest.php @@ -10,13 +10,13 @@ class DeleteTest extends ApiTest { public function testEmpty() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Delete::rawContent(); } public function testWrong() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Delete::rawContent(['album' => 'album_name']); } diff --git a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php index 93c1cdc866..2e4de7a798 100644 --- a/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php +++ b/tests/src/Module/Api/Friendica/Photoalbum/UpdateTest.php @@ -10,19 +10,19 @@ class UpdateTest extends ApiTest { public function testEmpty() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Update::rawContent(); } public function testTooFewArgs() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Update::rawContent(['album' => 'album_name']); } public function testWrongUpdate() { - self::expectException(BadRequestException::class); + $this->expectException(BadRequestException::class); Update::rawContent(['album' => 'album_name', 'album_new' => 'album_name']); }