Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
therouv committed Jul 31, 2016
2 parents 468369f + 778b3c1 commit e10349e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
28 changes: 20 additions & 8 deletions Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 4 additions & 6 deletions Model/Processor/ImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>ERROR: Invalid scopeId "%s" for scope "%s" (%s => %s)</error>',
Expand All @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions Model/Validator/ScopeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion Test/Unit/Model/Validator/ScopeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,15 +21,28 @@ class ScopeValidatorTest extends \PHPUnit_Framework_TestCase
*/
private $validator;

/**
* @var PHPUnit_Framework_MockObject_MockObject|WebsiteInterface
*/
protected $mockWebsiteOne = null;



/**
* Set up test class
*/
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);
Expand All @@ -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?'));
}
}

0 comments on commit e10349e

Please sign in to comment.