View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
class ContentTranslationWorkflowsTest extends ContentTranslationTestBase {
protected $entity;
public static $modules = array(
'language',
'content_translation',
'entity_test',
);
protected function setUp() {
parent::setUp();
$this
->setupEntity();
}
protected function getTranslatorPermissions() {
$permissions = parent::getTranslatorPermissions();
$permissions[] = 'view test entity';
return $permissions;
}
protected function getEditorPermissions() {
return array(
'administer entity_test content',
);
}
protected function setupEntity() {
$default_langcode = $this->langcodes[0];
$user = $this
->drupalCreateUser();
$values = array(
'name' => $this
->randomMachineName(),
'user_id' => $user
->id(),
$this->fieldName => array(
array(
'value' => $this
->randomMachineName(16),
),
),
);
$id = $this
->createEntity($values, $default_langcode);
$this->entity = entity_load($this->entityTypeId, $id, TRUE);
$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, array(), t('Save'));
$this
->rebuildContainer();
}
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 = array(
'create' => t('Add'),
'update' => t('Edit'),
'delete' => t('Delete'),
);
$translations_url = $this->entity
->urlInfo('drupal:content-translation-overview');
foreach ($ops as $current_op => $item) {
$user = $this
->drupalCreateUser(array(
$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
->assertNoLink($label, format_string('No %op link found.', array(
'%op' => $label,
)));
}
else {
$this
->assertLink($label, 0, format_string('%op link found.', array(
'%op' => $label,
)));
}
}
}
}
protected function doTestWorkflows(UserInterface $user, $expected_status) {
$default_langcode = $this->langcodes[0];
$languages = $this->container
->get('language_manager')
->getLanguages();
$args = [
'@user_label' => $user
->getUsername(),
];
$options = [
'language' => $languages[$default_langcode],
'absolute' => TRUE,
];
$this
->drupalLogin($user);
$edit_url = $this->entity
->urlInfo('edit-form', $options);
$this
->drupalGet($edit_url, $options);
$this
->assertResponse($expected_status['edit'], SafeMarkup::format('The @user_label has the expected edit access.', $args));
$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));
$langcode = $this->langcodes[1];
$options['language'] = $languages[$langcode];
$translations_url = $this->entity
->url('drupal:content-translation-overview', $options);
$this
->drupalGet($translations_url);
$this
->assertResponse($expected_status['overview'], SafeMarkup::format('The @user_label has the expected translation overview access.', $args));
$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
->assertResponse($expected_status['add_translation'], SafeMarkup::format('The @user_label has the expected translation creation access.', $args));
$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
->url('edit-form', $options);
$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
->assertResponse($expected_status['edit_translation'], SafeMarkup::format('The @user_label has the expected translation edit access.', $args));
$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
->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');
$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));
}
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.');
}
}