From 5ec4708ced1cdca01eddd7e52377ab5e5f8b3290 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 20 May 2020 10:47:20 +0200 Subject: [PATCH] Process OpenSearch controller through Slim Also it was missing on the default template feeds --- .../controllers/OpenSearchController.php | 28 ++++++ index.php | 5 +- .../controller/OpenSearchControllerTest.php | 92 +++++++++++++++++++ tpl/default/feed.atom.html | 2 + tpl/default/feed.rss.html | 4 +- tpl/default/includes.html | 3 +- tpl/vintage/feed.atom.html | 4 +- tpl/vintage/feed.rss.html | 2 +- tpl/vintage/includes.html | 3 +- 9 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 application/front/controllers/OpenSearchController.php create mode 100644 tests/front/controller/OpenSearchControllerTest.php diff --git a/application/front/controllers/OpenSearchController.php b/application/front/controllers/OpenSearchController.php new file mode 100644 index 00000000..fa32c5f1 --- /dev/null +++ b/application/front/controllers/OpenSearchController.php @@ -0,0 +1,28 @@ +withHeader('Content-Type', 'application/opensearchdescription+xml; charset=utf-8'); + + $this->assignView('serverurl', index_url($this->container->environment)); + + return $response->write($this->render('opensearch')); + } +} diff --git a/index.php b/index.php index c3e0a5bf..04ec0d73 100644 --- a/index.php +++ b/index.php @@ -439,9 +439,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM // Display opensearch plugin (XML) if ($targetPage == Router::$PAGE_OPENSEARCH) { - header('Content-Type: application/xml; charset=utf-8'); - $PAGE->assign('serverurl', index_url($_SERVER)); - $PAGE->renderPage('opensearch'); + header('Location: ./open-search'); exit; } @@ -1575,6 +1573,7 @@ $app->group('', function () { $this->get('/daily-rss', '\Shaarli\Front\Controller\DailyController:rss')->setName('dailyrss'); $this->get('/feed-atom', '\Shaarli\Front\Controller\FeedController:atom')->setName('feedatom'); $this->get('/feed-rss', '\Shaarli\Front\Controller\FeedController:rss')->setName('feedrss'); + $this->get('/open-search', '\Shaarli\Front\Controller\OpenSearchController:index')->setName('opensearch'); $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag'); })->add('\Shaarli\Front\ShaarliMiddleware'); diff --git a/tests/front/controller/OpenSearchControllerTest.php b/tests/front/controller/OpenSearchControllerTest.php new file mode 100644 index 00000000..7ba0f7df --- /dev/null +++ b/tests/front/controller/OpenSearchControllerTest.php @@ -0,0 +1,92 @@ +container = $this->createMock(ShaarliContainer::class); + $this->controller = new OpenSearchController($this->container); + } + + public function testOpenSearchController(): void + { + $this->createValidContainerMockSet(); + + $request = $this->createMock(Request::class); + $response = new Response(); + + // Save RainTPL assigned variables + $assignedVariables = []; + $this->assignTemplateVars($assignedVariables); + + $result = $this->controller->index($request, $response); + + static::assertSame(200, $result->getStatusCode()); + static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]); + static::assertSame('opensearch', (string) $result->getBody()); + static::assertSame('http://shaarli', $assignedVariables['serverurl']); + } + + protected function createValidContainerMockSet(): void + { + $loginManager = $this->createMock(LoginManager::class); + $this->container->loginManager = $loginManager; + + // PageBuilder + $pageBuilder = $this->createMock(PageBuilder::class); + $pageBuilder + ->method('render') + ->willReturnCallback(function (string $template): string { + return $template; + }) + ; + $this->container->pageBuilder = $pageBuilder; + + $bookmarkService = $this->createMock(BookmarkServiceInterface::class); + $this->container->bookmarkService = $bookmarkService; + + // Plugin Manager + $pluginManager = $this->createMock(PluginManager::class); + $this->container->pluginManager = $pluginManager; + + // $_SERVER + $this->container->environment = [ + 'SERVER_NAME' => 'shaarli', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/open-search', + ]; + } + + protected function assignTemplateVars(array &$variables): void + { + $this->container->pageBuilder + ->expects(static::atLeastOnce()) + ->method('assign') + ->willReturnCallback(function ($key, $value) use (&$variables) { + $variables[$key] = $value; + + return $this; + }) + ; + } +} diff --git a/tpl/default/feed.atom.html b/tpl/default/feed.atom.html index bcfa7012..dd58bd1e 100644 --- a/tpl/default/feed.atom.html +++ b/tpl/default/feed.atom.html @@ -6,6 +6,8 @@ {$last_update} {/if} + {loop="$plugins_feed_header"} {$value} {/loop} diff --git a/tpl/default/feed.rss.html b/tpl/default/feed.rss.html index 66d9a869..85cec7f3 100644 --- a/tpl/default/feed.rss.html +++ b/tpl/default/feed.rss.html @@ -7,7 +7,9 @@ {$language} {$index_url} Shaarli - + +