You are here

public function ConfigurableFieldTest::testEncryptFieldNormal in Field Encryption 3.0.x

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 decryption support.

File

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

Class

ConfigurableFieldTest
Tests field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEncryptFieldNormal() {
  $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', [])));

  // 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', [])));

  // Check if text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertSession()
    ->pageTextContains("Lorem ipsum dolor sit amet.");
  $this
    ->assertSession()
    ->pageTextContains("one");
  $this
    ->assertSession()
    ->pageTextContains("two");
  $this
    ->assertSession()
    ->pageTextContains("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(ProcessEntities::ENCRYPTED_VALUE, $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(ProcessEntities::ENCRYPTED_VALUE, $record->field_test_multi_value);
  }

  // Change default encryption profile and ensure the entity can still be
  // decrypted.
  $this
    ->config('field_encrypt.settings')
    ->set('encryption_profile', 'encryption_profile_2')
    ->save();
  $this
    ->resetAll();

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

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

  // Update existing data with new field encryption settings.
  $this
    ->assertSession()
    ->linkByHrefExists('admin/config/system/field-encrypt/process-queues');
  $this
    ->drupalGet('admin/config/system/field-encrypt/process-queues');
  $this
    ->assertSession()
    ->pageTextContains('There are 2 entities queued for updating to use the latest field encryption settings.');
  $this
    ->assertTrue(\Drupal::database()
    ->schema()
    ->fieldExists('node_field_data', ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__value'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Process updates');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('There are 0 entities queued for updating to use the latest field encryption settings.');

  // Check if text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertSession()
    ->pageTextContains("Lorem ipsum dolor sit amet.");
  $this
    ->assertSession()
    ->pageTextContains("one");
  $this
    ->assertSession()
    ->pageTextContains("two");
  $this
    ->assertSession()
    ->pageTextContains("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));
  }
  $this
    ->assertFalse(\Drupal::database()
    ->schema()
    ->fieldExists('node_field_data', ProcessEntities::ENCRYPTED_FIELD_STORAGE_NAME . '__value'));

  // Ensure state is cleaned up on uninstall.
  $this
    ->assertSame([], \Drupal::state()
    ->get('field_encrypt.entity_types'));
  \Drupal::service('module_installer')
    ->uninstall([
    'field_encrypt',
  ]);
  $this
    ->assertSame(NULL, \Drupal::state()
    ->get('field_encrypt.entity_types'));
}