View source
<?php
namespace Drupal\Tests\content_translation\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\UserInterface;
class ContentTranslationWorkflowsTest extends ContentTranslationTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $entity;
public static $modules = [
'language',
'content_translation',
'entity_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->setupEntity();
}
protected function getTranslatorPermissions() {
$permissions = parent::getTranslatorPermissions();
$permissions[] = 'view test entity';
return $permissions;
}
protected function getEditorPermissions() {
return [
'administer entity_test content',
];
}
protected function setupEntity() {
$default_langcode = $this->langcodes[0];
$user = $this
->drupalCreateUser();
$values = [
'name' => $this
->randomMachineName(),
'user_id' => $user
->id(),
$this->fieldName => [
[
'value' => $this
->randomMachineName(16),
],
],
];
$id = $this
->createEntity($values, $default_langcode);
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$id,
]);
$this->entity = $storage
->load($id);
$this
->drupalLogin($this->translator);
$add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
$this->entityTypeId => $this->entity
->id(),
'source' => $default_langcode,
'target' => $this->langcodes[2],
]);
$this
->drupalPostForm($add_translation_url, [], t('Save'));
$this
->rebuildContainer();
}
public function testWorkflows() {
$expected_status = [
'edit' => 200,
'delete' => 200,
'overview' => 403,
'add_translation' => 403,
'edit_translation' => 403,
'delete_translation' => 403,
];
$this
->doTestWorkflows($this->editor, $expected_status);
$expected_status = [
'edit' => 403,
'delete' => 403,
'overview' => 200,
'add_translation' => 200,
'edit_translation' => 200,
'delete_translation' => 200,
];
$this
->doTestWorkflows($this->translator, $expected_status);
$expected_status = [
'edit' => 200,
'delete' => 200,
'overview' => 200,
'add_translation' => 200,
'edit_translation' => 403,
'delete_translation' => 403,
];
$this
->doTestWorkflows($this->administrator, $expected_status);
$ops = [
'create' => t('Add'),
'update' => t('Edit'),
'delete' => t('Delete'),
];
$translations_url = $this->entity
->toUrl('drupal:content-translation-overview');
foreach ($ops as $current_op => $item) {
$user = $this
->drupalCreateUser([
$this
->getTranslatePermission(),
"{$current_op} content translations",
'view test entity',
]);
$this
->drupalLogin($user);
$this
->drupalGet($translations_url);
$this
->assertCacheContext('user.permissions');
foreach ($this->entity
->getCacheTags() as $cache_tag) {
$this
->assertCacheTag($cache_tag);
}
foreach ($ops as $op => $label) {
if ($op != $current_op) {
$this
->assertSession()
->linkNotExists($label, new FormattableMarkup('No %op link found.', [
'%op' => $label,
]));
}
else {
$this
->assertSession()
->linkExists($label, 0, new FormattableMarkup('%op link found.', [
'%op' => $label,
]));
}
}
}
}
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);
$edit_url = $this->entity
->toUrl('edit-form', $options);
$this
->drupalGet($edit_url, $options);
$this
->assertSession()
->statusCodeEquals($expected_status['edit']);
$delete_url = $this->entity
->toUrl('delete-form', $options);
$this
->drupalGet($delete_url, $options);
$this
->assertSession()
->statusCodeEquals($expected_status['delete']);
$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']);
$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.');
if ($expected_status['edit'] == 403) {
$this
->assertNoSharedElements();
}
}
else {
$this
->drupalGet($add_translation_url);
}
$this
->assertSession()
->statusCodeEquals($expected_status['add_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);
$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');
$this
->assertUrl($edit_translation_url
->toString(), [], 'The translation overview points to the translation form for translators when editing translations.');
$this
->assertNoSharedElements();
}
}
else {
$this
->drupalGet($edit_translation_url);
}
$this
->assertSession()
->statusCodeEquals($expected_status['edit_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);
$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');
$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']);
}
protected function assertNoSharedElements() {
$language_none = LanguageInterface::LANGCODE_NOT_SPECIFIED;
return $this
->assertNoFieldByXPath("//input[@name='field_test_text[{$language_none}][0][value]']", NULL, 'Shared elements are not available on the translation form.');
}
}