2 use Shaarli\Config\ConfigManager
;
5 * ApplicationUtils' tests
8 require_once 'application/ApplicationUtils.php';
11 * Fake ApplicationUtils class to avoid HTTP requests
13 class FakeApplicationUtils
extends ApplicationUtils
15 public static $VERSION_CODE = '';
18 * Toggle HTTP requests, allow overriding the version code
20 public static function getLatestGitVersionCode($url, $timeout=0)
22 return self
::$VERSION_CODE;
28 * Unitary tests for Shaarli utilities
30 class ApplicationUtilsTest
extends PHPUnit_Framework_TestCase
32 protected static $testUpdateFile = 'sandbox/update.txt';
33 protected static $testVersion = '0.5.0';
34 protected static $versionPattern = '/^\d+\.\d+\.\d+$/';
37 * Reset test data for each test
39 public function setUp()
41 FakeApplicationUtils
::$VERSION_CODE = '';
42 if (file_exists(self
::$testUpdateFile)) {
43 unlink(self
::$testUpdateFile);
48 * Retrieve the latest version code available on Git
50 * Expected format: Semantic Versioning - major.minor.patch
52 public function testGetLatestGitVersionCode()
58 ApplicationUtils
::getLatestGitVersionCode(
59 'https://raw.githubusercontent.com/shaarli/Shaarli/'
60 .'v0.5.4/shaarli_version.php',
65 self
::$versionPattern,
66 ApplicationUtils
::getLatestGitVersionCode(
67 'https://raw.githubusercontent.com/shaarli/Shaarli/'
68 .'master/shaarli_version.php',
75 * Attempt to retrieve the latest version from an invalid URL
77 public function testGetLatestGitVersionCodeInvalidUrl()
79 $oldlog = ini_get('error_log');
80 ini_set('error_log', '/dev/null');
82 ApplicationUtils
::getLatestGitVersionCode('htttp://null.io', 1)
84 ini_set('error_log', $oldlog);
88 * Test update checks - the user is logged off
90 public function testCheckUpdateLoggedOff()
93 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, false)
98 * Test update checks - the user has disabled updates
100 public function testCheckUpdateUserDisabled()
103 ApplicationUtils
::checkUpdate(self
::$testVersion, 'null', 0, false, true)
108 * A newer version is available
110 public function testCheckUpdateNewVersionAvailable()
112 $newVersion = '1.8.3';
113 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
115 $version = FakeApplicationUtils
::checkUpdate(
117 self
::$testUpdateFile,
123 $this->assertEquals($newVersion, $version);
127 * No available information about versions
129 public function testCheckUpdateNewVersionUnavailable()
131 $version = FakeApplicationUtils
::checkUpdate(
133 self
::$testUpdateFile,
139 $this->assertFalse($version);
143 * Test update checks - invalid Git branch
144 * @expectedException Exception
145 * @expectedExceptionMessageRegExp /Invalid branch selected for updates/
147 public function testCheckUpdateInvalidGitBranch()
149 ApplicationUtils
::checkUpdate('', 'null', 0, true, true, 'unstable');
153 * Shaarli is up-to-date
155 public function testCheckUpdateNewVersionUpToDate()
157 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
159 $version = FakeApplicationUtils
::checkUpdate(
161 self
::$testUpdateFile,
167 $this->assertFalse($version);
171 * Time-traveller's Shaarli
173 public function testCheckUpdateNewVersionMaartiMcFly()
175 FakeApplicationUtils
::$VERSION_CODE = '0.4.1';
177 $version = FakeApplicationUtils
::checkUpdate(
179 self
::$testUpdateFile,
185 $this->assertFalse($version);
189 * The version has been checked recently and Shaarli is up-to-date
191 public function testCheckUpdateNewVersionTwiceUpToDate()
193 FakeApplicationUtils
::$VERSION_CODE = self
::$testVersion;
195 // Create the update file
196 $version = FakeApplicationUtils
::checkUpdate(
198 self
::$testUpdateFile,
204 $this->assertFalse($version);
206 // Reuse the update file
207 $version = FakeApplicationUtils
::checkUpdate(
209 self
::$testUpdateFile,
215 $this->assertFalse($version);
219 * The version has been checked recently and Shaarli is outdated
221 public function testCheckUpdateNewVersionTwiceOutdated()
223 $newVersion = '1.8.3';
224 FakeApplicationUtils
::$VERSION_CODE = $newVersion;
226 // Create the update file
227 $version = FakeApplicationUtils
::checkUpdate(
229 self
::$testUpdateFile,
234 $this->assertEquals($newVersion, $version);
236 // Reuse the update file
237 $version = FakeApplicationUtils
::checkUpdate(
239 self
::$testUpdateFile,
244 $this->assertEquals($newVersion, $version);
248 * Check supported PHP versions
250 public function testCheckSupportedPHPVersion()
253 ApplicationUtils
::checkPHPVersion($minVersion, '5.4.32');
254 ApplicationUtils
::checkPHPVersion($minVersion, '5.5');
255 ApplicationUtils
::checkPHPVersion($minVersion, '5.6.10');
259 * Check a unsupported PHP version
260 * @expectedException Exception
261 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
263 public function testCheckSupportedPHPVersion51()
265 ApplicationUtils
::checkPHPVersion('5.3', '5.1.0');
269 * Check another unsupported PHP version
270 * @expectedException Exception
271 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
273 public function testCheckSupportedPHPVersion52()
275 ApplicationUtils
::checkPHPVersion('5.3', '5.2');
279 * Checks resource permissions for the current Shaarli installation
281 public function testCheckCurrentResourcePermissions()
283 $conf = new ConfigManager('');
284 $conf->set('resource.thumbnails_cache', 'cache');
285 $conf->set('resource.config', 'data/config.php');
286 $conf->set('resource.data_dir', 'data');
287 $conf->set('resource.datastore', 'data/datastore.php');
288 $conf->set('resource.ban_file', 'data/ipbans.php');
289 $conf->set('resource.log', 'data/log.txt');
290 $conf->set('resource.page_cache', 'pagecache');
291 $conf->set('resource.raintpl_tmp', 'tmp');
292 $conf->set('resource.raintpl_tpl', 'tpl');
293 $conf->set('resource.theme', 'default');
294 $conf->set('resource.update_check', 'data/lastupdatecheck.txt');
298 ApplicationUtils
::checkResourcePermissions($conf)
303 * Checks resource permissions for a non-existent Shaarli installation
305 public function testCheckCurrentResourcePermissionsErrors()
307 $conf = new ConfigManager('');
308 $conf->set('resource.thumbnails_cache', 'null/cache');
309 $conf->set('resource.config', 'null/data/config.php');
310 $conf->set('resource.data_dir', 'null/data');
311 $conf->set('resource.datastore', 'null/data/store.php');
312 $conf->set('resource.ban_file', 'null/data/ipbans.php');
313 $conf->set('resource.log', 'null/data/log.txt');
314 $conf->set('resource.page_cache', 'null/pagecache');
315 $conf->set('resource.raintpl_tmp', 'null/tmp');
316 $conf->set('resource.raintpl_tpl', 'null/tpl');
317 $conf->set('resource.raintpl_theme', 'null/tpl/default');
318 $conf->set('resource.update_check', 'null/data/lastupdatecheck.txt');
321 '"null/tpl" directory is not readable',
322 '"null/tpl/default" directory is not readable',
323 '"null/cache" directory is not readable',
324 '"null/cache" directory is not writable',
325 '"null/data" directory is not readable',
326 '"null/data" directory is not writable',
327 '"null/pagecache" directory is not readable',
328 '"null/pagecache" directory is not writable',
329 '"null/tmp" directory is not readable',
330 '"null/tmp" directory is not writable'
332 ApplicationUtils
::checkResourcePermissions($conf)