]>
Commit | Line | Data |
---|---|---|
c4ad3d4f A |
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', | |
3ee8351e | 192 | 'general.header_link' => '/subfolder', |
c4ad3d4f A |
193 | ]; |
194 | ||
195 | $request = $this->createMock(Request::class); | |
196 | $request->method('getParam')->willReturnCallback(function (string $key) use ($providedParameters) { | |
197 | return $providedParameters[$key] ?? null; | |
198 | }); | |
199 | $response = new Response(); | |
200 | ||
201 | $this->container->conf = $this->createMock(ConfigManager::class); | |
202 | $this->container->conf | |
203 | ->method('get') | |
204 | ->willReturnCallback(function (string $key, $value) { | |
205 | if ($key === 'credentials.login') { | |
206 | return 'bob'; | |
207 | } elseif ($key === 'credentials.salt') { | |
208 | return 'salt'; | |
209 | } | |
210 | ||
211 | return $value; | |
212 | }) | |
213 | ; | |
214 | $this->container->conf | |
215 | ->expects(static::exactly(count($expectedSettings))) | |
216 | ->method('set') | |
217 | ->willReturnCallback(function (string $key, $value) use ($expectedSettings) { | |
218 | if ($expectedSettings[$key] ?? null === '_NOT_EMPTY') { | |
219 | static::assertNotEmpty($value); | |
220 | } else { | |
221 | static::assertSame($expectedSettings[$key], $value); | |
222 | } | |
223 | }) | |
224 | ; | |
225 | $this->container->conf->expects(static::once())->method('write'); | |
226 | ||
c4ad3d4f A |
227 | $this->container->sessionManager |
228 | ->expects(static::once()) | |
229 | ->method('setSessionParameter') | |
230 | ->with(SessionManager::KEY_SUCCESS_MESSAGES) | |
231 | ; | |
232 | ||
233 | $result = $this->controller->save($request, $response); | |
234 | ||
235 | static::assertSame(302, $result->getStatusCode()); | |
87ae3c4f | 236 | static::assertSame('/subfolder/login', $result->getHeader('location')[0]); |
c4ad3d4f A |
237 | } |
238 | ||
239 | /** | |
240 | * Test default settings (timezone and title). | |
241 | * Also check that bookmarks are not initialized if | |
242 | */ | |
243 | public function testSaveInstallDefaultValues(): void | |
244 | { | |
245 | $confSettings = []; | |
246 | ||
247 | $request = $this->createMock(Request::class); | |
248 | $response = new Response(); | |
249 | ||
250 | $this->container->conf->method('set')->willReturnCallback(function (string $key, $value) use (&$confSettings) { | |
251 | $confSettings[$key] = $value; | |
252 | }); | |
253 | ||
254 | $result = $this->controller->save($request, $response); | |
255 | ||
256 | static::assertSame(302, $result->getStatusCode()); | |
87ae3c4f | 257 | static::assertSame('/subfolder/login', $result->getHeader('location')[0]); |
c4ad3d4f A |
258 | |
259 | static::assertSame('UTC', $confSettings['general.timezone']); | |
260 | static::assertSame('Shared bookmarks on http://shaarli', $confSettings['general.title']); | |
261 | } | |
262 | } |