Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

fix(widget): fix widget upgrade if directory has changed #6975

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/CentreonLegacy/Core/Widget/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,37 @@ public function newInformation()

/**
*
* @param string $widgetName
* @param string $widgetDirectory
* @return \CentreonLegacy\Core\Widget\Installer
*/
public function newInstaller($widgetName)
public function newInstaller($widgetDirectory)
{
$informationObj = $this->newInformation();

return new Installer($this->dependencyInjector, $informationObj, $widgetName, $this->utils);
return new Installer($this->dependencyInjector, $informationObj, $widgetDirectory, $this->utils);
}

/**
*
* @param string $widgetName
* @param string $widgetDirectory
* @return \CentreonLegacy\Core\Widget\Upgrader
*/
public function newUpgrader($widgetName)
public function newUpgrader($widgetDirectory)
{
$informationObj = $this->newInformation();

return new Upgrader($this->dependencyInjector, $informationObj, $widgetName, $this->utils);
return new Upgrader($this->dependencyInjector, $informationObj, $widgetDirectory, $this->utils);
}

/**
*
* @param string $widgetName
* @param string $widgetDirectory
* @return \CentreonLegacy\Core\Widget\Remover
*/
public function newRemover($widgetName)
public function newRemover($widgetDirectory)
{
$informationObj = $this->newInformation();

return new Remover($this->dependencyInjector, $informationObj, $widgetName, $this->utils);
return new Remover($this->dependencyInjector, $informationObj, $widgetDirectory, $this->utils);
}
}
21 changes: 12 additions & 9 deletions src/CentreonLegacy/Core/Widget/Information.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,20 @@ public function __construct(\Pimple\Container $dependencyInjector, \CentreonLega

/**
* Get module configuration from file
* @param string $widgetName
* @param string $widgetDirectory the widget directory (usually the widget name)
* @return array
* @throws \Exception
*/
public function getConfiguration($widgetName)
public function getConfiguration($widgetDirectory)
{
$widgetPath = $this->utils->buildPath('/widgets/' . $widgetName);
$widgetPath = $this->utils->buildPath('/widgets/' . $widgetDirectory);
if (!$this->dependencyInjector['filesystem']->exists($widgetPath . '/configs.xml')) {
throw new \Exception('Cannot get configuration file of widget "' . $widgetName . '"');
throw new \Exception('Cannot get configuration file of widget "' . $widgetDirectory . '"');
}

$conf = $this->utils->xmlIntoArray($widgetPath . '/configs.xml');

$conf['directory'] = $widgetDirectory;
$conf['autoRefresh'] = isset($conf['autoRefresh']) ? $conf['autoRefresh'] : 0;

return $conf;
Expand Down Expand Up @@ -211,7 +212,8 @@ private function getInstalledList()

$installedWidgets = array();
foreach ($widgets as $widget) {
$installedWidgets[$widget['directory']] = $widget;
// we use lowercase to avoid problems if directory name have some letters in uppercase
$installedWidgets[strtolower($widget['directory'])] = $widget;
}

return $installedWidgets;
Expand All @@ -230,17 +232,18 @@ public function getAvailableList($search = '')
$widgets = $this->dependencyInjector['finder']->directories()->depth('== 0')->in($widgetsPath);

foreach ($widgets as $widget) {
$widgetName = $widget->getBasename();
if (!empty($search) && !stristr($widgetName, $search)) {
$widgetDirectory = $widget->getBasename();
if (!empty($search) && !stristr($widgetDirectory, $search)) {
continue;
}

$widgetPath = $widgetsPath . $widgetName;
$widgetPath = $widgetsPath . $widgetDirectory;
if (!$this->dependencyInjector['filesystem']->exists($widgetPath . '/configs.xml')) {
continue;
}

$widgetsConf[$widgetName] = $this->getConfiguration($widgetName);
// we use lowercase to avoid problems if directory name have some letters in uppercase
$widgetsConf[strtolower($widgetDirectory)] = $this->getConfiguration($widgetDirectory);
}

return $widgetsConf;
Expand Down
2 changes: 1 addition & 1 deletion src/CentreonLegacy/Core/Widget/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function installConfiguration()
$sth->bindParam(':description', $this->widgetConfiguration['description'], \PDO::PARAM_STR);
$sth->bindParam(':url', $this->widgetConfiguration['url'], \PDO::PARAM_STR);
$sth->bindParam(':version', $this->widgetConfiguration['version'], \PDO::PARAM_STR);
$sth->bindParam(':directory', $this->widgetName, \PDO::PARAM_STR);
$sth->bindParam(':directory', $this->widgetConfiguration['directory'], \PDO::PARAM_STR);
$sth->bindParam(':author', $this->widgetConfiguration['author'], \PDO::PARAM_STR);
$sth->bindParam(':email', $this->widgetConfiguration['email'], \PDO::PARAM_STR);
$sth->bindParam(':website', $this->widgetConfiguration['website'], \PDO::PARAM_STR);
Expand Down
2 changes: 1 addition & 1 deletion src/CentreonLegacy/Core/Widget/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function upgradeConfiguration()
$sth->bindParam(':keywords', $this->widgetConfiguration['keywords'], \PDO::PARAM_STR);
$sth->bindParam(':thumbnail', $this->widgetConfiguration['thumbnail'], \PDO::PARAM_STR);
$sth->bindParam(':autoRefresh', $this->widgetConfiguration['autoRefresh'], \PDO::PARAM_INT);
$sth->bindParam(':directory', $this->widgetName, \PDO::PARAM_STR);
$sth->bindParam(':directory', $this->widgetConfiguration['directory'], \PDO::PARAM_STR);

if (!$sth->execute()) {
throw new \Exception('Cannot upgrade widget "' . $this->widgetName . '".');
Expand Down
3 changes: 2 additions & 1 deletion tests/php/CentreonLegacy/Core/Widget/InformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function setUp()
'screenshot' => '',
'thumbnail' => './widgets/host-monitoring/resources/centreon-logo.png',
'url' => './widgets/host-monitoring/index.php',
'directory' => 'my-widget',
'preferences' => array(
'preference' => array(
array(
Expand Down Expand Up @@ -96,7 +97,7 @@ public function testGetConfiguration()
->willReturn($this->configuration);

$information = new Information($this->container, $this->utils);
$configuration = $information->getConfiguration('MyWidget');
$configuration = $information->getConfiguration('my-widget');

$this->assertEquals($configuration, $expectedResult);
}
Expand Down
1 change: 1 addition & 0 deletions tests/php/CentreonLegacy/Core/Widget/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setUp()
'screenshot' => '',
'thumbnail' => './widgets/host-monitoring/resources/centreon-logo.png',
'url' => './widgets/host-monitoring/index.php',
'directory' => 'my-widget',
'preferences' => array(
'preference' => array(
array(
Expand Down
3 changes: 2 additions & 1 deletion tests/php/CentreonLegacy/Core/Widget/UpgraderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setUp()
'screenshot' => '',
'thumbnail' => './widgets/host-monitoring/resources/centreon-logo.png',
'url' => './widgets/host-monitoring/index.php',
'directory' => 'my-widget',
'preferences' => array(
'preference' => array(
array(
Expand Down Expand Up @@ -185,7 +186,7 @@ public function tearDown()
$this->container = null;
}

public function testInstall()
public function testUpgrade()
{
$query = 'UPDATE widget_models SET ' .
'title = :title, ' .
Expand Down
6 changes: 3 additions & 3 deletions www/include/options/oreon/widgets/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@

// install all available widgets
foreach ($widgetInfoObj->getInstallableList() as $widgetName => $widgetData) {
$widgetInstaller = $factory->newInstaller($widgetName);
$widgetInstaller = $factory->newInstaller($widgetData['directory']);
$widgetInstaller->install();
}

// upgrade all available widgets
foreach ($widgetInfoObj->getUpgradeableList() as $widgetName => $widgetData) {
$widgetUpgrader = $factory->newUpgrader($widgetName);
$widgetUpgrader = $factory->newUpgrader($widgetData['directory']);
$widgetUpgrader->upgrade();
}

Expand All @@ -96,7 +96,7 @@
$widgetsToUpgrade = $widgetInfoObj->getUpgradeableList();

foreach ($widgetsToUpgrade as $widgetName => $widgetData) {
$widgetUpgrader = $factory->newUpgrader($widgetName);
$widgetUpgrader = $factory->newUpgrader($widgetData['directory']);
$widgetUpgrader->upgrade();
}
break;
Expand Down