From d1e2f8e52c931f84c11d4f54f32959710d528182 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 11 Jul 2015 01:29:12 +0200 Subject: PHP: ensure 5.3 compatibility, refactor timezone utilities Relates to #250 Modifications - supported version - bump required version from 5.1.0 to 5.3.x - update README - add PHP 5.3 to Travis environments - rewrite array declarations: explicitely use array() instead of [] - move checkPHPVersion to application/Utils.php - move timezone functions to application/TimeZone.php - cleanup code - improve test coverage Signed-off-by: VirtualTam --- .travis.yml | 1 + README.md | 2 +- application/Config.php | 6 +-- application/TimeZone.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++ application/Utils.php | 23 +++++++++- index.php | 109 +++++++++++----------------------------------- tests/ConfigTest.php | 10 ++--- tests/LinkDBTest.php | 12 +++--- tests/TimeZoneTest.php | 83 +++++++++++++++++++++++++++++++++++ tests/UtilsTest.php | 34 ++++++++++++++- 10 files changed, 288 insertions(+), 102 deletions(-) create mode 100644 application/TimeZone.php create mode 100644 tests/TimeZoneTest.php diff --git a/.travis.yml b/.travis.yml index bcaf682c..d1c70172 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ php: - 5.6 - 5.5 - 5.4 + - 5.3 install: - composer self-update - composer install diff --git a/README.md b/README.md index 15d4379a..b3dbaf5f 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Password: `demo` ## Installing -Shaarli requires php 5.4. `php-gd` is optional and provides thumbnail resizing. +Shaarli requires PHP 5.3. `php-gd` is optional and provides thumbnail resizing. * Download the latest stable release from https://github.com/shaarli/Shaarli/releases * Unpack the archive in a directory on your web server diff --git a/application/Config.php b/application/Config.php index 0b01b524..ec799d7f 100755 --- a/application/Config.php +++ b/application/Config.php @@ -19,10 +19,10 @@ function writeConfig($config, $isLoggedIn) { // These fields are required in configuration. - $MANDATORY_FIELDS = [ + $MANDATORY_FIELDS = array( 'login', 'hash', 'salt', 'timezone', 'title', 'titleLink', 'redirector', 'disablesessionprotection', 'privateLinkByDefault' - ]; + ); if (!isset($config['config']['CONFIG_FILE'])) { throw new MissingFieldConfigException('CONFIG_FILE'); @@ -126,4 +126,4 @@ class UnauthorizedConfigException extends Exception { $this->message = 'You are not authorized to alter config.'; } -} \ No newline at end of file +} diff --git a/application/TimeZone.php b/application/TimeZone.php new file mode 100644 index 00000000..ccbef918 --- /dev/null +++ b/application/TimeZone.php @@ -0,0 +1,110 @@ +'.$city.''; + } + } + + $continents_html = ''; + $continents = array_keys($continents); + + foreach ($continents as $continent) { + $continents_html .= ''; - } - } - $continents_html = ''; - $continents = array_keys($continents); - foreach($continents as $continent) - $continents_html.=''; - $cities_html = $cities[$pcontinent]; - $timezone_form = "Continent: "; - $timezone_form .= "    City:
"; - $timezone_js = "" ; - return array($timezone_form,$timezone_js); - } - return array('',''); -} - -// Tells if a timezone is valid or not. -// If not valid, returns false. -// If system does not support timezone list, returns false. -function isTZvalid($continent,$city) -{ - $tz = $continent.'/'.$city; - if (function_exists('timezone_identifiers_list')) // because of old PHP version (5.1) which can be found on free.fr - { - if (in_array($tz, timezone_identifiers_list())) // it's a valid timezone? - return true; - } - return false; -} if (!function_exists('json_encode')) { function json_encode($data) { switch ($type = gettype($data)) { diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 4279c57e..a239d8b7 100755 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -18,7 +18,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase */ public function setUp() { - self::$_configFields = [ + self::$_configFields = array( 'login' => 'login', 'hash' => 'hash', 'salt' => 'salt', @@ -28,13 +28,13 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'redirector' => '', 'disablesessionprotection' => false, 'privateLinkByDefault' => false, - 'config' => [ + 'config' => array( 'CONFIG_FILE' => 'tests/config.php', 'DATADIR' => 'tests', 'config1' => 'config1data', 'config2' => 'config2data', - ] - ]; + ) + ); } /** @@ -174,4 +174,4 @@ class ConfigTest extends PHPUnit_Framework_TestCase include self::$_configFields['config']['CONFIG_FILE']; $this->assertEquals(self::$_configFields['login'], $GLOBALS['login']); } -} \ No newline at end of file +} diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index d34ea4f5..504c8190 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -228,12 +228,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testDays() { $this->assertEquals( - ['20121206', '20130614', '20150310'], + array('20121206', '20130614', '20150310'), self::$publicLinkDB->days() ); $this->assertEquals( - ['20121206', '20130614', '20141125', '20150310'], + array('20121206', '20130614', '20141125', '20150310'), self::$privateLinkDB->days() ); } @@ -269,7 +269,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testAllTags() { $this->assertEquals( - [ + array( 'web' => 3, 'cartoon' => 2, 'gnu' => 2, @@ -279,12 +279,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'software' => 1, 'stallman' => 1, 'free' => 1 - ], + ), self::$publicLinkDB->allTags() ); $this->assertEquals( - [ + array( 'web' => 4, 'cartoon' => 3, 'gnu' => 2, @@ -298,7 +298,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'w3c' => 1, 'css' => 1, 'Mercurial' => 1 - ], + ), self::$privateLinkDB->allTags() ); } diff --git a/tests/TimeZoneTest.php b/tests/TimeZoneTest.php new file mode 100644 index 00000000..f3de3913 --- /dev/null +++ b/tests/TimeZoneTest.php @@ -0,0 +1,83 @@ +assertStringStartsWith('Continent:assertContains('selected="selected"', $generated[0]); + $this->assertStringEndsWith('
', $generated[0]); + + // Javascript handler + $this->assertStringStartsWith('', $generated[1]); + } + + /** + * Generate a timezone selection form, with a preselected timezone + */ + public function testGenerateTimeZoneFormPreselected() + { + $generated = generateTimeZoneForm('Antarctica/Syowa'); + + // HTML form + $this->assertStringStartsWith('Continent:assertContains( + 'value="Antarctica" selected="selected"', + $generated[0] + ); + $this->assertContains( + 'value="Syowa" selected="selected"', + $generated[0] + ); + $this->assertStringEndsWith('
', $generated[0]); + + + // Javascript handler + $this->assertStringStartsWith('', $generated[1]); + } + + /** + * Check valid timezones + */ + public function testValidTimeZone() + { + $this->assertTrue(isTimeZoneValid('America', 'Argentina/Ushuaia')); + $this->assertTrue(isTimeZoneValid('Europe', 'Oslo')); + $this->assertTrue(isTimeZoneValid('UTC', 'UTC')); + } + + /** + * Check invalid timezones + */ + public function testInvalidTimeZone() + { + $this->assertFalse(isTimeZoneValid('CEST', 'CEST')); + $this->assertFalse(isTimeZoneValid('Europe', 'Atlantis')); + $this->assertFalse(isTimeZoneValid('Middle_Earth', 'Moria')); + } +} +?> diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 8355c7f8..28e15f5a 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -109,7 +109,7 @@ class UtilsTest extends PHPUnit_Framework_TestCase */ public function testGenerateLocationLoop() { $ref = 'http://localhost/?test'; - $this->assertEquals('?', generateLocation($ref, 'localhost', ['test'])); + $this->assertEquals('?', generateLocation($ref, 'localhost', array('test'))); } /** @@ -119,4 +119,36 @@ class UtilsTest extends PHPUnit_Framework_TestCase $ref = 'http://somewebsite.com/?test'; $this->assertEquals('?', generateLocation($ref, 'localhost')); } + + /** + * Check supported PHP versions + */ + public function testCheckSupportedPHPVersion() + { + $minVersion = '5.3'; + checkPHPVersion($minVersion, '5.4.32'); + checkPHPVersion($minVersion, '5.5'); + checkPHPVersion($minVersion, '5.6.10'); + } + + /** + * Check a unsupported PHP version + * @expectedException Exception + * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ + */ + public function testCheckSupportedPHPVersion51() + { + checkPHPVersion('5.3', '5.1.0'); + } + + /** + * Check another unsupported PHP version + * @expectedException Exception + * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ + */ + public function testCheckSupportedPHPVersion52() + { + checkPHPVersion('5.3', '5.2'); + } } +?> -- cgit v1.2.3