You are here

public function FieldEncryptTest::testEncryptFieldTranslation in Field Encryption 8.2

Test encrypting fields with translations.

File

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

Class

FieldEncryptTest
Tests field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

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

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

  // Reload node after saving.
  $controller = $this->entityTypeManager
    ->getStorage($this->testNode
    ->getEntityTypeId());
  $controller
    ->resetCache([
    $this->testNode
      ->id(),
  ]);
  $this->testNode = $controller
    ->load($this->testNode
    ->id());

  // Add translated values.
  $translated_values = [
    'title' => $this
      ->randomMachineName(8),
    'field_test_single' => [
      [
        'value' => "Ceci est un text francais.",
        'summary' => "Text francais",
        'format' => filter_default_format(),
      ],
    ],
    'field_test_multi' => [
      [
        'value' => "un",
      ],
      [
        'value' => "deux",
      ],
      [
        'value' => "trois",
      ],
    ],
  ];
  $this->testNode
    ->addTranslation('fr', $translated_values);
  $this->testNode
    ->save();

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

  // Check if English 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 if French text is displayed unencrypted.
  $this
    ->drupalGet('fr/node/' . $this->testNode
    ->id());
  $this
    ->assertText("Ceci est un text francais.");
  $this
    ->assertText("un");
  $this
    ->assertText("deux");
  $this
    ->assertText("trois");

  // 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(),
  ])
    ->fetchAll();
  foreach ($result as $record) {
    $this
      ->assertEquals("[ENCRYPTED]", $record->field_test_single_value);
  }
  $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);
  }
}