From ba43064ddb7771fc97df135a32f9b0d5e373dd36 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 22 May 2020 13:47:02 +0200 Subject: Process tools page through Slim controller --- .../front/controller/admin/ToolsController.php | 49 +++++++++++++++ doc/md/Translations.md | 2 +- index.php | 18 ++---- .../controller/admin/LogoutControllerTest.php | 2 +- .../front/controller/admin/ToolsControllerTest.php | 73 ++++++++++++++++++++++ tpl/default/page.header.html | 2 +- tpl/vintage/page.header.html | 4 +- 7 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 application/front/controller/admin/ToolsController.php create mode 100644 tests/front/controller/admin/ToolsControllerTest.php diff --git a/application/front/controller/admin/ToolsController.php b/application/front/controller/admin/ToolsController.php new file mode 100644 index 00000000..66db5ad9 --- /dev/null +++ b/application/front/controller/admin/ToolsController.php @@ -0,0 +1,49 @@ + 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; + } +} diff --git a/doc/md/Translations.md b/doc/md/Translations.md index e0e411bb..38878940 100644 --- a/doc/md/Translations.md +++ b/doc/md/Translations.md @@ -36,7 +36,7 @@ http:///?do=addlink http:///?do=changepasswd http:///?do=changetag http:///?do=configure -http:///?do=tools +http:///tools http:///daily http:///?post http:///?do=export diff --git a/index.php b/index.php index 4cd6d5f4..f4c8b391 100644 --- a/index.php +++ b/index.php @@ -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 ''; + echo ''; exit; } - echo ''; + echo ''; 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') diff --git a/tests/front/controller/admin/LogoutControllerTest.php b/tests/front/controller/admin/LogoutControllerTest.php index 239e39b2..ba681b16 100644 --- a/tests/front/controller/admin/LogoutControllerTest.php +++ b/tests/front/controller/admin/LogoutControllerTest.php @@ -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; } diff --git a/tests/front/controller/admin/ToolsControllerTest.php b/tests/front/controller/admin/ToolsControllerTest.php new file mode 100644 index 00000000..47c5746e --- /dev/null +++ b/tests/front/controller/admin/ToolsControllerTest.php @@ -0,0 +1,73 @@ +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']); + } +} diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html index 624367e4..ca7dc1bc 100644 --- a/tpl/default/page.header.html +++ b/tpl/default/page.header.html @@ -26,7 +26,7 @@
  • - {'Tools'|t} + {'Tools'|t}
  • {/if}
  • diff --git a/tpl/vintage/page.header.html b/tpl/vintage/page.header.html index 9268ced9..c265d6d0 100644 --- a/tpl/vintage/page.header.html +++ b/tpl/vintage/page.header.html @@ -19,10 +19,10 @@
  • Home
  • {if="$is_logged_in"}
  • Logout
  • -
  • Tools
  • +
  • Tools
  • Add link
  • {elseif="$openshaarli"} -
  • Tools
  • +
  • Tools
  • Add link
  • {else}
  • Login
  • -- cgit v1.2.3