You are here

protected function ContentTranslationWorkflowsTest::doTestWorkflows 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::doTestWorkflows()
  2. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationWorkflowsTest.php \Drupal\Tests\content_translation\Functional\ContentTranslationWorkflowsTest::doTestWorkflows()

Checks that workflows have the expected behaviors for the given user.

Parameters

\Drupal\user\UserInterface $user: The user to test the workflow behavior against.

array $expected_status: The an associative array with the operation name as key and the expected status as value.

File

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

Class

ContentTranslationWorkflowsTest
Tests the content translation workflows for the test entity.

Namespace

Drupal\Tests\content_translation\Functional

Code

protected function doTestWorkflows(UserInterface $user, $expected_status) {
  $default_langcode = $this->langcodes[0];
  $languages = $this->container
    ->get('language_manager')
    ->getLanguages();
  $options = [
    'language' => $languages[$default_langcode],
    'absolute' => TRUE,
  ];
  $this
    ->drupalLogin($user);

  // Check whether the user is allowed to access the entity form in edit mode.
  $edit_url = $this->entity
    ->toUrl('edit-form', $options);
  $this
    ->drupalGet($edit_url, $options);
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['edit']);

  // Check whether the user is allowed to access the entity delete form.
  $delete_url = $this->entity
    ->toUrl('delete-form', $options);
  $this
    ->drupalGet($delete_url, $options);
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['delete']);

  // Check whether the user is allowed to access the translation overview.
  $langcode = $this->langcodes[1];
  $options['language'] = $languages[$langcode];
  $translations_url = $this->entity
    ->toUrl('drupal:content-translation-overview', $options)
    ->toString();
  $this
    ->drupalGet($translations_url);
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['overview']);

  // Check whether the user is allowed to create a translation.
  $add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
    $this->entityTypeId => $this->entity
      ->id(),
    'source' => $default_langcode,
    'target' => $langcode,
  ], $options);
  if ($expected_status['add_translation'] == 200) {
    $this
      ->clickLink('Add');
    $this
      ->assertSession()
      ->addressEquals($add_translation_url);

    // Check that the translation form does not contain shared elements for
    // translators.
    if ($expected_status['edit'] == 403) {
      $this
        ->assertNoSharedElements();
    }
  }
  else {
    $this
      ->drupalGet($add_translation_url);
  }
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['add_translation']);

  // Check whether the user is allowed to edit a translation.
  $langcode = $this->langcodes[2];
  $options['language'] = $languages[$langcode];
  $edit_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_edit", [
    $this->entityTypeId => $this->entity
      ->id(),
    'language' => $langcode,
  ], $options);
  if ($expected_status['edit_translation'] == 200) {
    $this
      ->drupalGet($translations_url);
    $editor = $expected_status['edit'] == 200;
    if ($editor) {
      $this
        ->clickLink('Edit', 1);

      // An editor should be pointed to the entity form in multilingual mode.
      // We need a new expected edit path with a new language.
      $expected_edit_path = $this->entity
        ->toUrl('edit-form', $options)
        ->toString();
      $this
        ->assertSession()
        ->addressEquals($expected_edit_path);
    }
    else {
      $this
        ->clickLink('Edit');

      // While a translator should be pointed to the translation form.
      $this
        ->assertSession()
        ->addressEquals($edit_translation_url);

      // Check that the translation form does not contain shared elements.
      $this
        ->assertNoSharedElements();
    }
  }
  else {
    $this
      ->drupalGet($edit_translation_url);
  }
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['edit_translation']);

  // When viewing an unpublished entity directly, access is currently denied
  // completely. See https://www.drupal.org/node/2978048.
  $this
    ->drupalGet($this->entity
    ->getTranslation($langcode)
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['view_unpublished_translation']);

  // On a reference field, the translation falls back to the default language.
  $this
    ->drupalGet($this->referencingEntity
    ->getTranslation($langcode)
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  if ($expected_status['view_unpublished_translation_reference']) {
    $this
      ->assertSession()
      ->pageTextContains('translation name');
  }
  else {
    $this
      ->assertSession()
      ->pageTextContains($this->entity
      ->label());
  }

  // Check whether the user is allowed to delete a translation.
  $delete_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_delete", [
    $this->entityTypeId => $this->entity
      ->id(),
    'language' => $langcode,
  ], $options);
  if ($expected_status['delete_translation'] == 200) {
    $this
      ->drupalGet($translations_url);
    $editor = $expected_status['delete'] == 200;
    if ($editor) {
      $this
        ->clickLink('Delete', 1);

      // An editor should be pointed to the entity deletion form in
      // multilingual mode. We need a new expected delete path with a new
      // language.
      $expected_delete_path = $this->entity
        ->toUrl('delete-form', $options)
        ->toString();
      $this
        ->assertSession()
        ->addressEquals($expected_delete_path);
    }
    else {
      $this
        ->clickLink('Delete');

      // While a translator should be pointed to the translation deletion
      // form.
      $this
        ->assertSession()
        ->addressEquals($delete_translation_url);
    }
  }
  else {
    $this
      ->drupalGet($delete_translation_url);
  }
  $this
    ->assertSession()
    ->statusCodeEquals($expected_status['delete_translation']);
}