Skip to content

Commit f88a510

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'falcons/MAGETWO-62735' into MAGETWO-63936-2
2 parents 061977e + 8644bc1 commit f88a510

File tree

5 files changed

+396
-1
lines changed

5 files changed

+396
-1
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Theme\Model\Source;
7+
8+
use Magento\Framework\App\Config\ConfigSourceInterface;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Theme\Model\ResourceModel\Theme;
11+
use Magento\Theme\Model\ResourceModel\ThemeFactory;
12+
use Magento\Framework\DataObject\Factory as DataObjectFactory;
13+
14+
/**
15+
* Class InitialThemeSource.
16+
*
17+
* Retrieves theme configurations by path.
18+
*/
19+
class InitialThemeSource implements ConfigSourceInterface
20+
{
21+
/**
22+
* A deployment config.
23+
*
24+
* @var DeploymentConfig
25+
*/
26+
private $deploymentConfig;
27+
28+
/**
29+
* A theme factory.
30+
*
31+
* @var ThemeFactory
32+
*/
33+
private $themeFactory;
34+
35+
/**
36+
* A data object factory.
37+
*
38+
* @var DataObjectFactory
39+
*/
40+
private $dataObjectFactory;
41+
42+
/**
43+
* Array with theme data.
44+
*
45+
* @var array
46+
*/
47+
private $data;
48+
49+
/**
50+
* @param DeploymentConfig $deploymentConfig A deployment config
51+
* @param ThemeFactory $themeFactory A theme factory
52+
* @param DataObjectFactory $dataObjectFactory A data object factory
53+
*/
54+
public function __construct(
55+
DeploymentConfig $deploymentConfig,
56+
ThemeFactory $themeFactory,
57+
DataObjectFactory $dataObjectFactory
58+
) {
59+
$this->deploymentConfig = $deploymentConfig;
60+
$this->themeFactory = $themeFactory;
61+
$this->dataObjectFactory = $dataObjectFactory;
62+
}
63+
64+
/**
65+
* Retrieves configuration data array.
66+
* Example:
67+
*
68+
* ```php
69+
* ['Magento/backend' =>
70+
* [
71+
* 'parent_id' => NULL,
72+
* 'theme_path' => 'Magento/backend',
73+
* 'theme_title' => 'Magento 2 backend',
74+
* 'is_featured' => '0',
75+
* 'area' => 'adminhtml',
76+
* 'type' => '0',
77+
* 'code' => 'Magento/backend',
78+
* ]
79+
* ]
80+
* ```
81+
*
82+
* @param string $path The path to theme configuration.
83+
* @return array The data array with theme configurations.
84+
*/
85+
public function get($path = '')
86+
{
87+
if (!$this->deploymentConfig->isDbAvailable()) {
88+
return [];
89+
}
90+
91+
if (!$this->data) {
92+
$rawThemes = $this->fetchThemes();
93+
$themes = [];
94+
95+
foreach ($rawThemes as $themeRow) {
96+
unset($themeRow['theme_id'], $themeRow['preview_image']);
97+
98+
$themes[$themeRow['code']] = $themeRow;
99+
100+
if (isset($rawThemes[$themeRow['parent_id']]['code'])) {
101+
$themes[$themeRow['code']]['parent_id'] = $rawThemes[$themeRow['parent_id']]['code'];
102+
}
103+
}
104+
105+
$this->data = $this->dataObjectFactory->create($themes);
106+
}
107+
108+
return $this->data->getData($path) ?: [];
109+
}
110+
111+
/**
112+
* Fetches themes from data source.
113+
*
114+
* @return array An associative list with found themes
115+
*/
116+
private function fetchThemes()
117+
{
118+
/** @var Theme $theme */
119+
$theme = $this->themeFactory->create();
120+
$select = $theme->getConnection()->select()
121+
->from($theme->getMainTable())
122+
->order('theme_id');
123+
124+
return $theme->getConnection()->fetchAssoc($select);
125+
}
126+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Theme\Test\Unit\Model\Source;
7+
8+
use Magento\Framework\App\DeploymentConfig;
9+
use Magento\Framework\DataObject;
10+
use Magento\Theme\Model\ResourceModel\Theme;
11+
use Magento\Theme\Model\ResourceModel\ThemeFactory;
12+
use Magento\Theme\Model\Source\InitialThemeSource;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use PHPUnit_Framework_MockObject_MockObject as Mock;
15+
use Magento\Framework\DB\Select;
16+
use Magento\Framework\DataObject\Factory as DataObjectFactory;
17+
18+
/**
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
*/
21+
class InitialThemeSourceTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @var InitialThemeSource
25+
*/
26+
private $model;
27+
28+
/**
29+
* @var DeploymentConfig|Mock
30+
*/
31+
private $deploymentConfigMock;
32+
33+
/**
34+
* @var ThemeFactory|Mock
35+
*/
36+
private $themeFactoryMock;
37+
38+
/**
39+
* @var DataObjectFactory|Mock
40+
*/
41+
private $dataObjectFactoryMock;
42+
43+
/**
44+
* @var DataObject|Mock
45+
*/
46+
private $dataObjectMock;
47+
48+
/**
49+
* @var Theme|Mock
50+
*/
51+
private $themeMock;
52+
53+
/**
54+
* @var AdapterInterface|Mock
55+
*/
56+
private $connectionMock;
57+
58+
/**
59+
* @var Select|Mock
60+
*/
61+
private $selectMock;
62+
63+
protected function setUp()
64+
{
65+
$this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->themeFactoryMock = $this->getMockBuilder(ThemeFactory::class)
69+
->setMethods(['create'])
70+
->disableOriginalConstructor()
71+
->getMock();
72+
$this->themeMock = $this->getMockBuilder(Theme::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
$this->selectMock = $this->getMockBuilder(Select::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
79+
->getMockForAbstractClass();
80+
$this->dataObjectFactoryMock = $this->getMockBuilder(DataObjectFactory::class)
81+
->setMethods(['create'])
82+
->disableOriginalConstructor()
83+
->getMock();
84+
$this->dataObjectMock = $this->getMockBuilder(DataObject::class)
85+
->disableOriginalConstructor()
86+
->getMock();
87+
88+
$this->themeMock->expects($this->any())
89+
->method('getConnection')
90+
->willReturn($this->connectionMock);
91+
$this->themeFactoryMock->expects($this->any())
92+
->method('create')
93+
->willReturn($this->themeMock);
94+
$this->connectionMock->expects($this->any())
95+
->method('select')
96+
->willReturn($this->selectMock);
97+
$this->selectMock->expects($this->any())
98+
->method('from')
99+
->willReturnSelf();
100+
$this->selectMock->expects($this->any())
101+
->method('sort')
102+
->willReturnSelf();
103+
104+
$this->model = new InitialThemeSource(
105+
$this->deploymentConfigMock,
106+
$this->themeFactoryMock,
107+
$this->dataObjectFactoryMock
108+
);
109+
}
110+
111+
public function testGetNotDeployed()
112+
{
113+
$this->deploymentConfigMock->expects($this->once())
114+
->method('isDbAvailable')
115+
->willReturn(false);
116+
117+
$this->assertSame([], $this->model->get());
118+
}
119+
120+
public function testGet()
121+
{
122+
$this->deploymentConfigMock->expects($this->once())
123+
->method('isDbAvailable')
124+
->willReturn(true);
125+
$this->connectionMock->expects($this->once())
126+
->method('fetchAssoc')
127+
->willReturn(
128+
[
129+
'1' => [
130+
'theme_id' => '1',
131+
'parent_id' => null,
132+
'theme_path' => 'Magento/backend',
133+
'theme_title' => 'Magento 2 backend',
134+
'is_featured' => '0',
135+
'area' => 'adminhtml',
136+
'type' => '0',
137+
'code' => 'Magento/backend',
138+
],
139+
'2' => [
140+
'theme_id' => '2',
141+
'parent_id' => null,
142+
'theme_path' => 'Magento/blank',
143+
'theme_title' => 'Magento Blank',
144+
'is_featured' => '0',
145+
'area' => 'frontend',
146+
'type' => '0',
147+
'code' => 'Magento/blank',
148+
],
149+
'3' => [
150+
'theme_id' => '3',
151+
'parent_id' => '2',
152+
'theme_path' => 'Magento/luma',
153+
'theme_title' => 'Magento Luma',
154+
'is_featured' => '0',
155+
'area' => 'frontend',
156+
'type' => '0',
157+
'code' => 'Magento/luma',
158+
],
159+
]
160+
);
161+
$this->dataObjectFactoryMock->expects($this->once())
162+
->method('create')
163+
->with(
164+
[
165+
'Magento/backend' => [
166+
'parent_id' => null,
167+
'theme_path' => 'Magento/backend',
168+
'theme_title' => 'Magento 2 backend',
169+
'is_featured' => '0',
170+
'area' => 'adminhtml',
171+
'type' => '0',
172+
'code' => 'Magento/backend',
173+
],
174+
'Magento/blank' => [
175+
'parent_id' => null,
176+
'theme_path' => 'Magento/blank',
177+
'theme_title' => 'Magento Blank',
178+
'is_featured' => '0',
179+
'area' => 'frontend',
180+
'type' => '0',
181+
'code' => 'Magento/blank',
182+
],
183+
'Magento/luma' => [
184+
'parent_id' => 'Magento/blank',
185+
'theme_path' => 'Magento/luma',
186+
'theme_title' => 'Magento Luma',
187+
'is_featured' => '0',
188+
'area' => 'frontend',
189+
'type' => '0',
190+
'code' => 'Magento/luma',
191+
],
192+
]
193+
)
194+
->willReturn($this->dataObjectMock);
195+
$this->dataObjectMock->expects($this->once())
196+
->method('getData');
197+
198+
$this->model->get();
199+
}
200+
}

app/code/Magento/Theme/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
},
1919
"suggest": {
2020
"magento/module-translation": "100.2.*",
21-
"magento/module-theme-sample-data": "Sample Data version:100.2.*"
21+
"magento/module-theme-sample-data": "Sample Data version:100.2.*",
22+
"magento/module-deploy": "100.2.*"
2223
},
2324
"type": "magento2-module",
2425
"version": "100.2.0-dev",

app/code/Magento/Theme/etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,14 @@
259259
<type name="Magento\Store\Model\Group">
260260
<plugin name="themeDesignConfigGridIndexerStoreGroup" type="Magento\Theme\Model\Indexer\Design\Config\Plugin\StoreGroup"/>
261261
</type>
262+
<type name="Magento\Deploy\Console\Command\App\ApplicationDumpCommand">
263+
<arguments>
264+
<argument name="sources" xsi:type="array">
265+
<item name="themes" xsi:type="array">
266+
<item name="source" xsi:type="object">Magento\Theme\Model\Source\InitialThemeSource</item>
267+
<item name="namespace" xsi:type="string">themes</item>
268+
</item>
269+
</argument>
270+
</arguments>
271+
</type>
262272
</config>

0 commit comments

Comments
 (0)