You are here

public function FieldEncryptTest::testEncryptFieldRevision in Field Encryption 8.2

Test encrypting fields with revisions.

This test also covers deletion of an encrypted field with existing data.

File

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

Class

FieldEncryptTest
Tests field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

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

  // Save test entity.
  $this
    ->createTestNode();

  // Create a new revision for the entity.
  $old_revision_id = $this->testNode
    ->getRevisionId();
  $this->testNode
    ->setNewRevision(TRUE);
  $this->testNode->field_test_single->value = "Lorem ipsum dolor sit amet revisioned.";
  $this->testNode->field_test_single->summary = "Lorem ipsum revisioned.";
  $multi_field = $this->testNode
    ->get('field_test_multi');
  $multi_field_value = $multi_field
    ->getValue();
  $multi_field_value[0]['value'] = "four";
  $multi_field_value[1]['value'] = "five";
  $multi_field_value[2]['value'] = "six";
  $multi_field
    ->setValue($multi_field_value);
  $this->testNode
    ->save();

  // Ensure that the node revision has been created.
  $this->entityTypeManager
    ->getStorage('node')
    ->resetCache([
    $this->testNode
      ->id(),
  ]);
  $this
    ->assertNotIdentical($this->testNode
    ->getRevisionId(), $old_revision_id, 'A new revision has been created.');

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

  // Check if revisioned text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText("Lorem ipsum dolor sit amet revisioned.");
  $this
    ->assertText("four");
  $this
    ->assertText("five");
  $this
    ->assertText("six");

  // Check if original text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id() . '/revisions/' . $old_revision_id . '/view');
  $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_revision__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_revision__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);
  }
  $this
    ->drupalPostForm('admin/structure/types/manage/page/fields/node.page.field_test_multi/delete', [], 'Delete');

  // Test if EncryptedFieldValue entities got deleted.
  $encrypted_field_values = EncryptedFieldValue::loadMultiple();
  $this
    ->assertEquals(4, count($encrypted_field_values));
}