aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-23 20:06:32 +0100
committerArthurHoaro <arthur@hoa.ro>2020-01-26 11:34:14 +0100
commit0498b209b551cad5595312583e5d6fb1bc3303a5 (patch)
treea7b176fc39788bc7f5eef1ddcd18877d2defdfd9
parent9e4cc28e2957e1f7df713d52a03e350d728dc58e (diff)
downloadShaarli-0498b209b551cad5595312583e5d6fb1bc3303a5.tar.gz
Shaarli-0498b209b551cad5595312583e5d6fb1bc3303a5.tar.zst
Shaarli-0498b209b551cad5595312583e5d6fb1bc3303a5.zip
Execute common plugin hooks before rendering login page
-rw-r--r--application/container/ContainerBuilder.php4
-rw-r--r--application/container/ShaarliContainer.php2
-rw-r--r--application/front/controllers/LoginController.php2
-rw-r--r--application/front/controllers/ShaarliController.php38
-rw-r--r--tests/front/controller/LoginControllerTest.php9
-rw-r--r--tests/front/controller/ShaarliControllerTest.php116
6 files changed, 168 insertions, 3 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php
index ff29825c..e2c78ccc 100644
--- a/application/container/ContainerBuilder.php
+++ b/application/container/ContainerBuilder.php
@@ -72,6 +72,10 @@ class ContainerBuilder
72 ); 72 );
73 }; 73 };
74 74
75 $container['pluginManager'] = function (ShaarliContainer $container): PluginManager {
76 return new PluginManager($container->conf);
77 };
78
75 return $container; 79 return $container;
76 } 80 }
77} 81}
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
index f5483d5e..3fa9116e 100644
--- a/application/container/ShaarliContainer.php
+++ b/application/container/ShaarliContainer.php
@@ -7,6 +7,7 @@ namespace Shaarli\Container;
7use Shaarli\Bookmark\BookmarkServiceInterface; 7use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
9use Shaarli\History; 9use Shaarli\History;
10use Shaarli\Plugin\PluginManager;
10use Shaarli\Render\PageBuilder; 11use Shaarli\Render\PageBuilder;
11use Shaarli\Security\LoginManager; 12use Shaarli\Security\LoginManager;
12use Shaarli\Security\SessionManager; 13use Shaarli\Security\SessionManager;
@@ -21,6 +22,7 @@ use Slim\Container;
21 * @property History $history 22 * @property History $history
22 * @property BookmarkServiceInterface $bookmarkService 23 * @property BookmarkServiceInterface $bookmarkService
23 * @property PageBuilder $pageBuilder 24 * @property PageBuilder $pageBuilder
25 * @property PluginManager $pluginManager
24 */ 26 */
25class ShaarliContainer extends Container 27class ShaarliContainer extends Container
26{ 28{
diff --git a/application/front/controllers/LoginController.php b/application/front/controllers/LoginController.php
index 47fa3ee3..23efb592 100644
--- a/application/front/controllers/LoginController.php
+++ b/application/front/controllers/LoginController.php
@@ -41,6 +41,6 @@ class LoginController extends ShaarliController
41 ->assignView('pagetitle', t('Login') .' - '. $this->ci->conf->get('general.title', 'Shaarli')) 41 ->assignView('pagetitle', t('Login') .' - '. $this->ci->conf->get('general.title', 'Shaarli'))
42 ; 42 ;
43 43
44 return $response->write($this->ci->pageBuilder->render('loginform')); 44 return $response->write($this->render('loginform'));
45 } 45 }
46} 46}
diff --git a/application/front/controllers/ShaarliController.php b/application/front/controllers/ShaarliController.php
index 2a166c3c..99e66d53 100644
--- a/application/front/controllers/ShaarliController.php
+++ b/application/front/controllers/ShaarliController.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller;
6 6
7use Shaarli\Bookmark\BookmarkFilter;
7use Shaarli\Container\ShaarliContainer; 8use Shaarli\Container\ShaarliContainer;
8 9
9abstract class ShaarliController 10abstract class ShaarliController
@@ -28,4 +29,41 @@ abstract class ShaarliController
28 29
29 return $this; 30 return $this;
30 } 31 }
32
33 protected function render(string $template): string
34 {
35 $this->assignView('linkcount', $this->ci->bookmarkService->count(BookmarkFilter::$ALL));
36 $this->assignView('privateLinkcount', $this->ci->bookmarkService->count(BookmarkFilter::$PRIVATE));
37 $this->assignView('plugin_errors', $this->ci->pluginManager->getErrors());
38
39 $this->executeDefaultHooks($template);
40
41 return $this->ci->pageBuilder->render($template);
42 }
43
44 /**
45 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
46 * Then assign generated data to RainTPL.
47 */
48 protected function executeDefaultHooks(string $template): void
49 {
50 $common_hooks = [
51 'includes',
52 'header',
53 'footer',
54 ];
55
56 foreach ($common_hooks as $name) {
57 $plugin_data = [];
58 $this->ci->pluginManager->executeHooks(
59 'render_' . $name,
60 $plugin_data,
61 [
62 'target' => $template,
63 'loggedin' => $this->ci->loginManager->isLoggedIn()
64 ]
65 );
66 $this->assignView('plugins_' . $name, $plugin_data);
67 }
68 }
31} 69}
diff --git a/tests/front/controller/LoginControllerTest.php b/tests/front/controller/LoginControllerTest.php
index ddcfe154..8cf8ece7 100644
--- a/tests/front/controller/LoginControllerTest.php
+++ b/tests/front/controller/LoginControllerTest.php
@@ -5,9 +5,11 @@ declare(strict_types=1);
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
9use Shaarli\Container\ShaarliContainer; 10use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Exception\LoginBannedException; 11use Shaarli\Front\Exception\LoginBannedException;
12use Shaarli\Plugin\PluginManager;
11use Shaarli\Render\PageBuilder; 13use Shaarli\Render\PageBuilder;
12use Shaarli\Security\LoginManager; 14use Shaarli\Security\LoginManager;
13use Slim\Http\Request; 15use Slim\Http\Request;
@@ -37,7 +39,6 @@ class LoginControllerTest extends TestCase
37 39
38 $assignedVariables = []; 40 $assignedVariables = [];
39 $this->container->pageBuilder 41 $this->container->pageBuilder
40 ->expects(static::exactly(3))
41 ->method('assign') 42 ->method('assign')
42 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { 43 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
43 $assignedVariables[$key] = $value; 44 $assignedVariables[$key] = $value;
@@ -68,7 +69,6 @@ class LoginControllerTest extends TestCase
68 69
69 $assignedVariables = []; 70 $assignedVariables = [];
70 $this->container->pageBuilder 71 $this->container->pageBuilder
71 ->expects(static::exactly(4))
72 ->method('assign') 72 ->method('assign')
73 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { 73 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
74 $assignedVariables[$key] = $value; 74 $assignedVariables[$key] = $value;
@@ -169,5 +169,10 @@ class LoginControllerTest extends TestCase
169 }) 169 })
170 ; 170 ;
171 $this->container->pageBuilder = $pageBuilder; 171 $this->container->pageBuilder = $pageBuilder;
172
173 $pluginManager = $this->createMock(PluginManager::class);
174 $this->container->pluginManager = $pluginManager;
175 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
176 $this->container->bookmarkService = $bookmarkService;
172 } 177 }
173} 178}
diff --git a/tests/front/controller/ShaarliControllerTest.php b/tests/front/controller/ShaarliControllerTest.php
new file mode 100644
index 00000000..6fa3feb9
--- /dev/null
+++ b/tests/front/controller/ShaarliControllerTest.php
@@ -0,0 +1,116 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter;
9use Shaarli\Bookmark\BookmarkServiceInterface;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder;
13use Shaarli\Security\LoginManager;
14
15/**
16 * Class ShaarliControllerTest
17 *
18 * This class is used to test default behavior of ShaarliController abstract class.
19 * It uses a dummy non abstract controller.
20 */
21class ShaarliControllerTest extends TestCase
22{
23 /** @var ShaarliContainer */
24 protected $container;
25
26 /** @var LoginController */
27 protected $controller;
28
29 /** @var mixed[] List of variable assigned to the template */
30 protected $assignedValues;
31
32 public function setUp(): void
33 {
34 $this->container = $this->createMock(ShaarliContainer::class);
35 $this->controller = new class($this->container) extends ShaarliController
36 {
37 public function assignView(string $key, $value): ShaarliController
38 {
39 return parent::assignView($key, $value);
40 }
41
42 public function render(string $template): string
43 {
44 return parent::render($template);
45 }
46 };
47 $this->assignedValues = [];
48 }
49
50 public function testAssignView(): void
51 {
52 $this->createValidContainerMockSet();
53
54 $self = $this->controller->assignView('variableName', 'variableValue');
55
56 static::assertInstanceOf(ShaarliController::class, $self);
57 static::assertSame('variableValue', $this->assignedValues['variableName']);
58 }
59
60 public function testRender(): void
61 {
62 $this->createValidContainerMockSet();
63
64 $render = $this->controller->render('templateName');
65
66 static::assertSame('templateName', $render);
67
68 static::assertSame(10, $this->assignedValues['linkcount']);
69 static::assertSame(5, $this->assignedValues['privateLinkcount']);
70 static::assertSame(['error'], $this->assignedValues['plugin_errors']);
71
72 static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']);
73 static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']);
74 static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']);
75 static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']);
76 static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
77 static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
78 }
79
80 protected function createValidContainerMockSet(): void
81 {
82 $pageBuilder = $this->createMock(PageBuilder::class);
83 $pageBuilder
84 ->method('assign')
85 ->willReturnCallback(function (string $key, $value): void {
86 $this->assignedValues[$key] = $value;
87 });
88 $pageBuilder
89 ->method('render')
90 ->willReturnCallback(function (string $template): string {
91 return $template;
92 });
93 $this->container->pageBuilder = $pageBuilder;
94
95 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
96 $bookmarkService
97 ->method('count')
98 ->willReturnCallback(function (string $visibility): int {
99 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
100 });
101 $this->container->bookmarkService = $bookmarkService;
102
103 $pluginManager = $this->createMock(PluginManager::class);
104 $pluginManager
105 ->method('executeHooks')
106 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
107 return $data[$hook] = $params;
108 });
109 $pluginManager->method('getErrors')->willReturn(['error']);
110 $this->container->pluginManager = $pluginManager;
111
112 $loginManager = $this->createMock(LoginManager::class);
113 $loginManager->method('isLoggedIn')->willReturn(true);
114 $this->container->loginManager = $loginManager;
115 }
116}