You are here

protected function ContentTranslationUITestBase::doTestTranslationChanged in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php \Drupal\content_translation\Tests\ContentTranslationUITestBase::doTestTranslationChanged()

Tests the basic translation workflow.

1 call to ContentTranslationUITestBase::doTestTranslationChanged()
ContentTranslationUITestBase::testTranslationUI in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation UI.
1 method overrides ContentTranslationUITestBase::doTestTranslationChanged()
ShortcutTranslationUITest::doTestTranslationChanged in core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php
Tests the basic translation workflow.

File

core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php, line 491
Contains \Drupal\content_translation\Tests\ContentTranslationUITestBase.

Class

ContentTranslationUITestBase
Tests the Content Translation UI.

Namespace

Drupal\content_translation\Tests

Code

protected function doTestTranslationChanged() {
  $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  $changed_field_name = $this
    ->getChangedFieldName($entity);
  $definition = $entity
    ->getFieldDefinition($changed_field_name);
  $config = $definition
    ->getConfig($entity
    ->bundle());
  foreach ([
    FALSE,
    TRUE,
  ] as $translatable_changed_field) {
    if ($definition
      ->isTranslatable()) {

      // For entities defining a translatable changed field we want to test
      // the correct behavior of that field even if the translatability is
      // revoked. In that case the changed timestamp should be synchronized
      // across all translations.
      $config
        ->setTranslatable($translatable_changed_field);
      $config
        ->save();
    }
    elseif ($translatable_changed_field) {

      // For entities defining a non-translatable changed field we cannot
      // declare the field as translatable on the fly by modifying its config
      // because the schema doesn't support this.
      break;
    }
    foreach ($entity
      ->getTranslationLanguages() as $language) {

      // Ensure different timestamps.
      sleep(1);
      $langcode = $language
        ->getId();
      $edit = array(
        $this->fieldName . '[0][value]' => $this
          ->randomString(),
      );
      $edit_path = $entity
        ->urlInfo('edit-form', array(
        'language' => $language,
      ));
      $this
        ->drupalPostForm($edit_path, $edit, $this
        ->getFormSubmitAction($entity, $langcode));
      $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
      $this
        ->assertEqual($entity
        ->getChangedTimeAcrossTranslations(), $entity
        ->getTranslation($langcode)
        ->getChangedTime(), format_string('Changed time for language %language is the latest change over all languages.', array(
        '%language' => $language
          ->getName(),
      )));
    }
    $timestamps = array();
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $next_timestamp = $entity
        ->getTranslation($language
        ->getId())
        ->getChangedTime();
      if (!in_array($next_timestamp, $timestamps)) {
        $timestamps[] = $next_timestamp;
      }
    }
    if ($translatable_changed_field) {
      $this
        ->assertEqual(count($timestamps), count($entity
        ->getTranslationLanguages()), 'All timestamps from all languages are different.');
    }
    else {
      $this
        ->assertEqual(count($timestamps), 1, 'All timestamps from all languages are identical.');
    }
  }
}