function ContentTranslationUITestBase::doTestTranslationChanged

Same name in this branch
  1. 8.9.x core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php \Drupal\content_translation\Tests\ContentTranslationUITestBase::doTestTranslationChanged()
Same name in other branches
  1. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestTranslationChanged()
  2. 10 core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestTranslationChanged()
  3. 11.x 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 522

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->drupalPostForm($edit_path, $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->assertEqual($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->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.');
        }
    }
}

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