You are here

protected function ContentTranslationWorkflowsTest::setupEntity in Drupal 10

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

Creates a test entity and translate it.

Parameters

Drupal\User\UserInterface|null $user: (optional) The entity owner.

File

core/modules/content_translation/tests/src/Functional/ContentTranslationWorkflowsTest.php, line 154

Class

ContentTranslationWorkflowsTest
Tests the content translation workflows for the test entity.

Namespace

Drupal\Tests\content_translation\Functional

Code

protected function setupEntity(UserInterface $user = NULL) {
  $default_langcode = $this->langcodes[0];

  // Create a test entity.
  $user = $user ?: $this
    ->drupalCreateUser();
  $values = [
    'name' => $this
      ->randomMachineName(),
    'user_id' => $user
      ->id(),
    $this->fieldName => [
      [
        'value' => $this
          ->randomMachineName(16),
      ],
    ],
  ];
  $id = $this
    ->createEntity($values, $default_langcode);
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityTypeId);

  // Create a translation that is not published to test view access.
  $this
    ->drupalLogin($this->translator);
  $add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
    $this->entityTypeId => $id,
    'source' => $default_langcode,
    'target' => $this->langcodes[2],
  ]);
  $edit = [
    'name[0][value]' => 'translation name',
    'content_translation[status]' => FALSE,
  ];
  $this
    ->drupalGet($add_translation_url);
  $this
    ->submitForm($edit, 'Save');
  $storage
    ->resetCache([
    $id,
  ]);
  $this->entity = $storage
    ->load($id);
  $this
    ->rebuildContainer();
}