Chris@0: fieldName = mb_strtolower($this->randomMachineName() . '_field_name'); Chris@0: Chris@0: $field_storage = [ Chris@0: 'field_name' => $this->fieldName, Chris@0: 'entity_type' => $this->entityTypeId, Chris@0: 'type' => 'test_field', Chris@0: 'cardinality' => 4, Chris@0: ]; Chris@0: FieldStorageConfig::create($field_storage)->save(); Chris@0: $this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName); Chris@0: Chris@0: $field = [ Chris@0: 'field_storage' => $this->fieldStorage, Chris@0: 'bundle' => $this->entityTypeId, Chris@0: ]; Chris@0: FieldConfig::create($field)->save(); Chris@0: $this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName); Chris@0: Chris@0: entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default') Chris@0: ->setComponent($this->fieldName) Chris@0: ->save(); Chris@0: Chris@0: for ($i = 0; $i < 3; ++$i) { Chris@0: ConfigurableLanguage::create([ Chris@0: 'id' => 'l' . $i, Chris@0: 'label' => $this->randomString(), Chris@0: ])->save(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests field translations when creating a new revision. Chris@0: */ Chris@0: public function testFieldFormTranslationRevisions() { Chris@0: $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']); Chris@0: $this->drupalLogin($web_user); Chris@0: Chris@0: // Prepare the field translations. Chris@0: field_test_entity_info_translatable($this->entityTypeId, TRUE); Chris@0: $entity = $this->container->get('entity_type.manager') Chris@0: ->getStorage($this->entityTypeId) Chris@0: ->create(); Chris@0: $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages())); Chris@0: $field_name = $this->fieldStorage->getName(); Chris@0: Chris@0: // Store the field translations. Chris@0: ksort($available_langcodes); Chris@0: $entity->langcode->value = key($available_langcodes); Chris@0: foreach ($available_langcodes as $langcode => $value) { Chris@0: $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode); Chris@0: $translation->{$field_name}->value = $value + 1; Chris@0: } Chris@0: $entity->save(); Chris@0: Chris@0: // Create a new revision. Chris@0: $edit = [ Chris@0: "{$field_name}[0][value]" => $entity->{$field_name}->value, Chris@0: 'revision' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Check translation revisions. Chris@0: $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes); Chris@0: $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if the field translation attached to the entity revision identified Chris@0: * by the passed arguments were correctly stored. Chris@0: */ Chris@0: private function checkTranslationRevisions($id, $revision_id, $available_langcodes) { Chris@0: $field_name = $this->fieldStorage->getName(); Chris@0: $entity = $this->container->get('entity_type.manager') Chris@0: ->getStorage($this->entityTypeId) Chris@0: ->loadRevision($revision_id); Chris@0: foreach ($available_langcodes as $langcode => $value) { Chris@0: $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1; Chris@0: $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', ['@language' => $langcode, '@revision' => $entity->getRevisionId()])); Chris@0: } Chris@0: } Chris@0: Chris@0: }