3 namespace Drupal\Tests\field\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests multilanguage fields logic that require a full environment.
15 class TranslationWebTest extends FieldTestBase {
22 public static $modules = ['language', 'field_test', 'entity_test'];
25 * The name of the field to use in this test.
32 * The name of the entity type to use in this test.
36 protected $entityTypeId = 'entity_test_mulrev';
39 * The field storage to use in this test.
41 * @var \Drupal\field\Entity\FieldStorageConfig
43 protected $fieldStorage;
46 * The field to use in this test.
48 * @var \Drupal\field\Entity\FieldConfig
52 protected function setUp() {
55 $this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
58 'field_name' => $this->fieldName,
59 'entity_type' => $this->entityTypeId,
60 'type' => 'test_field',
63 FieldStorageConfig::create($field_storage)->save();
64 $this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName);
67 'field_storage' => $this->fieldStorage,
68 'bundle' => $this->entityTypeId,
70 FieldConfig::create($field)->save();
71 $this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName);
73 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
74 ->setComponent($this->fieldName)
77 for ($i = 0; $i < 3; ++$i) {
78 ConfigurableLanguage::create([
80 'label' => $this->randomString(),
86 * Tests field translations when creating a new revision.
88 public function testFieldFormTranslationRevisions() {
89 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
90 $this->drupalLogin($web_user);
92 // Prepare the field translations.
93 field_test_entity_info_translatable($this->entityTypeId, TRUE);
94 $entity = $this->container->get('entity_type.manager')
95 ->getStorage($this->entityTypeId)
97 $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
98 $field_name = $this->fieldStorage->getName();
100 // Store the field translations.
101 ksort($available_langcodes);
102 $entity->langcode->value = key($available_langcodes);
103 foreach ($available_langcodes as $langcode => $value) {
104 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
105 $translation->{$field_name}->value = $value + 1;
109 // Create a new revision.
111 "{$field_name}[0][value]" => $entity->{$field_name}->value,
114 $this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
116 // Check translation revisions.
117 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
118 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
122 * Check if the field translation attached to the entity revision identified
123 * by the passed arguments were correctly stored.
125 private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
126 $field_name = $this->fieldStorage->getName();
127 $entity = $this->container->get('entity_type.manager')
128 ->getStorage($this->entityTypeId)
129 ->loadRevision($revision_id);
130 foreach ($available_langcodes as $langcode => $value) {
131 $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1;
132 $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', ['@language' => $langcode, '@revision' => $entity->getRevisionId()]));