You are here

public function ContentTranslationHandlerTest::testEntityFormSharedElements in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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.

array $expected: The expected altered element.

File

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

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));
  }
}