function ContentTranslationHandlerTest::testEntityFormSharedElements

Same name in other branches
  1. 8.9.x core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php \Drupal\Tests\content_translation\Kernel\ContentTranslationHandlerTest::testEntityFormSharedElements()
  2. 10 core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php \Drupal\Tests\content_translation\Kernel\ContentTranslationHandlerTest::testEntityFormSharedElements()
  3. 11.x core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php \Drupal\Tests\content_translation\Kernel\ContentTranslationHandlerTest::testEntityFormSharedElements()

Tests ContentTranslationHandler::entityFormSharedElements()

@dataProvider providerTestEntityFormSharedElements

@covers ::entityFormSharedElements @covers ::addTranslatabilityClue

Parameters

array $element: The element that will be altered.

bool $default_translation_affected: Whether or not only the default translation of the entity is affected.

bool $default_translation: Whether or not the entity is the default translation.

bool $translation_form: Whether or not the form is a translation form.

bool $is_submitted: Whether or not the form should be marked as submitted.

bool $is_rebuilding: Whether or not the form should be flagged for rebuild.

array $expected: The expected altered element.

bool $display_warning: Whether or not the warning message should be displayed.

File

core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php, line 110

Class

ContentTranslationHandlerTest
Tests the content translation handler.

Namespace

Drupal\Tests\content_translation\Kernel

Code

public function testEntityFormSharedElements(array $element, $default_translation_affected, $default_translation, $translation_form, $is_submitted, $is_rebuilding, array $expected, $display_warning) {
    $this->state
        ->set('entity_test.translation', TRUE);
    $this->state
        ->set('entity_test.untranslatable_fields.default_translation_affected', $default_translation_affected);
    $this->entityTypeBundleInfo
        ->clearCachedBundles();
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->entityTypeManager
        ->getStorage($this->entityTypeId)
        ->create();
    if (!$default_translation) {
        $entity = $entity->addTranslation($this->translationLangcode);
    }
    $entity->save();
    $form_object = $this->entityTypeManager
        ->getFormObject($this->entityTypeId, 'default');
    $form_object->setEntity($entity);
    $form_state = new FormState();
    $form_state->addBuildInfo('callback_object', $form_object)
        ->set([
        'content_translation',
        'translation_form',
    ], $translation_form);
    if ($is_submitted) {
        $form_state->setSubmitted();
    }
    $form_state->setRebuild($is_rebuilding);
    $handler = $this->entityTypeManager
        ->getHandler($this->entityTypeId, 'translation');
    $actual = $handler->entityFormSharedElements($element, $form_state, $element);
    $this->assertEquals($expected, $actual);
    if ($display_warning) {
        $messages = $this->messenger
            ->messagesByType('warning');
        $this->assertCount(1, $messages);
        $expected_message = sprintf('Fields that apply to all languages are hidden to avoid conflicting changes. <a href="%s">Edit them on the original language form</a>.', $entity->toUrl('edit-form')
            ->toString());
        $this->assertEquals($expected_message, reset($messages));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.