-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Maksold-feature/issue-73-encrypt-resolver'
- Loading branch information
Showing
4 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Copyright © semaio GmbH. All rights reserved. | ||
* See LICENSE.md bundled with this module for license details. | ||
*/ | ||
|
||
namespace Semaio\ConfigImportExport\Model\Resolver; | ||
|
||
use Magento\Framework\Encryption\EncryptorInterface; | ||
use Semaio\ConfigImportExport\Exception\UnresolveableValueException; | ||
use function strlen; | ||
|
||
class EncryptResolver extends AbstractResolver | ||
{ | ||
/** | ||
* @var EncryptorInterface | ||
*/ | ||
private $encryptor; | ||
|
||
public function __construct(EncryptorInterface $encryptor) | ||
{ | ||
$this->encryptor = $encryptor; | ||
} | ||
|
||
/** | ||
* Resolve the config value if wrapped with '%encrypt(value)%', this method encrypts the value. | ||
* | ||
* @param string|null $value | ||
* @param string|null $configPath | ||
* | ||
* @return string|null | ||
* | ||
* @throws UnresolveableValueException | ||
*/ | ||
public function resolve($value, $configPath = null) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
$value = (string)$value; | ||
if ($value === '%encrypt()%') { | ||
throw new UnresolveableValueException('Please specify a valid value to encrypt.'); | ||
} | ||
|
||
$valueToEncrypt = preg_replace_callback( | ||
'/\%encrypt\(([^)]+)\)\%/', | ||
function ($matches) { | ||
return $matches[1]; | ||
}, | ||
$value | ||
); | ||
|
||
return $this->encryptor->encrypt($valueToEncrypt); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function supports($value, $configPath = null): bool | ||
{ | ||
return 0 === strncmp((string)$value, '%encrypt', strlen('%encrypt')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Copyright © semaio GmbH. All rights reserved. | ||
* See LICENSE.md bundled with this module for license details. | ||
*/ | ||
|
||
namespace Semaio\ConfigImportExport\Test\Unit\Model\Validator; | ||
|
||
use Generator; | ||
use Magento\Framework\Encryption\EncryptorInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Semaio\ConfigImportExport\Exception\UnresolveableValueException; | ||
use Semaio\ConfigImportExport\Model\Resolver\EncryptResolver; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class EncryptResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var InputInterface | ||
*/ | ||
private $input; | ||
|
||
/** | ||
* @var OutputInterface | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* @var QuestionHelper | ||
*/ | ||
private $questionHelper; | ||
|
||
/** | ||
* @var MockObject|EncryptorInterface | ||
*/ | ||
private $encryptor; | ||
|
||
/** | ||
* Set up test class | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->input = $this->createMock(InputInterface::class); | ||
$this->output = $this->createMock(OutputInterface::class); | ||
$this->questionHelper = $this->createMock(QuestionHelper::class); | ||
$this->encryptor = $this->createMock(EncryptorInterface::class); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider resolveDataProvider | ||
*/ | ||
public function validate($value, $expectedResult): void | ||
{ | ||
$this->encryptor->expects($this->any()) | ||
->method('encrypt') | ||
->with($expectedResult) | ||
->willReturn($expectedResult); | ||
|
||
$this->assertEquals($this->getEncryptResolver()->resolve($value), $expectedResult); | ||
} | ||
|
||
public function resolveDataProvider(): Generator | ||
{ | ||
yield [ | ||
'test_without_data_to_encrypt', | ||
'test_without_data_to_encrypt', | ||
]; | ||
yield [ | ||
'%encrypt(data_to_encrypt)%', | ||
'data_to_encrypt', | ||
]; | ||
yield [ | ||
null, | ||
'', | ||
]; | ||
yield [ | ||
false, | ||
'', | ||
]; | ||
yield [ | ||
true, | ||
'1', | ||
]; | ||
} | ||
|
||
public function testItWillRaiseErrorIfEncryptValueIsEmpty(): void | ||
{ | ||
$this->expectException(UnresolveableValueException::class); | ||
|
||
$this->getEncryptResolver()->resolve('%encrypt()%'); | ||
} | ||
|
||
/** | ||
* @return EncryptResolver | ||
*/ | ||
private function getEncryptResolver() | ||
{ | ||
$resolver = new EncryptResolver($this->encryptor); | ||
$resolver->setInput($this->input); | ||
$resolver->setOutput($this->output); | ||
$resolver->setQuestionHelper($this->questionHelper); | ||
|
||
return $resolver; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters