You are here

public function TranslationWebTest::testFieldFormTranslationRevisions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/TranslationWebTest.php \Drupal\Tests\field\Functional\TranslationWebTest::testFieldFormTranslationRevisions()
  2. 10 core/modules/field/tests/src/Functional/TranslationWebTest.php \Drupal\Tests\field\Functional\TranslationWebTest::testFieldFormTranslationRevisions()

Tests field translations when creating a new revision.

File

core/modules/field/tests/src/Functional/TranslationWebTest.php, line 94

Class

TranslationWebTest
Tests multilanguage fields logic that require a full environment.

Namespace

Drupal\Tests\field\Functional

Code

public function testFieldFormTranslationRevisions() {
  $web_user = $this
    ->drupalCreateUser([
    'view test entity',
    'administer entity_test content',
  ]);
  $this
    ->drupalLogin($web_user);

  // Prepare the field translations.
  field_test_entity_info_translatable($this->entityTypeId, TRUE);
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityTypeId)
    ->create();
  $available_langcodes = array_flip(array_keys($this->container
    ->get('language_manager')
    ->getLanguages()));
  $field_name = $this->fieldStorage
    ->getName();

  // Store the field translations.
  ksort($available_langcodes);
  $entity->langcode->value = key($available_langcodes);
  foreach ($available_langcodes as $langcode => $value) {
    $translation = $entity
      ->hasTranslation($langcode) ? $entity
      ->getTranslation($langcode) : $entity
      ->addTranslation($langcode);
    $translation->{$field_name}->value = $value + 1;
  }
  $entity
    ->save();

  // Create a new revision.
  $edit = [
    "{$field_name}[0][value]" => $entity->{$field_name}->value,
    'revision' => TRUE,
  ];
  $this
    ->drupalPostForm($this->entityTypeId . '/manage/' . $entity
    ->id() . '/edit', $edit, t('Save'));

  // Check translation revisions.
  $this
    ->checkTranslationRevisions($entity
    ->id(), $entity
    ->getRevisionId(), $available_langcodes);
  $this
    ->checkTranslationRevisions($entity
    ->id(), $entity
    ->getRevisionId() + 1, $available_langcodes);
}