Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpartington-cti committed Jun 29, 2017
2 parents dcef1e7 + ff6e051 commit fd78c64
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 6 deletions.
94 changes: 89 additions & 5 deletions Model/Component/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\WebsiteFactory;
use Magento\Theme\Model\ResourceModel\Theme\Collection;
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory;
use Symfony\Component\Yaml\Yaml;

class Config extends YamlComponentAbstract
{
const PATH_THEME_ID = 'design/theme/theme_id';

protected $alias = 'config';
protected $name = 'Configuration';
Expand All @@ -26,12 +29,28 @@ class Config extends YamlComponentAbstract
*/
protected $scopeConfig;

public function __construct(LoggingInterface $log, ObjectManagerInterface $objectManager)
{
/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* Config constructor.
*
* @param LoggingInterface $log
* @param ObjectManagerInterface $objectManager
* @param CollectionFactory $collectionFactory
*/
public function __construct(
LoggingInterface $log,
ObjectManagerInterface $objectManager,
CollectionFactory $collectionFactory
) {
parent::__construct($log, $objectManager);

$this->configResource = $this->objectManager->create(\Magento\Config\Model\ResourceModel\Config::class);
$this->scopeConfig = $this->objectManager->create(\Magento\Framework\App\Config::class);
$this->collectionFactory = $collectionFactory;
}

/**
Expand All @@ -50,22 +69,36 @@ protected function processData($data = null)

if ($scope == "global") {
foreach ($configurations as $configuration) {
$this->setGlobalConfig($configuration['path'], $configuration['value']);
$convertedConfiguration = $this->convert($configuration);
$this->setGlobalConfig(
$convertedConfiguration['path'],
$convertedConfiguration['value']
);
}
}

if ($scope == "websites") {
foreach ($configurations as $code => $websiteConfigurations) {
foreach ($websiteConfigurations as $configuration) {
$this->setWebsiteConfig($configuration['path'], $configuration['value'], $code);
$convertedConfiguration = $this->convert($configuration);
$this->setWebsiteConfig(
$convertedConfiguration['path'],
$convertedConfiguration['value'],
$code
);
}
}
}

if ($scope == "stores") {
foreach ($configurations as $code => $storeConfigurations) {
foreach ($storeConfigurations as $configuration) {
$this->setStoreConfig($configuration['path'], $configuration['value'], $code);
$convertedConfiguration = $this->convert($configuration);
$this->setStoreConfig(
$convertedConfiguration['path'],
$convertedConfiguration['value'],
$code
);
}
}
}
Expand Down Expand Up @@ -136,6 +169,24 @@ private function setWebsiteConfig($path, $value, $code, $encrypted = 0)
}
}

/**
* Convert paths or values before they're processed
*
* @param array $configuration
*
* @return array
*/
protected function convert(array $configuration)
{
$convertedConfig = $configuration;
if (isset($convertedConfig['path']) && isset($convertedConfig['value'])) {
if ($this->isConfigTheme($convertedConfig['path'], $convertedConfig['value'])) {
$convertedConfig['value'] = $this->getThemeIdByPath($convertedConfig['value']);
}
}
return $convertedConfig;
}

private function setStoreConfig($path, $value, $code, $encrypted = 0)
{
try {
Expand Down Expand Up @@ -169,4 +220,37 @@ private function setStoreConfig($path, $value, $code, $encrypted = 0)
}

}

/**
* Checks if the config path is setting the theme by its path so we can get the ID
*
* @param $path
* @param $value
*
* @return bool
*/
public function isConfigTheme($path, $value)
{
if ($path === self::PATH_THEME_ID && is_int($value) === false) {
return true;
}
return false;
}

/**
* Get the theme ID by the path
*
* @param $themePath
*
* @return int
*/
public function getThemeIdByPath($themePath)
{
/**
* @var Collection $themeCollection
*/
$themeCollection = $this->collectionFactory->create();
$theme = $themeCollection->getThemeByFullPath($themePath);
return $theme->getThemeId();
}
}
84 changes: 83 additions & 1 deletion Test/Unit/Component/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,89 @@ class ConfigTest extends ComponentAbstractTestCase

protected function componentSetUp()
{
$this->component = new Config($this->logInterface, $this->objectManager);
$collectionFactory = $this->getMockBuilder('\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory')
->disableOriginalConstructor()
->getMock();
$this->component = new Config($this->logInterface, $this->objectManager, $collectionFactory);
$this->className = Config::class;
}

/**
* Test check if the path is the configuration path without an ID
*/
public function testIsConfigTheme()
{
/**
* @var Config $config
*/
$config = $this->testObjectManager->getObject(Config::class);
$this->assertTrue($config->isConfigTheme($config::PATH_THEME_ID, 'theme'));
}

/**
* Test ignoring a path that has an ID for the config path
*/
public function testIsConfigThemeWithAnId()
{
/**
* @var Config $config
*/
$config = $this->testObjectManager->getObject(Config::class);
$this->assertTrue($config->isConfigTheme($config::PATH_THEME_ID, '1'));
}

/**
* Test ignoring non-config theme path
*/
public function testNotConfigTheme()
{
/**
* @var Config $config
*/
$config = $this->testObjectManager->getObject(Config::class);
$this->assertFalse($config->isConfigTheme('a/path', '1'));
}

/**
* Test getting theme by the path
*/
public function testGetThemeById()
{
$mockThemeModel = $this->getMockBuilder('Magento\Theme\Model\Theme')
->disableOriginalConstructor()
->setMethods(['getThemeId'])
->getMock();

$mockThemeModel->expects($this->once())
->method('getThemeId')
->willReturn(3);

$mockFactory = $this->getMockBuilder('Magento\Theme\Model\ResourceModel\Theme\CollectionFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$mockCollection = $this->getMockBuilder('Magento\Theme\Model\ResourceModel\Theme\Collection')
->disableOriginalConstructor()
->setMethods(['getThemeByFullPath'])
->getMock();

$mockCollection->expects($this->once())
->method('getThemeByFullPath')
->with('frontend/test/theme')
->willReturn($mockThemeModel);

$mockFactory->expects($this->once())
->method('create')
->willReturn($mockCollection);

$config = $this->testObjectManager->getObject(
Config::class,
[
'collectionFactory' => $mockFactory
]
);

$this->assertEquals(3, $config->getThemeIdByPath('frontend/test/theme'));
}
}

0 comments on commit fd78c64

Please sign in to comment.