Skip to content

Commit 2e2230a

Browse files
author
Andrii Kasian
committed
Merge remote-tracking branch 'mainline/develop' into MAGETWO-42501
2 parents ce9a578 + c440305 commit 2e2230a

File tree

23 files changed

+1436
-299
lines changed

23 files changed

+1436
-299
lines changed

app/code/Magento/EncryptionKey/Controller/Adminhtml/Crypt/Key.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
8+
namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt;
9+
710
/**
811
* Encryption key changer controller
912
*/
10-
namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt;
11-
1213
abstract class Key extends \Magento\Backend\App\Action
1314
{
1415
/**

app/code/Magento/EncryptionKey/Controller/Adminhtml/Crypt/Key/Save.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function execute()
6464
}
6565

6666
$newKey = $this->change->changeEncryptionKey($key);
67-
$this->messageManager->addSuccess(__('The encryption key has been changed.'));
67+
$this->messageManager->addSuccessMessage(__('The encryption key has been changed.'));
6868

6969
if (!$key) {
70-
$this->messageManager->addNotice(
70+
$this->messageManager->addNoticeMessage(
7171
__(
7272
'This is your new encryption key: <span style="font-family:monospace;">%1</span>. ' .
7373
'Be sure to write it down and take good care of it!',
@@ -77,7 +77,7 @@ public function execute()
7777
}
7878
$this->cache->clean();
7979
} catch (\Exception $e) {
80-
$this->messageManager->addError($e->getMessage());
80+
$this->messageManager->addErrorMessage($e->getMessage());
8181
$this->_session->setFormData(['crypt_key' => $key]);
8282
}
8383
$this->_redirect('adminhtml/*/');

app/code/Magento/EncryptionKey/Model/Resource/Key/Change.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,6 @@ protected function _construct()
7777
$this->_init('core_config_data', 'config_id');
7878
}
7979

80-
/**
81-
* Re-encrypt all encrypted data in the database
82-
*
83-
* TODO: seems not used
84-
*
85-
* @param bool $safe Specifies whether wrapping re-encryption into the database transaction or not
86-
* @return void
87-
* @throws \Exception
88-
*/
89-
public function reEncryptDatabaseValues($safe = true)
90-
{
91-
// update database only
92-
if ($safe) {
93-
$this->beginTransaction();
94-
}
95-
try {
96-
$this->_reEncryptSystemConfigurationValues();
97-
$this->_reEncryptCreditCardNumbers();
98-
if ($safe) {
99-
$this->commit();
100-
}
101-
} catch (\Exception $e) {
102-
if ($safe) {
103-
$this->rollBack();
104-
}
105-
throw $e;
106-
}
107-
}
108-
10980
/**
11081
* Change encryption key
11182
*
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\EncryptionKey\Test\Unit\Controller\Adminhtml\Crypt\Key;
9+
10+
/**
11+
* Test class for Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save
12+
*/
13+
class SaveTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
16+
protected $encryptMock;
17+
/** @var \Magento\EncryptionKey\Model\Resource\Key\Change|\PHPUnit_Framework_MockObject_MockObject */
18+
protected $changeMock;
19+
/** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $cacheMock;
21+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $requestMock;
23+
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
24+
protected $managerMock;
25+
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $responseMock;
27+
/** @var \Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save */
28+
protected $model;
29+
30+
public function setUp()
31+
{
32+
$this->encryptMock = $this->getMockBuilder('Magento\Framework\Encryption\EncryptorInterface')
33+
->disableOriginalConstructor()
34+
->setMethods([])
35+
->getMock();
36+
$this->changeMock = $this->getMockBuilder('Magento\EncryptionKey\Model\Resource\Key\Change')
37+
->disableOriginalConstructor()
38+
->setMethods([])
39+
->getMock();
40+
$this->cacheMock = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
41+
->disableOriginalConstructor()
42+
->setMethods([])
43+
->getMock();
44+
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
45+
->disableOriginalConstructor()
46+
->setMethods(['getPost'])
47+
->getMockForAbstractClass();
48+
$this->managerMock = $this->getMockBuilder('\Magento\Framework\Message\ManagerInterface')
49+
->disableOriginalConstructor()
50+
->setMethods([])
51+
->getMock();
52+
$this->responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface')
53+
->disableOriginalConstructor()
54+
->setMethods(['setRedirect'])
55+
->getMockForAbstractClass();
56+
57+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
58+
59+
$this->model = $helper->getObject(
60+
'Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save',
61+
[
62+
'encryptor' => $this->encryptMock,
63+
'change' => $this->changeMock,
64+
'cache' => $this->cacheMock,
65+
'request' => $this->requestMock,
66+
'messageManager' => $this->managerMock,
67+
'response' => $this->responseMock,
68+
]
69+
);
70+
}
71+
72+
public function testExecuteNonRandomAndWithCryptKey()
73+
{
74+
$expectedMessage = 'The encryption key has been changed.';
75+
$key = 1;
76+
$newKey = 'RSASHA9000VERYSECURESUPERMANKEY';
77+
$this->requestMock
78+
->expects($this->at(0))
79+
->method('getPost')
80+
->with($this->equalTo('generate_random'))
81+
->willReturn(0);
82+
$this->requestMock
83+
->expects($this->at(1))
84+
->method('getPost')
85+
->with($this->equalTo('crypt_key'))
86+
->willReturn($key);
87+
$this->encryptMock->expects($this->once())->method('validateKey');
88+
$this->changeMock->expects($this->once())->method('changeEncryptionKey')->willReturn($newKey);
89+
$this->managerMock->expects($this->once())->method('addSuccessMessage')->with($expectedMessage);
90+
$this->cacheMock->expects($this->once())->method('clean');
91+
$this->responseMock->expects($this->once())->method('setRedirect');
92+
93+
$this->model->execute();
94+
}
95+
96+
public function testExecuteNonRandomAndWithoutCryptKey()
97+
{
98+
$key = null;
99+
$this->requestMock
100+
->expects($this->at(0))
101+
->method('getPost')
102+
->with($this->equalTo('generate_random'))
103+
->willReturn(0);
104+
$this->requestMock
105+
->expects($this->at(1))
106+
->method('getPost')
107+
->with($this->equalTo('crypt_key'))
108+
->willReturn($key);
109+
$this->managerMock->expects($this->once())->method('addErrorMessage');
110+
111+
$this->model->execute();
112+
}
113+
114+
public function testExecuteRandom()
115+
{
116+
$newKey = 'RSASHA9000VERYSECURESUPERMANKEY';
117+
$this->requestMock
118+
->expects($this->at(0))
119+
->method('getPost')
120+
->with($this->equalTo('generate_random'))
121+
->willReturn(1);
122+
$this->changeMock->expects($this->once())->method('changeEncryptionKey')->willReturn($newKey);
123+
$this->managerMock->expects($this->once())->method('addSuccessMessage');
124+
$this->managerMock->expects($this->once())->method('addNoticeMessage');
125+
$this->cacheMock->expects($this->once())->method('clean');
126+
$this->responseMock->expects($this->once())->method('setRedirect');
127+
128+
$this->model->execute();
129+
}
130+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\EncryptionKey\Test\Unit\Model\Resource\Key;
8+
9+
/**
10+
* Test Class For Magento\EncryptionKey\Model\Resource\Key\Change
11+
*/
12+
class ChangeTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
15+
protected $encryptMock;
16+
/** @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject */
17+
protected $filesystemMock;
18+
/** @var \Magento\Config\Model\Config\Structure|\PHPUnit_Framework_MockObject_MockObject */
19+
protected $structureMock;
20+
/** @var \Magento\Framework\App\DeploymentConfig\Writer|\PHPUnit_Framework_MockObject_MockObject */
21+
protected $writerMock;
22+
/** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $adapterMock;
24+
/** @var \Magento\Framework\App\Resource|\PHPUnit_Framework_MockObject_MockObject */
25+
protected $resourceMock;
26+
/** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
27+
protected $selectMock;
28+
/** @var \Magento\Framework\Model\Resource\Db\TransactionManagerInterface */
29+
protected $tansactionMock;
30+
/** @var \Magento\Framework\Model\Resource\Db\ObjectRelationProcessor|\PHPUnit_Framework_MockObject_MockObject */
31+
protected $objRelationMock;
32+
/** @var \Magento\EncryptionKey\Model\Resource\Key\Change */
33+
protected $model;
34+
35+
public function setUp()
36+
{
37+
$this->encryptMock = $this->getMockBuilder('Magento\Framework\Encryption\EncryptorInterface')
38+
->disableOriginalConstructor()
39+
->setMethods(['setNewKey', 'exportKeys'])
40+
->getMockForAbstractClass();
41+
$this->filesystemMock = $this->getMockBuilder('Magento\Framework\Filesystem')
42+
->disableOriginalConstructor()
43+
->setMethods([])
44+
->getMock();
45+
$this->structureMock = $this->getMockBuilder('Magento\Config\Model\Config\Structure')
46+
->disableOriginalConstructor()
47+
->setMethods([])
48+
->getMock();
49+
$this->writerMock = $this->getMockBuilder('Magento\Framework\App\DeploymentConfig\Writer')
50+
->disableOriginalConstructor()
51+
->setMethods([])
52+
->getMock();
53+
$this->adapterMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\AdapterInterface')
54+
->disableOriginalConstructor()
55+
->setMethods([])
56+
->getMock();
57+
$this->resourceMock = $this->getMockBuilder('Magento\Framework\App\Resource')
58+
->disableOriginalConstructor()
59+
->setMethods([])
60+
->getMock();
61+
$this->selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
62+
->disableOriginalConstructor()
63+
->setMethods([])
64+
->getMock();
65+
$this->tansactionMock = $this->getMockBuilder('Magento\Framework\Model\Resource\Db\TransactionManagerInterface')
66+
->disableOriginalConstructor()
67+
->setMethods([])
68+
->getMock();
69+
$this->objRelationMock = $this->getMockBuilder('Magento\Framework\Model\Resource\Db\ObjectRelationProcessor')
70+
->disableOriginalConstructor()
71+
->setMethods([])
72+
->getMock();
73+
74+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
75+
76+
$this->model = $helper->getObject(
77+
'Magento\EncryptionKey\Model\Resource\Key\Change',
78+
[
79+
'filesystem' => $this->filesystemMock,
80+
'structure' => $this->structureMock,
81+
'encryptor' => $this->encryptMock,
82+
'writer' => $this->writerMock,
83+
'adapterInterface' => $this->adapterMock,
84+
'resource' => $this->resourceMock,
85+
'transactionManager' => $this->tansactionMock,
86+
'relationProcessor' => $this->objRelationMock
87+
]
88+
);
89+
}
90+
91+
public function testChangeEncryptionKey()
92+
{
93+
$paths = ['path1', 'path2'];
94+
$table = ['item1', 'item2'];
95+
$values = [
96+
'key1' => 'value1',
97+
'key2' => 'value2'
98+
];
99+
$key = 'key';
100+
101+
$this->writerMock->expects($this->once())->method('checkIfWritable')->willReturn(true);
102+
$this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->adapterMock);
103+
$this->adapterMock->expects($this->once())->method('beginTransaction');
104+
$this->structureMock->expects($this->once())->method('getFieldPathsByAttribute')->willReturn($paths);
105+
$this->resourceMock->expects($this->atLeastOnce())->method('getTableName')->willReturn($table);
106+
$this->adapterMock->expects($this->any())->method('select')->willReturn($this->selectMock);
107+
$this->adapterMock->expects($this->any())->method('fetchPairs')->willReturn($values);
108+
$this->selectMock->expects($this->any())->method('from')->willReturnSelf();
109+
$this->selectMock->expects($this->atLeastOnce())->method('where')->willReturnSelf();
110+
$this->selectMock->expects($this->any())->method('update')->willReturnSelf();
111+
$this->writerMock->expects($this->once())->method('saveConfig');
112+
$this->adapterMock->expects($this->once())->method('getTransactionLevel')->willReturn(1);
113+
114+
$this->assertEquals($key, $this->model->changeEncryptionKey($key));
115+
}
116+
117+
public function testChangeEncryptionKeyThrowsException()
118+
{
119+
$key = 'key';
120+
$this->writerMock->expects($this->once())->method('checkIfWritable')->willReturn(false);
121+
122+
try {
123+
$this->model->changeEncryptionKey($key);
124+
} catch (\Exception $e) {
125+
return;
126+
}
127+
128+
$this->fail('An excpected exception was not signaled.');
129+
}
130+
}

0 commit comments

Comments
 (0)