Newer
Older
Angie Byron
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\entity\Tests\ContentTranslationTestBase.
Angie Byron
committed
*/
Alex Pott
committed
namespace Drupal\content_translation\Tests;
Angie Byron
committed
catch
committed
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
Angie Byron
committed
use Drupal\Core\Language\Language;
use Drupal\simpletest\WebTestBase;
/**
Alex Pott
committed
* Tests content translation workflows.
Angie Byron
committed
*/
Alex Pott
committed
abstract class ContentTranslationTestBase extends WebTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('text');
Angie Byron
committed
/**
* The entity type being tested.
*
* @var string
*/
Alex Pott
committed
protected $entityTypeId = 'entity_test_mul';
Angie Byron
committed
/**
* The bundle being tested.
*
* @var string
*/
protected $bundle;
/**
* The added languages.
Angie Byron
committed
*
* @var array
*/
protected $langcodes;
/**
* The account to be used to test translation operations.
*
Jennifer Hodgdon
committed
* @var \Drupal\user\UserInterface
Angie Byron
committed
*/
protected $translator;
/**
* The account to be used to test multilingual entity editing.
*
Jennifer Hodgdon
committed
* @var \Drupal\user\UserInterface
Angie Byron
committed
*/
protected $editor;
/**
* The account to be used to test access to both workflows.
*
Jennifer Hodgdon
committed
* @var \Drupal\user\UserInterface
Angie Byron
committed
*/
protected $administrator;
/**
* The name of the field used to test translation.
*
* @var string
*/
protected $fieldName;
/**
* The translation controller for the current entity type.
*
* @var \Drupal\content_translation\ContentTranslationHandlerInterface
Angie Byron
committed
*/
protected $controller;
function setUp() {
parent::setUp();
$this->setupLanguages();
$this->setupBundle();
$this->enableTranslation();
$this->setupUsers();
$this->setupTestFields();
Alex Pott
committed
$this->controller = content_translation_controller($this->entityTypeId);
// Rebuild the container so that the new languages are picked up by services
// that hold a list of languages.
$this->rebuildContainer();
Angie Byron
committed
}
/**
* Adds additional languages.
Angie Byron
committed
*/
protected function setupLanguages() {
$this->langcodes = array('it', 'fr');
foreach ($this->langcodes as $langcode) {
language_save(new Language(array('id' => $langcode)));
Angie Byron
committed
}
array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->id);
Angie Byron
committed
}
/**
* Returns an array of permissions needed for the translator.
*/
protected function getTranslatorPermissions() {
Alex Pott
committed
return array_filter(array($this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations'));
Angie Byron
committed
}
/**
* Returns the translate permissions for the current entity and bundle.
*/
protected function getTranslatePermission() {
Alex Pott
committed
$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}";
Angie Byron
committed
}
}
/**
* Returns an array of permissions needed for the editor.
*/
protected function getEditorPermissions() {
// Every entity-type-specific test needs to define these.
return array();
}
/**
* Returns an array of permissions needed for the administrator.
*/
protected function getAdministratorPermissions() {
return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), array('administer content translation'));
}
Angie Byron
committed
/**
* Creates and activates translator, editor and admin users.
*/
protected function setupUsers() {
$this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
$this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
$this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
Angie Byron
committed
$this->drupalLogin($this->translator);
}
/**
* Creates or initializes the bundle date if needed.
*/
protected function setupBundle() {
if (empty($this->bundle)) {
Alex Pott
committed
$this->bundle = $this->entityTypeId;
Angie Byron
committed
}
}
/**
* Enables translation for the current entity type and bundle.
*/
protected function enableTranslation() {
// Enable translation for the current entity type and ensure the change is
// picked up.
Alex Pott
committed
content_translation_set_config($this->entityTypeId, $this->bundle, 'enabled', TRUE);
Angie Byron
committed
drupal_static_reset();
entity_info_cache_clear();
Angie Byron
committed
\Drupal::service('router.builder')->rebuild();
Angie Byron
committed
}
/**
* Creates the test fields.
*/
protected function setupTestFields() {
$this->fieldName = 'field_test_et_ui_test';
entity_create('field_config', array(
'name' => $this->fieldName,
Angie Byron
committed
'type' => 'text',
Alex Pott
committed
'entity_type' => $this->entityTypeId,
Angie Byron
committed
'cardinality' => 1,
'translatable' => TRUE,
))->save();
entity_create('field_instance_config', array(
Alex Pott
committed
'entity_type' => $this->entityTypeId,
Angie Byron
committed
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'label' => 'Test translatable text-field',
))->save();
Alex Pott
committed
entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
Alex Pott
committed
->setComponent($this->fieldName, array(
'type' => 'text_textfield',
'weight' => 0,
))
->save();
Angie Byron
committed
}
/**
* Creates the entity to be translated.
*
* @param array $values
* An array of initial values for the entity.
* @param string $langcode
* The initial language code of the entity.
* @param string $bundle_name
* (optional) The entity bundle, if the entity uses bundles. Defaults to
* NULL. If left NULL, $this->bundle will be used.
*
* @return
* The entity id.
*/
protected function createEntity($values, $langcode, $bundle_name = NULL) {
$entity_values = $values;
$entity_values['langcode'] = $langcode;
Alex Pott
committed
$entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
if ($bundle_key = $entity_type->getKey('bundle')) {
Alex Pott
committed
$entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
Angie Byron
committed
}
catch
committed
$controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
if (!($controller instanceof ContentEntityDatabaseStorage)) {
Angie Byron
committed
foreach ($values as $property => $value) {
if (is_array($value)) {
$entity_values[$property] = array($langcode => $value);
}
}
}
Alex Pott
committed
$entity = entity_create($this->entityTypeId, $entity_values);
Angie Byron
committed
$entity->save();
return $entity->id();
}
}