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