function ContentTranslationUITestBase::doTestAuthoringInfo

Same name and namespace in other branches
  1. 8.9.x core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php \Drupal\content_translation\Tests\ContentTranslationUITestBase::doTestAuthoringInfo()
  2. 8.9.x core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()
  3. 10 core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase::doTestAuthoringInfo()
  4. 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/tests/src/Functional/ContentTranslationUITestBase.php
Tests the basic translation UI.
2 methods override ContentTranslationUITestBase::doTestAuthoringInfo()
CommentTranslationUITest::doTestAuthoringInfo in core/modules/comment/tests/src/Functional/CommentTranslationUITest.php
Tests the translation authoring information.
NodeTranslationUITest::doTestAuthoringInfo in core/modules/node/tests/src/Functional/NodeTranslationUITest.php
Tests the translation authoring information.

File

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

Class

ContentTranslationUITestBase
Tests the Content Translation UI.

Namespace

Drupal\Tests\content_translation\Functional

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->drupalGet($url);
        $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);
    foreach ($this->langcodes as $langcode) {
        $metadata = $this->manager
            ->getTranslationMetadata($entity->getTranslation($langcode));
        $this->assertEquals($values[$langcode]['uid'], $metadata->getAuthor()
            ->id(), 'Translation author correctly stored.');
        $this->assertEquals($values[$langcode]['created'], $metadata->getCreatedTime(), '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->drupalGet($entity->toUrl('edit-form'));
    $this->submitForm($edit, $this->getFormSubmitAction($entity, $langcode));
    $this->assertSession()
        ->statusMessageExists('error');
    $metadata = $this->manager
        ->getTranslationMetadata($entity->getTranslation($langcode));
    $this->assertEquals($values[$langcode]['uid'], $metadata->getAuthor()
        ->id(), 'Translation author correctly kept.');
    $this->assertEquals($values[$langcode]['created'], $metadata->getCreatedTime(), 'Translation date correctly kept.');
}

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