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