You are here

public function BaseFieldTest::testEncryptFieldTranslation in Field Encryption 3.0.x

Test encrypting fields with translations.

File

tests/src/Functional/BaseFieldTest.php, line 232

Class

BaseFieldTest
Tests base field encryption.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEncryptFieldTranslation() {

  // Set up extra language.
  ConfigurableLanguage::createFromLangcode('fr')
    ->save();

  // Enable translation for the current entity type and ensure the change is
  // picked up.
  \Drupal::service('content_translation.manager')
    ->setEnabled('node', 'page', TRUE);
  drupal_static_reset();
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  \Drupal::service('router.builder')
    ->rebuild();
  $this
    ->rebuildContainer();
  $this
    ->setFieldStorageSettings(TRUE);

  // Save test entity.
  $test_node = $this
    ->createNode([
    'title' => 'Test title en',
  ]);

  // Reload node after saving.
  $controller = \Drupal::entityTypeManager()
    ->getStorage($test_node
    ->getEntityTypeId());
  $controller
    ->resetCache([
    $test_node
      ->id(),
  ]);
  $test_node = $controller
    ->load($test_node
    ->id());

  // Add translated values.
  $test_node
    ->addTranslation('fr', [
    'title' => "test title fr",
  ]);
  $test_node
    ->save();

  // Check if English text is displayed unencrypted.
  $this
    ->drupalGet('node/' . $test_node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains("Test title en");

  // Check if French text is displayed unencrypted.
  $this
    ->drupalGet('fr/node/' . $test_node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains("Test title fr");

  // Check values saved in the database.
  $result = \Drupal::database()
    ->query("SELECT title FROM {node_field_data} WHERE nid = :entity_id", [
    ':entity_id' => $test_node
      ->id(),
  ])
    ->fetchAll();
  $this
    ->assertCount(2, $result);
  foreach ($result as $record) {
    $this
      ->assertEquals(ProcessEntities::ENCRYPTED_VALUE, $record->title);
  }
}