Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit ee49a31

Browse files
Merge remote-tracking branch 'origin/MAGETWO-71620-Null-input-not-allowed-for-config-sensitive-set' into BundledPR-Sep7b
2 parents 910e62c + aeb5995 commit ee49a31

File tree

5 files changed

+262
-70
lines changed

5 files changed

+262
-70
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\Model\Config\Backend\Email;
7+
8+
use Magento\Config\Model\Config\Backend\Email\Address;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
12+
class AddressTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var Address
16+
*/
17+
private $model;
18+
19+
protected function setUp()
20+
{
21+
$objectManager = new ObjectManager($this);
22+
$this->model = $objectManager->getObject(Address::class);
23+
}
24+
25+
/**
26+
* @dataProvider beforeSaveDataProvider
27+
* @param string|null $value
28+
* @param string|bool $expectedValue false if exception to be thrown
29+
* @return void
30+
*/
31+
public function testBeforeSave($value, $expectedValue)
32+
{
33+
$this->model->setValue($value);
34+
35+
if ($expectedValue === false) {
36+
$this->expectException(LocalizedException::class);
37+
}
38+
39+
$this->model->beforeSave();
40+
$this->assertEquals($expectedValue, $this->model->getValue());
41+
}
42+
43+
public function beforeSaveDataProvider()
44+
{
45+
return [
46+
47+
48+
['not.a.real.email', false],
49+
[null, false],
50+
['', false]
51+
];
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\Model\Config\Backend\Email;
7+
8+
use Magento\Config\Model\Config\Backend\Email\Sender;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
12+
class SenderTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var Sender
16+
*/
17+
private $model;
18+
19+
protected function setUp()
20+
{
21+
$objectManager = new ObjectManager($this);
22+
$this->model = $objectManager->getObject(Sender::class);
23+
}
24+
25+
/**
26+
* @dataProvider beforeSaveDataProvider
27+
* @param string|null $value
28+
* @param string|bool $expectedValue false if exception to be thrown
29+
* @return void
30+
*/
31+
public function testBeforeSave($value, $expectedValue)
32+
{
33+
$this->model->setValue($value);
34+
35+
if ($expectedValue === false) {
36+
$this->expectException(LocalizedException::class);
37+
}
38+
39+
$this->model->beforeSave();
40+
41+
$this->assertEquals($expectedValue, $this->model->getValue());
42+
}
43+
44+
public function beforeSaveDataProvider()
45+
{
46+
return [
47+
['Mr. Real Name', 'Mr. Real Name'],
48+
[str_repeat('a', 256), false],
49+
[null, false],
50+
['', false],
51+
];
52+
}
53+
}

app/code/Magento/Deploy/Model/ConfigWriter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function save(array $values, $scope = ScopeConfigInterface::SCOPE_TYPE_DE
6363
{
6464
$config = [];
6565
$pathPrefix = $this->getPathPrefix($scope, $scopeCode);
66+
67+
$values = array_filter(
68+
$values,
69+
function ($value) {
70+
return $value !== null;
71+
}
72+
);
73+
6674
foreach ($values as $configPath => $configValue) {
6775
$fullConfigPath = $pathPrefix . $configPath;
6876
$backendModel = $this->preparedValueFactory->create($configPath, $configValue, $scope, $scopeCode);

app/code/Magento/Deploy/Test/Unit/Model/ConfigWriterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,23 @@ public function testSaveDefaultScope()
168168

169169
$this->model->save($values);
170170
}
171+
172+
/**
173+
* Save null (empty input) through CLI and assert it does not create backend model for validation
174+
* @return void
175+
*/
176+
public function testSavingNullValues()
177+
{
178+
$values = [
179+
'some1/config1/path1' => null,
180+
];
181+
182+
$this->preparedValueFactoryMock->expects($this->never())->method('create');
183+
184+
$this->writerMock->expects($this->once())
185+
->method('saveConfig')
186+
->with([ConfigFilePool::APP_ENV => []]);
187+
188+
$this->model->save($values);
189+
}
171190
}

0 commit comments

Comments
 (0)