--- /dev/null
+<?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'));
+ }
+}
// 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;
}
$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');
--- /dev/null
+<?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;
+ })
+ ;
+ }
+}
<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}
<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}
{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}" />
<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}
<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}
<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}" />