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

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

8
namespace Drupal\content_translation\Tests;
9

10
use Drupal\Component\Utility\SafeMarkup;
11
use Drupal\Core\Language\LanguageInterface;
12
use Drupal\Core\Url;
13
use Drupal\user\UserInterface;
14 15

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

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

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

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

  /**
42
   * Overrides \Drupal\content_translation\Tests\ContentTranslationTestBase::getEditorPermissions().
43 44 45 46 47 48 49 50 51 52 53 54
   */
  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.
55
    $user = $this->drupalCreateUser();
56
    $values = array(
57
      'name' => $this->randomMachineName(),
58
      'user_id' => $user->id(),
59
      $this->fieldName => array(array('value' => $this->randomMachineName(16))),
60 61
    );
    $id = $this->createEntity($values, $default_langcode);
62
    $this->entity = entity_load($this->entityTypeId, $id, TRUE);
63 64 65

    // Create a translation.
    $this->drupalLogin($this->translator);
66 67
    $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'));
68
    $this->rebuildContainer();
69 70 71 72 73 74 75
  }

  /**
   * Test simple and editorial translation workflows.
   */
  function testWorkflows() {
    // Test workflows for the editor.
76 77 78 79 80 81 82 83 84
    $expected_status = [
      'edit' => 200,
      'delete' => 200,
      'overview' => 403,
      'add_translation' => 403,
      'edit_translation' => 403,
      'delete_translation' => 403,
    ];
    $this->doTestWorkflows($this->editor, $expected_status);
85 86

    // Test workflows for the translator.
87 88 89 90 91 92 93 94 95
    $expected_status = [
      'edit' => 403,
      'delete' => 403,
      'overview' => 200,
      'add_translation' => 200,
      'edit_translation' => 200,
      'delete_translation' => 200,
    ];
    $this->doTestWorkflows($this->translator, $expected_status);
96 97

    // Test workflows for the admin.
98 99 100 101 102 103 104 105 106
    $expected_status = [
      'edit' => 200,
      'delete' => 200,
      'overview' => 200,
      'add_translation' => 200,
      'edit_translation' => 403,
      'delete_translation' => 403,
    ];
    $this->doTestWorkflows($this->administrator, $expected_status);
107

108
    // Check that translation permissions allow the associated operations.
109
    $ops = array('create' => t('Add'), 'update' => t('Edit'), 'delete' => t('Delete'));
110
    $translations_url = $this->entity->urlInfo('drupal:content-translation-overview');
111
    foreach ($ops as $current_op => $item) {
112
      $user = $this->drupalCreateUser(array($this->getTranslatePermission(), "$current_op content translations"));
113
      $this->drupalLogin($user);
114
      $this->drupalGet($translations_url);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

      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.
   *
130
   * @param \Drupal\user\UserInterface $user
131 132 133 134 135
   *   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.
   */
136
  protected function doTestWorkflows(UserInterface $user, $expected_status) {
137
    $default_langcode = $this->langcodes[0];
138
    $languages = $this->container->get('language_manager')->getLanguages();
139 140
    $args = ['@user_label' => $user->getUsername()];
    $options = ['language' => $languages[$default_langcode], 'absolute' => TRUE];
141 142 143
    $this->drupalLogin($user);

    // Check whether the user is allowed to access the entity form in edit mode.
144 145
    $edit_url = $this->entity->urlInfo('edit-form', $options);
    $this->drupalGet($edit_url, $options);
146 147 148 149 150 151
    $this->assertResponse($expected_status['edit'], SafeMarkup::format('The @user_label has the expected edit access.', $args));

    // Check whether the user is allowed to access the entity delete form.
    $delete_url = $this->entity->urlInfo('delete-form', $options);
    $this->drupalGet($delete_url, $options);
    $this->assertResponse($expected_status['delete'], SafeMarkup::format('The @user_label has the expected delete access.', $args));
152 153 154

    // Check whether the user is allowed to access the translation overview.
    $langcode = $this->langcodes[1];
155
    $options['language'] = $languages[$langcode];
156 157
    $translations_url = $this->entity->url('drupal:content-translation-overview', $options);
    $this->drupalGet($translations_url);
158
    $this->assertResponse($expected_status['overview'], SafeMarkup::format('The @user_label has the expected translation overview access.', $args));
159 160

    // Check whether the user is allowed to create a translation.
161
    $add_translation_url = Url::fromRoute('content_translation.translation_add_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $langcode], $options);
162
    if ($expected_status['add_translation'] == 200) {
163
      $this->clickLink('Add');
164
      $this->assertUrl($add_translation_url->toString(), [], 'The translation overview points to the translation form when creating translations.');
165 166 167 168 169 170 171
      // Check that the translation form does not contain shared elements for
      // translators.
      if ($expected_status['edit'] == 403) {
        $this->assertNoSharedElements();
      }
    }
    else {
172
      $this->drupalGet($add_translation_url);
173
    }
174
    $this->assertResponse($expected_status['add_translation'], SafeMarkup::format('The @user_label has the expected translation creation access.', $args));
175 176 177

    // Check whether the user is allowed to edit a translation.
    $langcode = $this->langcodes[2];
178 179
    $options['language'] = $languages[$langcode];
    $edit_translation_url = Url::fromRoute('content_translation.translation_edit_' . $this->entityTypeId, [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
180
    if ($expected_status['edit_translation'] == 200) {
181
      $this->drupalGet($translations_url);
182 183 184
      $editor = $expected_status['edit'] == 200;

      if ($editor) {
185
        $this->clickLink('Edit', 2);
186
        // An editor should be pointed to the entity form in multilingual mode.
187 188 189
        // 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.');
190 191
      }
      else {
192
        $this->clickLink('Edit');
193
        // While a translator should be pointed to the translation form.
194
        $this->assertUrl($edit_translation_url->toString(), [], 'The translation overview points to the translation form for translators when editing translations.');
195 196 197 198 199
        // Check that the translation form does not contain shared elements.
        $this->assertNoSharedElements();
      }
    }
    else {
200
      $this->drupalGet($edit_translation_url);
201
    }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    $this->assertResponse($expected_status['edit_translation'], SafeMarkup::format('The @user_label has the expected translation edit access.', $args));

    // Check whether the user is allowed to delete a translation.
    $langcode = $this->langcodes[2];
    $options['language'] = $languages[$langcode];
    $delete_translation_url = Url::fromRoute('content_translation.translation_delete_' . $this->entityTypeId, [$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->url('delete-form', $options);
        $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->assertResponse($expected_status['delete_translation'], SafeMarkup::format('The @user_label has the expected translation deletion access.', $args));
231 232 233 234 235 236
  }

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

}