You are here

protected function ContentTranslationUITestBase::doTestTranslationChanged in Drupal 9

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

Tests the basic translation workflow.

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

File

core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php, line 536

Class

ContentTranslationUITestBase
Tests the Content Translation UI.

Namespace

Drupal\Tests\content_translation\Functional

Code

protected function doTestTranslationChanged() {
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityTypeId);
  $storage
    ->resetCache([
    $this->entityId,
  ]);
  $entity = $storage
    ->load($this->entityId);
  $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 = [
        $this->fieldName . '[0][value]' => $this
          ->randomString(),
      ];
      $edit_path = $entity
        ->toUrl('edit-form', [
        'language' => $language,
      ]);
      $this
        ->drupalGet($edit_path);
      $this
        ->submitForm($edit, $this
        ->getFormSubmitAction($entity, $langcode));
      $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId);
      $storage
        ->resetCache([
        $this->entityId,
      ]);
      $entity = $storage
        ->load($this->entityId);
      $this
        ->assertEquals($entity
        ->getChangedTimeAcrossTranslations(), $entity
        ->getTranslation($langcode)
        ->getChangedTime(), new FormattableMarkup('Changed time for language %language is the latest change over all languages.', [
        '%language' => $language
          ->getName(),
      ]));
    }
    $timestamps = [];
    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
        ->assertSameSize($entity
        ->getTranslationLanguages(), $timestamps, 'All timestamps from all languages are different.');
    }
    else {
      $this
        ->assertCount(1, $timestamps, 'All timestamps from all languages are identical.');
    }
  }
}