ChangeTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\EncryptionKey\Model\ResourceModel\Key;
  8. class ChangeTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\ObjectManagerInterface
  12. */
  13. protected $objectManager;
  14. protected function setup()
  15. {
  16. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  17. }
  18. /**
  19. * @expectedException \Exception
  20. * @expectedExceptionMessage Deployment configuration file is not writable
  21. */
  22. public function testChangeEncryptionKeyConfigNotWritable()
  23. {
  24. $writerMock = $this->createMock(\Magento\Framework\App\DeploymentConfig\Writer::class);
  25. $writerMock->expects($this->once())->method('checkIfWritable')->will($this->returnValue(false));
  26. /** @var \Magento\EncryptionKey\Model\ResourceModel\Key\Change $keyChangeModel */
  27. $keyChangeModel = $this->objectManager->create(
  28. \Magento\EncryptionKey\Model\ResourceModel\Key\Change::class,
  29. ['writer' => $writerMock]
  30. );
  31. $keyChangeModel->changeEncryptionKey();
  32. }
  33. /**
  34. * @magentoDbIsolation enabled
  35. * @magentoDataFixture Magento/EncryptionKey/_files/payment_info.php
  36. */
  37. public function testChangeEncryptionKey()
  38. {
  39. $testPath = 'test/config';
  40. $testValue = 'test';
  41. $writerMock = $this->createMock(\Magento\Framework\App\DeploymentConfig\Writer::class);
  42. $writerMock->expects($this->once())->method('checkIfWritable')->will($this->returnValue(true));
  43. $structureMock = $this->createMock(\Magento\Config\Model\Config\Structure::class);
  44. $structureMock->expects($this->once())
  45. ->method('getFieldPathsByAttribute')
  46. ->will($this->returnValue([$testPath]));
  47. /** @var \Magento\EncryptionKey\Model\ResourceModel\Key\Change $keyChangeModel */
  48. $keyChangeModel = $this->objectManager->create(
  49. \Magento\EncryptionKey\Model\ResourceModel\Key\Change::class,
  50. ['structure' => $structureMock, 'writer' => $writerMock]
  51. );
  52. $configModel = $this->objectManager->create(
  53. \Magento\Config\Model\ResourceModel\Config::class
  54. );
  55. $configModel->saveConfig($testPath, 'test', 'default', 0);
  56. $this->assertNotNull($keyChangeModel->changeEncryptionKey());
  57. $connection = $keyChangeModel->getConnection();
  58. // Verify that the config value has been encrypted
  59. $values1 = $connection->fetchPairs(
  60. $connection->select()->from(
  61. $keyChangeModel->getTable('core_config_data'),
  62. ['config_id', 'value']
  63. )->where(
  64. 'path IN (?)',
  65. [$testPath]
  66. )->where(
  67. 'value NOT LIKE ?',
  68. ''
  69. )
  70. );
  71. $this->assertNotContains($testValue, $values1);
  72. $this->assertRegExp('|([0-9]+:)([0-9]+:)([a-zA-Z0-9+/]+=*)|', current($values1));
  73. // Verify that the credit card number has been encrypted
  74. $values2 = $connection->fetchPairs(
  75. $connection->select()->from(
  76. $keyChangeModel->getTable('sales_order_payment'),
  77. ['entity_id', 'cc_number_enc']
  78. )
  79. );
  80. $this->assertNotContains('1111111111', $values2);
  81. $this->assertRegExp('|([0-9]+:)([0-9]+:)([a-zA-Z0-9+/]+=*)|', current($values2));
  82. /** clean up */
  83. $select = $connection->select()->from($configModel->getMainTable())->where('path=?', $testPath);
  84. $this->assertNotEmpty($connection->fetchRow($select));
  85. $configModel->deleteConfig($testPath, 'default', 0);
  86. $this->assertEmpty($connection->fetchRow($select));
  87. }
  88. }