diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-30 14:00:06 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 66063ed1a18d739b1a60bfb163d8656417a4c529 (patch) | |
tree | 5af628861a42af1a4bf84f6dcd18a24b80cc3a1c /tests/front | |
parent | 465033230da0398426010aa7bd3694735b71c899 (diff) | |
download | Shaarli-66063ed1a18d739b1a60bfb163d8656417a4c529.tar.gz Shaarli-66063ed1a18d739b1a60bfb163d8656417a4c529.tar.zst Shaarli-66063ed1a18d739b1a60bfb163d8656417a4c529.zip |
Process configure page through Slim controller
Diffstat (limited to 'tests/front')
4 files changed, 294 insertions, 18 deletions
diff --git a/tests/front/controller/admin/ConfigureControllerTest.php b/tests/front/controller/admin/ConfigureControllerTest.php new file mode 100644 index 00000000..40304a18 --- /dev/null +++ b/tests/front/controller/admin/ConfigureControllerTest.php | |||
@@ -0,0 +1,252 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Exception\WrongTokenException; | ||
10 | use Shaarli\Security\SessionManager; | ||
11 | use Shaarli\Thumbnailer; | ||
12 | use Slim\Http\Request; | ||
13 | use Slim\Http\Response; | ||
14 | |||
15 | class ConfigureControllerTest extends TestCase | ||
16 | { | ||
17 | use FrontAdminControllerMockHelper; | ||
18 | |||
19 | /** @var ConfigureController */ | ||
20 | protected $controller; | ||
21 | |||
22 | public function setUp(): void | ||
23 | { | ||
24 | $this->createContainer(); | ||
25 | |||
26 | $this->controller = new ConfigureController($this->container); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Test displaying configure page - it should display all config variables | ||
31 | */ | ||
32 | public function testIndex(): void | ||
33 | { | ||
34 | $assignedVariables = []; | ||
35 | $this->assignTemplateVars($assignedVariables); | ||
36 | |||
37 | $request = $this->createMock(Request::class); | ||
38 | $response = new Response(); | ||
39 | |||
40 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
41 | $this->container->conf->method('get')->willReturnCallback(function (string $key) { | ||
42 | return $key; | ||
43 | }); | ||
44 | |||
45 | $result = $this->controller->index($request, $response); | ||
46 | |||
47 | static::assertSame(200, $result->getStatusCode()); | ||
48 | static::assertSame('configure', (string) $result->getBody()); | ||
49 | |||
50 | static::assertSame('Configure - general.title', $assignedVariables['pagetitle']); | ||
51 | static::assertSame('general.title', $assignedVariables['title']); | ||
52 | static::assertSame('resource.theme', $assignedVariables['theme']); | ||
53 | static::assertEmpty($assignedVariables['theme_available']); | ||
54 | static::assertSame(['default', 'markdown'], $assignedVariables['formatter_available']); | ||
55 | static::assertNotEmpty($assignedVariables['continents']); | ||
56 | static::assertNotEmpty($assignedVariables['cities']); | ||
57 | static::assertSame('general.retrieve_description', $assignedVariables['retrieve_description']); | ||
58 | static::assertSame('privacy.default_private_links', $assignedVariables['private_links_default']); | ||
59 | static::assertSame('security.session_protection_disabled', $assignedVariables['session_protection_disabled']); | ||
60 | static::assertSame('feed.rss_permalinks', $assignedVariables['enable_rss_permalinks']); | ||
61 | static::assertSame('updates.check_updates', $assignedVariables['enable_update_check']); | ||
62 | static::assertSame('privacy.hide_public_links', $assignedVariables['hide_public_links']); | ||
63 | static::assertSame('api.enabled', $assignedVariables['api_enabled']); | ||
64 | static::assertSame('api.secret', $assignedVariables['api_secret']); | ||
65 | static::assertCount(4, $assignedVariables['languages']); | ||
66 | static::assertArrayHasKey('gd_enabled', $assignedVariables); | ||
67 | static::assertSame('thumbnails.mode', $assignedVariables['thumbnails_mode']); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Test posting a new config - make sure that everything is saved properly, without errors. | ||
72 | */ | ||
73 | public function testSaveNewConfig(): void | ||
74 | { | ||
75 | $session = []; | ||
76 | $this->assignSessionVars($session); | ||
77 | |||
78 | $parameters = [ | ||
79 | 'token' => 'token', | ||
80 | 'continent' => 'Europe', | ||
81 | 'city' => 'Moscow', | ||
82 | 'title' => 'Shaarli', | ||
83 | 'titleLink' => './', | ||
84 | 'retrieveDescription' => 'on', | ||
85 | 'theme' => 'vintage', | ||
86 | 'disablesessionprotection' => null, | ||
87 | 'privateLinkByDefault' => true, | ||
88 | 'enableRssPermalinks' => true, | ||
89 | 'updateCheck' => false, | ||
90 | 'hidePublicLinks' => 'on', | ||
91 | 'enableApi' => 'on', | ||
92 | 'apiSecret' => 'abcdef', | ||
93 | 'formatter' => 'markdown', | ||
94 | 'language' => 'fr', | ||
95 | 'enableThumbnails' => Thumbnailer::MODE_NONE, | ||
96 | ]; | ||
97 | |||
98 | $parametersConfigMapping = [ | ||
99 | 'general.timezone' => $parameters['continent'] . '/' . $parameters['city'], | ||
100 | 'general.title' => $parameters['title'], | ||
101 | 'general.header_link' => $parameters['titleLink'], | ||
102 | 'general.retrieve_description' => !!$parameters['retrieveDescription'], | ||
103 | 'resource.theme' => $parameters['theme'], | ||
104 | 'security.session_protection_disabled' => !!$parameters['disablesessionprotection'], | ||
105 | 'privacy.default_private_links' => !!$parameters['privateLinkByDefault'], | ||
106 | 'feed.rss_permalinks' => !!$parameters['enableRssPermalinks'], | ||
107 | 'updates.check_updates' => !!$parameters['updateCheck'], | ||
108 | 'privacy.hide_public_links' => !!$parameters['hidePublicLinks'], | ||
109 | 'api.enabled' => !!$parameters['enableApi'], | ||
110 | 'api.secret' => $parameters['apiSecret'], | ||
111 | 'formatter' => $parameters['formatter'], | ||
112 | 'translation.language' => $parameters['language'], | ||
113 | 'thumbnails.mode' => $parameters['enableThumbnails'], | ||
114 | ]; | ||
115 | |||
116 | $request = $this->createMock(Request::class); | ||
117 | $request | ||
118 | ->expects(static::atLeastOnce()) | ||
119 | ->method('getParam')->willReturnCallback(function (string $key) use ($parameters) { | ||
120 | if (false === array_key_exists($key, $parameters)) { | ||
121 | static::fail('unknown key: ' . $key); | ||
122 | } | ||
123 | |||
124 | return $parameters[$key]; | ||
125 | } | ||
126 | ); | ||
127 | |||
128 | $response = new Response(); | ||
129 | |||
130 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
131 | $this->container->conf | ||
132 | ->expects(static::atLeastOnce()) | ||
133 | ->method('set') | ||
134 | ->willReturnCallback(function (string $key, $value) use ($parametersConfigMapping): void { | ||
135 | if (false === array_key_exists($key, $parametersConfigMapping)) { | ||
136 | static::fail('unknown key: ' . $key); | ||
137 | } | ||
138 | |||
139 | static::assertSame($parametersConfigMapping[$key], $value); | ||
140 | } | ||
141 | ); | ||
142 | |||
143 | $result = $this->controller->save($request, $response); | ||
144 | static::assertSame(302, $result->getStatusCode()); | ||
145 | static::assertSame(['./configure'], $result->getHeader('Location')); | ||
146 | |||
147 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | ||
148 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | ||
149 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | ||
150 | static::assertSame(['Configuration was saved.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Test posting a new config - wrong token. | ||
155 | */ | ||
156 | public function testSaveNewConfigWrongToken(): void | ||
157 | { | ||
158 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
159 | $this->container->sessionManager->method('checkToken')->willReturn(false); | ||
160 | |||
161 | $this->container->conf->expects(static::never())->method('set'); | ||
162 | $this->container->conf->expects(static::never())->method('write'); | ||
163 | |||
164 | $request = $this->createMock(Request::class); | ||
165 | $response = new Response(); | ||
166 | |||
167 | $this->expectException(WrongTokenException::class); | ||
168 | |||
169 | $this->controller->save($request, $response); | ||
170 | } | ||
171 | |||
172 | /** | ||
173 | * Test posting a new config - thumbnail activation. | ||
174 | */ | ||
175 | public function testSaveNewConfigThumbnailsActivation(): void | ||
176 | { | ||
177 | $session = []; | ||
178 | $this->assignSessionVars($session); | ||
179 | |||
180 | $request = $this->createMock(Request::class); | ||
181 | $request | ||
182 | ->expects(static::atLeastOnce()) | ||
183 | ->method('getParam')->willReturnCallback(function (string $key) { | ||
184 | if ('enableThumbnails' === $key) { | ||
185 | return Thumbnailer::MODE_ALL; | ||
186 | } | ||
187 | |||
188 | return $key; | ||
189 | }) | ||
190 | ; | ||
191 | $response = new Response(); | ||
192 | |||
193 | $result = $this->controller->save($request, $response); | ||
194 | |||
195 | static::assertSame(302, $result->getStatusCode()); | ||
196 | static::assertSame(['./configure'], $result->getHeader('Location')); | ||
197 | |||
198 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | ||
199 | static::assertArrayHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | ||
200 | static::assertStringContainsString( | ||
201 | 'You have enabled or changed thumbnails mode', | ||
202 | $session[SessionManager::KEY_WARNING_MESSAGES][0] | ||
203 | ); | ||
204 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | ||
205 | static::assertSame(['Configuration was saved.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]); | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Test posting a new config - thumbnail activation. | ||
210 | */ | ||
211 | public function testSaveNewConfigThumbnailsAlreadyActive(): void | ||
212 | { | ||
213 | $session = []; | ||
214 | $this->assignSessionVars($session); | ||
215 | |||
216 | $request = $this->createMock(Request::class); | ||
217 | $request | ||
218 | ->expects(static::atLeastOnce()) | ||
219 | ->method('getParam')->willReturnCallback(function (string $key) { | ||
220 | if ('enableThumbnails' === $key) { | ||
221 | return Thumbnailer::MODE_ALL; | ||
222 | } | ||
223 | |||
224 | return $key; | ||
225 | }) | ||
226 | ; | ||
227 | $response = new Response(); | ||
228 | |||
229 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
230 | $this->container->conf | ||
231 | ->expects(static::atLeastOnce()) | ||
232 | ->method('get') | ||
233 | ->willReturnCallback(function (string $key): string { | ||
234 | if ('thumbnails.mode' === $key) { | ||
235 | return Thumbnailer::MODE_ALL; | ||
236 | } | ||
237 | |||
238 | return $key; | ||
239 | }) | ||
240 | ; | ||
241 | |||
242 | $result = $this->controller->save($request, $response); | ||
243 | |||
244 | static::assertSame(302, $result->getStatusCode()); | ||
245 | static::assertSame(['./configure'], $result->getHeader('Location')); | ||
246 | |||
247 | static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session); | ||
248 | static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session); | ||
249 | static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session); | ||
250 | static::assertSame(['Configuration was saved.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]); | ||
251 | } | ||
252 | } | ||
diff --git a/tests/front/controller/admin/FrontAdminControllerMockHelper.php b/tests/front/controller/admin/FrontAdminControllerMockHelper.php index bd40c0c7..2b9f2ef1 100644 --- a/tests/front/controller/admin/FrontAdminControllerMockHelper.php +++ b/tests/front/controller/admin/FrontAdminControllerMockHelper.php | |||
@@ -6,6 +6,7 @@ namespace Shaarli\Front\Controller\Admin; | |||
6 | 6 | ||
7 | use Shaarli\Container\ShaarliTestContainer; | 7 | use Shaarli\Container\ShaarliTestContainer; |
8 | use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper; | 8 | use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper; |
9 | use Shaarli\History; | ||
9 | 10 | ||
10 | /** | 11 | /** |
11 | * Trait FrontControllerMockHelper | 12 | * Trait FrontControllerMockHelper |
@@ -27,7 +28,29 @@ trait FrontAdminControllerMockHelper | |||
27 | { | 28 | { |
28 | $this->parentCreateContainer(); | 29 | $this->parentCreateContainer(); |
29 | 30 | ||
31 | $this->container->history = $this->createMock(History::class); | ||
32 | |||
30 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); | 33 | $this->container->loginManager->method('isLoggedIn')->willReturn(true); |
31 | $this->container->sessionManager->method('checkToken')->willReturn(true); | 34 | $this->container->sessionManager->method('checkToken')->willReturn(true); |
32 | } | 35 | } |
36 | |||
37 | |||
38 | /** | ||
39 | * Pass a reference of an array which will be populated by `sessionManager->setSessionParameter` | ||
40 | * calls during execution. | ||
41 | * | ||
42 | * @param mixed $variables Array reference to populate. | ||
43 | */ | ||
44 | protected function assignSessionVars(array &$variables): void | ||
45 | { | ||
46 | $this->container->sessionManager | ||
47 | ->expects(static::atLeastOnce()) | ||
48 | ->method('setSessionParameter') | ||
49 | ->willReturnCallback(function ($key, $value) use (&$variables) { | ||
50 | $variables[$key] = $value; | ||
51 | |||
52 | return $this->container->sessionManager; | ||
53 | }) | ||
54 | ; | ||
55 | } | ||
33 | } | 56 | } |
diff --git a/tests/front/controller/visitor/DailyControllerTest.php b/tests/front/controller/visitor/DailyControllerTest.php index 872420fd..b802c62c 100644 --- a/tests/front/controller/visitor/DailyControllerTest.php +++ b/tests/front/controller/visitor/DailyControllerTest.php | |||
@@ -57,20 +57,20 @@ class DailyControllerTest extends TestCase | |||
57 | (new Bookmark()) | 57 | (new Bookmark()) |
58 | ->setId(1) | 58 | ->setId(1) |
59 | ->setUrl('http://url.tld') | 59 | ->setUrl('http://url.tld') |
60 | ->setTitle(static::generateContent(50)) | 60 | ->setTitle(static::generateString(50)) |
61 | ->setDescription(static::generateContent(500)) | 61 | ->setDescription(static::generateString(500)) |
62 | , | 62 | , |
63 | (new Bookmark()) | 63 | (new Bookmark()) |
64 | ->setId(2) | 64 | ->setId(2) |
65 | ->setUrl('http://url2.tld') | 65 | ->setUrl('http://url2.tld') |
66 | ->setTitle(static::generateContent(50)) | 66 | ->setTitle(static::generateString(50)) |
67 | ->setDescription(static::generateContent(500)) | 67 | ->setDescription(static::generateString(500)) |
68 | , | 68 | , |
69 | (new Bookmark()) | 69 | (new Bookmark()) |
70 | ->setId(3) | 70 | ->setId(3) |
71 | ->setUrl('http://url3.tld') | 71 | ->setUrl('http://url3.tld') |
72 | ->setTitle(static::generateContent(50)) | 72 | ->setTitle(static::generateString(50)) |
73 | ->setDescription(static::generateContent(500)) | 73 | ->setDescription(static::generateString(500)) |
74 | , | 74 | , |
75 | ]; | 75 | ]; |
76 | }) | 76 | }) |
@@ -194,8 +194,8 @@ class DailyControllerTest extends TestCase | |||
194 | (new Bookmark()) | 194 | (new Bookmark()) |
195 | ->setId(1) | 195 | ->setId(1) |
196 | ->setUrl('http://url.tld') | 196 | ->setUrl('http://url.tld') |
197 | ->setTitle(static::generateContent(50)) | 197 | ->setTitle(static::generateString(50)) |
198 | ->setDescription(static::generateContent(500)) | 198 | ->setDescription(static::generateString(500)) |
199 | , | 199 | , |
200 | ]; | 200 | ]; |
201 | }) | 201 | }) |
@@ -267,8 +267,8 @@ class DailyControllerTest extends TestCase | |||
267 | (new Bookmark()) | 267 | (new Bookmark()) |
268 | ->setId(2) | 268 | ->setId(2) |
269 | ->setUrl('http://url.tld') | 269 | ->setUrl('http://url.tld') |
270 | ->setTitle(static::generateContent(50)) | 270 | ->setTitle(static::generateString(50)) |
271 | ->setDescription(static::generateContent(5000)) | 271 | ->setDescription(static::generateString(5000)) |
272 | , | 272 | , |
273 | (new Bookmark())->setId(3)->setUrl('http://url.tld')->setTitle('title'), | 273 | (new Bookmark())->setId(3)->setUrl('http://url.tld')->setTitle('title'), |
274 | (new Bookmark())->setId(4)->setUrl('http://url.tld')->setTitle('title'), | 274 | (new Bookmark())->setId(4)->setUrl('http://url.tld')->setTitle('title'), |
@@ -473,11 +473,4 @@ class DailyControllerTest extends TestCase | |||
473 | static::assertFalse($assignedVariables['hide_timestamps']); | 473 | static::assertFalse($assignedVariables['hide_timestamps']); |
474 | static::assertCount(0, $assignedVariables['days']); | 474 | static::assertCount(0, $assignedVariables['days']); |
475 | } | 475 | } |
476 | |||
477 | protected static function generateContent(int $length): string | ||
478 | { | ||
479 | // bin2hex(random_bytes) generates string twice as long as given parameter | ||
480 | $length = (int) ceil($length / 2); | ||
481 | return bin2hex(random_bytes($length)); | ||
482 | } | ||
483 | } | 476 | } |
diff --git a/tests/front/controller/visitor/FrontControllerMockHelper.php b/tests/front/controller/visitor/FrontControllerMockHelper.php index d16b6949..fecd0c82 100644 --- a/tests/front/controller/visitor/FrontControllerMockHelper.php +++ b/tests/front/controller/visitor/FrontControllerMockHelper.php | |||
@@ -42,7 +42,7 @@ trait FrontControllerMockHelper | |||
42 | // Config | 42 | // Config |
43 | $this->container->conf = $this->createMock(ConfigManager::class); | 43 | $this->container->conf = $this->createMock(ConfigManager::class); |
44 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | 44 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { |
45 | return $default; | 45 | return $default === null ? $parameter : $default; |
46 | }); | 46 | }); |
47 | 47 | ||
48 | // PageBuilder | 48 | // PageBuilder |
@@ -101,6 +101,14 @@ trait FrontControllerMockHelper | |||
101 | ; | 101 | ; |
102 | } | 102 | } |
103 | 103 | ||
104 | protected static function generateString(int $length): string | ||
105 | { | ||
106 | // bin2hex(random_bytes) generates string twice as long as given parameter | ||
107 | $length = (int) ceil($length / 2); | ||
108 | |||
109 | return bin2hex(random_bytes($length)); | ||
110 | } | ||
111 | |||
104 | /** | 112 | /** |
105 | * Force to be used in PHPUnit context. | 113 | * Force to be used in PHPUnit context. |
106 | */ | 114 | */ |