2016-05-18 21:43:59 +02:00
|
|
|
<?php
|
2021-04-05 09:39:34 +02:00
|
|
|
|
2017-03-03 23:06:12 +01:00
|
|
|
namespace Shaarli\Config;
|
|
|
|
|
2017-03-08 19:59:00 +01:00
|
|
|
use Shaarli\Config\Exception\PluginConfigOrderException;
|
2020-07-26 14:43:10 +02:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2021-04-05 11:00:28 +02:00
|
|
|
use Shaarli\TestCase;
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for Shaarli config related functions
|
|
|
|
*/
|
2021-04-05 11:00:28 +02:00
|
|
|
class ConfigPluginTest extends TestCase
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test save_plugin_config with valid data.
|
|
|
|
*
|
|
|
|
* @throws PluginConfigOrderException
|
|
|
|
*/
|
|
|
|
public function testSavePluginConfigValid()
|
|
|
|
{
|
2020-07-26 14:43:10 +02:00
|
|
|
$data = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'order_plugin1' => 2, // no plugin related
|
|
|
|
'plugin2' => 0, // new - at the end
|
|
|
|
'plugin3' => 0, // 2nd
|
|
|
|
'order_plugin3' => 8,
|
|
|
|
'plugin4' => 0, // 1st
|
|
|
|
'order_plugin4' => 5,
|
2020-07-26 14:43:10 +02:00
|
|
|
];
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2020-07-26 14:43:10 +02:00
|
|
|
$expected = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'plugin3',
|
|
|
|
'plugin4',
|
|
|
|
'plugin2',
|
2020-07-26 14:43:10 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
mkdir($path = __DIR__ . '/folder');
|
|
|
|
PluginManager::$PLUGINS_PATH = $path;
|
2021-04-05 09:39:34 +02:00
|
|
|
array_map(function (string $plugin) use ($path) {
|
|
|
|
touch($path . '/' . $plugin);
|
|
|
|
}, $expected);
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
$out = save_plugin_config($data);
|
|
|
|
$this->assertEquals($expected, $out);
|
2020-07-26 14:43:10 +02:00
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
array_map(function (string $plugin) use ($path) {
|
|
|
|
unlink($path . '/' . $plugin);
|
|
|
|
}, $expected);
|
2020-07-26 14:43:10 +02:00
|
|
|
rmdir($path);
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test save_plugin_config with invalid data.
|
|
|
|
*/
|
|
|
|
public function testSavePluginConfigInvalid()
|
|
|
|
{
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->expectException(PluginConfigOrderException::class);
|
2020-09-27 14:07:08 +02:00
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$data = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'plugin2' => 0,
|
|
|
|
'plugin3' => 0,
|
|
|
|
'order_plugin3' => 0,
|
|
|
|
'plugin4' => 0,
|
|
|
|
'order_plugin4' => 0,
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
save_plugin_config($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test save_plugin_config without data.
|
|
|
|
*/
|
|
|
|
public function testSavePluginConfigEmpty()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->assertEquals([], save_plugin_config([]));
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test validate_plugin_order with valid data.
|
|
|
|
*/
|
|
|
|
public function testValidatePluginOrderValid()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$data = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'order_plugin1' => 2,
|
|
|
|
'plugin2' => 0,
|
|
|
|
'plugin3' => 0,
|
|
|
|
'order_plugin3' => 1,
|
|
|
|
'plugin4' => 0,
|
|
|
|
'order_plugin4' => 5,
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
$this->assertTrue(validate_plugin_order($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test validate_plugin_order with invalid data.
|
|
|
|
*/
|
|
|
|
public function testValidatePluginOrderInvalid()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$data = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'order_plugin1' => 2,
|
|
|
|
'order_plugin3' => 1,
|
|
|
|
'order_plugin4' => 1,
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
$this->assertFalse(validate_plugin_order($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test load_plugin_parameter_values.
|
|
|
|
*/
|
|
|
|
public function testLoadPluginParameterValues()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$plugins = [
|
|
|
|
'plugin_name' => [
|
|
|
|
'parameters' => [
|
|
|
|
'param1' => ['value' => true],
|
|
|
|
'param2' => ['value' => false],
|
|
|
|
'param3' => ['value' => ''],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$parameters = [
|
2016-05-18 21:43:59 +02:00
|
|
|
'param1' => 'value1',
|
|
|
|
'param2' => 'value2',
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
$result = load_plugin_parameter_values($plugins, $parameters);
|
2016-08-02 11:02:20 +02:00
|
|
|
$this->assertEquals('value1', $result['plugin_name']['parameters']['param1']['value']);
|
|
|
|
$this->assertEquals('value2', $result['plugin_name']['parameters']['param2']['value']);
|
|
|
|
$this->assertEquals('', $result['plugin_name']['parameters']['param3']['value']);
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
}
|