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\entity_test\Entity\EntityTestMulRevPub;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\UserInterface;
class ContentTranslationWorkflowsTest extends ContentTranslationTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $entity;
protected $entityTypeId = 'entity_test_mulrevpub';
protected $referencingEntity;
protected $entityOwner;
protected $notEntityOwner;
protected static $modules = [
'language',
'content_translation',
'entity_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_reference',
'type' => 'entity_reference',
'entity_type' => $this->entityTypeId,
'cardinality' => 1,
'settings' => [
'target_type' => $this->entityTypeId,
],
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->entityTypeId,
'label' => 'Reference',
'translatable' => FALSE,
])
->save();
$this->container
->get('entity_display.repository')
->getViewDisplay($this->entityTypeId, $this->entityTypeId, 'default')
->setComponent('field_reference', [
'type' => 'entity_reference_entity_view',
])
->save();
$this
->setupEntity();
$this->referencingEntity = EntityTestMulRevPub::create([
'name' => 'referencing',
'field_reference' => $this->entity
->id(),
]);
$this->referencingEntity
->addTranslation($this->langcodes[2], $this->referencingEntity
->toArray());
$this->referencingEntity
->save();
}
protected function setupUsers() {
$this->entityOwner = $this
->drupalCreateUser($this
->getEntityOwnerPermissions(), 'entity_owner');
$this->notEntityOwner = $this
->drupalCreateUser();
$this->notEntityOwner
->set('roles', $this->entityOwner
->getRoles(TRUE));
$this->notEntityOwner
->save();
parent::setupUsers();
}
protected function getEntityOwnerPermissions() {
return [
'edit own entity_test content',
'translate editable entities',
'view test entity',
'view test entity translations',
'view unpublished test entity translations',
];
}
protected function getTranslatorPermissions() {
$permissions = parent::getTranslatorPermissions();
$permissions[] = 'view test entity';
$permissions[] = 'view test entity translations';
$permissions[] = 'view unpublished test entity translations';
return $permissions;
}
protected function getEditorPermissions() {
return [
'administer entity_test content',
'view test entity',
'view test entity translations',
];
}
protected function setupEntity(UserInterface $user = NULL) {
$default_langcode = $this->langcodes[0];
$user = $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);
$this
->drupalLogin($this->translator);
$add_translation_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
$this->entityTypeId => $id,
'source' => $default_langcode,
'target' => $this->langcodes[2],
]);
$edit = [
'name[0][value]' => 'translation name',
'content_translation[status]' => FALSE,
];
$this
->drupalGet($add_translation_url);
$this
->submitForm($edit, 'Save');
$storage
->resetCache([
$id,
]);
$this->entity = $storage
->load($id);
$this
->rebuildContainer();
}
public function testWorkflows() {
$expected_status = [
'edit' => 200,
'delete' => 200,
'overview' => 403,
'add_translation' => 403,
'edit_translation' => 403,
'delete_translation' => 403,
'view_unpublished_translation' => 403,
'view_unpublished_translation_reference' => FALSE,
];
$this
->doTestWorkflows($this->editor, $expected_status);
$expected_status = [
'edit' => 403,
'delete' => 403,
'overview' => 200,
'add_translation' => 200,
'edit_translation' => 200,
'delete_translation' => 200,
'view_unpublished_translation' => 200,
'view_unpublished_translation_reference' => TRUE,
];
$this
->doTestWorkflows($this->translator, $expected_status);
$expected_status = [
'edit' => 200,
'delete' => 200,
'overview' => 200,
'add_translation' => 200,
'edit_translation' => 403,
'delete_translation' => 403,
'view_unpublished_translation' => 200,
'view_unpublished_translation_reference' => TRUE,
];
$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
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', $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,
]));
}
}
}
$expected_status = [
'edit' => 403,
'delete' => 403,
'overview' => 403,
'add_translation' => 403,
'edit_translation' => 403,
'delete_translation' => 403,
'view_unpublished_translation' => 200,
'view_unpublished_translation_reference' => TRUE,
];
$this
->doTestWorkflows($this->entityOwner, $expected_status);
$this
->setupEntity($this->entityOwner);
$this->referencingEntity
->set('field_reference', $this->entity
->id());
$this->referencingEntity
->save();
$expected_status = [
'edit' => 200,
'delete' => 403,
'overview' => 200,
'add_translation' => 200,
'edit_translation' => 200,
'delete_translation' => 200,
'view_unpublished_translation' => 200,
'view_unpublished_translation_reference' => TRUE,
];
$this
->doTestWorkflows($this->entityOwner, $expected_status);
$expected_status = [
'edit' => 403,
'delete' => 403,
'overview' => 403,
'add_translation' => 403,
'edit_translation' => 403,
'delete_translation' => 403,
'view_unpublished_translation' => 200,
'view_unpublished_translation_reference' => TRUE,
];
$this
->doTestWorkflows($this->notEntityOwner, $expected_status);
}
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
->assertSession()
->addressEquals($add_translation_url);
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', 1);
$expected_edit_path = $this->entity
->toUrl('edit-form', $options)
->toString();
$this
->assertSession()
->addressEquals($expected_edit_path);
}
else {
$this
->clickLink('Edit');
$this
->assertSession()
->addressEquals($edit_translation_url);
$this
->assertNoSharedElements();
}
}
else {
$this
->drupalGet($edit_translation_url);
}
$this
->assertSession()
->statusCodeEquals($expected_status['edit_translation']);
$this
->drupalGet($this->entity
->getTranslation($langcode)
->toUrl());
$this
->assertSession()
->statusCodeEquals($expected_status['view_unpublished_translation']);
$this
->drupalGet($this->referencingEntity
->getTranslation($langcode)
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
if ($expected_status['view_unpublished_translation_reference']) {
$this
->assertSession()
->pageTextContains('translation name');
}
else {
$this
->assertSession()
->pageTextContains($this->entity
->label());
}
$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', 1);
$expected_delete_path = $this->entity
->toUrl('delete-form', $options)
->toString();
$this
->assertSession()
->addressEquals($expected_delete_path);
}
else {
$this
->clickLink('Delete');
$this
->assertSession()
->addressEquals($delete_translation_url);
}
}
else {
$this
->drupalGet($delete_translation_url);
}
$this
->assertSession()
->statusCodeEquals($expected_status['delete_translation']);
}
protected function assertNoSharedElements() {
$language_none = LanguageInterface::LANGCODE_NOT_SPECIFIED;
return $this
->assertSession()
->fieldNotExists("field_test_text[{$language_none}][0][value]");
}
}