3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Config\ConfigManager
;
8 use Shaarli\Front\Exception\WrongTokenException
;
9 use Shaarli\Plugin\PluginManager
;
10 use Shaarli\Security\SessionManager
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class PluginsControllerTest
extends TestCase
17 use FrontAdminControllerMockHelper
;
19 const PLUGIN_NAMES
= ['plugin1', 'plugin2', 'plugin3', 'plugin4'];
21 /** @var PluginsController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->controller
= new PluginsController($this->container
);
30 mkdir($path = __DIR__
. '/folder');
31 PluginManager
::$PLUGINS_PATH = $path;
32 array_map(function (string $plugin) use ($path) { touch($path
. '/' . $plugin
); }, static::PLUGIN_NAMES
);
35 public function tearDown(): void
37 $path = __DIR__
. '/folder';
38 array_map(function (string $plugin) use ($path) { unlink($path
. '/' . $plugin
); }, static::PLUGIN_NAMES
);
43 * Test displaying plugins admin page
45 public function testIndex(): void
47 $assignedVariables = [];
48 $this->assignTemplateVars($assignedVariables);
50 $request = $this->createMock(Request
::class);
51 $response = new Response();
54 'plugin1' => ['order' => 2, 'other' => 'field'],
55 'plugin2' => ['order' => 1],
56 'plugin3' => ['order' => false, 'abc' => 'def'],
60 $this->container
->pluginManager
61 ->expects(static::once())
62 ->method('getPluginsMeta')
65 $result = $this->controller
->index($request, $response);
67 static::assertSame(200, $result->getStatusCode());
68 static::assertSame('pluginsadmin', (string) $result->getBody());
70 static::assertSame('Plugin Administration - Shaarli', $assignedVariables['pagetitle']);
72 ['plugin2' => $data['plugin2'], 'plugin1' => $data['plugin1']],
73 $assignedVariables['enabledPlugins']
76 ['plugin3' => $data['plugin3'], 'plugin4' => $data['plugin4']],
77 $assignedVariables['disabledPlugins']
82 * Test save plugins admin page
84 public function testSaveEnabledPlugins(): void
88 'order_plugin1' => '2',
92 $request = $this->createMock(Request
::class);
94 ->expects(static::atLeastOnce())
96 ->willReturnCallback(function () use ($parameters): array {
100 $response = new Response();
102 $this->container
->pluginManager
103 ->expects(static::once())
104 ->method('executeHooks')
105 ->with('save_plugin_parameters', $parameters)
107 $this->container
->conf
108 ->expects(static::atLeastOnce())
110 ->with('general.enabled_plugins', ['plugin1', 'plugin2'])
113 $result = $this->controller
->save($request, $response);
115 static::assertSame(302, $result->getStatusCode());
116 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
120 * Test save plugin parameters
122 public function testSavePluginParameters(): void
125 'parameters_form' => true,
126 'parameter1' => 'blip',
127 'parameter2' => 'blop',
128 'token' => 'this parameter should not be saved'
131 $request = $this->createMock(Request
::class);
133 ->expects(static::atLeastOnce())
134 ->method('getParams')
135 ->willReturnCallback(function () use ($parameters): array {
139 $response = new Response();
141 $this->container
->pluginManager
142 ->expects(static::once())
143 ->method('executeHooks')
144 ->with('save_plugin_parameters', $parameters)
146 $this->container
->conf
147 ->expects(static::exactly(2))
149 ->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
152 $result = $this->controller
->save($request, $response);
154 static::assertSame(302, $result->getStatusCode());
155 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
159 * Test save plugin parameters - error encountered
161 public function testSaveWithError(): void
163 $request = $this->createMock(Request
::class);
164 $response = new Response();
166 $this->container
->conf
= $this->createMock(ConfigManager
::class);
167 $this->container
->conf
168 ->expects(static::atLeastOnce())
170 ->willThrowException(new \
Exception($message = 'error message'))
173 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
174 $this->container
->sessionManager
->method('checkToken')->willReturn(true);
175 $this->container
->sessionManager
176 ->expects(static::once())
177 ->method('setSessionParameter')
179 SessionManager
::KEY_ERROR_MESSAGES
,
180 ['Error while saving plugin configuration: ' . PHP_EOL
. $message]
184 $result = $this->controller
->save($request, $response);
186 static::assertSame(302, $result->getStatusCode());
187 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
191 * Test save plugin parameters - wrong token
193 public function testSaveWrongToken(): void
195 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
196 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
198 $request = $this->createMock(Request
::class);
199 $response = new Response();
201 $this->expectException(WrongTokenException
::class);
203 $this->controller
->save($request, $response);