diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bootstrap.php | 11 | ||||
-rw-r--r-- | tests/container/ContainerBuilderTest.php | 11 | ||||
-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 | ||||
-rw-r--r-- | tests/render/PageCacheManagerTest.php | 5 | ||||
-rw-r--r-- | tests/security/LoginManagerTest.php | 30 | ||||
-rw-r--r-- | tests/security/SessionManagerTest.php | 14 | ||||
-rw-r--r-- | tests/updater/UpdaterTest.php | 42 |
9 files changed, 365 insertions, 42 deletions
diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 511698ff..d4ddedd5 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php | |||
@@ -18,9 +18,14 @@ require_once 'application/bookmark/LinkUtils.php'; | |||
18 | require_once 'application/Utils.php'; | 18 | require_once 'application/Utils.php'; |
19 | require_once 'application/http/UrlUtils.php'; | 19 | require_once 'application/http/UrlUtils.php'; |
20 | require_once 'application/http/HttpUtils.php'; | 20 | require_once 'application/http/HttpUtils.php'; |
21 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
22 | require_once 'tests/utils/ReferenceHistory.php'; | ||
23 | require_once 'tests/utils/FakeBookmarkService.php'; | ||
24 | require_once 'tests/container/ShaarliTestContainer.php'; | 21 | require_once 'tests/container/ShaarliTestContainer.php'; |
25 | require_once 'tests/front/controller/visitor/FrontControllerMockHelper.php'; | 22 | require_once 'tests/front/controller/visitor/FrontControllerMockHelper.php'; |
26 | require_once 'tests/front/controller/admin/FrontAdminControllerMockHelper.php'; | 23 | require_once 'tests/front/controller/admin/FrontAdminControllerMockHelper.php'; |
24 | require_once 'tests/updater/DummyUpdater.php'; | ||
25 | require_once 'tests/utils/FakeBookmarkService.php'; | ||
26 | require_once 'tests/utils/FakeConfigManager.php'; | ||
27 | require_once 'tests/utils/ReferenceHistory.php'; | ||
28 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
29 | require_once 'tests/utils/ReferenceSessionIdHashes.php'; | ||
30 | |||
31 | \ReferenceSessionIdHashes::genAllHashes(); | ||
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php index db533f37..fa77bf31 100644 --- a/tests/container/ContainerBuilderTest.php +++ b/tests/container/ContainerBuilderTest.php | |||
@@ -11,12 +11,15 @@ use Shaarli\Feed\FeedBuilder; | |||
11 | use Shaarli\Formatter\FormatterFactory; | 11 | use Shaarli\Formatter\FormatterFactory; |
12 | use Shaarli\History; | 12 | use Shaarli\History; |
13 | use Shaarli\Http\HttpAccess; | 13 | use Shaarli\Http\HttpAccess; |
14 | use Shaarli\Netscape\NetscapeBookmarkUtils; | ||
14 | use Shaarli\Plugin\PluginManager; | 15 | use Shaarli\Plugin\PluginManager; |
15 | use Shaarli\Render\PageBuilder; | 16 | use Shaarli\Render\PageBuilder; |
16 | use Shaarli\Render\PageCacheManager; | 17 | use Shaarli\Render\PageCacheManager; |
18 | use Shaarli\Security\CookieManager; | ||
17 | use Shaarli\Security\LoginManager; | 19 | use Shaarli\Security\LoginManager; |
18 | use Shaarli\Security\SessionManager; | 20 | use Shaarli\Security\SessionManager; |
19 | use Shaarli\Thumbnailer; | 21 | use Shaarli\Thumbnailer; |
22 | use Shaarli\Updater\Updater; | ||
20 | 23 | ||
21 | class ContainerBuilderTest extends TestCase | 24 | class ContainerBuilderTest extends TestCase |
22 | { | 25 | { |
@@ -32,10 +35,14 @@ class ContainerBuilderTest extends TestCase | |||
32 | /** @var ContainerBuilder */ | 35 | /** @var ContainerBuilder */ |
33 | protected $containerBuilder; | 36 | protected $containerBuilder; |
34 | 37 | ||
38 | /** @var CookieManager */ | ||
39 | protected $cookieManager; | ||
40 | |||
35 | public function setUp(): void | 41 | public function setUp(): void |
36 | { | 42 | { |
37 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | 43 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
38 | $this->sessionManager = $this->createMock(SessionManager::class); | 44 | $this->sessionManager = $this->createMock(SessionManager::class); |
45 | $this->cookieManager = $this->createMock(CookieManager::class); | ||
39 | 46 | ||
40 | $this->loginManager = $this->createMock(LoginManager::class); | 47 | $this->loginManager = $this->createMock(LoginManager::class); |
41 | $this->loginManager->method('isLoggedIn')->willReturn(true); | 48 | $this->loginManager->method('isLoggedIn')->willReturn(true); |
@@ -43,6 +50,7 @@ class ContainerBuilderTest extends TestCase | |||
43 | $this->containerBuilder = new ContainerBuilder( | 50 | $this->containerBuilder = new ContainerBuilder( |
44 | $this->conf, | 51 | $this->conf, |
45 | $this->sessionManager, | 52 | $this->sessionManager, |
53 | $this->cookieManager, | ||
46 | $this->loginManager | 54 | $this->loginManager |
47 | ); | 55 | ); |
48 | } | 56 | } |
@@ -53,6 +61,7 @@ class ContainerBuilderTest extends TestCase | |||
53 | 61 | ||
54 | static::assertInstanceOf(ConfigManager::class, $container->conf); | 62 | static::assertInstanceOf(ConfigManager::class, $container->conf); |
55 | static::assertInstanceOf(SessionManager::class, $container->sessionManager); | 63 | static::assertInstanceOf(SessionManager::class, $container->sessionManager); |
64 | static::assertInstanceOf(CookieManager::class, $container->cookieManager); | ||
56 | static::assertInstanceOf(LoginManager::class, $container->loginManager); | 65 | static::assertInstanceOf(LoginManager::class, $container->loginManager); |
57 | static::assertInstanceOf(History::class, $container->history); | 66 | static::assertInstanceOf(History::class, $container->history); |
58 | static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); | 67 | static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); |
@@ -63,6 +72,8 @@ class ContainerBuilderTest extends TestCase | |||
63 | static::assertInstanceOf(FeedBuilder::class, $container->feedBuilder); | 72 | static::assertInstanceOf(FeedBuilder::class, $container->feedBuilder); |
64 | static::assertInstanceOf(Thumbnailer::class, $container->thumbnailer); | 73 | static::assertInstanceOf(Thumbnailer::class, $container->thumbnailer); |
65 | static::assertInstanceOf(HttpAccess::class, $container->httpAccess); | 74 | static::assertInstanceOf(HttpAccess::class, $container->httpAccess); |
75 | static::assertInstanceOf(NetscapeBookmarkUtils::class, $container->netscapeBookmarkUtils); | ||
76 | static::assertInstanceOf(Updater::class, $container->updater); | ||
66 | 77 | ||
67 | // Set by the middleware | 78 | // Set by the middleware |
68 | static::assertNull($container->basePath); | 79 | static::assertNull($container->basePath); |
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 | } | ||
diff --git a/tests/render/PageCacheManagerTest.php b/tests/render/PageCacheManagerTest.php index b870e6eb..c258f45f 100644 --- a/tests/render/PageCacheManagerTest.php +++ b/tests/render/PageCacheManagerTest.php | |||
@@ -1,15 +1,14 @@ | |||
1 | <?php | 1 | <?php |
2 | |||
2 | /** | 3 | /** |
3 | * Cache tests | 4 | * Cache tests |
4 | */ | 5 | */ |
6 | |||
5 | namespace Shaarli\Render; | 7 | namespace Shaarli\Render; |
6 | 8 | ||
7 | use PHPUnit\Framework\TestCase; | 9 | use PHPUnit\Framework\TestCase; |
8 | use Shaarli\Security\SessionManager; | 10 | use Shaarli\Security\SessionManager; |
9 | 11 | ||
10 | // required to access $_SESSION array | ||
11 | session_start(); | ||
12 | |||
13 | /** | 12 | /** |
14 | * Unitary tests for cached pages | 13 | * Unitary tests for cached pages |
15 | */ | 14 | */ |
diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php index 8fd1698c..f242be09 100644 --- a/tests/security/LoginManagerTest.php +++ b/tests/security/LoginManagerTest.php | |||
@@ -1,7 +1,6 @@ | |||
1 | <?php | 1 | <?php |
2 | namespace Shaarli\Security; | ||
3 | 2 | ||
4 | require_once 'tests/utils/FakeConfigManager.php'; | 3 | namespace Shaarli\Security; |
5 | 4 | ||
6 | use PHPUnit\Framework\TestCase; | 5 | use PHPUnit\Framework\TestCase; |
7 | 6 | ||
@@ -58,6 +57,9 @@ class LoginManagerTest extends TestCase | |||
58 | /** @var string Salt used by hash functions */ | 57 | /** @var string Salt used by hash functions */ |
59 | protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; | 58 | protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; |
60 | 59 | ||
60 | /** @var CookieManager */ | ||
61 | protected $cookieManager; | ||
62 | |||
61 | /** | 63 | /** |
62 | * Prepare or reset test resources | 64 | * Prepare or reset test resources |
63 | */ | 65 | */ |
@@ -84,8 +86,12 @@ class LoginManagerTest extends TestCase | |||
84 | $this->cookie = []; | 86 | $this->cookie = []; |
85 | $this->session = []; | 87 | $this->session = []; |
86 | 88 | ||
87 | $this->sessionManager = new SessionManager($this->session, $this->configManager); | 89 | $this->cookieManager = $this->createMock(CookieManager::class); |
88 | $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); | 90 | $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) { |
91 | return $this->cookie[$key] ?? null; | ||
92 | }); | ||
93 | $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path'); | ||
94 | $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager); | ||
89 | $this->server['REMOTE_ADDR'] = $this->ipAddr; | 95 | $this->server['REMOTE_ADDR'] = $this->ipAddr; |
90 | } | 96 | } |
91 | 97 | ||
@@ -193,8 +199,8 @@ class LoginManagerTest extends TestCase | |||
193 | $configManager = new \FakeConfigManager([ | 199 | $configManager = new \FakeConfigManager([ |
194 | 'resource.ban_file' => $this->banFile, | 200 | 'resource.ban_file' => $this->banFile, |
195 | ]); | 201 | ]); |
196 | $loginManager = new LoginManager($configManager, null); | 202 | $loginManager = new LoginManager($configManager, null, $this->cookieManager); |
197 | $loginManager->checkLoginState([], ''); | 203 | $loginManager->checkLoginState(''); |
198 | 204 | ||
199 | $this->assertFalse($loginManager->isLoggedIn()); | 205 | $this->assertFalse($loginManager->isLoggedIn()); |
200 | } | 206 | } |
@@ -210,9 +216,9 @@ class LoginManagerTest extends TestCase | |||
210 | 'expires_on' => time() + 100, | 216 | 'expires_on' => time() + 100, |
211 | ]; | 217 | ]; |
212 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 218 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
213 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; | 219 | $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope'; |
214 | 220 | ||
215 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 221 | $this->loginManager->checkLoginState($this->clientIpAddress); |
216 | 222 | ||
217 | $this->assertTrue($this->loginManager->isLoggedIn()); | 223 | $this->assertTrue($this->loginManager->isLoggedIn()); |
218 | $this->assertTrue(empty($this->session['username'])); | 224 | $this->assertTrue(empty($this->session['username'])); |
@@ -224,9 +230,9 @@ class LoginManagerTest extends TestCase | |||
224 | public function testCheckLoginStateStaySignedInWithValidToken() | 230 | public function testCheckLoginStateStaySignedInWithValidToken() |
225 | { | 231 | { |
226 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 232 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
227 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); | 233 | $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken(); |
228 | 234 | ||
229 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 235 | $this->loginManager->checkLoginState($this->clientIpAddress); |
230 | 236 | ||
231 | $this->assertTrue($this->loginManager->isLoggedIn()); | 237 | $this->assertTrue($this->loginManager->isLoggedIn()); |
232 | $this->assertEquals($this->login, $this->session['username']); | 238 | $this->assertEquals($this->login, $this->session['username']); |
@@ -241,7 +247,7 @@ class LoginManagerTest extends TestCase | |||
241 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 247 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
242 | $this->session['expires_on'] = time() - 100; | 248 | $this->session['expires_on'] = time() - 100; |
243 | 249 | ||
244 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 250 | $this->loginManager->checkLoginState($this->clientIpAddress); |
245 | 251 | ||
246 | $this->assertFalse($this->loginManager->isLoggedIn()); | 252 | $this->assertFalse($this->loginManager->isLoggedIn()); |
247 | } | 253 | } |
@@ -253,7 +259,7 @@ class LoginManagerTest extends TestCase | |||
253 | { | 259 | { |
254 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 260 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
255 | 261 | ||
256 | $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); | 262 | $this->loginManager->checkLoginState('10.7.157.98'); |
257 | 263 | ||
258 | $this->assertFalse($this->loginManager->isLoggedIn()); | 264 | $this->assertFalse($this->loginManager->isLoggedIn()); |
259 | } | 265 | } |
diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index d9db775e..60695dcf 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php | |||
@@ -1,12 +1,8 @@ | |||
1 | <?php | 1 | <?php |
2 | require_once 'tests/utils/FakeConfigManager.php'; | ||
3 | 2 | ||
4 | // Initialize reference data _before_ PHPUnit starts a session | 3 | namespace Shaarli\Security; |
5 | require_once 'tests/utils/ReferenceSessionIdHashes.php'; | ||
6 | ReferenceSessionIdHashes::genAllHashes(); | ||
7 | 4 | ||
8 | use PHPUnit\Framework\TestCase; | 5 | use PHPUnit\Framework\TestCase; |
9 | use Shaarli\Security\SessionManager; | ||
10 | 6 | ||
11 | /** | 7 | /** |
12 | * Test coverage for SessionManager | 8 | * Test coverage for SessionManager |
@@ -30,7 +26,7 @@ class SessionManagerTest extends TestCase | |||
30 | */ | 26 | */ |
31 | public static function setUpBeforeClass() | 27 | public static function setUpBeforeClass() |
32 | { | 28 | { |
33 | self::$sidHashes = ReferenceSessionIdHashes::getHashes(); | 29 | self::$sidHashes = \ReferenceSessionIdHashes::getHashes(); |
34 | } | 30 | } |
35 | 31 | ||
36 | /** | 32 | /** |
@@ -38,13 +34,13 @@ class SessionManagerTest extends TestCase | |||
38 | */ | 34 | */ |
39 | public function setUp() | 35 | public function setUp() |
40 | { | 36 | { |
41 | $this->conf = new FakeConfigManager([ | 37 | $this->conf = new \FakeConfigManager([ |
42 | 'credentials.login' => 'johndoe', | 38 | 'credentials.login' => 'johndoe', |
43 | 'credentials.salt' => 'salt', | 39 | 'credentials.salt' => 'salt', |
44 | 'security.session_protection_disabled' => false, | 40 | 'security.session_protection_disabled' => false, |
45 | ]); | 41 | ]); |
46 | $this->session = []; | 42 | $this->session = []; |
47 | $this->sessionManager = new SessionManager($this->session, $this->conf); | 43 | $this->sessionManager = new SessionManager($this->session, $this->conf, 'session_path'); |
48 | } | 44 | } |
49 | 45 | ||
50 | /** | 46 | /** |
@@ -69,7 +65,7 @@ class SessionManagerTest extends TestCase | |||
69 | $token => 1, | 65 | $token => 1, |
70 | ], | 66 | ], |
71 | ]; | 67 | ]; |
72 | $sessionManager = new SessionManager($session, $this->conf); | 68 | $sessionManager = new SessionManager($session, $this->conf, 'session_path'); |
73 | 69 | ||
74 | // check and destroy the token | 70 | // check and destroy the token |
75 | $this->assertTrue($sessionManager->checkToken($token)); | 71 | $this->assertTrue($sessionManager->checkToken($token)); |
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php index afc35aec..c801d451 100644 --- a/tests/updater/UpdaterTest.php +++ b/tests/updater/UpdaterTest.php | |||
@@ -7,9 +7,6 @@ use Shaarli\Bookmark\BookmarkServiceInterface; | |||
7 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
8 | use Shaarli\History; | 8 | use Shaarli\History; |
9 | 9 | ||
10 | require_once 'tests/updater/DummyUpdater.php'; | ||
11 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
12 | require_once 'inc/rain.tpl.class.php'; | ||
13 | 10 | ||
14 | /** | 11 | /** |
15 | * Class UpdaterTest. | 12 | * Class UpdaterTest. |
@@ -35,6 +32,9 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
35 | /** @var BookmarkServiceInterface */ | 32 | /** @var BookmarkServiceInterface */ |
36 | protected $bookmarkService; | 33 | protected $bookmarkService; |
37 | 34 | ||
35 | /** @var \ReferenceLinkDB */ | ||
36 | protected $refDB; | ||
37 | |||
38 | /** @var Updater */ | 38 | /** @var Updater */ |
39 | protected $updater; | 39 | protected $updater; |
40 | 40 | ||
@@ -43,6 +43,9 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
43 | */ | 43 | */ |
44 | public function setUp() | 44 | public function setUp() |
45 | { | 45 | { |
46 | $this->refDB = new \ReferenceLinkDB(); | ||
47 | $this->refDB->write(self::$testDatastore); | ||
48 | |||
46 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); | 49 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); |
47 | $this->conf = new ConfigManager(self::$configFile); | 50 | $this->conf = new ConfigManager(self::$configFile); |
48 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true); | 51 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true); |
@@ -181,9 +184,40 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
181 | 184 | ||
182 | public function testUpdateMethodRelativeHomeLinkRename(): void | 185 | public function testUpdateMethodRelativeHomeLinkRename(): void |
183 | { | 186 | { |
187 | $this->updater->setBasePath('/subfolder'); | ||
184 | $this->conf->set('general.header_link', '?'); | 188 | $this->conf->set('general.header_link', '?'); |
189 | |||
190 | $this->updater->updateMethodRelativeHomeLink(); | ||
191 | |||
192 | static::assertSame('/subfolder/', $this->conf->get('general.header_link')); | ||
193 | } | ||
194 | |||
195 | public function testUpdateMethodRelativeHomeLinkDoNotRename(): void | ||
196 | { | ||
197 | $this->updater->setBasePath('/subfolder'); | ||
198 | $this->conf->set('general.header_link', '~/my-blog'); | ||
199 | |||
185 | $this->updater->updateMethodRelativeHomeLink(); | 200 | $this->updater->updateMethodRelativeHomeLink(); |
186 | 201 | ||
187 | static::assertSame(); | 202 | static::assertSame('~/my-blog', $this->conf->get('general.header_link')); |
203 | } | ||
204 | |||
205 | public function testUpdateMethodMigrateExistingNotesUrl(): void | ||
206 | { | ||
207 | $this->updater->setBasePath('/subfolder'); | ||
208 | |||
209 | $this->updater->updateMethodMigrateExistingNotesUrl(); | ||
210 | |||
211 | static::assertSame($this->refDB->getLinks()[0]->getUrl(), $this->bookmarkService->get(0)->getUrl()); | ||
212 | static::assertSame($this->refDB->getLinks()[1]->getUrl(), $this->bookmarkService->get(1)->getUrl()); | ||
213 | static::assertSame($this->refDB->getLinks()[4]->getUrl(), $this->bookmarkService->get(4)->getUrl()); | ||
214 | static::assertSame($this->refDB->getLinks()[6]->getUrl(), $this->bookmarkService->get(6)->getUrl()); | ||
215 | static::assertSame($this->refDB->getLinks()[7]->getUrl(), $this->bookmarkService->get(7)->getUrl()); | ||
216 | static::assertSame($this->refDB->getLinks()[8]->getUrl(), $this->bookmarkService->get(8)->getUrl()); | ||
217 | static::assertSame($this->refDB->getLinks()[9]->getUrl(), $this->bookmarkService->get(9)->getUrl()); | ||
218 | static::assertSame('/subfolder/shaare/WDWyig', $this->bookmarkService->get(42)->getUrl()); | ||
219 | static::assertSame('/subfolder/shaare/WDWyig', $this->bookmarkService->get(41)->getUrl()); | ||
220 | static::assertSame('/subfolder/shaare/0gCTjQ', $this->bookmarkService->get(10)->getUrl()); | ||
221 | static::assertSame('/subfolder/shaare/PCRizQ', $this->bookmarkService->get(11)->getUrl()); | ||
188 | } | 222 | } |
189 | } | 223 | } |