diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ApplicationUtilsTest.php | 62 | ||||
-rw-r--r-- | tests/FileUtilsTest.php | 88 | ||||
-rw-r--r-- | tests/front/controller/admin/ServerControllerTest.php | 184 | ||||
-rw-r--r-- | tests/front/controller/visitor/InstallControllerTest.php | 9 |
4 files changed, 342 insertions, 1 deletions
diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index a232b351..ac46cbf1 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php | |||
@@ -340,6 +340,35 @@ class ApplicationUtilsTest extends \Shaarli\TestCase | |||
340 | } | 340 | } |
341 | 341 | ||
342 | /** | 342 | /** |
343 | * Checks resource permissions in minimal mode. | ||
344 | */ | ||
345 | public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void | ||
346 | { | ||
347 | $conf = new ConfigManager(''); | ||
348 | $conf->set('resource.thumbnails_cache', 'null/cache'); | ||
349 | $conf->set('resource.config', 'null/data/config.php'); | ||
350 | $conf->set('resource.data_dir', 'null/data'); | ||
351 | $conf->set('resource.datastore', 'null/data/store.php'); | ||
352 | $conf->set('resource.ban_file', 'null/data/ipbans.php'); | ||
353 | $conf->set('resource.log', 'null/data/log.txt'); | ||
354 | $conf->set('resource.page_cache', 'null/pagecache'); | ||
355 | $conf->set('resource.raintpl_tmp', 'null/tmp'); | ||
356 | $conf->set('resource.raintpl_tpl', 'null/tpl'); | ||
357 | $conf->set('resource.raintpl_theme', 'null/tpl/default'); | ||
358 | $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); | ||
359 | |||
360 | static::assertSame( | ||
361 | [ | ||
362 | '"null/tpl" directory is not readable', | ||
363 | '"null/tpl/default" directory is not readable', | ||
364 | '"null/tmp" directory is not readable', | ||
365 | '"null/tmp" directory is not writable' | ||
366 | ], | ||
367 | ApplicationUtils::checkResourcePermissions($conf, true) | ||
368 | ); | ||
369 | } | ||
370 | |||
371 | /** | ||
343 | * Check update with 'dev' as curent version (master branch). | 372 | * Check update with 'dev' as curent version (master branch). |
344 | * It should always return false. | 373 | * It should always return false. |
345 | */ | 374 | */ |
@@ -349,4 +378,37 @@ class ApplicationUtilsTest extends \Shaarli\TestCase | |||
349 | ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) | 378 | ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) |
350 | ); | 379 | ); |
351 | } | 380 | } |
381 | |||
382 | /** | ||
383 | * Basic test of getPhpExtensionsRequirement() | ||
384 | */ | ||
385 | public function testGetPhpExtensionsRequirementSimple(): void | ||
386 | { | ||
387 | static::assertCount(8, ApplicationUtils::getPhpExtensionsRequirement()); | ||
388 | static::assertSame([ | ||
389 | 'name' => 'json', | ||
390 | 'required' => true, | ||
391 | 'desc' => 'Configuration parsing', | ||
392 | 'loaded' => true, | ||
393 | ], ApplicationUtils::getPhpExtensionsRequirement()[0]); | ||
394 | } | ||
395 | |||
396 | /** | ||
397 | * Test getPhpEol with a known version: 7.4 -> 2022 | ||
398 | */ | ||
399 | public function testGetKnownPhpEol(): void | ||
400 | { | ||
401 | static::assertSame('2022-11-28', ApplicationUtils::getPhpEol('7.4.7')); | ||
402 | } | ||
403 | |||
404 | /** | ||
405 | * Test getPhpEol with an unknown version: 7.4 -> 2022 | ||
406 | */ | ||
407 | public function testGetUnknownPhpEol(): void | ||
408 | { | ||
409 | static::assertSame( | ||
410 | (((int) (new \DateTime())->format('Y')) + 2) . (new \DateTime())->format('-m-d'), | ||
411 | ApplicationUtils::getPhpEol('7.51.34') | ||
412 | ); | ||
413 | } | ||
352 | } | 414 | } |
diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php index 9163bdf1..3384504a 100644 --- a/tests/FileUtilsTest.php +++ b/tests/FileUtilsTest.php | |||
@@ -3,25 +3,48 @@ | |||
3 | namespace Shaarli; | 3 | namespace Shaarli; |
4 | 4 | ||
5 | use Exception; | 5 | use Exception; |
6 | use Shaarli\Exceptions\IOException; | ||
6 | 7 | ||
7 | /** | 8 | /** |
8 | * Class FileUtilsTest | 9 | * Class FileUtilsTest |
9 | * | 10 | * |
10 | * Test file utility class. | 11 | * Test file utility class. |
11 | */ | 12 | */ |
12 | class FileUtilsTest extends \Shaarli\TestCase | 13 | class FileUtilsTest extends TestCase |
13 | { | 14 | { |
14 | /** | 15 | /** |
15 | * @var string Test file path. | 16 | * @var string Test file path. |
16 | */ | 17 | */ |
17 | protected static $file = 'sandbox/flat.db'; | 18 | protected static $file = 'sandbox/flat.db'; |
18 | 19 | ||
20 | protected function setUp(): void | ||
21 | { | ||
22 | @mkdir('sandbox'); | ||
23 | mkdir('sandbox/folder2'); | ||
24 | touch('sandbox/file1'); | ||
25 | touch('sandbox/file2'); | ||
26 | mkdir('sandbox/folder1'); | ||
27 | touch('sandbox/folder1/file1'); | ||
28 | touch('sandbox/folder1/file2'); | ||
29 | mkdir('sandbox/folder3'); | ||
30 | mkdir('/tmp/shaarli-to-delete'); | ||
31 | } | ||
32 | |||
19 | /** | 33 | /** |
20 | * Delete test file after every test. | 34 | * Delete test file after every test. |
21 | */ | 35 | */ |
22 | protected function tearDown(): void | 36 | protected function tearDown(): void |
23 | { | 37 | { |
24 | @unlink(self::$file); | 38 | @unlink(self::$file); |
39 | |||
40 | @unlink('sandbox/folder1/file1'); | ||
41 | @unlink('sandbox/folder1/file2'); | ||
42 | @rmdir('sandbox/folder1'); | ||
43 | @unlink('sandbox/file1'); | ||
44 | @unlink('sandbox/file2'); | ||
45 | @rmdir('sandbox/folder2'); | ||
46 | @rmdir('sandbox/folder3'); | ||
47 | @rmdir('/tmp/shaarli-to-delete'); | ||
25 | } | 48 | } |
26 | 49 | ||
27 | /** | 50 | /** |
@@ -107,4 +130,67 @@ class FileUtilsTest extends \Shaarli\TestCase | |||
107 | $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); | 130 | $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); |
108 | $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); | 131 | $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); |
109 | } | 132 | } |
133 | |||
134 | /** | ||
135 | * Test clearFolder with self delete and excluded files | ||
136 | */ | ||
137 | public function testClearFolderSelfDeleteWithExclusion(): void | ||
138 | { | ||
139 | FileUtils::clearFolder('sandbox', true, ['file2']); | ||
140 | |||
141 | static::assertFileExists('sandbox/folder1/file2'); | ||
142 | static::assertFileExists('sandbox/folder1'); | ||
143 | static::assertFileExists('sandbox/file2'); | ||
144 | static::assertFileExists('sandbox'); | ||
145 | |||
146 | static::assertFileNotExists('sandbox/folder1/file1'); | ||
147 | static::assertFileNotExists('sandbox/file1'); | ||
148 | static::assertFileNotExists('sandbox/folder3'); | ||
149 | } | ||
150 | |||
151 | /** | ||
152 | * Test clearFolder with self delete and excluded files | ||
153 | */ | ||
154 | public function testClearFolderSelfDeleteWithoutExclusion(): void | ||
155 | { | ||
156 | FileUtils::clearFolder('sandbox', true); | ||
157 | |||
158 | static::assertFileNotExists('sandbox'); | ||
159 | } | ||
160 | |||
161 | /** | ||
162 | * Test clearFolder with self delete and excluded files | ||
163 | */ | ||
164 | public function testClearFolderNoSelfDeleteWithoutExclusion(): void | ||
165 | { | ||
166 | FileUtils::clearFolder('sandbox', false); | ||
167 | |||
168 | static::assertFileExists('sandbox'); | ||
169 | |||
170 | // 2 because '.' and '..' | ||
171 | static::assertCount(2, new \DirectoryIterator('sandbox')); | ||
172 | } | ||
173 | |||
174 | /** | ||
175 | * Test clearFolder on a file instead of a folder | ||
176 | */ | ||
177 | public function testClearFolderOnANonDirectory(): void | ||
178 | { | ||
179 | $this->expectException(IOException::class); | ||
180 | $this->expectExceptionMessage('Provided path is not a directory.'); | ||
181 | |||
182 | FileUtils::clearFolder('sandbox/file1', false); | ||
183 | } | ||
184 | |||
185 | /** | ||
186 | * Test clearFolder on a file instead of a folder | ||
187 | */ | ||
188 | public function testClearFolderOutsideOfShaarliDirectory(): void | ||
189 | { | ||
190 | $this->expectException(IOException::class); | ||
191 | $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.'); | ||
192 | |||
193 | |||
194 | FileUtils::clearFolder('/tmp/shaarli-to-delete', true); | ||
195 | } | ||
110 | } | 196 | } |
diff --git a/tests/front/controller/admin/ServerControllerTest.php b/tests/front/controller/admin/ServerControllerTest.php new file mode 100644 index 00000000..355cce7d --- /dev/null +++ b/tests/front/controller/admin/ServerControllerTest.php | |||
@@ -0,0 +1,184 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Shaarli\Security\SessionManager; | ||
9 | use Shaarli\TestCase; | ||
10 | use Slim\Http\Request; | ||
11 | use Slim\Http\Response; | ||
12 | |||
13 | /** | ||
14 | * Test Server administration controller. | ||
15 | */ | ||
16 | class ServerControllerTest extends TestCase | ||
17 | { | ||
18 | use FrontAdminControllerMockHelper; | ||
19 | |||
20 | /** @var ServerController */ | ||
21 | protected $controller; | ||
22 | |||
23 | public function setUp(): void | ||
24 | { | ||
25 | $this->createContainer(); | ||
26 | |||
27 | $this->controller = new ServerController($this->container); | ||
28 | |||
29 | // initialize dummy cache | ||
30 | @mkdir('sandbox/'); | ||
31 | foreach (['pagecache', 'tmp', 'cache'] as $folder) { | ||
32 | @mkdir('sandbox/' . $folder); | ||
33 | @touch('sandbox/' . $folder . '/.htaccess'); | ||
34 | @touch('sandbox/' . $folder . '/1'); | ||
35 | @touch('sandbox/' . $folder . '/2'); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | public function tearDown(): void | ||
40 | { | ||
41 | foreach (['pagecache', 'tmp', 'cache'] as $folder) { | ||
42 | @unlink('sandbox/' . $folder . '/.htaccess'); | ||
43 | @unlink('sandbox/' . $folder . '/1'); | ||
44 | @unlink('sandbox/' . $folder . '/2'); | ||
45 | @rmdir('sandbox/' . $folder); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Test default display of server administration page. | ||
51 | */ | ||
52 | public function testIndex(): void | ||
53 | { | ||
54 | $request = $this->createMock(Request::class); | ||
55 | $response = new Response(); | ||
56 | |||
57 | // Save RainTPL assigned variables | ||
58 | $assignedVariables = []; | ||
59 | $this->assignTemplateVars($assignedVariables); | ||
60 | |||
61 | $result = $this->controller->index($request, $response); | ||
62 | |||
63 | static::assertSame(200, $result->getStatusCode()); | ||
64 | static::assertSame('server', (string) $result->getBody()); | ||
65 | |||
66 | static::assertSame(PHP_VERSION, $assignedVariables['php_version']); | ||
67 | static::assertArrayHasKey('php_has_reached_eol', $assignedVariables); | ||
68 | static::assertArrayHasKey('php_eol', $assignedVariables); | ||
69 | static::assertArrayHasKey('php_extensions', $assignedVariables); | ||
70 | static::assertArrayHasKey('permissions', $assignedVariables); | ||
71 | static::assertEmpty($assignedVariables['permissions']); | ||
72 | |||
73 | static::assertRegExp( | ||
74 | '#https://github\.com/shaarli/Shaarli/releases/tag/v\d+\.\d+\.\d+#', | ||
75 | $assignedVariables['release_url'] | ||
76 | ); | ||
77 | static::assertRegExp('#v\d+\.\d+\.\d+#', $assignedVariables['latest_version']); | ||
78 | static::assertRegExp('#(v\d+\.\d+\.\d+|dev)#', $assignedVariables['current_version']); | ||
79 | static::assertArrayHasKey('index_url', $assignedVariables); | ||
80 | static::assertArrayHasKey('client_ip', $assignedVariables); | ||
81 | static::assertArrayHasKey('trusted_proxies', $assignedVariables); | ||
82 | |||
83 | static::assertSame('Server administration - Shaarli', $assignedVariables['pagetitle']); | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * Test clearing the main cache | ||
88 | */ | ||
89 | public function testClearMainCache(): void | ||
90 | { | ||
91 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
92 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
93 | if ($key === 'resource.page_cache') { | ||
94 | return 'sandbox/pagecache'; | ||
95 | } elseif ($key === 'resource.raintpl_tmp') { | ||
96 | return 'sandbox/tmp'; | ||
97 | } elseif ($key === 'resource.thumbnails_cache') { | ||
98 | return 'sandbox/cache'; | ||
99 | } else { | ||
100 | return $default; | ||
101 | } | ||
102 | }); | ||
103 | |||
104 | $this->container->sessionManager | ||
105 | ->expects(static::once()) | ||
106 | ->method('setSessionParameter') | ||
107 | ->with(SessionManager::KEY_SUCCESS_MESSAGES, ['Shaarli\'s cache folder has been cleared!']) | ||
108 | ; | ||
109 | |||
110 | $request = $this->createMock(Request::class); | ||
111 | $request->method('getQueryParam')->with('type')->willReturn('main'); | ||
112 | $response = new Response(); | ||
113 | |||
114 | $result = $this->controller->clearCache($request, $response); | ||
115 | |||
116 | static::assertSame(302, $result->getStatusCode()); | ||
117 | static::assertSame('/subfolder/admin/server', (string) $result->getHeaderLine('Location')); | ||
118 | |||
119 | static::assertFileNotExists('sandbox/pagecache/1'); | ||
120 | static::assertFileNotExists('sandbox/pagecache/2'); | ||
121 | static::assertFileNotExists('sandbox/tmp/1'); | ||
122 | static::assertFileNotExists('sandbox/tmp/2'); | ||
123 | |||
124 | static::assertFileExists('sandbox/pagecache/.htaccess'); | ||
125 | static::assertFileExists('sandbox/tmp/.htaccess'); | ||
126 | static::assertFileExists('sandbox/cache'); | ||
127 | static::assertFileExists('sandbox/cache/.htaccess'); | ||
128 | static::assertFileExists('sandbox/cache/1'); | ||
129 | static::assertFileExists('sandbox/cache/2'); | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * Test clearing thumbnails cache | ||
134 | */ | ||
135 | public function testClearThumbnailsCache(): void | ||
136 | { | ||
137 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
138 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
139 | if ($key === 'resource.page_cache') { | ||
140 | return 'sandbox/pagecache'; | ||
141 | } elseif ($key === 'resource.raintpl_tmp') { | ||
142 | return 'sandbox/tmp'; | ||
143 | } elseif ($key === 'resource.thumbnails_cache') { | ||
144 | return 'sandbox/cache'; | ||
145 | } else { | ||
146 | return $default; | ||
147 | } | ||
148 | }); | ||
149 | |||
150 | $this->container->sessionManager | ||
151 | ->expects(static::once()) | ||
152 | ->method('setSessionParameter') | ||
153 | ->willReturnCallback(function (string $key, array $value): SessionManager { | ||
154 | static::assertSame(SessionManager::KEY_WARNING_MESSAGES, $key); | ||
155 | static::assertCount(1, $value); | ||
156 | static::assertStringStartsWith('Thumbnails cache has been cleared.', $value[0]); | ||
157 | |||
158 | return $this->container->sessionManager; | ||
159 | }); | ||
160 | ; | ||
161 | |||
162 | $request = $this->createMock(Request::class); | ||
163 | $request->method('getQueryParam')->with('type')->willReturn('thumbnails'); | ||
164 | $response = new Response(); | ||
165 | |||
166 | $result = $this->controller->clearCache($request, $response); | ||
167 | |||
168 | static::assertSame(302, $result->getStatusCode()); | ||
169 | static::assertSame('/subfolder/admin/server', (string) $result->getHeaderLine('Location')); | ||
170 | |||
171 | static::assertFileNotExists('sandbox/cache/1'); | ||
172 | static::assertFileNotExists('sandbox/cache/2'); | ||
173 | |||
174 | static::assertFileExists('sandbox/cache/.htaccess'); | ||
175 | static::assertFileExists('sandbox/pagecache'); | ||
176 | static::assertFileExists('sandbox/pagecache/.htaccess'); | ||
177 | static::assertFileExists('sandbox/pagecache/1'); | ||
178 | static::assertFileExists('sandbox/pagecache/2'); | ||
179 | static::assertFileExists('sandbox/tmp'); | ||
180 | static::assertFileExists('sandbox/tmp/.htaccess'); | ||
181 | static::assertFileExists('sandbox/tmp/1'); | ||
182 | static::assertFileExists('sandbox/tmp/2'); | ||
183 | } | ||
184 | } | ||
diff --git a/tests/front/controller/visitor/InstallControllerTest.php b/tests/front/controller/visitor/InstallControllerTest.php index 345ad544..2105ed77 100644 --- a/tests/front/controller/visitor/InstallControllerTest.php +++ b/tests/front/controller/visitor/InstallControllerTest.php | |||
@@ -79,6 +79,15 @@ class InstallControllerTest extends TestCase | |||
79 | static::assertIsArray($assignedVariables['languages']); | 79 | static::assertIsArray($assignedVariables['languages']); |
80 | static::assertSame('Automatic', $assignedVariables['languages']['auto']); | 80 | static::assertSame('Automatic', $assignedVariables['languages']['auto']); |
81 | static::assertSame('French', $assignedVariables['languages']['fr']); | 81 | static::assertSame('French', $assignedVariables['languages']['fr']); |
82 | |||
83 | static::assertSame(PHP_VERSION, $assignedVariables['php_version']); | ||
84 | static::assertArrayHasKey('php_has_reached_eol', $assignedVariables); | ||
85 | static::assertArrayHasKey('php_eol', $assignedVariables); | ||
86 | static::assertArrayHasKey('php_extensions', $assignedVariables); | ||
87 | static::assertArrayHasKey('permissions', $assignedVariables); | ||
88 | static::assertEmpty($assignedVariables['permissions']); | ||
89 | |||
90 | static::assertSame('Install Shaarli', $assignedVariables['pagetitle']); | ||
82 | } | 91 | } |
83 | 92 | ||
84 | /** | 93 | /** |