View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Utility\Unicode;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\field\Entity\FieldConfig;
abstract class EntityLanguageTestBase extends EntityUnitTestBase {
protected $languageManager;
protected $langcodes;
protected $fieldName;
protected $untranslatableFieldName;
public static $modules = array(
'language',
'entity_test',
);
protected function setUp() {
parent::setUp();
$this->languageManager = $this->container
->get('language_manager');
foreach (entity_test_entity_types() as $entity_type_id) {
if ($entity_type_id != 'entity_test') {
$this
->installEntitySchema($entity_type_id);
}
}
$this
->installConfig(array(
'language',
));
module_load_install('entity_test');
entity_test_install();
$this->state
->set('entity_test.translation', TRUE);
$this->fieldName = Unicode::strtolower($this
->randomMachineName() . '_field_name');
$this->untranslatableFieldName = Unicode::strtolower($this
->randomMachineName() . '_field_name');
foreach (entity_test_entity_types() as $entity_type) {
entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'entity_type' => $entity_type,
'type' => 'text',
'cardinality' => 4,
))
->save();
entity_create('field_config', array(
'field_name' => $this->fieldName,
'entity_type' => $entity_type,
'bundle' => $entity_type,
'translatable' => TRUE,
))
->save();
$this->field[$entity_type] = FieldConfig::load($entity_type . '.' . $entity_type . '.' . $this->fieldName);
entity_create('field_storage_config', array(
'field_name' => $this->untranslatableFieldName,
'entity_type' => $entity_type,
'type' => 'text',
'cardinality' => 4,
))
->save();
entity_create('field_config', array(
'field_name' => $this->untranslatableFieldName,
'entity_type' => $entity_type,
'bundle' => $entity_type,
'translatable' => FALSE,
))
->save();
}
$this
->installConfig(array(
'language',
));
$this->langcodes = array();
for ($i = 0; $i < 3; ++$i) {
$language = ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'label' => $this
->randomString(),
'weight' => $i,
));
$this->langcodes[$i] = $language
->getId();
$language
->save();
}
}
protected function toggleFieldTranslatability($entity_type, $bundle) {
$fields = array(
$this->fieldName,
$this->untranslatableFieldName,
);
foreach ($fields as $field_name) {
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$translatable = !$field
->isTranslatable();
$field
->set('translatable', $translatable);
$field
->save();
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$this
->assertEqual($field
->isTranslatable(), $translatable, 'Field translatability changed.');
}
\Drupal::cache('entity')
->deleteAll();
}
}