Skip to content

Commit a0f5f96

Browse files
committed
MAGETWO-98527: Config:set command failed for path bigger than 3 depth
1 parent d70883e commit a0f5f96

File tree

2 files changed

+71
-30
lines changed

2 files changed

+71
-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: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -355,22 +355,60 @@ public function testSaveToCheckScopeDataSet()
355355
$this->model->save();
356356
}
357357

358-
public function testSetDataByPath()
358+
/**
359+
* @param string $path
360+
* @param string $value
361+
* @param string $section
362+
* @param array $groups
363+
* @return void
364+
* @dataProvider setDataByPathDataProvider
365+
*/
366+
public function testSetDataByPath(string $path, string $value, string $section, array $groups)
359367
{
360-
$value = 'value';
361-
$path = '<section>/<group>/<field>';
362368
$this->model->setDataByPath($path, $value);
363-
$expected = [
364-
'section' => '<section>',
365-
'groups' => [
366-
'<group>' => [
367-
'fields' => [
368-
'<field>' => ['value' => $value],
369+
$this->assertEquals($section, $this->model->getData('section'));
370+
$this->assertEquals($groups, $this->model->getData('groups'));
371+
}
372+
373+
/**
374+
* @return array
375+
*/
376+
public function setDataByPathDataProvider(): array
377+
{
378+
return [
379+
'depth 3' => [
380+
'a/b/c',
381+
'value1',
382+
'a',
383+
[
384+
'b' => [
385+
'fields' => [
386+
'c' => ['value' => 'value1'],
387+
],
388+
],
389+
],
390+
],
391+
'depth 5' => [
392+
'a/b/c/d/e',
393+
'value1',
394+
'a',
395+
[
396+
'b' => [
397+
'groups' => [
398+
'c' => [
399+
'groups' => [
400+
'd' => [
401+
'fields' => [
402+
'e' => ['value' => 'value1'],
403+
],
404+
],
405+
],
406+
],
407+
],
369408
],
370409
],
371410
],
372411
];
373-
$this->assertSame($expected, $this->model->getData());
374412
}
375413

376414
/**
@@ -384,14 +422,14 @@ public function testSetDataByPathEmpty()
384422

385423
/**
386424
* @param string $path
387-
* @param string $expectedException
388-
*
425+
* @return void
389426
* @dataProvider setDataByPathWrongDepthDataProvider
390427
*/
391-
public function testSetDataByPathWrongDepth($path, $expectedException)
428+
public function testSetDataByPathWrongDepth(string $path)
392429
{
393-
$expectedException = 'Allowed depth of configuration is 3 (<section>/<group>/<field>). ' . $expectedException;
394-
$this->expectException('\UnexpectedValueException');
430+
$currentDepth = count(explode('/', $path));
431+
$expectedException = 'Minimal depth of configuration is 3. Your configuration depth is ' . $currentDepth;
432+
$this->expectException(\UnexpectedValueException::class);
395433
$this->expectExceptionMessage($expectedException);
396434
$value = 'value';
397435
$this->model->setDataByPath($path, $value);
@@ -400,13 +438,11 @@ public function testSetDataByPathWrongDepth($path, $expectedException)
400438
/**
401439
* @return array
402440
*/
403-
public function setDataByPathWrongDepthDataProvider()
441+
public function setDataByPathWrongDepthDataProvider(): array
404442
{
405443
return [
406-
'depth 2' => ['section/group', "Your configuration depth is 2 for path 'section/group'"],
407-
'depth 1' => ['section', "Your configuration depth is 1 for path 'section'"],
408-
'depth 4' => ['section/group/field/sub-field', "Your configuration depth is 4 for path"
409-
. " 'section/group/field/sub-field'", ],
444+
'depth 2' => ['section/group'],
445+
'depth 1' => ['section'],
410446
];
411447
}
412448
}

0 commit comments

Comments
 (0)