/** * Tests loadEncryptionMethods method. * * @covers ::__construct * @covers ::loadEncryptionMethods */ public function testLoadEncryptionMethods() { $definitions = array('test_encryption_method' => array('id' => 'test_encryption_method', 'title' => "Test encryption method")); $this->encryptManager->expects($this->once())->method('getDefinitions')->will($this->returnValue($definitions)); $service = new EncryptService($this->encryptManager, $this->keyRepository); $methods = $service->loadEncryptionMethods(); $this->assertEquals(['test_encryption_method'], array_keys($methods)); }
/** * {@inheritdoc} */ function decrypt($text) { // Get settings. $settings = $this->config->get('encrypt.settings'); // Load the key. $key = $this->key->getKey($settings->get('encryption_key')); // Load the encryption method. $enc_method = $this->manager->createInstance($settings->get('encryption_method')); // Return the encrypted string. return $enc_method->decrypt($text, $key->getKeyValue()); }
/** * 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); } }
/** * Get the used encryption key. * * @param string $text * The text to encrypt / decrypt. * @param \Drupal\encrypt\Entity\EncryptionProfile $encryption_profile * The encryption profile to use. * * @return string * The encryption key value. * * @throws \Drupal\encrypt\Exception\EncryptException * Can throw an EncryptException. */ protected function getEncryptionKeyValue($text, EncryptionProfile $encryption_profile) { // Load the encryption method. $enc_method = $encryption_profile->getEncryptionMethod(); $this->encryptionMethod = $this->encryptManager->createInstance($enc_method); // Load the encryption key. $key_value = $this->loadEncryptionProfileKey($encryption_profile); // Check for missing dependencies. $errors = $this->encryptionMethod->checkDependencies($text, $key_value); if (!empty($errors)) { // Throw an exception with the errors from the encryption method. throw new EncryptException(implode('; ', $errors)); } else { return $key_value; } }
/** * {@inheritdoc} */ public function loadEncryptionMethods() { return $this->encryptManager->getDefinitions(); }
/** * Loads an encryption profile method. * @param \Drupal\encrypt\Entity\EncryptionProfile $enc_profile */ private function loadEncryptionMethod($enc_profile) { // Load the encryption method. $enc_method = $enc_profile->getEncryptionMethod(); return $this->encryptManager->createInstance($enc_method); }