aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2019-07-13 10:38:19 +0200
committerArthurHoaro <arthur@hoa.ro>2019-07-20 09:32:52 +0200
commitb5507350543f9259196996a24bbae2717b1b20be (patch)
tree93b5975c8c26561b555d059eef4400df54f49d9f
parenta5a0c0399bcfea518330c4bad186da77f89ace6e (diff)
downloadShaarli-b5507350543f9259196996a24bbae2717b1b20be.tar.gz
Shaarli-b5507350543f9259196996a24bbae2717b1b20be.tar.zst
Shaarli-b5507350543f9259196996a24bbae2717b1b20be.zip
Default colors plugin - Add unit tests
-rw-r--r--plugins/default_colors/default_colors.css.template2
-rw-r--r--plugins/default_colors/default_colors.php65
-rw-r--r--tests/plugins/PluginDefaultColorsTest.php195
3 files changed, 250 insertions, 12 deletions
diff --git a/plugins/default_colors/default_colors.css.template b/plugins/default_colors/default_colors.css.template
index 4b269baf..87e22a08 100644
--- a/plugins/default_colors/default_colors.css.template
+++ b/plugins/default_colors/default_colors.css.template
@@ -1,3 +1,3 @@
1:root { 1:root {
2 %s 2%s
3} 3}
diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php
index b898814b..1928cc9f 100644
--- a/plugins/default_colors/default_colors.php
+++ b/plugins/default_colors/default_colors.php
@@ -6,6 +6,7 @@
6 * Allow users to easily overrides colors of the default theme. 6 * Allow users to easily overrides colors of the default theme.
7 */ 7 */
8 8
9use Shaarli\Config\ConfigManager;
9use Shaarli\Plugin\PluginManager; 10use Shaarli\Plugin\PluginManager;
10 11
11const DEFAULT_COLORS_PLACEHOLDERS = [ 12const DEFAULT_COLORS_PLACEHOLDERS = [
@@ -15,7 +16,32 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
15]; 16];
16 17
17/** 18/**
18 * When plugin parameters are saved 19 * Display an error if the plugin is active a no color is configured.
20 *
21 * @param $conf ConfigManager instance
22 *
23 * @return array|null The errors array or null of there is none.
24 */
25function default_colors_init($conf)
26{
27 $params = '';
28 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
29 $params .= trim($conf->get('plugins.'. $placeholder, ''));
30 }
31
32 if (empty($params)) {
33 $error = t('Default colors plugin error: '.
34 'This plugin is active and no custom color is configured.');
35 return array($error);
36 }
37}
38
39/**
40 * When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
41 *
42 * @param array $data $_POST array
43 *
44 * @return array Updated $_POST array
19 */ 45 */
20function hook_default_colors_save_plugin_parameters($data) 46function hook_default_colors_save_plugin_parameters($data)
21{ 47{
@@ -27,16 +53,20 @@ function hook_default_colors_save_plugin_parameters($data)
27 ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL 53 ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
28 : ''; 54 : '';
29 } 55 }
30 file_put_contents($file, sprintf($template, $content)); 56
57 if (! empty($content)) {
58 file_put_contents($file, sprintf($template, $content));
59 }
60
31 return $data; 61 return $data;
32} 62}
33 63
34/** 64/**
35 * When linklist is displayed, include isso CSS file. 65 * When linklist is displayed, include default_colors CSS file.
36 * 66 *
37 * @param array $data - header data. 67 * @param array $data - header data.
38 * 68 *
39 * @return mixed - header data with isso CSS file added. 69 * @return mixed - header data with default_colors CSS file added.
40 */ 70 */
41function hook_default_colors_render_includes($data) 71function hook_default_colors_render_includes($data)
42{ 72{
@@ -49,6 +79,26 @@ function hook_default_colors_render_includes($data)
49} 79}
50 80
51/** 81/**
82 * Create a valid CSS rule from parameters settings and plugin parameter.
83 *
84 * @param array $data $_POST array
85 * @param string $parameter Plugin parameter name
86 *
87 * @return string CSS rules for the provided parameter and its matching value.
88 */
89function default_colors_format_css_rule($data, $parameter)
90{
91 if (empty($data[$parameter])) {
92 return '';
93 }
94
95 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
96 $key = str_replace('_', '-', strtolower($key)) .'-color';
97 return ' --'. $key .': '. $data[$parameter];
98}
99
100
101/**
52 * This function is never called, but contains translation calls for GNU gettext extraction. 102 * This function is never called, but contains translation calls for GNU gettext extraction.
53 */ 103 */
54function default_colors_translation() 104function default_colors_translation()
@@ -59,10 +109,3 @@ function default_colors_translation()
59 t('Background color (light grey)'); 109 t('Background color (light grey)');
60 t('Dark main color (e.g. visited links)'); 110 t('Dark main color (e.g. visited links)');
61} 111}
62
63function default_colors_format_css_rule($data, $parameter)
64{
65 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
66 $key = str_replace('_', '-', strtolower($key)) .'-color';
67 return ' --'. $key .': '. $data[$parameter];
68}
diff --git a/tests/plugins/PluginDefaultColorsTest.php b/tests/plugins/PluginDefaultColorsTest.php
new file mode 100644
index 00000000..b9951cca
--- /dev/null
+++ b/tests/plugins/PluginDefaultColorsTest.php
@@ -0,0 +1,195 @@
1<?php
2
3namespace Shaarli\Plugin\DefaultColors;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\LinkDB;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Plugin\PluginManager;
10
11require_once 'plugins/default_colors/default_colors.php';
12
13/**
14 * Class PluginDefaultColorsTest
15 *
16 * Test the DefaultColors plugin (allowing to override default template colors).
17 */
18class PluginDefaultColorsTest extends TestCase
19{
20 /**
21 * Reset plugin path
22 */
23 public function setUp()
24 {
25 PluginManager::$PLUGINS_PATH = 'sandbox';
26 mkdir(PluginManager::$PLUGINS_PATH . '/default_colors/');
27 copy(
28 'plugins/default_colors/default_colors.css.template',
29 PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'
30 );
31 }
32
33 /**
34 * Remove sandbox files and folder
35 */
36 public function tearDown()
37 {
38 if (file_exists('sandbox/default_colors/default_colors.css.template')) {
39 unlink('sandbox/default_colors/default_colors.css.template');
40 }
41
42 if (file_exists('sandbox/default_colors/default_colors.css')) {
43 unlink('sandbox/default_colors/default_colors.css');
44 }
45
46 if (is_dir('sandbox/default_colors')) {
47 rmdir('sandbox/default_colors');
48 }
49 }
50
51 /**
52 * Test DefaultColors init without errors.
53 */
54 public function testDefaultColorsInitNoError()
55 {
56 $conf = new ConfigManager('');
57 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
58 $errors = default_colors_init($conf);
59 $this->assertEmpty($errors);
60 }
61
62 /**
63 * Test DefaultColors init with errors.
64 */
65 public function testDefaultColorsInitError()
66 {
67 $conf = new ConfigManager('');
68 $errors = default_colors_init($conf);
69 $this->assertNotEmpty($errors);
70 }
71
72 /**
73 * Test the save plugin parameters hook with all colors specified.
74 */
75 public function testSavePluginParametersAll()
76 {
77 $post = [
78 'other1' => true,
79 'DEFAULT_COLORS_MAIN' => 'blue',
80 'DEFAULT_COLORS_BACKGROUND' => 'pink',
81 'other2' => ['yep'],
82 'DEFAULT_COLORS_DARK_MAIN' => 'green',
83 ];
84
85 hook_default_colors_save_plugin_parameters($post);
86 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
87 $content = file_get_contents($file);
88 $expected = ':root {
89 --main-color: blue;
90 --background-color: pink;
91 --dark-main-color: green;
92
93}
94';
95 $this->assertEquals($expected, $content);
96 }
97
98 /**
99 * Test the save plugin parameters hook with only one color specified.
100 */
101 public function testSavePluginParametersSingle()
102 {
103 $post = [
104 'other1' => true,
105 'DEFAULT_COLORS_BACKGROUND' => 'pink',
106 'other2' => ['yep'],
107 'DEFAULT_COLORS_DARK_MAIN' => '',
108 ];
109
110 hook_default_colors_save_plugin_parameters($post);
111 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
112 $content = file_get_contents($file);
113 $expected = ':root {
114 --background-color: pink;
115
116}
117';
118 $this->assertEquals($expected, $content);
119 }
120
121 /**
122 * Test the save plugin parameters hook with no color specified.
123 */
124 public function testSavePluginParametersNone()
125 {
126 hook_default_colors_save_plugin_parameters([]);
127 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
128 }
129
130 /**
131 * Make sure that the CSS is properly included by the include hook.
132 */
133 public function testIncludeWithFile()
134 {
135 $data = [
136 'css_files' => ['file1'],
137 'js_files' => ['file2'],
138 ];
139 touch($file = 'sandbox/default_colors/default_colors.css');
140 $processedData = hook_default_colors_render_includes($data);
141
142 $this->assertCount(2, $processedData['css_files']);
143 $this->assertEquals($file, $processedData['css_files'][1]);
144 $this->assertCount(1, $processedData['js_files']);
145 }
146
147 /**
148 * Make sure that the CSS is not included by the include hook if the CSS file does not exist.
149 */
150 public function testIncludeWithoutFile()
151 {
152 $data = [
153 'css_files' => ['file1'],
154 'js_files' => ['file2'],
155 ];
156 $processedData = hook_default_colors_render_includes($data);
157
158 $this->assertEquals($data, $processedData);
159 }
160
161 /**
162 * Test helper function which generates CSS rules with valid input.
163 */
164 public function testFormatCssRuleValid()
165 {
166 $data = [
167 'other1' => true,
168 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
169 'other2' => ['yep'],
170 ];
171 $result = default_colors_format_css_rule($data, 'DEFAULT_COLORS_BLIP_BLOP');
172 $this->assertEquals(' --blip-blop-color: shinyColor', $result);
173
174 $data = ['unknown-parameter' => true];
175 $result = default_colors_format_css_rule($data, 'unknown-parameter');
176 $this->assertEquals(' --unknown-parameter-color: 1', $result);
177 }
178
179 /**
180 * Test helper function which generates CSS rules with invalid input.
181 */
182 public function testFormatCssRuleInvalid()
183 {
184 $result = default_colors_format_css_rule([], 'DEFAULT_COLORS_BLIP_BLOP');
185 $this->assertEmpty($result);
186
187 $data = [
188 'other1' => true,
189 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
190 'other2' => ['yep'],
191 ];
192 $result = default_colors_format_css_rule($data, '');
193 $this->assertEmpty($result);
194 }
195}