]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/helper/ApplicationUtilsTest.php
Merge tag 'v0.12.1' into latest
[github/shaarli/Shaarli.git] / tests / helper / ApplicationUtilsTest.php
CommitLineData
2e28269b 1<?php
c2cd15da 2namespace Shaarli\Helper;
4bf35ba5 3
9778a155 4use Shaarli\Config\ConfigManager;
c2cd15da 5use Shaarli\FakeApplicationUtils;
4bf35ba5 6
9778a155 7require_once 'tests/utils/FakeApplicationUtils.php';
2e28269b
V
8
9/**
10 * Unitary tests for Shaarli utilities
11 */
a5a9cf23 12class ApplicationUtilsTest extends \Shaarli\TestCase
2e28269b 13{
4bf35ba5
V
14 protected static $testUpdateFile = 'sandbox/update.txt';
15 protected static $testVersion = '0.5.0';
16 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
17
18 /**
19 * Reset test data for each test
20 */
8f60e120 21 protected function setUp(): void
4bf35ba5
V
22 {
23 FakeApplicationUtils::$VERSION_CODE = '';
24 if (file_exists(self::$testUpdateFile)) {
25 unlink(self::$testUpdateFile);
26 }
27 }
28
b786c883
A
29 /**
30 * Remove test version file if it exists
31 */
8f60e120 32 protected function tearDown(): void
b786c883
A
33 {
34 if (is_file('sandbox/version.php')) {
35 unlink('sandbox/version.php');
36 }
37 }
38
4bf35ba5
V
39 /**
40 * Retrieve the latest version code available on Git
41 *
42 * Expected format: Semantic Versioning - major.minor.patch
43 */
b786c883 44 public function testGetVersionCode()
4bf35ba5
V
45 {
46 $testTimeout = 10;
47
48 $this->assertEquals(
49 '0.5.4',
b786c883 50 ApplicationUtils::getVersion(
4bf35ba5 51 'https://raw.githubusercontent.com/shaarli/Shaarli/'
067c2dd8 52 .'v0.5.4/shaarli_version.php',
4bf35ba5
V
53 $testTimeout
54 )
55 );
684e662a 56 $this->assertRegExp(
4bf35ba5 57 self::$versionPattern,
b786c883 58 ApplicationUtils::getVersion(
4bf35ba5 59 'https://raw.githubusercontent.com/shaarli/Shaarli/'
067c2dd8 60 .'latest/shaarli_version.php',
4bf35ba5
V
61 $testTimeout
62 )
63 );
64 }
65
66 /**
b786c883
A
67 * Attempt to retrieve the latest version from an invalid File
68 */
69 public function testGetVersionCodeFromFile()
70 {
71 file_put_contents('sandbox/version.php', '<?php /* 1.2.3 */ ?>'. PHP_EOL);
72 $this->assertEquals(
73 '1.2.3',
74 ApplicationUtils::getVersion('sandbox/version.php', 1)
75 );
76 }
77
78 /**
79 * Attempt to retrieve the latest version from an invalid File
4bf35ba5 80 */
b786c883 81 public function testGetVersionCodeInvalidFile()
4bf35ba5 82 {
87f9f4f9
A
83 $oldlog = ini_get('error_log');
84 ini_set('error_log', '/dev/null');
4bf35ba5 85 $this->assertFalse(
b786c883 86 ApplicationUtils::getVersion('idontexist', 1)
4bf35ba5 87 );
87f9f4f9 88 ini_set('error_log', $oldlog);
4bf35ba5
V
89 }
90
91 /**
92 * Test update checks - the user is logged off
93 */
94 public function testCheckUpdateLoggedOff()
95 {
96 $this->assertFalse(
97 ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false)
98 );
99 }
100
101 /**
102 * Test update checks - the user has disabled updates
103 */
104 public function testCheckUpdateUserDisabled()
105 {
106 $this->assertFalse(
107 ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true)
108 );
109 }
110
111 /**
112 * A newer version is available
113 */
4407b45f 114 public function testCheckUpdateNewVersionAvailable()
4bf35ba5
V
115 {
116 $newVersion = '1.8.3';
117 FakeApplicationUtils::$VERSION_CODE = $newVersion;
118
119 $version = FakeApplicationUtils::checkUpdate(
120 self::$testVersion,
121 self::$testUpdateFile,
122 100,
123 true,
124 true
125 );
126
127 $this->assertEquals($newVersion, $version);
128 }
129
130 /**
131 * No available information about versions
132 */
133 public function testCheckUpdateNewVersionUnavailable()
134 {
135 $version = FakeApplicationUtils::checkUpdate(
136 self::$testVersion,
137 self::$testUpdateFile,
138 100,
139 true,
140 true
141 );
142
143 $this->assertFalse($version);
144 }
145
4407b45f
V
146 /**
147 * Test update checks - invalid Git branch
4407b45f
V
148 */
149 public function testCheckUpdateInvalidGitBranch()
150 {
f447edb7 151 $this->expectException(\Exception::class);
b1baca99
A
152 $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/');
153
4407b45f
V
154 ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
155 }
156
4bf35ba5
V
157 /**
158 * Shaarli is up-to-date
159 */
160 public function testCheckUpdateNewVersionUpToDate()
161 {
162 FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
163
164 $version = FakeApplicationUtils::checkUpdate(
165 self::$testVersion,
166 self::$testUpdateFile,
167 100,
168 true,
169 true
170 );
171
172 $this->assertFalse($version);
173 }
174
175 /**
176 * Time-traveller's Shaarli
177 */
178 public function testCheckUpdateNewVersionMaartiMcFly()
179 {
180 FakeApplicationUtils::$VERSION_CODE = '0.4.1';
181
182 $version = FakeApplicationUtils::checkUpdate(
183 self::$testVersion,
184 self::$testUpdateFile,
185 100,
186 true,
187 true
188 );
189
190 $this->assertFalse($version);
191 }
192
193 /**
194 * The version has been checked recently and Shaarli is up-to-date
195 */
196 public function testCheckUpdateNewVersionTwiceUpToDate()
197 {
198 FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
199
200 // Create the update file
201 $version = FakeApplicationUtils::checkUpdate(
202 self::$testVersion,
203 self::$testUpdateFile,
204 100,
205 true,
206 true
207 );
208
209 $this->assertFalse($version);
210
211 // Reuse the update file
212 $version = FakeApplicationUtils::checkUpdate(
213 self::$testVersion,
214 self::$testUpdateFile,
215 100,
216 true,
217 true
218 );
219
220 $this->assertFalse($version);
221 }
222
223 /**
224 * The version has been checked recently and Shaarli is outdated
225 */
226 public function testCheckUpdateNewVersionTwiceOutdated()
227 {
228 $newVersion = '1.8.3';
229 FakeApplicationUtils::$VERSION_CODE = $newVersion;
230
231 // Create the update file
232 $version = FakeApplicationUtils::checkUpdate(
233 self::$testVersion,
234 self::$testUpdateFile,
235 100,
236 true,
237 true
238 );
239 $this->assertEquals($newVersion, $version);
240
241 // Reuse the update file
242 $version = FakeApplicationUtils::checkUpdate(
243 self::$testVersion,
244 self::$testUpdateFile,
245 100,
246 true,
247 true
248 );
249 $this->assertEquals($newVersion, $version);
250 }
251
c9cf2715
V
252 /**
253 * Check supported PHP versions
254 */
255 public function testCheckSupportedPHPVersion()
256 {
257 $minVersion = '5.3';
def39d0d
A
258 $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.4.32'));
259 $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.5'));
260 $this->assertTrue(ApplicationUtils::checkPHPVersion($minVersion, '5.6.10'));
c9cf2715
V
261 }
262
263 /**
264 * Check a unsupported PHP version
c9cf2715
V
265 */
266 public function testCheckSupportedPHPVersion51()
267 {
f447edb7 268 $this->expectException(\Exception::class);
b1baca99
A
269 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
270
def39d0d 271 $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0'));
c9cf2715
V
272 }
273
274 /**
275 * Check another unsupported PHP version
c9cf2715
V
276 */
277 public function testCheckSupportedPHPVersion52()
278 {
f447edb7 279 $this->expectException(\Exception::class);
b1baca99
A
280 $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/');
281
def39d0d 282 $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2'));
c9cf2715
V
283 }
284
2e28269b
V
285 /**
286 * Checks resource permissions for the current Shaarli installation
287 */
288 public function testCheckCurrentResourcePermissions()
289 {
278d9ee2 290 $conf = new ConfigManager('');
894a3c4b
A
291 $conf->set('resource.thumbnails_cache', 'cache');
292 $conf->set('resource.config', 'data/config.php');
293 $conf->set('resource.data_dir', 'data');
294 $conf->set('resource.datastore', 'data/datastore.php');
295 $conf->set('resource.ban_file', 'data/ipbans.php');
296 $conf->set('resource.log', 'data/log.txt');
297 $conf->set('resource.page_cache', 'pagecache');
298 $conf->set('resource.raintpl_tmp', 'tmp');
299 $conf->set('resource.raintpl_tpl', 'tpl');
adc4aee8 300 $conf->set('resource.theme', 'default');
894a3c4b 301 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
684e662a 302
2e28269b
V
303 $this->assertEquals(
304 array(),
278d9ee2 305 ApplicationUtils::checkResourcePermissions($conf)
2e28269b
V
306 );
307 }
308
309 /**
310 * Checks resource permissions for a non-existent Shaarli installation
311 */
312 public function testCheckCurrentResourcePermissionsErrors()
313 {
278d9ee2 314 $conf = new ConfigManager('');
894a3c4b
A
315 $conf->set('resource.thumbnails_cache', 'null/cache');
316 $conf->set('resource.config', 'null/data/config.php');
317 $conf->set('resource.data_dir', 'null/data');
318 $conf->set('resource.datastore', 'null/data/store.php');
319 $conf->set('resource.ban_file', 'null/data/ipbans.php');
320 $conf->set('resource.log', 'null/data/log.txt');
321 $conf->set('resource.page_cache', 'null/pagecache');
322 $conf->set('resource.raintpl_tmp', 'null/tmp');
323 $conf->set('resource.raintpl_tpl', 'null/tpl');
adc4aee8 324 $conf->set('resource.raintpl_theme', 'null/tpl/default');
894a3c4b 325 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
2e28269b
V
326 $this->assertEquals(
327 array(
328 '"null/tpl" directory is not readable',
adc4aee8 329 '"null/tpl/default" directory is not readable',
2e28269b
V
330 '"null/cache" directory is not readable',
331 '"null/cache" directory is not writable',
332 '"null/data" directory is not readable',
333 '"null/data" directory is not writable',
334 '"null/pagecache" directory is not readable',
335 '"null/pagecache" directory is not writable',
336 '"null/tmp" directory is not readable',
337 '"null/tmp" directory is not writable'
338 ),
278d9ee2 339 ApplicationUtils::checkResourcePermissions($conf)
2e28269b
V
340 );
341 }
b897c81f 342
0cf76ccb
A
343 /**
344 * Checks resource permissions in minimal mode.
345 */
346 public function testCheckCurrentResourcePermissionsErrorsMinimalMode(): void
347 {
348 $conf = new ConfigManager('');
349 $conf->set('resource.thumbnails_cache', 'null/cache');
350 $conf->set('resource.config', 'null/data/config.php');
351 $conf->set('resource.data_dir', 'null/data');
352 $conf->set('resource.datastore', 'null/data/store.php');
353 $conf->set('resource.ban_file', 'null/data/ipbans.php');
354 $conf->set('resource.log', 'null/data/log.txt');
355 $conf->set('resource.page_cache', 'null/pagecache');
356 $conf->set('resource.raintpl_tmp', 'null/tmp');
357 $conf->set('resource.raintpl_tpl', 'null/tpl');
358 $conf->set('resource.raintpl_theme', 'null/tpl/default');
359 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
360
361 static::assertSame(
362 [
363 '"null/tpl" directory is not readable',
364 '"null/tpl/default" directory is not readable',
365 '"null/tmp" directory is not readable',
366 '"null/tmp" directory is not writable'
367 ],
368 ApplicationUtils::checkResourcePermissions($conf, true)
369 );
370 }
371
b897c81f
A
372 /**
373 * Check update with 'dev' as curent version (master branch).
374 * It should always return false.
375 */
376 public function testCheckUpdateDev()
377 {
378 $this->assertFalse(
379 ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true)
380 );
381 }
0cf76ccb
A
382
383 /**
384 * Basic test of getPhpExtensionsRequirement()
385 */
386 public function testGetPhpExtensionsRequirementSimple(): void
387 {
388 static::assertCount(8, ApplicationUtils::getPhpExtensionsRequirement());
389 static::assertSame([
390 'name' => 'json',
391 'required' => true,
392 'desc' => 'Configuration parsing',
393 'loaded' => true,
394 ], ApplicationUtils::getPhpExtensionsRequirement()[0]);
395 }
396
397 /**
398 * Test getPhpEol with a known version: 7.4 -> 2022
399 */
400 public function testGetKnownPhpEol(): void
401 {
402 static::assertSame('2022-11-28', ApplicationUtils::getPhpEol('7.4.7'));
403 }
404
405 /**
406 * Test getPhpEol with an unknown version: 7.4 -> 2022
407 */
408 public function testGetUnknownPhpEol(): void
409 {
410 static::assertSame(
411 (((int) (new \DateTime())->format('Y')) + 2) . (new \DateTime())->format('-m-d'),
412 ApplicationUtils::getPhpEol('7.51.34')
413 );
414 }
2e28269b 415}