EncryptionServiceTest.php in Encryption 8
File
tests/src/Kernel/EncryptionServiceTest.php
View source
<?php
namespace Drupal\Tests\encryption\Kernel;
use Drupal\encryption\EncryptionServiceInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Core\Site\Settings;
class EncryptionServiceTest extends KernelTestBase {
public static $modules = [
'encryption',
];
public function setUp() {
parent::setUp();
$settings = Settings::getAll();
new Settings($settings + [
'encryption_key' => base64_encode(random_bytes(32)),
]);
}
public function testEncryptionService() {
$super_secret_string = 'Big time secrets!';
$encryption_service = \Drupal::service('encryption');
$encrypted_value = $encryption_service
->encrypt($super_secret_string);
$decrypted_value = $encryption_service
->decrypt($encrypted_value);
self::assertTrue($encryption_service instanceof EncryptionServiceInterface);
self::assertNotEquals($encrypted_value, $super_secret_string);
self::assertEquals($super_secret_string, $decrypted_value);
$null_result = $encryption_service
->decrypt(null);
self::assertNull($null_result);
$raw_encrypted_value = $encryption_service
->encrypt($super_secret_string, TRUE);
$raw_decrypted_value = $encryption_service
->decrypt($raw_encrypted_value, TRUE);
self::assertNotEquals($raw_encrypted_value, $super_secret_string);
self::assertNotEquals($raw_encrypted_value, $encrypted_value);
self::assertEquals($super_secret_string, $raw_decrypted_value);
}
}