/** * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $scope = $this->getRequest()->getParam('scope'); $scopeId = (int) $this->getRequest()->getParam('scope_id'); $data = $this->getRequestData(); try { $designConfigData = $this->configFactory->create($scope, $scopeId, $data); $this->designConfigRepository->save($designConfigData); $this->messageManager->addSuccess(__('You saved the configuration.')); $this->dataPersistor->clear('theme_design_config'); $returnToEdit = (bool) $this->getRequest()->getParam('back', false); $resultRedirect->setPath('theme/design_config/'); if ($returnToEdit) { $resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]); } return $resultRedirect; } catch (LocalizedException $e) { $messages = explode("\n", $e->getMessage()); foreach ($messages as $message) { $this->messageManager->addError(__('%1', $message)); } } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()); } $this->dataPersistor->set('theme_design_config', $data); $resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]); return $resultRedirect; }
/** * Load design config from storage * * @param string $scope * @param mixed $scopeId * @return DesignConfigInterface */ public function load($scope, $scopeId) { $designConfig = $this->configFactory->create($scope, $scopeId); $fieldsData = $designConfig->getExtensionAttributes()->getDesignConfigData(); foreach ($fieldsData as &$fieldData) { $value = $this->valueProcessor->process($this->scopeConfig->getValue($fieldData->getPath(), $scope, $scopeId), $fieldData->getPath()); if ($value !== null) { $fieldData->setValue($value); } } return $designConfig; }
public function testLoad() { $scope = 'website'; $scopeId = 1; $this->configFactory->expects($this->once())->method('create')->with($scope, $scopeId)->willReturn($this->designConfig); $this->designConfig->expects($this->once())->method('getExtensionAttributes')->willReturn($this->designConfigExtension); $this->designConfigExtension->expects($this->once())->method('getDesignConfigData')->willReturn([$this->designConfigData]); $this->designConfigData->expects($this->atLeastOnce())->method('getPath')->willReturn('path'); $this->scopeConfig->expects($this->once())->method('getValue')->with('path', $scope, $scopeId)->willReturn('value'); $this->valueProcessor->expects($this->once())->method('process')->with('value', 'path')->willReturnArgument(0); $this->designConfigData->expects($this->once())->method('setValue')->with('value'); $this->assertSame($this->designConfig, $this->model->load($scope, $scopeId)); }
public function testSaveWithException() { $scope = 'sadfa'; $scopeId = 0; $this->redirectFactory->expects($this->once())->method('create')->willReturn($this->redirect); $this->request->expects($this->exactly(2))->method('getParam')->withConsecutive(['scope'], ['scope_id'])->willReturnOnConsecutiveCalls($scope, $scopeId); $this->request->expects($this->once())->method('getParams')->willReturn(['header_default_title' => 'Default title']); $this->request->expects($this->once())->method('getFiles')->willReturn($this->fileParams); $this->fileParams->expects($this->once())->method('toArray')->willReturn(['header_logo' => ['tmp_name' => '', 'error' => 4]]); $this->configFactory->expects($this->once())->method('create')->with($scope, $scopeId, ['header_default_title' => 'Default title'])->willReturn($this->designConfig); $exception = new \Exception(__('Exception message')); $this->designConfigRepository->expects($this->once())->method('save')->with($this->designConfig)->willThrowException($exception); $this->messageManager->expects($this->once())->method('addException')->with($exception, 'Something went wrong while saving this configuration: Exception message'); $this->dataPersistor->expects($this->once())->method('set')->with('theme_design_config', ['header_default_title' => 'Default title']); $this->redirect->expects($this->once())->method('setPath')->with('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]); $this->assertSame($this->redirect, $this->controller->execute()); }
public function testCreateInSingleStoreMode() { $scope = 'default'; $scopeId = 0; $data = ['header_default_title' => 'value']; $metadata = ['header_default_title' => ['path' => 'design/header/default_title', 'fieldset' => 'head'], 'head_default_description' => ['path' => 'design/head/default_description', 'fieldset' => 'head']]; $this->scopeValidator->expects($this->once())->method('isValidScope')->with($scope, $scopeId)->willReturn(true); $this->storeManager->expects($this->once())->method('isSingleStoreMode')->willReturn(true); $this->storeManager->expects($this->once())->method('getWebsites')->willReturn([$this->website]); $this->website->expects($this->once())->method('getId')->willReturn(1); $this->designConfigFactory->expects($this->once())->method('create')->willReturn($this->designConfig); $this->designConfig->expects($this->once())->method('setScope')->willReturn('websites'); $this->designConfig->expects($this->once())->method('setScopeId')->willReturn(1); $this->metadataProvider->expects($this->once())->method('get')->willReturn($metadata); $this->designConfigDataFactory->expects($this->exactly(2))->method('create')->willReturn($this->designConfigData); $this->designConfigData->expects($this->exactly(2))->method('setPath')->withConsecutive(['design/header/default_title'], ['design/head/default_description']); $this->designConfigData->expects($this->exactly(2))->method('setFieldConfig')->withConsecutive([['path' => 'design/header/default_title', 'fieldset' => 'head', 'field' => 'header_default_title']], [['path' => 'design/head/default_description', 'fieldset' => 'head', 'field' => 'head_default_description']]); $this->designConfigData->expects($this->once())->method('setValue')->with('value'); $this->configExtensionFactory->expects($this->once())->method('create')->willReturn($this->designConfigExtension); $this->designConfigExtension->expects($this->once())->method('setDesignConfigData')->with([$this->designConfigData, $this->designConfigData]); $this->designConfig->expects($this->once())->method('setExtensionAttributes')->with($this->designConfigExtension); $this->assertSame($this->designConfig, $this->factory->create($scope, $scopeId, $data)); }