ContentTranslationWorkflowsTest.php 7.71 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity\Tests\ContentTranslationWorkflowsTest.
6 7
 */

8
namespace Drupal\content_translation\Tests;
9

10
use Drupal\Core\Language\LanguageInterface;
11
use Drupal\Core\Url;
12
use Drupal\user\UserInterface;
13 14

/**
15 16 17
 * Tests the content translation workflows for the test entity.
 *
 * @group content_translation
18
 */
19
class ContentTranslationWorkflowsTest extends ContentTranslationTestBase {
20 21 22 23 24 25 26 27 28 29 30 31 32

  /**
   * The entity used for testing.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * Modules to enable.
   *
   * @var array
   */
33
  public static $modules = array('language', 'content_translation', 'entity_test');
34

35
  protected function setUp() {
36 37 38 39 40
    parent::setUp();
    $this->setupEntity();
  }

  /**
41
   * Overrides \Drupal\content_translation\Tests\ContentTranslationTestBase::getEditorPermissions().
42 43 44 45 46 47 48 49 50 51 52 53
   */
  protected function getEditorPermissions() {
    return array('administer entity_test content');
  }

  /**
   * Creates a test entity and translate it.
   */
  protected function setupEntity() {
    $default_langcode = $this->langcodes[0];

    // Create a test entity.
54
    $user = $this->drupalCreateUser();
55
    $values = array(
56
      'name' => $this->randomMachineName(),
57
      'user_id' => $user->id(),
58
      $this->fieldName => array(array('value' => $this->randomMachineName(16))),
59 60
    );
    $id = $this->createEntity($values, $default_langcode);
61
    $this->entity = entity_load($this->entityTypeId, $id, TRUE);
62 63 64

    // Create a translation.
    $this->drupalLogin($this->translator);
65 66
    $add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $this->langcodes[2]]);
    $this->drupalPostForm($add_translation_url, array(), t('Save'));
67
    $this->rebuildContainer();
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * Test simple and editorial translation workflows.
   */
  function testWorkflows() {
    // Test workflows for the editor.
    $expected_status = array('edit' => 200, 'overview' => 403, 'add_translation' => 403, 'edit_translation' => 403);
    $this->assertWorkflows($this->editor, $expected_status);

    // Test workflows for the translator.
    $expected_status = array('edit' => 403, 'overview' => 200, 'add_translation' => 200, 'edit_translation' => 200);
    $this->assertWorkflows($this->translator, $expected_status);

    // Test workflows for the admin.
    $expected_status = array('edit' => 200, 'overview' => 200, 'add_translation' => 200, 'edit_translation' => 200);
    $this->assertWorkflows($this->administrator, $expected_status);

    // Check that translation permissions governate the associated operations.
87
    $ops = array('create' => t('Add'), 'update' => t('Edit'), 'delete' => t('Delete'));
88
    $translations_url = $this->entity->urlInfo('drupal:content-translation-overview');
89
    foreach ($ops as $current_op => $item) {
90
      $user = $this->drupalCreateUser(array($this->getTranslatePermission(), "$current_op content translations"));
91
      $this->drupalLogin($user);
92
      $this->drupalGet($translations_url);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

      foreach ($ops as $op => $label) {
        if ($op != $current_op) {
          $this->assertNoLink($label, format_string('No %op link found.', array('%op' => $label)));
        }
        else {
          $this->assertLink($label, 0, format_string('%op link found.', array('%op' => $label)));
        }
      }
    }
  }

  /**
   * Checks that workflows have the expected behaviors for the given user.
   *
108
   * @param \Drupal\user\UserInterface $user
109 110 111 112 113
   *   The user to test the workflow behavior against.
   * @param array $expected_status
   *   The an associative array with the operation name as key and the expected
   *   status as value.
   */
114
  protected function assertWorkflows(UserInterface $user, $expected_status) {
115
    $default_langcode = $this->langcodes[0];
116
    $languages = $this->container->get('language_manager')->getLanguages();
117
    $args = array('@user_label' => $user->getUsername());
118 119 120
    $this->drupalLogin($user);

    // Check whether the user is allowed to access the entity form in edit mode.
121 122 123
    $options = array('language' => $languages[$default_langcode], 'absolute' => TRUE);
    $edit_url = $this->entity->urlInfo('edit-form', $options);
    $this->drupalGet($edit_url, $options);
124 125 126 127
    $this->assertResponse($expected_status['edit'], format_string('The @user_label has the expected edit access.', $args));

    // Check whether the user is allowed to access the translation overview.
    $langcode = $this->langcodes[1];
128 129 130
    $options = array('language' => $languages[$langcode], 'absolute' => TRUE);
    $translations_url = $this->entity->url('drupal:content-translation-overview', $options);
    $this->drupalGet($translations_url);
131 132 133
    $this->assertResponse($expected_status['overview'], format_string('The @user_label has the expected translation overview access.', $args));

    // Check whether the user is allowed to create a translation.
134
    $add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $langcode], $options);
135
    if ($expected_status['add_translation'] == 200) {
136
      $this->clickLink('Add');
137
      $this->assertUrl($add_translation_url->toString(), array(), 'The translation overview points to the translation form when creating translations.');
138 139 140 141 142 143 144
      // Check that the translation form does not contain shared elements for
      // translators.
      if ($expected_status['edit'] == 403) {
        $this->assertNoSharedElements();
      }
    }
    else {
145
      $this->drupalGet($add_translation_url);
146 147 148 149 150
    }
    $this->assertResponse($expected_status['add_translation'], format_string('The @user_label has the expected translation creation access.', $args));

    // Check whether the user is allowed to edit a translation.
    $langcode = $this->langcodes[2];
151 152 153
    $options['language'] = $languages[$langcode];
    $edit_translation_url = Url::fromRoute('content_translation.translation_edit_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
    $options = ['language' => $languages[$langcode], 'absolute' => TRUE];
154
    if ($expected_status['edit_translation'] == 200) {
155
      $this->drupalGet($translations_url);
156 157 158
      $editor = $expected_status['edit'] == 200;

      if ($editor) {
159
        $this->clickLink('Edit', 2);
160
        // An editor should be pointed to the entity form in multilingual mode.
161 162 163 164

        // We need a new expected edit path with a new language.
        $expected_edit_path = $this->entity->url('edit-form', $options);
        $this->assertUrl($expected_edit_path, [], 'The translation overview points to the edit form for editors when editing translations.');
165 166
      }
      else {
167
        $this->clickLink('Edit');
168
        // While a translator should be pointed to the translation form.
169
        $this->assertUrl($edit_translation_url->toString(), array(), 'The translation overview points to the translation form for translators when editing translations.');
170 171 172 173 174
        // Check that the translation form does not contain shared elements.
        $this->assertNoSharedElements();
      }
    }
    else {
175
      $this->drupalGet($edit_translation_url);
176 177 178 179 180 181 182 183
    }
    $this->assertResponse($expected_status['edit_translation'], format_string('The @user_label has the expected translation creation access.', $args));
  }

  /**
   * Assert that the current page does not contain shared form elements.
   */
  protected function assertNoSharedElements() {
184
    $language_none = LanguageInterface::LANGCODE_NOT_SPECIFIED;
185 186 187 188
    return $this->assertNoFieldByXPath("//input[@name='field_test_text[$language_none][0][value]']", NULL, 'Shared elements are not available on the translation form.');
  }

}