From 04a0e8ea34c241fdf6bd30b11f5242656f9cd1c2 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 3 Jan 2017 12:01:25 +0100 Subject: [PATCH] Updater: keep custom theme preference with the new theme setting --- application/ApplicationUtils.php | 20 ------------ application/ThemeUtils.php | 33 +++++++++++++++++++ application/Updater.php | 29 +++++++++++++++++ composer.json | 1 + tests/ApplicationUtilsTest.php | 44 ------------------------- tests/ThemeUtilsTest.php | 55 ++++++++++++++++++++++++++++++++ tests/Updater/UpdaterTest.php | 44 +++++++++++++++++++++++++ tpl/default/configure.html | 6 +--- 8 files changed, 163 insertions(+), 69 deletions(-) create mode 100644 application/ThemeUtils.php create mode 100644 tests/ThemeUtilsTest.php diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php index cc009a1d..a0f482b0 100644 --- a/application/ApplicationUtils.php +++ b/application/ApplicationUtils.php @@ -195,24 +195,4 @@ class ApplicationUtils return $errors; } - - /** - * Get a list of available themes. - * - * It will return the name of any directory present in the template folder. - * - * @param string $tplDir Templates main directory. - * - * @return array List of theme names. - */ - public static function getThemes($tplDir) - { - $allTheme = glob($tplDir.'/*', GLOB_ONLYDIR); - $themes = []; - foreach ($allTheme as $value) { - $themes[] = str_replace($tplDir.'/', '', $value); - } - - return $themes; - } } diff --git a/application/ThemeUtils.php b/application/ThemeUtils.php new file mode 100644 index 00000000..2718ed13 --- /dev/null +++ b/application/ThemeUtils.php @@ -0,0 +1,33 @@ +conf->write($this->isLoggedIn); return true; } + + /** + * New setting: theme name. If the default theme is used, nothing to do. + * + * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory, + * and the current theme is set as default in the theme setting. + * + * @return bool true if the update is successful, false otherwise. + */ + public function updateMethodDefaultTheme() + { + // raintpl_tpl isn't the root template directory anymore. + // We run the update only if this folder still contains the template files. + $tplDir = $this->conf->get('resource.raintpl_tpl'); + $tplFile = $tplDir . '/linklist.html'; + if (! file_exists($tplFile)) { + return true; + } + + $parent = dirname($tplDir); + $this->conf->set('resource.raintpl_tpl', $parent); + $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/')); + $this->conf->write($this->isLoggedIn); + + // Dependency injection gore + RainTPL::$tpl_dir = $tplDir; + + return true; + } } /** diff --git a/composer.json b/composer.json index cfbde1a0..2fed0df7 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ }, "autoload": { "psr-4": { + "Shaarli\\": "application", "Shaarli\\Api\\": "application/api/", "Shaarli\\Api\\Controllers\\": "application/api/controllers", "Shaarli\\Api\\Exceptions\\": "application/api/exceptions" diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index c39649e8..634bd0ed 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php @@ -331,48 +331,4 @@ class ApplicationUtilsTest extends PHPUnit_Framework_TestCase ApplicationUtils::checkResourcePermissions($conf) ); } - - /** - * Test getThemes() with existing theme directories. - */ - public function testGetThemes() - { - $themes = ['theme1', 'default', 'Bl1p_- bL0p']; - foreach ($themes as $theme) { - mkdir('sandbox/tpl/'. $theme, 0777, true); - } - - // include a file which should be ignored - touch('sandbox/tpl/supertheme'); - - $res = ApplicationUtils::getThemes('sandbox/tpl/'); - foreach ($res as $theme) { - $this->assertTrue(in_array($theme, $themes)); - } - $this->assertFalse(in_array('supertheme', $res)); - - foreach ($themes as $theme) { - rmdir('sandbox/tpl/'. $theme); - } - unlink('sandbox/tpl/supertheme'); - rmdir('sandbox/tpl'); - } - - /** - * Test getThemes() without any theme dir. - */ - public function testGetThemesEmpty() - { - mkdir('sandbox/tpl/', 0777, true); - $this->assertEquals([], ApplicationUtils::getThemes('sandbox/tpl/')); - rmdir('sandbox/tpl/'); - } - - /** - * Test getThemes() with an invalid path. - */ - public function testGetThemesInvalid() - { - $this->assertEquals([], ApplicationUtils::getThemes('nope')); - } } diff --git a/tests/ThemeUtilsTest.php b/tests/ThemeUtilsTest.php new file mode 100644 index 00000000..e44564be --- /dev/null +++ b/tests/ThemeUtilsTest.php @@ -0,0 +1,55 @@ +assertTrue(in_array($theme, $themes)); + } + $this->assertFalse(in_array('supertheme', $res)); + + foreach ($themes as $theme) { + rmdir('sandbox/tpl/'. $theme); + } + unlink('sandbox/tpl/supertheme'); + rmdir('sandbox/tpl'); + } + + /** + * Test getThemes() without any theme dir. + */ + public function testGetThemesEmpty() + { + mkdir('sandbox/tpl/', 0755, true); + $this->assertEquals([], ThemeUtils::getThemes('sandbox/tpl/')); + rmdir('sandbox/tpl/'); + } + + /** + * Test getThemes() with an invalid path. + */ + public function testGetThemesInvalid() + { + $this->assertEquals([], ThemeUtils::getThemes('nope')); + } +} diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index a1530996..1d15cfaa 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -422,4 +422,48 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertTrue($updater->updateMethodDatastoreIds()); $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); } + + /** + * Test defaultTheme update with default settings: nothing to do. + */ + public function testDefaultThemeWithDefaultSettings() + { + $sandbox = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); + $this->conf = new ConfigManager($sandbox); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDefaultTheme()); + + $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals('default', $this->conf->get('resource.theme')); + $this->conf = new ConfigManager($sandbox); + $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals('default', $this->conf->get('resource.theme')); + unlink($sandbox . '.json.php'); + } + + /** + * Test defaultTheme update with a custom theme in a subfolder + */ + public function testDefaultThemeWithCustomTheme() + { + $theme = 'iamanartist'; + $sandbox = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); + $this->conf = new ConfigManager($sandbox); + mkdir('sandbox/'. $theme); + touch('sandbox/'. $theme .'/linklist.html'); + $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/'); + $updater = new Updater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDefaultTheme()); + + $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals($theme, $this->conf->get('resource.theme')); + $this->conf = new ConfigManager($sandbox); + $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals($theme, $this->conf->get('resource.theme')); + unlink($sandbox . '.json.php'); + unlink('sandbox/'. $theme .'/linklist.html'); + rmdir('sandbox/'. $theme); + } } diff --git a/tpl/default/configure.html b/tpl/default/configure.html index e71133b4..5820e6e4 100644 --- a/tpl/default/configure.html +++ b/tpl/default/configure.html @@ -25,11 +25,7 @@