/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $keys = $this->key_repository->getKeys(); if (empty($keys)) { drupal_set_message('No system keys (admin/config/system/key) are installed to manage encryption profiles.'); } /** @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $encryption_profile->label(), '#description' => $this->t("Label for the encryption profile."), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $encryption_profile->id(), '#machine_name' => array('exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load'), '#disabled' => !$encryption_profile->isNew()); /** @var $key \Drupal\key\Entity\KeyInterface */ foreach ($keys as $key) { $key_id = $key->id(); $key_title = $key->label(); $keys[$key_id] = (string) $key_title; } if ($profile_key = $encryption_profile->getEncryptionKey()) { $default_key = $profile_key; } $form['encryption_key'] = array('#type' => 'select', '#title' => $this->t('Encryption Key'), '#description' => $this->t('Select the key used for encryption.'), '#options' => $keys, '#default_value' => empty($default_key) ? NULL : $default_key, '#required' => TRUE); $enc_methods = []; foreach ($this->encrypt_service->loadEncryptionMethods() as $plugin_id => $definition) { $enc_methods[$plugin_id] = (string) $definition['title']; } $form['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $enc_methods, '#default_value' => $encryption_profile->getEncryptionMethod()); return $form; }
/** * Tests the getEncryptionKey method. * * @covers ::getEncryptionKey */ public function testGetEncryptionKey() { // Set up a mock for the EncryptionProfile class to mock some methods. $encryption_profile = $this->getMockBuilder('\\Drupal\\encrypt\\Entity\\EncryptionProfile')->setMethods(['getKeyRepository', 'getEncryptionKeyId'])->disableOriginalConstructor()->getMock(); $this->keyRepository->expects($this->any())->method('getKey')->with($this->equalTo('test_key'))->will($this->returnValue($this->key)); $encryption_profile->expects($this->any())->method('getKeyRepository')->will($this->returnValue($this->keyRepository)); $encryption_profile->expects($this->any())->method('getEncryptionKeyId')->will($this->returnValue('test_key')); $result = $encryption_profile->getEncryptionKey(); $this->assertTrue($result instanceof KeyInterface); }
/** * Test get keys by storage method. * * @group key */ public function testGetKeysByStorageMethod() { // Create a key provider plugin to play with. $defaults = ['key_value' => $this->createToken()]; $definition = ['id' => 'config', 'class' => 'Drupal\\key\\Plugin\\KeyProvider\\ConfigKeyProvider', 'title' => 'Configuration']; $KeyProvider = new ConfigKeyProvider($defaults, 'config', $definition); // Mock the loadByProperties method in entity manager. $this->configStorage->expects($this->any())->method('loadByProperties')->with(['key_provider' => 'config'])->willReturn([$this->key_id => $this->key]); // Make the key provider plugin manager return a plugin instance. $this->KeyProviderManager->expects($this->any())->method('createInstance')->with('config', $defaults)->willReturn($KeyProvider); $this->key->set('key_provider_settings', $defaults); $keys = $this->keyRepository->getKeysByStorageMethod('config'); $this->assertEquals([$this->key_id => $this->key], $keys); }
/** * Loads an encryption profile key. * * @param \Drupal\encrypt\Entity\EncryptionProfile $encryption_profile * The encryption profile to use. * * @return string * The encryption key value. */ protected function loadEncryptionProfileKey(EncryptionProfile $encryption_profile) { $key_id = $encryption_profile->getEncryptionKey(); return $this->keyRepository->getKey($key_id)->getKeyValue(); }
/** * Loads an encryption profile key. * @param \Drupal\encrypt\Entity\EncryptionProfile $enc_profile */ private function loadEncryptionProfileKey($enc_profile) { // Load the key. $key_id = $enc_profile->getEncryptionKey(); return $this->key->getKey($key_id)->getKeyValue(); }