3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Front\Exception\WrongTokenException
;
10 use Shaarli\Security\SessionManager
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
14 class PluginsControllerTest
extends TestCase
16 use FrontAdminControllerMockHelper
;
18 /** @var PluginsController */
19 protected $controller;
21 public function setUp(): void
23 $this->createContainer();
25 $this->controller
= new PluginsController($this->container
);
29 * Test displaying plugins admin page
31 public function testIndex(): void
33 $assignedVariables = [];
34 $this->assignTemplateVars($assignedVariables);
36 $request = $this->createMock(Request
::class);
37 $response = new Response();
40 'plugin1' => ['order' => 2, 'other' => 'field'],
41 'plugin2' => ['order' => 1],
42 'plugin3' => ['order' => false, 'abc' => 'def'],
46 $this->container
->pluginManager
47 ->expects(static::once())
48 ->method('getPluginsMeta')
51 $result = $this->controller
->index($request, $response);
53 static::assertSame(200, $result->getStatusCode());
54 static::assertSame('pluginsadmin', (string) $result->getBody());
56 static::assertSame('Plugin Administration - Shaarli', $assignedVariables['pagetitle']);
58 ['plugin2' => $data['plugin2'], 'plugin1' => $data['plugin1']],
59 $assignedVariables['enabledPlugins']
62 ['plugin3' => $data['plugin3'], 'plugin4' => $data['plugin4']],
63 $assignedVariables['disabledPlugins']
68 * Test save plugins admin page
70 public function testSaveEnabledPlugins(): void
74 'order_plugin1' => '2',
78 $request = $this->createMock(Request
::class);
80 ->expects(static::atLeastOnce())
82 ->willReturnCallback(function () use ($parameters): array {
86 $response = new Response();
88 $this->container
->pluginManager
89 ->expects(static::once())
90 ->method('executeHooks')
91 ->with('save_plugin_parameters', $parameters)
93 $this->container
->conf
94 ->expects(static::atLeastOnce())
96 ->with('general.enabled_plugins', ['plugin1', 'plugin2'])
99 $result = $this->controller
->save($request, $response);
101 static::assertSame(302, $result->getStatusCode());
102 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
106 * Test save plugin parameters
108 public function testSavePluginParameters(): void
111 'parameters_form' => true,
112 'parameter1' => 'blip',
113 'parameter2' => 'blop',
116 $request = $this->createMock(Request
::class);
118 ->expects(static::atLeastOnce())
119 ->method('getParams')
120 ->willReturnCallback(function () use ($parameters): array {
124 $response = new Response();
126 $this->container
->pluginManager
127 ->expects(static::once())
128 ->method('executeHooks')
129 ->with('save_plugin_parameters', $parameters)
131 $this->container
->conf
132 ->expects(static::atLeastOnce())
134 ->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
137 $result = $this->controller
->save($request, $response);
139 static::assertSame(302, $result->getStatusCode());
140 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
144 * Test save plugin parameters - error encountered
146 public function testSaveWithError(): void
148 $request = $this->createMock(Request
::class);
149 $response = new Response();
151 $this->container
->conf
= $this->createMock(ConfigManager
::class);
152 $this->container
->conf
153 ->expects(static::atLeastOnce())
155 ->willThrowException(new \
Exception($message = 'error message'))
158 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
159 $this->container
->sessionManager
->method('checkToken')->willReturn(true);
160 $this->container
->sessionManager
161 ->expects(static::once())
162 ->method('setSessionParameter')
164 SessionManager
::KEY_ERROR_MESSAGES
,
165 ['ERROR while saving plugin configuration: ' . PHP_EOL
. $message]
169 $result = $this->controller
->save($request, $response);
171 static::assertSame(302, $result->getStatusCode());
172 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
176 * Test save plugin parameters - wrong token
178 public function testSaveWrongToken(): void
180 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
181 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
183 $request = $this->createMock(Request
::class);
184 $response = new Response();
186 $this->expectException(WrongTokenException
::class);
188 $this->controller
->save($request, $response);