View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
abstract class ContentTranslationTestBase extends WebTestBase {
public static $modules = array(
'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 = array(
'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(array(
$this
->getTranslatePermission(),
'create content translations',
'update content translations',
'delete content translations',
));
}
protected function getTranslatePermission() {
$entity_type = \Drupal::entityManager()
->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 array();
}
protected function getAdministratorPermissions() {
return array_merge($this
->getEditorPermissions(), $this
->getTranslatorPermissions(), array(
'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);
drupal_static_reset();
\Drupal::entityManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
}
protected function setupTestFields() {
if (empty($this->fieldName)) {
$this->fieldName = 'field_test_et_ui_test';
}
entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'type' => 'string',
'entity_type' => $this->entityTypeId,
'cardinality' => 1,
))
->save();
entity_create('field_config', array(
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'label' => 'Test translatable text-field',
))
->save();
entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($this->fieldName, array(
'type' => 'string_textfield',
'weight' => 0,
))
->save();
}
protected function createEntity($values, $langcode, $bundle_name = NULL) {
$entity_values = $values;
$entity_values['langcode'] = $langcode;
$entity_type = \Drupal::entityManager()
->getDefinition($this->entityTypeId);
if ($bundle_key = $entity_type
->getKey('bundle')) {
$entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
}
$controller = $this->container
->get('entity.manager')
->getStorage($this->entityTypeId);
if (!$controller instanceof SqlContentEntityStorage) {
foreach ($values as $property => $value) {
if (is_array($value)) {
$entity_values[$property] = array(
$langcode => $value,
);
}
}
}
$entity = entity_create($this->entityTypeId, $entity_values);
$entity
->save();
return $entity
->id();
}
}