]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - tests/front/controller/admin/PluginsControllerTest.php
Fix UT
[github/shaarli/Shaarli.git] / tests / front / controller / admin / PluginsControllerTest.php
... / ...
CommitLineData
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Exception\WrongTokenException;
10use Shaarli\Plugin\PluginManager;
11use Shaarli\Security\SessionManager;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15class 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()
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 ];
129
130 $request = $this->createMock(Request::class);
131 $request
132 ->expects(static::atLeastOnce())
133 ->method('getParams')
134 ->willReturnCallback(function () use ($parameters): array {
135 return $parameters;
136 })
137 ;
138 $response = new Response();
139
140 $this->container->pluginManager
141 ->expects(static::once())
142 ->method('executeHooks')
143 ->with('save_plugin_parameters', $parameters)
144 ;
145 $this->container->conf
146 ->expects(static::atLeastOnce())
147 ->method('set')
148 ->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
149 ;
150
151 $result = $this->controller->save($request, $response);
152
153 static::assertSame(302, $result->getStatusCode());
154 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
155 }
156
157 /**
158 * Test save plugin parameters - error encountered
159 */
160 public function testSaveWithError(): void
161 {
162 $request = $this->createMock(Request::class);
163 $response = new Response();
164
165 $this->container->conf = $this->createMock(ConfigManager::class);
166 $this->container->conf
167 ->expects(static::atLeastOnce())
168 ->method('write')
169 ->willThrowException(new \Exception($message = 'error message'))
170 ;
171
172 $this->container->sessionManager = $this->createMock(SessionManager::class);
173 $this->container->sessionManager->method('checkToken')->willReturn(true);
174 $this->container->sessionManager
175 ->expects(static::once())
176 ->method('setSessionParameter')
177 ->with(
178 SessionManager::KEY_ERROR_MESSAGES,
179 ['Error while saving plugin configuration: ' . PHP_EOL . $message]
180 )
181 ;
182
183 $result = $this->controller->save($request, $response);
184
185 static::assertSame(302, $result->getStatusCode());
186 static::assertSame(['/subfolder/admin/plugins'], $result->getHeader('location'));
187 }
188
189 /**
190 * Test save plugin parameters - wrong token
191 */
192 public function testSaveWrongToken(): void
193 {
194 $this->container->sessionManager = $this->createMock(SessionManager::class);
195 $this->container->sessionManager->method('checkToken')->willReturn(false);
196
197 $request = $this->createMock(Request::class);
198 $response = new Response();
199
200 $this->expectException(WrongTokenException::class);
201
202 $this->controller->save($request, $response);
203 }
204}