/** * Initialize properties of object required for test. * * @return \Magento\Config\Block\System\Config\Form */ protected function _initObjects() { parent::_initObjects(); $this->_configData = $this->_configDataStub; if ($this->_configRootStub) { $this->_configRoot = $this->_configRootStub; } $this->_fieldRenderer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\View\\LayoutInterface')->createBlock('Magento\\Config\\Block\\System\\Config\\Form\\Field'); }
/** * @dataProvider initFieldsDataProvider * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitFields($backendConfigValue, $configValue, $configPath, $inherit, $expectedValue) { // Parameters initialization $fieldsetMock = $this->getMock('Magento\\Framework\\Data\\Form\\Element\\Fieldset', [], [], '', false, false); $groupMock = $this->getMock('Magento\\Config\\Model\\Config\\Structure\\Element\\Group', [], [], '', false, false); $sectionMock = $this->getMock('Magento\\Config\\Model\\Config\\Structure\\Element\\Section', [], [], '', false, false); $fieldPrefix = 'fieldPrefix'; $labelPrefix = 'labelPrefix'; // Field Renderer Mock configuration $fieldRendererMock = $this->getMock('Magento\\Config\\Block\\System\\Config\\Form\\Field', [], [], '', false, false); $this->_fieldFactoryMock->expects($this->once())->method('create')->will($this->returnValue($fieldRendererMock)); $this->_backendConfigMock->expects($this->once())->method('extendConfig')->with('some/config/path', false, ['section1/group1/field1' => 'some_value'])->will($this->returnValue($backendConfigValue)); $this->_coreConfigMock->expects($this->any())->method('getValue')->with($configPath)->will($this->returnValue($configValue)); // Field mock configuration $fieldMock = $this->getMock('Magento\\Config\\Model\\Config\\Structure\\Element\\Field', [], [], '', false, false); $fieldMock->expects($this->any())->method('getPath')->will($this->returnValue('section1/group1/field1')); $fieldMock->expects($this->any())->method('getConfigPath')->will($this->returnValue($configPath)); $fieldMock->expects($this->any())->method('getGroupPath')->will($this->returnValue('some/config/path')); $fieldMock->expects($this->once())->method('getSectionId')->will($this->returnValue('some_section')); $fieldMock->expects($this->once())->method('hasBackendModel')->will($this->returnValue(false)); $fieldMock->expects($this->once())->method('getDependencies')->with($fieldPrefix)->will($this->returnValue([])); $fieldMock->expects($this->any())->method('getType')->will($this->returnValue('field')); $fieldMock->expects($this->once())->method('getLabel')->will($this->returnValue('label')); $fieldMock->expects($this->once())->method('getComment')->will($this->returnValue('comment')); $fieldMock->expects($this->once())->method('getTooltip')->will($this->returnValue('tooltip')); $fieldMock->expects($this->once())->method('getHint')->will($this->returnValue('hint')); $fieldMock->expects($this->once())->method('getFrontendClass')->will($this->returnValue('frontClass')); $fieldMock->expects($this->once())->method('showInDefault')->will($this->returnValue(false)); $fieldMock->expects($this->any())->method('showInWebsite')->will($this->returnValue(false)); $fieldMock->expects($this->once())->method('getData')->will($this->returnValue('fieldData')); $fieldMock->expects($this->any())->method('getRequiredFields')->will($this->returnValue([])); $fieldMock->expects($this->any())->method('getRequiredGroups')->will($this->returnValue([])); $fields = [$fieldMock]; $groupMock->expects($this->once())->method('getChildren')->will($this->returnValue($fields)); $sectionMock->expects($this->once())->method('getId')->will($this->returnValue('section1')); $formFieldMock = $this->getMockForAbstractClass('Magento\\Framework\\Data\\Form\\Element\\AbstractElement', [], '', false, false, true, ['setRenderer']); $params = ['name' => 'groups[group1][fields][fieldPrefixfield1][value]', 'label' => 'label', 'comment' => 'comment', 'tooltip' => 'tooltip', 'hint' => 'hint', 'value' => $expectedValue, 'inherit' => $inherit, 'class' => 'frontClass', 'field_config' => 'fieldData', 'scope' => 'stores', 'scope_id' => 1, 'scope_label' => '[GLOBAL]', 'can_use_default_value' => false, 'can_use_website_value' => false]; $formFieldMock->expects($this->once())->method('setRenderer')->with($fieldRendererMock); $fieldsetMock->expects($this->once())->method('addField')->with('section1_group1_field1', 'field', $params)->will($this->returnValue($formFieldMock)); $fieldMock->expects($this->once())->method('populateInput'); $this->object->initFields($fieldsetMock, $groupMock, $sectionMock, $fieldPrefix, $labelPrefix); }
/** * Intercept core config form block getScopeLabel() method * to add additional override hints. * * @see Magento\Config\Block\System\Config\Form::getScopeLabel() * @param \Magento\Config\Block\System\Config\Form $form * @param callable $getScopeLabel * @param Field $field * @return Phrase */ public function aroundGetScopeLabel(\Magento\Config\Block\System\Config\Form $form, \Closure $getScopeLabel, Field $field) { $currentScopeId = null; switch ($form->getScope()) { case 'websites': $currentScopeId = $form->getWebsiteCode(); break; case 'stores': $currentScopeId = $form->getStoreCode(); break; } $overriddenLevels = $this->helper->getOverriddenLevels($field->getPath(), $form->getScope(), $currentScopeId); /* @var $returnPhrase Phrase */ $labelPhrase = $getScopeLabel($field); if (!empty($overriddenLevels)) { $scopeHintText = $labelPhrase . $this->helper->formatOverriddenScopes($form, $overriddenLevels); // create new phrase, now that constituent strings are translated individually $labelPhrase = new Phrase($scopeHintText, $labelPhrase->getArguments()); } return $labelPhrase; }
/** * Get HTML output for override hint UI * * @param \Magento\Config\Block\System\Config\Form $form * @param array $overridden * @return string */ public function formatOverriddenScopes(\Magento\Config\Block\System\Config\Form $form, array $overridden) { $title = __('This setting is overridden at a more specific scope. Click for details.'); $formatted = '<a class="overridden-hint-list-toggle" title="' . $title . '" href="#"><span>' . $title . '</span></a>' . '<ul class="overridden-hint-list">'; foreach ($overridden as $overriddenScope) { $scope = $overriddenScope['scope']; $scopeId = $overriddenScope['scope_id']; $scopeLabel = $scopeId; $url = '#'; $section = $form->getSectionCode(); switch ($scope) { case 'website': $url = $this->urlBuilder->getUrl('*/*/*', array('section' => $section, 'website' => $scopeId)); $scopeLabel = sprintf('website <a href="%s">%s</a>', $url, $this->storeManager->getWebsite($scopeId)->getName()); break; case 'store': $store = $this->storeManager->getStore($scopeId); $website = $store->getWebsite(); $url = $this->urlBuilder->getUrl('*/*/*', array('section' => $section, 'store' => $store->getId())); $scopeLabel = sprintf('store view <a href="%s">%s</a>', $url, $website->getName() . ' / ' . $store->getName()); break; } $formatted .= "<li class='{$scope}'>Overridden on {$scopeLabel}</li>"; } $formatted .= '</ul>'; return $formatted; }
/** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Data\FormFactory $formFactory * @param \Magento\Config\Model\Config\Factory $configFactory * @param \Magento\Config\Model\Config\Structure $configStructure * @param \Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory * @param \Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory * @param \Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface $groupGenerator * @param array $data */ public function __construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Config\Model\Config\Factory $configFactory, \Magento\Config\Model\Config\Structure $configStructure, \Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory, \Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory, \Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface $groupGenerator, array $data = []) { $this->groupGenerator = $groupGenerator; parent::__construct($context, $registry, $formFactory, $configFactory, $configStructure, $fieldsetFactory, $fieldFactory, $data); }
/** * Form constructor. * * @param \Magento\Backend\Block\Template\Context $context Application Context * @param \Magento\Framework\Registry $registry Magento Registry * @param FormFactory $formFactory Form Factory * @param Factory $configFactory Configuration Factory * @param Structure $configStructure Configuration Structure * @param \Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory Fieldset Factory * @param \Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory Field Factory * @param array $data Object Data */ public function __construct(Context $context, Registry $registry, FormFactory $formFactory, Factory $configFactory, Structure $configStructure, \Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory, \Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory, array $data = []) { parent::__construct($context, $registry, $formFactory, $configFactory, $configStructure, $fieldsetFactory, $fieldFactory, $data); $this->_scopeLabels = [ContainerScopeInterface::SCOPE_DEFAULT => __('[GLOBAL]'), ContainerScopeInterface::SCOPE_CONTAINERS => __('[CONTAINER]'), ContainerScopeInterface::SCOPE_STORE_CONTAINERS => __('[CONTAINER - STORE VIEW]')]; }