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