]>
Commit | Line | Data |
---|---|---|
2e28269b | 1 | <?php |
9778a155 | 2 | namespace Shaarli; |
4bf35ba5 | 3 | |
9778a155 | 4 | use Shaarli\Config\ConfigManager; |
4bf35ba5 | 5 | |
9778a155 | 6 | require_once 'tests/utils/FakeApplicationUtils.php'; |
2e28269b V |
7 | |
8 | /** | |
9 | * Unitary tests for Shaarli utilities | |
10 | */ | |
a5a9cf23 | 11 | class ApplicationUtilsTest extends \Shaarli\TestCase |
2e28269b | 12 | { |
4bf35ba5 V |
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 | */ | |
8f60e120 | 20 | protected function setUp(): void |
4bf35ba5 V |
21 | { |
22 | FakeApplicationUtils::$VERSION_CODE = ''; | |
23 | if (file_exists(self::$testUpdateFile)) { | |
24 | unlink(self::$testUpdateFile); | |
25 | } | |
26 | } | |
27 | ||
b786c883 A |
28 | /** |
29 | * Remove test version file if it exists | |
30 | */ | |
8f60e120 | 31 | protected function tearDown(): void |
b786c883 A |
32 | { |
33 | if (is_file('sandbox/version.php')) { | |
34 | unlink('sandbox/version.php'); | |
35 | } | |
36 | } | |
37 | ||
4bf35ba5 V |
38 | /** |
39 | * Retrieve the latest version code available on Git | |
40 | * | |
41 | * Expected format: Semantic Versioning - major.minor.patch | |
42 | */ | |
b786c883 | 43 | public function testGetVersionCode() |
4bf35ba5 V |
44 | { |
45 | $testTimeout = 10; | |
46 | ||
47 | $this->assertEquals( | |
48 | '0.5.4', | |
b786c883 | 49 | ApplicationUtils::getVersion( |
4bf35ba5 | 50 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' |
067c2dd8 | 51 | .'v0.5.4/shaarli_version.php', |
4bf35ba5 V |
52 | $testTimeout |
53 | ) | |
54 | ); | |
684e662a | 55 | $this->assertRegExp( |
4bf35ba5 | 56 | self::$versionPattern, |
b786c883 | 57 | ApplicationUtils::getVersion( |
4bf35ba5 | 58 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' |
067c2dd8 | 59 | .'latest/shaarli_version.php', |
4bf35ba5 V |
60 | $testTimeout |
61 | ) | |
62 | ); | |
63 | } | |
64 | ||
65 | /** | |
b786c883 A |
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 | |
4bf35ba5 | 79 | */ |
b786c883 | 80 | public function testGetVersionCodeInvalidFile() |
4bf35ba5 | 81 | { |
87f9f4f9 A |
82 | $oldlog = ini_get('error_log'); |
83 | ini_set('error_log', '/dev/null'); | |
4bf35ba5 | 84 | $this->assertFalse( |
b786c883 | 85 | ApplicationUtils::getVersion('idontexist', 1) |
4bf35ba5 | 86 | ); |
87f9f4f9 | 87 | ini_set('error_log', $oldlog); |
4bf35ba5 V |
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 | */ | |
4407b45f | 113 | public function testCheckUpdateNewVersionAvailable() |
4bf35ba5 V |
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 | ||
4407b45f V |
145 | /** |
146 | * Test update checks - invalid Git branch | |
4407b45f V |
147 | */ |
148 | public function testCheckUpdateInvalidGitBranch() | |
149 | { | |
f447edb7 | 150 | $this->expectException(\Exception::class); |
b1baca99 A |
151 | $this->expectExceptionMessageRegExp('/Invalid branch selected for updates/'); |
152 | ||
4407b45f V |
153 | ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable'); |
154 | } | |
155 | ||
4bf35ba5 V |
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 | ||
c9cf2715 V |
251 | /** |
252 | * Check supported PHP versions | |
253 | */ | |
254 | public function testCheckSupportedPHPVersion() | |
255 | { | |
256 | $minVersion = '5.3'; | |
def39d0d A |
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')); | |
c9cf2715 V |
260 | } |
261 | ||
262 | /** | |
263 | * Check a unsupported PHP version | |
c9cf2715 V |
264 | */ |
265 | public function testCheckSupportedPHPVersion51() | |
266 | { | |
f447edb7 | 267 | $this->expectException(\Exception::class); |
b1baca99 A |
268 | $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); |
269 | ||
def39d0d | 270 | $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.1.0')); |
c9cf2715 V |
271 | } |
272 | ||
273 | /** | |
274 | * Check another unsupported PHP version | |
c9cf2715 V |
275 | */ |
276 | public function testCheckSupportedPHPVersion52() | |
277 | { | |
f447edb7 | 278 | $this->expectException(\Exception::class); |
b1baca99 A |
279 | $this->expectExceptionMessageRegExp('/Your PHP version is obsolete/'); |
280 | ||
def39d0d | 281 | $this->assertTrue(ApplicationUtils::checkPHPVersion('5.3', '5.2')); |
c9cf2715 V |
282 | } |
283 | ||
2e28269b V |
284 | /** |
285 | * Checks resource permissions for the current Shaarli installation | |
286 | */ | |
287 | public function testCheckCurrentResourcePermissions() | |
288 | { | |
278d9ee2 | 289 | $conf = new ConfigManager(''); |
894a3c4b A |
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'); | |
adc4aee8 | 299 | $conf->set('resource.theme', 'default'); |
894a3c4b | 300 | $conf->set('resource.update_check', 'data/lastupdatecheck.txt'); |
684e662a | 301 | |
2e28269b V |
302 | $this->assertEquals( |
303 | array(), | |
278d9ee2 | 304 | ApplicationUtils::checkResourcePermissions($conf) |
2e28269b V |
305 | ); |
306 | } | |
307 | ||
308 | /** | |
309 | * Checks resource permissions for a non-existent Shaarli installation | |
310 | */ | |
311 | public function testCheckCurrentResourcePermissionsErrors() | |
312 | { | |
278d9ee2 | 313 | $conf = new ConfigManager(''); |
894a3c4b A |
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'); | |
adc4aee8 | 323 | $conf->set('resource.raintpl_theme', 'null/tpl/default'); |
894a3c4b | 324 | $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); |
2e28269b V |
325 | $this->assertEquals( |
326 | array( | |
327 | '"null/tpl" directory is not readable', | |
adc4aee8 | 328 | '"null/tpl/default" directory is not readable', |
2e28269b V |
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 | ), | |
278d9ee2 | 338 | ApplicationUtils::checkResourcePermissions($conf) |
2e28269b V |
339 | ); |
340 | } | |
b897c81f A |
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 | } | |
2e28269b | 352 | } |