diff options
author | ArthurHoaro <arthur.hoareau@wizacha.com> | 2020-07-07 10:15:56 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | c4ad3d4f061d05a01db25aa54dda830ba776792d (patch) | |
tree | 691d91a5b0bbac62cee41f7b95ad1daa38d610b3 /tests/front/controller/visitor | |
parent | 1a8ac737e52cb25a5c346232ee398f5908cee7d7 (diff) | |
download | Shaarli-c4ad3d4f061d05a01db25aa54dda830ba776792d.tar.gz Shaarli-c4ad3d4f061d05a01db25aa54dda830ba776792d.tar.zst Shaarli-c4ad3d4f061d05a01db25aa54dda830ba776792d.zip |
Process Shaarli install through Slim controller
Diffstat (limited to 'tests/front/controller/visitor')
-rw-r--r-- | tests/front/controller/visitor/InstallControllerTest.php | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/InstallControllerTest.php b/tests/front/controller/visitor/InstallControllerTest.php new file mode 100644 index 00000000..6871fdd9 --- /dev/null +++ b/tests/front/controller/visitor/InstallControllerTest.php | |||
@@ -0,0 +1,264 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Exception\AlreadyInstalledException; | ||
10 | use Shaarli\Security\SessionManager; | ||
11 | use Slim\Http\Request; | ||
12 | use Slim\Http\Response; | ||
13 | |||
14 | class InstallControllerTest extends TestCase | ||
15 | { | ||
16 | use FrontControllerMockHelper; | ||
17 | |||
18 | const MOCK_FILE = '.tmp'; | ||
19 | |||
20 | /** @var InstallController */ | ||
21 | protected $controller; | ||
22 | |||
23 | public function setUp(): void | ||
24 | { | ||
25 | $this->createContainer(); | ||
26 | |||
27 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
28 | $this->container->conf->method('getConfigFileExt')->willReturn(static::MOCK_FILE); | ||
29 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
30 | if ($key === 'resource.raintpl_tpl') { | ||
31 | return '.'; | ||
32 | } | ||
33 | |||
34 | return $default ?? $key; | ||
35 | }); | ||
36 | |||
37 | $this->controller = new InstallController($this->container); | ||
38 | } | ||
39 | |||
40 | protected function tearDown(): void | ||
41 | { | ||
42 | if (file_exists(static::MOCK_FILE)) { | ||
43 | unlink(static::MOCK_FILE); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Test displaying install page with valid session. | ||
49 | */ | ||
50 | public function testInstallIndexWithValidSession(): void | ||
51 | { | ||
52 | $assignedVariables = []; | ||
53 | $this->assignTemplateVars($assignedVariables); | ||
54 | |||
55 | $request = $this->createMock(Request::class); | ||
56 | $response = new Response(); | ||
57 | |||
58 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
59 | $this->container->sessionManager | ||
60 | ->method('getSessionParameter') | ||
61 | ->willReturnCallback(function (string $key, $default) { | ||
62 | return $key === 'session_tested' ? 'Working' : $default; | ||
63 | }) | ||
64 | ; | ||
65 | |||
66 | $result = $this->controller->index($request, $response); | ||
67 | |||
68 | static::assertSame(200, $result->getStatusCode()); | ||
69 | static::assertSame('install', (string) $result->getBody()); | ||
70 | |||
71 | static::assertIsArray($assignedVariables['continents']); | ||
72 | static::assertSame('Africa', $assignedVariables['continents'][0]); | ||
73 | static::assertSame('UTC', $assignedVariables['continents']['selected']); | ||
74 | |||
75 | static::assertIsArray($assignedVariables['cities']); | ||
76 | static::assertSame(['continent' => 'Africa', 'city' => 'Abidjan'], $assignedVariables['cities'][0]); | ||
77 | static::assertSame('UTC', $assignedVariables['continents']['selected']); | ||
78 | |||
79 | static::assertIsArray($assignedVariables['languages']); | ||
80 | static::assertSame('Automatic', $assignedVariables['languages']['auto']); | ||
81 | static::assertSame('French', $assignedVariables['languages']['fr']); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Instantiate the install controller with an existing config file: exception. | ||
86 | */ | ||
87 | public function testInstallWithExistingConfigFile(): void | ||
88 | { | ||
89 | $this->expectException(AlreadyInstalledException::class); | ||
90 | |||
91 | touch(static::MOCK_FILE); | ||
92 | |||
93 | $this->controller = new InstallController($this->container); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Call controller without session yet defined, redirect to test session install page. | ||
98 | */ | ||
99 | public function testInstallRedirectToSessionTest(): void | ||
100 | { | ||
101 | $request = $this->createMock(Request::class); | ||
102 | $response = new Response(); | ||
103 | |||
104 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
105 | $this->container->sessionManager | ||
106 | ->expects(static::once()) | ||
107 | ->method('setSessionParameter') | ||
108 | ->with(InstallController::SESSION_TEST_KEY, InstallController::SESSION_TEST_VALUE) | ||
109 | ; | ||
110 | |||
111 | $result = $this->controller->index($request, $response); | ||
112 | |||
113 | static::assertSame(302, $result->getStatusCode()); | ||
114 | static::assertSame('/subfolder/install/session-test', $result->getHeader('location')[0]); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Call controller in session test mode: valid session then redirect to install page. | ||
119 | */ | ||
120 | public function testInstallSessionTestValid(): void | ||
121 | { | ||
122 | $request = $this->createMock(Request::class); | ||
123 | $response = new Response(); | ||
124 | |||
125 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
126 | $this->container->sessionManager | ||
127 | ->method('getSessionParameter') | ||
128 | ->with(InstallController::SESSION_TEST_KEY) | ||
129 | ->willReturn(InstallController::SESSION_TEST_VALUE) | ||
130 | ; | ||
131 | |||
132 | $result = $this->controller->sessionTest($request, $response); | ||
133 | |||
134 | static::assertSame(302, $result->getStatusCode()); | ||
135 | static::assertSame('/subfolder/install', $result->getHeader('location')[0]); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Call controller in session test mode: invalid session then redirect to error page. | ||
140 | */ | ||
141 | public function testInstallSessionTestError(): void | ||
142 | { | ||
143 | $assignedVars = []; | ||
144 | $this->assignTemplateVars($assignedVars); | ||
145 | |||
146 | $request = $this->createMock(Request::class); | ||
147 | $response = new Response(); | ||
148 | |||
149 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
150 | $this->container->sessionManager | ||
151 | ->method('getSessionParameter') | ||
152 | ->with(InstallController::SESSION_TEST_KEY) | ||
153 | ->willReturn('KO') | ||
154 | ; | ||
155 | |||
156 | $result = $this->controller->sessionTest($request, $response); | ||
157 | |||
158 | static::assertSame(200, $result->getStatusCode()); | ||
159 | static::assertSame('error', (string) $result->getBody()); | ||
160 | static::assertStringStartsWith( | ||
161 | '<pre>Sessions do not seem to work correctly on your server', | ||
162 | $assignedVars['message'] | ||
163 | ); | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Test saving valid data from install form. Also initialize datastore. | ||
168 | */ | ||
169 | public function testSaveInstallValid(): void | ||
170 | { | ||
171 | $providedParameters = [ | ||
172 | 'continent' => 'Europe', | ||
173 | 'city' => 'Berlin', | ||
174 | 'setlogin' => 'bob', | ||
175 | 'setpassword' => 'password', | ||
176 | 'title' => 'Shaarli', | ||
177 | 'language' => 'fr', | ||
178 | 'updateCheck' => true, | ||
179 | 'enableApi' => true, | ||
180 | ]; | ||
181 | |||
182 | $expectedSettings = [ | ||
183 | 'general.timezone' => 'Europe/Berlin', | ||
184 | 'credentials.login' => 'bob', | ||
185 | 'credentials.salt' => '_NOT_EMPTY', | ||
186 | 'credentials.hash' => '_NOT_EMPTY', | ||
187 | 'general.title' => 'Shaarli', | ||
188 | 'translation.language' => 'en', | ||
189 | 'updates.check_updates' => true, | ||
190 | 'api.enabled' => true, | ||
191 | 'api.secret' => '_NOT_EMPTY', | ||
192 | ]; | ||
193 | |||
194 | $request = $this->createMock(Request::class); | ||
195 | $request->method('getParam')->willReturnCallback(function (string $key) use ($providedParameters) { | ||
196 | return $providedParameters[$key] ?? null; | ||
197 | }); | ||
198 | $response = new Response(); | ||
199 | |||
200 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
201 | $this->container->conf | ||
202 | ->method('get') | ||
203 | ->willReturnCallback(function (string $key, $value) { | ||
204 | if ($key === 'credentials.login') { | ||
205 | return 'bob'; | ||
206 | } elseif ($key === 'credentials.salt') { | ||
207 | return 'salt'; | ||
208 | } | ||
209 | |||
210 | return $value; | ||
211 | }) | ||
212 | ; | ||
213 | $this->container->conf | ||
214 | ->expects(static::exactly(count($expectedSettings))) | ||
215 | ->method('set') | ||
216 | ->willReturnCallback(function (string $key, $value) use ($expectedSettings) { | ||
217 | if ($expectedSettings[$key] ?? null === '_NOT_EMPTY') { | ||
218 | static::assertNotEmpty($value); | ||
219 | } else { | ||
220 | static::assertSame($expectedSettings[$key], $value); | ||
221 | } | ||
222 | }) | ||
223 | ; | ||
224 | $this->container->conf->expects(static::once())->method('write'); | ||
225 | |||
226 | $this->container->bookmarkService->expects(static::once())->method('count')->willReturn(0); | ||
227 | $this->container->bookmarkService->expects(static::once())->method('initialize'); | ||
228 | |||
229 | $this->container->sessionManager | ||
230 | ->expects(static::once()) | ||
231 | ->method('setSessionParameter') | ||
232 | ->with(SessionManager::KEY_SUCCESS_MESSAGES) | ||
233 | ; | ||
234 | |||
235 | $result = $this->controller->save($request, $response); | ||
236 | |||
237 | static::assertSame(302, $result->getStatusCode()); | ||
238 | static::assertSame('/subfolder/', $result->getHeader('location')[0]); | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * Test default settings (timezone and title). | ||
243 | * Also check that bookmarks are not initialized if | ||
244 | */ | ||
245 | public function testSaveInstallDefaultValues(): void | ||
246 | { | ||
247 | $confSettings = []; | ||
248 | |||
249 | $request = $this->createMock(Request::class); | ||
250 | $response = new Response(); | ||
251 | |||
252 | $this->container->conf->method('set')->willReturnCallback(function (string $key, $value) use (&$confSettings) { | ||
253 | $confSettings[$key] = $value; | ||
254 | }); | ||
255 | |||
256 | $result = $this->controller->save($request, $response); | ||
257 | |||
258 | static::assertSame(302, $result->getStatusCode()); | ||
259 | static::assertSame('/subfolder/', $result->getHeader('location')[0]); | ||
260 | |||
261 | static::assertSame('UTC', $confSettings['general.timezone']); | ||
262 | static::assertSame('Shared bookmarks on http://shaarli', $confSettings['general.title']); | ||
263 | } | ||
264 | } | ||