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