You are here

public function FieldTranslationSqlStorageTest::testFieldSqlStorage in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::testFieldSqlStorage()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::testFieldSqlStorage()

Tests field SQL storage.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php, line 19

Class

FieldTranslationSqlStorageTest
Tests Field translation SQL Storage.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldSqlStorage() {
  $entity_type = 'entity_test_mul';
  $controller = $this->entityTypeManager
    ->getStorage($entity_type);
  $values = [
    $this->fieldName => $this
      ->randomMachineName(),
    $this->untranslatableFieldName => $this
      ->randomMachineName(),
  ];
  $entity = $controller
    ->create($values);
  $entity
    ->save();

  // Tests that when changing language field language codes are still correct.
  $langcode = $this->langcodes[0];
  $entity->langcode->value = $langcode;
  $entity
    ->save();
  $this
    ->assertFieldStorageLangcode($entity, 'Field language successfully changed from language neutral.');
  $langcode = $this->langcodes[1];
  $entity->langcode->value = $langcode;
  $entity
    ->save();
  $this
    ->assertFieldStorageLangcode($entity, 'Field language successfully changed.');
  $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  $entity->langcode->value = $langcode;
  $entity
    ->save();
  $this
    ->assertFieldStorageLangcode($entity, 'Field language successfully changed to language neutral.');

  // Test that after switching field translatability things keep working as
  // before.
  $this
    ->toggleFieldTranslatability($entity_type, $entity_type);
  $entity = $this
    ->reloadEntity($entity);
  foreach ([
    $this->fieldName,
    $this->untranslatableFieldName,
  ] as $field_name) {
    $this
      ->assertEqual($entity
      ->get($field_name)->value, $values[$field_name], 'Field language works as expected after switching translatability.');
  }

  // Test that after disabling field translatability translated values are not
  // loaded.
  $this
    ->toggleFieldTranslatability($entity_type, $entity_type);
  $entity = $this
    ->reloadEntity($entity);
  $entity->langcode->value = $this->langcodes[0];
  $translation = $entity
    ->addTranslation($this->langcodes[1]);
  $translated_value = $this
    ->randomMachineName();
  $translation
    ->get($this->fieldName)->value = $translated_value;
  $translation
    ->save();
  $this
    ->toggleFieldTranslatability($entity_type, $entity_type);
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEqual($entity
    ->getTranslation($this->langcodes[1])
    ->get($this->fieldName)->value, $values[$this->fieldName], 'Existing field translations are not loaded for untranslatable fields.');
}