View source
<?php
namespace Drupal\Tests\encrypt\Functional;
class EncryptTest extends EncryptTestBase {
public function testEncryptAndDecrypt() {
$this
->drupalGet('admin/config/system/encryption/profiles/add');
$this
->assertOption('edit-encryption-method', 'test_encryption_method');
$this
->assertText('Test Encryption method');
$edit = [
'encryption_method' => 'test_encryption_method',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$edit = [
'id' => 'test_encryption_profile',
'label' => 'Test encryption profile',
'encryption_method' => 'test_encryption_method',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$encryption_profile = \Drupal::service('entity_type.manager')
->getStorage('encryption_profile')
->load('test_encryption_profile');
$this
->assertNotEmpty($encryption_profile, 'Encryption profile was successfully saved.');
$test_string = 'testing 123 &*#';
$enc_string = \Drupal::service('encryption')
->encrypt($test_string, $encryption_profile);
$this
->assertEquals('zhfgorfvkgrraovggrfgvat 123 &*#', $enc_string, 'The encryption service is not properly processing');
$dec_string = \Drupal::service('encryption')
->decrypt($enc_string, $encryption_profile);
$this
->assertEquals($test_string, $dec_string, 'The decryption service is not properly processing');
}
public function testProfileValidation() {
$this
->drupalGet('admin/config/system/encryption/profiles/add');
$this
->assertOption('edit-encryption-method', 'test_encryption_method');
$this
->assertText('Test Encryption method');
$edit = [
'encryption_method' => 'test_encryption_method',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$edit = [
'id' => 'test_encryption_profile',
'label' => 'Test encryption profile',
'encryption_method' => 'test_encryption_method',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_encryption_profile');
$this
->assertText('Be extremely careful when editing an encryption profile! It may result in making data encrypted with this profile unreadable. Are you sure you want to edit this profile?');
$this
->assertNoFieldByName('encryption_method', NULL);
$this
->assertNoFieldByName('encryption_key', NULL);
$this
->drupalPostForm(NULL, [], 'Edit');
$this
->assertNoText('Be extremely careful when editing an encryption profile! It may result in making data encrypted with this profile unreadable. Are you sure you want to edit this profile?');
$this
->assertFieldByName('encryption_method', NULL);
$this
->assertFieldByName('encryption_key', NULL);
$this
->drupalGet('admin/config/system/encryption/profiles');
$this
->assertText('Key 128 bit');
$this
->drupalGet('admin/config/system/keys');
$this
->clickLink('Delete');
$this
->assertText('Encryption Profile');
$this
->assertText('Test encryption profile');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->drupalGet('admin/config/system/encryption/profiles');
$this
->assertNoText('Key 128 bit');
$this
->config('encrypt.settings')
->set('check_profile_status', FALSE)
->save();
$this
->drupalGet('admin/config/system/encryption/profiles');
$this
->assertNoText('The key linked to this encryption profile does not exist.');
}
public function testEncryptionMethodConfig() {
$this
->drupalGet('admin/config/system/encryption/profiles/add');
$this
->assertOption('edit-encryption-method', 'config_test_encryption_method');
$this
->assertText('Config Test Encryption method');
$edit = [
'label' => 'Test',
'id' => 'test_encryption_profile',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
'encryption_method' => 'test_encryption_method',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertText('Saved the Test encryption profile.');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_encryption_profile');
$this
->drupalPostForm(NULL, [], 'Edit');
$this
->assertNoFieldByName('encryption_method_configuration[mode]', NULL);
$this
->drupalGet('admin/config/system/encryption/profiles/add');
$edit = [
'label' => 'Test 2',
'id' => 'test_encryption_profile_2',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
'encryption_method' => 'config_test_encryption_method',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertText('Saved the Test 2 encryption profile.');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_encryption_profile_2');
$this
->drupalPostForm(NULL, [], 'Edit');
$this
->assertFieldByName('encryption_method_configuration[mode]', NULL);
$this
->assertOptionByText('encryption_method_configuration[mode]', 'CBC');
$this
->drupalGet('admin/config/system/encryption/profiles/add');
$edit = [
'id' => 'test_config_encryption_profile',
'label' => 'Test encryption profile',
'encryption_method' => 'config_test_encryption_method',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_config_encryption_profile');
$this
->drupalPostForm(NULL, [], 'Edit');
$edit = [
'encryption_method_configuration[mode]' => 'CBC',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$encryption_profile = \Drupal::service('entity_type.manager')
->getStorage('encryption_profile')
->load('test_config_encryption_profile');
$this
->assertNotEmpty($encryption_profile, 'Encryption profile was successfully saved');
$encryption_method = $encryption_profile
->getEncryptionMethod();
$encryption_method_config = $encryption_method
->getConfiguration();
$this
->assertEquals([
'mode' => 'CBC',
], $encryption_method_config, 'Encryption method config correctly saved');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_config_encryption_profile');
$this
->drupalPostForm(NULL, [], 'Edit');
$edit = [
'encryption_method' => 'test_encryption_method',
'encryption_key' => $this->testKeys['testing_key_128']
->id(),
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/config/system/encryption/profiles/manage/test_config_encryption_profile');
$this
->drupalPostForm(NULL, [], 'Edit');
$this
->assertNoFieldByName('encryption_method_configuration[mode]', NULL);
}
}