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