]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Execute common plugin hooks before rendering login page
authorArthurHoaro <arthur@hoa.ro>
Thu, 23 Jan 2020 19:06:32 +0000 (20:06 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sun, 26 Jan 2020 10:34:14 +0000 (11:34 +0100)
application/container/ContainerBuilder.php
application/container/ShaarliContainer.php
application/front/controllers/LoginController.php
application/front/controllers/ShaarliController.php
tests/front/controller/LoginControllerTest.php
tests/front/controller/ShaarliControllerTest.php [new file with mode: 0644]

index ff29825cc7be2e85b552333d0048033b48103e9a..e2c78ccc44f4c47a412a4ba10a1e95c0c2f93fdb 100644 (file)
@@ -72,6 +72,10 @@ class ContainerBuilder
             );
         };
 
+        $container['pluginManager'] = function (ShaarliContainer $container): PluginManager {
+            return new PluginManager($container->conf);
+        };
+
         return $container;
     }
 }
index f5483d5e194b4d8dccc43c06d9fed92823d83e21..3fa9116e3544ee2e6b73f8b552a9bf30b6b92aa7 100644 (file)
@@ -7,6 +7,7 @@ namespace Shaarli\Container;
 use Shaarli\Bookmark\BookmarkServiceInterface;
 use Shaarli\Config\ConfigManager;
 use Shaarli\History;
+use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\PageBuilder;
 use Shaarli\Security\LoginManager;
 use Shaarli\Security\SessionManager;
@@ -21,6 +22,7 @@ use Slim\Container;
  * @property History                  $history
  * @property BookmarkServiceInterface $bookmarkService
  * @property PageBuilder              $pageBuilder
+ * @property PluginManager            $pluginManager
  */
 class ShaarliContainer extends Container
 {
index 47fa3ee3fff81d7bb2a45ff3beef0c7c4e83f719..23efb592d853ec77fa90cf2b9e0c5ec029f84f38 100644 (file)
@@ -41,6 +41,6 @@ class LoginController extends ShaarliController
             ->assignView('pagetitle', t('Login') .' - '. $this->ci->conf->get('general.title', 'Shaarli'))
         ;
 
-        return $response->write($this->ci->pageBuilder->render('loginform'));
+        return $response->write($this->render('loginform'));
     }
 }
index 2a166c3c4db56907207ae9adaf7f643df6d9cda9..99e66d53665d2a17794c244ff8592ce83c208e76 100644 (file)
@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace Shaarli\Front\Controller;
 
+use Shaarli\Bookmark\BookmarkFilter;
 use Shaarli\Container\ShaarliContainer;
 
 abstract class ShaarliController
@@ -28,4 +29,41 @@ abstract class ShaarliController
 
         return $this;
     }
+
+    protected function render(string $template): string
+    {
+        $this->assignView('linkcount', $this->ci->bookmarkService->count(BookmarkFilter::$ALL));
+        $this->assignView('privateLinkcount', $this->ci->bookmarkService->count(BookmarkFilter::$PRIVATE));
+        $this->assignView('plugin_errors', $this->ci->pluginManager->getErrors());
+
+        $this->executeDefaultHooks($template);
+
+        return $this->ci->pageBuilder->render($template);
+    }
+
+    /**
+     * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
+     * Then assign generated data to RainTPL.
+     */
+    protected function executeDefaultHooks(string $template): void
+    {
+        $common_hooks = [
+            'includes',
+            'header',
+            'footer',
+        ];
+
+        foreach ($common_hooks as $name) {
+            $plugin_data = [];
+            $this->ci->pluginManager->executeHooks(
+                'render_' . $name,
+                $plugin_data,
+                [
+                    'target' => $template,
+                    'loggedin' => $this->ci->loginManager->isLoggedIn()
+                ]
+            );
+            $this->assignView('plugins_' . $name, $plugin_data);
+        }
+    }
 }
index ddcfe15419f35e2eb3529d76f54eba2fb6e0f4f2..8cf8ece7982397d0e79f0dbeee6d0d3d8b3e82d1 100644 (file)
@@ -5,9 +5,11 @@ declare(strict_types=1);
 namespace Shaarli\Front\Controller;
 
 use PHPUnit\Framework\TestCase;
+use Shaarli\Bookmark\BookmarkServiceInterface;
 use Shaarli\Config\ConfigManager;
 use Shaarli\Container\ShaarliContainer;
 use Shaarli\Front\Exception\LoginBannedException;
+use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\PageBuilder;
 use Shaarli\Security\LoginManager;
 use Slim\Http\Request;
@@ -37,7 +39,6 @@ class LoginControllerTest extends TestCase
 
         $assignedVariables = [];
         $this->container->pageBuilder
-            ->expects(static::exactly(3))
             ->method('assign')
             ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
                 $assignedVariables[$key] = $value;
@@ -68,7 +69,6 @@ class LoginControllerTest extends TestCase
 
         $assignedVariables = [];
         $this->container->pageBuilder
-            ->expects(static::exactly(4))
             ->method('assign')
             ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
                 $assignedVariables[$key] = $value;
@@ -169,5 +169,10 @@ class LoginControllerTest extends TestCase
             })
         ;
         $this->container->pageBuilder = $pageBuilder;
+
+        $pluginManager = $this->createMock(PluginManager::class);
+        $this->container->pluginManager = $pluginManager;
+        $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
+        $this->container->bookmarkService = $bookmarkService;
     }
 }
diff --git a/tests/front/controller/ShaarliControllerTest.php b/tests/front/controller/ShaarliControllerTest.php
new file mode 100644 (file)
index 0000000..6fa3feb
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use PHPUnit\Framework\TestCase;
+use Shaarli\Bookmark\BookmarkFilter;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Container\ShaarliContainer;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Security\LoginManager;
+
+/**
+ * Class ShaarliControllerTest
+ *
+ * This class is used to test default behavior of ShaarliController abstract class.
+ * It uses a dummy non abstract controller.
+ */
+class ShaarliControllerTest extends TestCase
+{
+    /** @var ShaarliContainer */
+    protected $container;
+
+    /** @var LoginController */
+    protected $controller;
+
+    /** @var mixed[] List of variable assigned to the template */
+    protected $assignedValues;
+
+    public function setUp(): void
+    {
+        $this->container = $this->createMock(ShaarliContainer::class);
+        $this->controller = new class($this->container) extends ShaarliController
+        {
+            public function assignView(string $key, $value): ShaarliController
+            {
+                return parent::assignView($key, $value);
+            }
+
+            public function render(string $template): string
+            {
+                return parent::render($template);
+            }
+        };
+        $this->assignedValues = [];
+    }
+
+    public function testAssignView(): void
+    {
+        $this->createValidContainerMockSet();
+
+        $self = $this->controller->assignView('variableName', 'variableValue');
+
+        static::assertInstanceOf(ShaarliController::class, $self);
+        static::assertSame('variableValue', $this->assignedValues['variableName']);
+    }
+
+    public function testRender(): void
+    {
+        $this->createValidContainerMockSet();
+
+        $render = $this->controller->render('templateName');
+
+        static::assertSame('templateName', $render);
+
+        static::assertSame(10, $this->assignedValues['linkcount']);
+        static::assertSame(5, $this->assignedValues['privateLinkcount']);
+        static::assertSame(['error'], $this->assignedValues['plugin_errors']);
+
+        static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']);
+        static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']);
+        static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']);
+        static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']);
+        static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
+        static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
+    }
+
+    protected function createValidContainerMockSet(): void
+    {
+        $pageBuilder = $this->createMock(PageBuilder::class);
+        $pageBuilder
+            ->method('assign')
+            ->willReturnCallback(function (string $key, $value): void {
+                $this->assignedValues[$key] = $value;
+            });
+        $pageBuilder
+            ->method('render')
+            ->willReturnCallback(function (string $template): string {
+                return $template;
+            });
+        $this->container->pageBuilder = $pageBuilder;
+
+        $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
+        $bookmarkService
+            ->method('count')
+            ->willReturnCallback(function (string $visibility): int {
+                return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
+            });
+        $this->container->bookmarkService = $bookmarkService;
+
+        $pluginManager = $this->createMock(PluginManager::class);
+        $pluginManager
+            ->method('executeHooks')
+            ->willReturnCallback(function (string $hook, array &$data, array $params): array {
+                return $data[$hook] = $params;
+            });
+        $pluginManager->method('getErrors')->willReturn(['error']);
+        $this->container->pluginManager = $pluginManager;
+
+        $loginManager = $this->createMock(LoginManager::class);
+        $loginManager->method('isLoggedIn')->willReturn(true);
+        $this->container->loginManager = $loginManager;
+    }
+}