]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli; | |
3 | ||
4 | use Shaarli\Config\ConfigManager; | |
5 | ||
6 | require_once 'tests/utils/FakeApplicationUtils.php'; | |
7 | ||
8 | /** | |
9 | * Unitary tests for Shaarli utilities | |
10 | */ | |
11 | class ApplicationUtilsTest extends \PHPUnit\Framework\TestCase | |
12 | { | |
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 | */ | |
20 | public function setUp() | |
21 | { | |
22 | FakeApplicationUtils::$VERSION_CODE = ''; | |
23 | if (file_exists(self::$testUpdateFile)) { | |
24 | unlink(self::$testUpdateFile); | |
25 | } | |
26 | } | |
27 | ||
28 | /** | |
29 | * Remove test version file if it exists | |
30 | */ | |
31 | public function tearDown() | |
32 | { | |
33 | if (is_file('sandbox/version.php')) { | |
34 | unlink('sandbox/version.php'); | |
35 | } | |
36 | } | |
37 | ||
38 | /** | |
39 | * Retrieve the latest version code available on Git | |
40 | * | |
41 | * Expected format: Semantic Versioning - major.minor.patch | |
42 | */ | |
43 | public function testGetVersionCode() | |
44 | { | |
45 | $testTimeout = 10; | |
46 | ||
47 | $this->assertEquals( | |
48 | '0.5.4', | |
49 | ApplicationUtils::getVersion( | |
50 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | |
51 | .'v0.5.4/shaarli_version.php', | |
52 | $testTimeout | |
53 | ) | |
54 | ); | |
55 | $this->assertRegExp( | |
56 | self::$versionPattern, | |
57 | ApplicationUtils::getVersion( | |
58 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | |
59 | .'latest/shaarli_version.php', | |
60 | $testTimeout | |
61 | ) | |
62 | ); | |
63 | } | |
64 | ||
65 | /** | |
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 | |
79 | */ | |
80 | public function testGetVersionCodeInvalidFile() | |
81 | { | |
82 | $oldlog = ini_get('error_log'); | |
83 | ini_set('error_log', '/dev/null'); | |
84 | $this->assertFalse( | |
85 | ApplicationUtils::getVersion('idontexist', 1) | |
86 | ); | |
87 | ini_set('error_log', $oldlog); | |
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 | */ | |
113 | public function testCheckUpdateNewVersionAvailable() | |
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 | ||
145 | /** | |
146 | * Test update checks - invalid Git branch | |
147 | * @expectedException Exception | |
148 | * @expectedExceptionMessageRegExp /Invalid branch selected for updates/ | |
149 | */ | |
150 | public function testCheckUpdateInvalidGitBranch() | |
151 | { | |
152 | ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable'); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Shaarli is up-to-date | |
157 | */ | |
158 | public function testCheckUpdateNewVersionUpToDate() | |
159 | { | |
160 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | |
161 | ||
162 | $version = FakeApplicationUtils::checkUpdate( | |
163 | self::$testVersion, | |
164 | self::$testUpdateFile, | |
165 | 100, | |
166 | true, | |
167 | true | |
168 | ); | |
169 | ||
170 | $this->assertFalse($version); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Time-traveller's Shaarli | |
175 | */ | |
176 | public function testCheckUpdateNewVersionMaartiMcFly() | |
177 | { | |
178 | FakeApplicationUtils::$VERSION_CODE = '0.4.1'; | |
179 | ||
180 | $version = FakeApplicationUtils::checkUpdate( | |
181 | self::$testVersion, | |
182 | self::$testUpdateFile, | |
183 | 100, | |
184 | true, | |
185 | true | |
186 | ); | |
187 | ||
188 | $this->assertFalse($version); | |
189 | } | |
190 | ||
191 | /** | |
192 | * The version has been checked recently and Shaarli is up-to-date | |
193 | */ | |
194 | public function testCheckUpdateNewVersionTwiceUpToDate() | |
195 | { | |
196 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | |
197 | ||
198 | // Create the update file | |
199 | $version = FakeApplicationUtils::checkUpdate( | |
200 | self::$testVersion, | |
201 | self::$testUpdateFile, | |
202 | 100, | |
203 | true, | |
204 | true | |
205 | ); | |
206 | ||
207 | $this->assertFalse($version); | |
208 | ||
209 | // Reuse the update file | |
210 | $version = FakeApplicationUtils::checkUpdate( | |
211 | self::$testVersion, | |
212 | self::$testUpdateFile, | |
213 | 100, | |
214 | true, | |
215 | true | |
216 | ); | |
217 | ||
218 | $this->assertFalse($version); | |
219 | } | |
220 | ||
221 | /** | |
222 | * The version has been checked recently and Shaarli is outdated | |
223 | */ | |
224 | public function testCheckUpdateNewVersionTwiceOutdated() | |
225 | { | |
226 | $newVersion = '1.8.3'; | |
227 | FakeApplicationUtils::$VERSION_CODE = $newVersion; | |
228 | ||
229 | // Create the update file | |
230 | $version = FakeApplicationUtils::checkUpdate( | |
231 | self::$testVersion, | |
232 | self::$testUpdateFile, | |
233 | 100, | |
234 | true, | |
235 | true | |
236 | ); | |
237 | $this->assertEquals($newVersion, $version); | |
238 | ||
239 | // Reuse the update file | |
240 | $version = FakeApplicationUtils::checkUpdate( | |
241 | self::$testVersion, | |
242 | self::$testUpdateFile, | |
243 | 100, | |
244 | true, | |
245 | true | |
246 | ); | |
247 | $this->assertEquals($newVersion, $version); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Check supported PHP versions | |
252 | */ | |
253 | public function testCheckSupportedPHPVersion() | |
254 | { | |
255 | $minVersion = '5.3'; | |
256 | ApplicationUtils::checkPHPVersion($minVersion, '5.4.32'); | |
257 | ApplicationUtils::checkPHPVersion($minVersion, '5.5'); | |
258 | ApplicationUtils::checkPHPVersion($minVersion, '5.6.10'); | |
259 | } | |
260 | ||
261 | /** | |
262 | * Check a unsupported PHP version | |
263 | * @expectedException Exception | |
264 | * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ | |
265 | */ | |
266 | public function testCheckSupportedPHPVersion51() | |
267 | { | |
268 | ApplicationUtils::checkPHPVersion('5.3', '5.1.0'); | |
269 | } | |
270 | ||
271 | /** | |
272 | * Check another unsupported PHP version | |
273 | * @expectedException Exception | |
274 | * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ | |
275 | */ | |
276 | public function testCheckSupportedPHPVersion52() | |
277 | { | |
278 | ApplicationUtils::checkPHPVersion('5.3', '5.2'); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Checks resource permissions for the current Shaarli installation | |
283 | */ | |
284 | public function testCheckCurrentResourcePermissions() | |
285 | { | |
286 | $conf = new ConfigManager(''); | |
287 | $conf->set('resource.thumbnails_cache', 'cache'); | |
288 | $conf->set('resource.config', 'data/config.php'); | |
289 | $conf->set('resource.data_dir', 'data'); | |
290 | $conf->set('resource.datastore', 'data/datastore.php'); | |
291 | $conf->set('resource.ban_file', 'data/ipbans.php'); | |
292 | $conf->set('resource.log', 'data/log.txt'); | |
293 | $conf->set('resource.page_cache', 'pagecache'); | |
294 | $conf->set('resource.raintpl_tmp', 'tmp'); | |
295 | $conf->set('resource.raintpl_tpl', 'tpl'); | |
296 | $conf->set('resource.theme', 'default'); | |
297 | $conf->set('resource.update_check', 'data/lastupdatecheck.txt'); | |
298 | ||
299 | $this->assertEquals( | |
300 | array(), | |
301 | ApplicationUtils::checkResourcePermissions($conf) | |
302 | ); | |
303 | } | |
304 | ||
305 | /** | |
306 | * Checks resource permissions for a non-existent Shaarli installation | |
307 | */ | |
308 | public function testCheckCurrentResourcePermissionsErrors() | |
309 | { | |
310 | $conf = new ConfigManager(''); | |
311 | $conf->set('resource.thumbnails_cache', 'null/cache'); | |
312 | $conf->set('resource.config', 'null/data/config.php'); | |
313 | $conf->set('resource.data_dir', 'null/data'); | |
314 | $conf->set('resource.datastore', 'null/data/store.php'); | |
315 | $conf->set('resource.ban_file', 'null/data/ipbans.php'); | |
316 | $conf->set('resource.log', 'null/data/log.txt'); | |
317 | $conf->set('resource.page_cache', 'null/pagecache'); | |
318 | $conf->set('resource.raintpl_tmp', 'null/tmp'); | |
319 | $conf->set('resource.raintpl_tpl', 'null/tpl'); | |
320 | $conf->set('resource.raintpl_theme', 'null/tpl/default'); | |
321 | $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt'); | |
322 | $this->assertEquals( | |
323 | array( | |
324 | '"null/tpl" directory is not readable', | |
325 | '"null/tpl/default" directory is not readable', | |
326 | '"null/cache" directory is not readable', | |
327 | '"null/cache" directory is not writable', | |
328 | '"null/data" directory is not readable', | |
329 | '"null/data" directory is not writable', | |
330 | '"null/pagecache" directory is not readable', | |
331 | '"null/pagecache" directory is not writable', | |
332 | '"null/tmp" directory is not readable', | |
333 | '"null/tmp" directory is not writable' | |
334 | ), | |
335 | ApplicationUtils::checkResourcePermissions($conf) | |
336 | ); | |
337 | } | |
338 | ||
339 | /** | |
340 | * Check update with 'dev' as curent version (master branch). | |
341 | * It should always return false. | |
342 | */ | |
343 | public function testCheckUpdateDev() | |
344 | { | |
345 | $this->assertFalse( | |
346 | ApplicationUtils::checkUpdate('dev', self::$testUpdateFile, 100, true, true) | |
347 | ); | |
348 | } | |
349 | } |