diff options
Diffstat (limited to 'tests/front/controller/admin/PluginsControllerTest.php')
-rw-r--r-- | tests/front/controller/admin/PluginsControllerTest.php | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/tests/front/controller/admin/PluginsControllerTest.php b/tests/front/controller/admin/PluginsControllerTest.php new file mode 100644 index 00000000..974d614d --- /dev/null +++ b/tests/front/controller/admin/PluginsControllerTest.php | |||
@@ -0,0 +1,205 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Shaarli\Front\Exception\WrongTokenException; | ||
9 | use Shaarli\Plugin\PluginManager; | ||
10 | use Shaarli\Security\SessionManager; | ||
11 | use Shaarli\TestCase; | ||
12 | use Slim\Http\Request; | ||
13 | use Slim\Http\Response; | ||
14 | |||
15 | class PluginsControllerTest extends TestCase | ||
16 | { | ||
17 | use FrontAdminControllerMockHelper; | ||
18 | |||
19 | const PLUGIN_NAMES = ['plugin1', 'plugin2', 'plugin3', 'plugin4']; | ||
20 | |||
21 | /** @var PluginsController */ | ||
22 | protected $controller; | ||
23 | |||
24 | public function setUp(): void | ||
25 | { | ||
26 | $this->createContainer(); | ||
27 | |||
28 | $this->controller = new PluginsController($this->container); | ||
29 | |||
30 | mkdir($path = __DIR__ . '/folder'); | ||
31 | PluginManager::$PLUGINS_PATH = $path; | ||
32 | array_map(function (string $plugin) use ($path) { touch($path . '/' . $plugin); }, static::PLUGIN_NAMES); | ||
33 | } | ||
34 | |||
35 | public function tearDown(): void | ||
36 | { | ||
37 | $path = __DIR__ . '/folder'; | ||
38 | array_map(function (string $plugin) use ($path) { unlink($path . '/' . $plugin); }, static::PLUGIN_NAMES); | ||
39 | rmdir($path); | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Test displaying plugins admin page | ||
44 | */ | ||
45 | public function testIndex(): void | ||
46 | { | ||
47 | $assignedVariables = []; | ||
48 | $this->assignTemplateVars($assignedVariables); | ||
49 | |||
50 | $request = $this->createMock(Request::class); | ||
51 | $response = new Response(); | ||
52 | |||
53 | $data = [ | ||
54 | 'plugin1' => ['order' => 2, 'other' => 'field'], | ||
55 | 'plugin2' => ['order' => 1], | ||
56 | 'plugin3' => ['order' => false, 'abc' => 'def'], | ||
57 | 'plugin4' => [], | ||
58 | ]; | ||
59 | |||
60 | $this->container->pluginManager | ||
61 | ->expects(static::once()) | ||
62 | ->method('getPluginsMeta') | ||
63 | ->willReturn($data); | ||
64 | |||
65 | $result = $this->controller->index($request, $response); | ||
66 | |||
67 | static::assertSame(200, $result->getStatusCode()); | ||
68 | static::assertSame('pluginsadmin', (string) $result->getBody()); | ||
69 | |||
70 | static::assertSame('Plugin Administration - Shaarli', $assignedVariables['pagetitle']); | ||
71 | static::assertSame( | ||
72 | ['plugin2' => $data['plugin2'], 'plugin1' => $data['plugin1']], | ||
73 | $assignedVariables['enabledPlugins'] | ||
74 | ); | ||
75 | static::assertSame( | ||
76 | ['plugin3' => $data['plugin3'], 'plugin4' => $data['plugin4']], | ||
77 | $assignedVariables['disabledPlugins'] | ||
78 | ); | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Test save plugins admin page | ||
83 | */ | ||
84 | public function testSaveEnabledPlugins(): void | ||
85 | { | ||
86 | $parameters = [ | ||
87 | 'plugin1' => 'on', | ||
88 | 'order_plugin1' => '2', | ||
89 | 'plugin2' => 'on', | ||
90 | ]; | ||
91 | |||
92 | $request = $this->createMock(Request::class); | ||
93 | $request | ||
94 | ->expects(static::atLeastOnce()) | ||
95 | ->method('getParams') | ||
96 | ->willReturnCallback(function () use ($parameters): array { | ||
97 | return $parameters; | ||
98 | }) | ||
99 | ; | ||
100 | $response = new Response(); | ||
101 | |||
102 | $this->container->pluginManager | ||
103 | ->expects(static::once()) | ||
104 | ->method('executeHooks') | ||
105 | ->with('save_plugin_parameters', $parameters) | ||
106 | ; | ||
107 | $this->container->conf | ||
108 | ->expects(static::atLeastOnce()) | ||
109 | ->method('set') | ||
110 | ->with('general.enabled_plugins', ['plugin1', 'plugin2']) | ||
111 | ; | ||
112 | |||
113 | $result = $this->controller->save($request, $response); | ||
114 | |||
115 | static::assertSame(302, $result->getStatusCode()); | ||
116 | static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location')); | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * Test save plugin parameters | ||
121 | */ | ||
122 | public function testSavePluginParameters(): void | ||
123 | { | ||
124 | $parameters = [ | ||
125 | 'parameters_form' => true, | ||
126 | 'parameter1' => 'blip', | ||
127 | 'parameter2' => 'blop', | ||
128 | 'token' => 'this parameter should not be saved' | ||
129 | ]; | ||
130 | |||
131 | $request = $this->createMock(Request::class); | ||
132 | $request | ||
133 | ->expects(static::atLeastOnce()) | ||
134 | ->method('getParams') | ||
135 | ->willReturnCallback(function () use ($parameters): array { | ||
136 | return $parameters; | ||
137 | }) | ||
138 | ; | ||
139 | $response = new Response(); | ||
140 | |||
141 | $this->container->pluginManager | ||
142 | ->expects(static::once()) | ||
143 | ->method('executeHooks') | ||
144 | ->with('save_plugin_parameters', $parameters) | ||
145 | ; | ||
146 | $this->container->conf | ||
147 | ->expects(static::exactly(2)) | ||
148 | ->method('set') | ||
149 | ->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop']) | ||
150 | ; | ||
151 | |||
152 | $result = $this->controller->save($request, $response); | ||
153 | |||
154 | static::assertSame(302, $result->getStatusCode()); | ||
155 | static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location')); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Test save plugin parameters - error encountered | ||
160 | */ | ||
161 | public function testSaveWithError(): void | ||
162 | { | ||
163 | $request = $this->createMock(Request::class); | ||
164 | $response = new Response(); | ||
165 | |||
166 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
167 | $this->container->conf | ||
168 | ->expects(static::atLeastOnce()) | ||
169 | ->method('write') | ||
170 | ->willThrowException(new \Exception($message = 'error message')) | ||
171 | ; | ||
172 | |||
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') | ||
178 | ->with( | ||
179 | SessionManager::KEY_ERROR_MESSAGES, | ||
180 | ['Error while saving plugin configuration: ' . PHP_EOL . $message] | ||
181 | ) | ||
182 | ; | ||
183 | |||
184 | $result = $this->controller->save($request, $response); | ||
185 | |||
186 | static::assertSame(302, $result->getStatusCode()); | ||
187 | static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location')); | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * Test save plugin parameters - wrong token | ||
192 | */ | ||
193 | public function testSaveWrongToken(): void | ||
194 | { | ||
195 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
196 | $this->container->sessionManager->method('checkToken')->willReturn(false); | ||
197 | |||
198 | $request = $this->createMock(Request::class); | ||
199 | $response = new Response(); | ||
200 | |||
201 | $this->expectException(WrongTokenException::class); | ||
202 | |||
203 | $this->controller->save($request, $response); | ||
204 | } | ||
205 | } | ||