3 namespace Shaarli\Plugin\DefaultColors
;
6 use PHPUnit\Framework\TestCase
;
7 use Shaarli\Bookmark\LinkDB
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Plugin\PluginManager
;
11 require_once 'plugins/default_colors/default_colors.php';
14 * Class PluginDefaultColorsTest
16 * Test the DefaultColors plugin (allowing to override default template colors).
18 class PluginDefaultColorsTest
extends TestCase
23 public function setUp()
25 PluginManager
::$PLUGINS_PATH = 'sandbox';
26 mkdir(PluginManager
::$PLUGINS_PATH . '/default_colors/');
28 'plugins/default_colors/default_colors.css.template',
29 PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css.template'
34 * Remove sandbox files and folder
36 public function tearDown()
38 if (file_exists('sandbox/default_colors/default_colors.css.template')) {
39 unlink('sandbox/default_colors/default_colors.css.template');
42 if (file_exists('sandbox/default_colors/default_colors.css')) {
43 unlink('sandbox/default_colors/default_colors.css');
46 if (is_dir('sandbox/default_colors')) {
47 rmdir('sandbox/default_colors');
52 * Test DefaultColors init without errors.
54 public function testDefaultColorsInitNoError()
56 $conf = new ConfigManager('');
57 $conf->set('plugins.DEFAULT_COLORS_BACKGROUND', 'value');
58 $errors = default_colors_init($conf);
59 $this->assertEmpty($errors);
63 * Test DefaultColors init with errors.
65 public function testDefaultColorsInitError()
67 $conf = new ConfigManager('');
68 $errors = default_colors_init($conf);
69 $this->assertNotEmpty($errors);
73 * Test the save plugin parameters hook with all colors specified.
75 public function testSavePluginParametersAll()
79 'DEFAULT_COLORS_MAIN' => 'blue',
80 'DEFAULT_COLORS_BACKGROUND' => 'pink',
82 'DEFAULT_COLORS_DARK_MAIN' => 'green',
85 hook_default_colors_save_plugin_parameters($post);
86 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
87 $content = file_get_contents($file);
90 --background-color: pink;
91 --dark-main-color: green;
95 $this->assertEquals($expected, $content);
99 * Test the save plugin parameters hook with only one color specified.
101 public function testSavePluginParametersSingle()
105 'DEFAULT_COLORS_BACKGROUND' => 'pink',
107 'DEFAULT_COLORS_DARK_MAIN' => '',
110 hook_default_colors_save_plugin_parameters($post);
111 $this->assertFileExists($file = 'sandbox/default_colors/default_colors.css');
112 $content = file_get_contents($file);
114 --background-color: pink;
118 $this->assertEquals($expected, $content);
122 * Test the save plugin parameters hook with no color specified.
124 public function testSavePluginParametersNone()
126 hook_default_colors_save_plugin_parameters([]);
127 $this->assertFileNotExists($file = 'sandbox/default_colors/default_colors.css');
131 * Make sure that the CSS is properly included by the include hook.
133 public function testIncludeWithFile()
136 'css_files' => ['file1'],
137 'js_files' => ['file2'],
139 touch($file = 'sandbox/default_colors/default_colors.css');
140 $processedData = hook_default_colors_render_includes($data);
142 $this->assertCount(2, $processedData['css_files']);
143 $this->assertEquals($file, $processedData['css_files'][1]);
144 $this->assertCount(1, $processedData['js_files']);
148 * Make sure that the CSS is not included by the include hook if the CSS file does not exist.
150 public function testIncludeWithoutFile()
153 'css_files' => ['file1'],
154 'js_files' => ['file2'],
156 $processedData = hook_default_colors_render_includes($data);
158 $this->assertEquals($data, $processedData);
162 * Test helper function which generates CSS rules with valid input.
164 public function testFormatCssRuleValid()
168 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
171 $result = default_colors_format_css_rule($data, 'DEFAULT_COLORS_BLIP_BLOP');
172 $this->assertEquals(' --blip-blop-color: shinyColor', $result);
174 $data = ['unknown-parameter' => true];
175 $result = default_colors_format_css_rule($data, 'unknown-parameter');
176 $this->assertEquals(' --unknown-parameter-color: 1', $result);
180 * Test helper function which generates CSS rules with invalid input.
182 public function testFormatCssRuleInvalid()
184 $result = default_colors_format_css_rule([], 'DEFAULT_COLORS_BLIP_BLOP');
185 $this->assertEmpty($result);
189 'DEFAULT_COLORS_BLIP_BLOP' => 'shinyColor',
192 $result = default_colors_format_css_rule($data, '');
193 $this->assertEmpty($result);