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 | |
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')
-rw-r--r-- | tests/front/ShaarliMiddlewareTest.php | 12 | ||||
-rw-r--r-- | tests/front/controller/admin/LogoutControllerTest.php | 18 | ||||
-rw-r--r-- | tests/front/controller/visitor/InstallControllerTest.php | 264 |
3 files changed, 283 insertions, 11 deletions
diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php index 81ea1344..20090d8b 100644 --- a/tests/front/ShaarliMiddlewareTest.php +++ b/tests/front/ShaarliMiddlewareTest.php | |||
@@ -19,6 +19,8 @@ use Slim\Http\Uri; | |||
19 | 19 | ||
20 | class ShaarliMiddlewareTest extends TestCase | 20 | class ShaarliMiddlewareTest extends TestCase |
21 | { | 21 | { |
22 | protected const TMP_MOCK_FILE = '.tmp'; | ||
23 | |||
22 | /** @var ShaarliContainer */ | 24 | /** @var ShaarliContainer */ |
23 | protected $container; | 25 | protected $container; |
24 | 26 | ||
@@ -29,12 +31,21 @@ class ShaarliMiddlewareTest extends TestCase | |||
29 | { | 31 | { |
30 | $this->container = $this->createMock(ShaarliContainer::class); | 32 | $this->container = $this->createMock(ShaarliContainer::class); |
31 | 33 | ||
34 | touch(static::TMP_MOCK_FILE); | ||
35 | |||
32 | $this->container->conf = $this->createMock(ConfigManager::class); | 36 | $this->container->conf = $this->createMock(ConfigManager::class); |
37 | $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); | ||
38 | |||
33 | $this->container->loginManager = $this->createMock(LoginManager::class); | 39 | $this->container->loginManager = $this->createMock(LoginManager::class); |
34 | 40 | ||
35 | $this->middleware = new ShaarliMiddleware($this->container); | 41 | $this->middleware = new ShaarliMiddleware($this->container); |
36 | } | 42 | } |
37 | 43 | ||
44 | public function tearDown() | ||
45 | { | ||
46 | unlink(static::TMP_MOCK_FILE); | ||
47 | } | ||
48 | |||
38 | /** | 49 | /** |
39 | * Test middleware execution with valid controller call | 50 | * Test middleware execution with valid controller call |
40 | */ | 51 | */ |
@@ -179,6 +190,7 @@ class ShaarliMiddlewareTest extends TestCase | |||
179 | $this->container->conf->method('get')->willReturnCallback(function (string $key): string { | 190 | $this->container->conf->method('get')->willReturnCallback(function (string $key): string { |
180 | return $key; | 191 | return $key; |
181 | }); | 192 | }); |
193 | $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); | ||
182 | 194 | ||
183 | $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); | 195 | $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); |
184 | $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches'); | 196 | $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches'); |
diff --git a/tests/front/controller/admin/LogoutControllerTest.php b/tests/front/controller/admin/LogoutControllerTest.php index ca177085..45e84dc0 100644 --- a/tests/front/controller/admin/LogoutControllerTest.php +++ b/tests/front/controller/admin/LogoutControllerTest.php | |||
@@ -4,14 +4,8 @@ declare(strict_types=1); | |||
4 | 4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | 5 | namespace Shaarli\Front\Controller\Admin; |
6 | 6 | ||
7 | /** Override PHP builtin setcookie function in the local namespace to mock it... more or less */ | ||
8 | if (!function_exists('Shaarli\Front\Controller\Admin\setcookie')) { | ||
9 | function setcookie(string $name, string $value): void { | ||
10 | $_COOKIE[$name] = $value; | ||
11 | } | ||
12 | } | ||
13 | |||
14 | use PHPUnit\Framework\TestCase; | 7 | use PHPUnit\Framework\TestCase; |
8 | use Shaarli\Security\CookieManager; | ||
15 | use Shaarli\Security\LoginManager; | 9 | use Shaarli\Security\LoginManager; |
16 | use Shaarli\Security\SessionManager; | 10 | use Shaarli\Security\SessionManager; |
17 | use Slim\Http\Request; | 11 | use Slim\Http\Request; |
@@ -29,8 +23,6 @@ class LogoutControllerTest extends TestCase | |||
29 | $this->createContainer(); | 23 | $this->createContainer(); |
30 | 24 | ||
31 | $this->controller = new LogoutController($this->container); | 25 | $this->controller = new LogoutController($this->container); |
32 | |||
33 | setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there'); | ||
34 | } | 26 | } |
35 | 27 | ||
36 | public function testValidControllerInvoke(): void | 28 | public function testValidControllerInvoke(): void |
@@ -43,13 +35,17 @@ class LogoutControllerTest extends TestCase | |||
43 | $this->container->sessionManager = $this->createMock(SessionManager::class); | 35 | $this->container->sessionManager = $this->createMock(SessionManager::class); |
44 | $this->container->sessionManager->expects(static::once())->method('logout'); | 36 | $this->container->sessionManager->expects(static::once())->method('logout'); |
45 | 37 | ||
46 | static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); | 38 | $this->container->cookieManager = $this->createMock(CookieManager::class); |
39 | $this->container->cookieManager | ||
40 | ->expects(static::once()) | ||
41 | ->method('setCookieParameter') | ||
42 | ->with(CookieManager::STAY_SIGNED_IN, 'false', 0, '/subfolder/') | ||
43 | ; | ||
47 | 44 | ||
48 | $result = $this->controller->index($request, $response); | 45 | $result = $this->controller->index($request, $response); |
49 | 46 | ||
50 | static::assertInstanceOf(Response::class, $result); | 47 | static::assertInstanceOf(Response::class, $result); |
51 | static::assertSame(302, $result->getStatusCode()); | 48 | static::assertSame(302, $result->getStatusCode()); |
52 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | 49 | static::assertSame(['/subfolder/'], $result->getHeader('location')); |
53 | static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); | ||
54 | } | 50 | } |
55 | } | 51 | } |
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 | } | ||