You are here

public function ConfigTranslationCacheTest::testFieldConfigTranslation in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/config_translation/tests/src/Functional/ConfigTranslationCacheTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationCacheTest::testFieldConfigTranslation()
  2. 10 core/modules/config_translation/tests/src/Functional/ConfigTranslationCacheTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationCacheTest::testFieldConfigTranslation()

Tests the translation of field and field storage configuration.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationCacheTest.php, line 119

Class

ConfigTranslationCacheTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testFieldConfigTranslation() {

  // Add a test field which has a translatable field setting and a
  // translatable field storage setting.
  $field_name = strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ]);
  $translatable_storage_setting = $this
    ->randomString();
  $field_storage
    ->setSetting('translatable_storage_setting', $translatable_storage_setting);
  $field_storage
    ->save();
  $bundle = strtolower($this
    ->randomMachineName());
  entity_test_create_bundle($bundle);
  $field = FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => $bundle,
  ]);
  $translatable_field_setting = $this
    ->randomString();
  $field
    ->setSetting('translatable_field_setting', $translatable_field_setting);
  $field
    ->save();
  $this
    ->drupalLogin($this->translatorUser);
  $this
    ->drupalGet("/entity_test/structure/{$bundle}/fields/entity_test.{$bundle}.{$field_name}/translate");
  $this
    ->clickLink('Add');
  $this
    ->assertSession()
    ->pageTextContains('Translatable field setting');
  $this
    ->assertSession()
    ->assertEscaped($translatable_field_setting);
  $this
    ->assertSession()
    ->pageTextContains('Translatable storage setting');
  $this
    ->assertSession()
    ->assertEscaped($translatable_storage_setting);

  // Add translation for label.
  $field_label_fr = $this
    ->randomString();
  $edit = [
    "translation[config_names][field.field.entity_test.{$bundle}.{$field_name}][label]" => $field_label_fr,
  ];
  $this
    ->submitForm($edit, 'Save translation');
  $this
    ->drupalLogout();

  // Check if the translated label appears.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet("/fr/entity_test/structure/{$bundle}/fields");
  $this
    ->assertSession()
    ->assertEscaped($field_label_fr);

  // Clear cache on French version and check for translated label.
  $this
    ->drupalGet('/fr/admin/config/development/performance');
  $this
    ->submitForm([], 'Clear all caches');
  $this
    ->drupalGet("/fr/entity_test/structure/{$bundle}/fields");

  // Check if the translation is still there.
  $this
    ->assertSession()
    ->assertEscaped($field_label_fr);

  // Clear cache on default version and check for translated label.
  $this
    ->drupalGet('/admin/config/development/performance');
  $this
    ->submitForm([], 'Clear all caches');
  $this
    ->drupalGet("/fr/entity_test/structure/{$bundle}/fields");

  // Check if the translation is still there.
  $this
    ->assertSession()
    ->assertEscaped($field_label_fr);
}