Skip to content

Commit 34913c6

Browse files
author
Bryant Luk
committed
Merge remote-tracking branch 'api/MAGETWO-36930-Github-Issue-1223' into api-develop
2 parents 922904e + e5208f5 commit 34913c6

File tree

3 files changed

+79
-34
lines changed

3 files changed

+79
-34
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
namespace Magento\Config\Model\Config\Backend;
1212

13-
class Encrypted extends \Magento\Framework\App\Config\Value implements \Magento\Framework\App\Config\Data\ProcessorInterface
13+
class Encrypted extends \Magento\Framework\App\Config\Value implements
14+
\Magento\Framework\App\Config\Data\ProcessorInterface
1415
{
1516
/**
1617
* @var \Magento\Framework\Encryption\EncryptorInterface
@@ -58,7 +59,9 @@ public function __sleep()
5859
public function __wakeup()
5960
{
6061
parent::__wakeup();
61-
$this->_encryptor = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\Encryption\EncryptorInterface');
62+
$this->_encryptor = \Magento\Framework\App\ObjectManager::getInstance()->get(
63+
'Magento\Framework\Encryption\EncryptorInterface'
64+
);
6265
}
6366

6467
/**
@@ -81,16 +84,13 @@ protected function _afterLoad()
8184
*/
8285
public function beforeSave()
8386
{
87+
$this->_dataSaveAllowed = false;
8488
$value = (string)$this->getValue();
85-
// don't change value, if an obscured value came
86-
if (preg_match('/^\*+$/', $this->getValue())) {
87-
$value = $this->getOldValue();
88-
}
89-
if (!empty($value)) {
89+
// don't save value, if an obscured value was received. This indicates that data was not changed.
90+
if (!preg_match('/^\*+$/', $value) && !empty($value)) {
91+
$this->_dataSaveAllowed = true;
9092
$encrypted = $this->_encryptor->encrypt($value);
91-
if ($encrypted) {
92-
$this->setValue($encrypted);
93-
}
93+
$this->setValue($encrypted);
9494
}
9595
}
9696

app/code/Magento/Config/Test/Unit/Model/Config/Backend/EncryptedTest.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,44 +87,31 @@ public function testProcessValue()
8787
* @covers \Magento\Config\Model\Config\Backend\Encrypted::beforeSave
8888
* @dataProvider beforeSaveDataProvider
8989
*
90-
* @param $value
91-
* @param $valueToSave
90+
* @param string $value
91+
* @param string $expectedValue
92+
* @param int $encryptMethodCall
9293
*/
93-
public function testBeforeSave($value, $valueToSave)
94+
public function testBeforeSave($value, $expectedValue, $encryptMethodCall)
9495
{
9596
$this->_resourceMock->expects($this->any())->method('addCommitCallback')->will($this->returnSelf());
9697
$this->_resourceMock->expects($this->any())->method('commit')->will($this->returnSelf());
97-
98-
$this->_configMock->expects(
99-
$this->any()
100-
)->method(
101-
'getValue'
102-
)->with(
103-
'some/path'
104-
)->will(
105-
$this->returnValue('oldValue')
106-
);
107-
$this->_encryptorMock->expects(
108-
$this->once()
109-
)->method(
110-
'encrypt'
111-
)->with(
112-
$valueToSave
113-
)->will(
114-
$this->returnValue('encrypted')
115-
);
98+
$this->_encryptorMock->expects($this->exactly($encryptMethodCall))
99+
->method('encrypt')
100+
->with($value)
101+
->will($this->returnValue('encrypted'));
116102

117103
$this->_model->setValue($value);
118104
$this->_model->setPath('some/path');
119105
$this->_model->beforeSave();
120-
$this->assertEquals($this->_model->getValue(), 'encrypted');
106+
107+
$this->assertEquals($expectedValue, $this->_model->getValue());
121108
}
122109

123110
/**
124111
* @return array
125112
*/
126113
public function beforeSaveDataProvider()
127114
{
128-
return [['****', 'oldValue'], ['newValue', 'newValue']];
115+
return [['someValue', 'encrypted', 1], ['****', '****', 0]];
129116
}
130117
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Config\Model\Config\Backend;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/**
12+
* @magentoAppArea adminhtml
13+
*/
14+
class EncryptedTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @magentoDbIsolation enabled
18+
*/
19+
public function testEncryptionSave()
20+
{
21+
$originalValue = '1Password';
22+
23+
/** @var $model \Magento\Config\Model\Config\Backend\Encrypted */
24+
$model = Bootstrap::getObjectManager()->create('Magento\Config\Model\Config\Backend\Encrypted');
25+
$model->setPath('carriers/usps/password');
26+
$model->setScopeId(0);
27+
$model->setScope('default');
28+
$model->setScopeCode('');
29+
$model->setValue($originalValue);
30+
$model->save();
31+
32+
// Pass in the obscured value
33+
$model->setPath('carriers/usps/password');
34+
$model->setScopeId(0);
35+
$model->setScope('default');
36+
$model->setScopeCode('');
37+
$model->setValue('*****');
38+
$model->save();
39+
40+
//Verify original value is not changed for obscured value
41+
$value = $model->load($model->getId())->getValue();
42+
$this->assertEquals($originalValue, $value, 'Original value is not expected to change.');
43+
44+
// Verify if actual value is changed
45+
$changedValue = 'newPassword';
46+
47+
$model->setPath('carriers/usps/password');
48+
$model->setScopeId(0);
49+
$model->setScope('default');
50+
$model->setScopeCode('');
51+
$model->setValue($changedValue);
52+
$model->save();
53+
54+
//Verify original value is changed
55+
$value = $model->load($model->getId())->getValue();
56+
$this->assertEquals($changedValue, $value, 'Original value is expected to change.');
57+
}
58+
}

0 commit comments

Comments
 (0)