Skip to content

Commit

Permalink
Plugins: do not save metadata along plugin parameters
Browse files Browse the repository at this point in the history
Also prevent the token to be saved.

Fixes shaarli#1550
  • Loading branch information
ArthurHoaro committed Sep 10, 2020
1 parent e2dff28 commit 011dc9f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions application/front/controller/admin/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function save(Request $request, Response $response): Response

if (isset($parameters['parameters_form'])) {
unset($parameters['parameters_form']);
unset($parameters['token']);
foreach ($parameters as $param => $value) {
$this->container->conf->set('plugins.'. $param, escape($value));
}
Expand Down
29 changes: 15 additions & 14 deletions application/plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,17 @@ public function load($authorizedPlugins)
*/
public function executeHooks($hook, &$data, $params = array())
{
if (!empty($params['target'])) {
$data['_PAGE_'] = $params['target'];
}

if (isset($params['loggedin'])) {
$data['_LOGGEDIN_'] = $params['loggedin'];
}

if (isset($params['basePath'])) {
$data['_BASE_PATH_'] = $params['basePath'];
}

if (isset($params['bookmarkService'])) {
$data['_BOOKMARK_SERVICE_'] = $params['bookmarkService'];
$metadataParameters = [
'target' => '_PAGE_',
'loggedin' => '_LOGGEDIN_',
'basePath' => '_BASE_PATH_',
'bookmarkService' => '_BOOKMARK_SERVICE_',
];

foreach ($metadataParameters as $parameter => $metaKey) {
if (!empty($params[$parameter])) {
$data[$metaKey] = $params[$parameter];
}
}

foreach ($this->loadedPlugins as $plugin) {
Expand All @@ -128,6 +125,10 @@ public function executeHooks($hook, &$data, $params = array())
}
}
}

foreach ($metadataParameters as $metaKey) {
unset($data[$metaKey]);
}
}

/**
Expand Down
36 changes: 21 additions & 15 deletions tests/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ public function testPlugin(): void

$this->assertTrue(function_exists('hook_test_random'));

$data = array(0 => 'woot');
$data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data);
$this->assertEquals('woot', $data[1]);

$data = array(0 => 'woot');
static::assertCount(2, $data);
static::assertSame('woot', $data[1]);

$data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
$this->assertEquals('page test', $data[1]);

$data = array(0 => 'woot');
static::assertCount(2, $data);
static::assertSame('page test', $data[1]);

$data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
$this->assertEquals('loggedin', $data[1]);

static::assertCount(2, $data);
static::assertEquals('loggedin', $data[1]);
}

/**
Expand All @@ -78,8 +84,8 @@ public function testPluginWithPhpError(): void
*/
public function testPluginNotFound(): void
{
$this->pluginManager->load(array());
$this->pluginManager->load(array('nope', 'renope'));
$this->pluginManager->load([]);
$this->pluginManager->load(['nope', 'renope']);
$this->addToAssertionCount(1);
}

Expand All @@ -89,18 +95,18 @@ public function testPluginNotFound(): void
public function testGetPluginsMeta(): void
{
PluginManager::$PLUGINS_PATH = self::$pluginPath;
$this->pluginManager->load(array(self::$pluginName));
$this->pluginManager->load([self::$pluginName]);

$expectedParameters = array(
'pop' => array(
$expectedParameters = [
'pop' => [
'value' => '',
'desc' => 'pop description',
),
'hip' => array(
],
'hip' => [
'value' => '',
'desc' => '',
),
);
],
];
$meta = $this->pluginManager->getPluginsMeta();
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
Expand Down
5 changes: 3 additions & 2 deletions tests/front/controller/admin/PluginsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
array_map(function (string $plugin) use ($path) { touch($path . '/' . $plugin); }, static::PLUGIN_NAMES);
}

public function tearDown()
public function tearDown(): void
{
$path = __DIR__ . '/folder';
array_map(function (string $plugin) use ($path) { unlink($path . '/' . $plugin); }, static::PLUGIN_NAMES);
Expand Down Expand Up @@ -125,6 +125,7 @@ public function testSavePluginParameters(): void
'parameters_form' => true,
'parameter1' => 'blip',
'parameter2' => 'blop',
'token' => 'this parameter should not be saved'
];

$request = $this->createMock(Request::class);
Expand All @@ -143,7 +144,7 @@ public function testSavePluginParameters(): void
->with('save_plugin_parameters', $parameters)
;
$this->container->conf
->expects(static::atLeastOnce())
->expects(static::exactly(2))
->method('set')
->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
;
Expand Down

0 comments on commit 011dc9f

Please sign in to comment.