diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index 9119f66..02feeca 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -9,6 +9,7 @@ use Magento\Framework\App\State as AppState; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Registry; +use Magento\Framework\App\Area; use Magento\Framework\App\Cache\Manager as CacheManager; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -58,12 +59,12 @@ abstract class AbstractCommand extends Command private $cacheManager; /** - * @param Registry $registry - * @param AppState $appState - * @param ConfigLoader $configLoader - * @param ObjectManagerInterface $objectManager - * @param CacheManager $cacheManager - * @param null $name + * @param Registry $registry + * @param AppState $appState + * @param ConfigLoader $configLoader + * @param ObjectManagerInterface $objectManager + * @param CacheManager $cacheManager + * @param null $name */ public function __construct( Registry $registry, @@ -91,8 +92,19 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->input = $input; $this->output = $output; - $this->appState->setAreaCode('adminhtml'); - $this->objectManager->configure($this->configLoader->load('adminhtml')); + try { + $area = $this->appState->getAreaCode(); + if ($area != Area::AREA_ADMINHTML) { + $this->appState->setAreaCode(Area::AREA_ADMINHTML); + } + } catch (\Magento\Framework\Exception\LocalizedException $e) { + $this->appState->setAreaCode(Area::AREA_ADMINHTML); + } + + $area = $this->appState->getAreaCode(); + $configLoader = $this->objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface'); + $this->objectManager->configure($configLoader->load($area)); + $this->registry->register('isSecureArea', true); } diff --git a/Model/Processor/ImportProcessor.php b/Model/Processor/ImportProcessor.php index a7b91ff..353a1f1 100644 --- a/Model/Processor/ImportProcessor.php +++ b/Model/Processor/ImportProcessor.php @@ -107,9 +107,11 @@ public function transformConfigToScopeConfig($path, array $config) { $return = []; foreach ($config as $scope => $scopeIdValue) { - foreach ($scopeIdValue as $scopeId => $value) { - $scopeId = (int)$scopeId; + if (!$scopeIdValue) { + continue; + } + foreach ($scopeIdValue as $scopeId => $value) { if (!$this->scopeValidator->validate($scope, $scopeId)) { $errorMsg = sprintf( 'ERROR: Invalid scopeId "%s" for scope "%s" (%s => %s)', @@ -122,10 +124,6 @@ public function transformConfigToScopeConfig($path, array $config) continue; } - // Valid scope Write output - $value = str_replace("\r", '', addcslashes($value, '"')); - $value = str_replace("\n", '\\n', $value); - $return[] = [ 'value' => $value, 'scope' => $scope, diff --git a/Model/Validator/ScopeValidator.php b/Model/Validator/ScopeValidator.php index 83a0b89..4bf4941 100644 --- a/Model/Validator/ScopeValidator.php +++ b/Model/Validator/ScopeValidator.php @@ -5,7 +5,9 @@ */ namespace Semaio\ConfigImportExport\Model\Validator; +use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; +use Magento\Store\Model\Website; /** * Class ScopeValidator @@ -63,6 +65,18 @@ private function isValidWebsiteId($websiteId) return true; } + if (is_numeric($websiteId)) { + // Dont bother checking website codes on numeric input + return false; + } + // @todo hs: build up array of websiteCodes, to prevent wasting time looping + /** @var Website $website */ + foreach ($websites as $website) { + if ($website->getCode() == $websiteId) { + return true; + } + } + return false; } @@ -79,6 +93,18 @@ private function isValidStoreId($storeId) return true; } + // @todo hs: build up array of storeCodes, to prevent wasting time looping + if (is_numeric($storeId)) { + // Dont bother checking store codes on numeric input + return false; + } + /** @var Store $store */ + foreach ($stores as $store) { + if ($store->getCode() == $storeId) { + return true; + } + } + return false; } } diff --git a/README.md b/README.md index be96b52..89d9802 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This module is inspired by the awesome n98-magerun plugin "HarrisStreet ImpEx" b ## Facts -Version: 2.0.0 +Version: 2.1.0 ## Functionality diff --git a/Test/Unit/Model/Validator/ScopeValidatorTest.php b/Test/Unit/Model/Validator/ScopeValidatorTest.php index b7d07a5..b440114 100644 --- a/Test/Unit/Model/Validator/ScopeValidatorTest.php +++ b/Test/Unit/Model/Validator/ScopeValidatorTest.php @@ -5,6 +5,8 @@ */ namespace Semaio\ConfigImportExport\Test\Unit\Model\Validator; +use Magento\Store\Api\Data\WebsiteInterface; +use Magento\Store\Model\Website; use Semaio\ConfigImportExport\Model\Validator\ScopeValidator; /** @@ -19,6 +21,13 @@ class ScopeValidatorTest extends \PHPUnit_Framework_TestCase */ private $validator; + /** + * @var PHPUnit_Framework_MockObject_MockObject|WebsiteInterface + */ + protected $mockWebsiteOne = null; + + + /** * Set up test class */ @@ -26,8 +35,14 @@ public function setUp() { parent::setUp(); + $this->mockWebsiteOne = $this->getMockBuilder( + WebsiteInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getCode']) + ->getMockForAbstractClass(); + $storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface'); - $storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'ABC']); + $storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => $this->mockWebsiteOne]); $storeManagerMock->expects($this->any())->method('getStores')->willReturn([2 => 'ABC']); $this->validator = new ScopeValidator($storeManagerMock); @@ -47,4 +62,18 @@ public function validate() $this->assertTrue($this->validator->validate('stores', 2)); $this->assertFalse($this->validator->validate('stores', 3)); } + + /** + * @test + */ + public function validateNonNumericWebsite() + { + $existingWebsiteCode = 'my-cool-website'; + + $this->mockWebsiteOne ->expects($this->any())->method('getCode') + ->will($this->returnValue($existingWebsiteCode)); + + $this->assertTrue($this->validator->validate('websites', $existingWebsiteCode)); + $this->assertFalse($this->validator->validate('websites', 'am i real?')); + } }