Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SodiumChachaPatchTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
14 
15 class SodiumChachaPatchTest extends \PHPUnit\Framework\TestCase
16 {
17  const PATH_KEY = 'crypt/key';
18 
22  private $objectManager;
23 
27  private $deployConfig;
28 
29  protected function setUp()
30  {
32  $this->deployConfig = $this->objectManager->get(DeploymentConfig::class);
33  }
34 
35  public function testChangeEncryptionKey()
36  {
37  $testPath = 'test/config';
38  $testValue = 'test';
39 
40  $structureMock = $this->createMock(\Magento\Config\Model\Config\Structure\Proxy::class);
41  $structureMock->expects($this->once())
42  ->method('getFieldPathsByAttribute')
43  ->will($this->returnValue([$testPath]));
44 
46  $configModel = $this->objectManager->create(\Magento\Config\Model\ResourceModel\Config::class);
47  $configModel->saveConfig($testPath, $this->legacyEncrypt($testValue), 'default', 0);
48 
50  $patch = $this->objectManager->create(
51  \Magento\EncryptionKey\Setup\Patch\Data\SodiumChachaPatch::class,
52  [
53  'structure' => $structureMock,
54  ]
55  );
56  $patch->apply();
57 
58  $connection = $configModel->getConnection();
59  $values = $connection->fetchPairs(
60  $connection->select()->from(
61  $configModel->getMainTable(),
62  ['config_id', 'value']
63  )->where(
64  'path IN (?)',
65  [$testPath]
66  )->where(
67  'value NOT LIKE ?',
68  ''
69  )
70  );
71 
73  $encyptor = $this->objectManager->get(\Magento\Framework\Encryption\EncryptorInterface::class);
74 
75  $rawConfigValue = array_pop($values);
76 
77  $this->assertNotEquals($testValue, $rawConfigValue);
78  $this->assertStringStartsWith('0:' . Encryptor::CIPHER_LATEST . ':', $rawConfigValue);
79  $this->assertEquals($testValue, $encyptor->decrypt($rawConfigValue));
80  }
81 
82  private function legacyEncrypt(string $data): string
83  {
84  // @codingStandardsIgnoreStart
85  $handle = @mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
86  $initVectorSize = @mcrypt_enc_get_iv_size($handle);
87  $initVector = str_repeat("\0", $initVectorSize);
88  @mcrypt_generic_init($handle, $this->deployConfig->get(static::PATH_KEY), $initVector);
89 
90  $encrpted = @mcrypt_generic($handle, $data);
91 
92  @mcrypt_generic_deinit($handle);
93  @mcrypt_module_close($handle);
94  // @codingStandardsIgnoreEnd
95 
96  return '0:' . Encryptor::CIPHER_RIJNDAEL_256 . ':' . base64_encode($encrpted);
97  }
98 }
$initVector
$values
Definition: options.phtml:88
$initVectorSize
$connection
Definition: bulk.php:13
$handle