3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
9 use Drupal\user\UserInterface;
12 * Tests the content translation workflows for the test entity.
14 * @group content_translation
16 class ContentTranslationWorkflowsTest extends ContentTranslationTestBase {
18 use AssertPageCacheContextsAndTagsTrait;
21 * The entity used for testing.
23 * @var \Drupal\Core\Entity\EntityInterface
32 public static $modules = ['language', 'content_translation', 'entity_test'];
34 protected function setUp() {
42 protected function getTranslatorPermissions() {
43 $permissions = parent::getTranslatorPermissions();
44 $permissions[] = 'view test entity';
52 protected function getEditorPermissions() {
53 return ['administer entity_test content'];
57 * Creates a test entity and translate it.
59 protected function setupEntity() {
60 $default_langcode = $this->langcodes[0];
62 // Create a test entity.
63 $user = $this->drupalCreateUser();
65 'name' => $this->randomMachineName(),
66 'user_id' => $user->id(),
67 $this->fieldName => [['value' => $this->randomMachineName(16)]],
69 $id = $this->createEntity($values, $default_langcode);
70 $storage = $this->container->get('entity_type.manager')
71 ->getStorage($this->entityTypeId);
72 $storage->resetCache([$id]);
73 $this->entity = $storage->load($id);
75 // Create a translation.
76 $this->drupalLogin($this->translator);
77 $add_translation_url = Url::fromRoute("entity.$this->entityTypeId.content_translation_add", [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $this->langcodes[2]]);
78 $this->drupalPostForm($add_translation_url, [], t('Save'));
79 $this->rebuildContainer();
83 * Test simple and editorial translation workflows.
85 public function testWorkflows() {
86 // Test workflows for the editor.
91 'add_translation' => 403,
92 'edit_translation' => 403,
93 'delete_translation' => 403,
95 $this->doTestWorkflows($this->editor, $expected_status);
97 // Test workflows for the translator.
102 'add_translation' => 200,
103 'edit_translation' => 200,
104 'delete_translation' => 200,
106 $this->doTestWorkflows($this->translator, $expected_status);
108 // Test workflows for the admin.
113 'add_translation' => 200,
114 'edit_translation' => 403,
115 'delete_translation' => 403,
117 $this->doTestWorkflows($this->administrator, $expected_status);
119 // Check that translation permissions allow the associated operations.
120 $ops = ['create' => t('Add'), 'update' => t('Edit'), 'delete' => t('Delete')];
121 $translations_url = $this->entity->urlInfo('drupal:content-translation-overview');
122 foreach ($ops as $current_op => $item) {
123 $user = $this->drupalCreateUser([$this->getTranslatePermission(), "$current_op content translations", 'view test entity']);
124 $this->drupalLogin($user);
125 $this->drupalGet($translations_url);
127 // Make sure that the user.permissions cache context and the cache tags
128 // for the entity are present.
129 $this->assertCacheContext('user.permissions');
130 foreach ($this->entity->getCacheTags() as $cache_tag) {
131 $this->assertCacheTag($cache_tag);
134 foreach ($ops as $op => $label) {
135 if ($op != $current_op) {
136 $this->assertNoLink($label, format_string('No %op link found.', ['%op' => $label]));
139 $this->assertLink($label, 0, format_string('%op link found.', ['%op' => $label]));
146 * Checks that workflows have the expected behaviors for the given user.
148 * @param \Drupal\user\UserInterface $user
149 * The user to test the workflow behavior against.
150 * @param array $expected_status
151 * The an associative array with the operation name as key and the expected
154 protected function doTestWorkflows(UserInterface $user, $expected_status) {
155 $default_langcode = $this->langcodes[0];
156 $languages = $this->container->get('language_manager')->getLanguages();
157 $args = ['@user_label' => $user->getUsername()];
158 $options = ['language' => $languages[$default_langcode], 'absolute' => TRUE];
159 $this->drupalLogin($user);
161 // Check whether the user is allowed to access the entity form in edit mode.
162 $edit_url = $this->entity->urlInfo('edit-form', $options);
163 $this->drupalGet($edit_url, $options);
164 $this->assertResponse($expected_status['edit'], SafeMarkup::format('The @user_label has the expected edit access.', $args));
166 // Check whether the user is allowed to access the entity delete form.
167 $delete_url = $this->entity->urlInfo('delete-form', $options);
168 $this->drupalGet($delete_url, $options);
169 $this->assertResponse($expected_status['delete'], SafeMarkup::format('The @user_label has the expected delete access.', $args));
171 // Check whether the user is allowed to access the translation overview.
172 $langcode = $this->langcodes[1];
173 $options['language'] = $languages[$langcode];
174 $translations_url = $this->entity->url('drupal:content-translation-overview', $options);
175 $this->drupalGet($translations_url);
176 $this->assertResponse($expected_status['overview'], SafeMarkup::format('The @user_label has the expected translation overview access.', $args));
178 // Check whether the user is allowed to create a translation.
179 $add_translation_url = Url::fromRoute("entity.$this->entityTypeId.content_translation_add", [$this->entityTypeId => $this->entity->id(), 'source' => $default_langcode, 'target' => $langcode], $options);
180 if ($expected_status['add_translation'] == 200) {
181 $this->clickLink('Add');
182 $this->assertUrl($add_translation_url->toString(), [], 'The translation overview points to the translation form when creating translations.');
183 // Check that the translation form does not contain shared elements for
185 if ($expected_status['edit'] == 403) {
186 $this->assertNoSharedElements();
190 $this->drupalGet($add_translation_url);
192 $this->assertResponse($expected_status['add_translation'], SafeMarkup::format('The @user_label has the expected translation creation access.', $args));
194 // Check whether the user is allowed to edit a translation.
195 $langcode = $this->langcodes[2];
196 $options['language'] = $languages[$langcode];
197 $edit_translation_url = Url::fromRoute("entity.$this->entityTypeId.content_translation_edit", [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
198 if ($expected_status['edit_translation'] == 200) {
199 $this->drupalGet($translations_url);
200 $editor = $expected_status['edit'] == 200;
203 $this->clickLink('Edit', 2);
204 // An editor should be pointed to the entity form in multilingual mode.
205 // We need a new expected edit path with a new language.
206 $expected_edit_path = $this->entity->url('edit-form', $options);
207 $this->assertUrl($expected_edit_path, [], 'The translation overview points to the edit form for editors when editing translations.');
210 $this->clickLink('Edit');
211 // While a translator should be pointed to the translation form.
212 $this->assertUrl($edit_translation_url->toString(), [], 'The translation overview points to the translation form for translators when editing translations.');
213 // Check that the translation form does not contain shared elements.
214 $this->assertNoSharedElements();
218 $this->drupalGet($edit_translation_url);
220 $this->assertResponse($expected_status['edit_translation'], SafeMarkup::format('The @user_label has the expected translation edit access.', $args));
222 // Check whether the user is allowed to delete a translation.
223 $langcode = $this->langcodes[2];
224 $options['language'] = $languages[$langcode];
225 $delete_translation_url = Url::fromRoute("entity.$this->entityTypeId.content_translation_delete", [$this->entityTypeId => $this->entity->id(), 'language' => $langcode], $options);
226 if ($expected_status['delete_translation'] == 200) {
227 $this->drupalGet($translations_url);
228 $editor = $expected_status['delete'] == 200;
231 $this->clickLink('Delete', 2);
232 // An editor should be pointed to the entity deletion form in
233 // multilingual mode. We need a new expected delete path with a new
235 $expected_delete_path = $this->entity->url('delete-form', $options);
236 $this->assertUrl($expected_delete_path, [], 'The translation overview points to the delete form for editors when deleting translations.');
239 $this->clickLink('Delete');
240 // While a translator should be pointed to the translation deletion
242 $this->assertUrl($delete_translation_url->toString(), [], 'The translation overview points to the translation deletion form for translators when deleting translations.');
246 $this->drupalGet($delete_translation_url);
248 $this->assertResponse($expected_status['delete_translation'], SafeMarkup::format('The @user_label has the expected translation deletion access.', $args));
252 * Assert that the current page does not contain shared form elements.
254 protected function assertNoSharedElements() {
255 $language_none = LanguageInterface::LANGCODE_NOT_SPECIFIED;
256 return $this->assertNoFieldByXPath("//input[@name='field_test_text[$language_none][0][value]']", NULL, 'Shared elements are not available on the translation form.');