]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Process tools page through Slim controller
authorArthurHoaro <arthur@hoa.ro>
Fri, 22 May 2020 11:47:02 +0000 (13:47 +0200)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
application/front/controller/admin/ToolsController.php [new file with mode: 0644]
doc/md/Translations.md
index.php
tests/front/controller/admin/LogoutControllerTest.php
tests/front/controller/admin/ToolsControllerTest.php [new file with mode: 0644]
tpl/default/page.header.html
tpl/vintage/page.header.html

diff --git a/application/front/controller/admin/ToolsController.php b/application/front/controller/admin/ToolsController.php
new file mode 100644 (file)
index 0000000..66db5ad
--- /dev/null
@@ -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;
+    }
+}
index e0e411bb2b8e24848b02b09fa12020ed7ca90b1a..38878940176e0cc4bb5bd4a152a0f5c6cca313f8 100644 (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
index 4cd6d5f45675b63579935752a2c12b0cfb01e228..f4c8b391a44aee1572b57df299421c723e682405 100644 (file)
--- 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 '<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')
index 239e39b2dd50067720ee80baff4bf5339842b9ea..ba681b161e8da221c53f5ee781bd7b30adecaf2c 100644 (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;
     }
diff --git a/tests/front/controller/admin/ToolsControllerTest.php b/tests/front/controller/admin/ToolsControllerTest.php
new file mode 100644 (file)
index 0000000..47c5746
--- /dev/null
@@ -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']);
+    }
+}
index 624367e48050412cf14f22057ad710b064048638..ca7dc1bcd40217f33cb61e1d2b36a979c9022630 100644 (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">
index 9268ced967cdd89126435001f6eac405aa06761e..c265d6d0a8a972df53bdb111f80ef4e1cbae9c94 100644 (file)
 <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>