ContentTranslationHandlerTest.php 7.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
<?php

namespace Drupal\Tests\content_translation\Kernel;

use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;

/**
 * Tests the content translation handler.
 *
 * @group content_translation
 *
 * @coversDefaultClass \Drupal\content_translation\ContentTranslationHandler
 */
class ContentTranslationHandlerTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'content_translation',
    'entity_test',
    'language',
    'user',
  ];

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The entity type bundle information.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The ID of the entity type used in this test.
   *
   * @var string
   */
  protected $entityTypeId = 'entity_test_mul';

  /**
   * The ID of the translation language used in this test.
   *
   * @var string
   */
  protected $translationLangcode = 'af';

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->state = $this->container->get('state');
    $this->entityTypeBundleInfo = $this->container->get('entity_type.bundle.info');
    $this->entityTypeManager = $this->container->get('entity_type.manager');
    $this->messenger = $this->container->get('messenger');

    $this->installEntitySchema($this->entityTypeId);
    ConfigurableLanguage::createFromLangcode($this->translationLangcode)->save();
  }

  /**
   * Tests ContentTranslationHandler::entityFormSharedElements()
   *
   * @param array $element
   *   The element that will be altered.
   * @param bool $default_translation_affected
   *   Whether or not only the default translation of the entity is affected.
   * @param bool $default_translation
   *   Whether or not the entity is the default translation.
   * @param bool $translation_form
   *   Whether or not the form is a translation form.
   * @param array $expected
   *   The expected altered element.
   *
   * @dataProvider providerTestEntityFormSharedElements
   *
   * @covers ::entityFormSharedElements
   * @covers ::addTranslatabilityClue
   */
  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));
    }
  }

  /**
   * Returns test cases for ::testEntityFormSharedElements().
   *
   * @return array[]
   *   An array of test cases, each one containing the element to alter, the
   *   form state, and the expected altered element.
   */
  public function providerTestEntityFormSharedElements() {
    $tests = [];

    $element = [];
    $tests['empty'] = [
      'element' => $element,
      'default_translation_affected' => TRUE,
      'default_translation' => TRUE,
      'translation_form' => FALSE,
      'is_submitted' => TRUE,
      'is_rebuilding' => TRUE,
      'expected' => $element,
      'display_warning' => FALSE,
    ];

    $element = [
      '#type' => 'textfield',
    ];
    $tests['no-children'] = $tests['empty'];
    $tests['no-children']['element'] = $element;
    $tests['no-children']['expected'] = $element;

    $element = [
      'test' => [
        '#type' => 'textfield',
        '#multilingual' => TRUE,
      ],
    ];
    $tests['multilingual'] = $tests['empty'];
    $tests['multilingual']['element'] = $element;
    $tests['multilingual']['expected'] = $element;

    unset($element['test']['#multilingual']);
    $tests['no-title'] = $tests['empty'];
    $tests['no-title']['element'] = $element;
    $tests['no-title']['expected'] = $element;

    $element['test']['#title'] = 'Test';
    $tests['no-translatability-clue'] = $tests['empty'];
    $tests['no-translatability-clue']['element'] = $element;
    $tests['no-translatability-clue']['expected'] = $element;

    $expected = $element;
    $expected['test']['#title'] .= ' <span class="translation-entity-all-languages">(all languages)</span>';
    $tests['translatability-clue'] = $tests['no-translatability-clue'];
    $tests['translatability-clue']['default_translation_affected'] = FALSE;
    $tests['translatability-clue']['expected'] = $expected;

    $ignored_types = [
      'actions',
      'details',
      'hidden',
      'link',
      'token',
      'value',
      'vertical_tabs',
    ];
    foreach ($ignored_types as $ignored_type) {
      $element = [
        'test' => [
          '#type' => $ignored_type,
          '#title' => 'Test',
        ],
      ];
      $tests["ignore-$ignored_type"] = $tests['translatability-clue'];
      $tests["ignore-$ignored_type"]['element'] = $element;
      $tests["ignore-$ignored_type"]['expected'] = $element;
    }

    $tests['unknown-field'] = $tests['no-translatability-clue'];
    $tests['unknown-field']['default_translation'] = FALSE;

    $element = [
      'name' => [
        '#type' => 'textfield',
      ],
    ];
    $expected = $element;
    $expected['name']['#access'] = FALSE;
    $tests['hide-untranslatable'] = $tests['unknown-field'];
    $tests['hide-untranslatable']['element'] = $element;
    $tests['hide-untranslatable']['expected'] = $expected;

    $tests['is-rebuilding'] = $tests['hide-untranslatable'];
    $tests['is-rebuilding']['is_submitted'] = FALSE;

    $tests['display-warning'] = $tests['is-rebuilding'];
    $tests['display-warning']['is_rebuilding'] = FALSE;
    $tests['display-warning']['display_warning'] = TRUE;

    $tests['no-translation-form'] = $tests['no-translatability-clue'];
    $tests['no-translation-form']['translation_form'] = FALSE;

    return $tests;
  }

}