Skip to content

Commit 3d63d3e

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'falcons/MAGETWO-69535' into MAGETWO-69983
2 parents 9c59211 + f2899dc commit 3d63d3e

File tree

7 files changed

+457
-175
lines changed

7 files changed

+457
-175
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\App\Config\Source;
7+
8+
use Magento\Framework\App\Config\ConfigSourceInterface;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\DeploymentConfig\Reader;
11+
use Magento\Framework\DataObjectFactory;
12+
13+
/**
14+
* Config source to retrieve configuration from files.
15+
*/
16+
class InitialConfigSource implements ConfigSourceInterface
17+
{
18+
/**
19+
* The file reader
20+
*
21+
* @var Reader
22+
*/
23+
private $reader;
24+
25+
/**
26+
* The deployment config reader
27+
*
28+
* @var DeploymentConfig
29+
*/
30+
private $deploymentConfig;
31+
32+
/**
33+
* The config type
34+
*
35+
* @var string
36+
*/
37+
private $configType;
38+
39+
/**
40+
* The DataObject factory.
41+
*
42+
* @var DataObjectFactory
43+
*/
44+
private $dataObjectFactory;
45+
46+
/**
47+
* @param Reader $reader The file reader
48+
* @param DeploymentConfig $deploymentConfig The deployment config reader
49+
* @param DataObjectFactory $dataObjectFactory The DataObject factory
50+
* @param string $configType The config type
51+
*/
52+
public function __construct(
53+
Reader $reader,
54+
DeploymentConfig $deploymentConfig,
55+
DataObjectFactory $dataObjectFactory,
56+
$configType
57+
) {
58+
$this->reader = $reader;
59+
$this->deploymentConfig = $deploymentConfig;
60+
$this->dataObjectFactory = $dataObjectFactory;
61+
$this->configType = $configType;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function get($path = '')
68+
{
69+
/**
70+
* Magento store configuration should not be read from file source
71+
* on not installed instance.
72+
*
73+
* @see \Magento\Store\Model\Config\Importer To import store configs
74+
*/
75+
if (!$this->deploymentConfig->isAvailable()) {
76+
return [];
77+
}
78+
79+
$data = $this->dataObjectFactory->create([
80+
'data' => $this->reader->load()
81+
]);
82+
83+
if ($path !== '' && $path !== null) {
84+
$path = '/' . ltrim($path, '/');
85+
}
86+
87+
return $data->getData($this->configType . $path) ?: [];
88+
}
89+
}

app/code/Magento/Store/Model/Config/Importer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public function __construct(
8484
public function import(array $data)
8585
{
8686
$actions = [
87-
ProcessorFactory::TYPE_DELETE,
8887
ProcessorFactory::TYPE_CREATE,
88+
ProcessorFactory::TYPE_DELETE,
8989
ProcessorFactory::TYPE_UPDATE
9090
];
9191
$messages = ['Stores were processed'];

app/code/Magento/Store/Model/Config/Importer/Processor/Create.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,20 @@ public function run(array $data)
125125
private function createWebsites(array $items, array $data)
126126
{
127127
foreach ($items as $websiteData) {
128-
unset($websiteData['website_id']);
128+
$groupId = $websiteData['default_group_id'];
129+
130+
unset(
131+
$websiteData['website_id'],
132+
$websiteData['default_group_id']
133+
);
129134

130135
$website = $this->websiteFactory->create();
131136
$website->setData($websiteData);
132137
$website->getResource()->save($website);
133138

134-
$website->getResource()->addCommitCallback(function () use ($website, $data) {
139+
$website->getResource()->addCommitCallback(function () use ($website, $data, $groupId) {
135140
$website->setDefaultGroupId(
136-
$this->detectGroupById($data, $website->getDefaultGroupId())->getId()
141+
$this->detectGroupById($data, $groupId)->getId()
137142
);
138143
$website->getResource()->save($website);
139144
});
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Test\Unit\App\Config\Source;
7+
8+
use Magento\Framework\App\DeploymentConfig;
9+
use Magento\Framework\App\DeploymentConfig\Reader;
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
use Magento\Store\App\Config\Source\InitialConfigSource;
13+
use PHPUnit_Framework_MockObject_MockObject as Mock;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class InitialConfigSourceTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var Reader|Mock
22+
*/
23+
private $readerMock;
24+
25+
/**
26+
* @var DeploymentConfig|Mock
27+
*/
28+
private $deploymentConfigMock;
29+
30+
/**
31+
* @var DataObjectFactory|Mock
32+
*/
33+
private $dataObjectFactory;
34+
35+
/**
36+
* @var DataObject|Mock
37+
*/
38+
private $dataObjectMock;
39+
40+
/**
41+
* @var InitialConfigSource
42+
*/
43+
private $source;
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function setUp()
49+
{
50+
$this->readerMock = $this->getMockBuilder(Reader::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
$this->dataObjectFactory = $this->getMockBuilder(DataObjectFactory::class)
57+
->setMethods(['create'])
58+
->disableOriginalConstructor()
59+
->getMock();
60+
$this->dataObjectMock = $this->getMockBuilder(DataObject::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$this->source = new InitialConfigSource(
65+
$this->readerMock,
66+
$this->deploymentConfigMock,
67+
$this->dataObjectFactory,
68+
'configType'
69+
);
70+
}
71+
72+
/**
73+
* @param string $path
74+
* @param array $data
75+
* @param string|array $expected
76+
* @param string $expectedPath
77+
* @dataProvider getDataProvider
78+
*/
79+
public function testGet($path, $data, $expected, $expectedPath)
80+
{
81+
$this->readerMock->expects($this->once())
82+
->method('load')
83+
->willReturn($data);
84+
$this->deploymentConfigMock->expects($this->once())
85+
->method('isAvailable')
86+
->willReturn(true);
87+
$this->dataObjectFactory->expects($this->once())
88+
->method('create')
89+
->with(['data' => $data])
90+
->willReturn($this->dataObjectMock);
91+
$this->dataObjectMock->expects($this->once())
92+
->method('getData')
93+
->with($expectedPath)
94+
->willReturn($expected);
95+
96+
$this->assertEquals($expected, $this->source->get($path));
97+
}
98+
99+
/**
100+
* @return array
101+
*/
102+
public function getDataProvider()
103+
{
104+
return [
105+
'simple path' => ['path', ['configType' => 'value'], 'value', 'configType/path'],
106+
'empty path' => ['', [], [], 'configType'],
107+
'null path' => [null, [], [], 'configType'],
108+
'leading path' => ['/path', [], [], 'configType/path']
109+
];
110+
}
111+
112+
public function testGetNotInstalled()
113+
{
114+
$path = 'path';
115+
116+
$this->readerMock->expects($this->never())
117+
->method('load');
118+
$this->deploymentConfigMock->expects($this->once())
119+
->method('isAvailable')
120+
->willReturn(false);
121+
122+
$this->assertEquals([], $this->source->get($path));
123+
}
124+
}

0 commit comments

Comments
 (0)