EncryptionTest.php in Ubercart 8.4
File
uc_store/tests/src/Functional/EncryptionTest.php
View source
<?php
namespace Drupal\Tests\uc_store\Functional;
use Drupal\Tests\BrowserTestBase;
class EncryptionTest extends BrowserTestBase {
protected static $modules = [
'uc_store',
];
protected $crypt;
protected function setUp() {
parent::setUp();
$this->crypt = \Drupal::service('uc_store.encryption');
}
public function testEncryptionService() {
$key = $this
->randomMachineName(16);
$this->crypt
->setCypher('ubbi-dubbi');
$errors = $this->crypt
->getErrors();
if (!empty($errors)) {
$this
->assertTrue(TRUE, 'Tried to use invalid cypher.');
$this
->assertEquals('ubbi-dubbi is not a valid cypher', $errors[0], 'Invalid cypher error message found.');
}
$this->crypt
->setCypher('aes-128-cbc');
$errors = $this->crypt
->getErrors();
if (empty($errors)) {
$this
->assertTrue(TRUE, 'AES-128-CBC cypher found.');
}
$plaintext = 'The quick brown fox jumps over the lazy dog.';
$cyphertext = $this->crypt
->encrypt($key, $plaintext);
$errors = $this->crypt
->getErrors();
$this
->assertEmpty($errors, 'Encryption successful.');
$decrypted = $this->crypt
->decrypt($key, $cyphertext);
$errors = $this->crypt
->getErrors();
$this
->assertEmpty($errors, 'Decryption successful.');
$this
->assertEquals($plaintext, $decrypted, 'Decrypted text is the same as initial plaintext.');
}
}