3 namespace Drupal\Tests\field\Functional;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
10 * Tests multilanguage fields logic that require a full environment.
14 class TranslationWebTest extends FieldTestBase {
21 public static $modules = ['language', 'field_test', 'entity_test'];
24 * The name of the field to use in this test.
31 * The name of the entity type to use in this test.
35 protected $entityTypeId = 'entity_test_mulrev';
38 * The field storage to use in this test.
40 * @var \Drupal\field\Entity\FieldStorageConfig
42 protected $fieldStorage;
45 * The field to use in this test.
47 * @var \Drupal\field\Entity\FieldConfig
51 protected function setUp() {
54 $this->fieldName = mb_strtolower($this->randomMachineName() . '_field_name');
57 'field_name' => $this->fieldName,
58 'entity_type' => $this->entityTypeId,
59 'type' => 'test_field',
62 FieldStorageConfig::create($field_storage)->save();
63 $this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName);
66 'field_storage' => $this->fieldStorage,
67 'bundle' => $this->entityTypeId,
69 FieldConfig::create($field)->save();
70 $this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName);
72 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
73 ->setComponent($this->fieldName)
76 for ($i = 0; $i < 3; ++$i) {
77 ConfigurableLanguage::create([
79 'label' => $this->randomString(),
85 * Tests field translations when creating a new revision.
87 public function testFieldFormTranslationRevisions() {
88 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
89 $this->drupalLogin($web_user);
91 // Prepare the field translations.
92 field_test_entity_info_translatable($this->entityTypeId, TRUE);
93 $entity = $this->container->get('entity_type.manager')
94 ->getStorage($this->entityTypeId)
96 $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
97 $field_name = $this->fieldStorage->getName();
99 // Store the field translations.
100 ksort($available_langcodes);
101 $entity->langcode->value = key($available_langcodes);
102 foreach ($available_langcodes as $langcode => $value) {
103 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
104 $translation->{$field_name}->value = $value + 1;
108 // Create a new revision.
110 "{$field_name}[0][value]" => $entity->{$field_name}->value,
113 $this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
115 // Check translation revisions.
116 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
117 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
121 * Check if the field translation attached to the entity revision identified
122 * by the passed arguments were correctly stored.
124 private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
125 $field_name = $this->fieldStorage->getName();
126 $entity = $this->container->get('entity_type.manager')
127 ->getStorage($this->entityTypeId)
128 ->loadRevision($revision_id);
129 foreach ($available_langcodes as $langcode => $value) {
130 $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1;
131 $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', ['@language' => $langcode, '@revision' => $entity->getRevisionId()]));