/** * Assert that key entity getters work. * * @group key */ public function testGetters() { // Create a key entity using Configuration key provider. $values = ['key_id' => $this->getRandomGenerator()->word(15), 'key_provider' => 'config', 'key_settings' => $this->key_settings]; $key = new Key($values, 'key'); $this->assertEquals($values['key_provider'], $key->getKeyProvider()); $this->assertEquals($values['key_settings'], $key->getKeySettings()); $this->assertEquals($values['key_settings']['key_value'], $key->getKeyValue()); }
/** * 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); } }
/** * 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()); }
/** * 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_settings', $defaults); $keys = $this->keyManager->getKeysByStorageMethod('config'); $this->assertEquals([$this->key_id => $this->key], $keys); }
/** * Tests validation of encryption profiles. */ public function testProfileValidation() { // Create an encryption profile config entity. $this->drupalGet('admin/config/system/encryption/profiles/add'); // Check if the plugin exists. $this->assertOption('edit-encryption-method', 'test_encryption_method', t('Encryption method option is present.')); $this->assertText('Test Encryption method', t('Encryption method text is present')); $edit = ['encryption_method' => 'test_encryption_method']; $this->drupalPostAjaxForm(NULL, $edit, 'encryption_method'); $edit = ['id' => 'test_encryption_profile', 'label' => 'Test encryption profile', 'encryption_method' => 'test_encryption_method', 'encryption_key' => 'testing_key']; $this->drupalPostForm('admin/config/system/encryption/profiles/add', $edit, t('Save')); // Now delete the testkey. $this->testKey->delete(); // Check if the error message is shown. $this->drupalGet('admin/config/system/encryption/profiles'); $this->assertText('The key linked to this encryption profile does not exist.'); // Test "check_profile_status" setting. $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.'); }
/** * {@inheritdoc} */ public function setEncryptionKey(Key $key) { $this->encryption_key_entity = $key; $this->encryption_key = $key->id(); }