aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/plugins/PluginDefaultColorsTest.php195
1 files changed, 195 insertions, 0 deletions
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}