function ContentTranslationUITestBase::doTestAuthoringInfo

Same name in this branch
  1. 8.9.x core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()
Same name in other branches
  1. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()
  2. 10 core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()
  3. 11.x core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()

Tests the translation authoring information.

1 call to ContentTranslationUITestBase::doTestAuthoringInfo()
ContentTranslationUITestBase::testTranslationUI in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation UI.

File

core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php, line 316

Class

ContentTranslationUITestBase
Tests the Content Translation UI.

Namespace

Drupal\content_translation\Tests

Code

protected function doTestAuthoringInfo() {
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId);
    $storage->resetCache([
        $this->entityId,
    ]);
    $entity = $storage->load($this->entityId);
    $values = [];
    // Post different authoring information for each translation.
    foreach ($this->langcodes as $index => $langcode) {
        $user = $this->drupalCreateUser();
        $values[$langcode] = [
            'uid' => $user->id(),
            'created' => REQUEST_TIME - mt_rand(0, 1000),
        ];
        $edit = [
            'content_translation[uid]' => $user->getAccountName(),
            'content_translation[created]' => $this->container
                ->get('date.formatter')
                ->format($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
        ];
        $url = $entity->toUrl('edit-form', [
            'language' => ConfigurableLanguage::load($langcode),
        ]);
        $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
    }
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId);
    $storage->resetCache([
        $this->entityId,
    ]);
    $entity = $storage->load($this->entityId);
    foreach ($this->langcodes as $langcode) {
        $metadata = $this->manager
            ->getTranslationMetadata($entity->getTranslation($langcode));
        $this->assertEqual($metadata->getAuthor()
            ->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
        $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
    }
    // Try to post non valid values and check that they are rejected.
    $langcode = 'en';
    $edit = [
        // User names have by default length 8.
'content_translation[uid]' => $this->randomMachineName(12),
        'content_translation[created]' => '19/11/1978',
    ];
    $this->drupalPostForm($entity->toUrl('edit-form'), $edit, $this->getFormSubmitAction($entity, $langcode));
    $this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
    $metadata = $this->manager
        ->getTranslationMetadata($entity->getTranslation($langcode));
    $this->assertEqual($metadata->getAuthor()
        ->id(), $values[$langcode]['uid'], 'Translation author correctly kept.');
    $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly kept.');
}

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