]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Process OpenSearch controller through Slim
authorArthurHoaro <arthur@hoa.ro>
Wed, 20 May 2020 08:47:20 +0000 (10:47 +0200)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
Also it was missing on the default template feeds

application/front/controllers/OpenSearchController.php [new file with mode: 0644]
index.php
tests/front/controller/OpenSearchControllerTest.php [new file with mode: 0644]
tpl/default/feed.atom.html
tpl/default/feed.rss.html
tpl/default/includes.html
tpl/vintage/feed.atom.html
tpl/vintage/feed.rss.html
tpl/vintage/includes.html

diff --git a/application/front/controllers/OpenSearchController.php b/application/front/controllers/OpenSearchController.php
new file mode 100644 (file)
index 0000000..fa32c5f
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+/**
+ * Class OpenSearchController
+ *
+ * Slim controller used to render open search template.
+ * This allows to add Shaarli as a search engine within the browser.
+ *
+ * @package front\controllers
+ */
+class OpenSearchController extends ShaarliController
+{
+    public function index(Request $request, Response $response): Response
+    {
+        $response = $response->withHeader('Content-Type', 'application/opensearchdescription+xml; charset=utf-8');
+
+        $this->assignView('serverurl', index_url($this->container->environment));
+
+        return $response->write($this->render('opensearch'));
+    }
+}
index c3e0a5bf9c6a131bbcc2f631b7ecc8b235d76877..04ec0d731358d147eccec5860f9b34eceef4d39a 100644 (file)
--- 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 (file)
index 0000000..7ba0f7d
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+declare(strict_types=1);
+
+namespace front\controller;
+
+use PHPUnit\Framework\TestCase;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Container\ShaarliContainer;
+use Shaarli\Front\Controller\OpenSearchController;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Security\LoginManager;
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+class OpenSearchControllerTest extends TestCase
+{
+    /** @var ShaarliContainer */
+    protected $container;
+
+    /** @var OpenSearchController */
+    protected $controller;
+
+    public function setUp(): void
+    {
+        $this->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;
+            })
+        ;
+    }
+}
index bcfa7012dbe2ca93924e03bcaeab8ba070a0d149..dd58bd1e5196e5e09514a2787cb78647dd6ef699 100644 (file)
@@ -6,6 +6,8 @@
     <updated>{$last_update}</updated>
   {/if}
   <link rel="self" href="{$self_link}#" />
+  <link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
+        title="Shaarli search - {$shaarlititle}" />
   {loop="$plugins_feed_header"}
     {$value}
   {/loop}
index 66d9a8697b1ca9647305559c2da34f4a64d5ca14..85cec7f382ecf24e7a6ea23834ed448cff250605 100644 (file)
@@ -7,7 +7,9 @@
     <language>{$language}</language>
     <copyright>{$index_url}</copyright>
     <generator>Shaarli</generator>
-    <atom:link rel="self" href="{$self_link}"  />
+    <atom:link rel="self" href="{$self_link}" />
+    <atom:link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
+               title="Shaarli search - {$shaarlititle}" />
     {loop="$plugins_feed_header"}
       {$value}
     {/loop}
index cdbfeea1962fa49bd052663167692f5ed8487884..07a487bb03ae0249d64ecf24a98664444218307c 100644 (file)
@@ -17,7 +17,8 @@
 {if="is_file('data/user.css')"}
   <link type="text/css" rel="stylesheet" href="data/user.css#" />
 {/if}
-<link rel="search" type="application/opensearchdescription+xml" href="./?do=opensearch#" title="Shaarli search - {$shaarlititle}"/>
+<link rel="search" type="application/opensearchdescription+xml" href="./open-search#"
+      title="Shaarli search - {$shaarlititle}" />
 {if="! empty($links) && count($links) === 1"}
   {$link=reset($links)}
   <meta property="og:title" content="{$link.title}" />
index 0621cb9e456d105e8769275c7fd5fb7a10454259..5919bb4956210ddf152b2df4d8bc0dad067db26e 100644 (file)
@@ -6,8 +6,8 @@
     <updated>{$last_update}</updated>
   {/if}
   <link rel="self" href="{$self_link}#" />
-  <link rel="search" type="application/opensearchdescription+xml" href="{$index_url}?do=opensearch#"
-             title="Shaarli search - {$shaarlititle}" />
+  <link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
+        title="Shaarli search - {$shaarlititle}" />
   {loop="$feed_plugins_header"}
     {$value}
   {/loop}
index ee3fef880de3b7e931d4f04968d4cb3ca0403706..4be8202f57ced7cb19fbbd470b38fa70f857f843 100644 (file)
@@ -8,7 +8,7 @@
     <copyright>{$index_url}</copyright>
     <generator>Shaarli</generator>
     <atom:link rel="self" href="{$self_link}"  />
-    <atom:link rel="search" type="application/opensearchdescription+xml" href="{$index_url}?do=opensearch#"
+    <atom:link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
                title="Shaarli search - {$shaarlititle}" />
     {loop="$feed_plugins_header"}
       {$value}
index cf56ca6103811d3b84ae44fbcce1281a56a82028..61448eafabeea49dc8a92c9410b477979cbce6d3 100644 (file)
@@ -14,7 +14,8 @@
 <link type="text/css" rel="stylesheet" href="{$value}#"/>
 {/loop}
 {if="is_file('data/user.css')"}<link type="text/css" rel="stylesheet" href="data/user.css#" />{/if}
-<link rel="search" type="application/opensearchdescription+xml" href="./?do=opensearch#" title="Shaarli search - {$shaarlititle|htmlspecialchars}"/>
+<link rel="search" type="application/opensearchdescription+xml" href="./open-search#"
+      title="Shaarli search - {$shaarlititle|htmlspecialchars}" />
 {if="! empty($links) && count($links) === 1"}
   {$link=reset($links)}
   <meta property="og:title" content="{$link.title}" />