diff --git a/Model/Component/Config.php b/Model/Component/Config.php index a79482f..98d1359 100644 --- a/Model/Component/Config.php +++ b/Model/Component/Config.php @@ -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'; @@ -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; } /** @@ -50,14 +69,23 @@ 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 + ); } } } @@ -65,7 +93,12 @@ protected function processData($data = null) 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 + ); } } } @@ -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 { @@ -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(); + } } diff --git a/Test/Unit/Component/ConfigTest.php b/Test/Unit/Component/ConfigTest.php index 4e5dbea..9e2f5a0 100644 --- a/Test/Unit/Component/ConfigTest.php +++ b/Test/Unit/Component/ConfigTest.php @@ -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')); + } }