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