Process tools page through Slim controller

This commit is contained in:
ArthurHoaro 2020-05-22 13:47:02 +02:00
parent 2899ebb5b5
commit ba43064ddb
7 changed files with 131 additions and 19 deletions

View file

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller\Admin;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Class ToolsController
*
* Slim controller used to display the tools page.
*/
class ToolsController extends ShaarliAdminController
{
public function index(Request $request, Response $response): Response
{
$data = [
'pageabsaddr' => index_url($this->container->environment),
'sslenabled' => is_https($this->container->environment),
];
$this->executeHooks($data);
foreach ($data as $key => $value) {
$this->assignView($key, $value);
}
$this->assignView('pagetitle', t('Tools') .' - '. $this->container->conf->get('general.title', 'Shaarli'));
return $response->write($this->render('tools'));
}
/**
* @param mixed[] $data Variables passed to the template engine
*
* @return mixed[] Template data after active plugins render_picwall hook execution.
*/
protected function executeHooks(array $data): array
{
$this->container->pluginManager->executeHooks(
'render_tools',
$data
);
return $data;
}
}

View file

@ -36,7 +36,7 @@ http://<replace_domain>/?do=addlink
http://<replace_domain>/?do=changepasswd
http://<replace_domain>/?do=changetag
http://<replace_domain>/?do=configure
http://<replace_domain>/?do=tools
http://<replace_domain>/tools
http://<replace_domain>/daily
http://<replace_domain>/?post
http://<replace_domain>/?do=export

View file

@ -501,18 +501,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
// -------- Display the Tools menu if requested (import/export/bookmarklet...)
if ($targetPage == Router::$PAGE_TOOLS) {
$data = [
'pageabsaddr' => index_url($_SERVER),
'sslenabled' => is_https($_SERVER),
];
$pluginManager->executeHooks('render_tools', $data);
foreach ($data as $key => $value) {
$PAGE->assign($key, $value);
}
$PAGE->assign('pagetitle', t('Tools') .' - '. $conf->get('general.title', 'Shaarli'));
$PAGE->renderPage('tools');
header('Location: ./tools');
exit;
}
@ -557,10 +546,10 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
);
// TODO: do not handle exceptions/errors in JS.
echo '<script>alert("'. $e->getMessage() .'");document.location=\'./?do=tools\';</script>';
echo '<script>alert("'. $e->getMessage() .'");document.location=\'./tools\';</script>';
exit;
}
echo '<script>alert("'. t('Your password has been changed') .'");document.location=\'./?do=tools\';</script>';
echo '<script>alert("'. t('Your password has been changed') .'");document.location=\'./tools\';</script>';
exit;
} else {
// show the change password form.
@ -1514,6 +1503,7 @@ $app->group('', function () {
/* -- LOGGED IN -- */
$this->get('/logout', '\Shaarli\Front\Controller\Admin\LogoutController:index')->setName('logout');
$this->get('/tools', '\Shaarli\Front\Controller\Admin\ToolsController:index')->setName('tools');
$this
->get('/links-per-page', '\Shaarli\Front\Controller\Admin\SessionFilterController:linksPerPage')

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Shaarli\Front\Controller\Admin;
/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
if (!function_exists('Shaarli\Front\Controller\setcookie')) {
if (!function_exists('Shaarli\Front\Controller\Admin\setcookie')) {
function setcookie(string $name, string $value): void {
$_COOKIE[$name] = $value;
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller\Admin;
use PHPUnit\Framework\TestCase;
use Slim\Http\Request;
use Slim\Http\Response;
class ToolsControllerTestControllerTest extends TestCase
{
use FrontAdminControllerMockHelper;
/** @var ToolsController */
protected $controller;
public function setUp(): void
{
$this->createContainer();
$this->controller = new ToolsController($this->container);
}
public function testDefaultInvokeWithHttps(): void
{
$this->createValidContainerMockSet();
$request = $this->createMock(Request::class);
$response = new Response();
$this->container->environment = [
'SERVER_NAME' => 'shaarli',
'SERVER_PORT' => 443,
'HTTPS' => 'on',
];
// Save RainTPL assigned variables
$assignedVariables = [];
$this->assignTemplateVars($assignedVariables);
$result = $this->controller->index($request, $response);
static::assertSame(200, $result->getStatusCode());
static::assertSame('tools', (string) $result->getBody());
static::assertSame('https://shaarli', $assignedVariables['pageabsaddr']);
static::assertTrue($assignedVariables['sslenabled']);
}
public function testDefaultInvokeWithoutHttps(): void
{
$this->createValidContainerMockSet();
$request = $this->createMock(Request::class);
$response = new Response();
$this->container->environment = [
'SERVER_NAME' => 'shaarli',
'SERVER_PORT' => 80,
];
// Save RainTPL assigned variables
$assignedVariables = [];
$this->assignTemplateVars($assignedVariables);
$result = $this->controller->index($request, $response);
static::assertSame(200, $result->getStatusCode());
static::assertSame('tools', (string) $result->getBody());
static::assertSame('http://shaarli', $assignedVariables['pageabsaddr']);
static::assertFalse($assignedVariables['sslenabled']);
}
}

View file

@ -26,7 +26,7 @@
</a>
</li>
<li class="pure-menu-item" id="shaarli-menu-tools">
<a href="./?do=tools" class="pure-menu-link">{'Tools'|t}</a>
<a href="./tools" class="pure-menu-link">{'Tools'|t}</a>
</li>
{/if}
<li class="pure-menu-item" id="shaarli-menu-tags">

View file

@ -19,10 +19,10 @@
<li><a href="{$titleLink}" class="nomobile">Home</a></li>
{if="$is_logged_in"}
<li><a href="./logout">Logout</a></li>
<li><a href="?do=tools">Tools</a></li>
<li><a href="./tools">Tools</a></li>
<li><a href="?do=addlink">Add link</a></li>
{elseif="$openshaarli"}
<li><a href="./?do=tools">Tools</a></li>
<li><a href="./tools">Tools</a></li>
<li><a href="./?do=addlink">Add link</a></li>
{else}
<li><a href="./login">Login</a></li>