From 9cc779a717fad1389b7b2923ae407b9996735053 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 7 Feb 2021 13:04:44 -0500 Subject: [PATCH] Import/export tests --- lib/REST/Miniflux/V1.php | 6 ++--- tests/cases/REST/Miniflux/TestV1.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index db2b40d9..ee9c3329 100644 --- a/lib/REST/Miniflux/V1.php +++ b/lib/REST/Miniflux/V1.php @@ -263,7 +263,7 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { } if ($reqBody) { if ($func === "opmlImport") { - $args[] = (string) $req->getBody(); + $data = (string) $req->getBody(); } else { $data = (string) $req->getBody(); if (strlen($data)) { @@ -1188,14 +1188,14 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { case 10613: return new ErrorResponse("InvalidImportCategory", 422); case 10614: - return new ErrorResponse("DuplicateImportCatgory", 422); + return new ErrorResponse("DuplicateImportCategory", 422); case 10615: return new ErrorResponse("InvalidImportLabel", 422); } } catch (FeedException $e) { return new ErrorResponse(["FailedImportFeed", 'url' => $e->getParams()['url'], 'code' => $e->getCode()], 502); } - return new Response(['message' => Arsse::$lang->msg("ImportSuccess")]); + return new Response(['message' => Arsse::$lang->msg("API.Miniflux.ImportSuccess")]); } protected function opmlExport(): ResponseInterface { diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index fc08dea4..6466fba3 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/tests/cases/REST/Miniflux/TestV1.php @@ -16,12 +16,15 @@ use JKingWeb\Arsse\Misc\Date; use JKingWeb\Arsse\REST\Miniflux\V1; use JKingWeb\Arsse\REST\Miniflux\ErrorResponse; use JKingWeb\Arsse\Feed\Exception as FeedException; +use JKingWeb\Arsse\ImportExport\Exception as ImportException; +use JKingWeb\Arsse\ImportExport\OPML; use JKingWeb\Arsse\User\ExceptionConflict; use JKingWeb\Arsse\User\ExceptionInput as UserExceptionInput; use Psr\Http\Message\ResponseInterface; use Laminas\Diactoros\Response\JsonResponse as Response; use Laminas\Diactoros\Response\EmptyResponse; use JKingWeb\Arsse\Test\Result; +use Laminas\Diactoros\Response\TextResponse; /** @covers \JKingWeb\Arsse\REST\Miniflux\V1 */ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { @@ -934,4 +937,38 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { public function testRefreshAllFeeds(): void { $this->assertMessage(new EmptyResponse(204), $this->req("PUT", "/feeds/refresh")); } + + /** @dataProvider provideImports */ + public function testImport($out, ResponseInterface $exp): void { + $opml = \Phake::mock(OPML::class); + \Phake::when(Arsse::$obj)->get(OPML::class)->thenReturn($opml); + if ($out instanceof \Exception) { + \Phake::when($opml)->import->thenThrow($out); + } else { + \Phake::when($opml)->import->thenReturn($out); + } + $this->assertMessage($exp, $this->req("POST", "/import", "IMPORT DATA")); + \Phake::verify($opml)->import(Arsse::$user->id, "IMPORT DATA"); + } + + public function provideImports(): iterable { + self::clearData(); + return [ + [new ImportException("invalidSyntax"), new ErrorResponse("InvalidBodyXML", 400)], + [new ImportException("invalidSemantics"), new ErrorResponse("InvalidBodyOPML", 422)], + [new ImportException("invalidFolderName"), new ErrorResponse("InvalidImportCategory", 422)], + [new ImportException("invalidFolderCopy"), new ErrorResponse("DuplicateImportCategory", 422)], + [new ImportException("invalidTagName"), new ErrorResponse("InvalidImportLabel", 422)], + [new FeedException("invalidUrl", ['url' => "http://example.com/"]), new ErrorResponse(["FailedImportFeed", 'url' => "http://example.com/", 'code' => 10502], 502)], + [true, new Response(['message' => Arsse::$lang->msg("API.Miniflux.ImportSuccess")])], + ]; + } + + public function testExport(): void { + $opml = \Phake::mock(OPML::class); + \Phake::when(Arsse::$obj)->get(OPML::class)->thenReturn($opml); + \Phake::when($opml)->export->thenReturn("EXPORT DATA"); + $this->assertMessage(new TextResponse("EXPORT DATA", 200, ['Content-Type' => "application/xml"]), $this->req("GET", "/export")); + \Phake::verify($opml)->export(Arsse::$user->id); + } }