You are here

public function FieldEncryptTest::testEncryptField in Field Encryption 8.2

Test encrypting fields.

This test also covers changing field encryption settings when existing data already exists, as well as making fields unencrypted again with data unencryption support.

File

tests/src/Functional/FieldEncryptTest.php, line 25

Class

FieldEncryptTest
Tests field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEncryptField() {
  $this
    ->setFieldStorageSettings(TRUE);

  // Save test entity.
  $this
    ->createTestNode();
  $fields = $this->testNode
    ->getFields();

  // Check field_test_single settings.
  $single_field = $fields['field_test_single'];
  $definition = $single_field
    ->getFieldDefinition();
  $this
    ->assertTrue($definition instanceof FieldDefinitionInterface);

  /** @var \Drupal\Core\Field\FieldConfigInterface $storage */
  $storage = $definition
    ->getFieldStorageDefinition();
  $this
    ->assertEquals(TRUE, $storage
    ->getThirdPartySetting('field_encrypt', 'encrypt', FALSE));
  $this
    ->assertEquals([
    'value' => 'value',
    'summary' => 'summary',
  ], array_filter($storage
    ->getThirdPartySetting('field_encrypt', 'properties', [])));
  $this
    ->assertEquals('encryption_profile_1', $storage
    ->getThirdPartySetting('field_encrypt', 'encryption_profile', ''));

  // Check field_test_multi settings.
  $single_field = $fields['field_test_multi'];
  $definition = $single_field
    ->getFieldDefinition();
  $this
    ->assertTrue($definition instanceof FieldDefinitionInterface);

  /** @var \Drupal\Core\Field\FieldConfigInterface $storage */
  $storage = $definition
    ->getFieldStorageDefinition();
  $this
    ->assertEquals(TRUE, $storage
    ->getThirdPartySetting('field_encrypt', 'encrypt', FALSE));
  $this
    ->assertEquals([
    'value' => 'value',
  ], array_filter($storage
    ->getThirdPartySetting('field_encrypt', 'properties', [])));
  $this
    ->assertEquals('encryption_profile_2', $storage
    ->getThirdPartySetting('field_encrypt', 'encryption_profile', ''));

  // Check existence of EncryptedFieldValue entities.
  $encrypted_field_values = EncryptedFieldValue::loadMultiple();
  $this
    ->assertEqual(5, count($encrypted_field_values));

  // Check if text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText("Lorem ipsum dolor sit amet.");
  $this
    ->assertText("one");
  $this
    ->assertText("two");
  $this
    ->assertText("three");
  $result = \Drupal::database()
    ->query("SELECT field_test_single_value FROM {node__field_test_single} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchField();
  $this
    ->assertEqual("[ENCRYPTED]", $result);
  $result = \Drupal::database()
    ->query("SELECT field_test_multi_value FROM {node__field_test_multi} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchAll();
  foreach ($result as $record) {
    $this
      ->assertEquals("[ENCRYPTED]", $record->field_test_multi_value);
  }

  // Test updating entities with alternative encryption settings.
  $this
    ->setFieldStorageSettings(TRUE, TRUE);

  // Update existing data with new field encryption settings.
  $this
    ->assertLinkByHref('admin/config/system/field-encrypt/field-update');
  $this
    ->drupalGet('admin/config/system/field-encrypt/field-update');
  $this
    ->assertText('There are 2 fields queued for encryption updates.');
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/config/system/field-encrypt/field-update');
  $this
    ->assertText('There are 0 fields queued for encryption updates.');

  // Check existence of EncryptedFieldValue entities.
  $encrypted_field_values = EncryptedFieldValue::loadMultiple();
  $this
    ->assertEqual(5, count($encrypted_field_values));

  // Check if text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText("Lorem ipsum dolor sit amet.");
  $this
    ->assertText("one");
  $this
    ->assertText("two");
  $this
    ->assertText("three");

  // Check values saved in the database.
  $result = \Drupal::database()
    ->query("SELECT field_test_single_value FROM {node__field_test_single} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchField();
  $this
    ->assertEquals("[ENCRYPTED]", $result);
  $result = \Drupal::database()
    ->query("SELECT field_test_multi_value FROM {node__field_test_multi} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchAll();
  foreach ($result as $record) {
    $this
      ->assertEquals("[ENCRYPTED]", $record->field_test_multi_value);
  }

  // Test updating entities to remove field encryption.
  $this
    ->setFieldStorageSettings(FALSE);

  // Update existing data with new field encryption settings.
  $this
    ->assertLinkByHref('admin/config/system/field-encrypt/field-update');
  $this
    ->drupalGet('admin/config/system/field-encrypt/field-update');
  $this
    ->assertText('There are 2 fields queued for encryption updates.');
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/config/system/field-encrypt/field-update');
  $this
    ->assertText('There are 0 fields queued for encryption updates.');

  // Check removal of EncryptedFieldValue entities.
  $encrypted_field_values = EncryptedFieldValue::loadMultiple();
  $this
    ->assertEquals(0, count($encrypted_field_values));

  // Check if text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText("Lorem ipsum dolor sit amet.");
  $this
    ->assertText("one");
  $this
    ->assertText("two");
  $this
    ->assertText("three");
  $result = \Drupal::database()
    ->query("SELECT field_test_single_value FROM {node__field_test_single} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchField();
  $this
    ->assertEquals("Lorem ipsum dolor sit amet.", $result);
  $result = \Drupal::database()
    ->query("SELECT field_test_multi_value FROM {node__field_test_multi} WHERE entity_id = :entity_id", [
    ':entity_id' => $this->testNode
      ->id(),
  ])
    ->fetchAll();
  $valid_values = [
    "one",
    "two",
    "three",
  ];
  foreach ($result as $record) {
    $this
      ->assertTrue(in_array($record->field_test_multi_value, $valid_values));
  }
}