function TranslationWebTest::testFieldFormTranslationRevisions

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/TranslationWebTest.php \Drupal\Tests\field\Functional\TranslationWebTest::testFieldFormTranslationRevisions()
  2. 8.9.x core/modules/field/tests/src/Functional/TranslationWebTest.php \Drupal\Tests\field\Functional\TranslationWebTest::testFieldFormTranslationRevisions()
  3. 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 96

Class

TranslationWebTest
Tests multilingual fields logic that require a full environment.

Namespace

Drupal\Tests\field\Functional

Code

public function testFieldFormTranslationRevisions() : void {
    $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->drupalGet($this->entityTypeId . '/manage/' . $entity->id() . '/edit');
    $this->submitForm($edit, 'Save');
    // Check translation revisions.
    $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
    $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.