Skip to content

Commit

Permalink
Fix EZP-20004: Settings incorrectly dumped by ezpublish:configure
Browse files Browse the repository at this point in the history
Make sure the alias list and alias definition are correctly retrieved
  • Loading branch information
dpobel committed Nov 16, 2012
1 parent 9ae0fd5 commit 55382c0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 65 deletions.
101 changes: 56 additions & 45 deletions eZ/Bundle/EzPublishLegacyBundle/SetupWizard/ConfigurationConverter.php
Expand Up @@ -10,7 +10,9 @@

use eZ\Bundle\EzPublishLegacyBundle\DependencyInjection\Configuration\LegacyConfigResolver;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException,
eZ\Publish\Core\MVC\Legacy\Kernel as LegacyKernel;
eZ\Publish\Core\MVC\Legacy\Kernel as LegacyKernel,
eZINI,
eZSiteAccess;

/**
* Handles conversionlegacy eZ Publish 4 parameters from a set of settings to a configuration array
Expand Down Expand Up @@ -52,9 +54,9 @@ public function fromLegacy( $sitePackage, $adminSiteaccess )
$settings = array();
$settings['ezpublish'] = array();
$settings['ezpublish']['siteaccess'] = array();
$defaultSiteaccess = $this->legacyResolver->getParameter( 'SiteSettings.DefaultAccess' );
$defaultSiteaccess = $this->getParameter( 'SiteSettings', 'DefaultAccess' );
$settings['ezpublish']['siteaccess']['default_siteaccess'] = $defaultSiteaccess;
$siteList = $this->legacyResolver->getParameter( 'SiteAccessSettings.AvailableSiteAccessList' );
$siteList = $this->getParameter( 'SiteAccessSettings', 'AvailableSiteAccessList' );

if ( !is_array( $siteList ) || empty( $siteList ) )
throw new InvalidArgumentException( 'siteList', 'can not be empty' );
Expand All @@ -75,7 +77,7 @@ public function fromLegacy( $sitePackage, $adminSiteaccess )
'eZMySQLDB' => 'mysql',
);

$databaseSettings = $this->getGroupWithFallback( 'DatabaseSettings', 'site', $defaultSiteaccess );
$databaseSettings = $this->getGroup( 'DatabaseSettings', 'site.ini', $defaultSiteaccess );

$databaseType = $databaseSettings['DatabaseImplementation'];
if ( isset( $databaseMapping[$databaseType] ) )
Expand Down Expand Up @@ -109,21 +111,21 @@ public function fromLegacy( $sitePackage, $adminSiteaccess )

// FileSettings
$settings['ezpublish']['system'][$groupName]['var_dir'] =
$this->getParameterWithFallback( 'FileSettings.VarDir', 'site', $defaultSiteaccess );
$this->getParameter( 'FileSettings', 'VarDir', 'site.ini', $defaultSiteaccess );

// we don't map the default FileSettings.StorageDir value
$storageDir = $this->getParameterWithFallback( 'FileSettings.StorageDir', 'site', $defaultSiteaccess );
$storageDir = $this->getParameter( 'FileSettings', 'StorageDir', 'site.ini', $defaultSiteaccess );
if ( $storageDir !== 'storage' )
$settings['ezpublish']['system'][$groupName]['storage_dir'] = $storageDir;


// ImageMagick settings
$imageMagickEnabled = $this->getParameterWithFallback( 'ImageMagick.IsEnabled', 'image', $defaultSiteaccess );
$imageMagickEnabled = $this->getParameter( 'ImageMagick', 'IsEnabled', 'image.ini', $defaultSiteaccess );
if ( $imageMagickEnabled == 'true' )
{
$settings['ezpublish']['imagemagick']['enabled'] = true;
$imageMagickExecutablePath = $this->getParameterWithFallback( 'ImageMagick.ExecutablePath', 'image', $defaultSiteaccess );
$imageMagickExecutable = $this->getParameterWithFallback( 'ImageMagick.Executable', 'image', $defaultSiteaccess );
$imageMagickExecutablePath = $this->getParameter( 'ImageMagick', 'ExecutablePath', 'image.ini', $defaultSiteaccess );
$imageMagickExecutable = $this->getParameter( 'ImageMagick', 'Executable', 'image.ini', $defaultSiteaccess );
$settings['ezpublish']['imagemagick']['path'] = rtrim( $imageMagickExecutablePath, '/\\' ) . '/' . $imageMagickExecutable;
}
else
Expand All @@ -133,11 +135,11 @@ public function fromLegacy( $sitePackage, $adminSiteaccess )

// image variations settings
$settings['ezpublish']['system'][$defaultSiteaccess]['image_variations'] = array();
$imageAliasesList = $this->getGroupWithFallback( 'AliasSettings', 'image', $defaultSiteaccess );
$imageAliasesList = $this->getGroup( 'AliasSettings', 'image.ini', $defaultSiteaccess );
foreach( $imageAliasesList['AliasList'] as $imageAliasIdentifier )
{
$variationSettings = array( 'reference' => null, 'filters' => array() );
$aliasSettings = $this->getGroupWithFallback( $imageAliasIdentifier, 'image', $defaultSiteaccess );
$aliasSettings = $this->getGroup( $imageAliasIdentifier, 'image.ini', $defaultSiteaccess );
if ( isset( $aliasSettings['Reference'] ) && $aliasSettings['Reference'] != '' )
{
$variationSettings['reference'] = $aliasSettings['Reference'];
Expand Down Expand Up @@ -191,60 +193,69 @@ protected function mapDatabaseType( $databaseType )
}

/**
* Returns the contents of the legacy group $groupName, either in $defaultSiteaccess or,
* if not found, in the global settings
* Returns the contents of the legacy group $groupName. If $file and
* $siteaccess are null, the global value is fetched with the legacy resolver.
*
* @param $groupName
* @param $namespace
* @param $siteaccess
* @param string $groupName
* @param string|null $namespace
* @param string|null $siteaccess
*
* @internal param $defaultSiteaccess
* @return array
*
* @throws \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException
*/
public function getGroupWithFallback( $groupName, $namespace, $siteaccess )
public function getGroup( $groupName, $file = null, $siteaccess = null )
{
try
{
return $this->legacyResolver->getGroup( $groupName, $namespace, $siteaccess );
}
// fallback to global override
catch ( \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException $e )
if ( $file === null && $siteaccess === null )
{
return $this->legacyResolver->getGroup( $groupName, $namespace );
// in this case we want the "global" value, no need to use the
// legacy kernel, the legacy resolver is enough
return $this->legacyResolver->getGroup( $groupName );
}
return $this->legacyKernel->runCallback(
function () use ( $file, $groupName, $siteaccess )
{
// TODO: do reset injected settings everytime
// and make sure to restore the previous injected settings
eZINI::injectSettings( array() );
return eZSiteAccess::getIni( $siteaccess, $file )->group( $groupName );
}
);
}

/**
* Returns the value of the legacy parameter $parameterName, either in $defaultSiteaccess or,
* if not found, in the global settings
* Returns the value of the legacy parameter $parameterName in $groupName.
* If $file and $siteaccess are null, the global value is fetched with the
* legacy resolver.
*
* @param $parameterName
* @param $namespace
* @param $siteaccess
* @param string $groupName
* @param string $parameterName
* @param string|null $file
* @param string|null $siteaccess
*
* @internal param $groupName
* @internal param $defaultSiteaccess
* @return array
*
*/
public function getParameterWithFallback( $parameterName, $namespace, $siteaccess )
public function getParameter( $groupName, $parameterName, $file = null, $siteaccess = null )
{
try
{
return $this->legacyResolver->getParameter( $parameterName, $namespace, $siteaccess );
}
// fallback to global override
catch ( \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException $e )
if ( $file === null && $siteaccess === null )
{
return $this->legacyResolver->getParameter( $parameterName, $namespace );
// in this case we want the "global" value, no need to use the
// legacy kernel, the legacy resolver is enough
return $this->legacyResolver->getParameter( "$groupName.$parameterName" );
}
return $this->legacyKernel->runCallback(
function () use ( $file, $groupName, $parameterName, $siteaccess )
{
// TODO: do reset injected settings everytime
// and make sure to restore the previous injected settings
eZINI::injectSettings( array() );
return eZSiteAccess::getIni( $siteaccess, $file )
->variable( $groupName, $parameterName );
}
);
}

protected function resolveMatching()
{
$siteaccessSettings = $this->legacyResolver->getGroup( 'SiteAccessSettings' );
$siteaccessSettings = $this->getGroup( 'SiteAccessSettings' );

$matching = array(); $match = false;
foreach( explode( ';', $siteaccessSettings['MatchOrder'] ) as $matchMethod )
Expand All @@ -262,7 +273,7 @@ protected function resolveMatching()
$match = false;
break;
case 'port':
$match = array( 'Map\Port' => $this->legacyResolver->getGroup( 'PortAccessSettings' ) );
$match = array( 'Map\Port' => $this->getGroup( 'PortAccessSettings' ) );
break;
}
if ( $match !== false )
Expand Down
Expand Up @@ -14,6 +14,19 @@
class ConfigurationConverterTest extends LegacyBasedTestCase
{

protected function getConfigurationConverterMock( array $constructorParams )
{
return $this->getMock(
'eZ\\Bundle\\EzPublishLegacyBundle\\SetupWizard\\ConfigurationConverter',
array(
'getParameter',
'getGroup'
),
$constructorParams
);
}


/**
* @param $package
* @param $adminSiteaccess
Expand All @@ -31,15 +44,25 @@ class ConfigurationConverterTest extends LegacyBasedTestCase
*/
public function testFromLegacy( $package, $adminSiteaccess, $mockParameters, $expectedResult, $exception = null )
{
// a map of arguments + return value for getParameter/getGroup is converted to a callback in order to allow exceptions
$legacyResolver = $this->getLegacyConfigResolverMock();
foreach( $mockParameters as $method => $callbackMap )
$configurationConverter = $this->getConfigurationConverterMock(
array(
$this->getLegacyConfigResolverMock(),
$this->getLegacyKernelMock(),
array( $package )
)
);
foreach ( $mockParameters as $method => $callbackMap )
{
$legacyResolver->expects( $this->any() )->method( $method )->will( $this->returnCallback( $this->convertMapToCallback( $callbackMap ) ) );
$configurationConverter->expects( $this->any() )
->method( $method )
->will(
$this->returnCallback(
$this->convertMapToCallback( $callbackMap )
)
)
;
}

$configurationConverter = new ConfigurationConverter( $legacyResolver, $this->getLegacyKernelMock(), array( $package ) );

try
{
$result = $configurationConverter->fromLegacy( $package, $adminSiteaccess );
Expand Down Expand Up @@ -67,6 +90,8 @@ public function testFromLegacy( $package, $adminSiteaccess, $mockParameters, $ex
}

/**
* Converts a map of arguments + return value to a callback in order to allow exceptions
*
* @param array $callbackMap array of callback parameter arrays [0..n-1 => arguments, n => return value]
*
* @return callable
Expand Down Expand Up @@ -163,24 +188,24 @@ public function providerForTestFromLegacy()

$commonMockParameters = array(
'getParameter' => array(
'SiteSettings.DefaultAccess' => array( 'SiteSettings.DefaultAccess', null, null, 'eng' ),
'SiteAccessSettings.AvailableSiteAccessList' => array( 'SiteAccessSettings.AvailableSiteAccessList', null, null, array( 'eng', 'ezdemo_site', 'ezdemo_site_admin' ) ),
'FileSettings.VarDir' => array( 'FileSettings.VarDir', 'site', 'eng', 'var/ezdemo_site' ),
'FileSettings.StorageDir' => array( 'FileSettings.StorageDir', 'site', 'eng', 'storage' ),
'ImageMagick.IsEnabled' => array( 'ImageMagick.IsEnabled', 'image', 'eng', 'true' ),
'ImageMagick.ExecutablePath' => array( 'ImageMagick.ExecutablePath', 'image', 'eng', '/usr/bin' ),
'ImageMagick.Executable' => array( 'ImageMagick.Executable', 'image', 'eng', 'convert' ),
'SiteSettingsDefaultAccess' => array( 'SiteSettings', 'DefaultAccess', null, null, 'eng' ),
'SiteAccessSettingsAvailableSiteAccessList' => array( 'SiteAccessSettings', 'AvailableSiteAccessList', null, null, array( 'eng', 'ezdemo_site', 'ezdemo_site_admin' ) ),
'FileSettingsVarDir' => array( 'FileSettings', 'VarDir', 'site.ini', 'eng', 'var/ezdemo_site' ),
'FileSettingsStorageDir' => array( 'FileSettings', 'StorageDir', 'site.ini', 'eng', 'storage' ),
'ImageMagickIsEnabled' => array( 'ImageMagick', 'IsEnabled', 'image.ini', 'eng', 'true' ),
'ImageMagickExecutablePath' => array( 'ImageMagick', 'ExecutablePath', 'image.ini', 'eng', '/usr/bin' ),
'ImageMagickExecutable' => array( 'ImageMagick', 'Executable', 'image.ini', 'eng', 'convert' ),
),
'getGroup' => array(
'SiteAccessSettings' => array( 'SiteAccessSettings', null, null,
array( 'MatchOrder' => 'uri', 'URIMatchType' => 'element', 'URIMatchElement' => 1 ) ),
'DatabaseSettings' => array( 'DatabaseSettings', 'site', 'eng',
'DatabaseSettings' => array( 'DatabaseSettings', 'site.ini', 'eng',
array( 'DatabaseImplementation' => 'ezmysqli', 'Server' => 'localhost', 'User' => 'root', 'Password' => '', 'Database' => 'ezdemo' ) ),
'AliasSettings' => array( 'AliasSettings', 'image', 'eng',
'AliasSettings' => array( 'AliasSettings', 'image.ini', 'eng',
array( 'AliasList' => array( 'large', 'infoboximage' ) ) ),
'large' => array( 'large', 'image', 'eng',
'large' => array( 'large', 'image.ini', 'eng',
array( 'Reference' => '', 'Filters' => array( 'geometry/scaledownonly=360;440' ) ) ),
'infoboximage' => array( 'infoboximage', 'image', 'eng',
'infoboximage' => array( 'infoboximage', 'image.ini', 'eng',
array( 'Reference' => '', 'Filters' => array( 'geometry/scalewidth=75', 'flatten' ) ) ),
)
);
Expand All @@ -192,13 +217,13 @@ public function providerForTestFromLegacy()

// empty site list => invalid argument exception
$element = $baseData;
$element[IDX_MOCK_PARAMETERS]['getParameter']['SiteSettings.SiteList'] = array( 'SiteSettings.SiteList', null, null, array() );
$element[IDX_MOCK_PARAMETERS]['getParameter']['SiteSettingsSiteList'] = array( 'SiteSettings', 'SiteList', null, null, array() );
$element[IDX_EXCEPTION] = $exceptionType;
$data[] = $element;

// imagemagick disabled
$element = $baseData;
$element[IDX_MOCK_PARAMETERS]['getParameter']['ImageMagick.IsEnabled'] = array( 'ImageMagick.IsEnabled', 'eng', 'image', 'false' );
$element[IDX_MOCK_PARAMETERS]['getParameter']['ImageMagickIsEnabled'] = array( 'ImageMagick', 'IsEnabled', 'eng', 'image.ini', 'false' );
$element[IDX_EXPECTED_RESULT]['ezpublish']['imagemagick']['enabled'] = false;
unset( $element[IDX_EXPECTED_RESULT]['ezpublish']['imagemagick']['path'] );
$data[] = $element;
Expand Down Expand Up @@ -235,7 +260,7 @@ public function providerForTestFromLegacy()

// customized storage dir
$element = $baseData;
$element[IDX_MOCK_PARAMETERS]['getParameter']['FileSettings.StorageDir'] = array( 'FileSettings.StorageDir', 'site', 'eng', 'customstorage');
$element[IDX_MOCK_PARAMETERS]['getParameter']['FileSettingsStorageDir'] = array( 'FileSettings', 'StorageDir', 'site.ini', 'eng', 'customstorage' );
$element[IDX_EXPECTED_RESULT]['ezpublish']['system']['ezdemo_group']['storage_dir'] = 'customstorage';
$data[] = $element;

Expand Down

0 comments on commit 55382c0

Please sign in to comment.