/** * Tests the getEncryptionKeyValue() method. * * @covers ::getEncryptionKeyValue * * @dataProvider encryptionKeyValueDataProvider */ public function testGetEncryptionKeyValue($key, $valid_key) { // Set up expectations for the encryption manager. $this->encryptManager->expects($this->any())->method('createInstance')->with($this->equalTo('test_encryption_method'))->will($this->returnValue($this->encryptionMethod)); // Set up a mock for the EncryptService class to mock some methods. $encrypt_service = $this->getMockBuilder('\\Drupal\\encrypt\\EncryptService')->setMethods(['getEncryptionMethod', 'loadEncryptionProfileKey'])->setConstructorArgs(array($this->encryptManager, $this->keyRepository))->getMock(); // Set up expectations for encryption method. if ($valid_key) { $this->encryptionMethod->expects($this->once())->method('encrypt')->will($this->returnValue("encrypted_text")); $this->encryptionMethod->expects($this->once())->method('checkDependencies')->will($this->returnValue(array())); $encrypt_service->expects($this->once())->method('getEncryptionMethod')->will($this->returnValue($this->encryptionMethod)); } else { $this->encryptionMethod->expects($this->never())->method('encrypt'); $this->encryptionMethod->expects($this->once())->method('checkDependencies')->will($this->returnValue(array("Dependency error"))); $this->setExpectedException('\\Drupal\\encrypt\\Exception\\EncryptException'); $encrypt_service->expects($this->never())->method('getEncryptionMethod'); } // Set up expectation for handling of encryption profile by // the getEncryptionKeyValue() method. $this->encryptionProfile->expects($this->once())->method('getEncryptionMethod')->will($this->returnValue("test_encryption_method")); $encrypt_service->expects($this->once())->method('loadEncryptionProfileKey')->with($this->encryptionProfile)->will($this->returnValue($key)); // Call getEncryptionKeyValue() through the public encrypt() method. $encrypted_text = $encrypt_service->encrypt("text_to_encrypt", $this->encryptionProfile); if ($valid_key) { $this->assertEquals("encrypted_text", $encrypted_text); } }
/** * Tests the encrypt & decrypt method. * * @covers ::__construct * @covers ::encrypt * @covers ::decrypt * @covers ::validate * * @dataProvider encryptionDataProvider */ public function testEncryptDecrypt($key, $valid_key) { // Set up expectations for Key. $this->key->expects($this->any())->method('getKeyValue')->will($this->returnValue($key)); if ($valid_key) { // Set up expectations for encryption method. $this->encryptionMethod->expects($this->once())->method('encrypt')->will($this->returnValue("encrypted_text")); $this->encryptionMethod->expects($this->once())->method('decrypt')->will($this->returnValue("decrypted_text")); // Set up expectations for encryption profile. $this->encryptionProfile->expects($this->any())->method('getEncryptionKey')->will($this->returnValue($this->key)); $this->encryptionProfile->expects($this->any())->method('getEncryptionMethod')->will($this->returnValue($this->encryptionMethod)); $this->encryptionProfile->expects($this->any())->method('validate')->will($this->returnValue(array())); } else { // Set up expectations for encryption profile. $this->encryptionProfile->expects($this->never())->method('getEncryptionKey'); $this->encryptionProfile->expects($this->never())->method('getEncryptionMethod'); $this->encryptionProfile->expects($this->any())->method('validate')->will($this->returnValue(array("Validation error"))); $this->setExpectedException('\\Drupal\\encrypt\\Exception\\EncryptException'); } $service = new EncryptService($this->encryptManager, $this->keyRepository); $encrypted_text = $service->encrypt("text_to_encrypt", $this->encryptionProfile); $decrypted_text = $service->decrypt("text_to_decrypt", $this->encryptionProfile); if ($valid_key) { $this->assertEquals("encrypted_text", $encrypted_text); $this->assertEquals("decrypted_text", $decrypted_text); } }
/** * Determines whether the input is valid for encryption / decryption. * * @param string $text * The text to encrypt / decrypt. * @param \Drupal\encrypt\Entity\EncryptionProfile $encryption_profile * The encryption profile to validate. * * @return bool * Whether the encryption profile validated correctly. * * @throws \Drupal\encrypt\Exception\EncryptException * Error with validation failures. */ protected function validate($text, EncryptionProfile $encryption_profile) { $errors = $encryption_profile->validate($text); if (!empty($errors)) { // Throw an exception with the errors from the encryption method. throw new EncryptException(implode('; ', $errors)); } return TRUE; }
/** * Update the EncryptionMethod plugin. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function updateEncryptionMethod(FormStateInterface $form_state) { /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; /* @var $plugin \Drupal\encrypt\EncryptionMethodInterface */ $plugin = $encryption_profile->getEncryptionMethod(); $encryption_profile->setEncryptionMethod($plugin); // If an original profile exists and the plugin ID matches the existing one. if ($this->originalProfile && $this->originalProfile->getEncryptionMethod()->getPluginId() == $plugin->getPluginId()) { // Use the configuration from the original profile's plugin. $configuration = $this->originalProfile->getEncryptionMethod()->getConfiguration(); } else { // Use the plugin's default configuration. $configuration = $plugin->defaultConfiguration(); } $plugin->setConfiguration($configuration); $form_state->setValue('encryption_method_configuration', []); $form_state->getUserInput()['encryption_method_configuration'] = []; }
/** * 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(); }
/** * Tests the setEncryptionKey method. * * @covers ::setEncryptionKey */ public function testSetEncryptionKey() { $encryption_profile = new EncryptionProfile([], 'encryption_profile'); // Set up expectations for key entity. $this->key->expects($this->any())->method('id')->will($this->returnValue('test_key')); $encryption_profile->setEncryptionKey($this->key); $this->assertEquals("test_key", $encryption_profile->getEncryptionKeyId()); }
/** * 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(); }