3 namespace Shaarli\Plugin\DefaultColors
;
5 use Shaarli\Bookmark\LinkDB
;
6 use Shaarli\Config\ConfigManager
;
7 use Shaarli\Plugin\PluginManager
;
10 require_once 'plugins/default_colors/default_colors.php';
13 * Class PluginDefaultColorsTest
15 * Test the DefaultColors plugin (allowing to override default template colors).
17 class PluginDefaultColorsTest
extends TestCase
22 protected function setUp(): void
24 PluginManager
::$PLUGINS_PATH = 'sandbox';
25 mkdir(PluginManager
::$PLUGINS_PATH . '/default_colors/');
27 'plugins/default_colors/default_colors.css.template',
28 PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css.template'
33 * Remove sandbox files and folder
35 protected function tearDown(): void
37 if (file_exists('sandbox/default_colors/default_colors.css.template')) {
38 unlink('sandbox/default_colors/default_colors.css.template');
41 if (file_exists('sandbox/default_colors/default_colors.css')) {
42 unlink('sandbox/default_colors/default_colors.css');
45 if (is_dir('sandbox/default_colors')) {
46 rmdir('sandbox/default_colors');
51 * Test DefaultColors init without errors.
53 public function testDefaultColorsInitNoError()
55 $conf = new ConfigManager('');
56 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
57 $errors = default_colors_init($conf);
58 $this->assertEmpty($errors);
60 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
64 * Test DefaultColors init with errors.
66 public function testDefaultColorsInitError()
68 $conf = new ConfigManager('');
69 $errors = default_colors_init($conf);
70 $this->assertNotEmpty($errors);
74 * Test the save plugin parameters hook with all colors specified.
76 public function testGenerateCssFile()
80 'DEFAULT_COLORS_MAIN' => 'blue',
81 'DEFAULT_COLORS_BACKGROUND' => 'pink',
83 'DEFAULT_COLORS_DARK_MAIN' => 'green',
86 default_colors_generate_css_file($params);
87 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
88 $content = file_get_contents($file);
91 --background-color: pink;
92 --dark-main-color: green;
96 $this->assertEquals($expected, $content);
100 * Test the save plugin parameters hook with only one color specified.
102 public function testGenerateCssFileSingle()
106 'DEFAULT_COLORS_BACKGROUND' => 'pink',
108 'DEFAULT_COLORS_DARK_MAIN' => '',
111 default_colors_generate_css_file($params);
112 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
113 $content = file_get_contents($file);
115 --background-color: pink;
119 $this->assertEquals($expected, $content);
123 * Test the save plugin parameters hook with no color specified.
125 public function testGenerateCssFileNone()
127 default_colors_generate_css_file([]);
128 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
132 * Make sure that the CSS is properly included by the include hook.
134 public function testIncludeWithFile()
137 'css_files' => ['file1'],
138 'js_files' => ['file2'],
140 touch($file = 'sandbox/default_colors/default_colors.css');
141 $processedData = hook_default_colors_render_includes($data);
143 $this->assertCount(2, $processedData['css_files']);
144 $this->assertEquals($file, $processedData['css_files'][1]);
145 $this->assertCount(1, $processedData['js_files']);
149 * Make sure that the CSS is not included by the include hook if the CSS file does not exist.
151 public function testIncludeWithoutFile()
154 'css_files' => ['file1'],
155 'js_files' => ['file2'],
157 $processedData = hook_default_colors_render_includes($data);
159 $this->assertEquals($data, $processedData);
163 * Test helper function which generates CSS rules with valid input.
165 public function testFormatCssRuleValid()
169 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
172 $result = default_colors_format_css_rule($data, 'DEFAULT_COLORS_BLIP_BLOP');
173 $this->assertEquals(' --blip-blop-color: shinyColor', $result);
175 $data = ['unknown-parameter' => true];
176 $result = default_colors_format_css_rule($data, 'unknown-parameter');
177 $this->assertEquals(' --unknown-parameter-color: 1', $result);
181 * Test helper function which generates CSS rules with invalid input.
183 public function testFormatCssRuleInvalid()
185 $result = default_colors_format_css_rule([], 'DEFAULT_COLORS_BLIP_BLOP');
186 $this->assertEmpty($result);
190 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
193 $result = default_colors_format_css_rule($data, '');
194 $this->assertEmpty($result);