3 * ApplicationUtils' tests
6 require_once 'application/ApplicationUtils.php';
9 * Fake ApplicationUtils class to avoid HTTP requests
11 class FakeApplicationUtils
extends ApplicationUtils
13 public static $VERSION_CODE = '';
16 * Toggle HTTP requests, allow overriding the version code
18 public static function getLatestGitVersionCode($url, $timeout=0)
20 return self
::$VERSION_CODE;
26 * Unitary tests for Shaarli utilities
28 class ApplicationUtilsTest
extends PHPUnit_Framework_TestCase
30 protected static $testUpdateFile = 'sandbox/update.txt';
31 protected static $testVersion = '0.5.0';
32 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
35 * Reset test data for each test
37 public function setUp()
39 FakeApplicationUtils
::$VERSION_CODE = '';
40 if (file_exists(self
::$testUpdateFile)) {
41 unlink(self
::$testUpdateFile);
46 * Retrieve the latest version code available on Git
48 * Expected format: Semantic Versioning - major.minor.patch
50 public function testGetLatestGitVersionCode()
56 ApplicationUtils
::getLatestGitVersionCode(
57 'https://raw.githubusercontent.com/shaarli/Shaarli/'
58 .'v0.5.4/shaarli_version.php',
63 self
::$versionPattern,
64 ApplicationUtils
::getLatestGitVersionCode(
65 'https://raw.githubusercontent.com/shaarli/Shaarli/'
66 .'master/shaarli_version.php',
73 * Attempt to retrieve the latest version from an invalid URL
75 public function testGetLatestGitVersionCodeInvalidUrl()
78 ApplicationUtils
::getLatestGitVersionCode('htttp://null.io', 0)
83 * Test update checks - the user is logged off
85 public function testCheckUpdateLoggedOff()
88 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
93 * Test update checks - the user has disabled updates
95 public function testCheckUpdateUserDisabled()
98 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
103 * A newer version is available
105 public function testCheckUpdateNewVersionNew()
107 $newVersion = '1.8.3';
108 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
110 $version = FakeApplicationUtils
::checkUpdate(
112 self
::$testUpdateFile,
118 $this->assertEquals($newVersion, $version);
122 * No available information about versions
124 public function testCheckUpdateNewVersionUnavailable()
126 $version = FakeApplicationUtils
::checkUpdate(
128 self
::$testUpdateFile,
134 $this->assertFalse($version);
138 * Shaarli is up-to-date
140 public function testCheckUpdateNewVersionUpToDate()
142 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
144 $version = FakeApplicationUtils
::checkUpdate(
146 self
::$testUpdateFile,
152 $this->assertFalse($version);
156 * Time-traveller's Shaarli
158 public function testCheckUpdateNewVersionMaartiMcFly()
160 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
162 $version = FakeApplicationUtils
::checkUpdate(
164 self
::$testUpdateFile,
170 $this->assertFalse($version);
174 * The version has been checked recently and Shaarli is up-to-date
176 public function testCheckUpdateNewVersionTwiceUpToDate()
178 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
180 // Create the update file
181 $version = FakeApplicationUtils
::checkUpdate(
183 self
::$testUpdateFile,
189 $this->assertFalse($version);
191 // Reuse the update file
192 $version = FakeApplicationUtils
::checkUpdate(
194 self
::$testUpdateFile,
200 $this->assertFalse($version);
204 * The version has been checked recently and Shaarli is outdated
206 public function testCheckUpdateNewVersionTwiceOutdated()
208 $newVersion = '1.8.3';
209 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
211 // Create the update file
212 $version = FakeApplicationUtils
::checkUpdate(
214 self
::$testUpdateFile,
219 $this->assertEquals($newVersion, $version);
221 // Reuse the update file
222 $version = FakeApplicationUtils
::checkUpdate(
224 self
::$testUpdateFile,
229 $this->assertEquals($newVersion, $version);
233 * Check supported PHP versions
235 public function testCheckSupportedPHPVersion()
238 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
239 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
240 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
244 * Check a unsupported PHP version
245 * @expectedException Exception
246 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
248 public function testCheckSupportedPHPVersion51()
250 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
254 * Check another unsupported PHP version
255 * @expectedException Exception
256 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
258 public function testCheckSupportedPHPVersion52()
260 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
264 * Checks resource permissions for the current Shaarli installation
266 public function testCheckCurrentResourcePermissions()
269 'CACHEDIR' => 'cache',
270 'CONFIG_FILE' => 'data/config.php',
272 'DATASTORE' => 'data/datastore.php',
273 'IPBANS_FILENAME' => 'data/ipbans.php',
274 'LOG_FILE' => 'data/log.txt',
275 'PAGECACHE' => 'pagecache',
276 'RAINTPL_TMP' => 'tmp',
277 'RAINTPL_TPL' => 'tpl',
278 'UPDATECHECK_FILENAME' => 'data/lastupdatecheck.txt'
282 ApplicationUtils
::checkResourcePermissions($config)
287 * Checks resource permissions for a non-existent Shaarli installation
289 public function testCheckCurrentResourcePermissionsErrors()
292 'CACHEDIR' => 'null/cache',
293 'CONFIG_FILE' => 'null/data/config.php',
294 'DATADIR' => 'null/data',
295 'DATASTORE' => 'null/data/store.php',
296 'IPBANS_FILENAME' => 'null/data/ipbans.php',
297 'LOG_FILE' => 'null/data/log.txt',
298 'PAGECACHE' => 'null/pagecache',
299 'RAINTPL_TMP' => 'null/tmp',
300 'RAINTPL_TPL' => 'null/tpl',
301 'UPDATECHECK_FILENAME' => 'null/data/lastupdatecheck.txt'
305 '"null/tpl" directory is not readable',
306 '"null/cache" directory is not readable',
307 '"null/cache" directory is not writable',
308 '"null/data" directory is not readable',
309 '"null/data" directory is not writable',
310 '"null/pagecache" directory is not readable',
311 '"null/pagecache" directory is not writable',
312 '"null/tmp" directory is not readable',
313 '"null/tmp" directory is not writable'
315 ApplicationUtils
::checkResourcePermissions($config)