/** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Config\Model\Config\Structure $configStructure * @param \Magento\Backend\Helper\Data $backendHelper * @param array $data */ public function __construct(\Magento\Backend\Block\Template\Context $context, \Magento\Config\Model\Config\Structure $configStructure, \Magento\Backend\Helper\Data $backendHelper, array $data = []) { $this->_backendHelper = $backendHelper; parent::__construct($context, $data); $this->_tabs = $configStructure->getTabs(); $this->setId('system_config_tabs'); $this->setTitle(__('Configuration')); $this->_currentSectionId = $this->getRequest()->getParam('section'); $this->_backendHelper->addPageHelpUrl($this->getRequest()->getParam('section') . '/'); }
/** * Check if specified section allowed in ACL * * Will forward to deniedAction(), if not allowed. * * @param string $sectionId * @throws \Exception * @return bool * @throws NotFoundException */ public function isSectionAllowed($sectionId) { try { if (false == $this->_configStructure->getElement($sectionId)->isAllowed()) { throw new \Exception(''); } return true; } catch (\Zend_Acl_Exception $e) { throw new NotFoundException(__('Page not found.')); } catch (\Exception $e) { return false; } }
protected function setUp() { $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->scopeConfigMock = $this->getMock('Magento\\Framework\\App\\Config\\ScopeConfigInterface'); $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn(['test' => 1]); $this->structureMock = $this->getMock('Magento\\Config\\Model\\Config\\Structure', [], [], '', false); $this->structureMock->expects($this->any())->method('getFieldPathsByAttribute')->willReturn(['path' => 'test']); $this->resourceModelMock = $this->getMock('Magento\\Email\\Model\\Resource\\Template', [], [], '', false); $this->resourceModelMock->expects($this->any())->method('getSystemConfigByPathsAndTemplateId')->willReturn(['test_config' => 2015]); $objectManagerMock = $this->getMock('Magento\\Framework\\ObjectManagerInterface'); $objectManagerMock->expects($this->any())->method('get')->with('Magento\\Email\\Model\\Resource\\Template')->will($this->returnValue($this->resourceModelMock)); \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); $this->model = $helper->getObject('Magento\\Email\\Model\\BackendTemplate', ['scopeConfig' => $this->scopeConfigMock, 'structure' => $this->structureMock]); }
/** * Retrieve list of options * * @return array */ public function toOptionArray() { if ($this->_options === null) { $this->_options = []; /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement('trans_email'); /** @var $group \Magento\Config\Model\Config\Structure\Element\Group */ foreach ($section->getChildren() as $group) { $this->_options[] = ['value' => preg_replace('#^ident_(.*)$#', '$1', $group->getId()), 'label' => $group->getLabel()]; } ksort($this->_options); } return $this->_options; }
/** * Prepare layout object * * @return \Magento\Framework\View\Element\AbstractBlock */ protected function _prepareLayout() { /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement($this->getRequest()->getParam('section')); $this->_formBlockName = $section->getFrontendModel(); if (empty($this->_formBlockName)) { $this->_formBlockName = self::DEFAULT_SECTION_BLOCK; } $this->setTitle($section->getLabel()); $this->setHeaderCss($section->getHeaderCss()); $this->getToolbar()->addChild('save_button', 'Magento\\Backend\\Block\\Widget\\Button', ['id' => 'save', 'label' => __('Save Config'), 'class' => 'save primary', 'data_attribute' => ['mage-init' => ['button' => ['event' => 'save', 'target' => '#config-edit-form']]]]); $block = $this->getLayout()->createBlock($this->_formBlockName); $this->setChild('form', $block); return parent::_prepareLayout(); }
/** * {@inheritdoc} */ public function getElementByPathParts(array $pathParts) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getElementByPathParts'); if (!$pluginInfo) { return parent::getElementByPathParts($pathParts); } else { return $this->___callPlugins('getElementByPathParts', func_get_args(), $pluginInfo); } }
/** * {@inheritdoc} */ public function getFieldPathsByAttribute($attributeName, $attributeValue) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getFieldPathsByAttribute'); if (!$pluginInfo) { return parent::getFieldPathsByAttribute($attributeName, $attributeValue); } else { return $this->___callPlugins('getFieldPathsByAttribute', func_get_args(), $pluginInfo); } }
private function setUpChangeEncryptionKey() { $paths = ['path1', 'path2']; $table = ['item1', 'item2']; $values = ['key1' => 'value1', 'key2' => 'value2']; $this->writerMock->expects($this->once())->method('checkIfWritable')->willReturn(true); $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->adapterMock); $this->adapterMock->expects($this->once())->method('beginTransaction'); $this->structureMock->expects($this->once())->method('getFieldPathsByAttribute')->willReturn($paths); $this->resourceMock->expects($this->atLeastOnce())->method('getTableName')->willReturn($table); $this->adapterMock->expects($this->any())->method('select')->willReturn($this->selectMock); $this->adapterMock->expects($this->any())->method('fetchPairs')->willReturn($values); $this->selectMock->expects($this->any())->method('from')->willReturnSelf(); $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturnSelf(); $this->selectMock->expects($this->any())->method('update')->willReturnSelf(); $this->writerMock->expects($this->once())->method('saveConfig'); $this->adapterMock->expects($this->once())->method('getTransactionLevel')->willReturn(1); }
/** * Convert xml config paths to decorated names * * @param array $paths * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _getSystemConfigPathsParts($paths) { $result = $urlParams = $prefixParts = []; $scopeLabel = __('Default Config'); if ($paths) { /** @var $menu \Magento\Backend\Model\Menu */ $menu = $this->_menuConfig->getMenu(); $item = $menu->get('Magento_Backend::stores'); // create prefix path parts $prefixParts[] = ['title' => __($item->getTitle())]; $item = $menu->get('Magento_Config::system_config'); $prefixParts[] = ['title' => __($item->getTitle()), 'url' => $this->getUrl('adminhtml/system_config/')]; $pathParts = $prefixParts; foreach ($paths as $pathData) { $pathDataParts = explode('/', $pathData['path']); $sectionName = array_shift($pathDataParts); $urlParams = ['section' => $sectionName]; if (isset($pathData['scope']) && isset($pathData['scope_id'])) { switch ($pathData['scope']) { case 'stores': $store = $this->_storeManager->getStore($pathData['scope_id']); if ($store) { $urlParams['website'] = $store->getWebsite()->getCode(); $urlParams['store'] = $store->getCode(); $scopeLabel = $store->getWebsite()->getName() . '/' . $store->getName(); } break; case 'websites': $website = $this->_storeManager->getWebsite($pathData['scope_id']); if ($website) { $urlParams['website'] = $website->getCode(); $scopeLabel = $website->getName(); } break; default: break; } } $pathParts[] = ['title' => $this->_configStructure->getElement($sectionName)->getLabel(), 'url' => $this->getUrl('adminhtml/system_config/edit', $urlParams)]; $elementPathParts = [$sectionName]; while (count($pathDataParts) != 1) { $elementPathParts[] = array_shift($pathDataParts); $pathParts[] = ['title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel()]; } $elementPathParts[] = array_shift($pathDataParts); $pathParts[] = ['title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel(), 'scope' => $scopeLabel]; $result[] = $pathParts; $pathParts = $prefixParts; } } return $result; }
/** * Sanitize config state data * * @param array $configState * @return array * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function sanitizeConfigState($configState) { $sectionList = $this->_configStructure->getSectionList(); $sanitizedConfigState = $configState; foreach ($configState as $sectionId => $value) { if (array_key_exists($sectionId, $sectionList)) { $sanitizedConfigState[$sectionId] = (bool) $sanitizedConfigState[$sectionId] ? '1' : '0'; } else { unset($sanitizedConfigState[$sectionId]); } } return $sanitizedConfigState; }
/** * Initialize form * * @return $this */ public function initForm() { $this->_initObjects(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement($this->getSectionCode()); if ($section && $section->isVisible($this->getWebsiteCode(), $this->getStoreCode())) { foreach ($section->getChildren() as $group) { $this->_initGroup($group, $section, $form); } } $this->setForm($form); return $this; }
/** * Collect all system config paths where current template is currently used * * @return array */ public function getSystemConfigPathsWhereCurrentlyUsed() { $templateId = $this->getId(); if (!$templateId) { return []; } $templatePaths = $this->structure->getFieldPathsByAttribute('source_model', 'Magento\\Config\\Model\\Config\\Source\\Email\\Template'); if (!count($templatePaths)) { return []; } $configData = $this->_getResource()->getSystemConfigByPathsAndTemplateId($templatePaths, $templateId); foreach ($templatePaths as $path) { if ($this->scopeConfig->getValue($path, ScopeConfigInterface::SCOPE_TYPE_DEFAULT) == $templateId) { foreach ($configData as $data) { if ($data['path'] == $path) { continue 2; // don't add final fallback value if it was found in stored config } } $configData[] = ['scope' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'path' => $path]; } } return $configData; }
/** * Collect all system config paths where current template is currently used * * @return array */ public function getSystemConfigPathsWhereUsedCurrently() { $templateId = $this->getId(); if (!$templateId) { return []; } $templatePaths = $this->_structure->getFieldPathsByAttribute('source_model', 'Magento\\Config\\Model\\Config\\Source\\Email\\Template'); if (!count($templatePaths)) { return []; } $configData = $this->_getResource()->getSystemConfigByPathsAndTemplateId($templatePaths, $templateId); if (!$configData) { return []; } return $configData; }
/** * Iterate over all config tabs, extract sections and its subsections * @param string $itemsSeparator * @param string $itemPrefix * @return array */ public function getConfigSectionsArray($itemsSeparator = ' ', $itemPrefix = '') { $sections = array(); foreach ($this->_configStructure->getTabs() as $tab) { /** @var $tab \Magento\Config\Model\Config\Structure\Element\Tab */ foreach ($tab->getChildren() as $section) { /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */ // We need the label & url again for the sub sections $sectionLabel = $itemPrefix . $tab->getLabel() . $itemsSeparator . $section->getLabel(); $sectionUrl = $this->_url->getUrl('adminhtml/system_config/edit', array('section' => $section->getId())); // First add global section to the launcher items... $sections[] = ['label' => $sectionLabel, 'value' => $sectionUrl]; foreach ($section->getChildren() as $subSection) { /** @var $subSection \Magento\Config\Model\Config\Structure\Element\Section */ // ...then add all sub sections $sections[] = ['label' => $sectionLabel . $itemsSeparator . $subSection->getLabel(), 'value' => $sectionUrl . '#' . $section->getId() . '_' . $subSection->getId() . '-link']; } } } return $sections; }
public function testSaveToCheckScopeDataSet() { $transactionMock = $this->getMock('Magento\\Framework\\DB\\Transaction', [], [], '', false); $this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock)); $this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([])); $this->_eventManagerMock->expects($this->at(0))->method('dispatch')->with($this->equalTo('admin_system_config_changed_section_'), $this->arrayHasKey('website')); $this->_eventManagerMock->expects($this->at(0))->method('dispatch')->with($this->equalTo('admin_system_config_changed_section_'), $this->arrayHasKey('store')); $group = $this->getMock('Magento\\Config\\Model\\Config\\Structure\\Element\\Group', [], [], '', false); $field = $this->getMock('Magento\\Config\\Model\\Config\\Structure\\Element\\Field', [], [], '', false); $this->_configStructure->expects($this->at(0))->method('getElement')->with('/1')->will($this->returnValue($group)); $this->_configStructure->expects($this->at(1))->method('getElement')->with('/1/key')->will($this->returnValue($field)); $website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $website->expects($this->any())->method('getCode')->will($this->returnValue('website_code')); $this->_storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($website)); $this->_storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$website])); $this->_storeManager->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue(true)); $this->_model->setWebsite('website'); $this->_model->setGroups(['1' => ['fields' => ['key' => ['data']]]]); $backendModel = $this->getMock('Magento\\Framework\\App\\Config\\Value', ['setPath', 'addData', '__sleep', '__wakeup'], [], '', false); $backendModel->expects($this->once())->method('addData')->with(['field' => 'key', 'groups' => [1 => ['fields' => ['key' => ['data']]]], 'group_id' => null, 'scope' => 'websites', 'scope_id' => 0, 'scope_code' => 'website_code', 'field_config' => null, 'fieldset_data' => ['key' => null]]); $backendModel->expects($this->once())->method('setPath')->with('/key')->will($this->returnValue($backendModel)); $this->_dataFactoryMock->expects($this->any())->method('create')->will($this->returnValue($backendModel)); $this->_model->save(); }
/** * @param $attributeName * @param $attributeValue * @param $paths * @dataProvider getFieldPathsByAttributeDataProvider */ public function testGetFieldPathsByAttribute($attributeName, $attributeValue, $paths) { $this->assertEquals($paths, $this->_model->getFieldPathsByAttribute($attributeName, $attributeValue)); }
/** * * @param \Magento\Framework\App\Action\Context $context * @param \ADM\QuickDevBar\Helper\Data $qdbHelper * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory * @param \Magento\Framework\View\LayoutFactory $layoutFactory * @param \Magento\Config\Model\Config\Structure $structure */ public function __construct(\Magento\Framework\App\Action\Context $context, \ADM\QuickDevBar\Helper\Data $qdbHelper, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Framework\View\LayoutFactory $layoutFactory, \Magento\Config\Model\Config\Structure $configStructure) { parent::__construct($context, $qdbHelper, $resultRawFactory, $layoutFactory); //$this->_configStructure = $configStructure; $this->_tabs = $configStructure->getTabs(); }
/** * Check is allow modify system configuration * * @return bool */ protected function _isAllowed() { $sectionId = $this->_request->getParam('section'); return $this->_authorization->isAllowed('Magento_Config::config') || $this->_configStructure->getElement($sectionId)->isAllowed(); }
/** * Process group data * * @param string $groupId * @param array $groupData * @param array $groups * @param string $sectionPath * @param array &$extraOldGroups * @param array &$oldConfig * @param \Magento\Framework\DB\Transaction $saveTransaction * @param \Magento\Framework\DB\Transaction $deleteTransaction * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _processGroup($groupId, array $groupData, array $groups, $sectionPath, array &$extraOldGroups, array &$oldConfig, \Magento\Framework\DB\Transaction $saveTransaction, \Magento\Framework\DB\Transaction $deleteTransaction) { $groupPath = $sectionPath . '/' . $groupId; $scope = $this->getScope(); $scopeId = $this->getScopeId(); $scopeCode = $this->getScopeCode(); /** * * Map field names if they were cloned */ /** @var $group \Magento\Config\Model\Config\Structure\Element\Group */ $group = $this->_configStructure->getElement($groupPath); // set value for group field entry by fieldname // use extra memory $fieldsetData = []; if (isset($groupData['fields'])) { if ($group->shouldCloneFields()) { $cloneModel = $group->getCloneModel(); $mappedFields = []; /** @var $field \Magento\Config\Model\Config\Structure\Element\Field */ foreach ($group->getChildren() as $field) { foreach ($cloneModel->getPrefixes() as $prefix) { $mappedFields[$prefix['field'] . $field->getId()] = $field->getId(); } } } foreach ($groupData['fields'] as $fieldId => $fieldData) { $fieldsetData[$fieldId] = is_array($fieldData) && isset($fieldData['value']) ? $fieldData['value'] : null; } foreach ($groupData['fields'] as $fieldId => $fieldData) { $originalFieldId = $fieldId; if ($group->shouldCloneFields() && isset($mappedFields[$fieldId])) { $originalFieldId = $mappedFields[$fieldId]; } /** @var $field \Magento\Config\Model\Config\Structure\Element\Field */ $field = $this->_configStructure->getElement($groupPath . '/' . $originalFieldId); /** @var \Magento\Framework\App\Config\ValueInterface $backendModel */ $backendModel = $field->hasBackendModel() ? $field->getBackendModel() : $this->_configValueFactory->create(); $data = ['field' => $fieldId, 'groups' => $groups, 'group_id' => $group->getId(), 'scope' => $scope, 'scope_id' => $scopeId, 'scope_code' => $scopeCode, 'field_config' => $field->getData(), 'fieldset_data' => $fieldsetData]; $backendModel->addData($data); $this->_checkSingleStoreMode($field, $backendModel); if (false == isset($fieldData['value'])) { $fieldData['value'] = null; } $path = $field->getGroupPath() . '/' . $fieldId; /** * Look for custom defined field path */ if ($field && $field->getConfigPath()) { $configPath = $field->getConfigPath(); if (!empty($configPath) && strrpos($configPath, '/') > 0) { // Extend old data with specified section group $configGroupPath = substr($configPath, 0, strrpos($configPath, '/')); if (!isset($extraOldGroups[$configGroupPath])) { $oldConfig = $this->extendConfig($configGroupPath, true, $oldConfig); $extraOldGroups[$configGroupPath] = true; } $path = $configPath; } } $inherit = !empty($fieldData['inherit']); $backendModel->setPath($path)->setValue($fieldData['value']); if (isset($oldConfig[$path])) { $backendModel->setConfigId($oldConfig[$path]['config_id']); /** * Delete config data if inherit */ if (!$inherit) { $saveTransaction->addObject($backendModel); } else { $deleteTransaction->addObject($backendModel); } } elseif (!$inherit) { $backendModel->unsConfigId(); $saveTransaction->addObject($backendModel); } } } if (isset($groupData['groups'])) { foreach ($groupData['groups'] as $subGroupId => $subGroupData) { $this->_processGroup($subGroupId, $subGroupData, $groups, $groupPath, $extraOldGroups, $oldConfig, $saveTransaction, $deleteTransaction); } } }
/** * Check is allow modify system configuration * * @SuppressWarnings(PHPMD.CamelCaseMethodName) * * @return bool */ protected function _isAllowed() { $sectionId = $this->_request->getParam('section'); return $this->configStructure->getElement($sectionId)->isAllowed() || $this->_authorization->isAllowed('Smile_ElasticsuiteCore::manage_relevance'); }