/** * Test loadData * * @return void */ public function testLoadData() { $this->scopeTreeMock->expects($this->any())->method('get')->willReturn(['scope' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'scope_id' => null, 'scopes' => [['scope' => ScopeInterface::SCOPE_WEBSITE, 'scope_id' => 1, 'scopes' => [['scope' => ScopeInterface::SCOPE_GROUP, 'scope_id' => 1, 'scopes' => [['scope' => ScopeInterface::SCOPE_STORE, 'scope_id' => 1, 'scopes' => []]]]]]]]); $this->metadataProviderMock->expects($this->any())->method('get')->willReturn(['first_field' => ['path' => 'first/field/path', 'use_in_grid' => 0], 'second_field' => ['path' => 'second/field/path', 'use_in_grid' => true], 'third_field' => ['path' => 'third/field/path']]); $this->appConfigMock->expects($this->any())->method('getValue')->willReturnMap([['second/field/path', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, 'DefaultValue'], ['second/field/path', ScopeInterface::SCOPE_WEBSITE, 1, 'WebsiteValue'], ['second/field/path', ScopeInterface::SCOPE_STORE, 1, 'WebsiteValue']]); $this->valueProcessor->expects($this->atLeastOnce())->method('process')->withConsecutive(['DefaultValue', 'default', null, ['path' => 'second/field/path', 'use_in_grid' => 1]], ['WebsiteValue', 'website', 1, ['path' => 'second/field/path', 'use_in_grid' => 1]], ['WebsiteValue', 'store', 1, ['path' => 'second/field/path', 'use_in_grid' => 1]])->willReturnOnConsecutiveCalls('DefaultValue', 'WebsiteValue', 'WebsiteValue'); $expectedResult = [new \Magento\Framework\DataObject(['store_website_id' => null, 'store_group_id' => null, 'store_id' => null, 'second_field' => 'DefaultValue']), new \Magento\Framework\DataObject(['store_website_id' => 1, 'store_group_id' => null, 'store_id' => null, 'second_field' => 'WebsiteValue']), new \Magento\Framework\DataObject(['store_website_id' => 1, 'store_group_id' => 1, 'store_id' => 1, 'second_field' => 'WebsiteValue'])]; $this->assertEquals($expectedResult, $this->collection->getItems()); }
/** * Retrieve fields metadata * * @param string $scope * @param int $scopeId * @return array */ protected function getMetadataValues($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = null) { $result = []; foreach ($this->metadataProvider->get() as $itemName => $itemData) { if (isset($itemData['use_in_grid']) && (bool) $itemData['use_in_grid']) { $result[$itemName] = $this->valueProcessor->process($this->appConfig->getValue($itemData['path'], $scope, $scopeId), $itemData['path']); } } return $result; }
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)); }
/** * Create Design Configuration for scope * * @param mixed $scope * @param int $scopeId * @param array $data * @return DesignConfigInterface * @throws LocalizedException */ public function create($scope, $scopeId, array $data = []) { if (!$this->scopeValidator->isValidScope($scope, $scopeId)) { throw new LocalizedException(__('Invalid scope or scope id')); } $designConfigData = $this->getDesignConfigData($scope, $scopeId); $configData = []; foreach ($this->metadataProvider->get() as $name => $metadata) { $metadata['field'] = $name; /** @var DesignConfigDataInterface $configDataObject */ $configDataObject = $this->designConfigDataFactory->create(); $configDataObject->setPath($metadata['path']); $configDataObject->setFieldConfig($metadata); if (isset($data[$name]) || isset($metadata['default'])) { $configDataObject->setValue(isset($data[$name]) ? $data[$name] : $metadata['default']); } $configData[] = $configDataObject; } /** @var DesignConfigExtension $designConfigExtension */ $designConfigExtension = $this->configExtensionFactory->create(); $designConfigExtension->setDesignConfigData($configData); $designConfigData->setExtensionAttributes($designConfigExtension); return $designConfigData; }
/** * Add additional fields to fieldset * * @param array $data * @return array */ public function addDynamicData(array $data) { $additionalFields = $this->convert($this->metadataProvider->get()); $data['fields'] = $this->merge($data['fields'], $additionalFields); return $data; }