View source
<?php
namespace Drupal\Tests\content_translation\Functional;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
abstract class ContentTranslationTestBase extends BrowserTestBase {
protected static $modules = [
'text',
];
protected $entityTypeId = 'entity_test_mul';
protected $bundle;
protected $langcodes;
protected $translator;
protected $editor;
protected $administrator;
protected $fieldName;
protected $controller;
protected $manager;
protected function setUp() {
parent::setUp();
$this
->setupLanguages();
$this
->setupBundle();
$this
->enableTranslation();
$this
->setupUsers();
$this
->setupTestFields();
$this->manager = $this->container
->get('content_translation.manager');
$this->controller = $this->manager
->getTranslationHandler($this->entityTypeId);
$this
->rebuildContainer();
}
protected function setupLanguages() {
$this->langcodes = [
'it',
'fr',
];
foreach ($this->langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
array_unshift($this->langcodes, \Drupal::languageManager()
->getDefaultLanguage()
->getId());
}
protected function getTranslatorPermissions() {
return array_filter([
$this
->getTranslatePermission(),
'create content translations',
'update content translations',
'delete content translations',
]);
}
protected function getTranslatePermission() {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this->entityTypeId);
if ($permission_granularity = $entity_type
->getPermissionGranularity()) {
return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
}
}
protected function getEditorPermissions() {
return [];
}
protected function getAdministratorPermissions() {
return array_merge($this
->getEditorPermissions(), $this
->getTranslatorPermissions(), [
'administer languages',
'administer content translation',
]);
}
protected function setupUsers() {
$this->translator = $this
->drupalCreateUser($this
->getTranslatorPermissions(), 'translator');
$this->editor = $this
->drupalCreateUser($this
->getEditorPermissions(), 'editor');
$this->administrator = $this
->drupalCreateUser($this
->getAdministratorPermissions(), 'administrator');
$this
->drupalLogin($this->translator);
}
protected function setupBundle() {
if (empty($this->bundle)) {
$this->bundle = $this->entityTypeId;
}
}
protected function enableTranslation() {
\Drupal::service('content_translation.manager')
->setEnabled($this->entityTypeId, $this->bundle, TRUE);
}
protected function setupTestFields() {
if (empty($this->fieldName)) {
$this->fieldName = 'field_test_et_ui_test';
}
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'type' => 'string',
'entity_type' => $this->entityTypeId,
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'label' => 'Test translatable text-field',
])
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay($this->entityTypeId, $this->bundle, 'default')
->setComponent($this->fieldName, [
'type' => 'string_textfield',
'weight' => 0,
])
->save();
}
protected function createEntity($values, $langcode, $bundle_name = NULL) {
$entity_values = $values;
$entity_values['langcode'] = $langcode;
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this->entityTypeId);
if ($bundle_key = $entity_type
->getKey('bundle')) {
$entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
}
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
if (!$storage instanceof SqlContentEntityStorage) {
foreach ($values as $property => $value) {
if (is_array($value)) {
$entity_values[$property] = [
$langcode => $value,
];
}
}
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId)
->create($entity_values);
$entity
->save();
return $entity
->id();
}
protected function getEditUrl(ContentEntityInterface $entity) {
if ($entity
->access('update', $this->loggedInUser)) {
$url = $entity
->toUrl('edit-form');
}
else {
$url = $entity
->toUrl('drupal:content-translation-edit');
$url
->setRouteParameter('language', $entity
->language()
->getId());
}
return $url;
}
}