]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/plugins/PluginDefaultColorsTest.php
Fix default_colors plugin: update CSS file on color change
[github/shaarli/Shaarli.git] / tests / plugins / PluginDefaultColorsTest.php
CommitLineData
b5507350
A
1<?php
2
3namespace Shaarli\Plugin\DefaultColors;
4
b5507350
A
5use Shaarli\Bookmark\LinkDB;
6use Shaarli\Config\ConfigManager;
7use Shaarli\Plugin\PluginManager;
a5a9cf23 8use Shaarli\TestCase;
b5507350
A
9
10require_once 'plugins/default_colors/default_colors.php';
11
12/**
13 * Class PluginDefaultColorsTest
14 *
15 * Test the DefaultColors plugin (allowing to override default template colors).
16 */
17class PluginDefaultColorsTest extends TestCase
18{
19 /**
20 * Reset plugin path
21 */
8f60e120 22 protected function setUp(): void
b5507350
A
23 {
24 PluginManager::$PLUGINS_PATH = 'sandbox';
25 mkdir(PluginManager::$PLUGINS_PATH . '/default_colors/');
26 copy(
27 'plugins/default_colors/default_colors.css.template',
28 PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'
29 );
30 }
31
32 /**
33 * Remove sandbox files and folder
34 */
8f60e120 35 protected function tearDown(): void
b5507350
A
36 {
37 if (file_exists('sandbox/default_colors/default_colors.css.template')) {
38 unlink('sandbox/default_colors/default_colors.css.template');
39 }
40
41 if (file_exists('sandbox/default_colors/default_colors.css')) {
42 unlink('sandbox/default_colors/default_colors.css');
43 }
44
45 if (is_dir('sandbox/default_colors')) {
46 rmdir('sandbox/default_colors');
47 }
48 }
49
50 /**
51 * Test DefaultColors init without errors.
52 */
53 public function testDefaultColorsInitNoError()
54 {
55 $conf = new ConfigManager('');
56 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
57 $errors = default_colors_init($conf);
58 $this->assertEmpty($errors);
972daa45
A
59
60 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
b5507350
A
61 }
62
63 /**
64 * Test DefaultColors init with errors.
65 */
66 public function testDefaultColorsInitError()
67 {
68 $conf = new ConfigManager('');
69 $errors = default_colors_init($conf);
70 $this->assertNotEmpty($errors);
71 }
72
73 /**
74 * Test the save plugin parameters hook with all colors specified.
75 */
972daa45 76 public function testGenerateCssFile()
b5507350 77 {
972daa45 78 $params = [
b5507350
A
79 'other1' => true,
80 'DEFAULT_COLORS_MAIN' => 'blue',
81 'DEFAULT_COLORS_BACKGROUND' => 'pink',
82 'other2' => ['yep'],
83 'DEFAULT_COLORS_DARK_MAIN' => 'green',
84 ];
85
972daa45 86 default_colors_generate_css_file($params);
b5507350
A
87 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
88 $content = file_get_contents($file);
89 $expected = ':root {
90 --main-color: blue;
91 --background-color: pink;
92 --dark-main-color: green;
93
94}
95';
96 $this->assertEquals($expected, $content);
97 }
98
99 /**
100 * Test the save plugin parameters hook with only one color specified.
101 */
972daa45 102 public function testGenerateCssFileSingle()
b5507350 103 {
972daa45 104 $params = [
b5507350
A
105 'other1' => true,
106 'DEFAULT_COLORS_BACKGROUND' => 'pink',
107 'other2' => ['yep'],
108 'DEFAULT_COLORS_DARK_MAIN' => '',
109 ];
110
972daa45 111 default_colors_generate_css_file($params);
b5507350
A
112 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
113 $content = file_get_contents($file);
114 $expected = ':root {
115 --background-color: pink;
116
117}
118';
119 $this->assertEquals($expected, $content);
120 }
121
122 /**
123 * Test the save plugin parameters hook with no color specified.
124 */
972daa45 125 public function testGenerateCssFileNone()
b5507350 126 {
972daa45 127 default_colors_generate_css_file([]);
b5507350
A
128 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
129 }
130
131 /**
132 * Make sure that the CSS is properly included by the include hook.
133 */
134 public function testIncludeWithFile()
135 {
136 $data = [
137 'css_files' => ['file1'],
138 'js_files' => ['file2'],
139 ];
140 touch($file = 'sandbox/default_colors/default_colors.css');
141 $processedData = hook_default_colors_render_includes($data);
142
143 $this->assertCount(2, $processedData['css_files']);
144 $this->assertEquals($file, $processedData['css_files'][1]);
145 $this->assertCount(1, $processedData['js_files']);
146 }
147
148 /**
149 * Make sure that the CSS is not included by the include hook if the CSS file does not exist.
150 */
151 public function testIncludeWithoutFile()
152 {
153 $data = [
154 'css_files' => ['file1'],
155 'js_files' => ['file2'],
156 ];
157 $processedData = hook_default_colors_render_includes($data);
158
159 $this->assertEquals($data, $processedData);
160 }
161
162 /**
163 * Test helper function which generates CSS rules with valid input.
164 */
165 public function testFormatCssRuleValid()
166 {
167 $data = [
168 'other1' => true,
169 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
170 'other2' => ['yep'],
171 ];
172 $result = default_colors_format_css_rule($data, 'DEFAULT_COLORS_BLIP_BLOP');
173 $this->assertEquals(' --blip-blop-color: shinyColor', $result);
174
175 $data = ['unknown-parameter' => true];
176 $result = default_colors_format_css_rule($data, 'unknown-parameter');
177 $this->assertEquals(' --unknown-parameter-color: 1', $result);
178 }
179
180 /**
181 * Test helper function which generates CSS rules with invalid input.
182 */
183 public function testFormatCssRuleInvalid()
184 {
185 $result = default_colors_format_css_rule([], 'DEFAULT_COLORS_BLIP_BLOP');
186 $this->assertEmpty($result);
187
188 $data = [
189 'other1' => true,
190 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
191 'other2' => ['yep'],
192 ];
193 $result = default_colors_format_css_rule($data, '');
194 $this->assertEmpty($result);
195 }
035a002e
A
196
197 /**
198 * Make sure that a new CSS file is generated when save_plugin_parameters hook is triggered.
199 */
200 public function testHookSavePluginParameters(): void
201 {
202 $params = [
203 'other1' => true,
204 'DEFAULT_COLORS_BACKGROUND' => 'pink',
205 'other2' => ['yep'],
206 'DEFAULT_COLORS_DARK_MAIN' => '',
207 ];
208
209 hook_default_colors_save_plugin_parameters($params);
210 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
211 $content = file_get_contents($file);
212 $expected = ':root {
213 --background-color: pink;
214
215}
216';
217 $this->assertEquals($expected, $content);
218 }
b5507350 219}