]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Updater: keep custom theme preference with the new theme setting
authorArthurHoaro <arthur@hoa.ro>
Tue, 3 Jan 2017 11:01:25 +0000 (12:01 +0100)
committerArthurHoaro <arthur@hoa.ro>
Thu, 5 Jan 2017 15:16:27 +0000 (16:16 +0100)
application/ApplicationUtils.php
application/ThemeUtils.php [new file with mode: 0644]
application/Updater.php
composer.json
tests/ApplicationUtilsTest.php
tests/ThemeUtilsTest.php [new file with mode: 0644]
tests/Updater/UpdaterTest.php
tpl/default/configure.html

index cc009a1d25807fe4167750edf4110f6d3a704705..a0f482b0b9791e0f4c1e28b5406dc4895459a239 100644 (file)
@@ -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 (file)
index 0000000..2718ed1
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace Shaarli;
+
+/**
+ * Class ThemeUtils
+ *
+ * Utility functions related to theme management.
+ *
+ * @package Shaarli
+ */
+class ThemeUtils
+{
+    /**
+     * 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;
+    }
+}
index 38de33503a417ac06c9b186298c7621a471d3667..621c7238336a4e128187ca576b0d2784aaadcf86 100644 (file)
@@ -279,6 +279,35 @@ class Updater
         $this->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;
+    }
 }
 
 /**
index cfbde1a0695ee7a58eb86ee93e340e36baa6e9d4..2fed0df7700bc5211d9e515d9d0b60f9c2c19e3b 100644 (file)
@@ -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"
index c39649e81876ca51d518be966f4df639a79d26d0..634bd0eda01a76848ef80058aadcfc4db71e6d1c 100644 (file)
@@ -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 (file)
index 0000000..e44564b
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace Shaarli;
+
+/**
+ * Class ThemeUtilsTest
+ *
+ * @package Shaarli
+ */
+class ThemeUtilsTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Test getThemes() with existing theme directories.
+     */
+    public function testGetThemes()
+    {
+        $themes = ['theme1', 'default', 'Bl1p_- bL0p'];
+        foreach ($themes as $theme) {
+            mkdir('sandbox/tpl/'. $theme, 0755, true);
+        }
+
+        // include a file which should be ignored
+        touch('sandbox/tpl/supertheme');
+
+        $res = ThemeUtils::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/', 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'));
+    }
+}
index a1530996bf6ee473ff9bdeab0e2832e742308fd8..1d15cfaaace698d825a9b711258ee504f0927dc0 100644 (file)
@@ -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);
+    }
 }
index e71133b447f34d1b46901c58df44cf56cc8c1900..5820e6e4022d902f7284205405ab1e0495687b1a 100644 (file)
         <td>
           <select name="theme" id="theme">
             {loop="$theme_available"}
-              <option value="{$value}"
-                {if="$value===$theme"}
-                  selected="selected"
-                {/if}
-              >
+              <option value="{$value}" {if="$value===$theme"}selected{/if}>
                 {$value|ucfirst}
               </option>
             {/loop}