Skip to content

Commit 305bf94

Browse files
committed
MAGETWO-36930: [GITHUB] Store config re-encrypt encrypted values on save #1223
- Fix to make sure values are not encrypted if not changed - Removed re-encrypting values - Added integration test
1 parent b0c7944 commit 305bf94

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ protected function _afterLoad()
8181
*/
8282
public function beforeSave()
8383
{
84+
$this->_dataSaveAllowed = false;
8485
$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)) {
86+
// don't save value, if an obscured value was received. This indicates that data was not changed.
87+
if (!preg_match('/^\*+$/', $value) && !empty($value)) {
88+
$this->_dataSaveAllowed = true;
9089
$encrypted = $this->_encryptor->encrypt($value);
9190
if ($encrypted) {
9291
$this->setValue($encrypted);

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,41 +90,22 @@ public function testProcessValue()
9090
* @param $value
9191
* @param $valueToSave
9292
*/
93-
public function testBeforeSave($value, $valueToSave)
93+
public function testBeforeSave($value, $encryptMethodCall)
9494
{
9595
$this->_resourceMock->expects($this->any())->method('addCommitCallback')->will($this->returnSelf());
9696
$this->_resourceMock->expects($this->any())->method('commit')->will($this->returnSelf());
9797

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-
);
116-
11798
$this->_model->setValue($value);
11899
$this->_model->setPath('some/path');
119100
$this->_model->beforeSave();
120-
$this->assertEquals($this->_model->getValue(), 'encrypted');
101+
$this->_encryptorMock->expects($this->exactly($encryptMethodCall))->method('encrypt')->with($this->any());
121102
}
122103

123104
/**
124105
* @return array
125106
*/
126107
public function beforeSaveDataProvider()
127108
{
128-
return [['****', 'oldValue'], ['newValue', 'newValue']];
109+
return [['someValue', 1], ['****', 0]];
129110
}
130111
}
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)