Skip to content

Commit

Permalink
Merge pull request #664 from magento-api/API-Sprint-58-Bugs-and-Tests
Browse files Browse the repository at this point in the history
[API] Sprint 58 Bug Fixes and Test Coverage
  • Loading branch information
Paliarush, Alexander(apaliarush) committed Oct 2, 2015
2 parents 6e7c85c + ab0ce55 commit c440305
Show file tree
Hide file tree
Showing 23 changed files with 1,436 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* See COPYING.txt for license details.
*/


namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt;

/**
* Encryption key changer controller
*/
namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt;

abstract class Key extends \Magento\Backend\App\Action
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function execute()
}

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

if (!$key) {
$this->messageManager->addNotice(
$this->messageManager->addNoticeMessage(
__(
'This is your new encryption key: <span style="font-family:monospace;">%1</span>. ' .
'Be sure to write it down and take good care of it!',
Expand All @@ -77,7 +77,7 @@ public function execute()
}
$this->cache->clean();
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
$this->messageManager->addErrorMessage($e->getMessage());
$this->_session->setFormData(['crypt_key' => $key]);
}
$this->_redirect('adminhtml/*/');
Expand Down
29 changes: 0 additions & 29 deletions app/code/Magento/EncryptionKey/Model/Resource/Key/Change.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,6 @@ protected function _construct()
$this->_init('core_config_data', 'config_id');
}

/**
* Re-encrypt all encrypted data in the database
*
* TODO: seems not used
*
* @param bool $safe Specifies whether wrapping re-encryption into the database transaction or not
* @return void
* @throws \Exception
*/
public function reEncryptDatabaseValues($safe = true)
{
// update database only
if ($safe) {
$this->beginTransaction();
}
try {
$this->_reEncryptSystemConfigurationValues();
$this->_reEncryptCreditCardNumbers();
if ($safe) {
$this->commit();
}
} catch (\Exception $e) {
if ($safe) {
$this->rollBack();
}
throw $e;
}
}

/**
* Change encryption key
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\EncryptionKey\Test\Unit\Controller\Adminhtml\Crypt\Key;

/**
* Test class for Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save
*/
class SaveTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $encryptMock;
/** @var \Magento\EncryptionKey\Model\Resource\Key\Change|\PHPUnit_Framework_MockObject_MockObject */
protected $changeMock;
/** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $cacheMock;
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $requestMock;
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $managerMock;
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $responseMock;
/** @var \Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save */
protected $model;

public function setUp()
{
$this->encryptMock = $this->getMockBuilder('Magento\Framework\Encryption\EncryptorInterface')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->changeMock = $this->getMockBuilder('Magento\EncryptionKey\Model\Resource\Key\Change')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->cacheMock = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
->disableOriginalConstructor()
->setMethods(['getPost'])
->getMockForAbstractClass();
$this->managerMock = $this->getMockBuilder('\Magento\Framework\Message\ManagerInterface')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface')
->disableOriginalConstructor()
->setMethods(['setRedirect'])
->getMockForAbstractClass();

$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->model = $helper->getObject(
'Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key\Save',
[
'encryptor' => $this->encryptMock,
'change' => $this->changeMock,
'cache' => $this->cacheMock,
'request' => $this->requestMock,
'messageManager' => $this->managerMock,
'response' => $this->responseMock,
]
);
}

public function testExecuteNonRandomAndWithCryptKey()
{
$expectedMessage = 'The encryption key has been changed.';
$key = 1;
$newKey = 'RSASHA9000VERYSECURESUPERMANKEY';
$this->requestMock
->expects($this->at(0))
->method('getPost')
->with($this->equalTo('generate_random'))
->willReturn(0);
$this->requestMock
->expects($this->at(1))
->method('getPost')
->with($this->equalTo('crypt_key'))
->willReturn($key);
$this->encryptMock->expects($this->once())->method('validateKey');
$this->changeMock->expects($this->once())->method('changeEncryptionKey')->willReturn($newKey);
$this->managerMock->expects($this->once())->method('addSuccessMessage')->with($expectedMessage);
$this->cacheMock->expects($this->once())->method('clean');
$this->responseMock->expects($this->once())->method('setRedirect');

$this->model->execute();
}

public function testExecuteNonRandomAndWithoutCryptKey()
{
$key = null;
$this->requestMock
->expects($this->at(0))
->method('getPost')
->with($this->equalTo('generate_random'))
->willReturn(0);
$this->requestMock
->expects($this->at(1))
->method('getPost')
->with($this->equalTo('crypt_key'))
->willReturn($key);
$this->managerMock->expects($this->once())->method('addErrorMessage');

$this->model->execute();
}

public function testExecuteRandom()
{
$newKey = 'RSASHA9000VERYSECURESUPERMANKEY';
$this->requestMock
->expects($this->at(0))
->method('getPost')
->with($this->equalTo('generate_random'))
->willReturn(1);
$this->changeMock->expects($this->once())->method('changeEncryptionKey')->willReturn($newKey);
$this->managerMock->expects($this->once())->method('addSuccessMessage');
$this->managerMock->expects($this->once())->method('addNoticeMessage');
$this->cacheMock->expects($this->once())->method('clean');
$this->responseMock->expects($this->once())->method('setRedirect');

$this->model->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\EncryptionKey\Test\Unit\Model\Resource\Key;

/**
* Test Class For Magento\EncryptionKey\Model\Resource\Key\Change
*/
class ChangeTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Framework\Encryption\EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $encryptMock;
/** @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject */
protected $filesystemMock;
/** @var \Magento\Config\Model\Config\Structure|\PHPUnit_Framework_MockObject_MockObject */
protected $structureMock;
/** @var \Magento\Framework\App\DeploymentConfig\Writer|\PHPUnit_Framework_MockObject_MockObject */
protected $writerMock;
/** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $adapterMock;
/** @var \Magento\Framework\App\Resource|\PHPUnit_Framework_MockObject_MockObject */
protected $resourceMock;
/** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
protected $selectMock;
/** @var \Magento\Framework\Model\Resource\Db\TransactionManagerInterface */
protected $tansactionMock;
/** @var \Magento\Framework\Model\Resource\Db\ObjectRelationProcessor|\PHPUnit_Framework_MockObject_MockObject */
protected $objRelationMock;
/** @var \Magento\EncryptionKey\Model\Resource\Key\Change */
protected $model;

public function setUp()
{
$this->encryptMock = $this->getMockBuilder('Magento\Framework\Encryption\EncryptorInterface')
->disableOriginalConstructor()
->setMethods(['setNewKey', 'exportKeys'])
->getMockForAbstractClass();
$this->filesystemMock = $this->getMockBuilder('Magento\Framework\Filesystem')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->structureMock = $this->getMockBuilder('Magento\Config\Model\Config\Structure')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->writerMock = $this->getMockBuilder('Magento\Framework\App\DeploymentConfig\Writer')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->adapterMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\AdapterInterface')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->resourceMock = $this->getMockBuilder('Magento\Framework\App\Resource')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->tansactionMock = $this->getMockBuilder('Magento\Framework\Model\Resource\Db\TransactionManagerInterface')
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->objRelationMock = $this->getMockBuilder('Magento\Framework\Model\Resource\Db\ObjectRelationProcessor')
->disableOriginalConstructor()
->setMethods([])
->getMock();

$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->model = $helper->getObject(
'Magento\EncryptionKey\Model\Resource\Key\Change',
[
'filesystem' => $this->filesystemMock,
'structure' => $this->structureMock,
'encryptor' => $this->encryptMock,
'writer' => $this->writerMock,
'adapterInterface' => $this->adapterMock,
'resource' => $this->resourceMock,
'transactionManager' => $this->tansactionMock,
'relationProcessor' => $this->objRelationMock
]
);
}

public function testChangeEncryptionKey()
{
$paths = ['path1', 'path2'];
$table = ['item1', 'item2'];
$values = [
'key1' => 'value1',
'key2' => 'value2'
];
$key = 'key';

$this->writerMock->expects($this->once())->method('checkIfWritable')->willReturn(true);
$this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->adapterMock);
$this->adapterMock->expects($this->once())->method('beginTransaction');
$this->structureMock->expects($this->once())->method('getFieldPathsByAttribute')->willReturn($paths);
$this->resourceMock->expects($this->atLeastOnce())->method('getTableName')->willReturn($table);
$this->adapterMock->expects($this->any())->method('select')->willReturn($this->selectMock);
$this->adapterMock->expects($this->any())->method('fetchPairs')->willReturn($values);
$this->selectMock->expects($this->any())->method('from')->willReturnSelf();
$this->selectMock->expects($this->atLeastOnce())->method('where')->willReturnSelf();
$this->selectMock->expects($this->any())->method('update')->willReturnSelf();
$this->writerMock->expects($this->once())->method('saveConfig');
$this->adapterMock->expects($this->once())->method('getTransactionLevel')->willReturn(1);

$this->assertEquals($key, $this->model->changeEncryptionKey($key));
}

public function testChangeEncryptionKeyThrowsException()
{
$key = 'key';
$this->writerMock->expects($this->once())->method('checkIfWritable')->willReturn(false);

try {
$this->model->changeEncryptionKey($key);
} catch (\Exception $e) {
return;
}

$this->fail('An excpected exception was not signaled.');
}
}
Loading

0 comments on commit c440305

Please sign in to comment.