]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/ApplicationUtilsTest.php
Fix version check test
[github/shaarli/Shaarli.git] / tests / ApplicationUtilsTest.php
1 <?php
2 /**
3 * ApplicationUtils' tests
4 */
5
6 require_once 'application/config/ConfigManager.php';
7 require_once 'application/ApplicationUtils.php';
8
9 /**
10 * Fake ApplicationUtils class to avoid HTTP requests
11 */
12 class 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
25
26 /**
27 * Unitary tests for Shaarli utilities
28 */
29 class ApplicationUtilsTest extends PHPUnit_Framework_TestCase
30 {
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 );
63 $this->assertRegExp(
64 self::$versionPattern,
65 ApplicationUtils::getLatestGitVersionCode(
66 'https://raw.githubusercontent.com/shaarli/Shaarli/'
67 .'stable/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 $oldlog = ini_get('error_log');
79 ini_set('error_log', '/dev/null');
80 $this->assertFalse(
81 ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1)
82 );
83 ini_set('error_log', $oldlog);
84 }
85
86 /**
87 * Test update checks - the user is logged off
88 */
89 public function testCheckUpdateLoggedOff()
90 {
91 $this->assertFalse(
92 ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false)
93 );
94 }
95
96 /**
97 * Test update checks - the user has disabled updates
98 */
99 public function testCheckUpdateUserDisabled()
100 {
101 $this->assertFalse(
102 ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true)
103 );
104 }
105
106 /**
107 * A newer version is available
108 */
109 public function testCheckUpdateNewVersionAvailable()
110 {
111 $newVersion = '1.8.3';
112 FakeApplicationUtils::$VERSION_CODE = $newVersion;
113
114 $version = FakeApplicationUtils::checkUpdate(
115 self::$testVersion,
116 self::$testUpdateFile,
117 100,
118 true,
119 true
120 );
121
122 $this->assertEquals($newVersion, $version);
123 }
124
125 /**
126 * No available information about versions
127 */
128 public function testCheckUpdateNewVersionUnavailable()
129 {
130 $version = FakeApplicationUtils::checkUpdate(
131 self::$testVersion,
132 self::$testUpdateFile,
133 100,
134 true,
135 true
136 );
137
138 $this->assertFalse($version);
139 }
140
141 /**
142 * Test update checks - invalid Git branch
143 * @expectedException Exception
144 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
145 */
146 public function testCheckUpdateInvalidGitBranch()
147 {
148 ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable');
149 }
150
151 /**
152 * Shaarli is up-to-date
153 */
154 public function testCheckUpdateNewVersionUpToDate()
155 {
156 FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
157
158 $version = FakeApplicationUtils::checkUpdate(
159 self::$testVersion,
160 self::$testUpdateFile,
161 100,
162 true,
163 true
164 );
165
166 $this->assertFalse($version);
167 }
168
169 /**
170 * Time-traveller's Shaarli
171 */
172 public function testCheckUpdateNewVersionMaartiMcFly()
173 {
174 FakeApplicationUtils::$VERSION_CODE = '0.4.1';
175
176 $version = FakeApplicationUtils::checkUpdate(
177 self::$testVersion,
178 self::$testUpdateFile,
179 100,
180 true,
181 true
182 );
183
184 $this->assertFalse($version);
185 }
186
187 /**
188 * The version has been checked recently and Shaarli is up-to-date
189 */
190 public function testCheckUpdateNewVersionTwiceUpToDate()
191 {
192 FakeApplicationUtils::$VERSION_CODE = self::$testVersion;
193
194 // Create the update file
195 $version = FakeApplicationUtils::checkUpdate(
196 self::$testVersion,
197 self::$testUpdateFile,
198 100,
199 true,
200 true
201 );
202
203 $this->assertFalse($version);
204
205 // Reuse the update file
206 $version = FakeApplicationUtils::checkUpdate(
207 self::$testVersion,
208 self::$testUpdateFile,
209 100,
210 true,
211 true
212 );
213
214 $this->assertFalse($version);
215 }
216
217 /**
218 * The version has been checked recently and Shaarli is outdated
219 */
220 public function testCheckUpdateNewVersionTwiceOutdated()
221 {
222 $newVersion = '1.8.3';
223 FakeApplicationUtils::$VERSION_CODE = $newVersion;
224
225 // Create the update file
226 $version = FakeApplicationUtils::checkUpdate(
227 self::$testVersion,
228 self::$testUpdateFile,
229 100,
230 true,
231 true
232 );
233 $this->assertEquals($newVersion, $version);
234
235 // Reuse the update file
236 $version = FakeApplicationUtils::checkUpdate(
237 self::$testVersion,
238 self::$testUpdateFile,
239 100,
240 true,
241 true
242 );
243 $this->assertEquals($newVersion, $version);
244 }
245
246 /**
247 * Check supported PHP versions
248 */
249 public function testCheckSupportedPHPVersion()
250 {
251 $minVersion = '5.3';
252 ApplicationUtils::checkPHPVersion($minVersion, '5.4.32');
253 ApplicationUtils::checkPHPVersion($minVersion, '5.5');
254 ApplicationUtils::checkPHPVersion($minVersion, '5.6.10');
255 }
256
257 /**
258 * Check a unsupported PHP version
259 * @expectedException Exception
260 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
261 */
262 public function testCheckSupportedPHPVersion51()
263 {
264 ApplicationUtils::checkPHPVersion('5.3', '5.1.0');
265 }
266
267 /**
268 * Check another unsupported PHP version
269 * @expectedException Exception
270 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
271 */
272 public function testCheckSupportedPHPVersion52()
273 {
274 ApplicationUtils::checkPHPVersion('5.3', '5.2');
275 }
276
277 /**
278 * Checks resource permissions for the current Shaarli installation
279 */
280 public function testCheckCurrentResourcePermissions()
281 {
282 $conf = new ConfigManager('');
283 $conf->set('resource.thumbnails_cache', 'cache');
284 $conf->set('resource.config', 'data/config.php');
285 $conf->set('resource.data_dir', 'data');
286 $conf->set('resource.datastore', 'data/datastore.php');
287 $conf->set('resource.ban_file', 'data/ipbans.php');
288 $conf->set('resource.log', 'data/log.txt');
289 $conf->set('resource.page_cache', 'pagecache');
290 $conf->set('resource.raintpl_tmp', 'tmp');
291 $conf->set('resource.raintpl_tpl', 'tpl');
292 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
293
294 $this->assertEquals(
295 array(),
296 ApplicationUtils::checkResourcePermissions($conf)
297 );
298 }
299
300 /**
301 * Checks resource permissions for a non-existent Shaarli installation
302 */
303 public function testCheckCurrentResourcePermissionsErrors()
304 {
305 $conf = new ConfigManager('');
306 $conf->set('resource.thumbnails_cache', 'null/cache');
307 $conf->set('resource.config', 'null/data/config.php');
308 $conf->set('resource.data_dir', 'null/data');
309 $conf->set('resource.datastore', 'null/data/store.php');
310 $conf->set('resource.ban_file', 'null/data/ipbans.php');
311 $conf->set('resource.log', 'null/data/log.txt');
312 $conf->set('resource.page_cache', 'null/pagecache');
313 $conf->set('resource.raintpl_tmp', 'null/tmp');
314 $conf->set('resource.raintpl_tpl', 'null/tpl');
315 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
316 $this->assertEquals(
317 array(
318 '"null/tpl" directory is not readable',
319 '"null/cache" directory is not readable',
320 '"null/cache" directory is not writable',
321 '"null/data" directory is not readable',
322 '"null/data" directory is not writable',
323 '"null/pagecache" directory is not readable',
324 '"null/pagecache" directory is not writable',
325 '"null/tmp" directory is not readable',
326 '"null/tmp" directory is not writable'
327 ),
328 ApplicationUtils::checkResourcePermissions($conf)
329 );
330 }
331 }