function ContentTranslationWorkflowsTest::doTestWorkflows

Same name and namespace in other branches
  1. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationWorkflowsTest.php \Drupal\Tests\content_translation\Functional\ContentTranslationWorkflowsTest::doTestWorkflows()
  2. 10 core/modules/content_translation/tests/src/Functional/ContentTranslationWorkflowsTest.php \Drupal\Tests\content_translation\Functional\ContentTranslationWorkflowsTest::doTestWorkflows()
  3. 11.x 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.

1 call to ContentTranslationWorkflowsTest::doTestWorkflows()
ContentTranslationWorkflowsTest::testWorkflows in core/modules/content_translation/tests/src/Functional/ContentTranslationWorkflowsTest.php
Test simple and editorial translation workflows.

File

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

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->assertUrl($add_translation_url->toString(), [], 'The translation overview points to the translation form when creating translations.');
        // 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', 2);
            // 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->assertUrl($expected_edit_path, [], 'The translation overview points to the edit form for editors when editing translations.');
        }
        else {
            $this->clickLink('Edit');
            // While a translator should be pointed to the translation form.
            $this->assertUrl($edit_translation_url->toString(), [], 'The translation overview points to the translation form for translators when editing translations.');
            // 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']);
    // Check whether the user is allowed to delete a translation.
    $langcode = $this->langcodes[2];
    $options['language'] = $languages[$langcode];
    $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', 2);
            // 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->assertUrl($expected_delete_path, [], 'The translation overview points to the delete form for editors when deleting translations.');
        }
        else {
            $this->clickLink('Delete');
            // While a translator should be pointed to the translation deletion
            // form.
            $this->assertUrl($delete_translation_url->toString(), [], 'The translation overview points to the translation deletion form for translators when deleting translations.');
        }
    }
    else {
        $this->drupalGet($delete_translation_url);
    }
    $this->assertSession()
        ->statusCodeEquals($expected_status['delete_translation']);
}

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