You are here

function TranslationWebTest::testFieldFormTranslationRevisions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/TranslationWebTest.php \Drupal\field\Tests\TranslationWebTest::testFieldFormTranslationRevisions()

Tests field translations when creating a new revision.

File

core/modules/field/src/Tests/TranslationWebTest.php, line 93
Contains \Drupal\field\Tests\TranslationWebTest.

Class

TranslationWebTest
Tests multilanguage fields logic that require a full environment.

Namespace

Drupal\field\Tests

Code

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

  // Prepare the field translations.
  field_test_entity_info_translatable($this->entityTypeId, TRUE);
  $entity = entity_create($this->entityTypeId);
  $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 = array(
    "{$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);
}