Skip to content

Commit 7dc9e60

Browse files
author
Oleksandr Karpenko
committed
MAGETWO-43188: Unable to rename theme by editing theme.xml file
1 parent 071413e commit 7dc9e60

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
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

1518
class Registration
1619
{
1720
/** @var ThemeRegistration */
1821
protected $themeRegistration;
1922

23+
/** @var ThemeCollection */
24+
protected $themeCollection;
25+
26+
/** @var ThemeLoader */
27+
protected $themeLoader;
28+
2029
/** @var LoggerInterface */
2130
protected $logger;
2231

@@ -25,21 +34,27 @@ class Registration
2534

2635
/**
2736
* @param ThemeRegistration $themeRegistration
37+
* @param ThemeCollection $themeCollection
38+
* @param ThemeLoader $themeLoader
2839
* @param LoggerInterface $logger
2940
* @param AppState $appState
3041
*/
3142
public function __construct(
3243
ThemeRegistration $themeRegistration,
44+
ThemeCollection $themeCollection,
45+
ThemeLoader $themeLoader,
3346
LoggerInterface $logger,
3447
AppState $appState
3548
) {
3649
$this->themeRegistration = $themeRegistration;
50+
$this->themeCollection = $themeCollection;
51+
$this->themeLoader = $themeLoader;
3752
$this->logger = $logger;
3853
$this->appState = $appState;
3954
}
4055

4156
/**
42-
* Add new theme from filesystem
57+
* Add new theme from filesystem and update exists
4358
*
4459
* @param AbstractAction $subject
4560
* @param RequestInterface $request
@@ -54,9 +69,25 @@ public function beforeDispatch(
5469
try {
5570
if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
5671
$this->themeRegistration->register();
72+
$this->updateThemeData();
5773
}
5874
} catch (LocalizedException $e) {
5975
$this->logger->critical($e);
6076
}
6177
}
78+
79+
protected function updateThemeData()
80+
{
81+
$themesData = $this->themeCollection->loadData();
82+
/** @var \Magento\Theme\Model\Theme $themeData */
83+
foreach ($themesData as $themeData) {
84+
/** @var \Magento\Theme\Model\Theme $theme */
85+
$theme = $this->themeLoader->getThemeByFullPath(
86+
$themeData->getArea()
87+
. Theme::THEME_PATH_SEPARATOR
88+
. $themeData->getThemePath()
89+
);
90+
$theme->addData($themeData->toArray())->save();
91+
}
92+
}
6293
}

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,74 @@ 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('Magento\Theme\Model\Theme', [], [], '', false);
4059
$this->appState->expects($this->once())->method('getMode')->willReturn('default');
4160
$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);
61+
$this->themeCollection->expects($this->once())->method('loadData')->willReturn([$theme]);
62+
$theme->expects($this->once())->method('getArea')->willReturn('frontend');
63+
$theme->expects($this->once())->method('getThemePath')->willReturn('Magento/luma');
64+
$this->themeLoader->expects($this->once())
65+
->method('getThemeByFullPath')
66+
->with('frontend/Magento/luma')
67+
->willReturn($theme);
68+
$theme->expects($this->once())
69+
->method('toArray')
70+
->willReturn([
71+
'title' => 'Magento Luma'
72+
]);
73+
$theme->expects($this->once())
74+
->method('addData')
75+
->with([
76+
'title' => 'Magento Luma'
77+
])
78+
->willReturnSelf();
79+
$theme->expects($this->once())
80+
->method('save');
81+
82+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
4583
}
4684

4785
public function testBeforeDispatchWithProductionMode()
4886
{
4987
$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);
88+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
5489
}
5590

5691
public function testBeforeDispatchWithException()
5792
{
5893
$exception = new LocalizedException(new Phrase('Phrase'));
5994
$this->themeRegistration->expects($this->once())->method('register')->willThrowException($exception);
6095
$this->logger->expects($this->once())->method('critical');
61-
$object = new Registration($this->themeRegistration, $this->logger, $this->appState);
62-
$object->beforeDispatch($this->abstractAction, $this->request);
96+
97+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
6398
}
6499
}

0 commit comments

Comments
 (0)