diff --git a/app/code/Magento/Theme/Model/Theme/Plugin/Registration.php b/app/code/Magento/Theme/Model/Theme/Plugin/Registration.php index 487670e88a0e6a44ddf9c385d1c91487966a03f7..e17581cb283d90db59983b7c80412124ce6de245 100644 --- a/app/code/Magento/Theme/Model/Theme/Plugin/Registration.php +++ b/app/code/Magento/Theme/Model/Theme/Plugin/Registration.php @@ -86,23 +86,25 @@ class Registration */ protected function updateThemeData() { - $themesData = $this->themeCollection->loadData(); - /** @var \Magento\Theme\Model\Theme $themeData */ - foreach ($themesData as $themeData) { - if ($themeData->getParentTheme()) { - $parentTheme = $this->themeLoader->getThemeByFullPath( - $themeData->getParentTheme()->getFullPath() + $themesFromConfig = $this->themeCollection->loadData(); + /** @var \Magento\Theme\Model\Theme $themeFromConfig */ + foreach ($themesFromConfig as $themeFromConfig) { + /** @var \Magento\Theme\Model\Theme $themeFromDb */ + $themeFromDb = $this->themeLoader->getThemeByFullPath( + $themeFromConfig->getArea() + . Theme::THEME_PATH_SEPARATOR + . $themeFromConfig->getThemePath() + ); + + if ($themeFromConfig->getParentTheme()) { + $parentThemeFromDb = $this->themeLoader->getThemeByFullPath( + $themeFromConfig->getParentTheme()->getFullPath() ); - $themeData->setParentId($parentTheme->getId()); + $themeFromDb->setParentId($parentThemeFromDb->getId()); } - /** @var \Magento\Theme\Model\Theme $theme */ - $theme = $this->themeLoader->getThemeByFullPath( - $themeData->getArea() - . Theme::THEME_PATH_SEPARATOR - . $themeData->getThemePath() - ); - $theme->addData($themeData->toArray())->save(); + $themeFromDb->setThemeTitle($themeFromConfig->getThemeTitle()); + $themeFromDb->save(); } } } diff --git a/app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php b/app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php index 4ec24f3eea6a614374030630734222e51172e97c..53a569f01b72a29c1b989162e41d0e18b228b628 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Theme/Plugin/RegistrationTest.php @@ -53,61 +53,109 @@ class RegistrationTest extends \PHPUnit_Framework_TestCase ); } - public function testBeforeDispatch() - { - $theme = $this->getMock( - 'Magento\Theme\Model\Theme', - [ - 'setParentId', + /** + * @param bool $hasParentTheme + * @dataProvider dataProviderBeforeDispatch + * @SuppressWarnings(PHPMD.NPathComplexity) + */ + public function testBeforeDispatch( + $hasParentTheme + ) { + $themeId = 1; + $themeTitle = 'Theme title'; + + $themeFromConfigMock = $this->getMockBuilder('Magento\Theme\Model\Theme') + ->disableOriginalConstructor() + ->setMethods([ 'getArea', 'getThemePath', 'getParentTheme', - 'getId', - 'getFullPath', - 'toArray', - 'addData', + 'getThemeTitle', + ]) + ->getMock(); + + $themeFromDbMock = $this->getMockBuilder('Magento\Theme\Model\Theme') + ->disableOriginalConstructor() + ->setMethods([ + 'setParentId', + 'setThemeTitle', 'save', - ], - [], - '', - false - ); - $this->appState->expects($this->once())->method('getMode')->willReturn('default'); - $this->themeRegistration->expects($this->once())->method('register'); - $this->themeCollection->expects($this->once())->method('loadData')->willReturn([$theme]); - $theme->expects($this->once())->method('getArea')->willReturn('frontend'); - $theme->expects($this->once())->method('getThemePath')->willReturn('Magento/luma'); - $theme->expects($this->exactly(2))->method('getParentTheme')->willReturnSelf(); - $theme->expects($this->once())->method('getId')->willReturn(1); - $theme->expects($this->once())->method('getFullPath')->willReturn('frontend/Magento/blank'); - $theme->expects($this->once())->method('setParentId')->with(1); - $this->themeLoader->expects($this->exactly(2)) + ]) + ->getMock(); + + $parentThemeFromDbMock = $this->getMockBuilder('Magento\Theme\Model\Theme') + ->disableOriginalConstructor() + ->getMock(); + + $parentThemeFromConfigMock = $this->getMockBuilder('Magento\Theme\Model\Theme') + ->disableOriginalConstructor() + ->getMock(); + + $this->appState->expects($this->once()) + ->method('getMode') + ->willReturn('default'); + + $this->themeRegistration->expects($this->once()) + ->method('register'); + + $this->themeCollection->expects($this->once()) + ->method('loadData') + ->willReturn([$themeFromConfigMock]); + + $this->themeLoader->expects($hasParentTheme ? $this->exactly(2) : $this->once()) ->method('getThemeByFullPath') - ->withConsecutive( - ['frontend/Magento/blank'], - ['frontend/Magento/luma'] - ) - ->will($this->onConsecutiveCalls( - $theme, - $theme - )); - $theme->expects($this->once()) - ->method('toArray') - ->willReturn([ - 'title' => 'Magento Luma' + ->willReturnMap([ + ['frontend/Magento/blank', $parentThemeFromDbMock], + ['frontend/Magento/luma', $themeFromDbMock], ]); - $theme->expects($this->once()) - ->method('addData') - ->with([ - 'title' => 'Magento Luma' - ]) + + $themeFromConfigMock->expects($this->once()) + ->method('getArea') + ->willReturn('frontend'); + $themeFromConfigMock->expects($this->once()) + ->method('getThemePath') + ->willReturn('Magento/luma'); + $themeFromConfigMock->expects($hasParentTheme ? $this->exactly(2) : $this->once()) + ->method('getParentTheme') + ->willReturn($hasParentTheme ? $parentThemeFromConfigMock : null); + $themeFromConfigMock->expects($this->once()) + ->method('getThemeTitle') + ->willReturn($themeTitle); + + $parentThemeFromDbMock->expects($hasParentTheme ? $this->once() : $this->never()) + ->method('getId') + ->willReturn($themeId); + + $parentThemeFromConfigMock->expects($hasParentTheme ? $this->once() : $this->never()) + ->method('getFullPath') + ->willReturn('frontend/Magento/blank'); + + $themeFromDbMock->expects($hasParentTheme ? $this->once() : $this->never()) + ->method('setParentId') + ->with($themeId) + ->willReturnSelf(); + $themeFromDbMock->expects($this->once()) + ->method('setThemeTitle') + ->with($themeTitle) + ->willReturnSelf(); + $themeFromDbMock->expects($this->once()) + ->method('save') ->willReturnSelf(); - $theme->expects($this->once()) - ->method('save'); $this->plugin->beforeDispatch($this->abstractAction, $this->request); } + /** + * @return array + */ + public function dataProviderBeforeDispatch() + { + return [ + [true], + [false], + ]; + } + public function testBeforeDispatchWithProductionMode() { $this->appState->expects($this->once())->method('getMode')->willReturn('production');