You are here

public function ConfigurableLanguageManagerTest::testUserProfileTranslationWithPreferredAdminLanguage in Drupal 9

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

Tests translation of the user profile edit form.

The user profile edit form is a special case when used with the preferred admin language negotiator because of the recursive way that the negotiator is called.

File

core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php, line 204

Class

ConfigurableLanguageManagerTest
Tests Language Negotiation.

Namespace

Drupal\Tests\language\Functional

Code

public function testUserProfileTranslationWithPreferredAdminLanguage() {
  $assert_session = $this
    ->assertSession();

  // Set the interface language to use the preferred administration language.

  /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
  $language_negotiator = \Drupal::getContainer()
    ->get('language_negotiator');
  $language_negotiator
    ->saveConfiguration('language_interface', [
    'language-user-admin' => 1,
    'language-selected' => 2,
  ]);

  // Create a field on the user entity.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $label = mb_strtolower($this
    ->randomMachineName());
  $field_label_en = "English {$label}";
  $field_label_es = "Español {$label}";
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'user',
    'type' => 'string',
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'user',
    'label' => $field_label_en,
  ]);
  $instance
    ->save();

  // Add a Spanish translation.
  \Drupal::languageManager()
    ->getLanguageConfigOverride('es', "field.field.user.user.{$field_name}")
    ->set('label', $field_label_es)
    ->save();

  // Add the new field to the edit form.
  EntityFormDisplay::create([
    'targetEntityType' => 'user',
    'bundle' => 'user',
    'mode' => 'default',
    'status' => TRUE,
  ])
    ->setComponent($field_name, [
    'type' => 'string_textfield',
  ])
    ->save();
  $user_id = \Drupal::currentUser()
    ->id();
  $this
    ->drupalGet("/user/{$user_id}/edit");

  // Admin language choice is "No preference" so we should get the default.
  $assert_session
    ->pageTextContains($field_label_en);
  $assert_session
    ->pageTextNotContains($field_label_es);

  // Set admin language to Spanish.
  $this
    ->submitForm([
    'edit-preferred-admin-langcode' => 'es',
  ], 'edit-submit');
  $assert_session
    ->pageTextContains($field_label_es);
  $assert_session
    ->pageTextNotContains($field_label_en);

  // Set admin language to English.
  $this
    ->submitForm([
    'edit-preferred-admin-langcode' => 'en',
  ], 'edit-submit');
  $assert_session
    ->pageTextContains($field_label_en);
  $assert_session
    ->pageTextNotContains($field_label_es);
}