Skip to content

Commit 7342aa5

Browse files
author
Joan He
committed
Merge remote-tracking branch 'tango/MAGETWO-98409-2.3.1' into pr
2 parents 6fe70e1 + fc747e2 commit 7342aa5

File tree

4 files changed

+110
-30
lines changed

4 files changed

+110
-30
lines changed

app/code/Magento/Config/Model/Config.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,24 +520,29 @@ public function setDataByPath($path, $value)
520520
if ($path === '') {
521521
throw new \UnexpectedValueException('Path must not be empty');
522522
}
523+
523524
$pathParts = explode('/', $path);
524525
$keyDepth = count($pathParts);
525-
if ($keyDepth !== 3) {
526+
if ($keyDepth < 3) {
526527
throw new \UnexpectedValueException(
527-
"Allowed depth of configuration is 3 (<section>/<group>/<field>). Your configuration depth is "
528-
. $keyDepth . " for path '$path'"
528+
'Minimal depth of configuration is 3. Your configuration depth is ' . $keyDepth
529529
);
530530
}
531+
532+
$section = array_shift($pathParts);
531533
$data = [
532-
'section' => $pathParts[0],
533-
'groups' => [
534-
$pathParts[1] => [
535-
'fields' => [
536-
$pathParts[2] => ['value' => $value],
537-
],
538-
],
534+
'fields' => [
535+
array_pop($pathParts) => ['value' => $value],
539536
],
540537
];
538+
while ($pathParts) {
539+
$data = [
540+
'groups' => [
541+
array_pop($pathParts) => $data,
542+
],
543+
];
544+
}
545+
$data['section'] = $section;
541546
$this->addData($data);
542547
}
543548

app/code/Magento/Config/Test/Unit/Model/ConfigTest.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,59 @@ public function testSaveToCheckScopeDataSet()
330330
$this->model->save();
331331
}
332332

333-
public function testSetDataByPath()
333+
/**
334+
* @param string $path
335+
* @param string $value
336+
* @param string $section
337+
* @param array $groups
338+
* @dataProvider setDataByPathDataProvider
339+
*/
340+
public function testSetDataByPath(string $path, string $value, string $section, array $groups)
334341
{
335-
$value = 'value';
336-
$path = '<section>/<group>/<field>';
337342
$this->model->setDataByPath($path, $value);
338-
$expected = [
339-
'section' => '<section>',
340-
'groups' => [
341-
'<group>' => [
342-
'fields' => [
343-
'<field>' => ['value' => $value],
343+
$this->assertEquals($section, $this->model->getData('section'));
344+
$this->assertEquals($groups, $this->model->getData('groups'));
345+
}
346+
347+
/**
348+
* @return array
349+
*/
350+
public function setDataByPathDataProvider(): array
351+
{
352+
return [
353+
'depth 3' => [
354+
'a/b/c',
355+
'value1',
356+
'a',
357+
[
358+
'b' => [
359+
'fields' => [
360+
'c' => ['value' => 'value1'],
361+
],
362+
],
363+
],
364+
],
365+
'depth 5' => [
366+
'a/b/c/d/e',
367+
'value1',
368+
'a',
369+
[
370+
'b' => [
371+
'groups' => [
372+
'c' => [
373+
'groups' => [
374+
'd' => [
375+
'fields' => [
376+
'e' => ['value' => 'value1'],
377+
],
378+
],
379+
],
380+
],
381+
],
344382
],
345383
],
346384
],
347385
];
348-
$this->assertSame($expected, $this->model->getData());
349386
}
350387

351388
/**
@@ -359,14 +396,13 @@ public function testSetDataByPathEmpty()
359396

360397
/**
361398
* @param string $path
362-
* @param string $expectedException
363-
*
364399
* @dataProvider setDataByPathWrongDepthDataProvider
365400
*/
366-
public function testSetDataByPathWrongDepth($path, $expectedException)
401+
public function testSetDataByPathWrongDepth(string $path)
367402
{
368-
$expectedException = 'Allowed depth of configuration is 3 (<section>/<group>/<field>). ' . $expectedException;
369-
$this->expectException('\UnexpectedValueException');
403+
$currentDepth = count(explode('/', $path));
404+
$expectedException = 'Minimal depth of configuration is 3. Your configuration depth is ' . $currentDepth;
405+
$this->expectException(\UnexpectedValueException::class);
370406
$this->expectExceptionMessage($expectedException);
371407
$value = 'value';
372408
$this->model->setDataByPath($path, $value);
@@ -375,13 +411,11 @@ public function testSetDataByPathWrongDepth($path, $expectedException)
375411
/**
376412
* @return array
377413
*/
378-
public function setDataByPathWrongDepthDataProvider()
414+
public function setDataByPathWrongDepthDataProvider(): array
379415
{
380416
return [
381-
'depth 2' => ['section/group', "Your configuration depth is 2 for path 'section/group'"],
382-
'depth 1' => ['section', "Your configuration depth is 1 for path 'section'"],
383-
'depth 4' => ['section/group/field/sub-field', "Your configuration depth is 4 for path"
384-
. " 'section/group/field/sub-field'", ],
417+
'depth 2' => ['section/group'],
418+
'depth 1' => ['section'],
385419
];
386420
}
387421
}

dev/tests/integration/testsuite/Magento/Config/Console/Command/ConfigSetCommandTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\Config\Console\Command;
88

99
use Magento\Config\Model\Config\Backend\Admin\Custom;
10+
use Magento\Config\Model\Config\Structure\Converter;
11+
use Magento\Config\Model\Config\Structure\Data as StructureData;
1012
use Magento\Directory\Model\Currency;
1113
use Magento\Framework\App\Config\ConfigPathResolver;
1214
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -91,6 +93,8 @@ protected function setUp()
9193
{
9294
Bootstrap::getInstance()->reinitialize();
9395
$this->objectManager = Bootstrap::getObjectManager();
96+
$this->extendSystemStructure();
97+
9498
$this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
9599
$this->reader = $this->objectManager->get(FileReader::class);
96100
$this->filesystem = $this->objectManager->get(Filesystem::class);
@@ -123,6 +127,21 @@ protected function tearDown()
123127
$this->appConfig->reinit();
124128
}
125129

130+
/**
131+
* Add test system structure to main system structure
132+
*
133+
* @return void
134+
*/
135+
private function extendSystemStructure()
136+
{
137+
$document = new \DOMDocument();
138+
$document->load(__DIR__ . '/../../_files/system.xml');
139+
$converter = $this->objectManager->get(Converter::class);
140+
$systemConfig = $converter->convert($document);
141+
$structureData = $this->objectManager->get(StructureData::class);
142+
$structureData->merge($systemConfig);
143+
}
144+
126145
/**
127146
* @return array
128147
*/
@@ -191,6 +210,8 @@ public function runLockDataProvider()
191210
['general/region/display_all', '1'],
192211
['general/region/state_required', 'BR,FR', ScopeInterface::SCOPE_WEBSITE, 'base'],
193212
['admin/security/use_form_key', '0'],
213+
['general/group/subgroup/field', 'default_value'],
214+
['general/group/subgroup/field', 'website_value', ScopeInterface::SCOPE_WEBSITE, 'base'],
194215
];
195216
}
196217

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="general">
11+
<group id="group">
12+
<group id="subgroup">
13+
<field id="field" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
14+
<label>Label</label>
15+
</field>
16+
</group>
17+
</group>
18+
</section>
19+
</system>
20+
</config>

0 commit comments

Comments
 (0)