]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli\Plugin\DefaultColors; | |
4 | ||
5 | use Shaarli\Bookmark\LinkDB; | |
6 | use Shaarli\Config\ConfigManager; | |
7 | use Shaarli\Plugin\PluginManager; | |
8 | use Shaarli\TestCase; | |
9 | ||
10 | require_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 | */ | |
17 | class PluginDefaultColorsTest extends TestCase | |
18 | { | |
19 | /** | |
20 | * Reset plugin path | |
21 | */ | |
22 | protected function setUp(): void | |
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 | */ | |
35 | protected function tearDown(): void | |
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); | |
59 | ||
60 | $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css'); | |
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 | */ | |
76 | public function testGenerateCssFile() | |
77 | { | |
78 | $params = [ | |
79 | 'other1' => true, | |
80 | 'DEFAULT_COLORS_MAIN' => 'blue', | |
81 | 'DEFAULT_COLORS_BACKGROUND' => 'pink', | |
82 | 'other2' => ['yep'], | |
83 | 'DEFAULT_COLORS_DARK_MAIN' => 'green', | |
84 | ]; | |
85 | ||
86 | default_colors_generate_css_file($params); | |
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 | */ | |
102 | public function testGenerateCssFileSingle() | |
103 | { | |
104 | $params = [ | |
105 | 'other1' => true, | |
106 | 'DEFAULT_COLORS_BACKGROUND' => 'pink', | |
107 | 'other2' => ['yep'], | |
108 | 'DEFAULT_COLORS_DARK_MAIN' => '', | |
109 | ]; | |
110 | ||
111 | default_colors_generate_css_file($params); | |
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 | */ | |
125 | public function testGenerateCssFileNone() | |
126 | { | |
127 | default_colors_generate_css_file([]); | |
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 | } | |
196 | } |