Skip to content

Commit 733f260

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-43188' into BUGS
2 parents 6969b54 + 0aeac1b commit 733f260

File tree

2 files changed

+117
-10
lines changed

2 files changed

+117
-10
lines changed

app/code/Magento/Theme/Model/Theme/Plugin/Registration.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Psr\Log\LoggerInterface;
1313
use Magento\Framework\App\State as AppState;
14+
use Magento\Theme\Model\Theme\Collection as ThemeCollection;
15+
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeLoader;
16+
use Magento\Framework\Config\Theme;
1417

18+
/**
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
*/
1521
class Registration
1622
{
1723
/** @var ThemeRegistration */
1824
protected $themeRegistration;
1925

26+
/** @var ThemeCollection */
27+
protected $themeCollection;
28+
29+
/** @var ThemeLoader */
30+
protected $themeLoader;
31+
2032
/** @var LoggerInterface */
2133
protected $logger;
2234

@@ -25,21 +37,27 @@ class Registration
2537

2638
/**
2739
* @param ThemeRegistration $themeRegistration
40+
* @param ThemeCollection $themeCollection
41+
* @param ThemeLoader $themeLoader
2842
* @param LoggerInterface $logger
2943
* @param AppState $appState
3044
*/
3145
public function __construct(
3246
ThemeRegistration $themeRegistration,
47+
ThemeCollection $themeCollection,
48+
ThemeLoader $themeLoader,
3349
LoggerInterface $logger,
3450
AppState $appState
3551
) {
3652
$this->themeRegistration = $themeRegistration;
53+
$this->themeCollection = $themeCollection;
54+
$this->themeLoader = $themeLoader;
3755
$this->logger = $logger;
3856
$this->appState = $appState;
3957
}
4058

4159
/**
42-
* Add new theme from filesystem
60+
* Add new theme from filesystem and update existing
4361
*
4462
* @param AbstractAction $subject
4563
* @param RequestInterface $request
@@ -54,9 +72,37 @@ public function beforeDispatch(
5472
try {
5573
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
5674
$this->themeRegistration->register();
75+
$this->updateThemeData();
5776
}
5877
} catch (LocalizedException $e) {
5978
$this->logger->critical($e);
6079
}
6180
}
81+
82+
/**
83+
* Update theme data
84+
*
85+
* @return void
86+
*/
87+
protected function updateThemeData()
88+
{
89+
$themesData = $this->themeCollection->loadData();
90+
/** @var \Magento\Theme\Model\Theme $themeData */
91+
foreach ($themesData as $themeData) {
92+
if ($themeData->getParentTheme()) {
93+
$parentTheme = $this->themeLoader->getThemeByFullPath(
94+
$themeData->getParentTheme()->getFullPath()
95+
);
96+
$themeData->setParentId($parentTheme->getId());
97+
}
98+
99+
/** @var \Magento\Theme\Model\Theme $theme */
100+
$theme = $this->themeLoader->getThemeByFullPath(
101+
$themeData->getArea()
102+
. Theme::THEME_PATH_SEPARATOR
103+
. $themeData->getThemePath()
104+
);
105+
$theme->addData($themeData->toArray())->save();
106+
}
107+
}
62108
}

app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,100 @@ class RegistrationTest extends \PHPUnit_Framework_TestCase
2626
/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
2727
protected $appState;
2828

29+
/** @var \Magento\Theme\Model\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
30+
protected $themeCollection;
31+
32+
/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection|\PHPUnit_Framework_MockObject_MockObject */
33+
protected $themeLoader;
34+
35+
/** @var Registration */
36+
protected $plugin;
37+
2938
public function setUp()
3039
{
3140
$this->themeRegistration = $this->getMock('Magento\Theme\Model\Theme\Registration', [], [], '', false);
3241
$this->logger = $this->getMockForAbstractClass('Psr\Log\LoggerInterface', [], '', false);
3342
$this->abstractAction = $this->getMockForAbstractClass('Magento\Backend\App\AbstractAction', [], '', false);
3443
$this->request = $this->getMockForAbstractClass('Magento\Framework\App\RequestInterface', [], '', false);
3544
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
45+
$this->themeCollection = $this->getMock('Magento\Theme\Model\Theme\Collection', [], [], '', false);
46+
$this->themeLoader = $this->getMock('Magento\Theme\Model\ResourceModel\Theme\Collection', [], [], '', false);
47+
$this->plugin = new Registration(
48+
$this->themeRegistration,
49+
$this->themeCollection,
50+
$this->themeLoader,
51+
$this->logger,
52+
$this->appState
53+
);
3654
}
3755

3856
public function testBeforeDispatch()
3957
{
58+
$theme = $this->getMock(
59+
'Magento\Theme\Model\Theme',
60+
[
61+
'setParentId',
62+
'getArea',
63+
'getThemePath',
64+
'getParentTheme',
65+
'getId',
66+
'getFullPath',
67+
'toArray',
68+
'addData',
69+
'save',
70+
],
71+
[],
72+
'',
73+
false
74+
);
4075
$this->appState->expects($this->once())->method('getMode')->willReturn('default');
4176
$this->themeRegistration->expects($this->once())->method('register');
42-
$this->logger->expects($this->never())->method('critical');
43-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
44-
$object->beforeDispatch($this->abstractAction, $this->request);
77+
$this->themeCollection->expects($this->once())->method('loadData')->willReturn([$theme]);
78+
$theme->expects($this->once())->method('getArea')->willReturn('frontend');
79+
$theme->expects($this->once())->method('getThemePath')->willReturn('Magento/luma');
80+
$theme->expects($this->exactly(2))->method('getParentTheme')->willReturnSelf();
81+
$theme->expects($this->once())->method('getId')->willReturn(1);
82+
$theme->expects($this->once())->method('getFullPath')->willReturn('frontend/Magento/blank');
83+
$theme->expects($this->once())->method('setParentId')->with(1);
84+
$this->themeLoader->expects($this->exactly(2))
85+
->method('getThemeByFullPath')
86+
->withConsecutive(
87+
['frontend/Magento/blank'],
88+
['frontend/Magento/luma']
89+
)
90+
->will($this->onConsecutiveCalls(
91+
$theme,
92+
$theme
93+
));
94+
$theme->expects($this->once())
95+
->method('toArray')
96+
->willReturn([
97+
'title' => 'Magento Luma'
98+
]);
99+
$theme->expects($this->once())
100+
->method('addData')
101+
->with([
102+
'title' => 'Magento Luma'
103+
])
104+
->willReturnSelf();
105+
$theme->expects($this->once())
106+
->method('save');
107+
108+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
45109
}
46110

47111
public function testBeforeDispatchWithProductionMode()
48112
{
49113
$this->appState->expects($this->once())->method('getMode')->willReturn('production');
50-
$this->themeRegistration->expects($this->never())->method('register');
51-
$this->logger->expects($this->never())->method('critical');
52-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
53-
$object->beforeDispatch($this->abstractAction, $this->request);
114+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
54115
}
55116

56117
public function testBeforeDispatchWithException()
57118
{
58119
$exception = new LocalizedException(new Phrase('Phrase'));
59120
$this->themeRegistration->expects($this->once())->method('register')->willThrowException($exception);
60121
$this->logger->expects($this->once())->method('critical');
61-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
62-
$object->beforeDispatch($this->abstractAction, $this->request);
122+
123+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
63124
}
64125
}

0 commit comments

Comments
 (0)