Default colors plugin - Add unit tests

This commit is contained in:
ArthurHoaro 2019-07-13 10:38:19 +02:00
parent a5a0c0399b
commit b550735054
3 changed files with 250 additions and 12 deletions

View File

@ -1,3 +1,3 @@
:root { :root {
%s %s
} }

View File

@ -6,6 +6,7 @@
* Allow users to easily overrides colors of the default theme. * Allow users to easily overrides colors of the default theme.
*/ */
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager; use Shaarli\Plugin\PluginManager;
const DEFAULT_COLORS_PLACEHOLDERS = [ const DEFAULT_COLORS_PLACEHOLDERS = [
@ -15,7 +16,32 @@ const DEFAULT_COLORS_PLACEHOLDERS = [
]; ];
/** /**
* When plugin parameters are saved * Display an error if the plugin is active a no color is configured.
*
* @param $conf ConfigManager instance
*
* @return array|null The errors array or null of there is none.
*/
function default_colors_init($conf)
{
$params = '';
foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
$params .= trim($conf->get('plugins.'. $placeholder, ''));
}
if (empty($params)) {
$error = t('Default colors plugin error: '.
'This plugin is active and no custom color is configured.');
return array($error);
}
}
/**
* When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
*
* @param array $data $_POST array
*
* @return array Updated $_POST array
*/ */
function hook_default_colors_save_plugin_parameters($data) function hook_default_colors_save_plugin_parameters($data)
{ {
@ -27,16 +53,20 @@ function hook_default_colors_save_plugin_parameters($data)
? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
: ''; : '';
} }
file_put_contents($file, sprintf($template, $content));
if (! empty($content)) {
file_put_contents($file, sprintf($template, $content));
}
return $data; return $data;
} }
/** /**
* When linklist is displayed, include isso CSS file. * When linklist is displayed, include default_colors CSS file.
* *
* @param array $data - header data. * @param array $data - header data.
* *
* @return mixed - header data with isso CSS file added. * @return mixed - header data with default_colors CSS file added.
*/ */
function hook_default_colors_render_includes($data) function hook_default_colors_render_includes($data)
{ {
@ -48,6 +78,26 @@ function hook_default_colors_render_includes($data)
return $data; return $data;
} }
/**
* Create a valid CSS rule from parameters settings and plugin parameter.
*
* @param array $data $_POST array
* @param string $parameter Plugin parameter name
*
* @return string CSS rules for the provided parameter and its matching value.
*/
function default_colors_format_css_rule($data, $parameter)
{
if (empty($data[$parameter])) {
return '';
}
$key = str_replace('DEFAULT_COLORS_', '', $parameter);
$key = str_replace('_', '-', strtolower($key)) .'-color';
return ' --'. $key .': '. $data[$parameter];
}
/** /**
* This function is never called, but contains translation calls for GNU gettext extraction. * This function is never called, but contains translation calls for GNU gettext extraction.
*/ */
@ -59,10 +109,3 @@ function default_colors_translation()
t('Background color (light grey)'); t('Background color (light grey)');
t('Dark main color (e.g. visited links)'); t('Dark main color (e.g. visited links)');
} }
function default_colors_format_css_rule($data, $parameter)
{
$key = str_replace('DEFAULT_COLORS_', '', $parameter);
$key = str_replace('_', '-', strtolower($key)) .'-color';
return ' --'. $key .': '. $data[$parameter];
}

View File

@ -0,0 +1,195 @@
<?php
namespace Shaarli\Plugin\DefaultColors;
use DateTime;
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\LinkDB;
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
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
*/
public function setUp()
{
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
*/
public function tearDown()
{
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);
}
/**
* 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 testSavePluginParametersAll()
{
$post = [
'other1' => true,
'DEFAULT_COLORS_MAIN' => 'blue',
'DEFAULT_COLORS_BACKGROUND' => 'pink',
'other2' => ['yep'],
'DEFAULT_COLORS_DARK_MAIN' => 'green',
];
hook_default_colors_save_plugin_parameters($post);
$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 testSavePluginParametersSingle()
{
$post = [
'other1' => true,
'DEFAULT_COLORS_BACKGROUND' => 'pink',
'other2' => ['yep'],
'DEFAULT_COLORS_DARK_MAIN' => '',
];
hook_default_colors_save_plugin_parameters($post);
$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 testSavePluginParametersNone()
{
hook_default_colors_save_plugin_parameters([]);
$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);
}
}