Process OpenSearch controller through Slim

Also it was missing on the default template feeds
This commit is contained in:
ArthurHoaro 2020-05-20 10:47:20 +02:00
parent 7b2ba6ef82
commit 5ec4708ced
9 changed files with 134 additions and 9 deletions

View file

@ -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'));
}
}

View file

@ -439,9 +439,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
// Display opensearch plugin (XML) // Display opensearch plugin (XML)
if ($targetPage == Router::$PAGE_OPENSEARCH) { if ($targetPage == Router::$PAGE_OPENSEARCH) {
header('Content-Type: application/xml; charset=utf-8'); header('Location: ./open-search');
$PAGE->assign('serverurl', index_url($_SERVER));
$PAGE->renderPage('opensearch');
exit; exit;
} }
@ -1575,6 +1573,7 @@ function install($conf, $sessionManager, $loginManager)
$this->get('/daily-rss', '\Shaarli\Front\Controller\DailyController:rss')->setName('dailyrss'); $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-atom', '\Shaarli\Front\Controller\FeedController:atom')->setName('feedatom');
$this->get('/feed-rss', '\Shaarli\Front\Controller\FeedController:rss')->setName('feedrss'); $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'); $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
})->add('\Shaarli\Front\ShaarliMiddleware'); })->add('\Shaarli\Front\ShaarliMiddleware');

View file

@ -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;
})
;
}
}

View file

@ -6,6 +6,8 @@
<updated>{$last_update}</updated> <updated>{$last_update}</updated>
{/if} {/if}
<link rel="self" href="{$self_link}#" /> <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"} {loop="$plugins_feed_header"}
{$value} {$value}
{/loop} {/loop}

View file

@ -7,7 +7,9 @@
<language>{$language}</language> <language>{$language}</language>
<copyright>{$index_url}</copyright> <copyright>{$index_url}</copyright>
<generator>Shaarli</generator> <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"} {loop="$plugins_feed_header"}
{$value} {$value}
{/loop} {/loop}

View file

@ -17,7 +17,8 @@
{if="is_file('data/user.css')"} {if="is_file('data/user.css')"}
<link type="text/css" rel="stylesheet" href="data/user.css#" /> <link type="text/css" rel="stylesheet" href="data/user.css#" />
{/if} {/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"} {if="! empty($links) && count($links) === 1"}
{$link=reset($links)} {$link=reset($links)}
<meta property="og:title" content="{$link.title}" /> <meta property="og:title" content="{$link.title}" />

View file

@ -6,8 +6,8 @@
<updated>{$last_update}</updated> <updated>{$last_update}</updated>
{/if} {/if}
<link rel="self" href="{$self_link}#" /> <link rel="self" href="{$self_link}#" />
<link rel="search" type="application/opensearchdescription+xml" href="{$index_url}?do=opensearch#" <link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
title="Shaarli search - {$shaarlititle}" /> title="Shaarli search - {$shaarlititle}" />
{loop="$feed_plugins_header"} {loop="$feed_plugins_header"}
{$value} {$value}
{/loop} {/loop}

View file

@ -8,7 +8,7 @@
<copyright>{$index_url}</copyright> <copyright>{$index_url}</copyright>
<generator>Shaarli</generator> <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}?do=opensearch#" <atom:link rel="search" type="application/opensearchdescription+xml" href="{$index_url}open-search#"
title="Shaarli search - {$shaarlititle}" /> title="Shaarli search - {$shaarlititle}" />
{loop="$feed_plugins_header"} {loop="$feed_plugins_header"}
{$value} {$value}

View file

@ -14,7 +14,8 @@
<link type="text/css" rel="stylesheet" href="{$value}#"/> <link type="text/css" rel="stylesheet" href="{$value}#"/>
{/loop} {/loop}
{if="is_file('data/user.css')"}<link type="text/css" rel="stylesheet" href="data/user.css#" />{/if} {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"} {if="! empty($links) && count($links) === 1"}
{$link=reset($links)} {$link=reset($links)}
<meta property="og:title" content="{$link.title}" /> <meta property="og:title" content="{$link.title}" />